make the new isel generator plop out a CheckComplexPattern function
[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 "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/Support/FormattedStream.h"
20 using namespace llvm;
21
22 namespace {
23 enum {
24   CommentIndent = 25
25 };
26 }
27
28 /// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
29 /// fits in 1, 2, 4, or 8 sign extended bytes.
30 static char ClassifyInt(int64_t Val) {
31   if (Val == int8_t(Val))  return '1';
32   if (Val == int16_t(Val)) return '2';
33   if (Val == int32_t(Val)) return '4';
34   return '8';
35 }
36
37 /// EmitInt - Emit the specified integer, returning the number of bytes emitted.
38 static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) {
39   unsigned BytesEmitted = 1;
40   OS << (int)(unsigned char)Val << ", ";
41   if (Val == int8_t(Val)) {
42     OS << "\n";
43     return BytesEmitted;
44   }
45   
46   OS << (int)(unsigned char)(Val >> 8) << ", ";
47   ++BytesEmitted;
48   
49   if (Val != int16_t(Val)) {
50     OS << (int)(unsigned char)(Val >> 16) << ','
51        << (int)(unsigned char)(Val >> 24) << ',';
52     BytesEmitted += 2;
53     
54     if (Val != int32_t(Val)) {
55       OS << (int)(unsigned char)(Val >> 32) << ','
56          << (int)(unsigned char)(Val >> 40) << ','
57          << (int)(unsigned char)(Val >> 48) << ','
58          << (int)(unsigned char)(Val >> 56) << ',';
59       BytesEmitted += 4;
60     }   
61   }
62   
63   OS.PadToColumn(CommentIndent) << "// " << Val << '\n';
64   return BytesEmitted;
65 }
66
67 namespace {
68 class MatcherTableEmitter {
69   formatted_raw_ostream &OS;
70   
71   StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
72   std::vector<std::string> NodePredicates, PatternPredicates;
73
74   DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
75   std::vector<const ComplexPattern*> ComplexPatterns;
76 public:
77   MatcherTableEmitter(formatted_raw_ostream &os) : OS(os) {}
78
79   unsigned EmitMatcherAndChildren(const MatcherNode *N, unsigned Indent);
80   
81   void EmitPredicateFunctions();
82 private:
83   unsigned EmitMatcher(const MatcherNode *N, unsigned Indent);
84   
85   unsigned getNodePredicate(StringRef PredName) {
86     unsigned &Entry = NodePredicateMap[PredName];
87     if (Entry == 0) {
88       NodePredicates.push_back(PredName.str());
89       Entry = NodePredicates.size();
90     }
91     return Entry-1;
92   }
93   unsigned getPatternPredicate(StringRef PredName) {
94     unsigned &Entry = PatternPredicateMap[PredName];
95     if (Entry == 0) {
96       PatternPredicates.push_back(PredName.str());
97       Entry = PatternPredicates.size();
98     }
99     return Entry-1;
100   }
101   
102   unsigned getComplexPat(const ComplexPattern &P) {
103     unsigned &Entry = ComplexPatternMap[&P];
104     if (Entry == 0) {
105       ComplexPatterns.push_back(&P);
106       Entry = ComplexPatterns.size();
107     }
108     return Entry-1;
109   }
110 };
111 } // end anonymous namespace.
112
113 /// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
114 /// the number of bytes emitted.
115 unsigned MatcherTableEmitter::
116 EmitMatcher(const MatcherNode *N, unsigned Indent) {
117   OS.PadToColumn(Indent*2);
118   
119   switch (N->getKind()) {
120   case MatcherNode::Push: assert(0 && "Should be handled by caller");
121   case MatcherNode::EmitNode:
122     OS << "OPC_Emit, /*XXX*/";
123     OS.PadToColumn(CommentIndent) << "// Src: "
124       << *cast<EmitNodeMatcherNode>(N)->getPattern().getSrcPattern() << '\n';
125     OS.PadToColumn(CommentIndent) << "// Dst: "
126       << *cast<EmitNodeMatcherNode>(N)->getPattern().getDstPattern() << '\n';
127     return 1;
128   case MatcherNode::Record:
129     OS << "OPC_Record,\n";
130     return 1;
131   case MatcherNode::MoveChild:
132     OS << "OPC_MoveChild, "
133        << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
134     return 2;
135       
136   case MatcherNode::MoveParent:
137     OS << "OPC_MoveParent,\n";
138     return 1;
139       
140   case MatcherNode::CheckSame:
141     OS << "OPC_CheckSame, "
142        << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
143     return 2;
144
145   case MatcherNode::CheckPatternPredicate: {
146     StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate();
147     OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
148     OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
149     return 2;
150   }
151   case MatcherNode::CheckPredicate: {
152     StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName();
153     OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
154     OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
155     return 2;
156   }
157
158   case MatcherNode::CheckOpcode:
159     OS << "OPC_CheckOpcode, "
160        << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
161     return 2;
162       
163   case MatcherNode::CheckType:
164     OS << "OPC_CheckType, "
165        << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
166     return 2;
167
168   case MatcherNode::CheckInteger: {
169     int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
170     OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
171     return EmitInt(Val, OS)+1;
172   }   
173   case MatcherNode::CheckCondCode:
174     OS << "OPC_CheckCondCode, ISD::"
175        << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
176     return 2;
177       
178   case MatcherNode::CheckValueType:
179     OS << "OPC_CheckValueType, MVT::"
180        << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
181     return 2;
182
183   case MatcherNode::CheckComplexPat:
184     OS << "OPC_CheckComplexPat, "
185        << getComplexPat(cast<CheckComplexPatMatcherNode>(N)->getPattern())
186        << ",\n";
187     return 2;
188       
189   case MatcherNode::CheckAndImm: {
190     int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
191     OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
192     return EmitInt(Val, OS)+1;
193   }
194
195   case MatcherNode::CheckOrImm: {
196     int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
197     OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
198     return EmitInt(Val, OS)+1;
199   }
200   case MatcherNode::CheckFoldableChainNode:
201     OS << "OPC_CheckFoldableChainNode,\n";
202     return 1;
203   }
204   assert(0 && "Unreachable");
205   return 0;
206 }
207
208 /// EmitMatcherAndChildren - Emit the bytes for the specified matcher subtree.
209 unsigned MatcherTableEmitter::
210 EmitMatcherAndChildren(const MatcherNode *N, unsigned Indent) {
211   unsigned Size = 0;
212   while (1) {
213     // Push is a special case since it is binary.
214     if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
215       // We need to encode the child and the offset of the failure code before
216       // emitting either of them.  Handle this by buffering the output into a
217       // string while we get the size.
218       SmallString<128> TmpBuf;
219       unsigned ChildSize;
220       {
221         raw_svector_ostream OS(TmpBuf);
222         formatted_raw_ostream FOS(OS);
223         ChildSize = 
224           EmitMatcherAndChildren(cast<PushMatcherNode>(N)->getChild(),Indent+1);
225       }
226       
227       if (ChildSize > 255) {
228         errs() <<
229           "Tblgen internal error: can't handle predicate this complex yet\n";
230         exit(1);
231       }
232       
233       OS.PadToColumn(Indent*2);
234       OS << "OPC_Push, " << ChildSize << ",\n";
235       OS << TmpBuf.str();
236       
237       Size += 2 + ChildSize;
238       
239       N = PMN->getFailure();
240       continue;
241     }
242   
243     Size += EmitMatcher(N, Indent);
244     
245     // If there are children of this node, iterate to them, otherwise we're
246     // done.
247     if (const MatcherNodeWithChild *MNWC = dyn_cast<MatcherNodeWithChild>(N))
248       N = MNWC->getChild();
249     else
250       return Size;
251   }
252 }
253
254 void MatcherTableEmitter::EmitPredicateFunctions() {
255   // Emit pattern predicates.
256   OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
257   OS << "  switch (PredNo) {\n";
258   OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
259   for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
260     OS << "  case " << i << ": return "  << PatternPredicates[i] << ";\n";
261   OS << "  }\n";
262   OS << "}\n\n";
263
264   // Emit Node predicates.
265   OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
266   OS << "  switch (PredNo) {\n";
267   OS << "  default: assert(0 && \"Invalid predicate in table?\");\n";
268   for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
269     OS << "  case " << i << ": return "  << NodePredicates[i] << "(N);\n";
270   OS << "  }\n";
271   OS << "}\n\n";
272   
273   // Emit CompletePattern matchers.
274   
275   OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
276   OS << "      unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
277   OS << "  switch (PatternNo) {\n";
278   OS << "  default: assert(0 && \"Invalid pattern # in table?\");\n";
279   for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
280     const ComplexPattern &P = *ComplexPatterns[i];
281     unsigned NumOps = P.getNumOperands();
282     if (P.hasProperty(SDNPHasChain))
283       NumOps += 2; // Input and output chains.
284     OS << "  case " << i << ":\n";
285     OS << "    Result.resize(Result.size()+" << NumOps << ");\n";
286     OS << "    return "  << P.getSelectFunc() << "(Root, N";
287     for (unsigned i = 0; i != NumOps; ++i)
288       OS << ", Result[Result.size()-" << (NumOps-i) << ']';
289     OS << ");\n";
290   }
291   OS << "  }\n";
292   OS << "}\n\n";
293   
294 }
295
296
297 void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
298   formatted_raw_ostream OS(O);
299   
300   OS << "// The main instruction selector code.\n";
301   OS << "SDNode *SelectCode2(SDNode *N) {\n";
302
303   MatcherTableEmitter MatcherEmitter(OS);
304
305   OS << "  static const unsigned char MatcherTable[] = {\n";
306   unsigned TotalSize = MatcherEmitter.EmitMatcherAndChildren(Matcher, 2);
307   OS << "    0\n  }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
308   OS << "  return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
309   OS << "\n";
310   
311   // Next up, emit the function for node and pattern predicates:
312   MatcherEmitter.EmitPredicateFunctions();
313 }