inline the node transforms and node predicates into the generated
[oota-llvm.git] / utils / TableGen / DAGISelMatcherEmitter.cpp
1 //===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains code to generate C++ code a matcher.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DAGISelMatcher.h"
15 #include "CodeGenDAGPatterns.h"
16 #include "Record.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/Support/FormattedStream.h"
21 using namespace llvm;
22
23 enum {
24   CommentIndent = 30
25 };
26
27 namespace {
28 class MatcherTableEmitter {
29   StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
30   std::vector<std::string> NodePredicates, PatternPredicates;
31
32   DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
33   std::vector<const ComplexPattern*> ComplexPatterns;
34
35
36   DenseMap<Record*, unsigned> NodeXFormMap;
37   std::vector<Record*> NodeXForms;
38
39   // Per opcode frequence count. 
40   std::vector<unsigned> Histogram;
41 public:
42   MatcherTableEmitter() {}
43
44   unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
45                            unsigned StartIdx, formatted_raw_ostream &OS);
46   
47   void EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
48                               formatted_raw_ostream &OS);
49   
50   void EmitHistogram(formatted_raw_ostream &OS);
51 private:
52   unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
53                        formatted_raw_ostream &OS);
54   
55   unsigned getNodePredicate(StringRef PredName) {
56     unsigned &Entry = NodePredicateMap[PredName];
57     if (Entry == 0) {
58       NodePredicates.push_back(PredName.str());
59       Entry = NodePredicates.size();
60     }
61     return Entry-1;
62   }
63   unsigned getPatternPredicate(StringRef PredName) {
64     unsigned &Entry = PatternPredicateMap[PredName];
65     if (Entry == 0) {
66       PatternPredicates.push_back(PredName.str());
67       Entry = PatternPredicates.size();
68     }
69     return Entry-1;
70   }
71   
72   unsigned getComplexPat(const ComplexPattern &P) {
73     unsigned &Entry = ComplexPatternMap[&P];
74     if (Entry == 0) {
75       ComplexPatterns.push_back(&P);
76       Entry = ComplexPatterns.size();
77     }
78     return Entry-1;
79   }
80   
81   unsigned getNodeXFormID(Record *Rec) {
82     unsigned &Entry = NodeXFormMap[Rec];
83     if (Entry == 0) {
84       NodeXForms.push_back(Rec);
85       Entry = NodeXForms.size();
86     }
87     return Entry-1;
88   }
89   
90 };
91 } // end anonymous namespace.
92
93 static unsigned GetVBRSize(unsigned Val) {
94   if (Val <= 127) return 1;
95   
96   unsigned NumBytes = 0;
97   while (Val >= 128) {
98     Val >>= 7;
99     ++NumBytes;
100   }
101   return NumBytes+1;
102 }
103
104 /// EmitVBRValue - Emit the specified value as a VBR, returning the number of
105 /// bytes emitted.
106 static uint64_t EmitVBRValue(uint64_t Val, raw_ostream &OS) {
107   if (Val <= 127) {
108     OS << Val << ", ";
109     return 1;
110   }
111   
112   uint64_t InVal = Val;
113   unsigned NumBytes = 0;
114   while (Val >= 128) {
115     OS << (Val&127) << "|128,";
116     Val >>= 7;
117     ++NumBytes;
118   }
119   OS << Val << "/*" << InVal << "*/, ";
120   return NumBytes+1;
121 }
122
123 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
124 /// the number of bytes emitted.
125 unsigned MatcherTableEmitter::
126 EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
127             formatted_raw_ostream &OS) {
128   OS.PadToColumn(Indent*2);
129   
130   switch (N->getKind()) {
131   case Matcher::Scope: {
132     const ScopeMatcher *SM = cast<ScopeMatcher>(N);
133     assert(SM->getNext() == 0 && "Shouldn't have next after scope");
134     
135     unsigned StartIdx = CurrentIdx;
136     
137     // Emit all of the children.
138     for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
139       if (i == 0) {
140         OS << "OPC_Scope, ";
141         ++CurrentIdx;
142       } else {
143         OS << "/*" << CurrentIdx << "*/";
144         OS.PadToColumn(Indent*2) << "/*Scope*/ ";
145       }
146
147       // We need to encode the child and the offset of the failure code before
148       // emitting either of them.  Handle this by buffering the output into a
149       // string while we get the size.  Unfortunately, the offset of the
150       // children depends on the VBR size of the child, so for large children we
151       // have to iterate a bit.
152       SmallString<128> TmpBuf;
153       unsigned ChildSize = 0;
154       unsigned VBRSize = 0;
155       do {
156         VBRSize = GetVBRSize(ChildSize);
157         
158         TmpBuf.clear();
159         raw_svector_ostream OS(TmpBuf);
160         formatted_raw_ostream FOS(OS);
161         ChildSize = EmitMatcherList(cast<ScopeMatcher>(N)->getChild(i),
162                                    Indent+1, CurrentIdx+VBRSize, FOS);
163       } while (GetVBRSize(ChildSize) != VBRSize);
164       
165       assert(ChildSize != 0 && "Should not have a zero-sized child!");
166     
167       CurrentIdx += EmitVBRValue(ChildSize, OS);
168       OS << "/*->" << CurrentIdx+ChildSize << "*/";
169       
170       if (i == 0)
171         OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
172           << " children in Scope";
173       
174       OS << '\n' << TmpBuf.str();
175       CurrentIdx += ChildSize;
176     }
177     
178     // Emit a zero as a sentinel indicating end of 'Scope'.
179     OS << "/*" << CurrentIdx << "*/";
180     OS.PadToColumn(Indent*2) << "0, /*End of Scope*/\n";
181     return CurrentIdx - StartIdx + 1;
182   }
183       
184   case Matcher::RecordNode:
185     OS << "OPC_RecordNode,";
186     OS.PadToColumn(CommentIndent) << "// "
187        << cast<RecordMatcher>(N)->getWhatFor() << '\n';
188     return 1;
189
190   case Matcher::RecordChild:
191     OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
192        << ',';
193     OS.PadToColumn(CommentIndent) << "// "
194       << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
195     return 1;
196       
197   case Matcher::RecordMemRef:
198     OS << "OPC_RecordMemRef,\n";
199     return 1;
200       
201   case Matcher::CaptureFlagInput:
202     OS << "OPC_CaptureFlagInput,\n";
203     return 1;
204       
205   case Matcher::MoveChild:
206     OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
207     return 2;
208       
209   case Matcher::MoveParent:
210     OS << "OPC_MoveParent,\n";
211     return 1;
212       
213   case Matcher::CheckSame:
214     OS << "OPC_CheckSame, "
215        << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
216     return 2;
217
218   case Matcher::CheckPatternPredicate: {
219     StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
220     OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
221     OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
222     return 2;
223   }
224   case Matcher::CheckPredicate: {
225     StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
226     OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
227     OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
228     return 2;
229   }
230
231   case Matcher::CheckOpcode:
232     OS << "OPC_CheckOpcode, "
233        << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
234     return 2;
235       
236   case Matcher::CheckMultiOpcode: {
237     const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
238     OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodes() << ", ";
239     for (unsigned i = 0, e = CMO->getNumOpcodes(); i != e; ++i)
240       OS << CMO->getOpcode(i).getEnumName() << ", ";
241     OS << '\n';
242     return 2 + CMO->getNumOpcodes();
243   }
244       
245   case Matcher::CheckType:
246     OS << "OPC_CheckType, "
247        << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
248     return 2;
249   case Matcher::CheckChildType:
250     OS << "OPC_CheckChild"
251        << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
252        << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
253     return 2;
254       
255   case Matcher::CheckInteger:
256     OS << "OPC_CheckInteger, ";
257     return 1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
258   case Matcher::CheckCondCode:
259     OS << "OPC_CheckCondCode, ISD::"
260        << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
261     return 2;
262       
263   case Matcher::CheckValueType:
264     OS << "OPC_CheckValueType, MVT::"
265        << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
266     return 2;
267
268   case Matcher::CheckComplexPat: {
269     const ComplexPattern &Pattern =
270       cast<CheckComplexPatMatcher>(N)->getPattern();
271     OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
272     OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
273     OS << ": " << Pattern.getNumOperands() << " operands";
274     if (Pattern.hasProperty(SDNPHasChain))
275       OS << " + chain result and input";
276     OS << '\n';
277     return 2;
278   }
279       
280   case Matcher::CheckAndImm:
281     OS << "OPC_CheckAndImm, ";
282     return 1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
283
284   case Matcher::CheckOrImm:
285     OS << "OPC_CheckOrImm, ";
286     return 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
287       
288   case Matcher::CheckFoldableChainNode:
289     OS << "OPC_CheckFoldableChainNode,\n";
290     return 1;
291   case Matcher::CheckChainCompatible:
292     OS << "OPC_CheckChainCompatible, "
293        << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
294     return 2;
295       
296   case Matcher::EmitInteger: {
297     int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
298     OS << "OPC_EmitInteger, "
299        << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
300     return 2+EmitVBRValue(Val, OS);
301   }
302   case Matcher::EmitStringInteger: {
303     const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
304     // These should always fit into one byte.
305     OS << "OPC_EmitInteger, "
306       << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
307       << Val << ",\n";
308     return 3;
309   }
310       
311   case Matcher::EmitRegister:
312     OS << "OPC_EmitRegister, "
313        << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
314     if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
315       OS << getQualifiedName(R) << ",\n";
316     else
317       OS << "0 /*zero_reg*/,\n";
318     return 3;
319       
320   case Matcher::EmitConvertToTarget:
321     OS << "OPC_EmitConvertToTarget, "
322        << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
323     return 2;
324       
325   case Matcher::EmitMergeInputChains: {
326     const EmitMergeInputChainsMatcher *MN =
327       cast<EmitMergeInputChainsMatcher>(N);
328     OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
329     for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
330       OS << MN->getNode(i) << ", ";
331     OS << '\n';
332     return 2+MN->getNumNodes();
333   }
334   case Matcher::EmitCopyToReg:
335     OS << "OPC_EmitCopyToReg, "
336        << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
337        << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
338        << ",\n";
339     return 3;
340   case Matcher::EmitNodeXForm: {
341     const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
342     OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
343        << XF->getSlot() << ',';
344     OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
345     return 3;
346   }
347       
348   case Matcher::EmitNode:
349   case Matcher::MorphNodeTo: {
350     const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
351     OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
352     OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
353     
354     if (EN->hasChain())   OS << "|OPFL_Chain";
355     if (EN->hasInFlag())  OS << "|OPFL_FlagInput";
356     if (EN->hasOutFlag()) OS << "|OPFL_FlagOutput";
357     if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
358     if (EN->getNumFixedArityOperands() != -1)
359       OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
360     OS << ",\n";
361     
362     OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
363     for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
364       OS << getEnumName(EN->getVT(i)) << ", ";
365
366     OS << EN->getNumOperands() << "/*#Ops*/, ";
367     unsigned NumOperandBytes = 0;
368     for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
369       // We emit the operand numbers in VBR encoded format, in case the number
370       // is too large to represent with a byte.
371       NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
372     }
373     
374     // Print the result #'s for EmitNode.
375     if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
376       if (unsigned NumResults = EN->getNumVTs()) {
377         OS.PadToColumn(CommentIndent) << "// Results = ";
378         unsigned First = E->getFirstResultSlot();
379         for (unsigned i = 0; i != NumResults; ++i)
380           OS << "#" << First+i << " ";
381       }
382     }
383     OS << '\n';
384     
385     if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
386       OS.PadToColumn(Indent*2) << "// Src: "
387       << *SNT->getPattern().getSrcPattern() << '\n';
388       OS.PadToColumn(Indent*2) << "// Dst: " 
389       << *SNT->getPattern().getDstPattern() << '\n';
390       
391     }
392     
393     return 6+EN->getNumVTs()+NumOperandBytes;
394   }
395   case Matcher::MarkFlagResults: {
396     const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
397     OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
398     unsigned NumOperandBytes = 0;
399     for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
400       NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
401     OS << '\n';
402     return 2+NumOperandBytes;
403   }
404   case Matcher::CompleteMatch: {
405     const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
406     OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
407     unsigned NumResultBytes = 0;
408     for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
409       NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
410     OS << '\n';
411     OS.PadToColumn(Indent*2) << "// Src: "
412       << *CM->getPattern().getSrcPattern() << '\n';
413     OS.PadToColumn(Indent*2) << "// Dst: " 
414       << *CM->getPattern().getDstPattern() << '\n';
415     return 2 + NumResultBytes;
416   }
417   }
418   assert(0 && "Unreachable");
419   return 0;
420 }
421
422 /// EmitMatcherList - Emit the bytes for the specified matcher subtree.
423 unsigned MatcherTableEmitter::
424 EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
425                 formatted_raw_ostream &OS) {
426   unsigned Size = 0;
427   while (N) {
428     if (unsigned(N->getKind()) >= Histogram.size())
429       Histogram.resize(N->getKind()+1);
430     Histogram[N->getKind()]++;
431     
432     OS << "/*" << CurrentIdx << "*/";
433     unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
434     Size += MatcherSize;
435     CurrentIdx += MatcherSize;
436     
437     // If there are other nodes in this list, iterate to them, otherwise we're
438     // done.
439     N = N->getNext();
440   }
441   return Size;
442 }
443
444 void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
445                                                  formatted_raw_ostream &OS) {
446   // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
447   // here into the case stmts.
448   
449   // Emit pattern predicates.
450   if (!PatternPredicates.empty()) {
451     OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
452     OS << "  switch (PredNo) {\n";
453     OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
454     for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
455       OS << "  case " << i << ": return "  << PatternPredicates[i] << ";\n";
456     OS << "  }\n";
457     OS << "}\n\n";
458   }
459    
460   // Emit Node predicates.
461   // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
462   StringMap<TreePattern*> PFsByName;
463   
464   for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
465        I != E; ++I)
466     PFsByName[I->first->getName()] = I->second;
467   
468   if (!NodePredicates.empty()) {
469     OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
470     OS << "  switch (PredNo) {\n";
471     OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
472     for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
473       // FIXME: Storing this by name is horrible.
474       TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
475       assert(P && "Unknown name?");
476       
477       // Emit the predicate code corresponding to this pattern.
478       std::string Code = P->getRecord()->getValueAsCode("Predicate");
479       assert(!Code.empty() && "No code in this predicate");
480       OS << "  case " << i << ": { // " << NodePredicates[i] << '\n';
481       std::string ClassName;
482       if (P->getOnlyTree()->isLeaf())
483         ClassName = "SDNode";
484       else
485         ClassName =
486           CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
487       if (ClassName == "SDNode")
488         OS << "    SDNode *N = Node;\n";
489       else
490         OS << "    " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
491       OS << Code << "\n  }\n";
492     }
493     OS << "  }\n";
494     OS << "}\n\n";
495   }
496   
497   // Emit CompletePattern matchers.
498   // FIXME: This should be const.
499   if (!ComplexPatterns.empty()) {
500     OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
501     OS << "      unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
502     OS << "  switch (PatternNo) {\n";
503     OS << "  default: assert(0 && \"Invalid pattern # in table?\");\n";
504     for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
505       const ComplexPattern &P = *ComplexPatterns[i];
506       unsigned NumOps = P.getNumOperands();
507
508       if (P.hasProperty(SDNPHasChain))
509         ++NumOps;  // Get the chained node too.
510       
511       OS << "  case " << i << ":\n";
512       OS << "    Result.resize(Result.size()+" << NumOps << ");\n";
513       OS << "    return "  << P.getSelectFunc();
514
515       // FIXME: Temporary hack until old isel dies.
516       if (P.hasProperty(SDNPHasChain))
517         OS << "XXX";
518       
519       OS << "(Root, N";
520       for (unsigned i = 0; i != NumOps; ++i)
521         OS << ", Result[Result.size()-" << (NumOps-i) << ']';
522       OS << ");\n";
523     }
524     OS << "  }\n";
525     OS << "}\n\n";
526   }
527   
528   
529   // Emit SDNodeXForm handlers.
530   // FIXME: This should be const.
531   if (!NodeXForms.empty()) {
532     OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
533     OS << "  switch (XFormNo) {\n";
534     OS << "  default: assert(0 && \"Invalid xform # in table?\");\n";
535     
536     // FIXME: The node xform could take SDValue's instead of SDNode*'s.
537     for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
538       const CodeGenDAGPatterns::NodeXForm &Entry =
539         CGP.getSDNodeTransform(NodeXForms[i]);
540       
541       Record *SDNode = Entry.first;
542       const std::string &Code = Entry.second;
543       
544       OS << "  case " << i << ": {  // " << NodeXForms[i]->getName() << '\n';
545       
546       std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
547       if (ClassName == "SDNode")
548         OS << "    SDNode *N = V.getNode();\n";
549       else
550         OS << "    " << ClassName << " *N = cast<" << ClassName
551            << ">(V.getNode());\n";
552       OS << Code << "\n  }\n";
553     }
554     OS << "  }\n";
555     OS << "}\n\n";
556   }
557 }
558
559 void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
560   OS << "  // Opcode Histogram:\n";
561   for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
562     OS << "  // #";
563     switch ((Matcher::KindTy)i) {
564     case Matcher::Scope: OS << "OPC_Scope"; break; 
565     case Matcher::RecordNode: OS << "OPC_RecordNode"; break; 
566     case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
567     case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
568     case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
569     case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
570     case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
571     case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
572     case Matcher::CheckPatternPredicate:
573       OS << "OPC_CheckPatternPredicate"; break;
574     case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
575     case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
576     case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
577     case Matcher::CheckType: OS << "OPC_CheckType"; break;
578     case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
579     case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
580     case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
581     case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
582     case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
583     case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
584     case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
585     case Matcher::CheckFoldableChainNode:
586       OS << "OPC_CheckFoldableChainNode"; break;
587     case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
588     case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
589     case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
590     case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
591     case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
592     case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
593     case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
594     case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
595     case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
596     case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
597     case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
598     case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;    
599     }
600     
601     OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
602   }
603   OS << '\n';
604 }
605
606
607 void llvm::EmitMatcherTable(const Matcher *TheMatcher,
608                             const CodeGenDAGPatterns &CGP, raw_ostream &O) {
609   formatted_raw_ostream OS(O);
610   
611   OS << "// The main instruction selector code.\n";
612   OS << "SDNode *SelectCode(SDNode *N) {\n";
613
614   MatcherTableEmitter MatcherEmitter;
615
616   OS << "  // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
617   OS << "  #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
618   OS << "  static const unsigned char MatcherTable[] = {\n";
619   unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
620   OS << "    0\n  }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
621   
622   MatcherEmitter.EmitHistogram(OS);
623   
624   OS << "  #undef TARGET_OPCODE\n";
625   OS << "  return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
626   OS << "\n";
627   
628   // Next up, emit the function for node and pattern predicates:
629   MatcherEmitter.EmitPredicateFunctions(CGP, OS);
630 }