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;
46 bool useEmitRegister2;
49 MatcherTableEmitter(const CodeGenDAGPatterns &cgp, bool _useEmitRegister2)
50 : CGP(cgp), useEmitRegister2(_useEmitRegister2) {}
52 unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
53 unsigned StartIdx, formatted_raw_ostream &OS);
55 void EmitPredicateFunctions(formatted_raw_ostream &OS);
57 void EmitHistogram(const Matcher *N, formatted_raw_ostream &OS);
59 unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
60 formatted_raw_ostream &OS);
62 unsigned getNodePredicate(StringRef PredName) {
63 unsigned &Entry = NodePredicateMap[PredName];
65 NodePredicates.push_back(PredName.str());
66 Entry = NodePredicates.size();
70 unsigned getPatternPredicate(StringRef PredName) {
71 unsigned &Entry = PatternPredicateMap[PredName];
73 PatternPredicates.push_back(PredName.str());
74 Entry = PatternPredicates.size();
79 unsigned getComplexPat(const ComplexPattern &P) {
80 unsigned &Entry = ComplexPatternMap[&P];
82 ComplexPatterns.push_back(&P);
83 Entry = ComplexPatterns.size();
88 unsigned getNodeXFormID(Record *Rec) {
89 unsigned &Entry = NodeXFormMap[Rec];
91 NodeXForms.push_back(Rec);
92 Entry = NodeXForms.size();
98 } // end anonymous namespace.
100 static unsigned GetVBRSize(unsigned Val) {
101 if (Val <= 127) return 1;
103 unsigned NumBytes = 0;
111 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
113 static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
119 uint64_t InVal = Val;
120 unsigned NumBytes = 0;
122 OS << (Val&127) << "|128,";
128 OS << "/*" << InVal << "*/";
133 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
134 /// the number of bytes emitted.
135 unsigned MatcherTableEmitter::
136 EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
137 formatted_raw_ostream &OS) {
138 OS.PadToColumn(Indent*2);
140 switch (N->getKind()) {
141 case Matcher::Scope: {
142 const ScopeMatcher *SM = cast<ScopeMatcher>(N);
143 assert(SM->getNext() == 0 && "Shouldn't have next after scope");
145 unsigned StartIdx = CurrentIdx;
147 // Emit all of the children.
148 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
154 OS << "/*" << CurrentIdx << "*/";
155 OS.PadToColumn(Indent*2) << "/*Scope*/ ";
157 OS.PadToColumn(Indent*2);
160 // We need to encode the child and the offset of the failure code before
161 // emitting either of them. Handle this by buffering the output into a
162 // string while we get the size. Unfortunately, the offset of the
163 // children depends on the VBR size of the child, so for large children we
164 // have to iterate a bit.
165 SmallString<128> TmpBuf;
166 unsigned ChildSize = 0;
167 unsigned VBRSize = 0;
169 VBRSize = GetVBRSize(ChildSize);
172 raw_svector_ostream OS(TmpBuf);
173 formatted_raw_ostream FOS(OS);
174 ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
175 CurrentIdx+VBRSize, FOS);
176 } while (GetVBRSize(ChildSize) != VBRSize);
178 assert(ChildSize != 0 && "Should not have a zero-sized child!");
180 CurrentIdx += EmitVBRValue(ChildSize, OS);
182 OS << "/*->" << CurrentIdx+ChildSize << "*/";
185 OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
186 << " children in Scope";
189 OS << '\n' << TmpBuf.str();
190 CurrentIdx += ChildSize;
193 // Emit a zero as a sentinel indicating end of 'Scope'.
195 OS << "/*" << CurrentIdx << "*/";
196 OS.PadToColumn(Indent*2) << "0, ";
198 OS << "/*End of Scope*/";
200 return CurrentIdx - StartIdx + 1;
203 case Matcher::RecordNode:
204 OS << "OPC_RecordNode,";
206 OS.PadToColumn(CommentIndent) << "// #"
207 << cast<RecordMatcher>(N)->getResultNo() << " = "
208 << cast<RecordMatcher>(N)->getWhatFor();
212 case Matcher::RecordChild:
213 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
216 OS.PadToColumn(CommentIndent) << "// #"
217 << cast<RecordChildMatcher>(N)->getResultNo() << " = "
218 << cast<RecordChildMatcher>(N)->getWhatFor();
222 case Matcher::RecordMemRef:
223 OS << "OPC_RecordMemRef,\n";
226 case Matcher::CaptureGlueInput:
227 OS << "OPC_CaptureGlueInput,\n";
230 case Matcher::MoveChild:
231 OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
234 case Matcher::MoveParent:
235 OS << "OPC_MoveParent,\n";
238 case Matcher::CheckSame:
239 OS << "OPC_CheckSame, "
240 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
243 case Matcher::CheckPatternPredicate: {
244 StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
245 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
247 OS.PadToColumn(CommentIndent) << "// " << Pred;
251 case Matcher::CheckPredicate: {
252 StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
253 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
255 OS.PadToColumn(CommentIndent) << "// " << Pred;
260 case Matcher::CheckOpcode:
261 OS << "OPC_CheckOpcode, TARGET_VAL("
262 << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << "),\n";
265 case Matcher::SwitchOpcode:
266 case Matcher::SwitchType: {
267 unsigned StartIdx = CurrentIdx;
270 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
271 OS << "OPC_SwitchOpcode ";
272 NumCases = SOM->getNumCases();
274 OS << "OPC_SwitchType ";
275 NumCases = cast<SwitchTypeMatcher>(N)->getNumCases();
279 OS << "/*" << NumCases << " cases */";
283 // For each case we emit the size, then the opcode, then the matcher.
284 for (unsigned i = 0, e = NumCases; i != e; ++i) {
285 const Matcher *Child;
287 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N)) {
288 Child = SOM->getCaseMatcher(i);
289 IdxSize = 2; // size of opcode in table is 2 bytes.
291 Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
292 IdxSize = 1; // size of type in table is 1 byte.
295 // We need to encode the opcode and the offset of the case code before
296 // emitting the case code. Handle this by buffering the output into a
297 // string while we get the size. Unfortunately, the offset of the
298 // children depends on the VBR size of the child, so for large children we
299 // have to iterate a bit.
300 SmallString<128> TmpBuf;
301 unsigned ChildSize = 0;
302 unsigned VBRSize = 0;
304 VBRSize = GetVBRSize(ChildSize);
307 raw_svector_ostream OS(TmpBuf);
308 formatted_raw_ostream FOS(OS);
309 ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+IdxSize,
311 } while (GetVBRSize(ChildSize) != VBRSize);
313 assert(ChildSize != 0 && "Should not have a zero-sized child!");
316 OS.PadToColumn(Indent*2);
318 OS << (isa<SwitchOpcodeMatcher>(N) ?
319 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
323 CurrentIdx += EmitVBRValue(ChildSize, OS);
326 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
327 OS << "TARGET_VAL(" << SOM->getCaseOpcode(i).getEnumName() << "),";
329 OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i)) << ',';
331 CurrentIdx += IdxSize;
334 OS << "// ->" << CurrentIdx+ChildSize;
337 CurrentIdx += ChildSize;
340 // Emit the final zero to terminate the switch.
341 OS.PadToColumn(Indent*2) << "0, ";
343 OS << (isa<SwitchOpcodeMatcher>(N) ?
344 "// EndSwitchOpcode" : "// EndSwitchType");
348 return CurrentIdx-StartIdx;
351 case Matcher::CheckType:
352 assert(cast<CheckTypeMatcher>(N)->getResNo() == 0 &&
353 "FIXME: Add support for CheckType of resno != 0");
354 OS << "OPC_CheckType, "
355 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
358 case Matcher::CheckChildType:
359 OS << "OPC_CheckChild"
360 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
361 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
364 case Matcher::CheckInteger: {
365 OS << "OPC_CheckInteger, ";
366 unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
370 case Matcher::CheckCondCode:
371 OS << "OPC_CheckCondCode, ISD::"
372 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
375 case Matcher::CheckValueType:
376 OS << "OPC_CheckValueType, MVT::"
377 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
380 case Matcher::CheckComplexPat: {
381 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
382 const ComplexPattern &Pattern = CCPM->getPattern();
383 OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
384 << CCPM->getMatchNumber() << ',';
387 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
388 OS << ":$" << CCPM->getName();
389 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
390 OS << " #" << CCPM->getFirstResult()+i;
392 if (Pattern.hasProperty(SDNPHasChain))
393 OS << " + chain result";
399 case Matcher::CheckAndImm: {
400 OS << "OPC_CheckAndImm, ";
401 unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
406 case Matcher::CheckOrImm: {
407 OS << "OPC_CheckOrImm, ";
408 unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
413 case Matcher::CheckFoldableChainNode:
414 OS << "OPC_CheckFoldableChainNode,\n";
417 case Matcher::EmitInteger: {
418 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
419 OS << "OPC_EmitInteger, "
420 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
421 unsigned Bytes = 2+EmitVBRValue(Val, OS);
425 case Matcher::EmitStringInteger: {
426 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
427 // These should always fit into one byte.
428 OS << "OPC_EmitInteger, "
429 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
434 case Matcher::EmitRegister:
435 if (useEmitRegister2) {
436 OS << "OPC_EmitRegister2, "
437 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
438 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
439 OS << "TARGET_VAL(" << getQualifiedName(R) << "),\n";
441 OS << "TARGET_VAL(0) ";
443 OS << "/*zero_reg*/";
448 OS << "OPC_EmitRegister, "
449 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
450 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
451 OS << getQualifiedName(R) << ",\n";
455 OS << "/*zero_reg*/";
461 case Matcher::EmitConvertToTarget:
462 OS << "OPC_EmitConvertToTarget, "
463 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
466 case Matcher::EmitMergeInputChains: {
467 const EmitMergeInputChainsMatcher *MN =
468 cast<EmitMergeInputChainsMatcher>(N);
470 // Handle the specialized forms OPC_EmitMergeInputChains1_0 and 1_1.
471 if (MN->getNumNodes() == 1 && MN->getNode(0) < 2) {
472 OS << "OPC_EmitMergeInputChains1_" << MN->getNode(0) << ",\n";
476 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
477 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
478 OS << MN->getNode(i) << ", ";
480 return 2+MN->getNumNodes();
482 case Matcher::EmitCopyToReg:
483 OS << "OPC_EmitCopyToReg, "
484 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
485 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
488 case Matcher::EmitNodeXForm: {
489 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
490 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
491 << XF->getSlot() << ',';
493 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
498 case Matcher::EmitNode:
499 case Matcher::MorphNodeTo: {
500 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
501 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
502 OS << ", TARGET_VAL(" << EN->getOpcodeName() << "), 0";
504 if (EN->hasChain()) OS << "|OPFL_Chain";
505 if (EN->hasInFlag()) OS << "|OPFL_GlueInput";
506 if (EN->hasOutFlag()) OS << "|OPFL_GlueOutput";
507 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
508 if (EN->getNumFixedArityOperands() != -1)
509 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
512 OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
516 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
517 OS << getEnumName(EN->getVT(i)) << ", ";
519 OS << EN->getNumOperands();
523 unsigned NumOperandBytes = 0;
524 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
525 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
528 // Print the result #'s for EmitNode.
529 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
530 if (unsigned NumResults = EN->getNumVTs()) {
531 OS.PadToColumn(CommentIndent) << "// Results = ";
532 unsigned First = E->getFirstResultSlot();
533 for (unsigned i = 0; i != NumResults; ++i)
534 OS << "#" << First+i << " ";
539 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
540 OS.PadToColumn(Indent*2) << "// Src: "
541 << *SNT->getPattern().getSrcPattern() << " - Complexity = "
542 << SNT->getPattern().getPatternComplexity(CGP) << '\n';
543 OS.PadToColumn(Indent*2) << "// Dst: "
544 << *SNT->getPattern().getDstPattern() << '\n';
549 return 6+EN->getNumVTs()+NumOperandBytes;
551 case Matcher::MarkGlueResults: {
552 const MarkGlueResultsMatcher *CFR = cast<MarkGlueResultsMatcher>(N);
553 OS << "OPC_MarkGlueResults, " << CFR->getNumNodes() << ", ";
554 unsigned NumOperandBytes = 0;
555 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
556 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
558 return 2+NumOperandBytes;
560 case Matcher::CompleteMatch: {
561 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
562 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
563 unsigned NumResultBytes = 0;
564 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
565 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
568 OS.PadToColumn(Indent*2) << "// Src: "
569 << *CM->getPattern().getSrcPattern() << " - Complexity = "
570 << CM->getPattern().getPatternComplexity(CGP) << '\n';
571 OS.PadToColumn(Indent*2) << "// Dst: "
572 << *CM->getPattern().getDstPattern();
575 return 2 + NumResultBytes;
578 assert(0 && "Unreachable");
582 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
583 unsigned MatcherTableEmitter::
584 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
585 formatted_raw_ostream &OS) {
589 OS << "/*" << CurrentIdx << "*/";
590 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
592 CurrentIdx += MatcherSize;
594 // If there are other nodes in this list, iterate to them, otherwise we're
601 void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
602 // Emit pattern predicates.
603 if (!PatternPredicates.empty()) {
604 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
605 OS << " switch (PredNo) {\n";
606 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
607 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
608 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
613 // Emit Node predicates.
614 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
615 StringMap<TreePattern*> PFsByName;
617 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
619 PFsByName[I->first->getName()] = I->second;
621 if (!NodePredicates.empty()) {
622 OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
623 OS << " switch (PredNo) {\n";
624 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
625 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
626 // FIXME: Storing this by name is horrible.
627 TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
628 assert(P && "Unknown name?");
630 // Emit the predicate code corresponding to this pattern.
631 std::string Code = P->getRecord()->getValueAsCode("Predicate");
632 assert(!Code.empty() && "No code in this predicate");
633 OS << " case " << i << ": { // " << NodePredicates[i] << '\n';
634 std::string ClassName;
635 if (P->getOnlyTree()->isLeaf())
636 ClassName = "SDNode";
639 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
640 if (ClassName == "SDNode")
641 OS << " SDNode *N = Node;\n";
643 OS << " " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
644 OS << Code << "\n }\n";
650 // Emit CompletePattern matchers.
651 // FIXME: This should be const.
652 if (!ComplexPatterns.empty()) {
653 OS << "bool CheckComplexPattern(SDNode *Root, SDNode *Parent, SDValue N,\n";
654 OS << " unsigned PatternNo,\n";
655 OS << " SmallVectorImpl<std::pair<SDValue, SDNode*> > &Result) {\n";
656 OS << " unsigned NextRes = Result.size();\n";
657 OS << " switch (PatternNo) {\n";
658 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
659 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
660 const ComplexPattern &P = *ComplexPatterns[i];
661 unsigned NumOps = P.getNumOperands();
663 if (P.hasProperty(SDNPHasChain))
664 ++NumOps; // Get the chained node too.
666 OS << " case " << i << ":\n";
667 OS << " Result.resize(NextRes+" << NumOps << ");\n";
668 OS << " return " << P.getSelectFunc();
671 // If the complex pattern wants the root of the match, pass it in as the
673 if (P.hasProperty(SDNPWantRoot))
676 // If the complex pattern wants the parent of the operand being matched,
677 // pass it in as the next argument.
678 if (P.hasProperty(SDNPWantParent))
682 for (unsigned i = 0; i != NumOps; ++i)
683 OS << ", Result[NextRes+" << i << "].first";
691 // Emit SDNodeXForm handlers.
692 // FIXME: This should be const.
693 if (!NodeXForms.empty()) {
694 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
695 OS << " switch (XFormNo) {\n";
696 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
698 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
699 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
700 const CodeGenDAGPatterns::NodeXForm &Entry =
701 CGP.getSDNodeTransform(NodeXForms[i]);
703 Record *SDNode = Entry.first;
704 const std::string &Code = Entry.second;
706 OS << " case " << i << ": { ";
708 OS << "// " << NodeXForms[i]->getName();
711 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
712 if (ClassName == "SDNode")
713 OS << " SDNode *N = V.getNode();\n";
715 OS << " " << ClassName << " *N = cast<" << ClassName
716 << ">(V.getNode());\n";
717 OS << Code << "\n }\n";
724 static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
725 for (; M != 0; M = M->getNext()) {
727 if (unsigned(M->getKind()) >= OpcodeFreq.size())
728 OpcodeFreq.resize(M->getKind()+1);
729 OpcodeFreq[M->getKind()]++;
731 // Handle recursive nodes.
732 if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
733 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
734 BuildHistogram(SM->getChild(i), OpcodeFreq);
735 } else if (const SwitchOpcodeMatcher *SOM =
736 dyn_cast<SwitchOpcodeMatcher>(M)) {
737 for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
738 BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
739 } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
740 for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
741 BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
746 void MatcherTableEmitter::EmitHistogram(const Matcher *M,
747 formatted_raw_ostream &OS) {
751 std::vector<unsigned> OpcodeFreq;
752 BuildHistogram(M, OpcodeFreq);
754 OS << " // Opcode Histogram:\n";
755 for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
757 switch ((Matcher::KindTy)i) {
758 case Matcher::Scope: OS << "OPC_Scope"; break;
759 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
760 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
761 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
762 case Matcher::CaptureGlueInput: OS << "OPC_CaptureGlueInput"; break;
763 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
764 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
765 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
766 case Matcher::CheckPatternPredicate:
767 OS << "OPC_CheckPatternPredicate"; break;
768 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
769 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
770 case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
771 case Matcher::CheckType: OS << "OPC_CheckType"; break;
772 case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
773 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
774 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
775 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
776 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
777 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
778 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
779 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
780 case Matcher::CheckFoldableChainNode:
781 OS << "OPC_CheckFoldableChainNode"; break;
782 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
783 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
784 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
785 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
786 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
787 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
788 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
789 case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
790 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
791 case Matcher::MarkGlueResults: OS << "OPC_MarkGlueResults"; break;
792 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
795 OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
801 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
802 const CodeGenDAGPatterns &CGP,
803 bool useEmitRegister2,
805 formatted_raw_ostream OS(O);
807 OS << "// The main instruction selector code.\n";
808 OS << "SDNode *SelectCode(SDNode *N) {\n";
810 MatcherTableEmitter MatcherEmitter(CGP, useEmitRegister2);
812 OS << " // Some target values are emitted as 2 bytes, TARGET_VAL handles\n";
814 OS << " #define TARGET_VAL(X) X & 255, unsigned(X) >> 8\n";
815 OS << " static const unsigned char MatcherTable[] = {\n";
816 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
817 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
819 MatcherEmitter.EmitHistogram(TheMatcher, OS);
821 OS << " #undef TARGET_VAL\n";
822 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
825 // Next up, emit the function for node and pattern predicates:
826 MatcherEmitter.EmitPredicateFunctions(OS);