blob: 2df9e88d57c078e0704009ecffb7b10b9b82f31a [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>
Dan Gohman95d11092008-07-07 21:00:17 +000021#include <deque>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000022using namespace llvm;
23
Chris Lattnerca559d02005-09-08 21:03:01 +000024//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000025// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000026//
27
Chris Lattner6cefb772008-01-05 22:25:12 +000028/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
29/// ComplexPattern.
30static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000031 return (N->isLeaf() &&
32 dynamic_cast<DefInit*>(N->getLeafValue()) &&
33 static_cast<DefInit*>(N->getLeafValue())->getDef()->
34 isSubClassOf("ComplexPattern"));
35}
36
Chris Lattner6cefb772008-01-05 22:25:12 +000037/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
38/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000039static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000040 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000041 if (N->isLeaf() &&
42 dynamic_cast<DefInit*>(N->getLeafValue()) &&
43 static_cast<DefInit*>(N->getLeafValue())->getDef()->
44 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000045 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
46 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000047 }
48 return NULL;
49}
50
Chris Lattner05814af2005-09-28 17:57:56 +000051/// getPatternSize - Return the 'size' of this pattern. We want to match large
52/// patterns before small ones. This is used to determine the size of a
53/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000054static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000055 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
56 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000057 P->getExtTypeNum(0) == MVT::isVoid ||
58 P->getExtTypeNum(0) == MVT::Flag ||
59 P->getExtTypeNum(0) == MVT::iPTR) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000060 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000061 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000062 // If the root node is a ConstantSDNode, increases its size.
63 // e.g. (set R32:$dst, 0).
64 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000065 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000066
67 // FIXME: This is a hack to statically increase the priority of patterns
68 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
69 // Later we can allow complexity / cost for each pattern to be (optionally)
70 // specified. To get best possible pattern match we'll need to dynamically
71 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000072 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000073 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000074 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000075
76 // If this node has some predicate function that must match, it adds to the
77 // complexity of this node.
78 if (!P->getPredicateFn().empty())
79 ++Size;
80
Chris Lattner05814af2005-09-28 17:57:56 +000081 // Count children in the count if they are also nodes.
82 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
83 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000084 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000085 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000086 else if (Child->isLeaf()) {
87 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000088 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000089 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000090 Size += getPatternSize(Child, CGP);
Chris Lattner3e179802006-02-03 18:06:02 +000091 else if (!Child->getPredicateFn().empty())
92 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000093 }
Chris Lattner05814af2005-09-28 17:57:56 +000094 }
95
96 return Size;
97}
98
99/// getResultPatternCost - Compute the number of instructions for this pattern.
100/// This is a temporary hack. We should really include the instruction
101/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000102static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000103 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000104 if (P->isLeaf()) return 0;
105
Evan Chengfbad7082006-02-18 02:33:09 +0000106 unsigned Cost = 0;
107 Record *Op = P->getOperator();
108 if (Op->isSubClassOf("Instruction")) {
109 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000110 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000111 if (II.usesCustomDAGSchedInserter)
112 Cost += 10;
113 }
Chris Lattner05814af2005-09-28 17:57:56 +0000114 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000115 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000116 return Cost;
117}
118
Evan Chenge6f32032006-07-19 00:24:41 +0000119/// getResultPatternCodeSize - Compute the code size of instructions for this
120/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000121static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000122 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000123 if (P->isLeaf()) return 0;
124
125 unsigned Cost = 0;
126 Record *Op = P->getOperator();
127 if (Op->isSubClassOf("Instruction")) {
128 Cost += Op->getValueAsInt("CodeSize");
129 }
130 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000131 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000132 return Cost;
133}
134
Chris Lattner05814af2005-09-28 17:57:56 +0000135// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
136// In particular, we want to match maximal patterns first and lowest cost within
137// a particular complexity first.
138struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000139 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
140 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000141
Chris Lattner60d81392008-01-05 22:30:17 +0000142 bool operator()(const PatternToMatch *LHS,
143 const PatternToMatch *RHS) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000144 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
145 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000146 LHSSize += LHS->getAddedComplexity();
147 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000148 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
149 if (LHSSize < RHSSize) return false;
150
151 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000152 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
153 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000154 if (LHSCost < RHSCost) return true;
155 if (LHSCost > RHSCost) return false;
156
Chris Lattner6cefb772008-01-05 22:25:12 +0000157 return getResultPatternSize(LHS->getDstPattern(), CGP) <
158 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000159 }
160};
161
Nate Begeman6510b222005-12-01 04:51:06 +0000162/// getRegisterValueType - Look up and return the first ValueType of specified
163/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000164static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000165 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
166 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000167 return MVT::Other;
168}
169
Chris Lattner72fe91c2005-09-24 00:40:24 +0000170
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000171/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
172/// type information from it.
173static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000174 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000175 if (!N->isLeaf())
176 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
177 RemoveAllTypes(N->getChild(i));
178}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000179
Evan Cheng51fecc82006-01-09 18:27:06 +0000180/// NodeHasProperty - return true if TreePatternNode has the specified
181/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000182static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000183 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000184 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000185 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000186 if (CP)
187 return CP->hasProperty(Property);
188 return false;
189 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000190 Record *Operator = N->getOperator();
191 if (!Operator->isSubClassOf("SDNode")) return false;
192
Chris Lattner6cefb772008-01-05 22:25:12 +0000193 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000194}
195
Evan Cheng94b30402006-10-11 21:02:01 +0000196static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000197 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000198 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000199 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000200
201 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
202 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000203 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000204 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000205 }
206
207 return false;
208}
209
Evan Chengf9d03182008-07-03 08:39:51 +0000210static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
211 return CGP.getSDNodeInfo(Op).getEnumName();
212}
213
214static
215bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
216 bool isStore = !N->isLeaf() &&
217 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
218 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
219 return false;
220
221 bool HasChain = false;
222 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
223 TreePatternNode *Child = N->getChild(i);
224 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
225 HasChain = true;
226 break;
227 }
228 }
229 return HasChain;
230}
231
Chris Lattnerdc32f982008-01-05 22:43:57 +0000232//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000233// Node Transformation emitter implementation.
234//
235void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
236 // Walk the pattern fragments, adding them to a map, which sorts them by
237 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000238 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000239 NXsByNameTy NXsByName;
240
Chris Lattnerfe718932008-01-06 01:10:31 +0000241 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000242 I != E; ++I)
243 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
244
245 OS << "\n// Node transformations.\n";
246
247 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
248 I != E; ++I) {
249 Record *SDNode = I->second.first;
250 std::string Code = I->second.second;
251
252 if (Code.empty()) continue; // Empty code? Skip it.
253
Chris Lattner200c57e2008-01-05 22:58:54 +0000254 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000255 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
256
257 OS << "inline SDOperand Transform_" << I->first << "(SDNode *" << C2
258 << ") {\n";
259 if (ClassName != "SDNode")
260 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
261 OS << Code << "\n}\n";
262 }
263}
264
265//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000266// Predicate emitter implementation.
267//
268
269void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
270 OS << "\n// Predicate functions.\n";
271
272 // Walk the pattern fragments, adding them to a map, which sorts them by
273 // name.
274 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
275 PFsByNameTy PFsByName;
276
Chris Lattnerfe718932008-01-06 01:10:31 +0000277 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000278 I != E; ++I)
279 PFsByName.insert(std::make_pair(I->first->getName(), *I));
280
281
282 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
283 I != E; ++I) {
284 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
285 TreePattern *P = I->second.second;
286
287 // If there is a code init for this fragment, emit the predicate code.
288 std::string Code = PatFragRecord->getValueAsCode("Predicate");
289 if (Code.empty()) continue;
290
291 if (P->getOnlyTree()->isLeaf())
292 OS << "inline bool Predicate_" << PatFragRecord->getName()
293 << "(SDNode *N) {\n";
294 else {
295 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000296 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000297 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
298
299 OS << "inline bool Predicate_" << PatFragRecord->getName()
300 << "(SDNode *" << C2 << ") {\n";
301 if (ClassName != "SDNode")
302 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
303 }
304 OS << Code << "\n}\n";
305 }
306
307 OS << "\n\n";
308}
309
310
311//===----------------------------------------------------------------------===//
312// PatternCodeEmitter implementation.
313//
Evan Chengb915f312005-12-09 22:45:35 +0000314class PatternCodeEmitter {
315private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000316 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000317
Evan Cheng58e84a62005-12-14 22:02:59 +0000318 // Predicates.
319 ListInit *Predicates;
Evan Cheng59413202006-04-19 18:07:24 +0000320 // Pattern cost.
321 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000322 // Instruction selector pattern.
323 TreePatternNode *Pattern;
324 // Matched instruction.
325 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000326
Evan Chengb915f312005-12-09 22:45:35 +0000327 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000328 std::map<std::string, std::string> VariableMap;
329 // Node to operator mapping
330 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000331 // Name of the folded node which produces a flag.
332 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000333 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000334 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000335 // Original input chain(s).
336 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000337 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000338
Dan Gohman69de1932008-02-06 22:27:42 +0000339 /// LSI - Load/Store information.
340 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
341 /// for each memory access. This facilitates the use of AliasAnalysis in
342 /// the backend.
343 std::vector<std::string> LSI;
344
Evan Cheng676d7312006-08-26 00:59:04 +0000345 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000346 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000347 /// tested, and if true, the match fails) [when 1], or normal code to emit
348 /// [when 0], or initialization code to emit [when 2].
349 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Evan Cheng21ad3922006-02-07 00:37:41 +0000350 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
351 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000352 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000353 /// TargetOpcodes - The target specific opcodes used by the resulting
354 /// instructions.
355 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000356 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000357 /// OutputIsVariadic - Records whether the instruction output pattern uses
358 /// variable_ops. This requires that the Emit function be passed an
359 /// additional argument to indicate where the input varargs operands
360 /// begin.
361 bool &OutputIsVariadic;
362 /// NumInputRootOps - Records the number of operands the root node of the
363 /// input pattern has. This information is used in the generated code to
364 /// pass to Emit functions when variable_ops processing is needed.
365 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000366
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000367 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000368 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000369 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000370 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000371
372 void emitCheck(const std::string &S) {
373 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000374 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000375 }
376 void emitCode(const std::string &S) {
377 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000378 GeneratedCode.push_back(std::make_pair(0, S));
379 }
380 void emitInit(const std::string &S) {
381 if (!S.empty())
382 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000383 }
Evan Chengf5493192006-08-26 01:02:19 +0000384 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000385 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000386 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000387 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000388 void emitOpcode(const std::string &Opc) {
389 TargetOpcodes.push_back(Opc);
390 OpcNo++;
391 }
Evan Chengf8729402006-07-16 06:12:52 +0000392 void emitVT(const std::string &VT) {
393 TargetVTs.push_back(VT);
394 VTNo++;
395 }
Evan Chengb915f312005-12-09 22:45:35 +0000396public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000397 PatternCodeEmitter(CodeGenDAGPatterns &cgp, ListInit *preds,
Evan Cheng58e84a62005-12-14 22:02:59 +0000398 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000399 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000400 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000401 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000402 std::vector<std::string> &tv,
403 bool &oiv,
404 unsigned &niro)
Chris Lattner6cefb772008-01-05 22:25:12 +0000405 : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000406 GeneratedCode(gc), GeneratedDecl(gd),
407 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000408 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000409 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000410
411 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
412 /// if the match fails. At this point, we already know that the opcode for N
413 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000414 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
415 const std::string &RootName, const std::string &ChainSuffix,
416 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000417
418 // Save loads/stores matched by a pattern.
419 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000420 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000421 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000422 }
423
Evan Chenge41bf822006-02-05 06:43:12 +0000424 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000425 // Emit instruction predicates. Each predicate is just a string for now.
426 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000427 // Record input varargs info.
428 NumInputRootOps = N->getNumChildren();
429
Evan Chengf9d03182008-07-03 08:39:51 +0000430 if (DisablePatternForFastISel(N, CGP))
431 emitCheck("!FastISel");
432
Chris Lattner8a0604b2006-01-28 20:31:24 +0000433 std::string PredicateCheck;
Evan Cheng58e84a62005-12-14 22:02:59 +0000434 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
435 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
436 Record *Def = Pred->getDef();
Chris Lattner8a0604b2006-01-28 20:31:24 +0000437 if (!Def->isSubClassOf("Predicate")) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000438#ifndef NDEBUG
439 Def->dump();
440#endif
Evan Cheng58e84a62005-12-14 22:02:59 +0000441 assert(0 && "Unknown predicate type!");
442 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000443 if (!PredicateCheck.empty())
Chris Lattnerbc7fa522006-09-19 00:41:36 +0000444 PredicateCheck += " && ";
Chris Lattner67a202b2006-01-28 20:43:52 +0000445 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
Evan Cheng58e84a62005-12-14 22:02:59 +0000446 }
447 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000448
449 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000450 }
451
Evan Chengb915f312005-12-09 22:45:35 +0000452 if (N->isLeaf()) {
453 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000454 emitCheck("cast<ConstantSDNode>(" + RootName +
Chris Lattner67a202b2006-01-28 20:43:52 +0000455 ")->getSignExtended() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000456 return;
457 } else if (!NodeIsComplexPattern(N)) {
458 assert(0 && "Cannot match this as a leaf value!");
459 abort();
460 }
461 }
462
Chris Lattner488580c2006-01-28 19:06:51 +0000463 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000464 // we already saw this in the pattern, emit code to verify dagness.
465 if (!N->getName().empty()) {
466 std::string &VarMapEntry = VariableMap[N->getName()];
467 if (VarMapEntry.empty()) {
468 VarMapEntry = RootName;
469 } else {
470 // If we get here, this is a second reference to a specific name. Since
471 // we already have checked that the first reference is valid, we don't
472 // have to recursively match it, just check that it's the same as the
473 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000474 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000475 return;
476 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000477
478 if (!N->isLeaf())
479 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000480 }
481
482
483 // Emit code to load the child nodes and match their contents recursively.
484 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000485 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
486 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000487 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000488 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000489 if (NodeHasChain)
490 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000491 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000492 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000493 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000494 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000495 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000496 // If the immediate use can somehow reach this node through another
497 // path, then can't fold it either or it will create a cycle.
498 // e.g. In the following diagram, XX can reach ld through YY. If
499 // ld is folded into XX, then YY is both a predecessor and a successor
500 // of XX.
501 //
502 // [ld]
503 // ^ ^
504 // | |
505 // / \---
506 // / [YY]
507 // | ^
508 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000509 bool NeedCheck = P != Pattern;
510 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000511 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000512 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000513 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
514 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
515 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000516 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000517 PInfo.hasProperty(SDNPHasChain) ||
518 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000519 PInfo.hasProperty(SDNPOptInFlag);
520 }
521
522 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000523 std::string ParentName(RootName.begin(), RootName.end()-1);
Chris Lattner706d2d32006-08-09 16:44:44 +0000524 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
Evan Chengce1381a2006-10-14 08:30:15 +0000525 ".Val, N.Val)");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000526 }
Evan Chenge41bf822006-02-05 06:43:12 +0000527 }
Evan Chengb915f312005-12-09 22:45:35 +0000528 }
Evan Chenge41bf822006-02-05 06:43:12 +0000529
Evan Chengc15d18c2006-01-27 22:13:45 +0000530 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000531 if (FoundChain) {
532 emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || "
533 "IsChainCompatible(" + ChainName + ".Val, " +
534 RootName + ".Val))");
535 OrigChains.push_back(std::make_pair(ChainName, RootName));
536 } else
Evan Chenge6389932006-07-21 22:19:51 +0000537 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000538 ChainName = "Chain" + ChainSuffix;
Evan Cheng676d7312006-08-26 00:59:04 +0000539 emitInit("SDOperand " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000540 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000541 }
Evan Chengb915f312005-12-09 22:45:35 +0000542 }
543
Evan Cheng54597732006-01-26 00:22:25 +0000544 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000545 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000546 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000547 // FIXME: If the optional incoming flag does not exist. Then it is ok to
548 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000549 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000550 (PatternHasProperty(N, SDNPInFlag, CGP) ||
551 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
552 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000553 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000554 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000555 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000556 }
557 }
558
Evan Chengd3eea902006-10-09 21:02:17 +0000559 // If there is a node predicate for this, emit the call.
560 if (!N->getPredicateFn().empty())
561 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
562
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000563
Chris Lattner39e73f72006-10-11 04:05:55 +0000564 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
565 // a constant without a predicate fn that has more that one bit set, handle
566 // this as a special case. This is usually for targets that have special
567 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
568 // handling stuff). Using these instructions is often far more efficient
569 // than materializing the constant. Unfortunately, both the instcombiner
570 // and the dag combiner can often infer that bits are dead, and thus drop
571 // them from the mask in the dag. For example, it might turn 'AND X, 255'
572 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
573 // to handle this.
574 if (!N->isLeaf() &&
575 (N->getOperator()->getName() == "and" ||
576 N->getOperator()->getName() == "or") &&
577 N->getChild(1)->isLeaf() &&
578 N->getChild(1)->getPredicateFn().empty()) {
579 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
580 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
581 emitInit("SDOperand " + RootName + "0" + " = " +
582 RootName + ".getOperand(" + utostr(0) + ");");
583 emitInit("SDOperand " + RootName + "1" + " = " +
584 RootName + ".getOperand(" + utostr(1) + ");");
585
586 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
587 const char *MaskPredicate = N->getOperator()->getName() == "or"
588 ? "CheckOrMask(" : "CheckAndMask(";
589 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
590 RootName + "1), " + itostr(II->getValue()) + ")");
591
Christopher Lamb85356242008-01-31 07:27:46 +0000592 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000593 ChainSuffix + utostr(0), FoundChain);
594 return;
595 }
596 }
597 }
598
Evan Chengb915f312005-12-09 22:45:35 +0000599 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Evan Cheng676d7312006-08-26 00:59:04 +0000600 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000601 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000602
Christopher Lamb85356242008-01-31 07:27:46 +0000603 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000604 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000605 }
606
Evan Cheng676d7312006-08-26 00:59:04 +0000607 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000608 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000609 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000610 std::string Fn = CP->getSelectFunc();
611 unsigned NumOps = CP->getNumOperands();
612 for (unsigned i = 0; i < NumOps; ++i) {
613 emitDecl("CPTmp" + utostr(i));
614 emitCode("SDOperand CPTmp" + utostr(i) + ";");
615 }
Evan Cheng94b30402006-10-11 21:02:01 +0000616 if (CP->hasProperty(SDNPHasChain)) {
617 emitDecl("CPInChain");
618 emitDecl("Chain" + ChainSuffix);
619 emitCode("SDOperand CPInChain;");
620 emitCode("SDOperand Chain" + ChainSuffix + ";");
621 }
Evan Cheng676d7312006-08-26 00:59:04 +0000622
Evan Cheng811731e2006-11-08 20:31:10 +0000623 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000624 for (unsigned i = 0; i < NumOps; i++)
625 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000626 if (CP->hasProperty(SDNPHasChain)) {
627 ChainName = "Chain" + ChainSuffix;
628 Code += ", CPInChain, Chain" + ChainSuffix;
629 }
Evan Cheng676d7312006-08-26 00:59:04 +0000630 emitCheck(Code + ")");
631 }
Evan Chengb915f312005-12-09 22:45:35 +0000632 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000633
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000634 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000635 const std::string &RootName,
636 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000637 const std::string &ChainSuffix, bool &FoundChain) {
638 if (!Child->isLeaf()) {
639 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000640 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000641 emitCheck(RootName + ".getOpcode() == " +
642 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000643 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000644 bool HasChain = false;
645 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
646 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000647 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000648 }
649 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
650 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
651 "Pattern folded multiple nodes which produce flags?");
652 FoldedFlag = std::make_pair(RootName,
653 CInfo.getNumResults() + (unsigned)HasChain);
654 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000655 } else {
656 // If this child has a name associated with it, capture it in VarMap. If
657 // we already saw this in the pattern, emit code to verify dagness.
658 if (!Child->getName().empty()) {
659 std::string &VarMapEntry = VariableMap[Child->getName()];
660 if (VarMapEntry.empty()) {
661 VarMapEntry = RootName;
662 } else {
663 // If we get here, this is a second reference to a specific name.
664 // Since we already have checked that the first reference is valid,
665 // we don't have to recursively match it, just check that it's the
666 // same as the previously named thing.
667 emitCheck(VarMapEntry + " == " + RootName);
668 Duplicates.insert(RootName);
669 return;
670 }
671 }
672
673 // Handle leaves of various types.
674 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
675 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000676 if (LeafRec->isSubClassOf("RegisterClass") ||
677 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000678 // Handle register references. Nothing to do here.
679 } else if (LeafRec->isSubClassOf("Register")) {
680 // Handle register references.
681 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
682 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000683 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000684 std::string Fn = CP->getSelectFunc();
685 unsigned NumOps = CP->getNumOperands();
686 for (unsigned i = 0; i < NumOps; ++i) {
687 emitDecl("CPTmp" + utostr(i));
688 emitCode("SDOperand CPTmp" + utostr(i) + ";");
689 }
Evan Cheng94b30402006-10-11 21:02:01 +0000690 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000691 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000692 FoldedChains.push_back(std::make_pair("CPInChain",
693 PInfo.getNumResults()));
694 ChainName = "Chain" + ChainSuffix;
695 emitDecl("CPInChain");
696 emitDecl(ChainName);
697 emitCode("SDOperand CPInChain;");
698 emitCode("SDOperand " + ChainName + ";");
699 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000700
Christopher Lamb85356242008-01-31 07:27:46 +0000701 std::string Code = Fn + "(";
702 if (CP->hasAttribute(CPAttrParentAsRoot)) {
703 Code += ParentRootName + ", ";
704 } else {
705 Code += "N, ";
706 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000707 if (CP->hasProperty(SDNPHasChain)) {
708 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000709 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000710 }
711 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000712 for (unsigned i = 0; i < NumOps; i++)
713 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000714 if (CP->hasProperty(SDNPHasChain))
715 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000716 emitCheck(Code + ")");
717 } else if (LeafRec->getName() == "srcvalue") {
718 // Place holder for SRCVALUE nodes. Nothing to do here.
719 } else if (LeafRec->isSubClassOf("ValueType")) {
720 // Make sure this is the specified value type.
721 emitCheck("cast<VTSDNode>(" + RootName +
722 ")->getVT() == MVT::" + LeafRec->getName());
723 } else if (LeafRec->isSubClassOf("CondCode")) {
724 // Make sure this is the specified cond code.
725 emitCheck("cast<CondCodeSDNode>(" + RootName +
726 ")->get() == ISD::" + LeafRec->getName());
727 } else {
728#ifndef NDEBUG
729 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000730 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000731#endif
732 assert(0 && "Unknown leaf type!");
733 }
734
735 // If there is a node predicate for this, emit the call.
736 if (!Child->getPredicateFn().empty())
737 emitCheck(Child->getPredicateFn() + "(" + RootName +
738 ".Val)");
739 } else if (IntInit *II =
740 dynamic_cast<IntInit*>(Child->getLeafValue())) {
741 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
742 unsigned CTmp = TmpNo++;
743 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
744 RootName + ")->getSignExtended();");
745
746 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
747 } else {
748#ifndef NDEBUG
749 Child->dump();
750#endif
751 assert(0 && "Unknown leaf type!");
752 }
753 }
754 }
Evan Chengb915f312005-12-09 22:45:35 +0000755
756 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
757 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000758 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000759 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000760 bool InFlagDecled, bool ResNodeDecled,
761 bool LikeLeaf = false, bool isRoot = false) {
762 // List of arguments of getTargetNode() or SelectNodeTo().
763 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000764 // This is something selected from the pattern we matched.
765 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000766 const std::string &VarName = N->getName();
767 std::string Val = VariableMap[VarName];
768 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000769 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000770 cerr << "Variable '" << VarName << " referenced but not defined "
771 << "and not caught earlier!\n";
772 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000773 }
Evan Chengb915f312005-12-09 22:45:35 +0000774 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
775 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000776 NodeOps.push_back(Val);
777 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000778 }
779
780 const ComplexPattern *CP;
781 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000782 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000783 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000784 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000785 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000786 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000787 default:
788 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
789 << " type as an immediate constant. Aborting\n";
790 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000791 case MVT::i1: CastType = "bool"; break;
792 case MVT::i8: CastType = "unsigned char"; break;
793 case MVT::i16: CastType = "unsigned short"; break;
794 case MVT::i32: CastType = "unsigned"; break;
795 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000796 }
Scott Michel6be48d42008-01-29 02:29:31 +0000797 emitCode("SDOperand " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000798 " = CurDAG->getTargetConstant(((" + CastType +
799 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
800 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000801 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
802 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000803 Val = TmpVar;
804 ModifiedVal = true;
805 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000806 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
807 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
808 std::string TmpVar = "Tmp" + utostr(ResNo);
809 emitCode("SDOperand " + TmpVar +
810 " = CurDAG->getTargetConstantFP(cast<ConstantFPSDNode>(" +
811 Val + ")->getValueAPF(), cast<ConstantFPSDNode>(" + Val +
812 ")->getValueType(0));");
813 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
814 // value if used multiple times by this pattern result.
815 Val = TmpVar;
816 ModifiedVal = true;
817 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000818 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000819 Record *Op = OperatorMap[N->getName()];
820 // Transform ExternalSymbol to TargetExternalSymbol
821 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000822 std::string TmpVar = "Tmp"+utostr(ResNo);
823 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000824 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000825 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000826 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000827 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
828 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000829 Val = TmpVar;
830 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000831 }
Scott Michel6be48d42008-01-29 02:29:31 +0000832 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000833 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
834 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000835 Record *Op = OperatorMap[N->getName()];
836 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000837 if (Op && (Op->getName() == "globaladdr" ||
838 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000839 std::string TmpVar = "Tmp" + utostr(ResNo);
840 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000841 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000842 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000843 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000844 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
845 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000846 Val = TmpVar;
847 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000848 }
Evan Cheng676d7312006-08-26 00:59:04 +0000849 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000850 } else if (!N->isLeaf()
851 && (N->getOperator()->getName() == "texternalsym"
852 || N->getOperator()->getName() == "tconstpool")) {
853 // Do not rewrite the variable name, since we don't generate a new
854 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000855 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000856 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000857 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
858 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
859 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000860 }
Evan Chengb915f312005-12-09 22:45:35 +0000861 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000862 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000863 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000864 if (!LikeLeaf) {
865 emitCode("AddToISelQueue(" + Val + ");");
Chris Lattner706d2d32006-08-09 16:44:44 +0000866 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000867 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000868 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000869 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000870 }
Evan Cheng676d7312006-08-26 00:59:04 +0000871 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000872 }
Scott Michel6be48d42008-01-29 02:29:31 +0000873
874 if (ModifiedVal) {
875 VariableMap[VarName] = Val;
876 }
Evan Cheng676d7312006-08-26 00:59:04 +0000877 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000878 }
Evan Chengb915f312005-12-09 22:45:35 +0000879 if (N->isLeaf()) {
880 // If this is an explicit register reference, handle it.
881 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
882 unsigned ResNo = TmpNo++;
883 if (DI->getDef()->isSubClassOf("Register")) {
Evan Cheng676d7312006-08-26 00:59:04 +0000884 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000885 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000886 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000887 NodeOps.push_back("Tmp" + utostr(ResNo));
888 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000889 } else if (DI->getDef()->getName() == "zero_reg") {
890 emitCode("SDOperand Tmp" + utostr(ResNo) +
891 " = CurDAG->getRegister(0, " +
892 getEnumName(N->getTypeNum(0)) + ");");
893 NodeOps.push_back("Tmp" + utostr(ResNo));
894 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000895 }
896 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
897 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000898 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Evan Cheng676d7312006-08-26 00:59:04 +0000899 emitCode("SDOperand Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000900 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
901 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000902 NodeOps.push_back("Tmp" + utostr(ResNo));
903 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000904 }
905
Jim Laskey16d42c62006-07-11 18:25:13 +0000906#ifndef NDEBUG
907 N->dump();
908#endif
Evan Chengb915f312005-12-09 22:45:35 +0000909 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000910 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000911 }
912
913 Record *Op = N->getOperator();
914 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000915 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000916 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000917 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000918 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000919 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000920 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000921 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
922 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000923 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000924 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000925 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000926 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000927 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000928 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000929 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000930 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000931 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000932 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000933 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000934 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000935 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000936 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000937 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000938 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000939 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000940 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000941 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000942
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000943 // Record output varargs info.
944 OutputIsVariadic = IsVariadic;
945
Evan Chengfceb57a2006-07-15 08:45:20 +0000946 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000947 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000948 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000949 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000950 if (IsVariadic)
Evan Chengf037ca62006-08-27 08:11:28 +0000951 emitCode("SmallVector<SDOperand, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000952
Evan Cheng823b7522006-01-19 21:57:10 +0000953 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000954 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000955 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000956 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000957 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000958 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000959 }
960
Evan Cheng4326ef52006-10-12 02:08:53 +0000961 if (OrigChains.size() > 0) {
962 // The original input chain is being ignored. If it is not just
963 // pointing to the op that's being folded, we should create a
964 // TokenFactor with it and the chain of the folded op as the new chain.
965 // We could potentially be doing multiple levels of folding, in that
966 // case, the TokenFactor can have more operands.
967 emitCode("SmallVector<SDOperand, 8> InChains;");
968 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
969 emitCode("if (" + OrigChains[i].first + ".Val != " +
970 OrigChains[i].second + ".Val) {");
971 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
972 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
973 emitCode("}");
974 }
975 emitCode("AddToISelQueue(" + ChainName + ");");
976 emitCode("InChains.push_back(" + ChainName + ");");
977 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
978 "&InChains[0], InChains.size());");
979 }
980
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000981 // Loop over all of the operands of the instruction pattern, emitting code
982 // to fill them all in. The node 'N' usually has number children equal to
983 // the number of input operands of the instruction. However, in cases
984 // where there are predicate operands for an instruction, we need to fill
985 // in the 'execute always' values. Match up the node operands to the
986 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000987 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000988 for (unsigned ChildNo = 0, InstOpNo = NumResults;
989 InstOpNo != II.OperandList.size(); ++InstOpNo) {
990 std::vector<std::string> Ops;
991
Dan Gohmand35121a2008-05-29 19:57:41 +0000992 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000993 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000994 if ((OperandNode->isSubClassOf("PredicateOperand") ||
995 OperandNode->isSubClassOf("OptionalDefOperand")) &&
996 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000997 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000998 // 'default ops' operands.
999 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001000 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001001 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001002 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001003 InFlagDecled, ResNodeDecled);
1004 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1005 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001006 } else {
1007 // Otherwise this is a normal operand or a predicate operand without
1008 // 'execute always'; emit it.
1009 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1010 InFlagDecled, ResNodeDecled);
1011 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1012 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001013 }
Evan Chengb915f312005-12-09 22:45:35 +00001014 }
1015
Evan Chengb915f312005-12-09 22:45:35 +00001016 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001017 bool ChainEmitted = NodeHasChain;
1018 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +00001019 emitCode("AddToISelQueue(" + ChainName + ");");
Evan Chengbc6b86a2006-06-14 19:27:50 +00001020 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001021 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1022 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +00001023 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001024 if (!InFlagDecled) {
1025 emitCode("SDOperand InFlag(0, 0);");
1026 InFlagDecled = true;
1027 }
Evan Chengf037ca62006-08-27 08:11:28 +00001028 if (NodeHasOptInFlag) {
1029 emitCode("if (HasInFlag) {");
1030 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
1031 emitCode(" AddToISelQueue(InFlag);");
1032 emitCode("}");
1033 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001034 }
Evan Chengb915f312005-12-09 22:45:35 +00001035
Evan Chengb915f312005-12-09 22:45:35 +00001036 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001037
Dan Gohman95d11092008-07-07 21:00:17 +00001038 unsigned OpsNo = OpcNo;
1039 std::string CodePrefix;
1040 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1041 std::deque<std::string> After;
1042 std::string NodeName;
1043 if (!isRoot) {
1044 NodeName = "Tmp" + utostr(ResNo);
1045 CodePrefix = "SDOperand " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001046 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001047 NodeName = "ResNode";
1048 if (!ResNodeDecled) {
1049 CodePrefix = "SDNode *" + NodeName + " = ";
1050 ResNodeDecled = true;
1051 } else
1052 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001053 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001054
Dan Gohman95d11092008-07-07 21:00:17 +00001055 std::string Code = "Opc" + utostr(OpcNo);
1056
1057 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1058
1059 // Output order: results, chain, flags
1060 // Result types.
1061 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1062 Code += ", VT" + utostr(VTNo);
1063 emitVT(getEnumName(N->getTypeNum(0)));
1064 }
1065 // Add types for implicit results in physical registers, scheduler will
1066 // care of adding copyfromreg nodes.
1067 for (unsigned i = 0; i < NumDstRegs; i++) {
1068 Record *RR = DstRegs[i];
1069 if (RR->isSubClassOf("Register")) {
1070 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
1071 Code += ", " + getEnumName(RVT);
1072 }
1073 }
1074 if (NodeHasChain)
1075 Code += ", MVT::Other";
1076 if (NodeHasOutFlag)
1077 Code += ", MVT::Flag";
1078
1079 // Inputs.
1080 if (IsVariadic) {
1081 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1082 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1083 AllOps.clear();
1084
1085 // Figure out whether any operands at the end of the op list are not
1086 // part of the variable section.
1087 std::string EndAdjust;
1088 if (NodeHasInFlag || HasImpInputs)
1089 EndAdjust = "-1"; // Always has one flag.
1090 else if (NodeHasOptInFlag)
1091 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1092
1093 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1094 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1095
1096 emitCode(" AddToISelQueue(N.getOperand(i));");
1097 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1098 emitCode("}");
1099 }
1100
1101 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1102 // this pattern.
1103 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1104 std::vector<std::string>::const_iterator mi, mie;
1105 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
1106 emitCode("SDOperand LSI_" + *mi + " = "
1107 "CurDAG->getMemOperand(cast<MemSDNode>(" +
1108 *mi + ")->getMemOperand());");
1109 if (IsVariadic)
1110 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1111 else
1112 AllOps.push_back("LSI_" + *mi);
1113 }
1114 }
1115
1116 if (NodeHasChain) {
1117 if (IsVariadic)
1118 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1119 else
1120 AllOps.push_back(ChainName);
1121 }
1122
1123 if (IsVariadic) {
1124 if (NodeHasInFlag || HasImpInputs)
1125 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1126 else if (NodeHasOptInFlag) {
1127 emitCode("if (HasInFlag)");
1128 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1129 }
1130 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1131 ".size()";
1132 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1133 AllOps.push_back("InFlag");
1134
1135 unsigned NumOps = AllOps.size();
1136 if (NumOps) {
1137 if (!NodeHasOptInFlag && NumOps < 4) {
1138 for (unsigned i = 0; i != NumOps; ++i)
1139 Code += ", " + AllOps[i];
1140 } else {
1141 std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { ";
1142 for (unsigned i = 0; i != NumOps; ++i) {
1143 OpsCode += AllOps[i];
1144 if (i != NumOps-1)
1145 OpsCode += ", ";
1146 }
1147 emitCode(OpsCode + " };");
1148 Code += ", Ops" + utostr(OpsNo) + ", ";
1149 if (NodeHasOptInFlag) {
1150 Code += "HasInFlag ? ";
1151 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1152 } else
1153 Code += utostr(NumOps);
1154 }
1155 }
1156
1157 if (!isRoot)
1158 Code += "), 0";
1159
Dan Gohmane8be6c62008-07-17 19:10:17 +00001160 std::vector<std::string> ReplaceFroms;
1161 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001162 if (!isRoot) {
1163 NodeOps.push_back("Tmp" + utostr(ResNo));
1164 } else {
1165
1166 if (NodeHasOutFlag) {
1167 if (!InFlagDecled) {
1168 After.push_back("SDOperand InFlag(ResNode, " +
1169 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1170 ");");
1171 InFlagDecled = true;
1172 } else
1173 After.push_back("InFlag = SDOperand(ResNode, " +
1174 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1175 ");");
1176 }
1177
1178 if (FoldedChains.size() > 0) {
1179 std::string Code;
Dan Gohmane8be6c62008-07-17 19:10:17 +00001180 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
1181 ReplaceFroms.push_back("SDOperand(" +
1182 FoldedChains[j].first + ".Val, " +
1183 utostr(FoldedChains[j].second) +
1184 ")");
1185 ReplaceTos.push_back("SDOperand(ResNode, " +
1186 utostr(NumResults+NumDstRegs) + ")");
1187 }
Dan Gohman95d11092008-07-07 21:00:17 +00001188 }
1189
1190 if (NodeHasOutFlag) {
1191 if (FoldedFlag.first != "") {
Dan Gohmane8be6c62008-07-17 19:10:17 +00001192 ReplaceFroms.push_back("SDOperand(" + FoldedFlag.first + ".Val, " +
1193 utostr(FoldedFlag.second) + ")");
1194 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001195 } else {
1196 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Dan Gohmane8be6c62008-07-17 19:10:17 +00001197 ReplaceFroms.push_back("SDOperand(N.Val, " +
1198 utostr(NumPatResults + (unsigned)InputHasChain)
1199 + ")");
1200 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001201 }
Dan Gohman95d11092008-07-07 21:00:17 +00001202 }
1203
Dan Gohmane8be6c62008-07-17 19:10:17 +00001204 if (!ReplaceFroms.empty() && InputHasChain) {
1205 ReplaceFroms.push_back("SDOperand(N.Val, " +
1206 utostr(NumPatResults) + ")");
1207 ReplaceTos.push_back("SDOperand(" + ChainName + ".Val, " +
1208 ChainName + ".ResNo" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001209 ChainAssignmentNeeded |= NodeHasChain;
1210 }
1211
1212 // User does not expect the instruction would produce a chain!
1213 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
1214 ;
1215 } else if (InputHasChain && !NodeHasChain) {
1216 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001217 if (NodeHasOutFlag) {
1218 ReplaceFroms.push_back("SDOperand(N.Val, " +
1219 utostr(NumPatResults+1) +
1220 ")");
1221 ReplaceTos.push_back("SDOperand(ResNode, N.ResNo-1)");
1222 }
1223 ReplaceFroms.push_back("SDOperand(N.Val, " +
1224 utostr(NumPatResults) + ")");
1225 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001226 }
1227 }
1228
1229 if (ChainAssignmentNeeded) {
1230 // Remember which op produces the chain.
1231 std::string ChainAssign;
1232 if (!isRoot)
1233 ChainAssign = ChainName + " = SDOperand(" + NodeName +
1234 ".Val, " + utostr(NumResults+NumDstRegs) + ");";
1235 else
1236 ChainAssign = ChainName + " = SDOperand(" + NodeName +
1237 ", " + utostr(NumResults+NumDstRegs) + ");";
1238
1239 After.push_front(ChainAssign);
1240 }
1241
Dan Gohmane8be6c62008-07-17 19:10:17 +00001242 if (ReplaceFroms.size() == 1) {
1243 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1244 ReplaceTos[0] + ");");
1245 } else if (!ReplaceFroms.empty()) {
1246 After.push_back("const SDOperand Froms[] = {");
1247 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1248 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1249 After.push_back("};");
1250 After.push_back("const SDOperand Tos[] = {");
1251 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1252 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1253 After.push_back("};");
1254 After.push_back("ReplaceUses(Froms, Tos, " +
1255 itostr(ReplaceFroms.size()) + ");");
1256 }
1257
1258 // We prefer to use SelectNodeTo since it avoids allocation when
1259 // possible and it avoids CSE map recalculation for the node's
1260 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001261 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001262 // We also don't use if the pattern replacement is being used to
1263 // jettison a chain result, since morphing the node in place
1264 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001265 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001266 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001267 Code = "CurDAG->getTargetNode(" + Code;
1268 } else {
1269 Code = "CurDAG->SelectNodeTo(N.Val, " + Code;
1270 }
1271 if (isRoot) {
1272 if (After.empty())
1273 CodePrefix = "return ";
1274 else
1275 After.push_back("return ResNode;");
1276 }
1277
1278 emitCode(CodePrefix + Code + ");");
1279 for (unsigned i = 0, e = After.size(); i != e; ++i)
1280 emitCode(After[i]);
1281
Evan Cheng676d7312006-08-26 00:59:04 +00001282 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001283 } else if (Op->isSubClassOf("SDNodeXForm")) {
1284 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001285 // PatLeaf node - the operand may or may not be a leaf node. But it should
1286 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001287 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001288 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001289 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001290 unsigned ResNo = TmpNo++;
Evan Cheng676d7312006-08-26 00:59:04 +00001291 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
1292 + "(" + Ops.back() + ".Val);");
1293 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001294 if (isRoot)
1295 emitCode("return Tmp" + utostr(ResNo) + ".Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001296 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001297 } else {
1298 N->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001299 cerr << "\n";
Chris Lattner7893f132006-01-11 01:33:49 +00001300 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001301 }
1302 }
1303
Chris Lattner488580c2006-01-28 19:06:51 +00001304 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1305 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001306 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1307 /// for, this returns true otherwise false if Pat has all types.
1308 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001309 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001310 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001311 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001312 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001313 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001314 // The top level node type is checked outside of the select function.
1315 if (!isRoot)
1316 emitCheck(Prefix + ".Val->getValueType(0) == " +
1317 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001318 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001319 }
1320
Evan Cheng51fecc82006-01-09 18:27:06 +00001321 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001322 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001323 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1324 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1325 Prefix + utostr(OpNo)))
1326 return true;
1327 return false;
1328 }
1329
1330private:
Evan Cheng54597732006-01-26 00:22:25 +00001331 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001332 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001333 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001334 bool &ChainEmitted, bool &InFlagDecled,
1335 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001336 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001337 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001338 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1339 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001340 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1341 TreePatternNode *Child = N->getChild(i);
1342 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001343 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1344 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001345 } else {
1346 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001347 if (!Child->getName().empty()) {
1348 std::string Name = RootName + utostr(OpNo);
1349 if (Duplicates.find(Name) != Duplicates.end())
1350 // A duplicate! Do not emit a copy for this node.
1351 continue;
1352 }
1353
Evan Chengb915f312005-12-09 22:45:35 +00001354 Record *RR = DI->getDef();
1355 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001356 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001357 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001358 if (!InFlagDecled) {
1359 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
1360 InFlagDecled = true;
1361 } else
1362 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1363 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001364 } else {
1365 if (!ChainEmitted) {
Evan Cheng676d7312006-08-26 00:59:04 +00001366 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001367 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001368 ChainEmitted = true;
1369 }
Evan Cheng676d7312006-08-26 00:59:04 +00001370 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1371 if (!InFlagDecled) {
1372 emitCode("SDOperand InFlag(0, 0);");
1373 InFlagDecled = true;
1374 }
1375 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1376 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001377 ", " + getQualifiedName(RR) +
Evan Cheng7a33db02006-08-26 07:39:28 +00001378 ", " + RootName + utostr(OpNo) + ", InFlag).Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001379 ResNodeDecled = true;
Evan Cheng67212a02006-02-09 22:12:27 +00001380 emitCode(ChainName + " = SDOperand(ResNode, 0);");
1381 emitCode("InFlag = SDOperand(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001382 }
1383 }
1384 }
1385 }
1386 }
Evan Cheng54597732006-01-26 00:22:25 +00001387
Evan Cheng676d7312006-08-26 00:59:04 +00001388 if (HasInFlag) {
1389 if (!InFlagDecled) {
1390 emitCode("SDOperand InFlag = " + RootName +
1391 ".getOperand(" + utostr(OpNo) + ");");
1392 InFlagDecled = true;
1393 } else
1394 emitCode("InFlag = " + RootName +
1395 ".getOperand(" + utostr(OpNo) + ");");
1396 emitCode("AddToISelQueue(InFlag);");
1397 }
Evan Chengb915f312005-12-09 22:45:35 +00001398 }
1399};
1400
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001401/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1402/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001403/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001404void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001405 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001406 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001407 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001408 std::vector<std::string> &TargetVTs,
1409 bool &OutputIsVariadic,
1410 unsigned &NumInputRootOps) {
1411 OutputIsVariadic = false;
1412 NumInputRootOps = 0;
1413
Chris Lattner200c57e2008-01-05 22:58:54 +00001414 PatternCodeEmitter Emitter(CGP, Pattern.getPredicates(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001415 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001416 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001417 TargetOpcodes, TargetVTs,
1418 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001419
Chris Lattner8fc35682005-09-23 23:16:51 +00001420 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001421 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001422 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001423
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001424 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001425 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001426
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001427 // At this point, we know that we structurally match the pattern, but the
1428 // types of the nodes may not match. Figure out the fewest number of type
1429 // comparisons we need to emit. For example, if there is only one integer
1430 // type supported by a target, there should be no type comparisons at all for
1431 // integer patterns!
1432 //
1433 // To figure out the fewest number of type checks needed, clone the pattern,
1434 // remove the types, then perform type inference on the pattern as a whole.
1435 // If there are unresolved types, emit an explicit check for those types,
1436 // apply the type to the tree, then rerun type inference. Iterate until all
1437 // types are resolved.
1438 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001439 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001440 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001441
1442 do {
1443 // Resolve/propagate as many types as possible.
1444 try {
1445 bool MadeChange = true;
1446 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001447 MadeChange = Pat->ApplyTypeConstraints(TP,
1448 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001449 } catch (...) {
1450 assert(0 && "Error: could not find consistent types for something we"
1451 " already decided was ok!");
1452 abort();
1453 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001454
Chris Lattner7e82f132005-10-15 21:34:21 +00001455 // Insert a check for an unresolved type and add it to the tree. If we find
1456 // an unresolved type to add a check for, this returns true and we iterate,
1457 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001458 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001459
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001460 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001461 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001462 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001463}
1464
Chris Lattner24e00a42006-01-29 04:41:05 +00001465/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1466/// a line causes any of them to be empty, remove them and return true when
1467/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001468static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001469 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001470 &Patterns) {
1471 bool ErasedPatterns = false;
1472 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1473 Patterns[i].second.pop_back();
1474 if (Patterns[i].second.empty()) {
1475 Patterns.erase(Patterns.begin()+i);
1476 --i; --e;
1477 ErasedPatterns = true;
1478 }
1479 }
1480 return ErasedPatterns;
1481}
1482
Chris Lattner8bc74722006-01-29 04:25:26 +00001483/// EmitPatterns - Emit code for at least one pattern, but try to group common
1484/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001485void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001486 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001487 &Patterns, unsigned Indent,
1488 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001489 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001490 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001491 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001492
1493 if (Patterns.empty()) return;
1494
Chris Lattner24e00a42006-01-29 04:41:05 +00001495 // Figure out how many patterns share the next code line. Explicitly copy
1496 // FirstCodeLine so that we don't invalidate a reference when changing
1497 // Patterns.
1498 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001499 unsigned LastMatch = Patterns.size()-1;
1500 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1501 --LastMatch;
1502
1503 // If not all patterns share this line, split the list into two pieces. The
1504 // first chunk will use this line, the second chunk won't.
1505 if (LastMatch != 0) {
1506 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1507 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1508
1509 // FIXME: Emit braces?
1510 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001511 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001512 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1513 Pattern.getSrcPattern()->print(OS);
1514 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1515 Pattern.getDstPattern()->print(OS);
1516 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001517 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001518 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001519 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001520 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001521 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001522 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001523 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001524 }
Evan Cheng676d7312006-08-26 00:59:04 +00001525 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001526 OS << std::string(Indent, ' ') << "{\n";
1527 Indent += 2;
1528 }
1529 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001530 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001531 Indent -= 2;
1532 OS << std::string(Indent, ' ') << "}\n";
1533 }
1534
1535 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001536 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001537 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1538 Pattern.getSrcPattern()->print(OS);
1539 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1540 Pattern.getDstPattern()->print(OS);
1541 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001542 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001543 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001544 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001545 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001546 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001547 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001548 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001549 }
1550 EmitPatterns(Other, Indent, OS);
1551 return;
1552 }
1553
Chris Lattner24e00a42006-01-29 04:41:05 +00001554 // Remove this code from all of the patterns that share it.
1555 bool ErasedPatterns = EraseCodeLine(Patterns);
1556
Evan Cheng676d7312006-08-26 00:59:04 +00001557 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001558
1559 // Otherwise, every pattern in the list has this line. Emit it.
1560 if (!isPredicate) {
1561 // Normal code.
1562 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1563 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001564 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1565
1566 // If the next code line is another predicate, and if all of the pattern
1567 // in this group share the same next line, emit it inline now. Do this
1568 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001569 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001570 // Check that all of fhe patterns in Patterns end with the same predicate.
1571 bool AllEndWithSamePredicate = true;
1572 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1573 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1574 AllEndWithSamePredicate = false;
1575 break;
1576 }
1577 // If all of the predicates aren't the same, we can't share them.
1578 if (!AllEndWithSamePredicate) break;
1579
1580 // Otherwise we can. Emit it shared now.
1581 OS << " &&\n" << std::string(Indent+4, ' ')
1582 << Patterns.back().second.back().second;
1583 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001584 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001585
1586 OS << ") {\n";
1587 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001588 }
1589
1590 EmitPatterns(Patterns, Indent, OS);
1591
1592 if (isPredicate)
1593 OS << std::string(Indent-2, ' ') << "}\n";
1594}
1595
Evan Cheng892aaf82006-11-08 23:01:03 +00001596static std::string getLegalCName(std::string OpName) {
1597 std::string::size_type pos = OpName.find("::");
1598 if (pos != std::string::npos)
1599 OpName.replace(pos, 2, "_");
1600 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001601}
1602
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001603void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001604 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001605
Chris Lattnerf7560ed2006-11-20 18:54:33 +00001606 // Get the namespace to insert instructions into. Make sure not to pick up
1607 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
1608 // instruction or something.
1609 std::string InstNS;
1610 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
1611 e = Target.inst_end(); i != e; ++i) {
1612 InstNS = i->second.Namespace;
1613 if (InstNS != "TargetInstrInfo")
1614 break;
1615 }
1616
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001617 if (!InstNS.empty()) InstNS += "::";
1618
Chris Lattner602f6922006-01-04 00:25:00 +00001619 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001620 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001621 // All unique target node emission functions.
1622 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001623 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001624 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001625 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001626
1627 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001628 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001629 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001630 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001631 } else {
1632 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001633 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001634 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001635 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001636 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001637 std::vector<Record*> OpNodes = CP->getRootNodes();
1638 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001639 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1640 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001641 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001642 }
1643 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001644 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001645 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001646 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001647 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001648 exit(1);
1649 }
1650 }
1651 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001652
1653 // For each opcode, there might be multiple select functions, one per
1654 // ValueType of the node (or its first operand if it doesn't produce a
1655 // non-chain result.
1656 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1657
Chris Lattner602f6922006-01-04 00:25:00 +00001658 // Emit one Select_* method for each top-level opcode. We do this instead of
1659 // emitting one giant switch statement to support compilers where this will
1660 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001661 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001662 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1663 PBOI != E; ++PBOI) {
1664 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001665 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001666 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1667
Chris Lattner602f6922006-01-04 00:25:00 +00001668 // We want to emit all of the matching code now. However, we want to emit
1669 // the matches in order of minimal cost. Sort the patterns so the least
1670 // cost one is at the start.
Chris Lattner706d2d32006-08-09 16:44:44 +00001671 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001672 PatternSortingPredicate(CGP));
Evan Cheng21ad3922006-02-07 00:37:41 +00001673
Chris Lattner706d2d32006-08-09 16:44:44 +00001674 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001675 std::map<MVT::SimpleValueType,
1676 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001677 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001678 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001679 TreePatternNode *SrcPat = Pat->getSrcPattern();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001680 MVT::SimpleValueType VT = SrcPat->getTypeNum(0);
1681 std::map<MVT::SimpleValueType,
Chris Lattner60d81392008-01-05 22:30:17 +00001682 std::vector<const PatternToMatch*> >::iterator TI =
Chris Lattner706d2d32006-08-09 16:44:44 +00001683 PatternsByType.find(VT);
1684 if (TI != PatternsByType.end())
1685 TI->second.push_back(Pat);
1686 else {
Chris Lattner60d81392008-01-05 22:30:17 +00001687 std::vector<const PatternToMatch*> PVec;
Chris Lattner706d2d32006-08-09 16:44:44 +00001688 PVec.push_back(Pat);
1689 PatternsByType.insert(std::make_pair(VT, PVec));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001690 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001691 }
1692
Duncan Sands83ec4b62008-06-06 12:08:01 +00001693 for (std::map<MVT::SimpleValueType,
1694 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001695 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1696 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001697 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001698 std::vector<const PatternToMatch*> &Patterns = II->second;
Chris Lattner64906972006-09-21 18:28:27 +00001699 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1700 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001701
Chris Lattner60d81392008-01-05 22:30:17 +00001702 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001703 std::vector<std::vector<std::string> > PatternOpcodes;
1704 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001705 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001706 std::vector<bool> OutputIsVariadicFlags;
1707 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001708 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1709 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001710 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001711 std::vector<std::string> TargetOpcodes;
1712 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001713 bool OutputIsVariadic;
1714 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001715 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001716 TargetOpcodes, TargetVTs,
1717 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001718 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1719 PatternDecls.push_back(GeneratedDecl);
1720 PatternOpcodes.push_back(TargetOpcodes);
1721 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001722 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1723 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001724 }
1725
1726 // Scan the code to see if all of the patterns are reachable and if it is
1727 // possible that the last one might not match.
1728 bool mightNotMatch = true;
1729 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1730 CodeList &GeneratedCode = CodeForPatterns[i].second;
1731 mightNotMatch = false;
1732
1733 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001734 if (GeneratedCode[j].first == 1) { // predicate.
Chris Lattner706d2d32006-08-09 16:44:44 +00001735 mightNotMatch = true;
1736 break;
1737 }
1738 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001739
Chris Lattner706d2d32006-08-09 16:44:44 +00001740 // If this pattern definitely matches, and if it isn't the last one, the
1741 // patterns after it CANNOT ever match. Error out.
1742 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001743 cerr << "Pattern '";
1744 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1745 cerr << "' is impossible to select!\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001746 exit(1);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001747 }
1748 }
1749
Chris Lattner706d2d32006-08-09 16:44:44 +00001750 // Factor target node emission code (emitted by EmitResultCode) into
1751 // separate functions. Uniquing and share them among all instruction
1752 // selection routines.
1753 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1754 CodeList &GeneratedCode = CodeForPatterns[i].second;
1755 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1756 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001757 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001758 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1759 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001760 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001761 int CodeSize = (int)GeneratedCode.size();
1762 int LastPred = -1;
1763 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001764 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001765 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001766 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1767 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001768 }
1769
Evan Cheng9ade2182006-08-26 05:34:46 +00001770 std::string CalleeCode = "(const SDOperand &N";
1771 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001772 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1773 CalleeCode += ", unsigned Opc" + utostr(j);
1774 CallerCode += ", " + TargetOpcodes[j];
1775 }
1776 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001777 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001778 CallerCode += ", " + TargetVTs[j];
1779 }
Evan Chengf5493192006-08-26 01:02:19 +00001780 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001781 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001782 std::string Name = *I;
Evan Cheng676d7312006-08-26 00:59:04 +00001783 CalleeCode += ", SDOperand &" + Name;
1784 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001785 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001786
1787 if (OutputIsVariadic) {
1788 CalleeCode += ", unsigned NumInputRootOps";
1789 CallerCode += ", " + utostr(NumInputRootOps);
1790 }
1791
Chris Lattner706d2d32006-08-09 16:44:44 +00001792 CallerCode += ");";
1793 CalleeCode += ") ";
1794 // Prevent emission routines from being inlined to reduce selection
1795 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001796 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001797 CalleeCode += "{\n";
1798
1799 for (std::vector<std::string>::const_reverse_iterator
1800 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1801 CalleeCode += " " + *I + "\n";
1802
Evan Chengf5493192006-08-26 01:02:19 +00001803 for (int j = LastPred+1; j < CodeSize; ++j)
1804 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001805 for (int j = LastPred+1; j < CodeSize; ++j)
1806 GeneratedCode.pop_back();
1807 CalleeCode += "}\n";
1808
1809 // Uniquing the emission routines.
1810 unsigned EmitFuncNum;
1811 std::map<std::string, unsigned>::iterator EFI =
1812 EmitFunctions.find(CalleeCode);
1813 if (EFI != EmitFunctions.end()) {
1814 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001815 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001816 EmitFuncNum = EmitFunctions.size();
1817 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001818 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001819 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001820
Chris Lattner706d2d32006-08-09 16:44:44 +00001821 // Replace the emission code within selection routines with calls to the
1822 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001823 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001824 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001825 }
1826
Chris Lattner706d2d32006-08-09 16:44:44 +00001827 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001828 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001829 if (OpVT == MVT::iPTR) {
1830 OpVTStr = "_iPTR";
1831 } else if (OpVT == MVT::isVoid) {
1832 // Nodes with a void result actually have a first result type of either
1833 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1834 // void to this case, we handle it specially here.
1835 } else {
1836 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1837 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001838 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1839 OpcodeVTMap.find(OpName);
1840 if (OpVTI == OpcodeVTMap.end()) {
1841 std::vector<std::string> VTSet;
1842 VTSet.push_back(OpVTStr);
1843 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1844 } else
1845 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001846
Evan Cheng892aaf82006-11-08 23:01:03 +00001847 OS << "SDNode *Select_" << getLegalCName(OpName)
Chris Lattner33a40042006-11-14 22:17:10 +00001848 << OpVTStr << "(const SDOperand &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001849
Chris Lattner706d2d32006-08-09 16:44:44 +00001850 // Loop through and reverse all of the CodeList vectors, as we will be
1851 // accessing them from their logical front, but accessing the end of a
1852 // vector is more efficient.
1853 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1854 CodeList &GeneratedCode = CodeForPatterns[i].second;
1855 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001856 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001857
1858 // Next, reverse the list of patterns itself for the same reason.
1859 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1860
1861 // Emit all of the patterns now, grouped together to share code.
1862 EmitPatterns(CodeForPatterns, 2, OS);
1863
Chris Lattner64906972006-09-21 18:28:27 +00001864 // If the last pattern has predicates (which could fail) emit code to
1865 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001866 if (mightNotMatch) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001867 OS << " cerr << \"Cannot yet select: \";\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001868 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1869 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1870 OpName != "ISD::INTRINSIC_VOID") {
Chris Lattner706d2d32006-08-09 16:44:44 +00001871 OS << " N.Val->dump(CurDAG);\n";
1872 } else {
1873 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1874 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001875 << " cerr << \"intrinsic %\"<< "
Chris Lattner706d2d32006-08-09 16:44:44 +00001876 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1877 }
Bill Wendlingf5da1332006-12-07 22:21:48 +00001878 OS << " cerr << '\\n';\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001879 << " abort();\n"
1880 << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001881 }
1882 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001883 }
Chris Lattner602f6922006-01-04 00:25:00 +00001884 }
1885
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001886 // Emit boilerplate.
Evan Cheng9ade2182006-08-26 05:34:46 +00001887 OS << "SDNode *Select_INLINEASM(SDOperand N) {\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001888 << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001889 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n"
1890
1891 << " // Ensure that the asm operands are themselves selected.\n"
1892 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1893 << " AddToISelQueue(Ops[j]);\n\n"
1894
Duncan Sands83ec4b62008-06-06 12:08:01 +00001895 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001896 << " VTs.push_back(MVT::Other);\n"
1897 << " VTs.push_back(MVT::Flag);\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001898 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
1899 "Ops.size());\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001900 << " return New.Val;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001901 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001902
1903 OS << "SDNode *Select_UNDEF(const SDOperand &N) {\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001904 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::IMPLICIT_DEF,\n"
1905 << " N.getValueType());\n"
1906 << "}\n\n";
1907
1908 OS << "SDNode *Select_DBG_LABEL(const SDOperand &N) {\n"
1909 << " SDOperand Chain = N.getOperand(0);\n"
1910 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
1911 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1912 << " AddToISelQueue(Chain);\n"
1913 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::DBG_LABEL,\n"
1914 << " MVT::Other, Tmp, Chain);\n"
1915 << "}\n\n";
1916
1917 OS << "SDNode *Select_EH_LABEL(const SDOperand &N) {\n"
1918 << " SDOperand Chain = N.getOperand(0);\n"
1919 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
1920 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1921 << " AddToISelQueue(Chain);\n"
1922 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::EH_LABEL,\n"
1923 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001924 << "}\n\n";
1925
Evan Chenga844bde2008-02-02 04:07:54 +00001926 OS << "SDNode *Select_DECLARE(const SDOperand &N) {\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001927 << " SDOperand Chain = N.getOperand(0);\n"
1928 << " SDOperand N1 = N.getOperand(1);\n"
1929 << " SDOperand N2 = N.getOperand(2);\n"
1930 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
1931 << " cerr << \"Cannot yet select llvm.dbg.declare: \";\n"
1932 << " N.Val->dump(CurDAG);\n"
1933 << " abort();\n"
1934 << " }\n"
1935 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1936 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001937 << " SDOperand Tmp1 = "
1938 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
1939 << " SDOperand Tmp2 = "
1940 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1941 << " AddToISelQueue(Chain);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001942 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::DECLARE,\n"
1943 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001944 << "}\n\n";
1945
Christopher Lamb08d52072007-07-26 07:48:21 +00001946 OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n"
1947 << " SDOperand N0 = N.getOperand(0);\n"
1948 << " SDOperand N1 = N.getOperand(1);\n"
1949 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1950 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1951 << " AddToISelQueue(N0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001952 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::EXTRACT_SUBREG,\n"
1953 << " N.getValueType(), N0, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001954 << "}\n\n";
1955
1956 OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n"
1957 << " SDOperand N0 = N.getOperand(0);\n"
1958 << " SDOperand N1 = N.getOperand(1);\n"
1959 << " SDOperand N2 = N.getOperand(2);\n"
1960 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
1961 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1962 << " AddToISelQueue(N1);\n"
Christopher Lamb6634e262008-03-13 05:47:01 +00001963 << " AddToISelQueue(N0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001964 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::INSERT_SUBREG,\n"
1965 << " N.getValueType(), N0, N1, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001966 << "}\n\n";
1967
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001968 OS << "// The main instruction selector code.\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001969 << "SDNode *SelectCode(SDOperand N) {\n"
Dan Gohmane8be6c62008-07-17 19:10:17 +00001970 << " if (N.isMachineOpcode()) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001971 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001972 << " }\n\n"
Duncan Sands83ec4b62008-06-06 12:08:01 +00001973 << " MVT::SimpleValueType NVT = N.Val->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001974 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001975 << " default: break;\n"
1976 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001977 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001978 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001979 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001980 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001981 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001982 << " case ISD::TargetConstantPool:\n"
1983 << " case ISD::TargetFrameIndex:\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001984 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001985 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001986 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001987 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001988 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001989 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001990 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001991 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001992 << " AddToISelQueue(N.getOperand(0));\n"
1993 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001994 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001995 << " }\n"
1996 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001997 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001998 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001999 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
2000 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002001 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00002002 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00002003 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00002004 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
2005 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00002006 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00002007 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00002008 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
2009 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00002010
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002011
Chris Lattner602f6922006-01-04 00:25:00 +00002012 // Loop over all of the case statements, emiting a call to each method we
2013 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00002014 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00002015 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
2016 PBOI != E; ++PBOI) {
2017 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00002018 // Potentially multiple versions of select for this opcode. One for each
2019 // ValueType of the node (or its first true operand if it doesn't produce a
2020 // result.
2021 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
2022 OpcodeVTMap.find(OpName);
2023 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00002024 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002025 // Keep track of whether we see a pattern that has an iPtr result.
2026 bool HasPtrPattern = false;
2027 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00002028
Evan Cheng425e8c72007-09-04 20:18:28 +00002029 OS << " switch (NVT) {\n";
2030 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
2031 std::string &VTStr = OpVTs[i];
2032 if (VTStr.empty()) {
2033 HasDefaultPattern = true;
2034 continue;
2035 }
Chris Lattner717a6112006-11-14 21:50:27 +00002036
Evan Cheng425e8c72007-09-04 20:18:28 +00002037 // If this is a match on iPTR: don't emit it directly, we need special
2038 // code.
2039 if (VTStr == "_iPTR") {
2040 HasPtrPattern = true;
2041 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002042 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002043 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2044 << " return Select_" << getLegalCName(OpName)
2045 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002046 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002047 OS << " default:\n";
2048
2049 // If there is an iPTR result version of this pattern, emit it here.
2050 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002051 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002052 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2053 }
2054 if (HasDefaultPattern) {
2055 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2056 }
2057 OS << " break;\n";
2058 OS << " }\n";
2059 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002060 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002061 }
Chris Lattner81303322005-09-23 19:36:15 +00002062
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002063 OS << " } // end of big switch.\n\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002064 << " cerr << \"Cannot yet select: \";\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002065 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2066 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2067 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002068 << " N.Val->dump(CurDAG);\n"
2069 << " } else {\n"
2070 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2071 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002072 << " cerr << \"intrinsic %\"<< "
2073 "Intrinsic::getName((Intrinsic::ID)iid);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002074 << " }\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002075 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002076 << " abort();\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002077 << " return NULL;\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002078 << "}\n";
2079}
2080
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002081void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002082 EmitSourceFileHeader("DAG Instruction Selector for the " +
2083 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002084
Chris Lattner1f39e292005-09-14 00:09:24 +00002085 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2086 << "// *** instruction selector class. These functions are really "
2087 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002088
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002089 OS << "// Include standard, target-independent definitions and methods used\n"
2090 << "// by the instruction selector.\n";
2091 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002092
Chris Lattner443e3f92008-01-05 22:54:53 +00002093 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002094 EmitPredicateFunctions(OS);
2095
Bill Wendlingf5da1332006-12-07 22:21:48 +00002096 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002097 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002098 I != E; ++I) {
2099 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2100 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002101 DOUT << "\n";
2102 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002103
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002104 // At this point, we have full information about the 'Patterns' we need to
2105 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002106 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002107 EmitInstructionSelector(OS);
2108
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002109}