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 OS << "OPC_CheckType, "
345 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
348 case Matcher::CheckChildType:
349 OS << "OPC_CheckChild"
350 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
351 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
354 case Matcher::CheckInteger: {
355 OS << "OPC_CheckInteger, ";
356 unsigned Bytes=1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
360 case Matcher::CheckCondCode:
361 OS << "OPC_CheckCondCode, ISD::"
362 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
365 case Matcher::CheckValueType:
366 OS << "OPC_CheckValueType, MVT::"
367 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
370 case Matcher::CheckComplexPat: {
371 const CheckComplexPatMatcher *CCPM = cast<CheckComplexPatMatcher>(N);
372 const ComplexPattern &Pattern = CCPM->getPattern();
373 OS << "OPC_CheckComplexPat, /*CP*/" << getComplexPat(Pattern) << ", /*#*/"
374 << CCPM->getMatchNumber() << ',';
377 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
378 OS << ":$" << CCPM->getName();
379 for (unsigned i = 0, e = Pattern.getNumOperands(); i != e; ++i)
380 OS << " #" << CCPM->getFirstResult()+i;
382 if (Pattern.hasProperty(SDNPHasChain))
383 OS << " + chain result";
389 case Matcher::CheckAndImm: {
390 OS << "OPC_CheckAndImm, ";
391 unsigned Bytes=1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
396 case Matcher::CheckOrImm: {
397 OS << "OPC_CheckOrImm, ";
398 unsigned Bytes = 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
403 case Matcher::CheckFoldableChainNode:
404 OS << "OPC_CheckFoldableChainNode,\n";
407 case Matcher::EmitInteger: {
408 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
409 OS << "OPC_EmitInteger, "
410 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
411 unsigned Bytes = 2+EmitVBRValue(Val, OS);
415 case Matcher::EmitStringInteger: {
416 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
417 // These should always fit into one byte.
418 OS << "OPC_EmitInteger, "
419 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
424 case Matcher::EmitRegister:
425 OS << "OPC_EmitRegister, "
426 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
427 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
428 OS << getQualifiedName(R) << ",\n";
432 OS << "/*zero_reg*/";
437 case Matcher::EmitConvertToTarget:
438 OS << "OPC_EmitConvertToTarget, "
439 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
442 case Matcher::EmitMergeInputChains: {
443 const EmitMergeInputChainsMatcher *MN =
444 cast<EmitMergeInputChainsMatcher>(N);
445 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
446 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
447 OS << MN->getNode(i) << ", ";
449 return 2+MN->getNumNodes();
451 case Matcher::EmitCopyToReg:
452 OS << "OPC_EmitCopyToReg, "
453 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
454 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
457 case Matcher::EmitNodeXForm: {
458 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
459 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
460 << XF->getSlot() << ',';
462 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
467 case Matcher::EmitNode:
468 case Matcher::MorphNodeTo: {
469 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
470 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
471 OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
473 if (EN->hasChain()) OS << "|OPFL_Chain";
474 if (EN->hasInFlag()) OS << "|OPFL_FlagInput";
475 if (EN->hasOutFlag()) OS << "|OPFL_FlagOutput";
476 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
477 if (EN->getNumFixedArityOperands() != -1)
478 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
481 OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
485 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
486 OS << getEnumName(EN->getVT(i)) << ", ";
488 OS << EN->getNumOperands();
492 unsigned NumOperandBytes = 0;
493 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i)
494 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
497 // Print the result #'s for EmitNode.
498 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
499 if (unsigned NumResults = EN->getNumVTs()) {
500 OS.PadToColumn(CommentIndent) << "// Results = ";
501 unsigned First = E->getFirstResultSlot();
502 for (unsigned i = 0; i != NumResults; ++i)
503 OS << "#" << First+i << " ";
508 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
509 OS.PadToColumn(Indent*2) << "// Src: "
510 << *SNT->getPattern().getSrcPattern() << '\n';
511 OS.PadToColumn(Indent*2) << "// Dst: "
512 << *SNT->getPattern().getDstPattern() << '\n';
517 return 6+EN->getNumVTs()+NumOperandBytes;
519 case Matcher::MarkFlagResults: {
520 const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
521 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
522 unsigned NumOperandBytes = 0;
523 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
524 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
526 return 2+NumOperandBytes;
528 case Matcher::CompleteMatch: {
529 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
530 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
531 unsigned NumResultBytes = 0;
532 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
533 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
536 OS.PadToColumn(Indent*2) << "// Src: "
537 << *CM->getPattern().getSrcPattern() << '\n';
538 OS.PadToColumn(Indent*2) << "// Dst: "
539 << *CM->getPattern().getDstPattern();
542 return 2 + NumResultBytes;
545 assert(0 && "Unreachable");
549 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
550 unsigned MatcherTableEmitter::
551 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
552 formatted_raw_ostream &OS) {
556 OS << "/*" << CurrentIdx << "*/";
557 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
559 CurrentIdx += MatcherSize;
561 // If there are other nodes in this list, iterate to them, otherwise we're
568 void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
569 formatted_raw_ostream &OS) {
570 // Emit pattern predicates.
571 if (!PatternPredicates.empty()) {
572 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
573 OS << " switch (PredNo) {\n";
574 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
575 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
576 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
581 // Emit Node predicates.
582 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
583 StringMap<TreePattern*> PFsByName;
585 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
587 PFsByName[I->first->getName()] = I->second;
589 if (!NodePredicates.empty()) {
590 OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
591 OS << " switch (PredNo) {\n";
592 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
593 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
594 // FIXME: Storing this by name is horrible.
595 TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
596 assert(P && "Unknown name?");
598 // Emit the predicate code corresponding to this pattern.
599 std::string Code = P->getRecord()->getValueAsCode("Predicate");
600 assert(!Code.empty() && "No code in this predicate");
601 OS << " case " << i << ": { // " << NodePredicates[i] << '\n';
602 std::string ClassName;
603 if (P->getOnlyTree()->isLeaf())
604 ClassName = "SDNode";
607 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
608 if (ClassName == "SDNode")
609 OS << " SDNode *N = Node;\n";
611 OS << " " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
612 OS << Code << "\n }\n";
618 // Emit CompletePattern matchers.
619 // FIXME: This should be const.
620 if (!ComplexPatterns.empty()) {
621 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
622 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
623 OS << " switch (PatternNo) {\n";
624 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
625 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
626 const ComplexPattern &P = *ComplexPatterns[i];
627 unsigned NumOps = P.getNumOperands();
629 if (P.hasProperty(SDNPHasChain))
630 ++NumOps; // Get the chained node too.
632 OS << " case " << i << ":\n";
633 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
634 OS << " return " << P.getSelectFunc();
637 for (unsigned i = 0; i != NumOps; ++i)
638 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
646 // Emit SDNodeXForm handlers.
647 // FIXME: This should be const.
648 if (!NodeXForms.empty()) {
649 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
650 OS << " switch (XFormNo) {\n";
651 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
653 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
654 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
655 const CodeGenDAGPatterns::NodeXForm &Entry =
656 CGP.getSDNodeTransform(NodeXForms[i]);
658 Record *SDNode = Entry.first;
659 const std::string &Code = Entry.second;
661 OS << " case " << i << ": { ";
663 OS << "// " << NodeXForms[i]->getName();
666 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
667 if (ClassName == "SDNode")
668 OS << " SDNode *N = V.getNode();\n";
670 OS << " " << ClassName << " *N = cast<" << ClassName
671 << ">(V.getNode());\n";
672 OS << Code << "\n }\n";
679 static void BuildHistogram(const Matcher *M, std::vector<unsigned> &OpcodeFreq){
680 for (; M != 0; M = M->getNext()) {
682 if (unsigned(M->getKind()) >= OpcodeFreq.size())
683 OpcodeFreq.resize(M->getKind()+1);
684 OpcodeFreq[M->getKind()]++;
686 // Handle recursive nodes.
687 if (const ScopeMatcher *SM = dyn_cast<ScopeMatcher>(M)) {
688 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i)
689 BuildHistogram(SM->getChild(i), OpcodeFreq);
690 } else if (const SwitchOpcodeMatcher *SOM =
691 dyn_cast<SwitchOpcodeMatcher>(M)) {
692 for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i)
693 BuildHistogram(SOM->getCaseMatcher(i), OpcodeFreq);
694 } else if (const SwitchTypeMatcher *STM = dyn_cast<SwitchTypeMatcher>(M)) {
695 for (unsigned i = 0, e = STM->getNumCases(); i != e; ++i)
696 BuildHistogram(STM->getCaseMatcher(i), OpcodeFreq);
701 void MatcherTableEmitter::EmitHistogram(const Matcher *M,
702 formatted_raw_ostream &OS) {
706 std::vector<unsigned> OpcodeFreq;
707 BuildHistogram(M, OpcodeFreq);
709 OS << " // Opcode Histogram:\n";
710 for (unsigned i = 0, e = OpcodeFreq.size(); i != e; ++i) {
712 switch ((Matcher::KindTy)i) {
713 case Matcher::Scope: OS << "OPC_Scope"; break;
714 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
715 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
716 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
717 case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
718 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
719 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
720 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
721 case Matcher::CheckPatternPredicate:
722 OS << "OPC_CheckPatternPredicate"; break;
723 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
724 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
725 case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
726 case Matcher::CheckType: OS << "OPC_CheckType"; break;
727 case Matcher::SwitchType: OS << "OPC_SwitchType"; break;
728 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
729 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
730 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
731 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
732 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
733 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
734 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
735 case Matcher::CheckFoldableChainNode:
736 OS << "OPC_CheckFoldableChainNode"; break;
737 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
738 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
739 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
740 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
741 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
742 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
743 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
744 case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
745 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
746 case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
747 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
750 OS.PadToColumn(40) << " = " << OpcodeFreq[i] << '\n';
756 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
757 const CodeGenDAGPatterns &CGP, raw_ostream &O) {
758 formatted_raw_ostream OS(O);
760 OS << "// The main instruction selector code.\n";
761 OS << "SDNode *SelectCode(SDNode *N) {\n";
763 MatcherTableEmitter MatcherEmitter;
765 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
766 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
767 OS << " static const unsigned char MatcherTable[] = {\n";
768 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
769 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
771 MatcherEmitter.EmitHistogram(TheMatcher, OS);
773 OS << " #undef TARGET_OPCODE\n";
774 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
777 // Next up, emit the function for node and pattern predicates:
778 MatcherEmitter.EmitPredicateFunctions(CGP, OS);