blob: 8d89eeeb7ae22f68afeedda79bc9b36784da54e0 [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000018#include "llvm/Support/MathExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000019#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000020#include <algorithm>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000021using namespace llvm;
22
Chris Lattnerca559d02005-09-08 21:03:01 +000023//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000024// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000025//
26
Chris Lattner6cefb772008-01-05 22:25:12 +000027/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
28/// ComplexPattern.
29static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000030 return (N->isLeaf() &&
31 dynamic_cast<DefInit*>(N->getLeafValue()) &&
32 static_cast<DefInit*>(N->getLeafValue())->getDef()->
33 isSubClassOf("ComplexPattern"));
34}
35
Chris Lattner6cefb772008-01-05 22:25:12 +000036/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
37/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000038static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000039 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000040 if (N->isLeaf() &&
41 dynamic_cast<DefInit*>(N->getLeafValue()) &&
42 static_cast<DefInit*>(N->getLeafValue())->getDef()->
43 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000044 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
45 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000046 }
47 return NULL;
48}
49
Chris Lattner05814af2005-09-28 17:57:56 +000050/// getPatternSize - Return the 'size' of this pattern. We want to match large
51/// patterns before small ones. This is used to determine the size of a
52/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000053static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000054 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
55 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000056 P->getExtTypeNum(0) == MVT::isVoid ||
57 P->getExtTypeNum(0) == MVT::Flag ||
58 P->getExtTypeNum(0) == MVT::iPTR) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000059 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000060 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000061 // If the root node is a ConstantSDNode, increases its size.
62 // e.g. (set R32:$dst, 0).
63 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000064 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000065
66 // FIXME: This is a hack to statically increase the priority of patterns
67 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
68 // Later we can allow complexity / cost for each pattern to be (optionally)
69 // specified. To get best possible pattern match we'll need to dynamically
70 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000071 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000072 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000073 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000074
75 // If this node has some predicate function that must match, it adds to the
76 // complexity of this node.
77 if (!P->getPredicateFn().empty())
78 ++Size;
79
Chris Lattner05814af2005-09-28 17:57:56 +000080 // Count children in the count if they are also nodes.
81 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
82 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000083 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000084 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000085 else if (Child->isLeaf()) {
86 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000087 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000088 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000089 Size += getPatternSize(Child, CGP);
Chris Lattner3e179802006-02-03 18:06:02 +000090 else if (!Child->getPredicateFn().empty())
91 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000092 }
Chris Lattner05814af2005-09-28 17:57:56 +000093 }
94
95 return Size;
96}
97
98/// getResultPatternCost - Compute the number of instructions for this pattern.
99/// This is a temporary hack. We should really include the instruction
100/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000101static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000102 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000103 if (P->isLeaf()) return 0;
104
Evan Chengfbad7082006-02-18 02:33:09 +0000105 unsigned Cost = 0;
106 Record *Op = P->getOperator();
107 if (Op->isSubClassOf("Instruction")) {
108 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000109 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000110 if (II.usesCustomDAGSchedInserter)
111 Cost += 10;
112 }
Chris Lattner05814af2005-09-28 17:57:56 +0000113 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000114 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000115 return Cost;
116}
117
Evan Chenge6f32032006-07-19 00:24:41 +0000118/// getResultPatternCodeSize - Compute the code size of instructions for this
119/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000120static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000121 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000122 if (P->isLeaf()) return 0;
123
124 unsigned Cost = 0;
125 Record *Op = P->getOperator();
126 if (Op->isSubClassOf("Instruction")) {
127 Cost += Op->getValueAsInt("CodeSize");
128 }
129 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000130 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000131 return Cost;
132}
133
Chris Lattner05814af2005-09-28 17:57:56 +0000134// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
135// In particular, we want to match maximal patterns first and lowest cost within
136// a particular complexity first.
137struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000138 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
139 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000140
Chris Lattner60d81392008-01-05 22:30:17 +0000141 bool operator()(const PatternToMatch *LHS,
142 const PatternToMatch *RHS) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000143 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
144 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000145 LHSSize += LHS->getAddedComplexity();
146 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000147 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
148 if (LHSSize < RHSSize) return false;
149
150 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000151 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
152 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000153 if (LHSCost < RHSCost) return true;
154 if (LHSCost > RHSCost) return false;
155
Chris Lattner6cefb772008-01-05 22:25:12 +0000156 return getResultPatternSize(LHS->getDstPattern(), CGP) <
157 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000158 }
159};
160
Nate Begeman6510b222005-12-01 04:51:06 +0000161/// getRegisterValueType - Look up and return the first ValueType of specified
162/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000163static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000164 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
165 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000166 return MVT::Other;
167}
168
Chris Lattner72fe91c2005-09-24 00:40:24 +0000169
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000170/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
171/// type information from it.
172static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000173 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000174 if (!N->isLeaf())
175 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
176 RemoveAllTypes(N->getChild(i));
177}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000178
Evan Cheng51fecc82006-01-09 18:27:06 +0000179/// NodeHasProperty - return true if TreePatternNode has the specified
180/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000181static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000182 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000183 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000184 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000185 if (CP)
186 return CP->hasProperty(Property);
187 return false;
188 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000189 Record *Operator = N->getOperator();
190 if (!Operator->isSubClassOf("SDNode")) return false;
191
Chris Lattner6cefb772008-01-05 22:25:12 +0000192 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000193}
194
Evan Cheng94b30402006-10-11 21:02:01 +0000195static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000196 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000197 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000198 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000199
200 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
201 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000202 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000203 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000204 }
205
206 return false;
207}
208
Chris Lattnerdc32f982008-01-05 22:43:57 +0000209//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000210// Node Transformation emitter implementation.
211//
212void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
213 // Walk the pattern fragments, adding them to a map, which sorts them by
214 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000215 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000216 NXsByNameTy NXsByName;
217
Chris Lattnerfe718932008-01-06 01:10:31 +0000218 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000219 I != E; ++I)
220 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
221
222 OS << "\n// Node transformations.\n";
223
224 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
225 I != E; ++I) {
226 Record *SDNode = I->second.first;
227 std::string Code = I->second.second;
228
229 if (Code.empty()) continue; // Empty code? Skip it.
230
Chris Lattner200c57e2008-01-05 22:58:54 +0000231 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000232 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
233
234 OS << "inline SDOperand Transform_" << I->first << "(SDNode *" << C2
235 << ") {\n";
236 if (ClassName != "SDNode")
237 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
238 OS << Code << "\n}\n";
239 }
240}
241
242//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000243// Predicate emitter implementation.
244//
245
246void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
247 OS << "\n// Predicate functions.\n";
248
249 // Walk the pattern fragments, adding them to a map, which sorts them by
250 // name.
251 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
252 PFsByNameTy PFsByName;
253
Chris Lattnerfe718932008-01-06 01:10:31 +0000254 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000255 I != E; ++I)
256 PFsByName.insert(std::make_pair(I->first->getName(), *I));
257
258
259 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
260 I != E; ++I) {
261 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
262 TreePattern *P = I->second.second;
263
264 // If there is a code init for this fragment, emit the predicate code.
265 std::string Code = PatFragRecord->getValueAsCode("Predicate");
266 if (Code.empty()) continue;
267
268 if (P->getOnlyTree()->isLeaf())
269 OS << "inline bool Predicate_" << PatFragRecord->getName()
270 << "(SDNode *N) {\n";
271 else {
272 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000273 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000274 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
275
276 OS << "inline bool Predicate_" << PatFragRecord->getName()
277 << "(SDNode *" << C2 << ") {\n";
278 if (ClassName != "SDNode")
279 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
280 }
281 OS << Code << "\n}\n";
282 }
283
284 OS << "\n\n";
285}
286
287
288//===----------------------------------------------------------------------===//
289// PatternCodeEmitter implementation.
290//
Evan Chengb915f312005-12-09 22:45:35 +0000291class PatternCodeEmitter {
292private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000293 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000294
Evan Cheng58e84a62005-12-14 22:02:59 +0000295 // Predicates.
296 ListInit *Predicates;
Evan Cheng59413202006-04-19 18:07:24 +0000297 // Pattern cost.
298 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000299 // Instruction selector pattern.
300 TreePatternNode *Pattern;
301 // Matched instruction.
302 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000303
Evan Chengb915f312005-12-09 22:45:35 +0000304 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000305 std::map<std::string, std::string> VariableMap;
306 // Node to operator mapping
307 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000308 // Name of the folded node which produces a flag.
309 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000310 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000311 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000312 // Original input chain(s).
313 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000314 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000315
Dan Gohman69de1932008-02-06 22:27:42 +0000316 /// LSI - Load/Store information.
317 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
318 /// for each memory access. This facilitates the use of AliasAnalysis in
319 /// the backend.
320 std::vector<std::string> LSI;
321
Evan Cheng676d7312006-08-26 00:59:04 +0000322 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000323 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000324 /// tested, and if true, the match fails) [when 1], or normal code to emit
325 /// [when 0], or initialization code to emit [when 2].
326 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Evan Cheng21ad3922006-02-07 00:37:41 +0000327 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
328 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000329 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000330 /// TargetOpcodes - The target specific opcodes used by the resulting
331 /// instructions.
332 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000333 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000334 /// OutputIsVariadic - Records whether the instruction output pattern uses
335 /// variable_ops. This requires that the Emit function be passed an
336 /// additional argument to indicate where the input varargs operands
337 /// begin.
338 bool &OutputIsVariadic;
339 /// NumInputRootOps - Records the number of operands the root node of the
340 /// input pattern has. This information is used in the generated code to
341 /// pass to Emit functions when variable_ops processing is needed.
342 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000343
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000344 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000345 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000346 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000347 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000348
349 void emitCheck(const std::string &S) {
350 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000351 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000352 }
353 void emitCode(const std::string &S) {
354 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000355 GeneratedCode.push_back(std::make_pair(0, S));
356 }
357 void emitInit(const std::string &S) {
358 if (!S.empty())
359 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000360 }
Evan Chengf5493192006-08-26 01:02:19 +0000361 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000362 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000363 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000364 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000365 void emitOpcode(const std::string &Opc) {
366 TargetOpcodes.push_back(Opc);
367 OpcNo++;
368 }
Evan Chengf8729402006-07-16 06:12:52 +0000369 void emitVT(const std::string &VT) {
370 TargetVTs.push_back(VT);
371 VTNo++;
372 }
Evan Chengb915f312005-12-09 22:45:35 +0000373public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000374 PatternCodeEmitter(CodeGenDAGPatterns &cgp, ListInit *preds,
Evan Cheng58e84a62005-12-14 22:02:59 +0000375 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000376 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000377 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000378 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000379 std::vector<std::string> &tv,
380 bool &oiv,
381 unsigned &niro)
Chris Lattner6cefb772008-01-05 22:25:12 +0000382 : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000383 GeneratedCode(gc), GeneratedDecl(gd),
384 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000385 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000386 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000387
388 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
389 /// if the match fails. At this point, we already know that the opcode for N
390 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000391 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
392 const std::string &RootName, const std::string &ChainSuffix,
393 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000394
395 // Save loads/stores matched by a pattern.
396 if (!N->isLeaf() && N->getName().empty()) {
397 std::string EnumName = N->getOperator()->getValueAsString("Opcode");
398 if (EnumName == "ISD::LOAD" ||
399 EnumName == "ISD::STORE") {
400 LSI.push_back(RootName);
401 }
402 }
403
Evan Chenge41bf822006-02-05 06:43:12 +0000404 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000405 // Emit instruction predicates. Each predicate is just a string for now.
406 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000407 // Record input varargs info.
408 NumInputRootOps = N->getNumChildren();
409
Chris Lattner8a0604b2006-01-28 20:31:24 +0000410 std::string PredicateCheck;
Evan Cheng58e84a62005-12-14 22:02:59 +0000411 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
412 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
413 Record *Def = Pred->getDef();
Chris Lattner8a0604b2006-01-28 20:31:24 +0000414 if (!Def->isSubClassOf("Predicate")) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000415#ifndef NDEBUG
416 Def->dump();
417#endif
Evan Cheng58e84a62005-12-14 22:02:59 +0000418 assert(0 && "Unknown predicate type!");
419 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000420 if (!PredicateCheck.empty())
Chris Lattnerbc7fa522006-09-19 00:41:36 +0000421 PredicateCheck += " && ";
Chris Lattner67a202b2006-01-28 20:43:52 +0000422 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
Evan Cheng58e84a62005-12-14 22:02:59 +0000423 }
424 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000425
426 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000427 }
428
Evan Chengb915f312005-12-09 22:45:35 +0000429 if (N->isLeaf()) {
430 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000431 emitCheck("cast<ConstantSDNode>(" + RootName +
Chris Lattner67a202b2006-01-28 20:43:52 +0000432 ")->getSignExtended() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000433 return;
434 } else if (!NodeIsComplexPattern(N)) {
435 assert(0 && "Cannot match this as a leaf value!");
436 abort();
437 }
438 }
439
Chris Lattner488580c2006-01-28 19:06:51 +0000440 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000441 // we already saw this in the pattern, emit code to verify dagness.
442 if (!N->getName().empty()) {
443 std::string &VarMapEntry = VariableMap[N->getName()];
444 if (VarMapEntry.empty()) {
445 VarMapEntry = RootName;
446 } else {
447 // If we get here, this is a second reference to a specific name. Since
448 // we already have checked that the first reference is valid, we don't
449 // have to recursively match it, just check that it's the same as the
450 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000451 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000452 return;
453 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000454
455 if (!N->isLeaf())
456 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000457 }
458
459
460 // Emit code to load the child nodes and match their contents recursively.
461 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000462 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
463 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000464 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000465 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000466 if (NodeHasChain)
467 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000468 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000469 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000470 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000471 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000472 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000473 // If the immediate use can somehow reach this node through another
474 // path, then can't fold it either or it will create a cycle.
475 // e.g. In the following diagram, XX can reach ld through YY. If
476 // ld is folded into XX, then YY is both a predecessor and a successor
477 // of XX.
478 //
479 // [ld]
480 // ^ ^
481 // | |
482 // / \---
483 // / [YY]
484 // | ^
485 // [XX]-------|
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000486 bool NeedCheck = false;
487 if (P != Pattern)
488 NeedCheck = true;
489 else {
Chris Lattner6cefb772008-01-05 22:25:12 +0000490 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000491 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000492 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
493 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
494 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000495 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000496 PInfo.hasProperty(SDNPHasChain) ||
497 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000498 PInfo.hasProperty(SDNPOptInFlag);
499 }
500
501 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000502 std::string ParentName(RootName.begin(), RootName.end()-1);
Chris Lattner706d2d32006-08-09 16:44:44 +0000503 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
Evan Chengce1381a2006-10-14 08:30:15 +0000504 ".Val, N.Val)");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000505 }
Evan Chenge41bf822006-02-05 06:43:12 +0000506 }
Evan Chengb915f312005-12-09 22:45:35 +0000507 }
Evan Chenge41bf822006-02-05 06:43:12 +0000508
Evan Chengc15d18c2006-01-27 22:13:45 +0000509 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000510 if (FoundChain) {
511 emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || "
512 "IsChainCompatible(" + ChainName + ".Val, " +
513 RootName + ".Val))");
514 OrigChains.push_back(std::make_pair(ChainName, RootName));
515 } else
Evan Chenge6389932006-07-21 22:19:51 +0000516 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000517 ChainName = "Chain" + ChainSuffix;
Evan Cheng676d7312006-08-26 00:59:04 +0000518 emitInit("SDOperand " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000519 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000520 }
Evan Chengb915f312005-12-09 22:45:35 +0000521 }
522
Evan Cheng54597732006-01-26 00:22:25 +0000523 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000524 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000525 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000526 // FIXME: If the optional incoming flag does not exist. Then it is ok to
527 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000528 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000529 (PatternHasProperty(N, SDNPInFlag, CGP) ||
530 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
531 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000532 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000533 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000534 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000535 }
536 }
537
Evan Chengd3eea902006-10-09 21:02:17 +0000538 // If there is a node predicate for this, emit the call.
539 if (!N->getPredicateFn().empty())
540 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
541
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000542
Chris Lattner39e73f72006-10-11 04:05:55 +0000543 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
544 // a constant without a predicate fn that has more that one bit set, handle
545 // this as a special case. This is usually for targets that have special
546 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
547 // handling stuff). Using these instructions is often far more efficient
548 // than materializing the constant. Unfortunately, both the instcombiner
549 // and the dag combiner can often infer that bits are dead, and thus drop
550 // them from the mask in the dag. For example, it might turn 'AND X, 255'
551 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
552 // to handle this.
553 if (!N->isLeaf() &&
554 (N->getOperator()->getName() == "and" ||
555 N->getOperator()->getName() == "or") &&
556 N->getChild(1)->isLeaf() &&
557 N->getChild(1)->getPredicateFn().empty()) {
558 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
559 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
560 emitInit("SDOperand " + RootName + "0" + " = " +
561 RootName + ".getOperand(" + utostr(0) + ");");
562 emitInit("SDOperand " + RootName + "1" + " = " +
563 RootName + ".getOperand(" + utostr(1) + ");");
564
565 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
566 const char *MaskPredicate = N->getOperator()->getName() == "or"
567 ? "CheckOrMask(" : "CheckAndMask(";
568 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
569 RootName + "1), " + itostr(II->getValue()) + ")");
570
Christopher Lamb85356242008-01-31 07:27:46 +0000571 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000572 ChainSuffix + utostr(0), FoundChain);
573 return;
574 }
575 }
576 }
577
Evan Chengb915f312005-12-09 22:45:35 +0000578 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Evan Cheng676d7312006-08-26 00:59:04 +0000579 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000580 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000581
Christopher Lamb85356242008-01-31 07:27:46 +0000582 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000583 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000584 }
585
Evan Cheng676d7312006-08-26 00:59:04 +0000586 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000587 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000588 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000589 std::string Fn = CP->getSelectFunc();
590 unsigned NumOps = CP->getNumOperands();
591 for (unsigned i = 0; i < NumOps; ++i) {
592 emitDecl("CPTmp" + utostr(i));
593 emitCode("SDOperand CPTmp" + utostr(i) + ";");
594 }
Evan Cheng94b30402006-10-11 21:02:01 +0000595 if (CP->hasProperty(SDNPHasChain)) {
596 emitDecl("CPInChain");
597 emitDecl("Chain" + ChainSuffix);
598 emitCode("SDOperand CPInChain;");
599 emitCode("SDOperand Chain" + ChainSuffix + ";");
600 }
Evan Cheng676d7312006-08-26 00:59:04 +0000601
Evan Cheng811731e2006-11-08 20:31:10 +0000602 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000603 for (unsigned i = 0; i < NumOps; i++)
604 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000605 if (CP->hasProperty(SDNPHasChain)) {
606 ChainName = "Chain" + ChainSuffix;
607 Code += ", CPInChain, Chain" + ChainSuffix;
608 }
Evan Cheng676d7312006-08-26 00:59:04 +0000609 emitCheck(Code + ")");
610 }
Evan Chengb915f312005-12-09 22:45:35 +0000611 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000612
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000613 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000614 const std::string &RootName,
615 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000616 const std::string &ChainSuffix, bool &FoundChain) {
617 if (!Child->isLeaf()) {
618 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000619 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000620 emitCheck(RootName + ".getOpcode() == " +
621 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000622 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000623 bool HasChain = false;
624 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
625 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000626 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000627 }
628 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
629 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
630 "Pattern folded multiple nodes which produce flags?");
631 FoldedFlag = std::make_pair(RootName,
632 CInfo.getNumResults() + (unsigned)HasChain);
633 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000634 } else {
635 // If this child has a name associated with it, capture it in VarMap. If
636 // we already saw this in the pattern, emit code to verify dagness.
637 if (!Child->getName().empty()) {
638 std::string &VarMapEntry = VariableMap[Child->getName()];
639 if (VarMapEntry.empty()) {
640 VarMapEntry = RootName;
641 } else {
642 // If we get here, this is a second reference to a specific name.
643 // Since we already have checked that the first reference is valid,
644 // we don't have to recursively match it, just check that it's the
645 // same as the previously named thing.
646 emitCheck(VarMapEntry + " == " + RootName);
647 Duplicates.insert(RootName);
648 return;
649 }
650 }
651
652 // Handle leaves of various types.
653 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
654 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000655 if (LeafRec->isSubClassOf("RegisterClass") ||
656 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000657 // Handle register references. Nothing to do here.
658 } else if (LeafRec->isSubClassOf("Register")) {
659 // Handle register references.
660 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
661 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000662 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000663 std::string Fn = CP->getSelectFunc();
664 unsigned NumOps = CP->getNumOperands();
665 for (unsigned i = 0; i < NumOps; ++i) {
666 emitDecl("CPTmp" + utostr(i));
667 emitCode("SDOperand CPTmp" + utostr(i) + ";");
668 }
Evan Cheng94b30402006-10-11 21:02:01 +0000669 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000670 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000671 FoldedChains.push_back(std::make_pair("CPInChain",
672 PInfo.getNumResults()));
673 ChainName = "Chain" + ChainSuffix;
674 emitDecl("CPInChain");
675 emitDecl(ChainName);
676 emitCode("SDOperand CPInChain;");
677 emitCode("SDOperand " + ChainName + ";");
678 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000679
Christopher Lamb85356242008-01-31 07:27:46 +0000680 std::string Code = Fn + "(";
681 if (CP->hasAttribute(CPAttrParentAsRoot)) {
682 Code += ParentRootName + ", ";
683 } else {
684 Code += "N, ";
685 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000686 if (CP->hasProperty(SDNPHasChain)) {
687 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000688 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000689 }
690 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000691 for (unsigned i = 0; i < NumOps; i++)
692 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000693 if (CP->hasProperty(SDNPHasChain))
694 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000695 emitCheck(Code + ")");
696 } else if (LeafRec->getName() == "srcvalue") {
697 // Place holder for SRCVALUE nodes. Nothing to do here.
698 } else if (LeafRec->isSubClassOf("ValueType")) {
699 // Make sure this is the specified value type.
700 emitCheck("cast<VTSDNode>(" + RootName +
701 ")->getVT() == MVT::" + LeafRec->getName());
702 } else if (LeafRec->isSubClassOf("CondCode")) {
703 // Make sure this is the specified cond code.
704 emitCheck("cast<CondCodeSDNode>(" + RootName +
705 ")->get() == ISD::" + LeafRec->getName());
706 } else {
707#ifndef NDEBUG
708 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000709 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000710#endif
711 assert(0 && "Unknown leaf type!");
712 }
713
714 // If there is a node predicate for this, emit the call.
715 if (!Child->getPredicateFn().empty())
716 emitCheck(Child->getPredicateFn() + "(" + RootName +
717 ".Val)");
718 } else if (IntInit *II =
719 dynamic_cast<IntInit*>(Child->getLeafValue())) {
720 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
721 unsigned CTmp = TmpNo++;
722 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
723 RootName + ")->getSignExtended();");
724
725 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
726 } else {
727#ifndef NDEBUG
728 Child->dump();
729#endif
730 assert(0 && "Unknown leaf type!");
731 }
732 }
733 }
Evan Chengb915f312005-12-09 22:45:35 +0000734
735 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
736 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000737 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000738 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000739 bool InFlagDecled, bool ResNodeDecled,
740 bool LikeLeaf = false, bool isRoot = false) {
741 // List of arguments of getTargetNode() or SelectNodeTo().
742 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000743 // This is something selected from the pattern we matched.
744 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000745 const std::string &VarName = N->getName();
746 std::string Val = VariableMap[VarName];
747 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000748 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000749 cerr << "Variable '" << VarName << " referenced but not defined "
750 << "and not caught earlier!\n";
751 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000752 }
Evan Chengb915f312005-12-09 22:45:35 +0000753 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
754 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000755 NodeOps.push_back(Val);
756 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000757 }
758
759 const ComplexPattern *CP;
760 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000761 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000762 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000763 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000764 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000765 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000766 default:
767 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
768 << " type as an immediate constant. Aborting\n";
769 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000770 case MVT::i1: CastType = "bool"; break;
771 case MVT::i8: CastType = "unsigned char"; break;
772 case MVT::i16: CastType = "unsigned short"; break;
773 case MVT::i32: CastType = "unsigned"; break;
774 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000775 }
Scott Michel6be48d42008-01-29 02:29:31 +0000776 emitCode("SDOperand " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000777 " = CurDAG->getTargetConstant(((" + CastType +
778 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
779 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000780 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
781 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000782 Val = TmpVar;
783 ModifiedVal = true;
784 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000785 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
786 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
787 std::string TmpVar = "Tmp" + utostr(ResNo);
788 emitCode("SDOperand " + TmpVar +
789 " = CurDAG->getTargetConstantFP(cast<ConstantFPSDNode>(" +
790 Val + ")->getValueAPF(), cast<ConstantFPSDNode>(" + Val +
791 ")->getValueType(0));");
792 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
793 // value if used multiple times by this pattern result.
794 Val = TmpVar;
795 ModifiedVal = true;
796 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000797 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000798 Record *Op = OperatorMap[N->getName()];
799 // Transform ExternalSymbol to TargetExternalSymbol
800 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000801 std::string TmpVar = "Tmp"+utostr(ResNo);
802 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000803 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000804 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000805 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000806 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
807 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000808 Val = TmpVar;
809 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000810 }
Scott Michel6be48d42008-01-29 02:29:31 +0000811 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000812 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
813 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000814 Record *Op = OperatorMap[N->getName()];
815 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000816 if (Op && (Op->getName() == "globaladdr" ||
817 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000818 std::string TmpVar = "Tmp" + utostr(ResNo);
819 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000820 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000821 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000822 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000823 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
824 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000825 Val = TmpVar;
826 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000827 }
Evan Cheng676d7312006-08-26 00:59:04 +0000828 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000829 } else if (!N->isLeaf()
830 && (N->getOperator()->getName() == "texternalsym"
831 || N->getOperator()->getName() == "tconstpool")) {
832 // Do not rewrite the variable name, since we don't generate a new
833 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000834 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000835 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000836 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
837 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
838 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000839 }
Evan Chengb915f312005-12-09 22:45:35 +0000840 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000841 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000842 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000843 if (!LikeLeaf) {
844 emitCode("AddToISelQueue(" + Val + ");");
Chris Lattner706d2d32006-08-09 16:44:44 +0000845 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000846 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000847 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000848 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000849 }
Evan Cheng676d7312006-08-26 00:59:04 +0000850 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000851 }
Scott Michel6be48d42008-01-29 02:29:31 +0000852
853 if (ModifiedVal) {
854 VariableMap[VarName] = Val;
855 }
Evan Cheng676d7312006-08-26 00:59:04 +0000856 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000857 }
Evan Chengb915f312005-12-09 22:45:35 +0000858 if (N->isLeaf()) {
859 // If this is an explicit register reference, handle it.
860 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
861 unsigned ResNo = TmpNo++;
862 if (DI->getDef()->isSubClassOf("Register")) {
Evan Cheng676d7312006-08-26 00:59:04 +0000863 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000864 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000865 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000866 NodeOps.push_back("Tmp" + utostr(ResNo));
867 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000868 } else if (DI->getDef()->getName() == "zero_reg") {
869 emitCode("SDOperand Tmp" + utostr(ResNo) +
870 " = CurDAG->getRegister(0, " +
871 getEnumName(N->getTypeNum(0)) + ");");
872 NodeOps.push_back("Tmp" + utostr(ResNo));
873 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000874 }
875 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
876 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000877 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Evan Cheng676d7312006-08-26 00:59:04 +0000878 emitCode("SDOperand Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000879 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
880 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000881 NodeOps.push_back("Tmp" + utostr(ResNo));
882 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000883 }
884
Jim Laskey16d42c62006-07-11 18:25:13 +0000885#ifndef NDEBUG
886 N->dump();
887#endif
Evan Chengb915f312005-12-09 22:45:35 +0000888 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000889 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000890 }
891
892 Record *Op = N->getOperator();
893 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000894 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000895 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000896 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000897 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000898 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000899 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000900 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
901 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000902 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000903 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000904 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000905 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000906 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000907 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000908 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000909 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000910 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000911 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000912 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000913 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000914 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000915 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000916 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000917 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000918 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000919 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000920 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000921
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000922 // Record output varargs info.
923 OutputIsVariadic = IsVariadic;
924
Evan Chengfceb57a2006-07-15 08:45:20 +0000925 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000926 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000927 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000928 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000929 if (IsVariadic)
Evan Chengf037ca62006-08-27 08:11:28 +0000930 emitCode("SmallVector<SDOperand, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000931
Evan Cheng823b7522006-01-19 21:57:10 +0000932 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000933 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000934 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000935 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000936 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000937 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000938 }
939
Evan Cheng4326ef52006-10-12 02:08:53 +0000940 if (OrigChains.size() > 0) {
941 // The original input chain is being ignored. If it is not just
942 // pointing to the op that's being folded, we should create a
943 // TokenFactor with it and the chain of the folded op as the new chain.
944 // We could potentially be doing multiple levels of folding, in that
945 // case, the TokenFactor can have more operands.
946 emitCode("SmallVector<SDOperand, 8> InChains;");
947 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
948 emitCode("if (" + OrigChains[i].first + ".Val != " +
949 OrigChains[i].second + ".Val) {");
950 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
951 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
952 emitCode("}");
953 }
954 emitCode("AddToISelQueue(" + ChainName + ");");
955 emitCode("InChains.push_back(" + ChainName + ");");
956 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
957 "&InChains[0], InChains.size());");
958 }
959
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000960 // Loop over all of the operands of the instruction pattern, emitting code
961 // to fill them all in. The node 'N' usually has number children equal to
962 // the number of input operands of the instruction. However, in cases
963 // where there are predicate operands for an instruction, we need to fill
964 // in the 'execute always' values. Match up the node operands to the
965 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000966 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000967 for (unsigned ChildNo = 0, InstOpNo = NumResults;
968 InstOpNo != II.OperandList.size(); ++InstOpNo) {
969 std::vector<std::string> Ops;
970
Dan Gohmand35121a2008-05-29 19:57:41 +0000971 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000972 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000973 if ((OperandNode->isSubClassOf("PredicateOperand") ||
974 OperandNode->isSubClassOf("OptionalDefOperand")) &&
975 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000976 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000977 // 'default ops' operands.
978 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +0000979 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +0000980 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +0000981 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000982 InFlagDecled, ResNodeDecled);
983 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
984 }
Dan Gohmand35121a2008-05-29 19:57:41 +0000985 } else {
986 // Otherwise this is a normal operand or a predicate operand without
987 // 'execute always'; emit it.
988 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
989 InFlagDecled, ResNodeDecled);
990 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
991 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000992 }
Evan Chengb915f312005-12-09 22:45:35 +0000993 }
994
Evan Chengb915f312005-12-09 22:45:35 +0000995 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +0000996 bool ChainEmitted = NodeHasChain;
997 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +0000998 emitCode("AddToISelQueue(" + ChainName + ");");
Evan Chengbc6b86a2006-06-14 19:27:50 +0000999 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001000 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1001 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +00001002 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001003 if (!InFlagDecled) {
1004 emitCode("SDOperand InFlag(0, 0);");
1005 InFlagDecled = true;
1006 }
Evan Chengf037ca62006-08-27 08:11:28 +00001007 if (NodeHasOptInFlag) {
1008 emitCode("if (HasInFlag) {");
1009 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
1010 emitCode(" AddToISelQueue(InFlag);");
1011 emitCode("}");
1012 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001013 }
Evan Chengb915f312005-12-09 22:45:35 +00001014
Evan Chengb915f312005-12-09 22:45:35 +00001015 unsigned ResNo = TmpNo++;
Evan Cheng3eff89b2006-05-10 02:47:57 +00001016 if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag ||
Evan Chengef61ed32007-09-07 23:59:02 +00001017 NodeHasOptInFlag || HasImpResults) {
Evan Chenge945f4d2006-06-14 22:22:20 +00001018 std::string Code;
1019 std::string Code2;
1020 std::string NodeName;
1021 if (!isRoot) {
1022 NodeName = "Tmp" + utostr(ResNo);
Dan Gohmana6a1ab32007-07-24 22:58:00 +00001023 Code2 = "SDOperand " + NodeName + "(";
Evan Cheng9789aaa2006-01-24 20:46:50 +00001024 } else {
Evan Chenge945f4d2006-06-14 22:22:20 +00001025 NodeName = "ResNode";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +00001026 if (!ResNodeDecled) {
Evan Cheng676d7312006-08-26 00:59:04 +00001027 Code2 = "SDNode *" + NodeName + " = ";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +00001028 ResNodeDecled = true;
1029 } else
Evan Cheng676d7312006-08-26 00:59:04 +00001030 Code2 = NodeName + " = ";
Evan Chengbcecf332005-12-17 01:19:28 +00001031 }
Evan Chengf037ca62006-08-27 08:11:28 +00001032
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001033 Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
Evan Chengf037ca62006-08-27 08:11:28 +00001034 unsigned OpsNo = OpcNo;
Evan Chengfceb57a2006-07-15 08:45:20 +00001035 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
Evan Chenge945f4d2006-06-14 22:22:20 +00001036
1037 // Output order: results, chain, flags
1038 // Result types.
Evan Chengf8729402006-07-16 06:12:52 +00001039 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1040 Code += ", VT" + utostr(VTNo);
1041 emitVT(getEnumName(N->getTypeNum(0)));
1042 }
Evan Chengef61ed32007-09-07 23:59:02 +00001043 // Add types for implicit results in physical registers, scheduler will
1044 // care of adding copyfromreg nodes.
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001045 for (unsigned i = 0; i < NumDstRegs; i++) {
1046 Record *RR = DstRegs[i];
1047 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001048 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001049 Code += ", " + getEnumName(RVT);
Evan Chengef61ed32007-09-07 23:59:02 +00001050 }
1051 }
Evan Chenge945f4d2006-06-14 22:22:20 +00001052 if (NodeHasChain)
1053 Code += ", MVT::Other";
1054 if (NodeHasOutFlag)
1055 Code += ", MVT::Flag";
1056
1057 // Inputs.
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001058 if (IsVariadic) {
Evan Chengf037ca62006-08-27 08:11:28 +00001059 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1060 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1061 AllOps.clear();
Evan Chenge945f4d2006-06-14 22:22:20 +00001062
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001063 // Figure out whether any operands at the end of the op list are not
1064 // part of the variable section.
1065 std::string EndAdjust;
Evan Chenge945f4d2006-06-14 22:22:20 +00001066 if (NodeHasInFlag || HasImpInputs)
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001067 EndAdjust = "-1"; // Always has one flag.
1068 else if (NodeHasOptInFlag)
1069 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1070
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001071 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001072 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1073
Evan Cheng676d7312006-08-26 00:59:04 +00001074 emitCode(" AddToISelQueue(N.getOperand(i));");
Evan Chengf037ca62006-08-27 08:11:28 +00001075 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
Evan Chenge945f4d2006-06-14 22:22:20 +00001076 emitCode("}");
1077 }
1078
Dan Gohman37cdad32008-06-02 17:40:38 +00001079 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1080 // this pattern.
1081 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1082 std::vector<std::string>::const_iterator mi, mie;
1083 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
1084 emitCode("SDOperand LSI_" + *mi + " = "
1085 "CurDAG->getMemOperand(cast<LSBaseSDNode>(" +
1086 *mi + ")->getMemOperand());");
1087 if (IsVariadic)
1088 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1089 else
1090 AllOps.push_back("LSI_" + *mi);
1091 }
1092 }
1093
Evan Chenge945f4d2006-06-14 22:22:20 +00001094 if (NodeHasChain) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001095 if (IsVariadic)
Evan Chengf037ca62006-08-27 08:11:28 +00001096 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +00001097 else
Evan Chengf037ca62006-08-27 08:11:28 +00001098 AllOps.push_back(ChainName);
Evan Chenge945f4d2006-06-14 22:22:20 +00001099 }
1100
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001101 if (IsVariadic) {
Evan Chengf037ca62006-08-27 08:11:28 +00001102 if (NodeHasInFlag || HasImpInputs)
1103 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1104 else if (NodeHasOptInFlag) {
1105 emitCode("if (HasInFlag)");
1106 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1107 }
1108 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1109 ".size()";
1110 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001111 AllOps.push_back("InFlag");
Evan Chenge945f4d2006-06-14 22:22:20 +00001112
Evan Chengf037ca62006-08-27 08:11:28 +00001113 unsigned NumOps = AllOps.size();
1114 if (NumOps) {
1115 if (!NodeHasOptInFlag && NumOps < 4) {
1116 for (unsigned i = 0; i != NumOps; ++i)
1117 Code += ", " + AllOps[i];
1118 } else {
1119 std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { ";
1120 for (unsigned i = 0; i != NumOps; ++i) {
1121 OpsCode += AllOps[i];
1122 if (i != NumOps-1)
1123 OpsCode += ", ";
1124 }
1125 emitCode(OpsCode + " };");
1126 Code += ", Ops" + utostr(OpsNo) + ", ";
1127 if (NodeHasOptInFlag) {
1128 Code += "HasInFlag ? ";
1129 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1130 } else
1131 Code += utostr(NumOps);
1132 }
1133 }
1134
Evan Chenge945f4d2006-06-14 22:22:20 +00001135 if (!isRoot)
1136 Code += "), 0";
1137 emitCode(Code2 + Code + ");");
1138
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00001139 if (NodeHasChain) {
Evan Chenge945f4d2006-06-14 22:22:20 +00001140 // Remember which op produces the chain.
1141 if (!isRoot)
1142 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001143 ".Val, " + utostr(NumResults+NumDstRegs) + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +00001144 else
1145 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001146 ", " + utostr(NumResults+NumDstRegs) + ");");
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00001147 }
Evan Cheng1b80f4d2005-12-19 07:18:51 +00001148
Evan Cheng676d7312006-08-26 00:59:04 +00001149 if (!isRoot) {
1150 NodeOps.push_back("Tmp" + utostr(ResNo));
1151 return NodeOps;
1152 }
Evan Cheng045953c2006-05-10 00:05:46 +00001153
Evan Cheng06d64702006-08-11 08:59:35 +00001154 bool NeedReplace = false;
Evan Cheng676d7312006-08-26 00:59:04 +00001155 if (NodeHasOutFlag) {
1156 if (!InFlagDecled) {
Dan Gohmana6a1ab32007-07-24 22:58:00 +00001157 emitCode("SDOperand InFlag(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001158 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001159 InFlagDecled = true;
1160 } else
1161 emitCode("InFlag = SDOperand(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001162 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001163 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001164
Evan Cheng97938882005-12-22 02:24:50 +00001165 if (FoldedChains.size() > 0) {
Chris Lattner8a0604b2006-01-28 20:31:24 +00001166 std::string Code;
Evan Cheng1b80f4d2005-12-19 07:18:51 +00001167 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++)
Chris Lattner706d2d32006-08-09 16:44:44 +00001168 emitCode("ReplaceUses(SDOperand(" +
Evan Cheng67212a02006-02-09 22:12:27 +00001169 FoldedChains[j].first + ".Val, " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001170 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001171 utostr(NumResults+NumDstRegs) + "));");
Evan Cheng06d64702006-08-11 08:59:35 +00001172 NeedReplace = true;
Evan Chengb915f312005-12-09 22:45:35 +00001173 }
Evan Chengf9fc25d2005-12-19 22:40:04 +00001174
Evan Cheng06d64702006-08-11 08:59:35 +00001175 if (NodeHasOutFlag) {
Evan Chenga58891f2008-02-05 22:50:29 +00001176 if (FoldedFlag.first != "") {
1177 emitCode("ReplaceUses(SDOperand(" + FoldedFlag.first + ".Val, " +
1178 utostr(FoldedFlag.second) + "), InFlag);");
1179 } else {
1180 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
1181 emitCode("ReplaceUses(SDOperand(N.Val, " +
1182 utostr(NumPatResults + (unsigned)InputHasChain)
1183 +"), InFlag);");
1184 }
Evan Cheng06d64702006-08-11 08:59:35 +00001185 NeedReplace = true;
1186 }
1187
Evan Cheng30729b42007-09-17 22:26:41 +00001188 if (NeedReplace && InputHasChain)
1189 emitCode("ReplaceUses(SDOperand(N.Val, " +
1190 utostr(NumPatResults) + "), SDOperand(" + ChainName
1191 + ".Val, " + ChainName + ".ResNo" + "));");
Evan Cheng97938882005-12-22 02:24:50 +00001192
Evan Chenged66e852006-03-09 08:19:11 +00001193 // User does not expect the instruction would produce a chain!
Evan Cheng06d64702006-08-11 08:59:35 +00001194 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Evan Cheng9ade2182006-08-26 05:34:46 +00001195 ;
Evan Cheng3eff89b2006-05-10 02:47:57 +00001196 } else if (InputHasChain && !NodeHasChain) {
1197 // One of the inner node produces a chain.
Evan Cheng9ade2182006-08-26 05:34:46 +00001198 if (NodeHasOutFlag)
Bill Wendling27926af2008-02-26 10:45:29 +00001199 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) +
1200 "), SDOperand(ResNode, N.ResNo-1));");
1201 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) +
1202 "), " + ChainName + ");");
Evan Cheng4fba2812005-12-20 07:37:41 +00001203 }
Evan Cheng06d64702006-08-11 08:59:35 +00001204
Evan Cheng30729b42007-09-17 22:26:41 +00001205 emitCode("return ResNode;");
Evan Chengb915f312005-12-09 22:45:35 +00001206 } else {
Evan Cheng9ade2182006-08-26 05:34:46 +00001207 std::string Code = "return CurDAG->SelectNodeTo(N.Val, Opc" +
Evan Chengfceb57a2006-07-15 08:45:20 +00001208 utostr(OpcNo);
Nate Begemanb73628b2005-12-30 00:12:56 +00001209 if (N->getTypeNum(0) != MVT::isVoid)
Evan Chengf8729402006-07-16 06:12:52 +00001210 Code += ", VT" + utostr(VTNo);
Evan Cheng54597732006-01-26 00:22:25 +00001211 if (NodeHasOutFlag)
Chris Lattner8a0604b2006-01-28 20:31:24 +00001212 Code += ", MVT::Flag";
Evan Chengf037ca62006-08-27 08:11:28 +00001213
1214 if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1215 AllOps.push_back("InFlag");
1216
1217 unsigned NumOps = AllOps.size();
1218 if (NumOps) {
1219 if (!NodeHasOptInFlag && NumOps < 4) {
1220 for (unsigned i = 0; i != NumOps; ++i)
1221 Code += ", " + AllOps[i];
1222 } else {
1223 std::string OpsCode = "SDOperand Ops" + utostr(OpcNo) + "[] = { ";
1224 for (unsigned i = 0; i != NumOps; ++i) {
1225 OpsCode += AllOps[i];
1226 if (i != NumOps-1)
1227 OpsCode += ", ";
1228 }
1229 emitCode(OpsCode + " };");
1230 Code += ", Ops" + utostr(OpcNo) + ", ";
1231 Code += utostr(NumOps);
1232 }
1233 }
Evan Cheng95514ba2006-08-26 08:00:10 +00001234 emitCode(Code + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001235 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1236 if (N->getTypeNum(0) != MVT::isVoid)
1237 emitVT(getEnumName(N->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001238 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001239
Evan Cheng676d7312006-08-26 00:59:04 +00001240 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001241 } else if (Op->isSubClassOf("SDNodeXForm")) {
1242 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001243 // PatLeaf node - the operand may or may not be a leaf node. But it should
1244 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001245 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001246 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001247 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001248 unsigned ResNo = TmpNo++;
Evan Cheng676d7312006-08-26 00:59:04 +00001249 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
1250 + "(" + Ops.back() + ".Val);");
1251 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001252 if (isRoot)
1253 emitCode("return Tmp" + utostr(ResNo) + ".Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001254 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001255 } else {
1256 N->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001257 cerr << "\n";
Chris Lattner7893f132006-01-11 01:33:49 +00001258 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001259 }
1260 }
1261
Chris Lattner488580c2006-01-28 19:06:51 +00001262 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1263 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001264 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1265 /// for, this returns true otherwise false if Pat has all types.
1266 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001267 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001268 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001269 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001270 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001271 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001272 // The top level node type is checked outside of the select function.
1273 if (!isRoot)
1274 emitCheck(Prefix + ".Val->getValueType(0) == " +
1275 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001276 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001277 }
1278
Evan Cheng51fecc82006-01-09 18:27:06 +00001279 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001280 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001281 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1282 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1283 Prefix + utostr(OpNo)))
1284 return true;
1285 return false;
1286 }
1287
1288private:
Evan Cheng54597732006-01-26 00:22:25 +00001289 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001290 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001291 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001292 bool &ChainEmitted, bool &InFlagDecled,
1293 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001294 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001295 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001296 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1297 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001298 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1299 TreePatternNode *Child = N->getChild(i);
1300 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001301 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1302 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001303 } else {
1304 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001305 if (!Child->getName().empty()) {
1306 std::string Name = RootName + utostr(OpNo);
1307 if (Duplicates.find(Name) != Duplicates.end())
1308 // A duplicate! Do not emit a copy for this node.
1309 continue;
1310 }
1311
Evan Chengb915f312005-12-09 22:45:35 +00001312 Record *RR = DI->getDef();
1313 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001314 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001315 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001316 if (!InFlagDecled) {
1317 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
1318 InFlagDecled = true;
1319 } else
1320 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1321 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001322 } else {
1323 if (!ChainEmitted) {
Evan Cheng676d7312006-08-26 00:59:04 +00001324 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001325 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001326 ChainEmitted = true;
1327 }
Evan Cheng676d7312006-08-26 00:59:04 +00001328 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1329 if (!InFlagDecled) {
1330 emitCode("SDOperand InFlag(0, 0);");
1331 InFlagDecled = true;
1332 }
1333 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1334 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001335 ", " + getQualifiedName(RR) +
Evan Cheng7a33db02006-08-26 07:39:28 +00001336 ", " + RootName + utostr(OpNo) + ", InFlag).Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001337 ResNodeDecled = true;
Evan Cheng67212a02006-02-09 22:12:27 +00001338 emitCode(ChainName + " = SDOperand(ResNode, 0);");
1339 emitCode("InFlag = SDOperand(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001340 }
1341 }
1342 }
1343 }
1344 }
Evan Cheng54597732006-01-26 00:22:25 +00001345
Evan Cheng676d7312006-08-26 00:59:04 +00001346 if (HasInFlag) {
1347 if (!InFlagDecled) {
1348 emitCode("SDOperand InFlag = " + RootName +
1349 ".getOperand(" + utostr(OpNo) + ");");
1350 InFlagDecled = true;
1351 } else
1352 emitCode("InFlag = " + RootName +
1353 ".getOperand(" + utostr(OpNo) + ");");
1354 emitCode("AddToISelQueue(InFlag);");
1355 }
Evan Chengb915f312005-12-09 22:45:35 +00001356 }
1357};
1358
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001359/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1360/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001361/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001362void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001363 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001364 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001365 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001366 std::vector<std::string> &TargetVTs,
1367 bool &OutputIsVariadic,
1368 unsigned &NumInputRootOps) {
1369 OutputIsVariadic = false;
1370 NumInputRootOps = 0;
1371
Chris Lattner200c57e2008-01-05 22:58:54 +00001372 PatternCodeEmitter Emitter(CGP, Pattern.getPredicates(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001373 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001374 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001375 TargetOpcodes, TargetVTs,
1376 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001377
Chris Lattner8fc35682005-09-23 23:16:51 +00001378 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001379 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001380 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001381
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001382 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001383 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001384
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001385 // At this point, we know that we structurally match the pattern, but the
1386 // types of the nodes may not match. Figure out the fewest number of type
1387 // comparisons we need to emit. For example, if there is only one integer
1388 // type supported by a target, there should be no type comparisons at all for
1389 // integer patterns!
1390 //
1391 // To figure out the fewest number of type checks needed, clone the pattern,
1392 // remove the types, then perform type inference on the pattern as a whole.
1393 // If there are unresolved types, emit an explicit check for those types,
1394 // apply the type to the tree, then rerun type inference. Iterate until all
1395 // types are resolved.
1396 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001397 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001398 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001399
1400 do {
1401 // Resolve/propagate as many types as possible.
1402 try {
1403 bool MadeChange = true;
1404 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001405 MadeChange = Pat->ApplyTypeConstraints(TP,
1406 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001407 } catch (...) {
1408 assert(0 && "Error: could not find consistent types for something we"
1409 " already decided was ok!");
1410 abort();
1411 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001412
Chris Lattner7e82f132005-10-15 21:34:21 +00001413 // Insert a check for an unresolved type and add it to the tree. If we find
1414 // an unresolved type to add a check for, this returns true and we iterate,
1415 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001416 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001417
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001418 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001419 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001420 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001421}
1422
Chris Lattner24e00a42006-01-29 04:41:05 +00001423/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1424/// a line causes any of them to be empty, remove them and return true when
1425/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001426static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001427 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001428 &Patterns) {
1429 bool ErasedPatterns = false;
1430 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1431 Patterns[i].second.pop_back();
1432 if (Patterns[i].second.empty()) {
1433 Patterns.erase(Patterns.begin()+i);
1434 --i; --e;
1435 ErasedPatterns = true;
1436 }
1437 }
1438 return ErasedPatterns;
1439}
1440
Chris Lattner8bc74722006-01-29 04:25:26 +00001441/// EmitPatterns - Emit code for at least one pattern, but try to group common
1442/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001443void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001444 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001445 &Patterns, unsigned Indent,
1446 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001447 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001448 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001449 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001450
1451 if (Patterns.empty()) return;
1452
Chris Lattner24e00a42006-01-29 04:41:05 +00001453 // Figure out how many patterns share the next code line. Explicitly copy
1454 // FirstCodeLine so that we don't invalidate a reference when changing
1455 // Patterns.
1456 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001457 unsigned LastMatch = Patterns.size()-1;
1458 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1459 --LastMatch;
1460
1461 // If not all patterns share this line, split the list into two pieces. The
1462 // first chunk will use this line, the second chunk won't.
1463 if (LastMatch != 0) {
1464 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1465 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1466
1467 // FIXME: Emit braces?
1468 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001469 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001470 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1471 Pattern.getSrcPattern()->print(OS);
1472 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1473 Pattern.getDstPattern()->print(OS);
1474 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001475 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001476 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001477 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001478 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001479 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001480 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001481 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001482 }
Evan Cheng676d7312006-08-26 00:59:04 +00001483 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001484 OS << std::string(Indent, ' ') << "{\n";
1485 Indent += 2;
1486 }
1487 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001488 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001489 Indent -= 2;
1490 OS << std::string(Indent, ' ') << "}\n";
1491 }
1492
1493 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001494 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001495 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1496 Pattern.getSrcPattern()->print(OS);
1497 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1498 Pattern.getDstPattern()->print(OS);
1499 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001500 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001501 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001502 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001503 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001504 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001505 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001506 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001507 }
1508 EmitPatterns(Other, Indent, OS);
1509 return;
1510 }
1511
Chris Lattner24e00a42006-01-29 04:41:05 +00001512 // Remove this code from all of the patterns that share it.
1513 bool ErasedPatterns = EraseCodeLine(Patterns);
1514
Evan Cheng676d7312006-08-26 00:59:04 +00001515 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001516
1517 // Otherwise, every pattern in the list has this line. Emit it.
1518 if (!isPredicate) {
1519 // Normal code.
1520 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1521 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001522 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1523
1524 // If the next code line is another predicate, and if all of the pattern
1525 // in this group share the same next line, emit it inline now. Do this
1526 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001527 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001528 // Check that all of fhe patterns in Patterns end with the same predicate.
1529 bool AllEndWithSamePredicate = true;
1530 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1531 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1532 AllEndWithSamePredicate = false;
1533 break;
1534 }
1535 // If all of the predicates aren't the same, we can't share them.
1536 if (!AllEndWithSamePredicate) break;
1537
1538 // Otherwise we can. Emit it shared now.
1539 OS << " &&\n" << std::string(Indent+4, ' ')
1540 << Patterns.back().second.back().second;
1541 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001542 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001543
1544 OS << ") {\n";
1545 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001546 }
1547
1548 EmitPatterns(Patterns, Indent, OS);
1549
1550 if (isPredicate)
1551 OS << std::string(Indent-2, ' ') << "}\n";
1552}
1553
Chris Lattnerfe718932008-01-06 01:10:31 +00001554static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001555 return CGP.getSDNodeInfo(Op).getEnumName();
Evan Cheng892aaf82006-11-08 23:01:03 +00001556}
Chris Lattner8bc74722006-01-29 04:25:26 +00001557
Evan Cheng892aaf82006-11-08 23:01:03 +00001558static std::string getLegalCName(std::string OpName) {
1559 std::string::size_type pos = OpName.find("::");
1560 if (pos != std::string::npos)
1561 OpName.replace(pos, 2, "_");
1562 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001563}
1564
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001565void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001566 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001567
Chris Lattnerf7560ed2006-11-20 18:54:33 +00001568 // Get the namespace to insert instructions into. Make sure not to pick up
1569 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
1570 // instruction or something.
1571 std::string InstNS;
1572 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
1573 e = Target.inst_end(); i != e; ++i) {
1574 InstNS = i->second.Namespace;
1575 if (InstNS != "TargetInstrInfo")
1576 break;
1577 }
1578
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001579 if (!InstNS.empty()) InstNS += "::";
1580
Chris Lattner602f6922006-01-04 00:25:00 +00001581 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001582 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001583 // All unique target node emission functions.
1584 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001585 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001586 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001587 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001588
1589 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001590 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001591 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001592 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001593 } else {
1594 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001595 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001596 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001597 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001598 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001599 std::vector<Record*> OpNodes = CP->getRootNodes();
1600 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001601 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1602 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001603 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001604 }
1605 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001606 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001607 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001608 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001609 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001610 exit(1);
1611 }
1612 }
1613 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001614
1615 // For each opcode, there might be multiple select functions, one per
1616 // ValueType of the node (or its first operand if it doesn't produce a
1617 // non-chain result.
1618 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1619
Chris Lattner602f6922006-01-04 00:25:00 +00001620 // Emit one Select_* method for each top-level opcode. We do this instead of
1621 // emitting one giant switch statement to support compilers where this will
1622 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001623 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001624 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1625 PBOI != E; ++PBOI) {
1626 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001627 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001628 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1629
Chris Lattner602f6922006-01-04 00:25:00 +00001630 // We want to emit all of the matching code now. However, we want to emit
1631 // the matches in order of minimal cost. Sort the patterns so the least
1632 // cost one is at the start.
Chris Lattner706d2d32006-08-09 16:44:44 +00001633 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001634 PatternSortingPredicate(CGP));
Evan Cheng21ad3922006-02-07 00:37:41 +00001635
Chris Lattner706d2d32006-08-09 16:44:44 +00001636 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001637 std::map<MVT::SimpleValueType,
1638 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001639 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001640 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001641 TreePatternNode *SrcPat = Pat->getSrcPattern();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001642 MVT::SimpleValueType VT = SrcPat->getTypeNum(0);
1643 std::map<MVT::SimpleValueType,
Chris Lattner60d81392008-01-05 22:30:17 +00001644 std::vector<const PatternToMatch*> >::iterator TI =
Chris Lattner706d2d32006-08-09 16:44:44 +00001645 PatternsByType.find(VT);
1646 if (TI != PatternsByType.end())
1647 TI->second.push_back(Pat);
1648 else {
Chris Lattner60d81392008-01-05 22:30:17 +00001649 std::vector<const PatternToMatch*> PVec;
Chris Lattner706d2d32006-08-09 16:44:44 +00001650 PVec.push_back(Pat);
1651 PatternsByType.insert(std::make_pair(VT, PVec));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001652 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001653 }
1654
Duncan Sands83ec4b62008-06-06 12:08:01 +00001655 for (std::map<MVT::SimpleValueType,
1656 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001657 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1658 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001659 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001660 std::vector<const PatternToMatch*> &Patterns = II->second;
Chris Lattner64906972006-09-21 18:28:27 +00001661 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1662 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001663
Chris Lattner60d81392008-01-05 22:30:17 +00001664 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001665 std::vector<std::vector<std::string> > PatternOpcodes;
1666 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001667 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001668 std::vector<bool> OutputIsVariadicFlags;
1669 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001670 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1671 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001672 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001673 std::vector<std::string> TargetOpcodes;
1674 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001675 bool OutputIsVariadic;
1676 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001677 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001678 TargetOpcodes, TargetVTs,
1679 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001680 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1681 PatternDecls.push_back(GeneratedDecl);
1682 PatternOpcodes.push_back(TargetOpcodes);
1683 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001684 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1685 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001686 }
1687
1688 // Scan the code to see if all of the patterns are reachable and if it is
1689 // possible that the last one might not match.
1690 bool mightNotMatch = true;
1691 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1692 CodeList &GeneratedCode = CodeForPatterns[i].second;
1693 mightNotMatch = false;
1694
1695 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001696 if (GeneratedCode[j].first == 1) { // predicate.
Chris Lattner706d2d32006-08-09 16:44:44 +00001697 mightNotMatch = true;
1698 break;
1699 }
1700 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001701
Chris Lattner706d2d32006-08-09 16:44:44 +00001702 // If this pattern definitely matches, and if it isn't the last one, the
1703 // patterns after it CANNOT ever match. Error out.
1704 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001705 cerr << "Pattern '";
1706 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1707 cerr << "' is impossible to select!\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001708 exit(1);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001709 }
1710 }
1711
Chris Lattner706d2d32006-08-09 16:44:44 +00001712 // Factor target node emission code (emitted by EmitResultCode) into
1713 // separate functions. Uniquing and share them among all instruction
1714 // selection routines.
1715 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1716 CodeList &GeneratedCode = CodeForPatterns[i].second;
1717 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1718 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001719 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001720 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1721 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001722 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001723 int CodeSize = (int)GeneratedCode.size();
1724 int LastPred = -1;
1725 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001726 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001727 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001728 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1729 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001730 }
1731
Evan Cheng9ade2182006-08-26 05:34:46 +00001732 std::string CalleeCode = "(const SDOperand &N";
1733 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001734 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1735 CalleeCode += ", unsigned Opc" + utostr(j);
1736 CallerCode += ", " + TargetOpcodes[j];
1737 }
1738 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001740 CallerCode += ", " + TargetVTs[j];
1741 }
Evan Chengf5493192006-08-26 01:02:19 +00001742 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001743 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001744 std::string Name = *I;
Evan Cheng676d7312006-08-26 00:59:04 +00001745 CalleeCode += ", SDOperand &" + Name;
1746 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001747 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001748
1749 if (OutputIsVariadic) {
1750 CalleeCode += ", unsigned NumInputRootOps";
1751 CallerCode += ", " + utostr(NumInputRootOps);
1752 }
1753
Chris Lattner706d2d32006-08-09 16:44:44 +00001754 CallerCode += ");";
1755 CalleeCode += ") ";
1756 // Prevent emission routines from being inlined to reduce selection
1757 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001758 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001759 CalleeCode += "{\n";
1760
1761 for (std::vector<std::string>::const_reverse_iterator
1762 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1763 CalleeCode += " " + *I + "\n";
1764
Evan Chengf5493192006-08-26 01:02:19 +00001765 for (int j = LastPred+1; j < CodeSize; ++j)
1766 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001767 for (int j = LastPred+1; j < CodeSize; ++j)
1768 GeneratedCode.pop_back();
1769 CalleeCode += "}\n";
1770
1771 // Uniquing the emission routines.
1772 unsigned EmitFuncNum;
1773 std::map<std::string, unsigned>::iterator EFI =
1774 EmitFunctions.find(CalleeCode);
1775 if (EFI != EmitFunctions.end()) {
1776 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001777 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001778 EmitFuncNum = EmitFunctions.size();
1779 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001780 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001781 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001782
Chris Lattner706d2d32006-08-09 16:44:44 +00001783 // Replace the emission code within selection routines with calls to the
1784 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001785 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001786 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001787 }
1788
Chris Lattner706d2d32006-08-09 16:44:44 +00001789 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001790 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001791 if (OpVT == MVT::iPTR) {
1792 OpVTStr = "_iPTR";
1793 } else if (OpVT == MVT::isVoid) {
1794 // Nodes with a void result actually have a first result type of either
1795 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1796 // void to this case, we handle it specially here.
1797 } else {
1798 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1799 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001800 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1801 OpcodeVTMap.find(OpName);
1802 if (OpVTI == OpcodeVTMap.end()) {
1803 std::vector<std::string> VTSet;
1804 VTSet.push_back(OpVTStr);
1805 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1806 } else
1807 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001808
Evan Cheng892aaf82006-11-08 23:01:03 +00001809 OS << "SDNode *Select_" << getLegalCName(OpName)
Chris Lattner33a40042006-11-14 22:17:10 +00001810 << OpVTStr << "(const SDOperand &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001811
Chris Lattner706d2d32006-08-09 16:44:44 +00001812 // Loop through and reverse all of the CodeList vectors, as we will be
1813 // accessing them from their logical front, but accessing the end of a
1814 // vector is more efficient.
1815 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1816 CodeList &GeneratedCode = CodeForPatterns[i].second;
1817 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001818 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001819
1820 // Next, reverse the list of patterns itself for the same reason.
1821 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1822
1823 // Emit all of the patterns now, grouped together to share code.
1824 EmitPatterns(CodeForPatterns, 2, OS);
1825
Chris Lattner64906972006-09-21 18:28:27 +00001826 // If the last pattern has predicates (which could fail) emit code to
1827 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001828 if (mightNotMatch) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001829 OS << " cerr << \"Cannot yet select: \";\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001830 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1831 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1832 OpName != "ISD::INTRINSIC_VOID") {
Chris Lattner706d2d32006-08-09 16:44:44 +00001833 OS << " N.Val->dump(CurDAG);\n";
1834 } else {
1835 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1836 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001837 << " cerr << \"intrinsic %\"<< "
Chris Lattner706d2d32006-08-09 16:44:44 +00001838 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1839 }
Bill Wendlingf5da1332006-12-07 22:21:48 +00001840 OS << " cerr << '\\n';\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001841 << " abort();\n"
1842 << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001843 }
1844 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001845 }
Chris Lattner602f6922006-01-04 00:25:00 +00001846 }
1847
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001848 // Emit boilerplate.
Evan Cheng9ade2182006-08-26 05:34:46 +00001849 OS << "SDNode *Select_INLINEASM(SDOperand N) {\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001850 << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001851 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n"
1852
1853 << " // Ensure that the asm operands are themselves selected.\n"
1854 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1855 << " AddToISelQueue(Ops[j]);\n\n"
1856
Duncan Sands83ec4b62008-06-06 12:08:01 +00001857 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001858 << " VTs.push_back(MVT::Other);\n"
1859 << " VTs.push_back(MVT::Flag);\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001860 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
1861 "Ops.size());\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001862 << " return New.Val;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001863 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001864
1865 OS << "SDNode *Select_UNDEF(const SDOperand &N) {\n"
1866 << " return CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,\n"
1867 << " N.getValueType());\n"
1868 << "}\n\n";
1869
Jim Laskeya683f9b2007-01-26 17:29:20 +00001870 OS << "SDNode *Select_LABEL(const SDOperand &N) {\n"
1871 << " SDOperand Chain = N.getOperand(0);\n"
1872 << " SDOperand N1 = N.getOperand(1);\n"
Evan Chengbb81d972008-01-31 09:59:15 +00001873 << " SDOperand N2 = N.getOperand(2);\n"
1874 << " unsigned C1 = cast<ConstantSDNode>(N1)->getValue();\n"
1875 << " unsigned C2 = cast<ConstantSDNode>(N2)->getValue();\n"
1876 << " SDOperand Tmp1 = CurDAG->getTargetConstant(C1, MVT::i32);\n"
1877 << " SDOperand Tmp2 = CurDAG->getTargetConstant(C2, MVT::i32);\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001878 << " AddToISelQueue(Chain);\n"
Evan Chengbb81d972008-01-31 09:59:15 +00001879 << " SDOperand Ops[] = { Tmp1, Tmp2, Chain };\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001880 << " return CurDAG->getTargetNode(TargetInstrInfo::LABEL,\n"
Evan Chengbb81d972008-01-31 09:59:15 +00001881 << " MVT::Other, Ops, 3);\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001882 << "}\n\n";
1883
Evan Chenga844bde2008-02-02 04:07:54 +00001884 OS << "SDNode *Select_DECLARE(const SDOperand &N) {\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001885 << " SDOperand Chain = N.getOperand(0);\n"
1886 << " SDOperand N1 = N.getOperand(1);\n"
1887 << " SDOperand N2 = N.getOperand(2);\n"
1888 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
1889 << " cerr << \"Cannot yet select llvm.dbg.declare: \";\n"
1890 << " N.Val->dump(CurDAG);\n"
1891 << " abort();\n"
1892 << " }\n"
1893 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1894 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001895 << " SDOperand Tmp1 = "
1896 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
1897 << " SDOperand Tmp2 = "
1898 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1899 << " AddToISelQueue(Chain);\n"
1900 << " SDOperand Ops[] = { Tmp1, Tmp2, Chain };\n"
1901 << " return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,\n"
1902 << " MVT::Other, Ops, 3);\n"
1903 << "}\n\n";
1904
Christopher Lamb08d52072007-07-26 07:48:21 +00001905 OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n"
1906 << " SDOperand N0 = N.getOperand(0);\n"
1907 << " SDOperand N1 = N.getOperand(1);\n"
1908 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1909 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1910 << " AddToISelQueue(N0);\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001911 << " SDOperand Ops[] = { N0, Tmp };\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001912 << " return CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001913 << " N.getValueType(), Ops, 2);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001914 << "}\n\n";
1915
1916 OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n"
1917 << " SDOperand N0 = N.getOperand(0);\n"
1918 << " SDOperand N1 = N.getOperand(1);\n"
1919 << " SDOperand N2 = N.getOperand(2);\n"
1920 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
1921 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1922 << " AddToISelQueue(N1);\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001923 << " SDOperand Ops[] = { N0, N1, Tmp };\n"
Christopher Lamb6634e262008-03-13 05:47:01 +00001924 << " AddToISelQueue(N0);\n"
1925 << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n"
1926 << " N.getValueType(), Ops, 3);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001927 << "}\n\n";
1928
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001929 OS << "// The main instruction selector code.\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001930 << "SDNode *SelectCode(SDOperand N) {\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001931 << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n"
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001932 << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS
Evan Cheng34167212006-02-09 00:37:58 +00001933 << "INSTRUCTION_LIST_END)) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001934 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001935 << " }\n\n"
Duncan Sands83ec4b62008-06-06 12:08:01 +00001936 << " MVT::SimpleValueType NVT = N.Val->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001937 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001938 << " default: break;\n"
1939 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001940 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001941 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001942 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001943 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001944 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001945 << " case ISD::TargetConstantPool:\n"
1946 << " case ISD::TargetFrameIndex:\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001947 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001948 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001949 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001950 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001951 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001952 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001953 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001954 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001955 << " AddToISelQueue(N.getOperand(0));\n"
1956 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001957 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001958 << " }\n"
1959 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001960 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001961 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001962 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1963 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001964 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001965 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001966 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001967 << " case ISD::LABEL: return Select_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001968 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001969 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001970 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1971 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001972
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001973
Chris Lattner602f6922006-01-04 00:25:00 +00001974 // Loop over all of the case statements, emiting a call to each method we
1975 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001976 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001977 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1978 PBOI != E; ++PBOI) {
1979 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001980 // Potentially multiple versions of select for this opcode. One for each
1981 // ValueType of the node (or its first true operand if it doesn't produce a
1982 // result.
1983 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1984 OpcodeVTMap.find(OpName);
1985 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001986 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00001987 // Keep track of whether we see a pattern that has an iPtr result.
1988 bool HasPtrPattern = false;
1989 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001990
Evan Cheng425e8c72007-09-04 20:18:28 +00001991 OS << " switch (NVT) {\n";
1992 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1993 std::string &VTStr = OpVTs[i];
1994 if (VTStr.empty()) {
1995 HasDefaultPattern = true;
1996 continue;
1997 }
Chris Lattner717a6112006-11-14 21:50:27 +00001998
Evan Cheng425e8c72007-09-04 20:18:28 +00001999 // If this is a match on iPTR: don't emit it directly, we need special
2000 // code.
2001 if (VTStr == "_iPTR") {
2002 HasPtrPattern = true;
2003 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002004 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002005 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2006 << " return Select_" << getLegalCName(OpName)
2007 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002008 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002009 OS << " default:\n";
2010
2011 // If there is an iPTR result version of this pattern, emit it here.
2012 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002013 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002014 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2015 }
2016 if (HasDefaultPattern) {
2017 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2018 }
2019 OS << " break;\n";
2020 OS << " }\n";
2021 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002022 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002023 }
Chris Lattner81303322005-09-23 19:36:15 +00002024
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002025 OS << " } // end of big switch.\n\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002026 << " cerr << \"Cannot yet select: \";\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002027 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2028 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2029 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002030 << " N.Val->dump(CurDAG);\n"
2031 << " } else {\n"
2032 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2033 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002034 << " cerr << \"intrinsic %\"<< "
2035 "Intrinsic::getName((Intrinsic::ID)iid);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002036 << " }\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002037 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002038 << " abort();\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002039 << " return NULL;\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002040 << "}\n";
2041}
2042
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002043void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002044 EmitSourceFileHeader("DAG Instruction Selector for the " +
2045 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002046
Chris Lattner1f39e292005-09-14 00:09:24 +00002047 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2048 << "// *** instruction selector class. These functions are really "
2049 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002050
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002051 OS << "// Include standard, target-independent definitions and methods used\n"
2052 << "// by the instruction selector.\n";
2053 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002054
Chris Lattner443e3f92008-01-05 22:54:53 +00002055 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002056 EmitPredicateFunctions(OS);
2057
Bill Wendlingf5da1332006-12-07 22:21:48 +00002058 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002059 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002060 I != E; ++I) {
2061 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2062 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002063 DOUT << "\n";
2064 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002065
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002066 // At this point, we have full information about the 'Patterns' we need to
2067 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002068 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002069 EmitInstructionSelector(OS);
2070
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002071}