blob: 93dbf2d162ac30ab68b25d57df2184c89b9934a0 [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 Lattnerb21ba712010-02-25 02:04:40 +000091 unsigned EmitMatcher(const Matcher *N, unsigned Indent,
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 Lattner5007e1e2010-02-23 00:59:59 +0000132/// EmitVBRValue - Emit the specified value as a VBR, returning the number of
133/// bytes emitted.
134static unsigned EmitVBRValue(unsigned Val, raw_ostream &OS) {
135 if (Val <= 127) {
136 OS << Val << ", ";
137 return 1;
138 }
139
140 unsigned InVal = Val;
141 unsigned NumBytes = 0;
Chris Lattner860d4a72010-02-23 01:07:39 +0000142 while (Val >= 128) {
Chris Lattner5007e1e2010-02-23 00:59:59 +0000143 OS << (Val&127) << "|128,";
144 Val >>= 7;
145 ++NumBytes;
146 }
147 OS << Val << "/*" << InVal << "*/, ";
148 return NumBytes+1;
149}
150
Chris Lattnerda272d12010-02-15 08:04:42 +0000151/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
152/// the number of bytes emitted.
Chris Lattnere02ea542010-02-16 06:52:01 +0000153unsigned MatcherTableEmitter::
Chris Lattnerb21ba712010-02-25 02:04:40 +0000154EmitMatcher(const Matcher *N, unsigned Indent, formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000155 OS.PadToColumn(Indent*2);
156
157 switch (N->getKind()) {
Chris Lattnerb21ba712010-02-25 02:04:40 +0000158 case Matcher::Scope: assert(0 && "Should be handled by caller");
159 case Matcher::RecordNode:
Chris Lattner845c0422010-02-18 22:03:03 +0000160 OS << "OPC_RecordNode,";
Chris Lattnerc96087b2010-02-17 01:03:09 +0000161 OS.PadToColumn(CommentIndent) << "// "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000162 << cast<RecordMatcher>(N)->getWhatFor() << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000163 return 1;
Chris Lattner19b5a752010-02-24 07:31:45 +0000164
Chris Lattnerb21ba712010-02-25 02:04:40 +0000165 case Matcher::RecordChild:
166 OS << "OPC_RecordChild" << cast<RecordChildMatcher>(N)->getChildNo()
Chris Lattner19b5a752010-02-24 07:31:45 +0000167 << ',';
168 OS.PadToColumn(CommentIndent) << "// "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000169 << cast<RecordChildMatcher>(N)->getWhatFor() << '\n';
Chris Lattner19b5a752010-02-24 07:31:45 +0000170 return 1;
Chris Lattner8e946be2010-02-21 03:22:59 +0000171
Chris Lattnerb21ba712010-02-25 02:04:40 +0000172 case Matcher::RecordMemRef:
Chris Lattner8e946be2010-02-21 03:22:59 +0000173 OS << "OPC_RecordMemRef,\n";
174 return 1;
175
Chris Lattnerb21ba712010-02-25 02:04:40 +0000176 case Matcher::CaptureFlagInput:
Chris Lattner8e946be2010-02-21 03:22:59 +0000177 OS << "OPC_CaptureFlagInput,\n";
178 return 1;
179
Chris Lattnerb21ba712010-02-25 02:04:40 +0000180 case Matcher::MoveChild:
181 OS << "OPC_MoveChild, " << cast<MoveChildMatcher>(N)->getChildNo() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000182 return 2;
183
Chris Lattnerb21ba712010-02-25 02:04:40 +0000184 case Matcher::MoveParent:
Chris Lattnerda272d12010-02-15 08:04:42 +0000185 OS << "OPC_MoveParent,\n";
186 return 1;
187
Chris Lattnerb21ba712010-02-25 02:04:40 +0000188 case Matcher::CheckSame:
Chris Lattnerda272d12010-02-15 08:04:42 +0000189 OS << "OPC_CheckSame, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000190 << cast<CheckSameMatcher>(N)->getMatchNumber() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000191 return 2;
192
Chris Lattnerb21ba712010-02-25 02:04:40 +0000193 case Matcher::CheckPatternPredicate: {
194 StringRef Pred = cast<CheckPatternPredicateMatcher>(N)->getPredicate();
Chris Lattner050a03d2010-02-16 07:21:10 +0000195 OS << "OPC_CheckPatternPredicate, " << getPatternPredicate(Pred) << ',';
196 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000197 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000198 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000199 case Matcher::CheckPredicate: {
200 StringRef Pred = cast<CheckPredicateMatcher>(N)->getPredicateName();
Chris Lattner050a03d2010-02-16 07:21:10 +0000201 OS << "OPC_CheckPredicate, " << getNodePredicate(Pred) << ',';
202 OS.PadToColumn(CommentIndent) << "// " << Pred << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000203 return 2;
Chris Lattner050a03d2010-02-16 07:21:10 +0000204 }
205
Chris Lattnerb21ba712010-02-25 02:04:40 +0000206 case Matcher::CheckOpcode:
Chris Lattnerda272d12010-02-15 08:04:42 +0000207 OS << "OPC_CheckOpcode, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000208 << cast<CheckOpcodeMatcher>(N)->getOpcodeName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000209 return 2;
210
Chris Lattnerb21ba712010-02-25 02:04:40 +0000211 case Matcher::CheckMultiOpcode: {
212 const CheckMultiOpcodeMatcher *CMO = cast<CheckMultiOpcodeMatcher>(N);
Chris Lattner12a667c2010-02-22 22:30:37 +0000213 OS << "OPC_CheckMultiOpcode, " << CMO->getNumOpcodeNames() << ", ";
214 for (unsigned i = 0, e = CMO->getNumOpcodeNames(); i != e; ++i)
215 OS << CMO->getOpcodeName(i) << ", ";
216 OS << '\n';
217 return 2 + CMO->getNumOpcodeNames();
218 }
219
Chris Lattnerb21ba712010-02-25 02:04:40 +0000220 case Matcher::CheckType:
Chris Lattnerda272d12010-02-15 08:04:42 +0000221 OS << "OPC_CheckType, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000222 << getEnumName(cast<CheckTypeMatcher>(N)->getType()) << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000223 return 2;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000224 case Matcher::CheckChildType:
Chris Lattner23cfda72010-02-24 20:15:25 +0000225 OS << "OPC_CheckChild"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000226 << cast<CheckChildTypeMatcher>(N)->getChildNo() << "Type, "
227 << getEnumName(cast<CheckChildTypeMatcher>(N)->getType()) << ",\n";
Chris Lattner23cfda72010-02-24 20:15:25 +0000228 return 2;
229
Chris Lattnerb21ba712010-02-25 02:04:40 +0000230 case Matcher::CheckInteger: {
231 int64_t Val = cast<CheckIntegerMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000232 OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
233 return EmitInt(Val, OS)+1;
234 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000235 case Matcher::CheckCondCode:
Chris Lattnerda272d12010-02-15 08:04:42 +0000236 OS << "OPC_CheckCondCode, ISD::"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000237 << cast<CheckCondCodeMatcher>(N)->getCondCodeName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000238 return 2;
239
Chris Lattnerb21ba712010-02-25 02:04:40 +0000240 case Matcher::CheckValueType:
Chris Lattnerda272d12010-02-15 08:04:42 +0000241 OS << "OPC_CheckValueType, MVT::"
Chris Lattnerb21ba712010-02-25 02:04:40 +0000242 << cast<CheckValueTypeMatcher>(N)->getTypeName() << ",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000243 return 2;
244
Chris Lattnerb21ba712010-02-25 02:04:40 +0000245 case Matcher::CheckComplexPat: {
Chris Lattner5be6e592010-02-17 00:39:26 +0000246 const ComplexPattern &Pattern =
Chris Lattnerb21ba712010-02-25 02:04:40 +0000247 cast<CheckComplexPatMatcher>(N)->getPattern();
Chris Lattner5be6e592010-02-17 00:39:26 +0000248 OS << "OPC_CheckComplexPat, " << getComplexPat(Pattern) << ',';
Chris Lattner781f3592010-02-17 06:47:35 +0000249 OS.PadToColumn(CommentIndent) << "// " << Pattern.getSelectFunc();
250 OS << ": " << Pattern.getNumOperands() << " operands";
251 if (Pattern.hasProperty(SDNPHasChain))
252 OS << " + chain result and input";
253 OS << '\n';
Chris Lattnerda272d12010-02-15 08:04:42 +0000254 return 2;
Chris Lattner5be6e592010-02-17 00:39:26 +0000255 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000256
Chris Lattnerb21ba712010-02-25 02:04:40 +0000257 case Matcher::CheckAndImm: {
258 int64_t Val = cast<CheckAndImmMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000259 OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
260 return EmitInt(Val, OS)+1;
261 }
262
Chris Lattnerb21ba712010-02-25 02:04:40 +0000263 case Matcher::CheckOrImm: {
264 int64_t Val = cast<CheckOrImmMatcher>(N)->getValue();
Chris Lattnerda272d12010-02-15 08:04:42 +0000265 OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
266 return EmitInt(Val, OS)+1;
267 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000268 case Matcher::CheckFoldableChainNode:
Chris Lattner21390d72010-02-16 19:15:55 +0000269 OS << "OPC_CheckFoldableChainNode,\n";
Chris Lattnere39650a2010-02-16 06:10:58 +0000270 return 1;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000271 case Matcher::CheckChainCompatible:
Chris Lattner9a747f12010-02-17 06:23:39 +0000272 OS << "OPC_CheckChainCompatible, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000273 << cast<CheckChainCompatibleMatcher>(N)->getPreviousOp() << ",\n";
Chris Lattner9a747f12010-02-17 06:23:39 +0000274 return 2;
Chris Lattner845c0422010-02-18 22:03:03 +0000275
Chris Lattnerb21ba712010-02-25 02:04:40 +0000276 case Matcher::EmitInteger: {
277 int64_t Val = cast<EmitIntegerMatcher>(N)->getValue();
Chris Lattner906b4992010-02-19 07:49:56 +0000278 OS << "OPC_EmitInteger" << ClassifyInt(Val) << ", "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000279 << getEnumName(cast<EmitIntegerMatcher>(N)->getVT()) << ", ";
Chris Lattner906b4992010-02-19 07:49:56 +0000280 return EmitInt(Val, OS)+2;
281 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000282 case Matcher::EmitStringInteger: {
283 const std::string &Val = cast<EmitStringIntegerMatcher>(N)->getValue();
Chris Lattner8e946be2010-02-21 03:22:59 +0000284 // These should always fit into one byte.
285 OS << "OPC_EmitInteger1, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000286 << getEnumName(cast<EmitStringIntegerMatcher>(N)->getVT()) << ", "
Chris Lattner8e946be2010-02-21 03:22:59 +0000287 << Val << ",\n";
288 return 3;
289 }
Chris Lattner906b4992010-02-19 07:49:56 +0000290
Chris Lattnerb21ba712010-02-25 02:04:40 +0000291 case Matcher::EmitRegister:
Chris Lattner906b4992010-02-19 07:49:56 +0000292 OS << "OPC_EmitRegister, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000293 << getEnumName(cast<EmitRegisterMatcher>(N)->getVT()) << ", ";
294 if (Record *R = cast<EmitRegisterMatcher>(N)->getReg())
Chris Lattner906b4992010-02-19 07:49:56 +0000295 OS << getQualifiedName(R) << ",\n";
296 else
297 OS << "0 /*zero_reg*/,\n";
298 return 3;
Chris Lattner8e946be2010-02-21 03:22:59 +0000299
Chris Lattnerb21ba712010-02-25 02:04:40 +0000300 case Matcher::EmitConvertToTarget:
Chris Lattner8e946be2010-02-21 03:22:59 +0000301 OS << "OPC_EmitConvertToTarget, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000302 << cast<EmitConvertToTargetMatcher>(N)->getSlot() << ",\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000303 return 2;
304
Chris Lattnerb21ba712010-02-25 02:04:40 +0000305 case Matcher::EmitMergeInputChains: {
306 const EmitMergeInputChainsMatcher *MN =
307 cast<EmitMergeInputChainsMatcher>(N);
Chris Lattner8e946be2010-02-21 03:22:59 +0000308 OS << "OPC_EmitMergeInputChains, " << MN->getNumNodes() << ", ";
309 for (unsigned i = 0, e = MN->getNumNodes(); i != e; ++i)
310 OS << MN->getNode(i) << ", ";
311 OS << '\n';
312 return 2+MN->getNumNodes();
313 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000314 case Matcher::EmitCopyToReg:
Chris Lattner8e946be2010-02-21 03:22:59 +0000315 OS << "OPC_EmitCopyToReg, "
Chris Lattnerb21ba712010-02-25 02:04:40 +0000316 << cast<EmitCopyToRegMatcher>(N)->getSrcSlot() << ", "
317 << getQualifiedName(cast<EmitCopyToRegMatcher>(N)->getDestPhysReg())
Chris Lattner8e946be2010-02-21 03:22:59 +0000318 << ",\n";
319 return 3;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000320 case Matcher::EmitNodeXForm: {
321 const EmitNodeXFormMatcher *XF = cast<EmitNodeXFormMatcher>(N);
Chris Lattner8e946be2010-02-21 03:22:59 +0000322 OS << "OPC_EmitNodeXForm, " << getNodeXFormID(XF->getNodeXForm()) << ", "
323 << XF->getSlot() << ',';
324 OS.PadToColumn(CommentIndent) << "// "<<XF->getNodeXForm()->getName()<<'\n';
325 return 3;
326 }
327
Chris Lattnerb21ba712010-02-25 02:04:40 +0000328 case Matcher::EmitNode: {
329 const EmitNodeMatcher *EN = cast<EmitNodeMatcher>(N);
Chris Lattner8e946be2010-02-21 03:22:59 +0000330 OS << "OPC_EmitNode, TARGET_OPCODE(" << EN->getOpcodeName() << "), 0";
331
332 if (EN->hasChain()) OS << "|OPFL_Chain";
333 if (EN->hasFlag()) OS << "|OPFL_Flag";
334 if (EN->hasMemRefs()) OS << "|OPFL_MemRefs";
335 if (EN->getNumFixedArityOperands() != -1)
336 OS << "|OPFL_Variadic" << EN->getNumFixedArityOperands();
337 OS << ",\n";
338
339 OS.PadToColumn(Indent*2+4) << EN->getNumVTs() << "/*#VTs*/, ";
340 for (unsigned i = 0, e = EN->getNumVTs(); i != e; ++i)
341 OS << getEnumName(EN->getVT(i)) << ", ";
342
343 OS << EN->getNumOperands() << "/*#Ops*/, ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000344 unsigned NumOperandBytes = 0;
345 for (unsigned i = 0, e = EN->getNumOperands(); i != e; ++i) {
346 // We emit the operand numbers in VBR encoded format, in case the number
347 // is too large to represent with a byte.
348 NumOperandBytes += EmitVBRValue(EN->getOperand(i), OS);
349 }
Chris Lattner8e946be2010-02-21 03:22:59 +0000350 OS << '\n';
Chris Lattner5007e1e2010-02-23 00:59:59 +0000351 return 6+EN->getNumVTs()+NumOperandBytes;
Chris Lattner8e946be2010-02-21 03:22:59 +0000352 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000353 case Matcher::MarkFlagResults: {
354 const MarkFlagResultsMatcher *CFR = cast<MarkFlagResultsMatcher>(N);
Chris Lattner02f73582010-02-24 05:33:42 +0000355 OS << "OPC_MarkFlagResults, " << CFR->getNumNodes() << ", ";
356 unsigned NumOperandBytes = 0;
357 for (unsigned i = 0, e = CFR->getNumNodes(); i != e; ++i)
358 NumOperandBytes += EmitVBRValue(CFR->getNode(i), OS);
359 OS << '\n';
360 return 2+NumOperandBytes;
361 }
Chris Lattnerb21ba712010-02-25 02:04:40 +0000362 case Matcher::CompleteMatch: {
363 const CompleteMatchMatcher *CM = cast<CompleteMatchMatcher>(N);
Chris Lattner77f2e272010-02-21 06:03:07 +0000364 OS << "OPC_CompleteMatch, " << CM->getNumResults() << ", ";
Chris Lattner5007e1e2010-02-23 00:59:59 +0000365 unsigned NumResultBytes = 0;
Chris Lattner77f2e272010-02-21 06:03:07 +0000366 for (unsigned i = 0, e = CM->getNumResults(); i != e; ++i)
Chris Lattner5007e1e2010-02-23 00:59:59 +0000367 NumResultBytes += EmitVBRValue(CM->getResult(i), OS);
Chris Lattner77f2e272010-02-21 06:03:07 +0000368 OS << '\n';
369 OS.PadToColumn(Indent*2) << "// Src: "
370 << *CM->getPattern().getSrcPattern() << '\n';
371 OS.PadToColumn(Indent*2) << "// Dst: "
372 << *CM->getPattern().getDstPattern() << '\n';
Chris Lattner5007e1e2010-02-23 00:59:59 +0000373 return 2 + NumResultBytes;
Chris Lattnerda272d12010-02-15 08:04:42 +0000374 }
Chris Lattner77f2e272010-02-21 06:03:07 +0000375 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000376 assert(0 && "Unreachable");
377 return 0;
378}
379
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000380/// EmitMatcherList - Emit the bytes for the specified matcher subtree.
Chris Lattnere02ea542010-02-16 06:52:01 +0000381unsigned MatcherTableEmitter::
Chris Lattnerb21ba712010-02-25 02:04:40 +0000382EmitMatcherList(const Matcher *N, unsigned Indent, unsigned CurrentIdx,
Chris Lattner984e1882010-02-21 06:30:04 +0000383 formatted_raw_ostream &OS) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000384 unsigned Size = 0;
Chris Lattner8ef9c792010-02-18 02:49:24 +0000385 while (N) {
Chris Lattner09b9f392010-02-24 19:17:12 +0000386 if (unsigned(N->getKind()) >= Histogram.size())
387 Histogram.resize(N->getKind()+1);
388 Histogram[N->getKind()]++;
389
Chris Lattner60df53e2010-02-25 01:56:48 +0000390 // Scope is a special case since it is binary.
Chris Lattnerb21ba712010-02-25 02:04:40 +0000391 if (const ScopeMatcher *SMN = dyn_cast<ScopeMatcher>(N)) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000392 // We need to encode the child and the offset of the failure code before
393 // emitting either of them. Handle this by buffering the output into a
394 // string while we get the size.
395 SmallString<128> TmpBuf;
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000396 unsigned NextSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000397 {
398 raw_svector_ostream OS(TmpBuf);
399 formatted_raw_ostream FOS(OS);
Chris Lattnerb21ba712010-02-25 02:04:40 +0000400 NextSize = EmitMatcherList(cast<ScopeMatcher>(N)->getCheck(),
Chris Lattner8fbad242010-02-21 07:16:41 +0000401 Indent+1, CurrentIdx+2, FOS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000402 }
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000403
404 // In the unlikely event that we have something too big to emit with a
405 // one byte offset, regenerate it with a two-byte one.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000406 if (NextSize > 255) {
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000407 TmpBuf.clear();
408 raw_svector_ostream OS(TmpBuf);
409 formatted_raw_ostream FOS(OS);
Chris Lattnerb21ba712010-02-25 02:04:40 +0000410 NextSize = EmitMatcherList(cast<ScopeMatcher>(N)->getCheck(),
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000411 Indent+1, CurrentIdx+3, FOS);
412 if (NextSize > 65535) {
413 errs() <<
414 "Tblgen internal error: can't handle pattern this complex yet\n";
415 exit(1);
416 }
Chris Lattnerda272d12010-02-15 08:04:42 +0000417 }
418
Chris Lattner8fbad242010-02-21 07:16:41 +0000419 OS << "/*" << CurrentIdx << "*/";
Chris Lattnerda272d12010-02-15 08:04:42 +0000420 OS.PadToColumn(Indent*2);
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000421
422 if (NextSize < 256)
Chris Lattner60df53e2010-02-25 01:56:48 +0000423 OS << "OPC_Scope, " << NextSize << ",\n";
Chris Lattner3e22f2d2010-02-22 23:55:39 +0000424 else
Chris Lattner60df53e2010-02-25 01:56:48 +0000425 OS << "OPC_Scope2, " << (NextSize&255) << ", " << (NextSize>>8) <<",\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000426 OS << TmpBuf.str();
427
Chris Lattner8fbad242010-02-21 07:16:41 +0000428 Size += 2+NextSize;
429 CurrentIdx += 2+NextSize;
Chris Lattner60df53e2010-02-25 01:56:48 +0000430 N = SMN->getNext();
Chris Lattnerda272d12010-02-15 08:04:42 +0000431 continue;
432 }
433
Chris Lattner8fbad242010-02-21 07:16:41 +0000434 OS << "/*" << CurrentIdx << "*/";
435 unsigned MatcherSize = EmitMatcher(N, Indent, OS);
436 Size += MatcherSize;
437 CurrentIdx += MatcherSize;
Chris Lattnerda272d12010-02-15 08:04:42 +0000438
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000439 // If there are other nodes in this list, iterate to them, otherwise we're
Chris Lattnerda272d12010-02-15 08:04:42 +0000440 // done.
Chris Lattnerbd8227f2010-02-18 02:53:41 +0000441 N = N->getNext();
Chris Lattnerda272d12010-02-15 08:04:42 +0000442 }
Chris Lattner8ef9c792010-02-18 02:49:24 +0000443 return Size;
Chris Lattnerda272d12010-02-15 08:04:42 +0000444}
445
Chris Lattner984e1882010-02-21 06:30:04 +0000446void MatcherTableEmitter::EmitPredicateFunctions(formatted_raw_ostream &OS) {
Chris Lattnerb49985a2010-02-18 06:47:49 +0000447 // FIXME: Don't build off the DAGISelEmitter's predicates, emit them directly
448 // here into the case stmts.
449
Chris Lattnere609a512010-02-17 00:31:50 +0000450 // Emit pattern predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000451 OS << "bool CheckPatternPredicate(unsigned PredNo) const {\n";
452 OS << " switch (PredNo) {\n";
453 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
454 for (unsigned i = 0, e = PatternPredicates.size(); i != e; ++i)
455 OS << " case " << i << ": return " << PatternPredicates[i] << ";\n";
456 OS << " }\n";
457 OS << "}\n\n";
458
Chris Lattnere609a512010-02-17 00:31:50 +0000459 // Emit Node predicates.
Chris Lattner050a03d2010-02-16 07:21:10 +0000460 OS << "bool CheckNodePredicate(SDNode *N, unsigned PredNo) const {\n";
461 OS << " switch (PredNo) {\n";
462 OS << " default: assert(0 && \"Invalid predicate in table?\");\n";
463 for (unsigned i = 0, e = NodePredicates.size(); i != e; ++i)
464 OS << " case " << i << ": return " << NodePredicates[i] << "(N);\n";
465 OS << " }\n";
466 OS << "}\n\n";
Chris Lattnere609a512010-02-17 00:31:50 +0000467
468 // Emit CompletePattern matchers.
Chris Lattner8e946be2010-02-21 03:22:59 +0000469 // FIXME: This should be const.
Chris Lattnere609a512010-02-17 00:31:50 +0000470 OS << "bool CheckComplexPattern(SDNode *Root, SDValue N,\n";
471 OS << " unsigned PatternNo, SmallVectorImpl<SDValue> &Result) {\n";
472 OS << " switch (PatternNo) {\n";
473 OS << " default: assert(0 && \"Invalid pattern # in table?\");\n";
474 for (unsigned i = 0, e = ComplexPatterns.size(); i != e; ++i) {
475 const ComplexPattern &P = *ComplexPatterns[i];
476 unsigned NumOps = P.getNumOperands();
Chris Lattner8e946be2010-02-21 03:22:59 +0000477
Chris Lattnere609a512010-02-17 00:31:50 +0000478 if (P.hasProperty(SDNPHasChain))
Chris Lattner8e946be2010-02-21 03:22:59 +0000479 ++NumOps; // Get the chained node too.
480
Chris Lattnere609a512010-02-17 00:31:50 +0000481 OS << " case " << i << ":\n";
482 OS << " Result.resize(Result.size()+" << NumOps << ");\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000483 OS << " return " << P.getSelectFunc();
484
485 // FIXME: Temporary hack until old isel dies.
486 if (P.hasProperty(SDNPHasChain))
487 OS << "XXX";
488
489 OS << "(Root, N";
Chris Lattnere609a512010-02-17 00:31:50 +0000490 for (unsigned i = 0; i != NumOps; ++i)
491 OS << ", Result[Result.size()-" << (NumOps-i) << ']';
492 OS << ");\n";
493 }
494 OS << " }\n";
495 OS << "}\n\n";
Chris Lattner8e946be2010-02-21 03:22:59 +0000496
497 // Emit SDNodeXForm handlers.
498 // FIXME: This should be const.
499 OS << "SDValue RunSDNodeXForm(SDValue V, unsigned XFormNo) {\n";
500 OS << " switch (XFormNo) {\n";
501 OS << " default: assert(0 && \"Invalid xform # in table?\");\n";
502
503 // FIXME: The node xform could take SDValue's instead of SDNode*'s.
504 for (unsigned i = 0, e = NodeXForms.size(); i != e; ++i)
505 OS << " case " << i << ": return Transform_" << NodeXForms[i]->getName()
506 << "(V.getNode());\n";
507 OS << " }\n";
508 OS << "}\n\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000509}
510
Chris Lattner09b9f392010-02-24 19:17:12 +0000511void MatcherTableEmitter::EmitHistogram(formatted_raw_ostream &OS) {
512 OS << " // Opcode Histogram:\n";
513 for (unsigned i = 0, e = Histogram.size(); i != e; ++i) {
514 OS << " // #";
Chris Lattnerb21ba712010-02-25 02:04:40 +0000515 switch ((Matcher::KindTy)i) {
516 case Matcher::Scope: OS << "OPC_Scope"; break;
517 case Matcher::RecordNode: OS << "OPC_RecordNode"; break;
518 case Matcher::RecordChild: OS << "OPC_RecordChild"; break;
519 case Matcher::RecordMemRef: OS << "OPC_RecordMemRef"; break;
520 case Matcher::CaptureFlagInput: OS << "OPC_CaptureFlagInput"; break;
521 case Matcher::MoveChild: OS << "OPC_MoveChild"; break;
522 case Matcher::MoveParent: OS << "OPC_MoveParent"; break;
523 case Matcher::CheckSame: OS << "OPC_CheckSame"; break;
524 case Matcher::CheckPatternPredicate:
Chris Lattner09b9f392010-02-24 19:17:12 +0000525 OS << "OPC_CheckPatternPredicate"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000526 case Matcher::CheckPredicate: OS << "OPC_CheckPredicate"; break;
527 case Matcher::CheckOpcode: OS << "OPC_CheckOpcode"; break;
528 case Matcher::CheckMultiOpcode: OS << "OPC_CheckMultiOpcode"; break;
529 case Matcher::CheckType: OS << "OPC_CheckType"; break;
530 case Matcher::CheckChildType: OS << "OPC_CheckChildType"; break;
531 case Matcher::CheckInteger: OS << "OPC_CheckInteger"; break;
532 case Matcher::CheckCondCode: OS << "OPC_CheckCondCode"; break;
533 case Matcher::CheckValueType: OS << "OPC_CheckValueType"; break;
534 case Matcher::CheckComplexPat: OS << "OPC_CheckComplexPat"; break;
535 case Matcher::CheckAndImm: OS << "OPC_CheckAndImm"; break;
536 case Matcher::CheckOrImm: OS << "OPC_CheckOrImm"; break;
537 case Matcher::CheckFoldableChainNode:
Chris Lattner09b9f392010-02-24 19:17:12 +0000538 OS << "OPC_CheckFoldableChainNode"; break;
Chris Lattner499622b2010-02-25 02:09:00 +0000539 case Matcher::CheckChainCompatible: OS << "OPC_CheckChainCompatible"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000540 case Matcher::EmitInteger: OS << "OPC_EmitInteger"; break;
541 case Matcher::EmitStringInteger: OS << "OPC_EmitStringInteger"; break;
542 case Matcher::EmitRegister: OS << "OPC_EmitRegister"; break;
Chris Lattner499622b2010-02-25 02:09:00 +0000543 case Matcher::EmitConvertToTarget: OS << "OPC_EmitConvertToTarget"; break;
544 case Matcher::EmitMergeInputChains: OS << "OPC_EmitMergeInputChains"; break;
Chris Lattnerb21ba712010-02-25 02:04:40 +0000545 case Matcher::EmitCopyToReg: OS << "OPC_EmitCopyToReg"; break;
546 case Matcher::EmitNode: OS << "OPC_EmitNode"; break;
547 case Matcher::EmitNodeXForm: OS << "OPC_EmitNodeXForm"; break;
548 case Matcher::MarkFlagResults: OS << "OPC_MarkFlagResults"; break;
549 case Matcher::CompleteMatch: OS << "OPC_CompleteMatch"; break;
Chris Lattner09b9f392010-02-24 19:17:12 +0000550 }
551
552 OS.PadToColumn(40) << " = " << Histogram[i] << '\n';
553 }
554 OS << '\n';
555}
556
Chris Lattner050a03d2010-02-16 07:21:10 +0000557
Chris Lattnerb21ba712010-02-25 02:04:40 +0000558void llvm::EmitMatcherTable(const Matcher *TheMatcher, raw_ostream &O) {
Chris Lattnerda272d12010-02-15 08:04:42 +0000559 formatted_raw_ostream OS(O);
560
561 OS << "// The main instruction selector code.\n";
Chris Lattnerc84edb72010-02-24 07:35:09 +0000562 OS << "SDNode *SelectCode(SDNode *N) {\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000563
Chris Lattner984e1882010-02-21 06:30:04 +0000564 MatcherTableEmitter MatcherEmitter;
Chris Lattnere02ea542010-02-16 06:52:01 +0000565
Chris Lattner8e946be2010-02-21 03:22:59 +0000566 OS << " // Opcodes are emitted as 2 bytes, TARGET_OPCODE handles this.\n";
567 OS << " #define TARGET_OPCODE(X) X & 255, unsigned(X) >> 8\n";
Chris Lattnerda272d12010-02-15 08:04:42 +0000568 OS << " static const unsigned char MatcherTable[] = {\n";
Chris Lattnerb21ba712010-02-25 02:04:40 +0000569 unsigned TotalSize = MatcherEmitter.EmitMatcherList(TheMatcher, 5, 0, OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000570 OS << " 0\n }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
Chris Lattner09b9f392010-02-24 19:17:12 +0000571
572 MatcherEmitter.EmitHistogram(OS);
573
Chris Lattner8e946be2010-02-21 03:22:59 +0000574 OS << " #undef TARGET_OPCODE\n";
Chris Lattnere02ea542010-02-16 06:52:01 +0000575 OS << " return SelectCodeCommon(N, MatcherTable,sizeof(MatcherTable));\n}\n";
Chris Lattner050a03d2010-02-16 07:21:10 +0000576 OS << "\n";
577
578 // Next up, emit the function for node and pattern predicates:
Chris Lattner984e1882010-02-21 06:30:04 +0000579 MatcherEmitter.EmitPredicateFunctions(OS);
Chris Lattnerda272d12010-02-15 08:04:42 +0000580}