blob: 158fe7ff3ab5a0dd055d9e601cf11ac2114982e5 [file] [log] [blame]
Chris Lattnerda272d12010-02-15 08:04:42 +00001//===- 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"
Chris Lattnere609a512010-02-17 00:31:50 +000016#include "llvm/ADT/DenseMap.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner050a03d2010-02-16 07:21:10 +000018#include "llvm/ADT/StringMap.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000019#include "llvm/Support/FormattedStream.h"
20using namespace llvm;
21
22namespace {
23enum {
Chris Lattner5be6e592010-02-17 00:39:26 +000024 CommentIndent = 30
Chris Lattnerda272d12010-02-15 08:04:42 +000025};
26}
27
Chris Lattnerda272d12010-02-15 08:04:42 +000028/// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
29/// fits in 1, 2, 4, or 8 sign extended bytes.
30static 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.
38static 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
Chris Lattnere02ea542010-02-16 06:52:01 +000067namespace {
68class MatcherTableEmitter {
69 formatted_raw_ostream &OS;
Chris Lattner050a03d2010-02-16 07:21:10 +000070
71 StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
72 std::vector<std::string> NodePredicates, PatternPredicates;
Chris Lattnere609a512010-02-17 00:31:50 +000073
74 DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
75 std::vector<const ComplexPattern*> ComplexPatterns;
Chris Lattnere02ea542010-02-16 06:52:01 +000076public:
77 MatcherTableEmitter(formatted_raw_ostream &os) : OS(os) {}
78
Chris Lattnerbd8227f2010-02-18 02:53:41 +000079 unsigned EmitMatcherList(const MatcherNode *N, unsigned Indent);
Chris Lattner050a03d2010-02-16 07:21:10 +000080
81 void EmitPredicateFunctions();
Chris Lattnere02ea542010-02-16 06:52:01 +000082private:
83 unsigned EmitMatcher(const MatcherNode *N, unsigned Indent);
Chris Lattner050a03d2010-02-16 07:21:10 +000084
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 }
Chris Lattnere609a512010-02-17 00:31:50 +0000101
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 }
Chris Lattnere02ea542010-02-16 06:52:01 +0000110};
111} // end anonymous namespace.
112
Chris Lattnerda272d12010-02-15 08:04:42 +0000113/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
114/// the number of bytes emitted.
Chris Lattnere02ea542010-02-16 06:52:01 +0000115unsigned MatcherTableEmitter::
116EmitMatcher(const MatcherNode *N, unsigned Indent) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000117 OS.PadToColumn(Indent*2);
118
119 switch (N->getKind()) {
120 case MatcherNode::Push: assert(0 && "Should be handled by caller");
121 case MatcherNode::EmitNode:
Chris Lattner5be6e592010-02-17 00:39:26 +0000122 OS << "// Src: "
123 << *cast<EmitNodeMatcherNode>(N)->getPattern().getSrcPattern() << '\n';
124 OS.PadToColumn(Indent*2) << "// Dst: "
125 << *cast<EmitNodeMatcherNode>(N)->getPattern().getDstPattern() << "\n";
126 OS.PadToColumn(Indent*2) << "OPC_Emit, /*XXX*/\n\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000127 return 1;
128 case MatcherNode::Record:
Chris Lattnerc96087b2010-02-17 01:03:09 +0000129 OS << "OPC_Record,";
130 OS.PadToColumn(CommentIndent) << "// "
131 << cast<RecordMatcherNode>(N)->getWhatFor() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000132 return 1;
133 case MatcherNode::MoveChild:
134 OS << "OPC_MoveChild, "
135 << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
136 return 2;
137
138 case MatcherNode::MoveParent:
139 OS << "OPC_MoveParent,\n";
140 return 1;
141
142 case MatcherNode::CheckSame:
143 OS << "OPC_CheckSame, "
144 << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
145 return 2;
146
Chris Lattner050a03d2010-02-16 07:21:10 +0000147 case MatcherNode::CheckPatternPredicate: {
148 StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate();
149 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
150 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000151 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000152 }
153 case MatcherNode::CheckPredicate: {
154 StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName();
155 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
156 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000157 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000158 }
159
Chris Lattnerda272d12010-02-15 08:04:42 +0000160 case MatcherNode::CheckOpcode:
161 OS << "OPC_CheckOpcode, "
162 << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
163 return 2;
164
165 case MatcherNode::CheckType:
166 OS << "OPC_CheckType, "
167 << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
168 return 2;
169
170 case MatcherNode::CheckInteger: {
171 int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
172 OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
173 return EmitInt(Val, OS)+1;
174 }
175 case MatcherNode::CheckCondCode:
176 OS << "OPC_CheckCondCode, ISD::"
177 << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
178 return 2;
179
180 case MatcherNode::CheckValueType:
181 OS << "OPC_CheckValueType, MVT::"
182 << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
183 return 2;
184
Chris Lattner5be6e592010-02-17 00:39:26 +0000185 case MatcherNode::CheckComplexPat: {
186 const ComplexPattern &Pattern =
187 cast<CheckComplexPatMatcherNode>(N)->getPattern();
188 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
Chris Lattner781f3592010-02-17 06:47:35 +0000189 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
190 OS << ": " << Pattern.getNumOperands() << " operands";
191 if (Pattern.hasProperty(SDNPHasChain))
192 OS << " + chain result and input";
193 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000194 return 2;
Chris Lattner5be6e592010-02-17 00:39:26 +0000195 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000196
197 case MatcherNode::CheckAndImm: {
198 int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
199 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
200 return EmitInt(Val, OS)+1;
201 }
202
203 case MatcherNode::CheckOrImm: {
204 int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
205 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
206 return EmitInt(Val, OS)+1;
207 }
Chris Lattner21390d72010-02-16 19:15:55 +0000208 case MatcherNode::CheckFoldableChainNode:
209 OS << "OPC_CheckFoldableChainNode,\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000210 return 1;
Chris Lattner9a747f12010-02-17 06:23:39 +0000211 case MatcherNode::CheckChainCompatible:
212 OS << "OPC_CheckChainCompatible, "
213 << cast<CheckChainCompatibleMatcherNode>(N)->getPreviousOp() << ",\n";
214 return 2;
Chris Lattnerda272d12010-02-15 08:04:42 +0000215 }
216 assert(0 && "Unreachable");
217 return 0;
218}
219
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000220/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
Chris Lattnere02ea542010-02-16 06:52:01 +0000221unsigned MatcherTableEmitter::
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000222EmitMatcherList(const MatcherNode *N, unsigned Indent) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000223 unsigned Size = 0;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000224 while (N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000225 // Push is a special case since it is binary.
226 if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
227 // We need to encode the child and the offset of the failure code before
228 // emitting either of them. Handle this by buffering the output into a
229 // string while we get the size.
230 SmallString<128> TmpBuf;
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000231 unsigned NextSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000232 {
233 raw_svector_ostream OS(TmpBuf);
234 formatted_raw_ostream FOS(OS);
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000235 NextSize = EmitMatcherList(cast<PushMatcherNode>(N)->getNext(),
236 Indent+1);
Chris Lattnerda272d12010-02-15 08:04:42 +0000237 }
238
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000239 if (NextSize > 255) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000240 errs() <<
241 "Tblgen internal error: can't handle predicate this complex yet\n";
242 exit(1);
243 }
244
245 OS.PadToColumn(Indent*2);
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000246 OS << "OPC_Push, " << NextSize << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000247 OS << TmpBuf.str();
248
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000249 Size += 2 + NextSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000250
251 N = PMN->getFailure();
252 continue;
253 }
254
Chris Lattnere02ea542010-02-16 06:52:01 +0000255 Size += EmitMatcher(N, Indent);
Chris Lattnerda272d12010-02-15 08:04:42 +0000256
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000257 // If there are other nodes in this list, iterate to them, otherwise we're
Chris Lattnerda272d12010-02-15 08:04:42 +0000258 // done.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000259 N = N->getNext();
Chris Lattnerda272d12010-02-15 08:04:42 +0000260 }
Chris Lattner8ef9c792010-02-18 02:49:24 +0000261 return Size;
Chris Lattnerda272d12010-02-15 08:04:42 +0000262}
263
Chris Lattner050a03d2010-02-16 07:21:10 +0000264void MatcherTableEmitter::EmitPredicateFunctions() {
Chris Lattnerb49985a2010-02-18 06:47:49 +0000265 // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
266 // here into the case stmts.
267
Chris Lattnere609a512010-02-17 00:31:50 +0000268 // Emit pattern predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000269 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
270 OS << " switch (PredNo) {\n";
271 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
272 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
273 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
274 OS << " }\n";
275 OS << "}\n\n";
276
Chris Lattnere609a512010-02-17 00:31:50 +0000277 // Emit Node predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000278 OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
279 OS << " switch (PredNo) {\n";
280 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
281 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
282 OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n";
283 OS << " }\n";
284 OS << "}\n\n";
Chris Lattnere609a512010-02-17 00:31:50 +0000285
286 // Emit CompletePattern matchers.
Chris Lattnere609a512010-02-17 00:31:50 +0000287 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
288 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
289 OS << " switch (PatternNo) {\n";
290 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
291 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
292 const ComplexPattern &P = *ComplexPatterns[i];
293 unsigned NumOps = P.getNumOperands();
294 if (P.hasProperty(SDNPHasChain))
295 NumOps += 2; // Input and output chains.
296 OS << " case " << i << ":\n";
297 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
298 OS << " return " << P.getSelectFunc() << "(Root, N";
299 for (unsigned i = 0; i != NumOps; ++i)
300 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
301 OS << ");\n";
302 }
303 OS << " }\n";
304 OS << "}\n\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000305}
306
307
Chris Lattnerda272d12010-02-15 08:04:42 +0000308void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
309 formatted_raw_ostream OS(O);
310
311 OS << "// The main instruction selector code.\n";
312 OS << "SDNode *SelectCode2(SDNode *N) {\n";
313
Chris Lattnere02ea542010-02-16 06:52:01 +0000314 MatcherTableEmitter MatcherEmitter(OS);
315
Chris Lattnerda272d12010-02-15 08:04:42 +0000316 OS << " static const unsigned char MatcherTable[] = {\n";
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000317 unsigned TotalSize = MatcherEmitter.EmitMatcherList(Matcher, 2);
Chris Lattnerda272d12010-02-15 08:04:42 +0000318 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000319 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000320 OS << "\n";
321
322 // Next up, emit the function for node and pattern predicates:
323 MatcherEmitter.EmitPredicateFunctions();
Chris Lattnerda272d12010-02-15 08:04:42 +0000324}