DAGISelMatcherEmitter.cpp revision 0cebe6181af8a2cf5e6cf9497cda4c47b426a0f8
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"
21using namespace llvm;
22
23enum {
24  CommentIndent = 30
25};
26
27namespace {
28class 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;
41public:
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);
51private:
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
93static 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.
106static 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.
125unsigned MatcherTableEmitter::
126EmitMatcher(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)->getResultNo() << " = "
188       << cast<RecordMatcher>(N)->getWhatFor() << '\n';
189    return 1;
190
191  case Matcher::RecordChild:
192    OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
193       << ',';
194    OS.PadToColumn(CommentIndent) << "// #"
195      << cast<RecordChildMatcher>(N)->getResultNo() << " = "
196      << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
197    return 1;
198
199  case Matcher::RecordMemRef:
200    OS << "OPC_RecordMemRef,\n";
201    return 1;
202
203  case Matcher::CaptureFlagInput:
204    OS << "OPC_CaptureFlagInput,\n";
205    return 1;
206
207  case Matcher::MoveChild:
208    OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
209    return 2;
210
211  case Matcher::MoveParent:
212    OS << "OPC_MoveParent,\n";
213    return 1;
214
215  case Matcher::CheckSame:
216    OS << "OPC_CheckSame, "
217       << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
218    return 2;
219
220  case Matcher::CheckPatternPredicate: {
221    StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
222    OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
223    OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
224    return 2;
225  }
226  case Matcher::CheckPredicate: {
227    StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
228    OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
229    OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
230    return 2;
231  }
232
233  case Matcher::CheckOpcode:
234    OS << "OPC_CheckOpcode, "
235       << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
236    return 2;
237
238  case Matcher::CheckMultiOpcode: {
239    const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
240    OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodes() << ", ";
241    for (unsigned i = 0, e = CMO->getNumOpcodes(); i != e; ++i)
242      OS << CMO->getOpcode(i).getEnumName() << ", ";
243    OS << '\n';
244    return 2 + CMO->getNumOpcodes();
245  }
246
247  case Matcher::CheckType:
248    OS << "OPC_CheckType, "
249       << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
250    return 2;
251  case Matcher::CheckChildType:
252    OS << "OPC_CheckChild"
253       << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
254       << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
255    return 2;
256
257  case Matcher::CheckInteger:
258    OS << "OPC_CheckInteger, ";
259    return 1+EmitVBRValue(cast<CheckIntegerMatcher>(N)->getValue(), OS);
260  case Matcher::CheckCondCode:
261    OS << "OPC_CheckCondCode, ISD::"
262       << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
263    return 2;
264
265  case Matcher::CheckValueType:
266    OS << "OPC_CheckValueType, MVT::"
267       << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
268    return 2;
269
270  case Matcher::CheckComplexPat: {
271    const ComplexPattern &Pattern =
272      cast<CheckComplexPatMatcher>(N)->getPattern();
273    OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
274    OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
275    OS << ": " << Pattern.getNumOperands() << " operands";
276    if (Pattern.hasProperty(SDNPHasChain))
277      OS << " + chain result and input";
278    OS << '\n';
279    return 2;
280  }
281
282  case Matcher::CheckAndImm:
283    OS << "OPC_CheckAndImm, ";
284    return 1+EmitVBRValue(cast<CheckAndImmMatcher>(N)->getValue(), OS);
285
286  case Matcher::CheckOrImm:
287    OS << "OPC_CheckOrImm, ";
288    return 1+EmitVBRValue(cast<CheckOrImmMatcher>(N)->getValue(), OS);
289
290  case Matcher::CheckFoldableChainNode:
291    OS << "OPC_CheckFoldableChainNode,\n";
292    return 1;
293  case Matcher::CheckChainCompatible:
294    OS << "OPC_CheckChainCompatible, "
295       << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
296    return 2;
297
298  case Matcher::EmitInteger: {
299    int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
300    OS << "OPC_EmitInteger, "
301       << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
302    return 2+EmitVBRValue(Val, OS);
303  }
304  case Matcher::EmitStringInteger: {
305    const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
306    // These should always fit into one byte.
307    OS << "OPC_EmitInteger, "
308      << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
309      << Val << ",\n";
310    return 3;
311  }
312
313  case Matcher::EmitRegister:
314    OS << "OPC_EmitRegister, "
315       << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
316    if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
317      OS << getQualifiedName(R) << ",\n";
318    else
319      OS << "0 /*zero_reg*/,\n";
320    return 3;
321
322  case Matcher::EmitConvertToTarget:
323    OS << "OPC_EmitConvertToTarget, "
324       << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
325    return 2;
326
327  case Matcher::EmitMergeInputChains: {
328    const EmitMergeInputChainsMatcher *MN =
329      cast<EmitMergeInputChainsMatcher>(N);
330    OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
331    for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
332      OS << MN->getNode(i) << ", ";
333    OS << '\n';
334    return 2+MN->getNumNodes();
335  }
336  case Matcher::EmitCopyToReg:
337    OS << "OPC_EmitCopyToReg, "
338       << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
339       << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
340       << ",\n";
341    return 3;
342  case Matcher::EmitNodeXForm: {
343    const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
344    OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
345       << XF->getSlot() << ',';
346    OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
347    return 3;
348  }
349
350  case Matcher::EmitNode:
351  case Matcher::MorphNodeTo: {
352    const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
353    OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_MorphNodeTo");
354    OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
355
356    if (EN->hasChain())   OS << "|OPFL_Chain";
357    if (EN->hasInFlag())  OS << "|OPFL_FlagInput";
358    if (EN->hasOutFlag()) OS << "|OPFL_FlagOutput";
359    if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
360    if (EN->getNumFixedArityOperands() != -1)
361      OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
362    OS << ",\n";
363
364    OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
365    for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
366      OS << getEnumName(EN->getVT(i)) << ", ";
367
368    OS << EN->getNumOperands() << "/*#Ops*/, ";
369    unsigned NumOperandBytes = 0;
370    for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
371      // We emit the operand numbers in VBR encoded format, in case the number
372      // is too large to represent with a byte.
373      NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
374    }
375
376    // Print the result #'s for EmitNode.
377    if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
378      if (unsigned NumResults = EN->getNumVTs()) {
379        OS.PadToColumn(CommentIndent) << "// Results = ";
380        unsigned First = E->getFirstResultSlot();
381        for (unsigned i = 0; i != NumResults; ++i)
382          OS << "#" << First+i << " ";
383      }
384    }
385    OS << '\n';
386
387    if (const MorphNodeToMatcher *SNT = dyn_cast<MorphNodeToMatcher>(N)) {
388      OS.PadToColumn(Indent*2) << "// Src: "
389      << *SNT->getPattern().getSrcPattern() << '\n';
390      OS.PadToColumn(Indent*2) << "// Dst: "
391      << *SNT->getPattern().getDstPattern() << '\n';
392
393    }
394
395    return 6+EN->getNumVTs()+NumOperandBytes;
396  }
397  case Matcher::MarkFlagResults: {
398    const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
399    OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
400    unsigned NumOperandBytes = 0;
401    for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
402      NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
403    OS << '\n';
404    return 2+NumOperandBytes;
405  }
406  case Matcher::CompleteMatch: {
407    const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
408    OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
409    unsigned NumResultBytes = 0;
410    for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
411      NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
412    OS << '\n';
413    OS.PadToColumn(Indent*2) << "// Src: "
414      << *CM->getPattern().getSrcPattern() << '\n';
415    OS.PadToColumn(Indent*2) << "// Dst: "
416      << *CM->getPattern().getDstPattern() << '\n';
417    return 2 + NumResultBytes;
418  }
419  }
420  assert(0 && "Unreachable");
421  return 0;
422}
423
424/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
425unsigned MatcherTableEmitter::
426EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
427                formatted_raw_ostream &OS) {
428  unsigned Size = 0;
429  while (N) {
430    if (unsigned(N->getKind()) >= Histogram.size())
431      Histogram.resize(N->getKind()+1);
432    Histogram[N->getKind()]++;
433
434    OS << "/*" << CurrentIdx << "*/";
435    unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
436    Size += MatcherSize;
437    CurrentIdx += MatcherSize;
438
439    // If there are other nodes in this list, iterate to them, otherwise we're
440    // done.
441    N = N->getNext();
442  }
443  return Size;
444}
445
446void MatcherTableEmitter::EmitPredicateFunctions(const CodeGenDAGPatterns &CGP,
447                                                 formatted_raw_ostream &OS) {
448  // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
449  // here into the case stmts.
450
451  // Emit pattern predicates.
452  if (!PatternPredicates.empty()) {
453    OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
454    OS << "  switch (PredNo) {\n";
455    OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
456    for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
457      OS << "  case " << i << ": return "  << PatternPredicates[i] << ";\n";
458    OS << "  }\n";
459    OS << "}\n\n";
460  }
461
462  // Emit Node predicates.
463  // FIXME: Annoyingly, these are stored by name, which we never even emit. Yay?
464  StringMap<TreePattern*> PFsByName;
465
466  for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
467       I != E; ++I)
468    PFsByName[I->first->getName()] = I->second;
469
470  if (!NodePredicates.empty()) {
471    OS << "bool CheckNodePredicate(SDNode *Node, unsigned PredNo) const {\n";
472    OS << "  switch (PredNo) {\n";
473    OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
474    for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i) {
475      // FIXME: Storing this by name is horrible.
476      TreePattern *P =PFsByName[NodePredicates[i].substr(strlen("Predicate_"))];
477      assert(P && "Unknown name?");
478
479      // Emit the predicate code corresponding to this pattern.
480      std::string Code = P->getRecord()->getValueAsCode("Predicate");
481      assert(!Code.empty() && "No code in this predicate");
482      OS << "  case " << i << ": { // " << NodePredicates[i] << '\n';
483      std::string ClassName;
484      if (P->getOnlyTree()->isLeaf())
485        ClassName = "SDNode";
486      else
487        ClassName =
488          CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
489      if (ClassName == "SDNode")
490        OS << "    SDNode *N = Node;\n";
491      else
492        OS << "    " << ClassName << "*N = cast<" << ClassName << ">(Node);\n";
493      OS << Code << "\n  }\n";
494    }
495    OS << "  }\n";
496    OS << "}\n\n";
497  }
498
499  // Emit CompletePattern matchers.
500  // FIXME: This should be const.
501  if (!ComplexPatterns.empty()) {
502    OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
503    OS << "      unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
504    OS << "  switch (PatternNo) {\n";
505    OS << "  default: assert(0 && \"Invalid pattern # in table?\");\n";
506    for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
507      const ComplexPattern &P = *ComplexPatterns[i];
508      unsigned NumOps = P.getNumOperands();
509
510      if (P.hasProperty(SDNPHasChain))
511        ++NumOps;  // Get the chained node too.
512
513      OS << "  case " << i << ":\n";
514      OS << "    Result.resize(Result.size()+" << NumOps << ");\n";
515      OS << "    return "  << P.getSelectFunc();
516
517      // FIXME: Temporary hack until old isel dies.
518      if (P.hasProperty(SDNPHasChain))
519        OS << "XXX";
520
521      OS << "(Root, N";
522      for (unsigned i = 0; i != NumOps; ++i)
523        OS << ", Result[Result.size()-" << (NumOps-i) << ']';
524      OS << ");\n";
525    }
526    OS << "  }\n";
527    OS << "}\n\n";
528  }
529
530
531  // Emit SDNodeXForm handlers.
532  // FIXME: This should be const.
533  if (!NodeXForms.empty()) {
534    OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
535    OS << "  switch (XFormNo) {\n";
536    OS << "  default: assert(0 && \"Invalid xform # in table?\");\n";
537
538    // FIXME: The node xform could take SDValue's instead of SDNode*'s.
539    for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i) {
540      const CodeGenDAGPatterns::NodeXForm &Entry =
541        CGP.getSDNodeTransform(NodeXForms[i]);
542
543      Record *SDNode = Entry.first;
544      const std::string &Code = Entry.second;
545
546      OS << "  case " << i << ": {  // " << NodeXForms[i]->getName() << '\n';
547
548      std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
549      if (ClassName == "SDNode")
550        OS << "    SDNode *N = V.getNode();\n";
551      else
552        OS << "    " << ClassName << " *N = cast<" << ClassName
553           << ">(V.getNode());\n";
554      OS << Code << "\n  }\n";
555    }
556    OS << "  }\n";
557    OS << "}\n\n";
558  }
559}
560
561void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
562  OS << "  // Opcode Histogram:\n";
563  for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
564    OS << "  // #";
565    switch ((Matcher::KindTy)i) {
566    case Matcher::Scope: OS << "OPC_Scope"; break;
567    case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
568    case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
569    case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
570    case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
571    case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
572    case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
573    case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
574    case Matcher::CheckPatternPredicate:
575      OS << "OPC_CheckPatternPredicate"; break;
576    case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
577    case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
578    case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
579    case Matcher::CheckType: OS << "OPC_CheckType"; break;
580    case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
581    case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
582    case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
583    case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
584    case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
585    case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
586    case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
587    case Matcher::CheckFoldableChainNode:
588      OS << "OPC_CheckFoldableChainNode"; break;
589    case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
590    case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
591    case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
592    case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
593    case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
594    case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
595    case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
596    case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
597    case Matcher::MorphNodeTo: OS << "OPC_MorphNodeTo"; break;
598    case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
599    case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
600    case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
601    }
602
603    OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
604  }
605  OS << '\n';
606}
607
608
609void llvm::EmitMatcherTable(const Matcher *TheMatcher,
610                            const CodeGenDAGPatterns &CGP, raw_ostream &O) {
611  formatted_raw_ostream OS(O);
612
613  OS << "// The main instruction selector code.\n";
614  OS << "SDNode *SelectCode(SDNode *N) {\n";
615
616  MatcherTableEmitter MatcherEmitter;
617
618  OS << "  // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
619  OS << "  #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
620  OS << "  static const unsigned char MatcherTable[] = {\n";
621  unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
622  OS << "    0\n  }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
623
624  MatcherEmitter.EmitHistogram(OS);
625
626  OS << "  #undef TARGET_OPCODE\n";
627  OS << "  return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
628  OS << "\n";
629
630  // Next up, emit the function for node and pattern predicates:
631  MatcherEmitter.EmitPredicateFunctions(CGP, OS);
632}
633