blob: aec1e18aa722fdf0c7bde4d433a2a5480b482a22 [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 Lattner8e946be2010-02-21 03:22:59 +000016#include "Record.h"
Chris Lattnere609a512010-02-17 00:31:50 +000017#include "llvm/ADT/DenseMap.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000018#include "llvm/ADT/SmallString.h"
Chris Lattner050a03d2010-02-16 07:21:10 +000019#include "llvm/ADT/StringMap.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000020#include "llvm/Support/FormattedStream.h"
21using namespace llvm;
22
Chris Lattnerda272d12010-02-15 08:04:42 +000023enum {
Chris Lattner5be6e592010-02-17 00:39:26 +000024 CommentIndent = 30
Chris Lattnerda272d12010-02-15 08:04:42 +000025};
Chris Lattnerda272d12010-02-15 08:04:42 +000026
Chris Lattnerda272d12010-02-15 08:04:42 +000027/// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
28/// fits in 1, 2, 4, or 8 sign extended bytes.
29static char ClassifyInt(int64_t Val) {
30 if (Val == int8_t(Val)) return '1';
31 if (Val == int16_t(Val)) return '2';
32 if (Val == int32_t(Val)) return '4';
33 return '8';
34}
35
36/// EmitInt - Emit the specified integer, returning the number of bytes emitted.
37static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) {
38 unsigned BytesEmitted = 1;
39 OS << (int)(unsigned char)Val << ", ";
40 if (Val == int8_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000041 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000042 return BytesEmitted;
43 }
44
45 OS << (int)(unsigned char)(Val >> 8) << ", ";
46 ++BytesEmitted;
47
48 if (Val != int16_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000049 OS << (int)(unsigned char)(Val >> 16) << ", "
50 << (int)(unsigned char)(Val >> 24) << ", ";
Chris Lattnerda272d12010-02-15 08:04:42 +000051 BytesEmitted += 2;
52
53 if (Val != int32_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000054 OS << (int)(unsigned char)(Val >> 32) << ", "
55 << (int)(unsigned char)(Val >> 40) << ", "
56 << (int)(unsigned char)(Val >> 48) << ", "
57 << (int)(unsigned char)(Val >> 56) << ", ";
Chris Lattnerda272d12010-02-15 08:04:42 +000058 BytesEmitted += 4;
59 }
60 }
61
Chris Lattner8e946be2010-02-21 03:22:59 +000062 OS.PadToColumn(CommentIndent) << "// " << Val << " aka 0x";
63 OS.write_hex(Val) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000064 return BytesEmitted;
65}
66
Chris Lattnere02ea542010-02-16 06:52:01 +000067namespace {
68class MatcherTableEmitter {
Chris Lattner050a03d2010-02-16 07:21:10 +000069 StringMap<unsigned> NodePredicateMap, PatternPredicateMap;
70 std::vector<std::string> NodePredicates, PatternPredicates;
Chris Lattnere609a512010-02-17 00:31:50 +000071
72 DenseMap<const ComplexPattern*, unsigned> ComplexPatternMap;
73 std::vector<const ComplexPattern*> ComplexPatterns;
Chris Lattner8e946be2010-02-21 03:22:59 +000074
75
76 DenseMap<Record*, unsigned> NodeXFormMap;
77 std::vector<const Record*> NodeXForms;
78
Chris Lattner09b9f392010-02-24 19:17:12 +000079 // Per opcode frequence count.
80 std::vector<unsigned> Histogram;
Chris Lattnere02ea542010-02-16 06:52:01 +000081public:
Chris Lattner984e1882010-02-21 06:30:04 +000082 MatcherTableEmitter() {}
Chris Lattnere02ea542010-02-16 06:52:01 +000083
Chris Lattnerb21ba712010-02-25 02:04:40 +000084 unsigned EmitMatcherList(const Matcher *N, unsigned Indent,
Chris Lattner8fbad242010-02-21 07:16:41 +000085 unsigned StartIdx, formatted_raw_ostream &OS);
Chris Lattner050a03d2010-02-16 07:21:10 +000086
Chris Lattner984e1882010-02-21 06:30:04 +000087 void EmitPredicateFunctions(formatted_raw_ostream &OS);
Chris Lattner09b9f392010-02-24 19:17:12 +000088
89 void EmitHistogram(formatted_raw_ostream &OS);
Chris Lattnere02ea542010-02-16 06:52:01 +000090private:
Chris Lattnerd6c84722010-02-25 19:00:39 +000091 unsigned EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
Chris Lattner984e1882010-02-21 06:30:04 +000092 formatted_raw_ostream &OS);
Chris Lattner050a03d2010-02-16 07:21:10 +000093
94 unsigned getNodePredicate(StringRef PredName) {
95 unsigned &Entry = NodePredicateMap[PredName];
96 if (Entry == 0) {
97 NodePredicates.push_back(PredName.str());
98 Entry = NodePredicates.size();
99 }
100 return Entry-1;
101 }
102 unsigned getPatternPredicate(StringRef PredName) {
103 unsigned &Entry = PatternPredicateMap[PredName];
104 if (Entry == 0) {
105 PatternPredicates.push_back(PredName.str());
106 Entry = PatternPredicates.size();
107 }
108 return Entry-1;
109 }
Chris Lattnere609a512010-02-17 00:31:50 +0000110
111 unsigned getComplexPat(const ComplexPattern &P) {
112 unsigned &Entry = ComplexPatternMap[&P];
113 if (Entry == 0) {
114 ComplexPatterns.push_back(&P);
115 Entry = ComplexPatterns.size();
116 }
117 return Entry-1;
118 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000119
120 unsigned getNodeXFormID(Record *Rec) {
121 unsigned &Entry = NodeXFormMap[Rec];
122 if (Entry == 0) {
123 NodeXForms.push_back(Rec);
124 Entry = NodeXForms.size();
125 }
126 return Entry-1;
127 }
128
Chris Lattnere02ea542010-02-16 06:52:01 +0000129};
130} // end anonymous namespace.
131
Chris Lattnerd6c84722010-02-25 19:00:39 +0000132static unsigned GetVBRSize(unsigned Val) {
133 if (Val <= 127) return 1;
134
135 unsigned NumBytes = 0;
136 while (Val >= 128) {
137 Val >>= 7;
138 ++NumBytes;
139 }
140 return NumBytes+1;
141}
142
Chris Lattner5007e1e2010-02-23 00:59:59 +0000143/// EmitVBRValue - Emit the specified value as a VBR, returning the number of
144/// bytes emitted.
145static unsigned EmitVBRValue(unsigned Val, raw_ostream &OS) {
146 if (Val <= 127) {
147 OS << Val << ", ";
148 return 1;
149 }
150
151 unsigned InVal = Val;
152 unsigned NumBytes = 0;
Chris Lattner860d4a72010-02-23 01:07:39 +0000153 while (Val >= 128) {
Chris Lattner5007e1e2010-02-23 00:59:59 +0000154 OS << (Val&127) << "|128,";
155 Val >>= 7;
156 ++NumBytes;
157 }
158 OS << Val << "/*" << InVal << "*/, ";
159 return NumBytes+1;
160}
161
Chris Lattnerda272d12010-02-15 08:04:42 +0000162/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
163/// the number of bytes emitted.
Chris Lattnere02ea542010-02-16 06:52:01 +0000164unsigned MatcherTableEmitter::
Chris Lattnerd6c84722010-02-25 19:00:39 +0000165EmitMatcher(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
166 formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000167 OS.PadToColumn(Indent*2);
168
169 switch (N->getKind()) {
Chris Lattnerd6c84722010-02-25 19:00:39 +0000170 case Matcher::Scope: {
171 const ScopeMatcher *SM = cast<ScopeMatcher>(N);
172 assert(SM->getNext() == 0 && "Shouldn't have next after scope");
173
174 unsigned StartIdx = CurrentIdx;
175
176 // Emit all of the children.
177 for (unsigned i = 0, e = SM->getNumChildren(); i != e; ++i) {
178 if (i == 0) {
179 OS << "OPC_Scope, ";
180 ++CurrentIdx;
181 } else {
182 OS << "/*" << CurrentIdx << "*/";
183 OS.PadToColumn(Indent*2) << "/*Scope*/ ";
184 }
185
186 // We need to encode the child and the offset of the failure code before
187 // emitting either of them. Handle this by buffering the output into a
188 // string while we get the size. Unfortunately, the offset of the
189 // children depends on the VBR size of the child, so for large children we
190 // have to iterate a bit.
191 SmallString<128> TmpBuf;
192 unsigned ChildSize = 0;
193 unsigned VBRSize = 0;
194 do {
195 VBRSize = GetVBRSize(ChildSize);
196
197 TmpBuf.clear();
198 raw_svector_ostream OS(TmpBuf);
199 formatted_raw_ostream FOS(OS);
200 ChildSize = EmitMatcherList(cast<ScopeMatcher>(N)->getChild(i),
201 Indent+1, CurrentIdx+VBRSize, FOS);
202 } while (GetVBRSize(ChildSize) != VBRSize);
203
204 assert(ChildSize != 0 && "Should not have a zero-sized child!");
205
206 CurrentIdx += EmitVBRValue(ChildSize, OS);
Chris Lattner5a56e5c2010-02-26 08:15:02 +0000207 OS << "/*->" << CurrentIdx+ChildSize << "*/";
208
209 if (i == 0)
210 OS.PadToColumn(CommentIndent) << "// " << SM->getNumChildren()
211 << " children in Scope";
212
Chris Lattnerd6c84722010-02-25 19:00:39 +0000213 OS << '\n' << TmpBuf.str();
214 CurrentIdx += ChildSize;
215 }
216
217 // Emit a zero as a sentinel indicating end of 'Scope'.
218 OS << "/*" << CurrentIdx << "*/";
219 OS.PadToColumn(Indent*2) << "0, /*End of Scope*/\n";
220 return CurrentIdx - StartIdx + 1;
221 }
222
Chris Lattnerb21ba712010-02-25 02:04:40 +0000223 case Matcher::RecordNode:
Chris Lattner845c0422010-02-18 22:03:03 +0000224 OS << "OPC_RecordNode,";
Chris Lattnerc96087b2010-02-17 01:03:09 +0000225 OS.PadToColumn(CommentIndent) << "// "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000226 << cast<RecordMatcher>(N)->getWhatFor() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000227 return 1;
Chris Lattner19b5a752010-02-24 07:31:45 +0000228
Chris Lattnerb21ba712010-02-25 02:04:40 +0000229 case Matcher::RecordChild:
230 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
Chris Lattner19b5a752010-02-24 07:31:45 +0000231 << ',';
232 OS.PadToColumn(CommentIndent) << "// "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000233 << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +0000234 return 1;
Chris Lattner8e946be2010-02-21 03:22:59 +0000235
Chris Lattnerb21ba712010-02-25 02:04:40 +0000236 case Matcher::RecordMemRef:
Chris Lattner8e946be2010-02-21 03:22:59 +0000237 OS << "OPC_RecordMemRef,\n";
238 return 1;
239
Chris Lattnerb21ba712010-02-25 02:04:40 +0000240 case Matcher::CaptureFlagInput:
Chris Lattner8e946be2010-02-21 03:22:59 +0000241 OS << "OPC_CaptureFlagInput,\n";
242 return 1;
243
Chris Lattnerb21ba712010-02-25 02:04:40 +0000244 case Matcher::MoveChild:
245 OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000246 return 2;
247
Chris Lattnerb21ba712010-02-25 02:04:40 +0000248 case Matcher::MoveParent:
Chris Lattnerda272d12010-02-15 08:04:42 +0000249 OS << "OPC_MoveParent,\n";
250 return 1;
251
Chris Lattnerb21ba712010-02-25 02:04:40 +0000252 case Matcher::CheckSame:
Chris Lattnerda272d12010-02-15 08:04:42 +0000253 OS << "OPC_CheckSame, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000254 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000255 return 2;
256
Chris Lattnerb21ba712010-02-25 02:04:40 +0000257 case Matcher::CheckPatternPredicate: {
258 StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
Chris Lattner050a03d2010-02-16 07:21:10 +0000259 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
260 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000261 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000262 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000263 case Matcher::CheckPredicate: {
264 StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
Chris Lattner050a03d2010-02-16 07:21:10 +0000265 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
266 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000267 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000268 }
269
Chris Lattnerb21ba712010-02-25 02:04:40 +0000270 case Matcher::CheckOpcode:
Chris Lattnerda272d12010-02-15 08:04:42 +0000271 OS << "OPC_CheckOpcode, "
Chris Lattnera230f962010-02-27 21:48:43 +0000272 << cast<CheckOpcodeMatcher>(N)->getOpcode().getEnumName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000273 return 2;
274
Chris Lattnerb21ba712010-02-25 02:04:40 +0000275 case Matcher::CheckMultiOpcode: {
276 const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
Chris Lattnera230f962010-02-27 21:48:43 +0000277 OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodes() << ", ";
278 for (unsigned i = 0, e = CMO->getNumOpcodes(); i != e; ++i)
279 OS << CMO->getOpcode(i).getEnumName() << ", ";
Chris Lattner12a667c2010-02-22 22:30:37 +0000280 OS << '\n';
Chris Lattnera230f962010-02-27 21:48:43 +0000281 return 2 + CMO->getNumOpcodes();
Chris Lattner12a667c2010-02-22 22:30:37 +0000282 }
283
Chris Lattnerb21ba712010-02-25 02:04:40 +0000284 case Matcher::CheckType:
Chris Lattnerda272d12010-02-15 08:04:42 +0000285 OS << "OPC_CheckType, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000286 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000287 return 2;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000288 case Matcher::CheckChildType:
Chris Lattner23cfda72010-02-24 20:15:25 +0000289 OS << "OPC_CheckChild"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000290 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
291 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
Chris Lattner23cfda72010-02-24 20:15:25 +0000292 return 2;
293
Chris Lattnerb21ba712010-02-25 02:04:40 +0000294 case Matcher::CheckInteger: {
295 int64_t Val = cast<CheckIntegerMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000296 OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
297 return EmitInt(Val, OS)+1;
298 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000299 case Matcher::CheckCondCode:
Chris Lattnerda272d12010-02-15 08:04:42 +0000300 OS << "OPC_CheckCondCode, ISD::"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000301 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000302 return 2;
303
Chris Lattnerb21ba712010-02-25 02:04:40 +0000304 case Matcher::CheckValueType:
Chris Lattnerda272d12010-02-15 08:04:42 +0000305 OS << "OPC_CheckValueType, MVT::"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000306 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000307 return 2;
308
Chris Lattnerb21ba712010-02-25 02:04:40 +0000309 case Matcher::CheckComplexPat: {
Chris Lattner5be6e592010-02-17 00:39:26 +0000310 const ComplexPattern &Pattern =
Chris Lattnerb21ba712010-02-25 02:04:40 +0000311 cast<CheckComplexPatMatcher>(N)->getPattern();
Chris Lattner5be6e592010-02-17 00:39:26 +0000312 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
Chris Lattner781f3592010-02-17 06:47:35 +0000313 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
314 OS << ": " << Pattern.getNumOperands() << " operands";
315 if (Pattern.hasProperty(SDNPHasChain))
316 OS << " + chain result and input";
317 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000318 return 2;
Chris Lattner5be6e592010-02-17 00:39:26 +0000319 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000320
Chris Lattnerb21ba712010-02-25 02:04:40 +0000321 case Matcher::CheckAndImm: {
322 int64_t Val = cast<CheckAndImmMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000323 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
324 return EmitInt(Val, OS)+1;
325 }
326
Chris Lattnerb21ba712010-02-25 02:04:40 +0000327 case Matcher::CheckOrImm: {
328 int64_t Val = cast<CheckOrImmMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000329 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
330 return EmitInt(Val, OS)+1;
331 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000332 case Matcher::CheckFoldableChainNode:
Chris Lattner21390d72010-02-16 19:15:55 +0000333 OS << "OPC_CheckFoldableChainNode,\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000334 return 1;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000335 case Matcher::CheckChainCompatible:
Chris Lattner9a747f12010-02-17 06:23:39 +0000336 OS << "OPC_CheckChainCompatible, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000337 << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000338 return 2;
Chris Lattner845c0422010-02-18 22:03:03 +0000339
Chris Lattnerb21ba712010-02-25 02:04:40 +0000340 case Matcher::EmitInteger: {
341 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
Chris Lattner906b4992010-02-19 07:49:56 +0000342 OS << "OPC_EmitInteger" << ClassifyInt(Val) << ", "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000343 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
Chris Lattner906b4992010-02-19 07:49:56 +0000344 return EmitInt(Val, OS)+2;
345 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000346 case Matcher::EmitStringInteger: {
347 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
Chris Lattner8e946be2010-02-21 03:22:59 +0000348 // These should always fit into one byte.
349 OS << "OPC_EmitInteger1, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000350 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
Chris Lattner8e946be2010-02-21 03:22:59 +0000351 << Val << ",\n";
352 return 3;
353 }
Chris Lattner906b4992010-02-19 07:49:56 +0000354
Chris Lattnerb21ba712010-02-25 02:04:40 +0000355 case Matcher::EmitRegister:
Chris Lattner906b4992010-02-19 07:49:56 +0000356 OS << "OPC_EmitRegister, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000357 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
358 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
Chris Lattner906b4992010-02-19 07:49:56 +0000359 OS << getQualifiedName(R) << ",\n";
360 else
361 OS << "0 /*zero_reg*/,\n";
362 return 3;
Chris Lattner8e946be2010-02-21 03:22:59 +0000363
Chris Lattnerb21ba712010-02-25 02:04:40 +0000364 case Matcher::EmitConvertToTarget:
Chris Lattner8e946be2010-02-21 03:22:59 +0000365 OS << "OPC_EmitConvertToTarget, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000366 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000367 return 2;
368
Chris Lattnerb21ba712010-02-25 02:04:40 +0000369 case Matcher::EmitMergeInputChains: {
370 const EmitMergeInputChainsMatcher *MN =
371 cast<EmitMergeInputChainsMatcher>(N);
Chris Lattner8e946be2010-02-21 03:22:59 +0000372 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
373 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
374 OS << MN->getNode(i) << ", ";
375 OS << '\n';
376 return 2+MN->getNumNodes();
377 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000378 case Matcher::EmitCopyToReg:
Chris Lattner8e946be2010-02-21 03:22:59 +0000379 OS << "OPC_EmitCopyToReg, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000380 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
381 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
Chris Lattner8e946be2010-02-21 03:22:59 +0000382 << ",\n";
383 return 3;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000384 case Matcher::EmitNodeXForm: {
385 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
Chris Lattner8e946be2010-02-21 03:22:59 +0000386 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
387 << XF->getSlot() << ',';
388 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
389 return 3;
390 }
391
Chris Lattnere86097a2010-02-28 02:31:26 +0000392 case Matcher::EmitNode:
393 case Matcher::SelectNodeTo: {
394 const EmitNodeMatcherCommon *EN = cast<EmitNodeMatcherCommon>(N);
Chris Lattner6281cda2010-02-28 02:41:25 +0000395 OS << (isa<EmitNodeMatcher>(EN) ? "OPC_EmitNode" : "OPC_SelectNodeTo");
Chris Lattnere86097a2010-02-28 02:31:26 +0000396 OS << ", TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
Chris Lattner8e946be2010-02-21 03:22:59 +0000397
398 if (EN->hasChain()) OS << "|OPFL_Chain";
399 if (EN->hasFlag()) OS << "|OPFL_Flag";
400 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
401 if (EN->getNumFixedArityOperands() != -1)
402 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
403 OS << ",\n";
404
405 OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
406 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
407 OS << getEnumName(EN->getVT(i)) << ", ";
408
409 OS << EN->getNumOperands() << "/*#Ops*/, ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000410 unsigned NumOperandBytes = 0;
411 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
412 // We emit the operand numbers in VBR encoded format, in case the number
413 // is too large to represent with a byte.
414 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
415 }
Chris Lattner6281cda2010-02-28 02:41:25 +0000416
417 // Print the result #'s for EmitNode.
418 if (const EmitNodeMatcher *E = dyn_cast<EmitNodeMatcher>(EN)) {
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000419 if (unsigned NumResults = EN->getNumNonChainFlagVTs()) {
Chris Lattner6281cda2010-02-28 02:41:25 +0000420 OS.PadToColumn(CommentIndent) << "// Results = ";
421 unsigned First = E->getFirstResultSlot();
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000422 for (unsigned i = 0; i != NumResults; ++i)
Chris Lattner6281cda2010-02-28 02:41:25 +0000423 OS << "#" << First+i << " ";
Chris Lattner6281cda2010-02-28 02:41:25 +0000424 }
425 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000426 OS << '\n';
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000427
428 if (const SelectNodeToMatcher *SNT = dyn_cast<SelectNodeToMatcher>(N)) {
429 OS.PadToColumn(Indent*2) << "// Src: "
430 << *SNT->getPattern().getSrcPattern() << '\n';
431 OS.PadToColumn(Indent*2) << "// Dst: "
432 << *SNT->getPattern().getDstPattern() << '\n';
433
434 }
435
Chris Lattner5007e1e2010-02-23 00:59:59 +0000436 return 6+EN->getNumVTs()+NumOperandBytes;
Chris Lattner8e946be2010-02-21 03:22:59 +0000437 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000438 case Matcher::MarkFlagResults: {
439 const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
Chris Lattner02f73582010-02-24 05:33:42 +0000440 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
441 unsigned NumOperandBytes = 0;
442 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
443 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
444 OS << '\n';
445 return 2+NumOperandBytes;
446 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000447 case Matcher::CompleteMatch: {
448 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
Chris Lattner77f2e272010-02-21 06:03:07 +0000449 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000450 unsigned NumResultBytes = 0;
Chris Lattner77f2e272010-02-21 06:03:07 +0000451 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
Chris Lattner5007e1e2010-02-23 00:59:59 +0000452 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
Chris Lattner77f2e272010-02-21 06:03:07 +0000453 OS << '\n';
454 OS.PadToColumn(Indent*2) << "// Src: "
455 << *CM->getPattern().getSrcPattern() << '\n';
456 OS.PadToColumn(Indent*2) << "// Dst: "
457 << *CM->getPattern().getDstPattern() << '\n';
Chris Lattner5007e1e2010-02-23 00:59:59 +0000458 return 2 + NumResultBytes;
Chris Lattnerda272d12010-02-15 08:04:42 +0000459 }
Chris Lattner77f2e272010-02-21 06:03:07 +0000460 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000461 assert(0 && "Unreachable");
462 return 0;
463}
464
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000465/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
Chris Lattnere02ea542010-02-16 06:52:01 +0000466unsigned MatcherTableEmitter::
Chris Lattnerb21ba712010-02-25 02:04:40 +0000467EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
Chris Lattner984e1882010-02-21 06:30:04 +0000468 formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000469 unsigned Size = 0;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000470 while (N) {
Chris Lattner09b9f392010-02-24 19:17:12 +0000471 if (unsigned(N->getKind()) >= Histogram.size())
472 Histogram.resize(N->getKind()+1);
473 Histogram[N->getKind()]++;
474
Chris Lattner8fbad242010-02-21 07:16:41 +0000475 OS << "/*" << CurrentIdx << "*/";
Chris Lattnerd6c84722010-02-25 19:00:39 +0000476 unsigned MatcherSize = EmitMatcher(N, Indent, CurrentIdx, OS);
Chris Lattner8fbad242010-02-21 07:16:41 +0000477 Size += MatcherSize;
478 CurrentIdx += MatcherSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000479
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000480 // If there are other nodes in this list, iterate to them, otherwise we're
Chris Lattnerda272d12010-02-15 08:04:42 +0000481 // done.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000482 N = N->getNext();
Chris Lattnerda272d12010-02-15 08:04:42 +0000483 }
Chris Lattner8ef9c792010-02-18 02:49:24 +0000484 return Size;
Chris Lattnerda272d12010-02-15 08:04:42 +0000485}
486
Chris Lattner984e1882010-02-21 06:30:04 +0000487void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
Chris Lattnerb49985a2010-02-18 06:47:49 +0000488 // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
489 // here into the case stmts.
490
Chris Lattnere609a512010-02-17 00:31:50 +0000491 // Emit pattern predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000492 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
493 OS << " switch (PredNo) {\n";
494 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
495 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
496 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
497 OS << " }\n";
498 OS << "}\n\n";
499
Chris Lattnere609a512010-02-17 00:31:50 +0000500 // Emit Node predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000501 OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
502 OS << " switch (PredNo) {\n";
503 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
504 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
505 OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n";
506 OS << " }\n";
507 OS << "}\n\n";
Chris Lattnere609a512010-02-17 00:31:50 +0000508
509 // Emit CompletePattern matchers.
Chris Lattner8e946be2010-02-21 03:22:59 +0000510 // FIXME: This should be const.
Chris Lattnere609a512010-02-17 00:31:50 +0000511 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
512 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
513 OS << " switch (PatternNo) {\n";
514 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
515 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
516 const ComplexPattern &P = *ComplexPatterns[i];
517 unsigned NumOps = P.getNumOperands();
Chris Lattner8e946be2010-02-21 03:22:59 +0000518
Chris Lattnere609a512010-02-17 00:31:50 +0000519 if (P.hasProperty(SDNPHasChain))
Chris Lattner8e946be2010-02-21 03:22:59 +0000520 ++NumOps; // Get the chained node too.
521
Chris Lattnere609a512010-02-17 00:31:50 +0000522 OS << " case " << i << ":\n";
523 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000524 OS << " return " << P.getSelectFunc();
525
526 // FIXME: Temporary hack until old isel dies.
527 if (P.hasProperty(SDNPHasChain))
528 OS << "XXX";
529
530 OS << "(Root, N";
Chris Lattnere609a512010-02-17 00:31:50 +0000531 for (unsigned i = 0; i != NumOps; ++i)
532 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
533 OS << ");\n";
534 }
535 OS << " }\n";
536 OS << "}\n\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000537
538 // Emit SDNodeXForm handlers.
539 // FIXME: This should be const.
540 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
541 OS << " switch (XFormNo) {\n";
542 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
543
544 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
545 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i)
546 OS << " case " << i << ": return Transform_" << NodeXForms[i]->getName()
547 << "(V.getNode());\n";
548 OS << " }\n";
549 OS << "}\n\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000550}
551
Chris Lattner09b9f392010-02-24 19:17:12 +0000552void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
553 OS << " // Opcode Histogram:\n";
554 for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
555 OS << " // #";
Chris Lattnerb21ba712010-02-25 02:04:40 +0000556 switch ((Matcher::KindTy)i) {
557 case Matcher::Scope: OS << "OPC_Scope"; break;
558 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
559 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
560 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
561 case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
562 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
563 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
564 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
565 case Matcher::CheckPatternPredicate:
Chris Lattner09b9f392010-02-24 19:17:12 +0000566 OS << "OPC_CheckPatternPredicate"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000567 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
568 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
569 case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
570 case Matcher::CheckType: OS << "OPC_CheckType"; break;
571 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
572 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
573 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
574 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
575 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
576 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
577 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
578 case Matcher::CheckFoldableChainNode:
Chris Lattner09b9f392010-02-24 19:17:12 +0000579 OS << "OPC_CheckFoldableChainNode"; break;
Chris Lattner499622b2010-02-25 02:09:00 +0000580 case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000581 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
582 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
583 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
Chris Lattner499622b2010-02-25 02:09:00 +0000584 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
585 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000586 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
587 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
Chris Lattnere86097a2010-02-28 02:31:26 +0000588 case Matcher::SelectNodeTo: OS << "OPC_SelectNodeTo"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000589 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
590 case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
591 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
Chris Lattner09b9f392010-02-24 19:17:12 +0000592 }
593
594 OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
595 }
596 OS << '\n';
597}
598
Chris Lattner050a03d2010-02-16 07:21:10 +0000599
Chris Lattnerb21ba712010-02-25 02:04:40 +0000600void llvm::EmitMatcherTable(const Matcher *TheMatcher, raw_ostream &O) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000601 formatted_raw_ostream OS(O);
602
603 OS << "// The main instruction selector code.\n";
Chris Lattnerc84edb72010-02-24 07:35:09 +0000604 OS << "SDNode *SelectCode(SDNode *N) {\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000605
Chris Lattner984e1882010-02-21 06:30:04 +0000606 MatcherTableEmitter MatcherEmitter;
Chris Lattnere02ea542010-02-16 06:52:01 +0000607
Chris Lattner8e946be2010-02-21 03:22:59 +0000608 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
609 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000610 OS << " static const unsigned char MatcherTable[] = {\n";
Chris Lattnerb21ba712010-02-25 02:04:40 +0000611 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000612 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
Chris Lattner09b9f392010-02-24 19:17:12 +0000613
614 MatcherEmitter.EmitHistogram(OS);
615
Chris Lattner8e946be2010-02-21 03:22:59 +0000616 OS << " #undef TARGET_OPCODE\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000617 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000618 OS << "\n";
619
620 // Next up, emit the function for node and pattern predicates:
Chris Lattner984e1882010-02-21 06:30:04 +0000621 MatcherEmitter.EmitPredicateFunctions(OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000622}