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;
45 // Per opcode frequence count.
46 std::vector<unsigned> Histogram;
48 MatcherTableEmitter() {}
50 unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
51 unsigned StartIdx, formatted_raw_ostream &OS);
53 void EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
54 formatted_raw_ostream &OS);
56 void EmitHistogram(formatted_raw_ostream &OS);
58 unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
59 formatted_raw_ostream &OS);
61 unsigned getNodePredicate(StringRef PredName) {
62 unsigned &Entry = NodePredicateMap[PredName];
64 NodePredicates.push_back(PredName.str());
65 Entry = NodePredicates.size();
69 unsigned getPatternPredicate(StringRef PredName) {
70 unsigned &Entry = PatternPredicateMap[PredName];
72 PatternPredicates.push_back(PredName.str());
73 Entry = PatternPredicates.size();
78 unsigned getComplexPat(const ComplexPattern &P) {
79 unsigned &Entry = ComplexPatternMap[&P];
81 ComplexPatterns.push_back(&P);
82 Entry = ComplexPatterns.size();
87 unsigned getNodeXFormID(Record *Rec) {
88 unsigned &Entry = NodeXFormMap[Rec];
90 NodeXForms.push_back(Rec);
91 Entry = NodeXForms.size();
97 } // end anonymous namespace.
99 static unsigned GetVBRSize(unsigned Val) {
100 if (Val <= 127) return 1;
102 unsigned NumBytes = 0;
110 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
112 static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
118 uint64_t InVal = Val;
119 unsigned NumBytes = 0;
121 OS << (Val&127) << "|128,";
127 OS << "/*" << InVal << "*/";
132 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
133 /// the number of bytes emitted.
134 unsigned MatcherTableEmitter::
135 EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
136 formatted_raw_ostream &OS) {
137 OS.PadToColumn(Indent*2);
139 switch (N->getKind()) {
140 case Matcher::Scope: {
141 const ScopeMatcher *SM = cast<ScopeMatcher>(N);
142 assert(SM->getNext() == 0 && "Shouldn't have next after scope");
144 unsigned StartIdx = CurrentIdx;
146 // Emit all of the children.
147 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
153 OS << "/*" << CurrentIdx << "*/";
154 OS.PadToColumn(Indent*2) << "/*Scope*/ ";
156 OS.PadToColumn(Indent*2);
159 // We need to encode the child and the offset of the failure code before
160 // emitting either of them. Handle this by buffering the output into a
161 // string while we get the size. Unfortunately, the offset of the
162 // children depends on the VBR size of the child, so for large children we
163 // have to iterate a bit.
164 SmallString<128> TmpBuf;
165 unsigned ChildSize = 0;
166 unsigned VBRSize = 0;
168 VBRSize = GetVBRSize(ChildSize);
171 raw_svector_ostream OS(TmpBuf);
172 formatted_raw_ostream FOS(OS);
173 ChildSize = EmitMatcherList(SM->getChild(i), Indent+1,
174 CurrentIdx+VBRSize, FOS);
175 } while (GetVBRSize(ChildSize) != VBRSize);
177 assert(ChildSize != 0 && "Should not have a zero-sized child!");
179 CurrentIdx += EmitVBRValue(ChildSize, OS);
181 OS << "/*->" << CurrentIdx+ChildSize << "*/";
184 OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
185 << " children in Scope";
188 OS << '\n' << TmpBuf.str();
189 CurrentIdx += ChildSize;
192 // Emit a zero as a sentinel indicating end of 'Scope'.
194 OS << "/*" << CurrentIdx << "*/";
195 OS.PadToColumn(Indent*2) << "0, ";
197 OS << "/*End of Scope*/";
199 return CurrentIdx - StartIdx + 1;
202 case Matcher::RecordNode:
203 OS << "OPC_RecordNode,";
205 OS.PadToColumn(CommentIndent) << "// #"
206 << cast<RecordMatcher>(N)->getResultNo() << " = "
207 << cast<RecordMatcher>(N)->getWhatFor();
211 case Matcher::RecordChild:
212 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
215 OS.PadToColumn(CommentIndent) << "// #"
216 << cast<RecordChildMatcher>(N)->getResultNo() << " = "
217 << cast<RecordChildMatcher>(N)->getWhatFor();
221 case Matcher::RecordMemRef:
222 OS << "OPC_RecordMemRef,\n";
225 case Matcher::CaptureFlagInput:
226 OS << "OPC_CaptureFlagInput,\n";
229 case Matcher::MoveChild:
230 OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
233 case Matcher::MoveParent:
234 OS << "OPC_MoveParent,\n";
237 case Matcher::CheckSame:
238 OS << "OPC_CheckSame, "
239 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
242 case Matcher::CheckPatternPredicate: {
243 StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
244 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
246 OS.PadToColumn(CommentIndent) << "// " << Pred;
250 case Matcher::CheckPredicate: {
251 StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
252 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
254 OS.PadToColumn(CommentIndent) << "// " << Pred;
259 case Matcher::CheckOpcode:
260 OS << "OPC_CheckOpcode, "
261 << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
264 case Matcher::SwitchOpcode: {
265 unsigned StartIdx = CurrentIdx;
266 const SwitchOpcodeMatcher *SOM = cast<SwitchOpcodeMatcher>(N);
267 OS << "OPC_SwitchOpcode ";
269 OS << "/*" << SOM->getNumCases() << " cases */";
273 // For each case we emit the size, then the opcode, then the matcher.
274 for (unsigned i = 0, e = SOM->getNumCases(); i != e; ++i) {
275 // We need to encode the opcode and the offset of the case code before
276 // emitting the case code. Handle this by buffering the output into a
277 // string while we get the size. Unfortunately, the offset of the
278 // children depends on the VBR size of the child, so for large children we
279 // have to iterate a bit.
280 SmallString<128> TmpBuf;
281 unsigned ChildSize = 0;
282 unsigned VBRSize = 0;
284 VBRSize = GetVBRSize(ChildSize);
287 raw_svector_ostream OS(TmpBuf);
288 formatted_raw_ostream FOS(OS);
289 ChildSize = EmitMatcherList(SOM->getCaseMatcher(i),
290 Indent+1, CurrentIdx+VBRSize+1, FOS);
291 } while (GetVBRSize(ChildSize) != VBRSize);
293 assert(ChildSize != 0 && "Should not have a zero-sized child!");
296 OS.PadToColumn(Indent*2);
298 OS << "/*SwitchOpcode*/ ";
302 CurrentIdx += EmitVBRValue(ChildSize, OS);
304 OS << " " << SOM->getCaseOpcode(i).getEnumName() << ",";
306 OS << "// ->" << CurrentIdx+ChildSize+1;
310 CurrentIdx += ChildSize;
313 // Emit the final zero to terminate the switch.
314 OS.PadToColumn(Indent*2) << "0, ";
316 OS << "// EndSwitchOpcode";
319 return CurrentIdx-StartIdx;
322 case Matcher::CheckType:
323 OS << "OPC_CheckType, "
324 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
326 case Matcher::CheckChildType:
327 OS << "OPC_CheckChild"
328 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
329 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
332 case Matcher::CheckInteger:
333 OS << "OPC_CheckInteger, ";
334 return 1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
335 case Matcher::CheckCondCode:
336 OS << "OPC_CheckCondCode, ISD::"
337 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
340 case Matcher::CheckValueType:
341 OS << "OPC_CheckValueType, MVT::"
342 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
345 case Matcher::CheckComplexPat: {
346 const ComplexPattern &Pattern =
347 cast<CheckComplexPatMatcher>(N)->getPattern();
348 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
350 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
351 OS << ": " << Pattern.getNumOperands() << " operands";
352 if (Pattern.hasProperty(SDNPHasChain))
353 OS << " + chain result and input";
359 case Matcher::CheckAndImm:
360 OS << "OPC_CheckAndImm, ";
361 return 1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
363 case Matcher::CheckOrImm:
364 OS << "OPC_CheckOrImm, ";
365 return 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
367 case Matcher::CheckFoldableChainNode:
368 OS << "OPC_CheckFoldableChainNode,\n";
370 case Matcher::CheckChainCompatible:
371 OS << "OPC_CheckChainCompatible, "
372 << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
375 case Matcher::EmitInteger: {
376 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
377 OS << "OPC_EmitInteger, "
378 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
379 return 2+EmitVBRValue(Val, OS);
381 case Matcher::EmitStringInteger: {
382 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
383 // These should always fit into one byte.
384 OS << "OPC_EmitInteger, "
385 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
390 case Matcher::EmitRegister:
391 OS << "OPC_EmitRegister, "
392 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
393 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
394 OS << getQualifiedName(R) << ",\n";
398 OS << "/*zero_reg*/";
403 case Matcher::EmitConvertToTarget:
404 OS << "OPC_EmitConvertToTarget, "
405 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
408 case Matcher::EmitMergeInputChains: {
409 const EmitMergeInputChainsMatcher *MN =
410 cast<EmitMergeInputChainsMatcher>(N);
411 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
412 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
413 OS << MN->getNode(i) << ", ";
415 return 2+MN->getNumNodes();
417 case Matcher::EmitCopyToReg:
418 OS << "OPC_EmitCopyToReg, "
419 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
420 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
423 case Matcher::EmitNodeXForm: {
424 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
425 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
426 << XF->getSlot() << ',';
428 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName();
433 case Matcher::EmitNode:
434 case Matcher::MorphNodeTo: {
435 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
436 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
437 OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
439 if (EN->hasChain()) OS << "|OPFL_Chain";
440 if (EN->hasInFlag()) OS << "|OPFL_FlagInput";
441 if (EN->hasOutFlag()) OS << "|OPFL_FlagOutput";
442 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
443 if (EN->getNumFixedArityOperands() != -1)
444 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
447 OS.PadToColumn(Indent*2+4) << EN->getNumVTs();
451 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
452 OS << getEnumName(EN->getVT(i)) << ", ";
454 OS << EN->getNumOperands();
458 unsigned NumOperandBytes = 0;
459 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
460 // We emit the operand numbers in VBR encoded format, in case the number
461 // is too large to represent with a byte.
462 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
466 // Print the result #'s for EmitNode.
467 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
468 if (unsigned NumResults = EN->getNumVTs()) {
469 OS.PadToColumn(CommentIndent) << "// Results = ";
470 unsigned First = E->getFirstResultSlot();
471 for (unsigned i = 0; i != NumResults; ++i)
472 OS << "#" << First+i << " ";
477 if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
478 OS.PadToColumn(Indent*2) << "// Src: "
479 << *SNT->getPattern().getSrcPattern() << '\n';
480 OS.PadToColumn(Indent*2) << "// Dst: "
481 << *SNT->getPattern().getDstPattern() << '\n';
486 return 6+EN->getNumVTs()+NumOperandBytes;
488 case Matcher::MarkFlagResults: {
489 const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
490 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
491 unsigned NumOperandBytes = 0;
492 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
493 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
495 return 2+NumOperandBytes;
497 case Matcher::CompleteMatch: {
498 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
499 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
500 unsigned NumResultBytes = 0;
501 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
502 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
505 OS.PadToColumn(Indent*2) << "// Src: "
506 << *CM->getPattern().getSrcPattern() << '\n';
507 OS.PadToColumn(Indent*2) << "// Dst: "
508 << *CM->getPattern().getDstPattern();
511 return 2 + NumResultBytes;
514 assert(0 && "Unreachable");
518 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
519 unsigned MatcherTableEmitter::
520 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
521 formatted_raw_ostream &OS) {
524 if (unsigned(N->getKind()) >= Histogram.size())
525 Histogram.resize(N->getKind()+1);
526 Histogram[N->getKind()]++;
528 OS << "/*" << CurrentIdx << "*/";
529 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
531 CurrentIdx += MatcherSize;
533 // If there are other nodes in this list, iterate to them, otherwise we're
540 void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
541 formatted_raw_ostream &OS) {
542 // Emit pattern predicates.
543 if (!PatternPredicates.empty()) {
544 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
545 OS << " switch (PredNo) {\n";
546 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
547 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
548 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
553 // Emit Node predicates.
554 // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
555 StringMap<TreePattern*> PFsByName;
557 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
559 PFsByName[I->first->getName()] = I->second;
561 if (!NodePredicates.empty()) {
562 OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
563 OS << " switch (PredNo) {\n";
564 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
565 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
566 // FIXME: Storing this by name is horrible.
567 TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
568 assert(P && "Unknown name?");
570 // Emit the predicate code corresponding to this pattern.
571 std::string Code = P->getRecord()->getValueAsCode("Predicate");
572 assert(!Code.empty() && "No code in this predicate");
573 OS << " case " << i << ": { // " << NodePredicates[i] << '\n';
574 std::string ClassName;
575 if (P->getOnlyTree()->isLeaf())
576 ClassName = "SDNode";
579 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
580 if (ClassName == "SDNode")
581 OS << " SDNode *N = Node;\n";
583 OS << " " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
584 OS << Code << "\n }\n";
590 // Emit CompletePattern matchers.
591 // FIXME: This should be const.
592 if (!ComplexPatterns.empty()) {
593 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
594 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
595 OS << " switch (PatternNo) {\n";
596 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
597 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
598 const ComplexPattern &P = *ComplexPatterns[i];
599 unsigned NumOps = P.getNumOperands();
601 if (P.hasProperty(SDNPHasChain))
602 ++NumOps; // Get the chained node too.
604 OS << " case " << i << ":\n";
605 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
606 OS << " return " << P.getSelectFunc();
609 for (unsigned i = 0; i != NumOps; ++i)
610 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
618 // Emit SDNodeXForm handlers.
619 // FIXME: This should be const.
620 if (!NodeXForms.empty()) {
621 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
622 OS << " switch (XFormNo) {\n";
623 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
625 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
626 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
627 const CodeGenDAGPatterns::NodeXForm &Entry =
628 CGP.getSDNodeTransform(NodeXForms[i]);
630 Record *SDNode = Entry.first;
631 const std::string &Code = Entry.second;
633 OS << " case " << i << ": { ";
635 OS << "// " << NodeXForms[i]->getName();
638 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
639 if (ClassName == "SDNode")
640 OS << " SDNode *N = V.getNode();\n";
642 OS << " " << ClassName << " *N = cast<" << ClassName
643 << ">(V.getNode());\n";
644 OS << Code << "\n }\n";
651 void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
654 OS << " // Opcode Histogram:\n";
655 for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
657 switch ((Matcher::KindTy)i) {
658 case Matcher::Scope: OS << "OPC_Scope"; break;
659 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
660 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
661 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
662 case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
663 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
664 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
665 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
666 case Matcher::CheckPatternPredicate:
667 OS << "OPC_CheckPatternPredicate"; break;
668 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
669 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
670 case Matcher::SwitchOpcode: OS << "OPC_SwitchOpcode"; break;
671 case Matcher::CheckType: OS << "OPC_CheckType"; break;
672 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
673 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
674 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
675 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
676 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
677 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
678 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
679 case Matcher::CheckFoldableChainNode:
680 OS << "OPC_CheckFoldableChainNode"; break;
681 case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
682 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
683 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
684 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
685 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
686 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
687 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
688 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
689 case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
690 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
691 case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
692 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
695 OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
701 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
702 const CodeGenDAGPatterns &CGP, raw_ostream &O) {
703 formatted_raw_ostream OS(O);
705 OS << "// The main instruction selector code.\n";
706 OS << "SDNode *SelectCode(SDNode *N) {\n";
708 MatcherTableEmitter MatcherEmitter;
710 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
711 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
712 OS << " static const unsigned char MatcherTable[] = {\n";
713 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
714 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
716 MatcherEmitter.EmitHistogram(OS);
718 OS << " #undef TARGET_OPCODE\n";
719 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
722 // Next up, emit the function for node and pattern predicates:
723 MatcherEmitter.EmitPredicateFunctions(CGP, OS);