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 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 StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
36 std::vector<std::string> NodePredicates, PatternPredicates;
38 DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
39 std::vector<const ComplexPattern*> ComplexPatterns;
42 DenseMap<Record*, unsigned> NodeXFormMap;
43 std::vector<Record*> NodeXForms;
46 MatcherTableEmitter() {}
48 unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
49 unsigned StartIdx, formatted_raw_ostream &OS);
51 void EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
52 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, "
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;
283 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
284 Child = SOM->getCaseMatcher(i);
286 Child = cast<SwitchTypeMatcher>(N)->getCaseMatcher(i);
288 // We need to encode the opcode and the offset of the case code before
289 // emitting the case code. Handle this by buffering the output into a
290 // string while we get the size. Unfortunately, the offset of the
291 // children depends on the VBR size of the child, so for large children we
292 // have to iterate a bit.
293 SmallString<128> TmpBuf;
294 unsigned ChildSize = 0;
295 unsigned VBRSize = 0;
297 VBRSize = GetVBRSize(ChildSize);
300 raw_svector_ostream OS(TmpBuf);
301 formatted_raw_ostream FOS(OS);
302 ChildSize = EmitMatcherList(Child, Indent+1, CurrentIdx+VBRSize+1, FOS);
303 } while (GetVBRSize(ChildSize) != VBRSize);
305 assert(ChildSize != 0 && "Should not have a zero-sized child!");
308 OS.PadToColumn(Indent*2);
310 OS << (isa<SwitchOpcodeMatcher>(N) ?
311 "/*SwitchOpcode*/ " : "/*SwitchType*/ ");
315 CurrentIdx += EmitVBRValue(ChildSize, OS);
318 if (const SwitchOpcodeMatcher *SOM = dyn_cast<SwitchOpcodeMatcher>(N))
319 OS << SOM->getCaseOpcode(i).getEnumName();
321 OS << getEnumName(cast<SwitchTypeMatcher>(N)->getCaseType(i));
325 OS << "// ->" << CurrentIdx+ChildSize+1;
329 CurrentIdx += ChildSize;
332 // Emit the final zero to terminate the switch.
333 OS.PadToColumn(Indent*2) << "0, ";
335 OS << (isa<SwitchOpcodeMatcher>(N) ?
336 "// EndSwitchOpcode" : "// EndSwitchType");
340 return CurrentIdx-StartIdx;
343 case Matcher::CheckType:
344 assert(cast<CheckTypeMatcher>(N)->getResNo() == 0 &&
345 "FIXME: Add support for CheckType of resno != 0");
346 OS << "OPC_CheckType, "
347 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
350 case Matcher::CheckChildType:
351 OS << "OPC_CheckChild"
352 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
353 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
356 case Matcher::CheckInteger: {
357 OS << "OPC_CheckInteger, ";
358 unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
362 case Matcher::CheckCondCode:
363 OS << "OPC_CheckCondCode, ISD::"
364 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
367 case Matcher::CheckValueType:
368 OS << "OPC_CheckValueType, MVT::"
369 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
372 case Matcher::CheckComplexPat: {
373 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
374 const ComplexPattern &Pattern = CCPM->getPattern();
375 OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
376 << CCPM->getMatchNumber() << ',';
379 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
380 OS << ":$" << CCPM->getName();
381 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
382 OS << " #" << CCPM->getFirstResult()+i;
384 if (Pattern.hasProperty(SDNPHasChain))
385 OS << " + chain result";
391 case Matcher::CheckAndImm: {
392 OS << "OPC_CheckAndImm, ";
393 unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
398 case Matcher::CheckOrImm: {
399 OS << "OPC_CheckOrImm, ";
400 unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
405 case Matcher::CheckFoldableChainNode:
406 OS << "OPC_CheckFoldableChainNode,\n";
409 case Matcher::EmitInteger: {
410 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
411 OS << "OPC_EmitInteger, "
412 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
413 unsigned Bytes = 2+EmitVBRValue(Val, OS);
417 case Matcher::EmitStringInteger: {
418 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
419 // These should always fit into one byte.
420 OS << "OPC_EmitInteger, "
421 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
426 case Matcher::EmitRegister:
427 OS << "OPC_EmitRegister, "
428 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
429 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
430 OS << getQualifiedName(R) << ",\n";
434 OS << "/*zero_reg*/";
439 case Matcher::EmitConvertToTarget:
440 OS << "OPC_EmitConvertToTarget, "
441 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
444 case Matcher::EmitMergeInputChains: {
445 const EmitMergeInputChainsMatcher *MN =
446 cast<EmitMergeInputChainsMatcher>(N);
447 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
448 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
449 OS << MN->getNode(i) << ", ";
451 return 2+MN->getNumNodes();
453 case Matcher::EmitCopyToReg:
454 OS << "OPC_EmitCopyToReg, "
455 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
456 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
459 case Matcher::EmitNodeXForm: {
460 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
461 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
462 << XF->getSlot() << ',';
464 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
469 case Matcher::EmitNode:
470 case Matcher::MorphNodeTo: {
471 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
472 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
473 OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
475 if (EN->hasChain()) OS << "|OPFL_Chain";
476 if (EN->hasInFlag()) OS << "|OPFL_FlagInput";
477 if (EN->hasOutFlag()) OS << "|OPFL_FlagOutput";
478 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
479 if (EN->getNumFixedArityOperands() != -1)
480 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
483 OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
487 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
488 OS << getEnumName(EN->getVT(i)) << ", ";
490 OS << EN->getNumOperands();
494 unsigned NumOperandBytes = 0;
495 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
496 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
499 // Print the result #'s for EmitNode.
500 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
501 if (unsigned NumResults = EN->getNumVTs()) {
502 OS.PadToColumn(CommentIndent) << "// Results = ";
503 unsigned First = E->getFirstResultSlot();
504 for (unsigned i = 0; i != NumResults; ++i)
505 OS << "#" << First+i << " ";
510 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
511 OS.PadToColumn(Indent*2) << "// Src: "
512 << *SNT->getPattern().getSrcPattern() << '\n';
513 OS.PadToColumn(Indent*2) << "// Dst: "
514 << *SNT->getPattern().getDstPattern() << '\n';
519 return 6+EN->getNumVTs()+NumOperandBytes;
521 case Matcher::MarkFlagResults: {
522 const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
523 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
524 unsigned NumOperandBytes = 0;
525 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
526 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
528 return 2+NumOperandBytes;
530 case Matcher::CompleteMatch: {
531 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
532 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
533 unsigned NumResultBytes = 0;
534 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
535 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
538 OS.PadToColumn(Indent*2) << "// Src: "
539 << *CM->getPattern().getSrcPattern() << '\n';
540 OS.PadToColumn(Indent*2) << "// Dst: "
541 << *CM->getPattern().getDstPattern();
544 return 2 + NumResultBytes;
547 assert(0 && "Unreachable");
551 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
552 unsigned MatcherTableEmitter::
553 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
554 formatted_raw_ostream &OS) {
558 OS << "/*" << CurrentIdx << "*/";
559 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
561 CurrentIdx += MatcherSize;
563 // If there are other nodes in this list, iterate to them, otherwise we're
570 void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
571 formatted_raw_ostream &OS) {
572 // Emit pattern predicates.
573 if (!PatternPredicates.empty()) {
574 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
575 OS << " switch (PredNo) {\n";
576 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
577 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
578 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
583 // Emit Node predicates.
584 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
585 StringMap<TreePattern*> PFsByName;
587 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
589 PFsByName[I->first->getName()] = I->second;
591 if (!NodePredicates.empty()) {
592 OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
593 OS << " switch (PredNo) {\n";
594 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
595 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
596 // FIXME: Storing this by name is horrible.
597 TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
598 assert(P && "Unknown name?");
600 // Emit the predicate code corresponding to this pattern.
601 std::string Code = P->getRecord()->getValueAsCode("Predicate");
602 assert(!Code.empty() && "No code in this predicate");
603 OS << " case " << i << ": { // " << NodePredicates[i] << '\n';
604 std::string ClassName;
605 if (P->getOnlyTree()->isLeaf())
606 ClassName = "SDNode";
609 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
610 if (ClassName == "SDNode")
611 OS << " SDNode *N = Node;\n";
613 OS << " " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
614 OS << Code << "\n }\n";
620 // Emit CompletePattern matchers.
621 // FIXME: This should be const.
622 if (!ComplexPatterns.empty()) {
623 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
624 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
625 OS << " switch (PatternNo) {\n";
626 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
627 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
628 const ComplexPattern &P = *ComplexPatterns[i];
629 unsigned NumOps = P.getNumOperands();
631 if (P.hasProperty(SDNPHasChain))
632 ++NumOps; // Get the chained node too.
634 OS << " case " << i << ":\n";
635 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
636 OS << " return " << P.getSelectFunc();
639 for (unsigned i = 0; i != NumOps; ++i)
640 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
648 // Emit SDNodeXForm handlers.
649 // FIXME: This should be const.
650 if (!NodeXForms.empty()) {
651 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
652 OS << " switch (XFormNo) {\n";
653 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
655 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
656 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
657 const CodeGenDAGPatterns::NodeXForm &Entry =
658 CGP.getSDNodeTransform(NodeXForms[i]);
660 Record *SDNode = Entry.first;
661 const std::string &Code = Entry.second;
663 OS << " case " << i << ": { ";
665 OS << "// " << NodeXForms[i]->getName();
668 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
669 if (ClassName == "SDNode")
670 OS << " SDNode *N = V.getNode();\n";
672 OS << " " << ClassName << " *N = cast<" << ClassName
673 << ">(V.getNode());\n";
674 OS << Code << "\n }\n";
681 static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
682 for (; M != 0; M = M->getNext()) {
684 if (unsigned(M->getKind()) >= OpcodeFreq.size())
685 OpcodeFreq.resize(M->getKind()+1);
686 OpcodeFreq[M->getKind()]++;
688 // Handle recursive nodes.
689 if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
690 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
691 BuildHistogram(SM->getChild(i), OpcodeFreq);
692 } else if (const SwitchOpcodeMatcher *SOM =
693 dyn_cast<SwitchOpcodeMatcher>(M)) {
694 for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
695 BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
696 } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
697 for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
698 BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
703 void MatcherTableEmitter::EmitHistogram(const Matcher *M,
704 formatted_raw_ostream &OS) {
708 std::vector<unsigned> OpcodeFreq;
709 BuildHistogram(M, OpcodeFreq);
711 OS << " // Opcode Histogram:\n";
712 for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
714 switch ((Matcher::KindTy)i) {
715 case Matcher::Scope: OS << "OPC_Scope"; break;
716 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
717 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
718 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
719 case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
720 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
721 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
722 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
723 case Matcher::CheckPatternPredicate:
724 OS << "OPC_CheckPatternPredicate"; break;
725 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
726 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
727 case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
728 case Matcher::CheckType: OS << "OPC_CheckType"; break;
729 case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
730 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
731 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
732 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
733 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
734 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
735 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
736 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
737 case Matcher::CheckFoldableChainNode:
738 OS << "OPC_CheckFoldableChainNode"; break;
739 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
740 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
741 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
742 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
743 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
744 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
745 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
746 case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
747 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
748 case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
749 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
752 OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
758 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
759 const CodeGenDAGPatterns &CGP, raw_ostream &O) {
760 formatted_raw_ostream OS(O);
762 OS << "// The main instruction selector code.\n";
763 OS << "SDNode *SelectCode(SDNode *N) {\n";
765 MatcherTableEmitter MatcherEmitter;
767 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
768 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
769 OS << " static const unsigned char MatcherTable[] = {\n";
770 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
771 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
773 MatcherEmitter.EmitHistogram(TheMatcher, OS);
775 OS << " #undef TARGET_OPCODE\n";
776 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
779 // Next up, emit the function for node and pattern predicates:
780 MatcherEmitter.EmitPredicateFunctions(CGP, OS);