1 //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains code to generate C++ code for a matcher.
12 //===----------------------------------------------------------------------===//
14 #include "DAGISelMatcher.h"
15 #include "CodeGenDAGPatterns.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/FormattedStream.h"
28 // To reduce generated source code size.
30 OmitComments("omit-comments", cl::desc("Do not generate comments"),
34 class MatcherTableEmitter
{
35 const CodeGenDAGPatterns
&CGP
;
36 StringMap
<unsigned> NodePredicateMap
, PatternPredicateMap
;
37 std::vector
<std::string
> NodePredicates
, PatternPredicates
;
39 DenseMap
<const ComplexPattern
*, unsigned> ComplexPatternMap
;
40 std::vector
<const ComplexPattern
*> ComplexPatterns
;
43 DenseMap
<Record
*, unsigned> NodeXFormMap
;
44 std::vector
<Record
*> NodeXForms
;
47 MatcherTableEmitter(const CodeGenDAGPatterns
&cgp
) : CGP(cgp
) {}
49 unsigned EmitMatcherList(const Matcher
*N
, unsigned Indent
,
50 unsigned StartIdx
, formatted_raw_ostream
&OS
);
52 void EmitPredicateFunctions(formatted_raw_ostream
&OS
);
54 void EmitHistogram(const Matcher
*N
, formatted_raw_ostream
&OS
);
56 unsigned EmitMatcher(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
57 formatted_raw_ostream
&OS
);
59 unsigned getNodePredicate(StringRef PredName
) {
60 unsigned &Entry
= NodePredicateMap
[PredName
];
62 NodePredicates
.push_back(PredName
.str());
63 Entry
= NodePredicates
.size();
67 unsigned getPatternPredicate(StringRef PredName
) {
68 unsigned &Entry
= PatternPredicateMap
[PredName
];
70 PatternPredicates
.push_back(PredName
.str());
71 Entry
= PatternPredicates
.size();
76 unsigned getComplexPat(const ComplexPattern
&P
) {
77 unsigned &Entry
= ComplexPatternMap
[&P
];
79 ComplexPatterns
.push_back(&P
);
80 Entry
= ComplexPatterns
.size();
85 unsigned getNodeXFormID(Record
*Rec
) {
86 unsigned &Entry
= NodeXFormMap
[Rec
];
88 NodeXForms
.push_back(Rec
);
89 Entry
= NodeXForms
.size();
95 } // end anonymous namespace.
97 static unsigned GetVBRSize(unsigned Val
) {
98 if (Val
<= 127) return 1;
100 unsigned NumBytes
= 0;
108 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
110 static uint64_t EmitVBRValue(uint64_t Val
, raw_ostream
&OS
) {
116 uint64_t InVal
= Val
;
117 unsigned NumBytes
= 0;
119 OS
<< (Val
&127) << "|128,";
125 OS
<< "/*" << InVal
<< "*/";
130 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
131 /// the number of bytes emitted.
132 unsigned MatcherTableEmitter::
133 EmitMatcher(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
134 formatted_raw_ostream
&OS
) {
135 OS
.PadToColumn(Indent
*2);
137 switch (N
->getKind()) {
138 case Matcher::Scope
: {
139 const ScopeMatcher
*SM
= cast
<ScopeMatcher
>(N
);
140 assert(SM
->getNext() == 0 && "Shouldn't have next after scope");
142 unsigned StartIdx
= CurrentIdx
;
144 // Emit all of the children.
145 for (unsigned i
= 0, e
= SM
->getNumChildren(); i
!= e
; ++i
) {
151 OS
<< "/*" << CurrentIdx
<< "*/";
152 OS
.PadToColumn(Indent
*2) << "/*Scope*/ ";
154 OS
.PadToColumn(Indent
*2);
157 // We need to encode the child and the offset of the failure code before
158 // emitting either of them. Handle this by buffering the output into a
159 // string while we get the size. Unfortunately, the offset of the
160 // children depends on the VBR size of the child, so for large children we
161 // have to iterate a bit.
162 SmallString
<128> TmpBuf
;
163 unsigned ChildSize
= 0;
164 unsigned VBRSize
= 0;
166 VBRSize
= GetVBRSize(ChildSize
);
169 raw_svector_ostream
OS(TmpBuf
);
170 formatted_raw_ostream
FOS(OS
);
171 ChildSize
= EmitMatcherList(SM
->getChild(i
), Indent
+1,
172 CurrentIdx
+VBRSize
, FOS
);
173 } while (GetVBRSize(ChildSize
) != VBRSize
);
175 assert(ChildSize
!= 0 && "Should not have a zero-sized child!");
177 CurrentIdx
+= EmitVBRValue(ChildSize
, OS
);
179 OS
<< "/*->" << CurrentIdx
+ChildSize
<< "*/";
182 OS
.PadToColumn(CommentIndent
) << "// " << SM
->getNumChildren()
183 << " children in Scope";
186 OS
<< '\n' << TmpBuf
.str();
187 CurrentIdx
+= ChildSize
;
190 // Emit a zero as a sentinel indicating end of 'Scope'.
192 OS
<< "/*" << CurrentIdx
<< "*/";
193 OS
.PadToColumn(Indent
*2) << "0, ";
195 OS
<< "/*End of Scope*/";
197 return CurrentIdx
- StartIdx
+ 1;
200 case Matcher::RecordNode
:
201 OS
<< "OPC_RecordNode,";
203 OS
.PadToColumn(CommentIndent
) << "// #"
204 << cast
<RecordMatcher
>(N
)->getResultNo() << " = "
205 << cast
<RecordMatcher
>(N
)->getWhatFor();
209 case Matcher::RecordChild
:
210 OS
<< "OPC_RecordChild" << cast
<RecordChildMatcher
>(N
)->getChildNo()
213 OS
.PadToColumn(CommentIndent
) << "// #"
214 << cast
<RecordChildMatcher
>(N
)->getResultNo() << " = "
215 << cast
<RecordChildMatcher
>(N
)->getWhatFor();
219 case Matcher::RecordMemRef
:
220 OS
<< "OPC_RecordMemRef,\n";
223 case Matcher::CaptureFlagInput
:
224 OS
<< "OPC_CaptureFlagInput,\n";
227 case Matcher::MoveChild
:
228 OS
<< "OPC_MoveChild, " << cast
<MoveChildMatcher
>(N
)->getChildNo() << ",\n";
231 case Matcher::MoveParent
:
232 OS
<< "OPC_MoveParent,\n";
235 case Matcher::CheckSame
:
236 OS
<< "OPC_CheckSame, "
237 << cast
<CheckSameMatcher
>(N
)->getMatchNumber() << ",\n";
240 case Matcher::CheckPatternPredicate
: {
241 StringRef Pred
= cast
<CheckPatternPredicateMatcher
>(N
)->getPredicate();
242 OS
<< "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred
) << ',';
244 OS
.PadToColumn(CommentIndent
) << "// " << Pred
;
248 case Matcher::CheckPredicate
: {
249 StringRef Pred
= cast
<CheckPredicateMatcher
>(N
)->getPredicateName();
250 OS
<< "OPC_CheckPredicate, " << getNodePredicate(Pred
) << ',';
252 OS
.PadToColumn(CommentIndent
) << "// " << Pred
;
257 case Matcher::CheckOpcode
:
258 OS
<< "OPC_CheckOpcode, TARGET_OPCODE("
259 << cast
<CheckOpcodeMatcher
>(N
)->getOpcode().getEnumName() << "),\n";
262 case Matcher::SwitchOpcode
:
263 case Matcher::SwitchType
: {
264 unsigned StartIdx
= CurrentIdx
;
267 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
)) {
268 OS
<< "OPC_SwitchOpcode ";
269 NumCases
= SOM
->getNumCases();
271 OS
<< "OPC_SwitchType ";
272 NumCases
= cast
<SwitchTypeMatcher
>(N
)->getNumCases();
276 OS
<< "/*" << NumCases
<< " cases */";
280 // For each case we emit the size, then the opcode, then the matcher.
281 for (unsigned i
= 0, e
= NumCases
; i
!= e
; ++i
) {
282 const Matcher
*Child
;
284 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
)) {
285 Child
= SOM
->getCaseMatcher(i
);
286 IdxSize
= 2; // size of opcode in table is 2 bytes.
288 Child
= cast
<SwitchTypeMatcher
>(N
)->getCaseMatcher(i
);
289 IdxSize
= 1; // size of type in table is 1 byte.
292 // We need to encode the opcode and the offset of the case code before
293 // emitting the case code. Handle this by buffering the output into a
294 // string while we get the size. Unfortunately, the offset of the
295 // children depends on the VBR size of the child, so for large children we
296 // have to iterate a bit.
297 SmallString
<128> TmpBuf
;
298 unsigned ChildSize
= 0;
299 unsigned VBRSize
= 0;
301 VBRSize
= GetVBRSize(ChildSize
);
304 raw_svector_ostream
OS(TmpBuf
);
305 formatted_raw_ostream
FOS(OS
);
306 ChildSize
= EmitMatcherList(Child
, Indent
+1, CurrentIdx
+VBRSize
+IdxSize
,
308 } while (GetVBRSize(ChildSize
) != VBRSize
);
310 assert(ChildSize
!= 0 && "Should not have a zero-sized child!");
313 OS
.PadToColumn(Indent
*2);
315 OS
<< (isa
<SwitchOpcodeMatcher
>(N
) ?
316 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
320 CurrentIdx
+= EmitVBRValue(ChildSize
, OS
);
323 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
))
324 OS
<< "TARGET_OPCODE(" << SOM
->getCaseOpcode(i
).getEnumName() << "),";
326 OS
<< getEnumName(cast
<SwitchTypeMatcher
>(N
)->getCaseType(i
)) << ',';
328 CurrentIdx
+= IdxSize
;
331 OS
<< "// ->" << CurrentIdx
+ChildSize
;
334 CurrentIdx
+= ChildSize
;
337 // Emit the final zero to terminate the switch.
338 OS
.PadToColumn(Indent
*2) << "0, ";
340 OS
<< (isa
<SwitchOpcodeMatcher
>(N
) ?
341 "// EndSwitchOpcode" : "// EndSwitchType");
345 return CurrentIdx
-StartIdx
;
348 case Matcher::CheckType
:
349 assert(cast
<CheckTypeMatcher
>(N
)->getResNo() == 0 &&
350 "FIXME: Add support for CheckType of resno != 0");
351 OS
<< "OPC_CheckType, "
352 << getEnumName(cast
<CheckTypeMatcher
>(N
)->getType()) << ",\n";
355 case Matcher::CheckChildType
:
356 OS
<< "OPC_CheckChild"
357 << cast
<CheckChildTypeMatcher
>(N
)->getChildNo() << "Type, "
358 << getEnumName(cast
<CheckChildTypeMatcher
>(N
)->getType()) << ",\n";
361 case Matcher::CheckInteger
: {
362 OS
<< "OPC_CheckInteger, ";
363 unsigned Bytes
=1+EmitVBRValue(cast
<CheckIntegerMatcher
>(N
)->getValue(), OS
);
367 case Matcher::CheckCondCode
:
368 OS
<< "OPC_CheckCondCode, ISD::"
369 << cast
<CheckCondCodeMatcher
>(N
)->getCondCodeName() << ",\n";
372 case Matcher::CheckValueType
:
373 OS
<< "OPC_CheckValueType, MVT::"
374 << cast
<CheckValueTypeMatcher
>(N
)->getTypeName() << ",\n";
377 case Matcher::CheckComplexPat
: {
378 const CheckComplexPatMatcher
*CCPM
= cast
<CheckComplexPatMatcher
>(N
);
379 const ComplexPattern
&Pattern
= CCPM
->getPattern();
380 OS
<< "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern
) << ", /*#*/"
381 << CCPM
->getMatchNumber() << ',';
384 OS
.PadToColumn(CommentIndent
) << "// " << Pattern
.getSelectFunc();
385 OS
<< ":$" << CCPM
->getName();
386 for (unsigned i
= 0, e
= Pattern
.getNumOperands(); i
!= e
; ++i
)
387 OS
<< " #" << CCPM
->getFirstResult()+i
;
389 if (Pattern
.hasProperty(SDNPHasChain
))
390 OS
<< " + chain result";
396 case Matcher::CheckAndImm
: {
397 OS
<< "OPC_CheckAndImm, ";
398 unsigned Bytes
=1+EmitVBRValue(cast
<CheckAndImmMatcher
>(N
)->getValue(), OS
);
403 case Matcher::CheckOrImm
: {
404 OS
<< "OPC_CheckOrImm, ";
405 unsigned Bytes
= 1+EmitVBRValue(cast
<CheckOrImmMatcher
>(N
)->getValue(), OS
);
410 case Matcher::CheckFoldableChainNode
:
411 OS
<< "OPC_CheckFoldableChainNode,\n";
414 case Matcher::EmitInteger
: {
415 int64_t Val
= cast
<EmitIntegerMatcher
>(N
)->getValue();
416 OS
<< "OPC_EmitInteger, "
417 << getEnumName(cast
<EmitIntegerMatcher
>(N
)->getVT()) << ", ";
418 unsigned Bytes
= 2+EmitVBRValue(Val
, OS
);
422 case Matcher::EmitStringInteger
: {
423 const std::string
&Val
= cast
<EmitStringIntegerMatcher
>(N
)->getValue();
424 // These should always fit into one byte.
425 OS
<< "OPC_EmitInteger, "
426 << getEnumName(cast
<EmitStringIntegerMatcher
>(N
)->getVT()) << ", "
431 case Matcher::EmitRegister
:
432 OS
<< "OPC_EmitRegister, "
433 << getEnumName(cast
<EmitRegisterMatcher
>(N
)->getVT()) << ", ";
434 if (Record
*R
= cast
<EmitRegisterMatcher
>(N
)->getReg())
435 OS
<< getQualifiedName(R
) << ",\n";
439 OS
<< "/*zero_reg*/";
444 case Matcher::EmitConvertToTarget
:
445 OS
<< "OPC_EmitConvertToTarget, "
446 << cast
<EmitConvertToTargetMatcher
>(N
)->getSlot() << ",\n";
449 case Matcher::EmitMergeInputChains
: {
450 const EmitMergeInputChainsMatcher
*MN
=
451 cast
<EmitMergeInputChainsMatcher
>(N
);
453 // Handle the specialized forms OPC_EmitMergeInputChains1_0 and 1_1.
454 if (MN
->getNumNodes() == 1 && MN
->getNode(0) < 2) {
455 OS
<< "OPC_EmitMergeInputChains1_" << MN
->getNode(0) << ",\n";
459 OS
<< "OPC_EmitMergeInputChains, " << MN
->getNumNodes() << ", ";
460 for (unsigned i
= 0, e
= MN
->getNumNodes(); i
!= e
; ++i
)
461 OS
<< MN
->getNode(i
) << ", ";
463 return 2+MN
->getNumNodes();
465 case Matcher::EmitCopyToReg
:
466 OS
<< "OPC_EmitCopyToReg, "
467 << cast
<EmitCopyToRegMatcher
>(N
)->getSrcSlot() << ", "
468 << getQualifiedName(cast
<EmitCopyToRegMatcher
>(N
)->getDestPhysReg())
471 case Matcher::EmitNodeXForm
: {
472 const EmitNodeXFormMatcher
*XF
= cast
<EmitNodeXFormMatcher
>(N
);
473 OS
<< "OPC_EmitNodeXForm, " << getNodeXFormID(XF
->getNodeXForm()) << ", "
474 << XF
->getSlot() << ',';
476 OS
.PadToColumn(CommentIndent
) << "// "<<XF
->getNodeXForm()->getName();
481 case Matcher::EmitNode
:
482 case Matcher::MorphNodeTo
: {
483 const EmitNodeMatcherCommon
*EN
= cast
<EmitNodeMatcherCommon
>(N
);
484 OS
<< (isa
<EmitNodeMatcher
>(EN
) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
485 OS
<< ", TARGET_OPCODE(" << EN
->getOpcodeName() << "), 0";
487 if (EN
->hasChain()) OS
<< "|OPFL_Chain";
488 if (EN
->hasInFlag()) OS
<< "|OPFL_FlagInput";
489 if (EN
->hasOutFlag()) OS
<< "|OPFL_FlagOutput";
490 if (EN
->hasMemRefs()) OS
<< "|OPFL_MemRefs";
491 if (EN
->getNumFixedArityOperands() != -1)
492 OS
<< "|OPFL_Variadic" << EN
->getNumFixedArityOperands();
495 OS
.PadToColumn(Indent
*2+4) << EN
->getNumVTs();
499 for (unsigned i
= 0, e
= EN
->getNumVTs(); i
!= e
; ++i
)
500 OS
<< getEnumName(EN
->getVT(i
)) << ", ";
502 OS
<< EN
->getNumOperands();
506 unsigned NumOperandBytes
= 0;
507 for (unsigned i
= 0, e
= EN
->getNumOperands(); i
!= e
; ++i
)
508 NumOperandBytes
+= EmitVBRValue(EN
->getOperand(i
), OS
);
511 // Print the result #'s for EmitNode.
512 if (const EmitNodeMatcher
*E
= dyn_cast
<EmitNodeMatcher
>(EN
)) {
513 if (unsigned NumResults
= EN
->getNumVTs()) {
514 OS
.PadToColumn(CommentIndent
) << "// Results = ";
515 unsigned First
= E
->getFirstResultSlot();
516 for (unsigned i
= 0; i
!= NumResults
; ++i
)
517 OS
<< "#" << First
+i
<< " ";
522 if (const MorphNodeToMatcher
*SNT
= dyn_cast
<MorphNodeToMatcher
>(N
)) {
523 OS
.PadToColumn(Indent
*2) << "// Src: "
524 << *SNT
->getPattern().getSrcPattern() << " - Complexity = "
525 << SNT
->getPattern().getPatternComplexity(CGP
) << '\n';
526 OS
.PadToColumn(Indent
*2) << "// Dst: "
527 << *SNT
->getPattern().getDstPattern() << '\n';
532 return 6+EN
->getNumVTs()+NumOperandBytes
;
534 case Matcher::MarkFlagResults
: {
535 const MarkFlagResultsMatcher
*CFR
= cast
<MarkFlagResultsMatcher
>(N
);
536 OS
<< "OPC_MarkFlagResults, " << CFR
->getNumNodes() << ", ";
537 unsigned NumOperandBytes
= 0;
538 for (unsigned i
= 0, e
= CFR
->getNumNodes(); i
!= e
; ++i
)
539 NumOperandBytes
+= EmitVBRValue(CFR
->getNode(i
), OS
);
541 return 2+NumOperandBytes
;
543 case Matcher::CompleteMatch
: {
544 const CompleteMatchMatcher
*CM
= cast
<CompleteMatchMatcher
>(N
);
545 OS
<< "OPC_CompleteMatch, " << CM
->getNumResults() << ", ";
546 unsigned NumResultBytes
= 0;
547 for (unsigned i
= 0, e
= CM
->getNumResults(); i
!= e
; ++i
)
548 NumResultBytes
+= EmitVBRValue(CM
->getResult(i
), OS
);
551 OS
.PadToColumn(Indent
*2) << "// Src: "
552 << *CM
->getPattern().getSrcPattern() << " - Complexity = "
553 << CM
->getPattern().getPatternComplexity(CGP
) << '\n';
554 OS
.PadToColumn(Indent
*2) << "// Dst: "
555 << *CM
->getPattern().getDstPattern();
558 return 2 + NumResultBytes
;
561 assert(0 && "Unreachable");
565 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
566 unsigned MatcherTableEmitter::
567 EmitMatcherList(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
568 formatted_raw_ostream
&OS
) {
572 OS
<< "/*" << CurrentIdx
<< "*/";
573 unsigned MatcherSize
= EmitMatcher(N
, Indent
, CurrentIdx
, OS
);
575 CurrentIdx
+= MatcherSize
;
577 // If there are other nodes in this list, iterate to them, otherwise we're
584 void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream
&OS
) {
585 // Emit pattern predicates.
586 if (!PatternPredicates
.empty()) {
587 OS
<< "bool CheckPatternPredicate(unsigned PredNo) const {\n";
588 OS
<< " switch (PredNo) {\n";
589 OS
<< " default: assert(0 && \"Invalid predicate in table?\");\n";
590 for (unsigned i
= 0, e
= PatternPredicates
.size(); i
!= e
; ++i
)
591 OS
<< " case " << i
<< ": return " << PatternPredicates
[i
] << ";\n";
596 // Emit Node predicates.
597 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
598 StringMap
<TreePattern
*> PFsByName
;
600 for (CodeGenDAGPatterns::pf_iterator I
= CGP
.pf_begin(), E
= CGP
.pf_end();
602 PFsByName
[I
->first
->getName()] = I
->second
;
604 if (!NodePredicates
.empty()) {
605 OS
<< "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
606 OS
<< " switch (PredNo) {\n";
607 OS
<< " default: assert(0 && \"Invalid predicate in table?\");\n";
608 for (unsigned i
= 0, e
= NodePredicates
.size(); i
!= e
; ++i
) {
609 // FIXME: Storing this by name is horrible.
610 TreePattern
*P
=PFsByName
[NodePredicates
[i
].substr(strlen("Predicate_"))];
611 assert(P
&& "Unknown name?");
613 // Emit the predicate code corresponding to this pattern.
614 std::string Code
= P
->getRecord()->getValueAsCode("Predicate");
615 assert(!Code
.empty() && "No code in this predicate");
616 OS
<< " case " << i
<< ": { // " << NodePredicates
[i
] << '\n';
617 std::string ClassName
;
618 if (P
->getOnlyTree()->isLeaf())
619 ClassName
= "SDNode";
622 CGP
.getSDNodeInfo(P
->getOnlyTree()->getOperator()).getSDClassName();
623 if (ClassName
== "SDNode")
624 OS
<< " SDNode *N = Node;\n";
626 OS
<< " " << ClassName
<< "*N = cast<" << ClassName
<< ">(Node);\n";
627 OS
<< Code
<< "\n }\n";
633 // Emit CompletePattern matchers.
634 // FIXME: This should be const.
635 if (!ComplexPatterns
.empty()) {
636 OS
<< "bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,\n";
637 OS
<< " unsigned PatternNo,\n";
638 OS
<< " SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {\n";
639 OS
<< " unsigned NextRes = Result.size();\n";
640 OS
<< " switch (PatternNo) {\n";
641 OS
<< " default: assert(0 && \"Invalid pattern # in table?\");\n";
642 for (unsigned i
= 0, e
= ComplexPatterns
.size(); i
!= e
; ++i
) {
643 const ComplexPattern
&P
= *ComplexPatterns
[i
];
644 unsigned NumOps
= P
.getNumOperands();
646 if (P
.hasProperty(SDNPHasChain
))
647 ++NumOps
; // Get the chained node too.
649 OS
<< " case " << i
<< ":\n";
650 OS
<< " Result.resize(NextRes+" << NumOps
<< ");\n";
651 OS
<< " return " << P
.getSelectFunc();
654 // If the complex pattern wants the root of the match, pass it in as the
656 if (P
.hasProperty(SDNPWantRoot
))
659 // If the complex pattern wants the parent of the operand being matched,
660 // pass it in as the next argument.
661 if (P
.hasProperty(SDNPWantParent
))
665 for (unsigned i
= 0; i
!= NumOps
; ++i
)
666 OS
<< ", Result[NextRes+" << i
<< "].first";
674 // Emit SDNodeXForm handlers.
675 // FIXME: This should be const.
676 if (!NodeXForms
.empty()) {
677 OS
<< "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
678 OS
<< " switch (XFormNo) {\n";
679 OS
<< " default: assert(0 && \"Invalid xform # in table?\");\n";
681 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
682 for (unsigned i
= 0, e
= NodeXForms
.size(); i
!= e
; ++i
) {
683 const CodeGenDAGPatterns::NodeXForm
&Entry
=
684 CGP
.getSDNodeTransform(NodeXForms
[i
]);
686 Record
*SDNode
= Entry
.first
;
687 const std::string
&Code
= Entry
.second
;
689 OS
<< " case " << i
<< ": { ";
691 OS
<< "// " << NodeXForms
[i
]->getName();
694 std::string ClassName
= CGP
.getSDNodeInfo(SDNode
).getSDClassName();
695 if (ClassName
== "SDNode")
696 OS
<< " SDNode *N = V.getNode();\n";
698 OS
<< " " << ClassName
<< " *N = cast<" << ClassName
699 << ">(V.getNode());\n";
700 OS
<< Code
<< "\n }\n";
707 static void BuildHistogram(const Matcher
*M
, std::vector
<unsigned> &OpcodeFreq
){
708 for (; M
!= 0; M
= M
->getNext()) {
710 if (unsigned(M
->getKind()) >= OpcodeFreq
.size())
711 OpcodeFreq
.resize(M
->getKind()+1);
712 OpcodeFreq
[M
->getKind()]++;
714 // Handle recursive nodes.
715 if (const ScopeMatcher
*SM
= dyn_cast
<ScopeMatcher
>(M
)) {
716 for (unsigned i
= 0, e
= SM
->getNumChildren(); i
!= e
; ++i
)
717 BuildHistogram(SM
->getChild(i
), OpcodeFreq
);
718 } else if (const SwitchOpcodeMatcher
*SOM
=
719 dyn_cast
<SwitchOpcodeMatcher
>(M
)) {
720 for (unsigned i
= 0, e
= SOM
->getNumCases(); i
!= e
; ++i
)
721 BuildHistogram(SOM
->getCaseMatcher(i
), OpcodeFreq
);
722 } else if (const SwitchTypeMatcher
*STM
= dyn_cast
<SwitchTypeMatcher
>(M
)) {
723 for (unsigned i
= 0, e
= STM
->getNumCases(); i
!= e
; ++i
)
724 BuildHistogram(STM
->getCaseMatcher(i
), OpcodeFreq
);
729 void MatcherTableEmitter::EmitHistogram(const Matcher
*M
,
730 formatted_raw_ostream
&OS
) {
734 std::vector
<unsigned> OpcodeFreq
;
735 BuildHistogram(M
, OpcodeFreq
);
737 OS
<< " // Opcode Histogram:\n";
738 for (unsigned i
= 0, e
= OpcodeFreq
.size(); i
!= e
; ++i
) {
740 switch ((Matcher::KindTy
)i
) {
741 case Matcher::Scope
: OS
<< "OPC_Scope"; break;
742 case Matcher::RecordNode
: OS
<< "OPC_RecordNode"; break;
743 case Matcher::RecordChild
: OS
<< "OPC_RecordChild"; break;
744 case Matcher::RecordMemRef
: OS
<< "OPC_RecordMemRef"; break;
745 case Matcher::CaptureFlagInput
: OS
<< "OPC_CaptureFlagInput"; break;
746 case Matcher::MoveChild
: OS
<< "OPC_MoveChild"; break;
747 case Matcher::MoveParent
: OS
<< "OPC_MoveParent"; break;
748 case Matcher::CheckSame
: OS
<< "OPC_CheckSame"; break;
749 case Matcher::CheckPatternPredicate
:
750 OS
<< "OPC_CheckPatternPredicate"; break;
751 case Matcher::CheckPredicate
: OS
<< "OPC_CheckPredicate"; break;
752 case Matcher::CheckOpcode
: OS
<< "OPC_CheckOpcode"; break;
753 case Matcher::SwitchOpcode
: OS
<< "OPC_SwitchOpcode"; break;
754 case Matcher::CheckType
: OS
<< "OPC_CheckType"; break;
755 case Matcher::SwitchType
: OS
<< "OPC_SwitchType"; break;
756 case Matcher::CheckChildType
: OS
<< "OPC_CheckChildType"; break;
757 case Matcher::CheckInteger
: OS
<< "OPC_CheckInteger"; break;
758 case Matcher::CheckCondCode
: OS
<< "OPC_CheckCondCode"; break;
759 case Matcher::CheckValueType
: OS
<< "OPC_CheckValueType"; break;
760 case Matcher::CheckComplexPat
: OS
<< "OPC_CheckComplexPat"; break;
761 case Matcher::CheckAndImm
: OS
<< "OPC_CheckAndImm"; break;
762 case Matcher::CheckOrImm
: OS
<< "OPC_CheckOrImm"; break;
763 case Matcher::CheckFoldableChainNode
:
764 OS
<< "OPC_CheckFoldableChainNode"; break;
765 case Matcher::EmitInteger
: OS
<< "OPC_EmitInteger"; break;
766 case Matcher::EmitStringInteger
: OS
<< "OPC_EmitStringInteger"; break;
767 case Matcher::EmitRegister
: OS
<< "OPC_EmitRegister"; break;
768 case Matcher::EmitConvertToTarget
: OS
<< "OPC_EmitConvertToTarget"; break;
769 case Matcher::EmitMergeInputChains
: OS
<< "OPC_EmitMergeInputChains"; break;
770 case Matcher::EmitCopyToReg
: OS
<< "OPC_EmitCopyToReg"; break;
771 case Matcher::EmitNode
: OS
<< "OPC_EmitNode"; break;
772 case Matcher::MorphNodeTo
: OS
<< "OPC_MorphNodeTo"; break;
773 case Matcher::EmitNodeXForm
: OS
<< "OPC_EmitNodeXForm"; break;
774 case Matcher::MarkFlagResults
: OS
<< "OPC_MarkFlagResults"; break;
775 case Matcher::CompleteMatch
: OS
<< "OPC_CompleteMatch"; break;
778 OS
.PadToColumn(40) << " = " << OpcodeFreq
[i
] << '\n';
784 void llvm::EmitMatcherTable(const Matcher
*TheMatcher
,
785 const CodeGenDAGPatterns
&CGP
, raw_ostream
&O
) {
786 formatted_raw_ostream
OS(O
);
788 OS
<< "// The main instruction selector code.\n";
789 OS
<< "SDNode *SelectCode(SDNode *N) {\n";
791 MatcherTableEmitter
MatcherEmitter(CGP
);
793 OS
<< " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
794 OS
<< " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
795 OS
<< " static const unsigned char MatcherTable[] = {\n";
796 unsigned TotalSize
= MatcherEmitter
.EmitMatcherList(TheMatcher
, 5, 0, OS
);
797 OS
<< " 0\n }; // Total Array size is " << (TotalSize
+1) << " bytes\n\n";
799 MatcherEmitter
.EmitHistogram(TheMatcher
, OS
);
801 OS
<< " #undef TARGET_OPCODE\n";
802 OS
<< " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
805 // Next up, emit the function for node and pattern predicates:
806 MatcherEmitter
.EmitPredicateFunctions(OS
);