blob: e78be79119d64bd3e5ef6c1349b0e77028088279 [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
23namespace {
24enum {
Chris Lattner5be6e592010-02-17 00:39:26 +000025 CommentIndent = 30
Chris Lattnerda272d12010-02-15 08:04:42 +000026};
27}
28
Chris Lattnerda272d12010-02-15 08:04:42 +000029/// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
30/// fits in 1, 2, 4, or 8 sign extended bytes.
31static char ClassifyInt(int64_t Val) {
32 if (Val == int8_t(Val)) return '1';
33 if (Val == int16_t(Val)) return '2';
34 if (Val == int32_t(Val)) return '4';
35 return '8';
36}
37
38/// EmitInt - Emit the specified integer, returning the number of bytes emitted.
39static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) {
40 unsigned BytesEmitted = 1;
41 OS << (int)(unsigned char)Val << ", ";
42 if (Val == int8_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000043 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000044 return BytesEmitted;
45 }
46
47 OS << (int)(unsigned char)(Val >> 8) << ", ";
48 ++BytesEmitted;
49
50 if (Val != int16_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000051 OS << (int)(unsigned char)(Val >> 16) << ", "
52 << (int)(unsigned char)(Val >> 24) << ", ";
Chris Lattnerda272d12010-02-15 08:04:42 +000053 BytesEmitted += 2;
54
55 if (Val != int32_t(Val)) {
Chris Lattner8e946be2010-02-21 03:22:59 +000056 OS << (int)(unsigned char)(Val >> 32) << ", "
57 << (int)(unsigned char)(Val >> 40) << ", "
58 << (int)(unsigned char)(Val >> 48) << ", "
59 << (int)(unsigned char)(Val >> 56) << ", ";
Chris Lattnerda272d12010-02-15 08:04:42 +000060 BytesEmitted += 4;
61 }
62 }
63
Chris Lattner8e946be2010-02-21 03:22:59 +000064 OS.PadToColumn(CommentIndent) << "// " << Val << " aka 0x";
65 OS.write_hex(Val) << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +000066 return BytesEmitted;
67}
68
Chris Lattnere02ea542010-02-16 06:52:01 +000069namespace {
70class MatcherTableEmitter {
Chris Lattner050a03d2010-02-16 07:21:10 +000071 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 Lattner8e946be2010-02-21 03:22:59 +000076
77
78 DenseMap<Record*, unsigned> NodeXFormMap;
79 std::vector<const Record*> NodeXForms;
80
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 Lattner984e1882010-02-21 06:30:04 +000084 unsigned EmitMatcherList(const MatcherNode *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 Lattnere02ea542010-02-16 06:52:01 +000088private:
Chris Lattner984e1882010-02-21 06:30:04 +000089 unsigned EmitMatcher(const MatcherNode *N, unsigned Indent,
90 formatted_raw_ostream &OS);
Chris Lattner050a03d2010-02-16 07:21:10 +000091
92 unsigned getNodePredicate(StringRef PredName) {
93 unsigned &Entry = NodePredicateMap[PredName];
94 if (Entry == 0) {
95 NodePredicates.push_back(PredName.str());
96 Entry = NodePredicates.size();
97 }
98 return Entry-1;
99 }
100 unsigned getPatternPredicate(StringRef PredName) {
101 unsigned &Entry = PatternPredicateMap[PredName];
102 if (Entry == 0) {
103 PatternPredicates.push_back(PredName.str());
104 Entry = PatternPredicates.size();
105 }
106 return Entry-1;
107 }
Chris Lattnere609a512010-02-17 00:31:50 +0000108
109 unsigned getComplexPat(const ComplexPattern &P) {
110 unsigned &Entry = ComplexPatternMap[&P];
111 if (Entry == 0) {
112 ComplexPatterns.push_back(&P);
113 Entry = ComplexPatterns.size();
114 }
115 return Entry-1;
116 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000117
118 unsigned getNodeXFormID(Record *Rec) {
119 unsigned &Entry = NodeXFormMap[Rec];
120 if (Entry == 0) {
121 NodeXForms.push_back(Rec);
122 Entry = NodeXForms.size();
123 }
124 return Entry-1;
125 }
126
Chris Lattnere02ea542010-02-16 06:52:01 +0000127};
128} // end anonymous namespace.
129
Chris Lattner5007e1e2010-02-23 00:59:59 +0000130/// EmitVBRValue - Emit the specified value as a VBR, returning the number of
131/// bytes emitted.
132static unsigned EmitVBRValue(unsigned Val, raw_ostream &OS) {
133 if (Val <= 127) {
134 OS << Val << ", ";
135 return 1;
136 }
137
138 unsigned InVal = Val;
139 unsigned NumBytes = 0;
Chris Lattner860d4a72010-02-23 01:07:39 +0000140 while (Val >= 128) {
Chris Lattner5007e1e2010-02-23 00:59:59 +0000141 OS << (Val&127) << "|128,";
142 Val >>= 7;
143 ++NumBytes;
144 }
145 OS << Val << "/*" << InVal << "*/, ";
146 return NumBytes+1;
147}
148
Chris Lattnerda272d12010-02-15 08:04:42 +0000149/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
150/// the number of bytes emitted.
Chris Lattnere02ea542010-02-16 06:52:01 +0000151unsigned MatcherTableEmitter::
Chris Lattner984e1882010-02-21 06:30:04 +0000152EmitMatcher(const MatcherNode *N, unsigned Indent, formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000153 OS.PadToColumn(Indent*2);
154
155 switch (N->getKind()) {
156 case MatcherNode::Push: assert(0 && "Should be handled by caller");
Chris Lattner845c0422010-02-18 22:03:03 +0000157 case MatcherNode::RecordNode:
158 OS << "OPC_RecordNode,";
Chris Lattnerc96087b2010-02-17 01:03:09 +0000159 OS.PadToColumn(CommentIndent) << "// "
160 << cast<RecordMatcherNode>(N)->getWhatFor() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000161 return 1;
Chris Lattner19b5a752010-02-24 07:31:45 +0000162
163 case MatcherNode::RecordChild:
164 OS << "OPC_RecordChild" << cast<RecordChildMatcherNode>(N)->getChildNo()
165 << ',';
166 OS.PadToColumn(CommentIndent) << "// "
167 << cast<RecordChildMatcherNode>(N)->getWhatFor() << '\n';
168 return 1;
Chris Lattner8e946be2010-02-21 03:22:59 +0000169
170 case MatcherNode::RecordMemRef:
171 OS << "OPC_RecordMemRef,\n";
172 return 1;
173
174 case MatcherNode::CaptureFlagInput:
175 OS << "OPC_CaptureFlagInput,\n";
176 return 1;
177
Chris Lattnerda272d12010-02-15 08:04:42 +0000178 case MatcherNode::MoveChild:
179 OS << "OPC_MoveChild, "
180 << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
181 return 2;
182
183 case MatcherNode::MoveParent:
184 OS << "OPC_MoveParent,\n";
185 return 1;
186
187 case MatcherNode::CheckSame:
188 OS << "OPC_CheckSame, "
189 << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
190 return 2;
191
Chris Lattner050a03d2010-02-16 07:21:10 +0000192 case MatcherNode::CheckPatternPredicate: {
193 StringRef Pred = cast<CheckPatternPredicateMatcherNode>(N)->getPredicate();
194 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
195 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000196 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000197 }
198 case MatcherNode::CheckPredicate: {
199 StringRef Pred = cast<CheckPredicateMatcherNode>(N)->getPredicateName();
200 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
201 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000202 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000203 }
204
Chris Lattnerda272d12010-02-15 08:04:42 +0000205 case MatcherNode::CheckOpcode:
206 OS << "OPC_CheckOpcode, "
207 << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
208 return 2;
209
Chris Lattner12a667c2010-02-22 22:30:37 +0000210 case MatcherNode::CheckMultiOpcode: {
211 const CheckMultiOpcodeMatcherNode *CMO=cast<CheckMultiOpcodeMatcherNode>(N);
212 OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodeNames() << ", ";
213 for (unsigned i = 0, e = CMO->getNumOpcodeNames(); i != e; ++i)
214 OS << CMO->getOpcodeName(i) << ", ";
215 OS << '\n';
216 return 2 + CMO->getNumOpcodeNames();
217 }
218
Chris Lattnerda272d12010-02-15 08:04:42 +0000219 case MatcherNode::CheckType:
220 OS << "OPC_CheckType, "
221 << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
222 return 2;
223
224 case MatcherNode::CheckInteger: {
225 int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
226 OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
227 return EmitInt(Val, OS)+1;
228 }
229 case MatcherNode::CheckCondCode:
230 OS << "OPC_CheckCondCode, ISD::"
231 << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
232 return 2;
233
234 case MatcherNode::CheckValueType:
235 OS << "OPC_CheckValueType, MVT::"
236 << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
237 return 2;
238
Chris Lattner5be6e592010-02-17 00:39:26 +0000239 case MatcherNode::CheckComplexPat: {
240 const ComplexPattern &Pattern =
241 cast<CheckComplexPatMatcherNode>(N)->getPattern();
242 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
Chris Lattner781f3592010-02-17 06:47:35 +0000243 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
244 OS << ": " << Pattern.getNumOperands() << " operands";
245 if (Pattern.hasProperty(SDNPHasChain))
246 OS << " + chain result and input";
247 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000248 return 2;
Chris Lattner5be6e592010-02-17 00:39:26 +0000249 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000250
251 case MatcherNode::CheckAndImm: {
252 int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
253 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
254 return EmitInt(Val, OS)+1;
255 }
256
257 case MatcherNode::CheckOrImm: {
258 int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
259 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
260 return EmitInt(Val, OS)+1;
261 }
Chris Lattner21390d72010-02-16 19:15:55 +0000262 case MatcherNode::CheckFoldableChainNode:
263 OS << "OPC_CheckFoldableChainNode,\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000264 return 1;
Chris Lattner9a747f12010-02-17 06:23:39 +0000265 case MatcherNode::CheckChainCompatible:
266 OS << "OPC_CheckChainCompatible, "
267 << cast<CheckChainCompatibleMatcherNode>(N)->getPreviousOp() << ",\n";
268 return 2;
Chris Lattner845c0422010-02-18 22:03:03 +0000269
Chris Lattner906b4992010-02-19 07:49:56 +0000270 case MatcherNode::EmitInteger: {
271 int64_t Val = cast<EmitIntegerMatcherNode>(N)->getValue();
272 OS << "OPC_EmitInteger" << ClassifyInt(Val) << ", "
273 << getEnumName(cast<EmitIntegerMatcherNode>(N)->getVT()) << ", ";
274 return EmitInt(Val, OS)+2;
275 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000276 case MatcherNode::EmitStringInteger: {
277 const std::string &Val = cast<EmitStringIntegerMatcherNode>(N)->getValue();
278 // These should always fit into one byte.
279 OS << "OPC_EmitInteger1, "
280 << getEnumName(cast<EmitStringIntegerMatcherNode>(N)->getVT()) << ", "
281 << Val << ",\n";
282 return 3;
283 }
Chris Lattner906b4992010-02-19 07:49:56 +0000284
Chris Lattner845c0422010-02-18 22:03:03 +0000285 case MatcherNode::EmitRegister:
Chris Lattner906b4992010-02-19 07:49:56 +0000286 OS << "OPC_EmitRegister, "
287 << getEnumName(cast<EmitRegisterMatcherNode>(N)->getVT()) << ", ";
288 if (Record *R = cast<EmitRegisterMatcherNode>(N)->getReg())
289 OS << getQualifiedName(R) << ",\n";
290 else
291 OS << "0 /*zero_reg*/,\n";
292 return 3;
Chris Lattner8e946be2010-02-21 03:22:59 +0000293
294 case MatcherNode::EmitConvertToTarget:
295 OS << "OPC_EmitConvertToTarget, "
296 << cast<EmitConvertToTargetMatcherNode>(N)->getSlot() << ",\n";
297 return 2;
298
299 case MatcherNode::EmitMergeInputChains: {
300 const EmitMergeInputChainsMatcherNode *MN =
301 cast<EmitMergeInputChainsMatcherNode>(N);
302 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
303 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
304 OS << MN->getNode(i) << ", ";
305 OS << '\n';
306 return 2+MN->getNumNodes();
307 }
308 case MatcherNode::EmitCopyToReg:
309 OS << "OPC_EmitCopyToReg, "
310 << cast<EmitCopyToRegMatcherNode>(N)->getSrcSlot() << ", "
311 << getQualifiedName(cast<EmitCopyToRegMatcherNode>(N)->getDestPhysReg())
312 << ",\n";
313 return 3;
314 case MatcherNode::EmitNodeXForm: {
315 const EmitNodeXFormMatcherNode *XF = cast<EmitNodeXFormMatcherNode>(N);
316 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
317 << XF->getSlot() << ',';
318 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
319 return 3;
320 }
321
322 case MatcherNode::EmitNode: {
323 const EmitNodeMatcherNode *EN = cast<EmitNodeMatcherNode>(N);
324 OS << "OPC_EmitNode, TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
325
326 if (EN->hasChain()) OS << "|OPFL_Chain";
327 if (EN->hasFlag()) OS << "|OPFL_Flag";
328 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
329 if (EN->getNumFixedArityOperands() != -1)
330 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
331 OS << ",\n";
332
333 OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
334 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
335 OS << getEnumName(EN->getVT(i)) << ", ";
336
337 OS << EN->getNumOperands() << "/*#Ops*/, ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000338 unsigned NumOperandBytes = 0;
339 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
340 // We emit the operand numbers in VBR encoded format, in case the number
341 // is too large to represent with a byte.
342 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
343 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000344 OS << '\n';
Chris Lattner5007e1e2010-02-23 00:59:59 +0000345 return 6+EN->getNumVTs()+NumOperandBytes;
Chris Lattner8e946be2010-02-21 03:22:59 +0000346 }
Chris Lattner02f73582010-02-24 05:33:42 +0000347 case MatcherNode::MarkFlagResults: {
348 const MarkFlagResultsMatcherNode *CFR = cast<MarkFlagResultsMatcherNode>(N);
349 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
350 unsigned NumOperandBytes = 0;
351 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
352 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
353 OS << '\n';
354 return 2+NumOperandBytes;
355 }
Chris Lattner77f2e272010-02-21 06:03:07 +0000356 case MatcherNode::CompleteMatch: {
357 const CompleteMatchMatcherNode *CM = cast<CompleteMatchMatcherNode>(N);
358 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000359 unsigned NumResultBytes = 0;
Chris Lattner77f2e272010-02-21 06:03:07 +0000360 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
Chris Lattner5007e1e2010-02-23 00:59:59 +0000361 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
Chris Lattner77f2e272010-02-21 06:03:07 +0000362 OS << '\n';
363 OS.PadToColumn(Indent*2) << "// Src: "
364 << *CM->getPattern().getSrcPattern() << '\n';
365 OS.PadToColumn(Indent*2) << "// Dst: "
366 << *CM->getPattern().getDstPattern() << '\n';
Chris Lattner5007e1e2010-02-23 00:59:59 +0000367 return 2 + NumResultBytes;
Chris Lattnerda272d12010-02-15 08:04:42 +0000368 }
Chris Lattner77f2e272010-02-21 06:03:07 +0000369 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000370 assert(0 && "Unreachable");
371 return 0;
372}
373
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000374/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
Chris Lattnere02ea542010-02-16 06:52:01 +0000375unsigned MatcherTableEmitter::
Chris Lattner8fbad242010-02-21 07:16:41 +0000376EmitMatcherList(const MatcherNode *N, unsigned Indent, unsigned CurrentIdx,
Chris Lattner984e1882010-02-21 06:30:04 +0000377 formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000378 unsigned Size = 0;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000379 while (N) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000380 // Push is a special case since it is binary.
381 if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
382 // We need to encode the child and the offset of the failure code before
383 // emitting either of them. Handle this by buffering the output into a
384 // string while we get the size.
385 SmallString<128> TmpBuf;
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000386 unsigned NextSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000387 {
388 raw_svector_ostream OS(TmpBuf);
389 formatted_raw_ostream FOS(OS);
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000390 NextSize = EmitMatcherList(cast<PushMatcherNode>(N)->getNext(),
Chris Lattner8fbad242010-02-21 07:16:41 +0000391 Indent+1, CurrentIdx+2, FOS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000392 }
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000393
394 // In the unlikely event that we have something too big to emit with a
395 // one byte offset, regenerate it with a two-byte one.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000396 if (NextSize > 255) {
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000397 TmpBuf.clear();
398 raw_svector_ostream OS(TmpBuf);
399 formatted_raw_ostream FOS(OS);
400 NextSize = EmitMatcherList(cast<PushMatcherNode>(N)->getNext(),
401 Indent+1, CurrentIdx+3, FOS);
402 if (NextSize > 65535) {
403 errs() <<
404 "Tblgen internal error: can't handle pattern this complex yet\n";
405 exit(1);
406 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000407 }
408
Chris Lattner8fbad242010-02-21 07:16:41 +0000409 OS << "/*" << CurrentIdx << "*/";
Chris Lattnerda272d12010-02-15 08:04:42 +0000410 OS.PadToColumn(Indent*2);
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000411
412 if (NextSize < 256)
413 OS << "OPC_Push, " << NextSize << ",\n";
414 else
415 OS << "OPC_Push2, " << (NextSize&255) << ", " << (NextSize>>8) << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000416 OS << TmpBuf.str();
417
Chris Lattner8fbad242010-02-21 07:16:41 +0000418 Size += 2+NextSize;
419 CurrentIdx += 2+NextSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000420 N = PMN->getFailure();
421 continue;
422 }
423
Chris Lattner8fbad242010-02-21 07:16:41 +0000424 OS << "/*" << CurrentIdx << "*/";
425 unsigned MatcherSize = EmitMatcher(N, Indent, OS);
426 Size += MatcherSize;
427 CurrentIdx += MatcherSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000428
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000429 // If there are other nodes in this list, iterate to them, otherwise we're
Chris Lattnerda272d12010-02-15 08:04:42 +0000430 // done.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000431 N = N->getNext();
Chris Lattnerda272d12010-02-15 08:04:42 +0000432 }
Chris Lattner8ef9c792010-02-18 02:49:24 +0000433 return Size;
Chris Lattnerda272d12010-02-15 08:04:42 +0000434}
435
Chris Lattner984e1882010-02-21 06:30:04 +0000436void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
Chris Lattnerb49985a2010-02-18 06:47:49 +0000437 // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
438 // here into the case stmts.
439
Chris Lattnere609a512010-02-17 00:31:50 +0000440 // Emit pattern predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000441 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
442 OS << " switch (PredNo) {\n";
443 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
444 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
445 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
446 OS << " }\n";
447 OS << "}\n\n";
448
Chris Lattnere609a512010-02-17 00:31:50 +0000449 // Emit Node predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000450 OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
451 OS << " switch (PredNo) {\n";
452 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
453 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
454 OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n";
455 OS << " }\n";
456 OS << "}\n\n";
Chris Lattnere609a512010-02-17 00:31:50 +0000457
458 // Emit CompletePattern matchers.
Chris Lattner8e946be2010-02-21 03:22:59 +0000459 // FIXME: This should be const.
Chris Lattnere609a512010-02-17 00:31:50 +0000460 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
461 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
462 OS << " switch (PatternNo) {\n";
463 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
464 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
465 const ComplexPattern &P = *ComplexPatterns[i];
466 unsigned NumOps = P.getNumOperands();
Chris Lattner8e946be2010-02-21 03:22:59 +0000467
Chris Lattnere609a512010-02-17 00:31:50 +0000468 if (P.hasProperty(SDNPHasChain))
Chris Lattner8e946be2010-02-21 03:22:59 +0000469 ++NumOps; // Get the chained node too.
470
Chris Lattnere609a512010-02-17 00:31:50 +0000471 OS << " case " << i << ":\n";
472 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000473 OS << " return " << P.getSelectFunc();
474
475 // FIXME: Temporary hack until old isel dies.
476 if (P.hasProperty(SDNPHasChain))
477 OS << "XXX";
478
479 OS << "(Root, N";
Chris Lattnere609a512010-02-17 00:31:50 +0000480 for (unsigned i = 0; i != NumOps; ++i)
481 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
482 OS << ");\n";
483 }
484 OS << " }\n";
485 OS << "}\n\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000486
487 // Emit SDNodeXForm handlers.
488 // FIXME: This should be const.
489 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
490 OS << " switch (XFormNo) {\n";
491 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
492
493 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
494 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i)
495 OS << " case " << i << ": return Transform_" << NodeXForms[i]->getName()
496 << "(V.getNode());\n";
497 OS << " }\n";
498 OS << "}\n\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000499}
500
501
Chris Lattnerda272d12010-02-15 08:04:42 +0000502void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
503 formatted_raw_ostream OS(O);
504
505 OS << "// The main instruction selector code.\n";
506 OS << "SDNode *SelectCode2(SDNode *N) {\n";
507
Chris Lattner984e1882010-02-21 06:30:04 +0000508 MatcherTableEmitter MatcherEmitter;
Chris Lattnere02ea542010-02-16 06:52:01 +0000509
Chris Lattner8e946be2010-02-21 03:22:59 +0000510 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
511 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000512 OS << " static const unsigned char MatcherTable[] = {\n";
Chris Lattner8fbad242010-02-21 07:16:41 +0000513 unsigned TotalSize = MatcherEmitter.EmitMatcherList(Matcher, 5, 0, OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000514 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000515 OS << " #undef TARGET_OPCODE\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000516 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000517 OS << "\n";
518
519 // Next up, emit the function for node and pattern predicates:
Chris Lattner984e1882010-02-21 06:30:04 +0000520 MatcherEmitter.EmitPredicateFunctions(OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000521}