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
)
50 unsigned EmitMatcherList(const Matcher
*N
, unsigned Indent
,
51 unsigned StartIdx
, formatted_raw_ostream
&OS
);
53 void EmitPredicateFunctions(formatted_raw_ostream
&OS
);
55 void EmitHistogram(const Matcher
*N
, formatted_raw_ostream
&OS
);
57 unsigned EmitMatcher(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
58 formatted_raw_ostream
&OS
);
60 unsigned getNodePredicate(StringRef PredName
) {
61 unsigned &Entry
= NodePredicateMap
[PredName
];
63 NodePredicates
.push_back(PredName
.str());
64 Entry
= NodePredicates
.size();
68 unsigned getPatternPredicate(StringRef PredName
) {
69 unsigned &Entry
= PatternPredicateMap
[PredName
];
71 PatternPredicates
.push_back(PredName
.str());
72 Entry
= PatternPredicates
.size();
77 unsigned getComplexPat(const ComplexPattern
&P
) {
78 unsigned &Entry
= ComplexPatternMap
[&P
];
80 ComplexPatterns
.push_back(&P
);
81 Entry
= ComplexPatterns
.size();
86 unsigned getNodeXFormID(Record
*Rec
) {
87 unsigned &Entry
= NodeXFormMap
[Rec
];
89 NodeXForms
.push_back(Rec
);
90 Entry
= NodeXForms
.size();
96 } // end anonymous namespace.
98 static unsigned GetVBRSize(unsigned Val
) {
99 if (Val
<= 127) return 1;
101 unsigned NumBytes
= 0;
109 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
111 static uint64_t EmitVBRValue(uint64_t Val
, raw_ostream
&OS
) {
117 uint64_t InVal
= Val
;
118 unsigned NumBytes
= 0;
120 OS
<< (Val
&127) << "|128,";
126 OS
<< "/*" << InVal
<< "*/";
131 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
132 /// the number of bytes emitted.
133 unsigned MatcherTableEmitter::
134 EmitMatcher(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
135 formatted_raw_ostream
&OS
) {
136 OS
.PadToColumn(Indent
*2);
138 switch (N
->getKind()) {
139 case Matcher::Scope
: {
140 const ScopeMatcher
*SM
= cast
<ScopeMatcher
>(N
);
141 assert(SM
->getNext() == 0 && "Shouldn't have next after scope");
143 unsigned StartIdx
= CurrentIdx
;
145 // Emit all of the children.
146 for (unsigned i
= 0, e
= SM
->getNumChildren(); i
!= e
; ++i
) {
152 OS
<< "/*" << CurrentIdx
<< "*/";
153 OS
.PadToColumn(Indent
*2) << "/*Scope*/ ";
155 OS
.PadToColumn(Indent
*2);
158 // We need to encode the child and the offset of the failure code before
159 // emitting either of them. Handle this by buffering the output into a
160 // string while we get the size. Unfortunately, the offset of the
161 // children depends on the VBR size of the child, so for large children we
162 // have to iterate a bit.
163 SmallString
<128> TmpBuf
;
164 unsigned ChildSize
= 0;
165 unsigned VBRSize
= 0;
167 VBRSize
= GetVBRSize(ChildSize
);
170 raw_svector_ostream
OS(TmpBuf
);
171 formatted_raw_ostream
FOS(OS
);
172 ChildSize
= EmitMatcherList(SM
->getChild(i
), Indent
+1,
173 CurrentIdx
+VBRSize
, FOS
);
174 } while (GetVBRSize(ChildSize
) != VBRSize
);
176 assert(ChildSize
!= 0 && "Should not have a zero-sized child!");
178 CurrentIdx
+= EmitVBRValue(ChildSize
, OS
);
180 OS
<< "/*->" << CurrentIdx
+ChildSize
<< "*/";
183 OS
.PadToColumn(CommentIndent
) << "// " << SM
->getNumChildren()
184 << " children in Scope";
187 OS
<< '\n' << TmpBuf
.str();
188 CurrentIdx
+= ChildSize
;
191 // Emit a zero as a sentinel indicating end of 'Scope'.
193 OS
<< "/*" << CurrentIdx
<< "*/";
194 OS
.PadToColumn(Indent
*2) << "0, ";
196 OS
<< "/*End of Scope*/";
198 return CurrentIdx
- StartIdx
+ 1;
201 case Matcher::RecordNode
:
202 OS
<< "OPC_RecordNode,";
204 OS
.PadToColumn(CommentIndent
) << "// #"
205 << cast
<RecordMatcher
>(N
)->getResultNo() << " = "
206 << cast
<RecordMatcher
>(N
)->getWhatFor();
210 case Matcher::RecordChild
:
211 OS
<< "OPC_RecordChild" << cast
<RecordChildMatcher
>(N
)->getChildNo()
214 OS
.PadToColumn(CommentIndent
) << "// #"
215 << cast
<RecordChildMatcher
>(N
)->getResultNo() << " = "
216 << cast
<RecordChildMatcher
>(N
)->getWhatFor();
220 case Matcher::RecordMemRef
:
221 OS
<< "OPC_RecordMemRef,\n";
224 case Matcher::CaptureGlueInput
:
225 OS
<< "OPC_CaptureGlueInput,\n";
228 case Matcher::MoveChild
:
229 OS
<< "OPC_MoveChild, " << cast
<MoveChildMatcher
>(N
)->getChildNo() << ",\n";
232 case Matcher::MoveParent
:
233 OS
<< "OPC_MoveParent,\n";
236 case Matcher::CheckSame
:
237 OS
<< "OPC_CheckSame, "
238 << cast
<CheckSameMatcher
>(N
)->getMatchNumber() << ",\n";
241 case Matcher::CheckPatternPredicate
: {
242 StringRef Pred
= cast
<CheckPatternPredicateMatcher
>(N
)->getPredicate();
243 OS
<< "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred
) << ',';
245 OS
.PadToColumn(CommentIndent
) << "// " << Pred
;
249 case Matcher::CheckPredicate
: {
250 StringRef Pred
= cast
<CheckPredicateMatcher
>(N
)->getPredicateName();
251 OS
<< "OPC_CheckPredicate, " << getNodePredicate(Pred
) << ',';
253 OS
.PadToColumn(CommentIndent
) << "// " << Pred
;
258 case Matcher::CheckOpcode
:
259 OS
<< "OPC_CheckOpcode, TARGET_VAL("
260 << cast
<CheckOpcodeMatcher
>(N
)->getOpcode().getEnumName() << "),\n";
263 case Matcher::SwitchOpcode
:
264 case Matcher::SwitchType
: {
265 unsigned StartIdx
= CurrentIdx
;
268 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
)) {
269 OS
<< "OPC_SwitchOpcode ";
270 NumCases
= SOM
->getNumCases();
272 OS
<< "OPC_SwitchType ";
273 NumCases
= cast
<SwitchTypeMatcher
>(N
)->getNumCases();
277 OS
<< "/*" << NumCases
<< " cases */";
281 // For each case we emit the size, then the opcode, then the matcher.
282 for (unsigned i
= 0, e
= NumCases
; i
!= e
; ++i
) {
283 const Matcher
*Child
;
285 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
)) {
286 Child
= SOM
->getCaseMatcher(i
);
287 IdxSize
= 2; // size of opcode in table is 2 bytes.
289 Child
= cast
<SwitchTypeMatcher
>(N
)->getCaseMatcher(i
);
290 IdxSize
= 1; // size of type in table is 1 byte.
293 // We need to encode the opcode and the offset of the case code before
294 // emitting the case code. Handle this by buffering the output into a
295 // string while we get the size. Unfortunately, the offset of the
296 // children depends on the VBR size of the child, so for large children we
297 // have to iterate a bit.
298 SmallString
<128> TmpBuf
;
299 unsigned ChildSize
= 0;
300 unsigned VBRSize
= 0;
302 VBRSize
= GetVBRSize(ChildSize
);
305 raw_svector_ostream
OS(TmpBuf
);
306 formatted_raw_ostream
FOS(OS
);
307 ChildSize
= EmitMatcherList(Child
, Indent
+1, CurrentIdx
+VBRSize
+IdxSize
,
309 } while (GetVBRSize(ChildSize
) != VBRSize
);
311 assert(ChildSize
!= 0 && "Should not have a zero-sized child!");
314 OS
.PadToColumn(Indent
*2);
316 OS
<< (isa
<SwitchOpcodeMatcher
>(N
) ?
317 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
321 CurrentIdx
+= EmitVBRValue(ChildSize
, OS
);
324 if (const SwitchOpcodeMatcher
*SOM
= dyn_cast
<SwitchOpcodeMatcher
>(N
))
325 OS
<< "TARGET_VAL(" << SOM
->getCaseOpcode(i
).getEnumName() << "),";
327 OS
<< getEnumName(cast
<SwitchTypeMatcher
>(N
)->getCaseType(i
)) << ',';
329 CurrentIdx
+= IdxSize
;
332 OS
<< "// ->" << CurrentIdx
+ChildSize
;
335 CurrentIdx
+= ChildSize
;
338 // Emit the final zero to terminate the switch.
339 OS
.PadToColumn(Indent
*2) << "0, ";
341 OS
<< (isa
<SwitchOpcodeMatcher
>(N
) ?
342 "// EndSwitchOpcode" : "// EndSwitchType");
346 return CurrentIdx
-StartIdx
;
349 case Matcher::CheckType
:
350 assert(cast
<CheckTypeMatcher
>(N
)->getResNo() == 0 &&
351 "FIXME: Add support for CheckType of resno != 0");
352 OS
<< "OPC_CheckType, "
353 << getEnumName(cast
<CheckTypeMatcher
>(N
)->getType()) << ",\n";
356 case Matcher::CheckChildType
:
357 OS
<< "OPC_CheckChild"
358 << cast
<CheckChildTypeMatcher
>(N
)->getChildNo() << "Type, "
359 << getEnumName(cast
<CheckChildTypeMatcher
>(N
)->getType()) << ",\n";
362 case Matcher::CheckInteger
: {
363 OS
<< "OPC_CheckInteger, ";
364 unsigned Bytes
=1+EmitVBRValue(cast
<CheckIntegerMatcher
>(N
)->getValue(), OS
);
368 case Matcher::CheckCondCode
:
369 OS
<< "OPC_CheckCondCode, ISD::"
370 << cast
<CheckCondCodeMatcher
>(N
)->getCondCodeName() << ",\n";
373 case Matcher::CheckValueType
:
374 OS
<< "OPC_CheckValueType, MVT::"
375 << cast
<CheckValueTypeMatcher
>(N
)->getTypeName() << ",\n";
378 case Matcher::CheckComplexPat
: {
379 const CheckComplexPatMatcher
*CCPM
= cast
<CheckComplexPatMatcher
>(N
);
380 const ComplexPattern
&Pattern
= CCPM
->getPattern();
381 OS
<< "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern
) << ", /*#*/"
382 << CCPM
->getMatchNumber() << ',';
385 OS
.PadToColumn(CommentIndent
) << "// " << Pattern
.getSelectFunc();
386 OS
<< ":$" << CCPM
->getName();
387 for (unsigned i
= 0, e
= Pattern
.getNumOperands(); i
!= e
; ++i
)
388 OS
<< " #" << CCPM
->getFirstResult()+i
;
390 if (Pattern
.hasProperty(SDNPHasChain
))
391 OS
<< " + chain result";
397 case Matcher::CheckAndImm
: {
398 OS
<< "OPC_CheckAndImm, ";
399 unsigned Bytes
=1+EmitVBRValue(cast
<CheckAndImmMatcher
>(N
)->getValue(), OS
);
404 case Matcher::CheckOrImm
: {
405 OS
<< "OPC_CheckOrImm, ";
406 unsigned Bytes
= 1+EmitVBRValue(cast
<CheckOrImmMatcher
>(N
)->getValue(), OS
);
411 case Matcher::CheckFoldableChainNode
:
412 OS
<< "OPC_CheckFoldableChainNode,\n";
415 case Matcher::EmitInteger
: {
416 int64_t Val
= cast
<EmitIntegerMatcher
>(N
)->getValue();
417 OS
<< "OPC_EmitInteger, "
418 << getEnumName(cast
<EmitIntegerMatcher
>(N
)->getVT()) << ", ";
419 unsigned Bytes
= 2+EmitVBRValue(Val
, OS
);
423 case Matcher::EmitStringInteger
: {
424 const std::string
&Val
= cast
<EmitStringIntegerMatcher
>(N
)->getValue();
425 // These should always fit into one byte.
426 OS
<< "OPC_EmitInteger, "
427 << getEnumName(cast
<EmitStringIntegerMatcher
>(N
)->getVT()) << ", "
432 case Matcher::EmitRegister
: {
433 const EmitRegisterMatcher
*Matcher
= cast
<EmitRegisterMatcher
>(N
);
434 const CodeGenRegister
*Reg
= Matcher
->getReg();
435 // If the enum value of the register is larger than one byte can handle,
436 // use EmitRegister2.
437 if (Reg
&& Reg
->EnumValue
> 255) {
438 OS
<< "OPC_EmitRegister2, " << getEnumName(Matcher
->getVT()) << ", ";
439 OS
<< "TARGET_VAL(" << getQualifiedName(Reg
->TheDef
) << "),\n";
442 OS
<< "OPC_EmitRegister, " << getEnumName(Matcher
->getVT()) << ", ";
444 OS
<< getQualifiedName(Reg
->TheDef
) << ",\n";
448 OS
<< "/*zero_reg*/";
455 case Matcher::EmitConvertToTarget
:
456 OS
<< "OPC_EmitConvertToTarget, "
457 << cast
<EmitConvertToTargetMatcher
>(N
)->getSlot() << ",\n";
460 case Matcher::EmitMergeInputChains
: {
461 const EmitMergeInputChainsMatcher
*MN
=
462 cast
<EmitMergeInputChainsMatcher
>(N
);
464 // Handle the specialized forms OPC_EmitMergeInputChains1_0 and 1_1.
465 if (MN
->getNumNodes() == 1 && MN
->getNode(0) < 2) {
466 OS
<< "OPC_EmitMergeInputChains1_" << MN
->getNode(0) << ",\n";
470 OS
<< "OPC_EmitMergeInputChains, " << MN
->getNumNodes() << ", ";
471 for (unsigned i
= 0, e
= MN
->getNumNodes(); i
!= e
; ++i
)
472 OS
<< MN
->getNode(i
) << ", ";
474 return 2+MN
->getNumNodes();
476 case Matcher::EmitCopyToReg
:
477 OS
<< "OPC_EmitCopyToReg, "
478 << cast
<EmitCopyToRegMatcher
>(N
)->getSrcSlot() << ", "
479 << getQualifiedName(cast
<EmitCopyToRegMatcher
>(N
)->getDestPhysReg())
482 case Matcher::EmitNodeXForm
: {
483 const EmitNodeXFormMatcher
*XF
= cast
<EmitNodeXFormMatcher
>(N
);
484 OS
<< "OPC_EmitNodeXForm, " << getNodeXFormID(XF
->getNodeXForm()) << ", "
485 << XF
->getSlot() << ',';
487 OS
.PadToColumn(CommentIndent
) << "// "<<XF
->getNodeXForm()->getName();
492 case Matcher::EmitNode
:
493 case Matcher::MorphNodeTo
: {
494 const EmitNodeMatcherCommon
*EN
= cast
<EmitNodeMatcherCommon
>(N
);
495 OS
<< (isa
<EmitNodeMatcher
>(EN
) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
496 OS
<< ", TARGET_VAL(" << EN
->getOpcodeName() << "), 0";
498 if (EN
->hasChain()) OS
<< "|OPFL_Chain";
499 if (EN
->hasInFlag()) OS
<< "|OPFL_GlueInput";
500 if (EN
->hasOutFlag()) OS
<< "|OPFL_GlueOutput";
501 if (EN
->hasMemRefs()) OS
<< "|OPFL_MemRefs";
502 if (EN
->getNumFixedArityOperands() != -1)
503 OS
<< "|OPFL_Variadic" << EN
->getNumFixedArityOperands();
506 OS
.PadToColumn(Indent
*2+4) << EN
->getNumVTs();
510 for (unsigned i
= 0, e
= EN
->getNumVTs(); i
!= e
; ++i
)
511 OS
<< getEnumName(EN
->getVT(i
)) << ", ";
513 OS
<< EN
->getNumOperands();
517 unsigned NumOperandBytes
= 0;
518 for (unsigned i
= 0, e
= EN
->getNumOperands(); i
!= e
; ++i
)
519 NumOperandBytes
+= EmitVBRValue(EN
->getOperand(i
), OS
);
522 // Print the result #'s for EmitNode.
523 if (const EmitNodeMatcher
*E
= dyn_cast
<EmitNodeMatcher
>(EN
)) {
524 if (unsigned NumResults
= EN
->getNumVTs()) {
525 OS
.PadToColumn(CommentIndent
) << "// Results = ";
526 unsigned First
= E
->getFirstResultSlot();
527 for (unsigned i
= 0; i
!= NumResults
; ++i
)
528 OS
<< "#" << First
+i
<< " ";
533 if (const MorphNodeToMatcher
*SNT
= dyn_cast
<MorphNodeToMatcher
>(N
)) {
534 OS
.PadToColumn(Indent
*2) << "// Src: "
535 << *SNT
->getPattern().getSrcPattern() << " - Complexity = "
536 << SNT
->getPattern().getPatternComplexity(CGP
) << '\n';
537 OS
.PadToColumn(Indent
*2) << "// Dst: "
538 << *SNT
->getPattern().getDstPattern() << '\n';
543 return 6+EN
->getNumVTs()+NumOperandBytes
;
545 case Matcher::MarkGlueResults
: {
546 const MarkGlueResultsMatcher
*CFR
= cast
<MarkGlueResultsMatcher
>(N
);
547 OS
<< "OPC_MarkGlueResults, " << CFR
->getNumNodes() << ", ";
548 unsigned NumOperandBytes
= 0;
549 for (unsigned i
= 0, e
= CFR
->getNumNodes(); i
!= e
; ++i
)
550 NumOperandBytes
+= EmitVBRValue(CFR
->getNode(i
), OS
);
552 return 2+NumOperandBytes
;
554 case Matcher::CompleteMatch
: {
555 const CompleteMatchMatcher
*CM
= cast
<CompleteMatchMatcher
>(N
);
556 OS
<< "OPC_CompleteMatch, " << CM
->getNumResults() << ", ";
557 unsigned NumResultBytes
= 0;
558 for (unsigned i
= 0, e
= CM
->getNumResults(); i
!= e
; ++i
)
559 NumResultBytes
+= EmitVBRValue(CM
->getResult(i
), OS
);
562 OS
.PadToColumn(Indent
*2) << "// Src: "
563 << *CM
->getPattern().getSrcPattern() << " - Complexity = "
564 << CM
->getPattern().getPatternComplexity(CGP
) << '\n';
565 OS
.PadToColumn(Indent
*2) << "// Dst: "
566 << *CM
->getPattern().getDstPattern();
569 return 2 + NumResultBytes
;
572 assert(0 && "Unreachable");
576 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
577 unsigned MatcherTableEmitter::
578 EmitMatcherList(const Matcher
*N
, unsigned Indent
, unsigned CurrentIdx
,
579 formatted_raw_ostream
&OS
) {
583 OS
<< "/*" << CurrentIdx
<< "*/";
584 unsigned MatcherSize
= EmitMatcher(N
, Indent
, CurrentIdx
, OS
);
586 CurrentIdx
+= MatcherSize
;
588 // If there are other nodes in this list, iterate to them, otherwise we're
595 void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream
&OS
) {
596 // Emit pattern predicates.
597 if (!PatternPredicates
.empty()) {
598 OS
<< "bool CheckPatternPredicate(unsigned PredNo) const {\n";
599 OS
<< " switch (PredNo) {\n";
600 OS
<< " default: assert(0 && \"Invalid predicate in table?\");\n";
601 for (unsigned i
= 0, e
= PatternPredicates
.size(); i
!= e
; ++i
)
602 OS
<< " case " << i
<< ": return " << PatternPredicates
[i
] << ";\n";
607 // Emit Node predicates.
608 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
609 StringMap
<TreePattern
*> PFsByName
;
611 for (CodeGenDAGPatterns::pf_iterator I
= CGP
.pf_begin(), E
= CGP
.pf_end();
613 PFsByName
[I
->first
->getName()] = I
->second
;
615 if (!NodePredicates
.empty()) {
616 OS
<< "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
617 OS
<< " switch (PredNo) {\n";
618 OS
<< " default: assert(0 && \"Invalid predicate in table?\");\n";
619 for (unsigned i
= 0, e
= NodePredicates
.size(); i
!= e
; ++i
) {
620 // FIXME: Storing this by name is horrible.
621 TreePattern
*P
=PFsByName
[NodePredicates
[i
].substr(strlen("Predicate_"))];
622 assert(P
&& "Unknown name?");
624 // Emit the predicate code corresponding to this pattern.
625 std::string Code
= P
->getRecord()->getValueAsCode("Predicate");
626 assert(!Code
.empty() && "No code in this predicate");
627 OS
<< " case " << i
<< ": { // " << NodePredicates
[i
] << '\n';
628 std::string ClassName
;
629 if (P
->getOnlyTree()->isLeaf())
630 ClassName
= "SDNode";
633 CGP
.getSDNodeInfo(P
->getOnlyTree()->getOperator()).getSDClassName();
634 if (ClassName
== "SDNode")
635 OS
<< " SDNode *N = Node;\n";
637 OS
<< " " << ClassName
<< "*N = cast<" << ClassName
<< ">(Node);\n";
638 OS
<< Code
<< "\n }\n";
644 // Emit CompletePattern matchers.
645 // FIXME: This should be const.
646 if (!ComplexPatterns
.empty()) {
647 OS
<< "bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,\n";
648 OS
<< " unsigned PatternNo,\n";
649 OS
<< " SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {\n";
650 OS
<< " unsigned NextRes = Result.size();\n";
651 OS
<< " switch (PatternNo) {\n";
652 OS
<< " default: assert(0 && \"Invalid pattern # in table?\");\n";
653 for (unsigned i
= 0, e
= ComplexPatterns
.size(); i
!= e
; ++i
) {
654 const ComplexPattern
&P
= *ComplexPatterns
[i
];
655 unsigned NumOps
= P
.getNumOperands();
657 if (P
.hasProperty(SDNPHasChain
))
658 ++NumOps
; // Get the chained node too.
660 OS
<< " case " << i
<< ":\n";
661 OS
<< " Result.resize(NextRes+" << NumOps
<< ");\n";
662 OS
<< " return " << P
.getSelectFunc();
665 // If the complex pattern wants the root of the match, pass it in as the
667 if (P
.hasProperty(SDNPWantRoot
))
670 // If the complex pattern wants the parent of the operand being matched,
671 // pass it in as the next argument.
672 if (P
.hasProperty(SDNPWantParent
))
676 for (unsigned i
= 0; i
!= NumOps
; ++i
)
677 OS
<< ", Result[NextRes+" << i
<< "].first";
685 // Emit SDNodeXForm handlers.
686 // FIXME: This should be const.
687 if (!NodeXForms
.empty()) {
688 OS
<< "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
689 OS
<< " switch (XFormNo) {\n";
690 OS
<< " default: assert(0 && \"Invalid xform # in table?\");\n";
692 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
693 for (unsigned i
= 0, e
= NodeXForms
.size(); i
!= e
; ++i
) {
694 const CodeGenDAGPatterns::NodeXForm
&Entry
=
695 CGP
.getSDNodeTransform(NodeXForms
[i
]);
697 Record
*SDNode
= Entry
.first
;
698 const std::string
&Code
= Entry
.second
;
700 OS
<< " case " << i
<< ": { ";
702 OS
<< "// " << NodeXForms
[i
]->getName();
705 std::string ClassName
= CGP
.getSDNodeInfo(SDNode
).getSDClassName();
706 if (ClassName
== "SDNode")
707 OS
<< " SDNode *N = V.getNode();\n";
709 OS
<< " " << ClassName
<< " *N = cast<" << ClassName
710 << ">(V.getNode());\n";
711 OS
<< Code
<< "\n }\n";
718 static void BuildHistogram(const Matcher
*M
, std::vector
<unsigned> &OpcodeFreq
){
719 for (; M
!= 0; M
= M
->getNext()) {
721 if (unsigned(M
->getKind()) >= OpcodeFreq
.size())
722 OpcodeFreq
.resize(M
->getKind()+1);
723 OpcodeFreq
[M
->getKind()]++;
725 // Handle recursive nodes.
726 if (const ScopeMatcher
*SM
= dyn_cast
<ScopeMatcher
>(M
)) {
727 for (unsigned i
= 0, e
= SM
->getNumChildren(); i
!= e
; ++i
)
728 BuildHistogram(SM
->getChild(i
), OpcodeFreq
);
729 } else if (const SwitchOpcodeMatcher
*SOM
=
730 dyn_cast
<SwitchOpcodeMatcher
>(M
)) {
731 for (unsigned i
= 0, e
= SOM
->getNumCases(); i
!= e
; ++i
)
732 BuildHistogram(SOM
->getCaseMatcher(i
), OpcodeFreq
);
733 } else if (const SwitchTypeMatcher
*STM
= dyn_cast
<SwitchTypeMatcher
>(M
)) {
734 for (unsigned i
= 0, e
= STM
->getNumCases(); i
!= e
; ++i
)
735 BuildHistogram(STM
->getCaseMatcher(i
), OpcodeFreq
);
740 void MatcherTableEmitter::EmitHistogram(const Matcher
*M
,
741 formatted_raw_ostream
&OS
) {
745 std::vector
<unsigned> OpcodeFreq
;
746 BuildHistogram(M
, OpcodeFreq
);
748 OS
<< " // Opcode Histogram:\n";
749 for (unsigned i
= 0, e
= OpcodeFreq
.size(); i
!= e
; ++i
) {
751 switch ((Matcher::KindTy
)i
) {
752 case Matcher::Scope
: OS
<< "OPC_Scope"; break;
753 case Matcher::RecordNode
: OS
<< "OPC_RecordNode"; break;
754 case Matcher::RecordChild
: OS
<< "OPC_RecordChild"; break;
755 case Matcher::RecordMemRef
: OS
<< "OPC_RecordMemRef"; break;
756 case Matcher::CaptureGlueInput
: OS
<< "OPC_CaptureGlueInput"; break;
757 case Matcher::MoveChild
: OS
<< "OPC_MoveChild"; break;
758 case Matcher::MoveParent
: OS
<< "OPC_MoveParent"; break;
759 case Matcher::CheckSame
: OS
<< "OPC_CheckSame"; break;
760 case Matcher::CheckPatternPredicate
:
761 OS
<< "OPC_CheckPatternPredicate"; break;
762 case Matcher::CheckPredicate
: OS
<< "OPC_CheckPredicate"; break;
763 case Matcher::CheckOpcode
: OS
<< "OPC_CheckOpcode"; break;
764 case Matcher::SwitchOpcode
: OS
<< "OPC_SwitchOpcode"; break;
765 case Matcher::CheckType
: OS
<< "OPC_CheckType"; break;
766 case Matcher::SwitchType
: OS
<< "OPC_SwitchType"; break;
767 case Matcher::CheckChildType
: OS
<< "OPC_CheckChildType"; break;
768 case Matcher::CheckInteger
: OS
<< "OPC_CheckInteger"; break;
769 case Matcher::CheckCondCode
: OS
<< "OPC_CheckCondCode"; break;
770 case Matcher::CheckValueType
: OS
<< "OPC_CheckValueType"; break;
771 case Matcher::CheckComplexPat
: OS
<< "OPC_CheckComplexPat"; break;
772 case Matcher::CheckAndImm
: OS
<< "OPC_CheckAndImm"; break;
773 case Matcher::CheckOrImm
: OS
<< "OPC_CheckOrImm"; break;
774 case Matcher::CheckFoldableChainNode
:
775 OS
<< "OPC_CheckFoldableChainNode"; break;
776 case Matcher::EmitInteger
: OS
<< "OPC_EmitInteger"; break;
777 case Matcher::EmitStringInteger
: OS
<< "OPC_EmitStringInteger"; break;
778 case Matcher::EmitRegister
: OS
<< "OPC_EmitRegister"; break;
779 case Matcher::EmitConvertToTarget
: OS
<< "OPC_EmitConvertToTarget"; break;
780 case Matcher::EmitMergeInputChains
: OS
<< "OPC_EmitMergeInputChains"; break;
781 case Matcher::EmitCopyToReg
: OS
<< "OPC_EmitCopyToReg"; break;
782 case Matcher::EmitNode
: OS
<< "OPC_EmitNode"; break;
783 case Matcher::MorphNodeTo
: OS
<< "OPC_MorphNodeTo"; break;
784 case Matcher::EmitNodeXForm
: OS
<< "OPC_EmitNodeXForm"; break;
785 case Matcher::MarkGlueResults
: OS
<< "OPC_MarkGlueResults"; break;
786 case Matcher::CompleteMatch
: OS
<< "OPC_CompleteMatch"; break;
789 OS
.PadToColumn(40) << " = " << OpcodeFreq
[i
] << '\n';
795 void llvm::EmitMatcherTable(const Matcher
*TheMatcher
,
796 const CodeGenDAGPatterns
&CGP
,
798 formatted_raw_ostream
OS(O
);
800 OS
<< "// The main instruction selector code.\n";
801 OS
<< "SDNode *SelectCode(SDNode *N) {\n";
803 MatcherTableEmitter
MatcherEmitter(CGP
);
805 OS
<< " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
807 OS
<< " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
808 OS
<< " static const unsigned char MatcherTable[] = {\n";
809 unsigned TotalSize
= MatcherEmitter
.EmitMatcherList(TheMatcher
, 5, 0, OS
);
810 OS
<< " 0\n }; // Total Array size is " << (TotalSize
+1) << " bytes\n\n";
812 MatcherEmitter
.EmitHistogram(TheMatcher
, OS
);
814 OS
<< " #undef TARGET_VAL\n";
815 OS
<< " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
818 // Next up, emit the function for node and pattern predicates:
819 MatcherEmitter
.EmitPredicateFunctions(OS
);