blob: af75d7d71ec2708191478ea1158dca5ed702a2e1 [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
79 unsigned EmitMatcherAndChildren(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:
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
Chris Lattner050a03d2010-02-16 07:21:10 +0000145 case MatcherNode::CheckPatternPredicate: {
146 StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate();
147 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
148 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000149 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000150 }
151 case MatcherNode::CheckPredicate: {
152 StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName();
153 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
154 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000155 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000156 }
157
Chris Lattnerda272d12010-02-15 08:04:42 +0000158 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
Chris Lattner5be6e592010-02-17 00:39:26 +0000183 case MatcherNode::CheckComplexPat: {
184 const ComplexPattern &Pattern =
185 cast<CheckComplexPatMatcherNode>(N)->getPattern();
186 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
187 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000188 return 2;
Chris Lattner5be6e592010-02-17 00:39:26 +0000189 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000190
191 case MatcherNode::CheckAndImm: {
192 int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
193 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
194 return EmitInt(Val, OS)+1;
195 }
196
197 case MatcherNode::CheckOrImm: {
198 int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
199 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
200 return EmitInt(Val, OS)+1;
201 }
Chris Lattner21390d72010-02-16 19:15:55 +0000202 case MatcherNode::CheckFoldableChainNode:
203 OS << "OPC_CheckFoldableChainNode,\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000204 return 1;
Chris Lattnerda272d12010-02-15 08:04:42 +0000205 }
206 assert(0 && "Unreachable");
207 return 0;
208}
209
210/// EmitMatcherAndChildren - Emit the bytes for the specified matcher subtree.
Chris Lattnere02ea542010-02-16 06:52:01 +0000211unsigned MatcherTableEmitter::
212EmitMatcherAndChildren(const MatcherNode *N, unsigned Indent) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000213 unsigned Size = 0;
214 while (1) {
215 // Push is a special case since it is binary.
216 if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
217 // We need to encode the child and the offset of the failure code before
218 // emitting either of them. Handle this by buffering the output into a
219 // string while we get the size.
220 SmallString<128> TmpBuf;
221 unsigned ChildSize;
222 {
223 raw_svector_ostream OS(TmpBuf);
224 formatted_raw_ostream FOS(OS);
225 ChildSize =
Chris Lattnere02ea542010-02-16 06:52:01 +0000226 EmitMatcherAndChildren(cast<PushMatcherNode>(N)->getChild(),Indent+1);
Chris Lattnerda272d12010-02-15 08:04:42 +0000227 }
228
229 if (ChildSize > 255) {
230 errs() <<
231 "Tblgen internal error: can't handle predicate this complex yet\n";
232 exit(1);
233 }
234
235 OS.PadToColumn(Indent*2);
236 OS << "OPC_Push, " << ChildSize << ",\n";
237 OS << TmpBuf.str();
238
239 Size += 2 + ChildSize;
240
241 N = PMN->getFailure();
242 continue;
243 }
244
Chris Lattnere02ea542010-02-16 06:52:01 +0000245 Size += EmitMatcher(N, Indent);
Chris Lattnerda272d12010-02-15 08:04:42 +0000246
247 // If there are children of this node, iterate to them, otherwise we're
248 // done.
249 if (const MatcherNodeWithChild *MNWC = dyn_cast<MatcherNodeWithChild>(N))
250 N = MNWC->getChild();
251 else
252 return Size;
253 }
254}
255
Chris Lattner050a03d2010-02-16 07:21:10 +0000256void MatcherTableEmitter::EmitPredicateFunctions() {
Chris Lattnere609a512010-02-17 00:31:50 +0000257 // Emit pattern predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000258 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
259 OS << " switch (PredNo) {\n";
260 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
261 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
262 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
263 OS << " }\n";
264 OS << "}\n\n";
265
Chris Lattnere609a512010-02-17 00:31:50 +0000266 // Emit Node predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000267 OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
268 OS << " switch (PredNo) {\n";
269 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
270 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
271 OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n";
272 OS << " }\n";
273 OS << "}\n\n";
Chris Lattnere609a512010-02-17 00:31:50 +0000274
275 // Emit CompletePattern matchers.
Chris Lattnere609a512010-02-17 00:31:50 +0000276 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
277 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
278 OS << " switch (PatternNo) {\n";
279 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
280 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
281 const ComplexPattern &P = *ComplexPatterns[i];
282 unsigned NumOps = P.getNumOperands();
283 if (P.hasProperty(SDNPHasChain))
284 NumOps += 2; // Input and output chains.
285 OS << " case " << i << ":\n";
286 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
287 OS << " return " << P.getSelectFunc() << "(Root, N";
288 for (unsigned i = 0; i != NumOps; ++i)
289 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
290 OS << ");\n";
291 }
292 OS << " }\n";
293 OS << "}\n\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000294}
295
296
Chris Lattnerda272d12010-02-15 08:04:42 +0000297void 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
Chris Lattnere02ea542010-02-16 06:52:01 +0000303 MatcherTableEmitter MatcherEmitter(OS);
304
Chris Lattnerda272d12010-02-15 08:04:42 +0000305 OS << " static const unsigned char MatcherTable[] = {\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000306 unsigned TotalSize = MatcherEmitter.EmitMatcherAndChildren(Matcher, 2);
Chris Lattnerda272d12010-02-15 08:04:42 +0000307 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000308 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000309 OS << "\n";
310
311 // Next up, emit the function for node and pattern predicates:
312 MatcherEmitter.EmitPredicateFunctions();
Chris Lattnerda272d12010-02-15 08:04:42 +0000313}