blob: 8632bc5ce8db7adf7debe9a64921d3040784968f [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000018#include "llvm/Support/MathExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000019#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000020#include <algorithm>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000021using namespace llvm;
22
Chris Lattnerca559d02005-09-08 21:03:01 +000023//===----------------------------------------------------------------------===//
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024// DAGISelEmitter implementation
25//
26
Chris Lattner5a1df382006-03-24 23:10:39 +000027
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 Lattner6cefb772008-01-05 22:25:12 +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 Lattner6cefb772008-01-05 22:25:12 +000054static unsigned getPatternSize(TreePatternNode *P, CodegenDAGPatterns &CGP) {
55 assert((MVT::isExtIntegerInVTs(P->getExtTypes()) ||
56 MVT::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,
103 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,
122 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 Lattner6cefb772008-01-05 22:25:12 +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
Evan Cheng66a48bb2005-12-01 00:18:45 +0000164static MVT::ValueType 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 Lattner6cefb772008-01-05 22:25:12 +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 Lattner6cefb772008-01-05 22:25:12 +0000197 CodegenDAGPatterns &CGP) {
198 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 Chengb915f312005-12-09 22:45:35 +0000210class PatternCodeEmitter {
211private:
Chris Lattner6cefb772008-01-05 22:25:12 +0000212 CodegenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000213
Evan Cheng58e84a62005-12-14 22:02:59 +0000214 // Predicates.
215 ListInit *Predicates;
Evan Cheng59413202006-04-19 18:07:24 +0000216 // Pattern cost.
217 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000218 // Instruction selector pattern.
219 TreePatternNode *Pattern;
220 // Matched instruction.
221 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000222
Evan Chengb915f312005-12-09 22:45:35 +0000223 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000224 std::map<std::string, std::string> VariableMap;
225 // Node to operator mapping
226 std::map<std::string, Record*> OperatorMap;
Evan Chengb915f312005-12-09 22:45:35 +0000227 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000228 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000229 // Original input chain(s).
230 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000231 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000232
Evan Cheng676d7312006-08-26 00:59:04 +0000233 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000234 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000235 /// tested, and if true, the match fails) [when 1], or normal code to emit
236 /// [when 0], or initialization code to emit [when 2].
237 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Evan Cheng21ad3922006-02-07 00:37:41 +0000238 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
239 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000240 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000241 /// TargetOpcodes - The target specific opcodes used by the resulting
242 /// instructions.
243 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000244 std::vector<std::string> &TargetVTs;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000245
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000246 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000247 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000248 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000249 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000250
251 void emitCheck(const std::string &S) {
252 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000253 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000254 }
255 void emitCode(const std::string &S) {
256 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000257 GeneratedCode.push_back(std::make_pair(0, S));
258 }
259 void emitInit(const std::string &S) {
260 if (!S.empty())
261 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000262 }
Evan Chengf5493192006-08-26 01:02:19 +0000263 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000264 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000265 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000266 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000267 void emitOpcode(const std::string &Opc) {
268 TargetOpcodes.push_back(Opc);
269 OpcNo++;
270 }
Evan Chengf8729402006-07-16 06:12:52 +0000271 void emitVT(const std::string &VT) {
272 TargetVTs.push_back(VT);
273 VTNo++;
274 }
Evan Chengb915f312005-12-09 22:45:35 +0000275public:
Chris Lattner6cefb772008-01-05 22:25:12 +0000276 PatternCodeEmitter(CodegenDAGPatterns &cgp, ListInit *preds,
Evan Cheng58e84a62005-12-14 22:02:59 +0000277 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000278 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000279 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000280 std::vector<std::string> &to,
Chris Lattner706d2d32006-08-09 16:44:44 +0000281 std::vector<std::string> &tv)
Chris Lattner6cefb772008-01-05 22:25:12 +0000282 : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000283 GeneratedCode(gc), GeneratedDecl(gd),
284 TargetOpcodes(to), TargetVTs(tv),
Chris Lattner706d2d32006-08-09 16:44:44 +0000285 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000286
287 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
288 /// if the match fails. At this point, we already know that the opcode for N
289 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000290 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
291 const std::string &RootName, const std::string &ChainSuffix,
292 bool &FoundChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000293 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000294 // Emit instruction predicates. Each predicate is just a string for now.
295 if (isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000296 std::string PredicateCheck;
Evan Cheng58e84a62005-12-14 22:02:59 +0000297 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
298 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
299 Record *Def = Pred->getDef();
Chris Lattner8a0604b2006-01-28 20:31:24 +0000300 if (!Def->isSubClassOf("Predicate")) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000301#ifndef NDEBUG
302 Def->dump();
303#endif
Evan Cheng58e84a62005-12-14 22:02:59 +0000304 assert(0 && "Unknown predicate type!");
305 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000306 if (!PredicateCheck.empty())
Chris Lattnerbc7fa522006-09-19 00:41:36 +0000307 PredicateCheck += " && ";
Chris Lattner67a202b2006-01-28 20:43:52 +0000308 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
Evan Cheng58e84a62005-12-14 22:02:59 +0000309 }
310 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000311
312 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000313 }
314
Evan Chengb915f312005-12-09 22:45:35 +0000315 if (N->isLeaf()) {
316 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000317 emitCheck("cast<ConstantSDNode>(" + RootName +
Chris Lattner67a202b2006-01-28 20:43:52 +0000318 ")->getSignExtended() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000319 return;
320 } else if (!NodeIsComplexPattern(N)) {
321 assert(0 && "Cannot match this as a leaf value!");
322 abort();
323 }
324 }
325
Chris Lattner488580c2006-01-28 19:06:51 +0000326 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000327 // we already saw this in the pattern, emit code to verify dagness.
328 if (!N->getName().empty()) {
329 std::string &VarMapEntry = VariableMap[N->getName()];
330 if (VarMapEntry.empty()) {
331 VarMapEntry = RootName;
332 } else {
333 // If we get here, this is a second reference to a specific name. Since
334 // we already have checked that the first reference is valid, we don't
335 // have to recursively match it, just check that it's the same as the
336 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000337 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000338 return;
339 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000340
341 if (!N->isLeaf())
342 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000343 }
344
345
346 // Emit code to load the child nodes and match their contents recursively.
347 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000348 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
349 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000350 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000351 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000352 if (NodeHasChain)
353 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000354 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000355 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000356 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000357 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000358 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000359 // If the immediate use can somehow reach this node through another
360 // path, then can't fold it either or it will create a cycle.
361 // e.g. In the following diagram, XX can reach ld through YY. If
362 // ld is folded into XX, then YY is both a predecessor and a successor
363 // of XX.
364 //
365 // [ld]
366 // ^ ^
367 // | |
368 // / \---
369 // / [YY]
370 // | ^
371 // [XX]-------|
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000372 bool NeedCheck = false;
373 if (P != Pattern)
374 NeedCheck = true;
375 else {
Chris Lattner6cefb772008-01-05 22:25:12 +0000376 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000377 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000378 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
379 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
380 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000381 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000382 PInfo.hasProperty(SDNPHasChain) ||
383 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000384 PInfo.hasProperty(SDNPOptInFlag);
385 }
386
387 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000388 std::string ParentName(RootName.begin(), RootName.end()-1);
Chris Lattner706d2d32006-08-09 16:44:44 +0000389 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
Evan Chengce1381a2006-10-14 08:30:15 +0000390 ".Val, N.Val)");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000391 }
Evan Chenge41bf822006-02-05 06:43:12 +0000392 }
Evan Chengb915f312005-12-09 22:45:35 +0000393 }
Evan Chenge41bf822006-02-05 06:43:12 +0000394
Evan Chengc15d18c2006-01-27 22:13:45 +0000395 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000396 if (FoundChain) {
397 emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || "
398 "IsChainCompatible(" + ChainName + ".Val, " +
399 RootName + ".Val))");
400 OrigChains.push_back(std::make_pair(ChainName, RootName));
401 } else
Evan Chenge6389932006-07-21 22:19:51 +0000402 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000403 ChainName = "Chain" + ChainSuffix;
Evan Cheng676d7312006-08-26 00:59:04 +0000404 emitInit("SDOperand " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000405 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000406 }
Evan Chengb915f312005-12-09 22:45:35 +0000407 }
408
Evan Cheng54597732006-01-26 00:22:25 +0000409 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000410 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000411 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000412 // FIXME: If the optional incoming flag does not exist. Then it is ok to
413 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000414 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000415 (PatternHasProperty(N, SDNPInFlag, CGP) ||
416 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
417 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000418 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000419 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000420 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000421 }
422 }
423
Evan Chengd3eea902006-10-09 21:02:17 +0000424 // If there is a node predicate for this, emit the call.
425 if (!N->getPredicateFn().empty())
426 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
427
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000428
Chris Lattner39e73f72006-10-11 04:05:55 +0000429 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
430 // a constant without a predicate fn that has more that one bit set, handle
431 // this as a special case. This is usually for targets that have special
432 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
433 // handling stuff). Using these instructions is often far more efficient
434 // than materializing the constant. Unfortunately, both the instcombiner
435 // and the dag combiner can often infer that bits are dead, and thus drop
436 // them from the mask in the dag. For example, it might turn 'AND X, 255'
437 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
438 // to handle this.
439 if (!N->isLeaf() &&
440 (N->getOperator()->getName() == "and" ||
441 N->getOperator()->getName() == "or") &&
442 N->getChild(1)->isLeaf() &&
443 N->getChild(1)->getPredicateFn().empty()) {
444 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
445 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
446 emitInit("SDOperand " + RootName + "0" + " = " +
447 RootName + ".getOperand(" + utostr(0) + ");");
448 emitInit("SDOperand " + RootName + "1" + " = " +
449 RootName + ".getOperand(" + utostr(1) + ");");
450
451 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
452 const char *MaskPredicate = N->getOperator()->getName() == "or"
453 ? "CheckOrMask(" : "CheckAndMask(";
454 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
455 RootName + "1), " + itostr(II->getValue()) + ")");
456
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000457 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0),
Chris Lattner39e73f72006-10-11 04:05:55 +0000458 ChainSuffix + utostr(0), FoundChain);
459 return;
460 }
461 }
462 }
463
Evan Chengb915f312005-12-09 22:45:35 +0000464 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Evan Cheng676d7312006-08-26 00:59:04 +0000465 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000466 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000467
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000468 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo),
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000469 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000470 }
471
Evan Cheng676d7312006-08-26 00:59:04 +0000472 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000473 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000474 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000475 std::string Fn = CP->getSelectFunc();
476 unsigned NumOps = CP->getNumOperands();
477 for (unsigned i = 0; i < NumOps; ++i) {
478 emitDecl("CPTmp" + utostr(i));
479 emitCode("SDOperand CPTmp" + utostr(i) + ";");
480 }
Evan Cheng94b30402006-10-11 21:02:01 +0000481 if (CP->hasProperty(SDNPHasChain)) {
482 emitDecl("CPInChain");
483 emitDecl("Chain" + ChainSuffix);
484 emitCode("SDOperand CPInChain;");
485 emitCode("SDOperand Chain" + ChainSuffix + ";");
486 }
Evan Cheng676d7312006-08-26 00:59:04 +0000487
Evan Cheng811731e2006-11-08 20:31:10 +0000488 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000489 for (unsigned i = 0; i < NumOps; i++)
490 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000491 if (CP->hasProperty(SDNPHasChain)) {
492 ChainName = "Chain" + ChainSuffix;
493 Code += ", CPInChain, Chain" + ChainSuffix;
494 }
Evan Cheng676d7312006-08-26 00:59:04 +0000495 emitCheck(Code + ")");
496 }
Evan Chengb915f312005-12-09 22:45:35 +0000497 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000498
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000499 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
500 const std::string &RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000501 const std::string &ChainSuffix, bool &FoundChain) {
502 if (!Child->isLeaf()) {
503 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000504 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000505 emitCheck(RootName + ".getOpcode() == " +
506 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000507 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Chris Lattner6cefb772008-01-05 22:25:12 +0000508 if (NodeHasProperty(Child, SDNPHasChain, CGP))
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000509 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
510 } else {
511 // If this child has a name associated with it, capture it in VarMap. If
512 // we already saw this in the pattern, emit code to verify dagness.
513 if (!Child->getName().empty()) {
514 std::string &VarMapEntry = VariableMap[Child->getName()];
515 if (VarMapEntry.empty()) {
516 VarMapEntry = RootName;
517 } else {
518 // If we get here, this is a second reference to a specific name.
519 // Since we already have checked that the first reference is valid,
520 // we don't have to recursively match it, just check that it's the
521 // same as the previously named thing.
522 emitCheck(VarMapEntry + " == " + RootName);
523 Duplicates.insert(RootName);
524 return;
525 }
526 }
527
528 // Handle leaves of various types.
529 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
530 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000531 if (LeafRec->isSubClassOf("RegisterClass") ||
532 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000533 // Handle register references. Nothing to do here.
534 } else if (LeafRec->isSubClassOf("Register")) {
535 // Handle register references.
536 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
537 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000538 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000539 std::string Fn = CP->getSelectFunc();
540 unsigned NumOps = CP->getNumOperands();
541 for (unsigned i = 0; i < NumOps; ++i) {
542 emitDecl("CPTmp" + utostr(i));
543 emitCode("SDOperand CPTmp" + utostr(i) + ";");
544 }
Evan Cheng94b30402006-10-11 21:02:01 +0000545 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000546 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000547 FoldedChains.push_back(std::make_pair("CPInChain",
548 PInfo.getNumResults()));
549 ChainName = "Chain" + ChainSuffix;
550 emitDecl("CPInChain");
551 emitDecl(ChainName);
552 emitCode("SDOperand CPInChain;");
553 emitCode("SDOperand " + ChainName + ";");
554 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000555
Evan Cheng811731e2006-11-08 20:31:10 +0000556 std::string Code = Fn + "(N, ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000557 if (CP->hasProperty(SDNPHasChain)) {
558 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000559 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000560 }
561 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000562 for (unsigned i = 0; i < NumOps; i++)
563 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000564 if (CP->hasProperty(SDNPHasChain))
565 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000566 emitCheck(Code + ")");
567 } else if (LeafRec->getName() == "srcvalue") {
568 // Place holder for SRCVALUE nodes. Nothing to do here.
569 } else if (LeafRec->isSubClassOf("ValueType")) {
570 // Make sure this is the specified value type.
571 emitCheck("cast<VTSDNode>(" + RootName +
572 ")->getVT() == MVT::" + LeafRec->getName());
573 } else if (LeafRec->isSubClassOf("CondCode")) {
574 // Make sure this is the specified cond code.
575 emitCheck("cast<CondCodeSDNode>(" + RootName +
576 ")->get() == ISD::" + LeafRec->getName());
577 } else {
578#ifndef NDEBUG
579 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000580 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000581#endif
582 assert(0 && "Unknown leaf type!");
583 }
584
585 // If there is a node predicate for this, emit the call.
586 if (!Child->getPredicateFn().empty())
587 emitCheck(Child->getPredicateFn() + "(" + RootName +
588 ".Val)");
589 } else if (IntInit *II =
590 dynamic_cast<IntInit*>(Child->getLeafValue())) {
591 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
592 unsigned CTmp = TmpNo++;
593 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
594 RootName + ")->getSignExtended();");
595
596 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
597 } else {
598#ifndef NDEBUG
599 Child->dump();
600#endif
601 assert(0 && "Unknown leaf type!");
602 }
603 }
604 }
Evan Chengb915f312005-12-09 22:45:35 +0000605
606 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
607 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000608 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000609 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000610 bool InFlagDecled, bool ResNodeDecled,
611 bool LikeLeaf = false, bool isRoot = false) {
612 // List of arguments of getTargetNode() or SelectNodeTo().
613 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000614 // This is something selected from the pattern we matched.
615 if (!N->getName().empty()) {
Evan Chengb915f312005-12-09 22:45:35 +0000616 std::string &Val = VariableMap[N->getName()];
617 assert(!Val.empty() &&
618 "Variable referenced but not defined and not caught earlier!");
619 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
620 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000621 NodeOps.push_back(Val);
622 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000623 }
624
625 const ComplexPattern *CP;
626 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000627 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000628 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000629 std::string CastType;
Nate Begemanb73628b2005-12-30 00:12:56 +0000630 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000631 default:
632 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
633 << " type as an immediate constant. Aborting\n";
634 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000635 case MVT::i1: CastType = "bool"; break;
636 case MVT::i8: CastType = "unsigned char"; break;
637 case MVT::i16: CastType = "unsigned short"; break;
638 case MVT::i32: CastType = "unsigned"; break;
639 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000640 }
Evan Cheng676d7312006-08-26 00:59:04 +0000641 emitCode("SDOperand Tmp" + utostr(ResNo) +
Evan Chengfceb57a2006-07-15 08:45:20 +0000642 " = CurDAG->getTargetConstant(((" + CastType +
643 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
644 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000645 NodeOps.push_back("Tmp" + utostr(ResNo));
646 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
647 // value if used multiple times by this pattern result.
648 Val = "Tmp"+utostr(ResNo);
Evan Chengbb48e332006-01-12 07:54:57 +0000649 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000650 Record *Op = OperatorMap[N->getName()];
651 // Transform ExternalSymbol to TargetExternalSymbol
652 if (Op && Op->getName() == "externalsym") {
Evan Cheng676d7312006-08-26 00:59:04 +0000653 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000654 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000655 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000656 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000657 NodeOps.push_back("Tmp" + utostr(ResNo));
Chris Lattner64906972006-09-21 18:28:27 +0000658 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
659 // this value if used multiple times by this pattern result.
Evan Cheng676d7312006-08-26 00:59:04 +0000660 Val = "Tmp"+utostr(ResNo);
Chris Lattner8a0604b2006-01-28 20:31:24 +0000661 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000662 NodeOps.push_back(Val);
Chris Lattner8a0604b2006-01-28 20:31:24 +0000663 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000664 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
665 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000666 Record *Op = OperatorMap[N->getName()];
667 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000668 if (Op && (Op->getName() == "globaladdr" ||
669 Op->getName() == "globaltlsaddr")) {
Evan Cheng676d7312006-08-26 00:59:04 +0000670 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000671 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000672 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000673 ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000674 NodeOps.push_back("Tmp" + utostr(ResNo));
Chris Lattner64906972006-09-21 18:28:27 +0000675 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
676 // this value if used multiple times by this pattern result.
Evan Cheng676d7312006-08-26 00:59:04 +0000677 Val = "Tmp"+utostr(ResNo);
Chris Lattner8a0604b2006-01-28 20:31:24 +0000678 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000679 NodeOps.push_back(Val);
Chris Lattner8a0604b2006-01-28 20:31:24 +0000680 }
Chris Lattner4e3c8e512006-01-03 22:55:16 +0000681 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Cheng676d7312006-08-26 00:59:04 +0000682 NodeOps.push_back(Val);
683 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
684 // value if used multiple times by this pattern result.
685 Val = "Tmp"+utostr(ResNo);
Evan Chengbb48e332006-01-12 07:54:57 +0000686 } else if (!N->isLeaf() && N->getOperator()->getName() == "tconstpool") {
Evan Cheng676d7312006-08-26 00:59:04 +0000687 NodeOps.push_back(Val);
688 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
689 // value if used multiple times by this pattern result.
690 Val = "Tmp"+utostr(ResNo);
Chris Lattner6cefb772008-01-05 22:25:12 +0000691 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000692 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
693 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
694 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000695 }
Evan Chengb915f312005-12-09 22:45:35 +0000696 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000697 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000698 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000699 if (!LikeLeaf) {
700 emitCode("AddToISelQueue(" + Val + ");");
Chris Lattner706d2d32006-08-09 16:44:44 +0000701 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000702 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000703 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000704 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000705 }
Evan Cheng676d7312006-08-26 00:59:04 +0000706 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000707 }
Evan Cheng676d7312006-08-26 00:59:04 +0000708 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000709 }
Evan Chengb915f312005-12-09 22:45:35 +0000710 if (N->isLeaf()) {
711 // If this is an explicit register reference, handle it.
712 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
713 unsigned ResNo = TmpNo++;
714 if (DI->getDef()->isSubClassOf("Register")) {
Evan Cheng676d7312006-08-26 00:59:04 +0000715 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000716 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000717 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000718 NodeOps.push_back("Tmp" + utostr(ResNo));
719 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000720 } else if (DI->getDef()->getName() == "zero_reg") {
721 emitCode("SDOperand Tmp" + utostr(ResNo) +
722 " = CurDAG->getRegister(0, " +
723 getEnumName(N->getTypeNum(0)) + ");");
724 NodeOps.push_back("Tmp" + utostr(ResNo));
725 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000726 }
727 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
728 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000729 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Evan Cheng676d7312006-08-26 00:59:04 +0000730 emitCode("SDOperand Tmp" + utostr(ResNo) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000731 " = CurDAG->getTargetConstant(" + itostr(II->getValue()) +
Evan Cheng2618d072006-05-17 20:37:59 +0000732 ", " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000733 NodeOps.push_back("Tmp" + utostr(ResNo));
734 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000735 }
736
Jim Laskey16d42c62006-07-11 18:25:13 +0000737#ifndef NDEBUG
738 N->dump();
739#endif
Evan Chengb915f312005-12-09 22:45:35 +0000740 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000741 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000742 }
743
744 Record *Op = N->getOperator();
745 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000746 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000747 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000748 const DAGInstruction &Inst = CGP.getInstruction(Op);
Evan Cheng045953c2006-05-10 00:05:46 +0000749 TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000750 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000751 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000752 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
753 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000754 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000755 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000756 }
Evan Chenge945f4d2006-06-14 22:22:20 +0000757 bool HasVarOps = isRoot && II.hasVariableNumberOfOperands;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000758 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000759 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000760 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000761 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000762 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000763 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000764 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000765 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000766 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000767 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000768 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000769 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000770 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000771 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000772 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000773
Evan Chengfceb57a2006-07-15 08:45:20 +0000774 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000775 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000776 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000777 }
Evan Chenge945f4d2006-06-14 22:22:20 +0000778 if (HasVarOps)
Evan Chengf037ca62006-08-27 08:11:28 +0000779 emitCode("SmallVector<SDOperand, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000780
Evan Cheng823b7522006-01-19 21:57:10 +0000781 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000782 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000783 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
784 MVT::ValueType VT = Pattern->getTypeNum(i);
785 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000786 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000787 }
788
Evan Cheng4326ef52006-10-12 02:08:53 +0000789 if (OrigChains.size() > 0) {
790 // The original input chain is being ignored. If it is not just
791 // pointing to the op that's being folded, we should create a
792 // TokenFactor with it and the chain of the folded op as the new chain.
793 // We could potentially be doing multiple levels of folding, in that
794 // case, the TokenFactor can have more operands.
795 emitCode("SmallVector<SDOperand, 8> InChains;");
796 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
797 emitCode("if (" + OrigChains[i].first + ".Val != " +
798 OrigChains[i].second + ".Val) {");
799 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
800 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
801 emitCode("}");
802 }
803 emitCode("AddToISelQueue(" + ChainName + ");");
804 emitCode("InChains.push_back(" + ChainName + ");");
805 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
806 "&InChains[0], InChains.size());");
807 }
808
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000809 // Loop over all of the operands of the instruction pattern, emitting code
810 // to fill them all in. The node 'N' usually has number children equal to
811 // the number of input operands of the instruction. However, in cases
812 // where there are predicate operands for an instruction, we need to fill
813 // in the 'execute always' values. Match up the node operands to the
814 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000815 std::vector<std::string> AllOps;
Evan Cheng39376d02007-05-15 01:19:51 +0000816 unsigned NumEAInputs = 0; // # of synthesized 'execute always' inputs.
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000817 for (unsigned ChildNo = 0, InstOpNo = NumResults;
818 InstOpNo != II.OperandList.size(); ++InstOpNo) {
819 std::vector<std::string> Ops;
820
Evan Cheng59039632007-05-08 21:04:07 +0000821 // If this is a normal operand or a predicate operand without
822 // 'execute always', emit it.
823 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Evan Chenga9559392007-07-06 01:05:26 +0000824 if ((!OperandNode->isSubClassOf("PredicateOperand") &&
825 !OperandNode->isSubClassOf("OptionalDefOperand")) ||
Chris Lattner6cefb772008-01-05 22:25:12 +0000826 CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Evan Cheng30729b42007-09-17 22:26:41 +0000827 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000828 InFlagDecled, ResNodeDecled);
829 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
830 ++ChildNo;
831 } else {
Evan Chenga9559392007-07-06 01:05:26 +0000832 // Otherwise, this is a predicate or optional def operand, emit the
833 // 'default ops' operands.
834 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +0000835 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +0000836 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +0000837 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000838 InFlagDecled, ResNodeDecled);
839 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
Evan Cheng39376d02007-05-15 01:19:51 +0000840 NumEAInputs += Ops.size();
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000841 }
842 }
Evan Chengb915f312005-12-09 22:45:35 +0000843 }
844
Evan Chengb915f312005-12-09 22:45:35 +0000845 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +0000846 bool ChainEmitted = NodeHasChain;
847 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +0000848 emitCode("AddToISelQueue(" + ChainName + ");");
Evan Chengbc6b86a2006-06-14 19:27:50 +0000849 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +0000850 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
851 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +0000852 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +0000853 if (!InFlagDecled) {
854 emitCode("SDOperand InFlag(0, 0);");
855 InFlagDecled = true;
856 }
Evan Chengf037ca62006-08-27 08:11:28 +0000857 if (NodeHasOptInFlag) {
858 emitCode("if (HasInFlag) {");
859 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
860 emitCode(" AddToISelQueue(InFlag);");
861 emitCode("}");
862 }
Evan Chengbc6b86a2006-06-14 19:27:50 +0000863 }
Evan Chengb915f312005-12-09 22:45:35 +0000864
Evan Chengb915f312005-12-09 22:45:35 +0000865 unsigned ResNo = TmpNo++;
Evan Cheng3eff89b2006-05-10 02:47:57 +0000866 if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag ||
Evan Chengef61ed32007-09-07 23:59:02 +0000867 NodeHasOptInFlag || HasImpResults) {
Evan Chenge945f4d2006-06-14 22:22:20 +0000868 std::string Code;
869 std::string Code2;
870 std::string NodeName;
871 if (!isRoot) {
872 NodeName = "Tmp" + utostr(ResNo);
Dan Gohmana6a1ab32007-07-24 22:58:00 +0000873 Code2 = "SDOperand " + NodeName + "(";
Evan Cheng9789aaa2006-01-24 20:46:50 +0000874 } else {
Evan Chenge945f4d2006-06-14 22:22:20 +0000875 NodeName = "ResNode";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +0000876 if (!ResNodeDecled) {
Evan Cheng676d7312006-08-26 00:59:04 +0000877 Code2 = "SDNode *" + NodeName + " = ";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +0000878 ResNodeDecled = true;
879 } else
Evan Cheng676d7312006-08-26 00:59:04 +0000880 Code2 = NodeName + " = ";
Evan Chengbcecf332005-12-17 01:19:28 +0000881 }
Evan Chengf037ca62006-08-27 08:11:28 +0000882
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000883 Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
Evan Chengf037ca62006-08-27 08:11:28 +0000884 unsigned OpsNo = OpcNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000885 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
Evan Chenge945f4d2006-06-14 22:22:20 +0000886
887 // Output order: results, chain, flags
888 // Result types.
Evan Chengf8729402006-07-16 06:12:52 +0000889 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
890 Code += ", VT" + utostr(VTNo);
891 emitVT(getEnumName(N->getTypeNum(0)));
892 }
Evan Chengef61ed32007-09-07 23:59:02 +0000893 // Add types for implicit results in physical registers, scheduler will
894 // care of adding copyfromreg nodes.
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000895 for (unsigned i = 0; i < NumDstRegs; i++) {
896 Record *RR = DstRegs[i];
897 if (RR->isSubClassOf("Register")) {
898 MVT::ValueType RVT = getRegisterValueType(RR, CGT);
899 Code += ", " + getEnumName(RVT);
Evan Chengef61ed32007-09-07 23:59:02 +0000900 }
901 }
Evan Chenge945f4d2006-06-14 22:22:20 +0000902 if (NodeHasChain)
903 Code += ", MVT::Other";
904 if (NodeHasOutFlag)
905 Code += ", MVT::Flag";
906
Chris Lattner7c3a96b2006-11-14 18:41:38 +0000907 // Figure out how many fixed inputs the node has. This is important to
908 // know which inputs are the variable ones if present.
909 unsigned NumInputs = AllOps.size();
910 NumInputs += NodeHasChain;
911
Evan Chenge945f4d2006-06-14 22:22:20 +0000912 // Inputs.
Evan Chengf037ca62006-08-27 08:11:28 +0000913 if (HasVarOps) {
914 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
915 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
916 AllOps.clear();
Evan Chenge945f4d2006-06-14 22:22:20 +0000917 }
918
919 if (HasVarOps) {
Chris Lattner7c3a96b2006-11-14 18:41:38 +0000920 // Figure out whether any operands at the end of the op list are not
921 // part of the variable section.
922 std::string EndAdjust;
Evan Chenge945f4d2006-06-14 22:22:20 +0000923 if (NodeHasInFlag || HasImpInputs)
Chris Lattner7c3a96b2006-11-14 18:41:38 +0000924 EndAdjust = "-1"; // Always has one flag.
925 else if (NodeHasOptInFlag)
926 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
927
Evan Cheng39376d02007-05-15 01:19:51 +0000928 emitCode("for (unsigned i = " + utostr(NumInputs - NumEAInputs) +
Chris Lattner7c3a96b2006-11-14 18:41:38 +0000929 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
930
Evan Cheng676d7312006-08-26 00:59:04 +0000931 emitCode(" AddToISelQueue(N.getOperand(i));");
Evan Chengf037ca62006-08-27 08:11:28 +0000932 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
Evan Chenge945f4d2006-06-14 22:22:20 +0000933 emitCode("}");
934 }
935
936 if (NodeHasChain) {
937 if (HasVarOps)
Evan Chengf037ca62006-08-27 08:11:28 +0000938 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +0000939 else
Evan Chengf037ca62006-08-27 08:11:28 +0000940 AllOps.push_back(ChainName);
Evan Chenge945f4d2006-06-14 22:22:20 +0000941 }
942
Evan Chengf037ca62006-08-27 08:11:28 +0000943 if (HasVarOps) {
944 if (NodeHasInFlag || HasImpInputs)
945 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
946 else if (NodeHasOptInFlag) {
947 emitCode("if (HasInFlag)");
948 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
949 }
950 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
951 ".size()";
952 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000953 AllOps.push_back("InFlag");
Evan Chenge945f4d2006-06-14 22:22:20 +0000954
Evan Chengf037ca62006-08-27 08:11:28 +0000955 unsigned NumOps = AllOps.size();
956 if (NumOps) {
957 if (!NodeHasOptInFlag && NumOps < 4) {
958 for (unsigned i = 0; i != NumOps; ++i)
959 Code += ", " + AllOps[i];
960 } else {
961 std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { ";
962 for (unsigned i = 0; i != NumOps; ++i) {
963 OpsCode += AllOps[i];
964 if (i != NumOps-1)
965 OpsCode += ", ";
966 }
967 emitCode(OpsCode + " };");
968 Code += ", Ops" + utostr(OpsNo) + ", ";
969 if (NodeHasOptInFlag) {
970 Code += "HasInFlag ? ";
971 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
972 } else
973 Code += utostr(NumOps);
974 }
975 }
976
Evan Chenge945f4d2006-06-14 22:22:20 +0000977 if (!isRoot)
978 Code += "), 0";
979 emitCode(Code2 + Code + ");");
980
981 if (NodeHasChain)
982 // Remember which op produces the chain.
983 if (!isRoot)
984 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000985 ".Val, " + utostr(NumResults+NumDstRegs) + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +0000986 else
987 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000988 ", " + utostr(NumResults+NumDstRegs) + ");");
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000989
Evan Cheng676d7312006-08-26 00:59:04 +0000990 if (!isRoot) {
991 NodeOps.push_back("Tmp" + utostr(ResNo));
992 return NodeOps;
993 }
Evan Cheng045953c2006-05-10 00:05:46 +0000994
Evan Cheng06d64702006-08-11 08:59:35 +0000995 bool NeedReplace = false;
Evan Cheng676d7312006-08-26 00:59:04 +0000996 if (NodeHasOutFlag) {
997 if (!InFlagDecled) {
Dan Gohmana6a1ab32007-07-24 22:58:00 +0000998 emitCode("SDOperand InFlag(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +0000999 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001000 InFlagDecled = true;
1001 } else
1002 emitCode("InFlag = SDOperand(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001003 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001004 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001005
Evan Cheng97938882005-12-22 02:24:50 +00001006 if (FoldedChains.size() > 0) {
Chris Lattner8a0604b2006-01-28 20:31:24 +00001007 std::string Code;
Evan Cheng1b80f4d2005-12-19 07:18:51 +00001008 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++)
Chris Lattner706d2d32006-08-09 16:44:44 +00001009 emitCode("ReplaceUses(SDOperand(" +
Evan Cheng67212a02006-02-09 22:12:27 +00001010 FoldedChains[j].first + ".Val, " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001011 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001012 utostr(NumResults+NumDstRegs) + "));");
Evan Cheng06d64702006-08-11 08:59:35 +00001013 NeedReplace = true;
Evan Chengb915f312005-12-09 22:45:35 +00001014 }
Evan Chengf9fc25d2005-12-19 22:40:04 +00001015
Evan Cheng06d64702006-08-11 08:59:35 +00001016 if (NodeHasOutFlag) {
Chris Lattner706d2d32006-08-09 16:44:44 +00001017 emitCode("ReplaceUses(SDOperand(N.Val, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001018 utostr(NumPatResults + (unsigned)InputHasChain)
1019 +"), InFlag);");
Evan Cheng06d64702006-08-11 08:59:35 +00001020 NeedReplace = true;
1021 }
1022
Evan Cheng30729b42007-09-17 22:26:41 +00001023 if (NeedReplace && InputHasChain)
1024 emitCode("ReplaceUses(SDOperand(N.Val, " +
1025 utostr(NumPatResults) + "), SDOperand(" + ChainName
1026 + ".Val, " + ChainName + ".ResNo" + "));");
Evan Cheng97938882005-12-22 02:24:50 +00001027
Evan Chenged66e852006-03-09 08:19:11 +00001028 // User does not expect the instruction would produce a chain!
Evan Cheng06d64702006-08-11 08:59:35 +00001029 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Evan Cheng9ade2182006-08-26 05:34:46 +00001030 ;
Evan Cheng3eff89b2006-05-10 02:47:57 +00001031 } else if (InputHasChain && !NodeHasChain) {
1032 // One of the inner node produces a chain.
Evan Cheng9ade2182006-08-26 05:34:46 +00001033 if (NodeHasOutFlag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001034 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) +
Evan Cheng06d64702006-08-11 08:59:35 +00001035 "), SDOperand(ResNode, N.ResNo-1));");
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001036 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) +
Evan Cheng06d64702006-08-11 08:59:35 +00001037 "), " + ChainName + ");");
Evan Cheng4fba2812005-12-20 07:37:41 +00001038 }
Evan Cheng06d64702006-08-11 08:59:35 +00001039
Evan Cheng30729b42007-09-17 22:26:41 +00001040 emitCode("return ResNode;");
Evan Chengb915f312005-12-09 22:45:35 +00001041 } else {
Evan Cheng9ade2182006-08-26 05:34:46 +00001042 std::string Code = "return CurDAG->SelectNodeTo(N.Val, Opc" +
Evan Chengfceb57a2006-07-15 08:45:20 +00001043 utostr(OpcNo);
Nate Begemanb73628b2005-12-30 00:12:56 +00001044 if (N->getTypeNum(0) != MVT::isVoid)
Evan Chengf8729402006-07-16 06:12:52 +00001045 Code += ", VT" + utostr(VTNo);
Evan Cheng54597732006-01-26 00:22:25 +00001046 if (NodeHasOutFlag)
Chris Lattner8a0604b2006-01-28 20:31:24 +00001047 Code += ", MVT::Flag";
Evan Chengf037ca62006-08-27 08:11:28 +00001048
1049 if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1050 AllOps.push_back("InFlag");
1051
1052 unsigned NumOps = AllOps.size();
1053 if (NumOps) {
1054 if (!NodeHasOptInFlag && NumOps < 4) {
1055 for (unsigned i = 0; i != NumOps; ++i)
1056 Code += ", " + AllOps[i];
1057 } else {
1058 std::string OpsCode = "SDOperand Ops" + utostr(OpcNo) + "[] = { ";
1059 for (unsigned i = 0; i != NumOps; ++i) {
1060 OpsCode += AllOps[i];
1061 if (i != NumOps-1)
1062 OpsCode += ", ";
1063 }
1064 emitCode(OpsCode + " };");
1065 Code += ", Ops" + utostr(OpcNo) + ", ";
1066 Code += utostr(NumOps);
1067 }
1068 }
Evan Cheng95514ba2006-08-26 08:00:10 +00001069 emitCode(Code + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001070 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1071 if (N->getTypeNum(0) != MVT::isVoid)
1072 emitVT(getEnumName(N->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001073 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001074
Evan Cheng676d7312006-08-26 00:59:04 +00001075 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001076 } else if (Op->isSubClassOf("SDNodeXForm")) {
1077 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001078 // PatLeaf node - the operand may or may not be a leaf node. But it should
1079 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001080 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001081 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001082 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001083 unsigned ResNo = TmpNo++;
Evan Cheng676d7312006-08-26 00:59:04 +00001084 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
1085 + "(" + Ops.back() + ".Val);");
1086 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001087 if (isRoot)
1088 emitCode("return Tmp" + utostr(ResNo) + ".Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001089 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001090 } else {
1091 N->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001092 cerr << "\n";
Chris Lattner7893f132006-01-11 01:33:49 +00001093 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001094 }
1095 }
1096
Chris Lattner488580c2006-01-28 19:06:51 +00001097 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1098 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001099 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1100 /// for, this returns true otherwise false if Pat has all types.
1101 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001102 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001103 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001104 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001105 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001106 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001107 // The top level node type is checked outside of the select function.
1108 if (!isRoot)
1109 emitCheck(Prefix + ".Val->getValueType(0) == " +
1110 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001111 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001112 }
1113
Evan Cheng51fecc82006-01-09 18:27:06 +00001114 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001115 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001116 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1117 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1118 Prefix + utostr(OpNo)))
1119 return true;
1120 return false;
1121 }
1122
1123private:
Evan Cheng54597732006-01-26 00:22:25 +00001124 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001125 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001126 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001127 bool &ChainEmitted, bool &InFlagDecled,
1128 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001129 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001130 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001131 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1132 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001133 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1134 TreePatternNode *Child = N->getChild(i);
1135 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001136 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1137 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001138 } else {
1139 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001140 if (!Child->getName().empty()) {
1141 std::string Name = RootName + utostr(OpNo);
1142 if (Duplicates.find(Name) != Duplicates.end())
1143 // A duplicate! Do not emit a copy for this node.
1144 continue;
1145 }
1146
Evan Chengb915f312005-12-09 22:45:35 +00001147 Record *RR = DI->getDef();
1148 if (RR->isSubClassOf("Register")) {
1149 MVT::ValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001150 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001151 if (!InFlagDecled) {
1152 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
1153 InFlagDecled = true;
1154 } else
1155 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1156 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001157 } else {
1158 if (!ChainEmitted) {
Evan Cheng676d7312006-08-26 00:59:04 +00001159 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001160 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001161 ChainEmitted = true;
1162 }
Evan Cheng676d7312006-08-26 00:59:04 +00001163 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1164 if (!InFlagDecled) {
1165 emitCode("SDOperand InFlag(0, 0);");
1166 InFlagDecled = true;
1167 }
1168 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1169 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001170 ", " + getQualifiedName(RR) +
Evan Cheng7a33db02006-08-26 07:39:28 +00001171 ", " + RootName + utostr(OpNo) + ", InFlag).Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001172 ResNodeDecled = true;
Evan Cheng67212a02006-02-09 22:12:27 +00001173 emitCode(ChainName + " = SDOperand(ResNode, 0);");
1174 emitCode("InFlag = SDOperand(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001175 }
1176 }
1177 }
1178 }
1179 }
Evan Cheng54597732006-01-26 00:22:25 +00001180
Evan Cheng676d7312006-08-26 00:59:04 +00001181 if (HasInFlag) {
1182 if (!InFlagDecled) {
1183 emitCode("SDOperand InFlag = " + RootName +
1184 ".getOperand(" + utostr(OpNo) + ");");
1185 InFlagDecled = true;
1186 } else
1187 emitCode("InFlag = " + RootName +
1188 ".getOperand(" + utostr(OpNo) + ");");
1189 emitCode("AddToISelQueue(InFlag);");
1190 }
Evan Chengb915f312005-12-09 22:45:35 +00001191 }
1192};
1193
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001194/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1195/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001196/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001197void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001198 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001199 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001200 std::vector<std::string> &TargetOpcodes,
Evan Chengf5493192006-08-26 01:02:19 +00001201 std::vector<std::string> &TargetVTs) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001202 PatternCodeEmitter Emitter(*CGP, Pattern.getPredicates(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001203 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001204 GeneratedCode, GeneratedDecl,
Chris Lattner706d2d32006-08-09 16:44:44 +00001205 TargetOpcodes, TargetVTs);
Evan Chengb915f312005-12-09 22:45:35 +00001206
Chris Lattner8fc35682005-09-23 23:16:51 +00001207 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001208 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001209 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001210
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001211 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner6cefb772008-01-05 22:25:12 +00001212 TreePattern &TP = *CGP->pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001213
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001214 // At this point, we know that we structurally match the pattern, but the
1215 // types of the nodes may not match. Figure out the fewest number of type
1216 // comparisons we need to emit. For example, if there is only one integer
1217 // type supported by a target, there should be no type comparisons at all for
1218 // integer patterns!
1219 //
1220 // To figure out the fewest number of type checks needed, clone the pattern,
1221 // remove the types, then perform type inference on the pattern as a whole.
1222 // If there are unresolved types, emit an explicit check for those types,
1223 // apply the type to the tree, then rerun type inference. Iterate until all
1224 // types are resolved.
1225 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001226 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001227 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001228
1229 do {
1230 // Resolve/propagate as many types as possible.
1231 try {
1232 bool MadeChange = true;
1233 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001234 MadeChange = Pat->ApplyTypeConstraints(TP,
1235 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001236 } catch (...) {
1237 assert(0 && "Error: could not find consistent types for something we"
1238 " already decided was ok!");
1239 abort();
1240 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001241
Chris Lattner7e82f132005-10-15 21:34:21 +00001242 // Insert a check for an unresolved type and add it to the tree. If we find
1243 // an unresolved type to add a check for, this returns true and we iterate,
1244 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001245 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001246
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001247 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001248 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001249 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001250}
1251
Chris Lattner24e00a42006-01-29 04:41:05 +00001252/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1253/// a line causes any of them to be empty, remove them and return true when
1254/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001255static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001256 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001257 &Patterns) {
1258 bool ErasedPatterns = false;
1259 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1260 Patterns[i].second.pop_back();
1261 if (Patterns[i].second.empty()) {
1262 Patterns.erase(Patterns.begin()+i);
1263 --i; --e;
1264 ErasedPatterns = true;
1265 }
1266 }
1267 return ErasedPatterns;
1268}
1269
Chris Lattner8bc74722006-01-29 04:25:26 +00001270/// EmitPatterns - Emit code for at least one pattern, but try to group common
1271/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001272void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001273 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001274 &Patterns, unsigned Indent,
1275 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001276 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001277 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001278 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001279
1280 if (Patterns.empty()) return;
1281
Chris Lattner24e00a42006-01-29 04:41:05 +00001282 // Figure out how many patterns share the next code line. Explicitly copy
1283 // FirstCodeLine so that we don't invalidate a reference when changing
1284 // Patterns.
1285 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001286 unsigned LastMatch = Patterns.size()-1;
1287 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1288 --LastMatch;
1289
1290 // If not all patterns share this line, split the list into two pieces. The
1291 // first chunk will use this line, the second chunk won't.
1292 if (LastMatch != 0) {
1293 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1294 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1295
1296 // FIXME: Emit braces?
1297 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001298 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001299 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1300 Pattern.getSrcPattern()->print(OS);
1301 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1302 Pattern.getDstPattern()->print(OS);
1303 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001304 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001305 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001306 << getPatternSize(Pattern.getSrcPattern(), *CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001307 << " cost = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001308 << getResultPatternCost(Pattern.getDstPattern(), *CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001309 << " size = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001310 << getResultPatternSize(Pattern.getDstPattern(), *CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001311 }
Evan Cheng676d7312006-08-26 00:59:04 +00001312 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001313 OS << std::string(Indent, ' ') << "{\n";
1314 Indent += 2;
1315 }
1316 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001317 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001318 Indent -= 2;
1319 OS << std::string(Indent, ' ') << "}\n";
1320 }
1321
1322 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001323 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001324 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1325 Pattern.getSrcPattern()->print(OS);
1326 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1327 Pattern.getDstPattern()->print(OS);
1328 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001329 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001330 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001331 << getPatternSize(Pattern.getSrcPattern(), *CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001332 << " cost = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001333 << getResultPatternCost(Pattern.getDstPattern(), *CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001334 << " size = "
Chris Lattner6cefb772008-01-05 22:25:12 +00001335 << getResultPatternSize(Pattern.getDstPattern(), *CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001336 }
1337 EmitPatterns(Other, Indent, OS);
1338 return;
1339 }
1340
Chris Lattner24e00a42006-01-29 04:41:05 +00001341 // Remove this code from all of the patterns that share it.
1342 bool ErasedPatterns = EraseCodeLine(Patterns);
1343
Evan Cheng676d7312006-08-26 00:59:04 +00001344 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001345
1346 // Otherwise, every pattern in the list has this line. Emit it.
1347 if (!isPredicate) {
1348 // Normal code.
1349 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1350 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001351 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1352
1353 // If the next code line is another predicate, and if all of the pattern
1354 // in this group share the same next line, emit it inline now. Do this
1355 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001356 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001357 // Check that all of fhe patterns in Patterns end with the same predicate.
1358 bool AllEndWithSamePredicate = true;
1359 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1360 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1361 AllEndWithSamePredicate = false;
1362 break;
1363 }
1364 // If all of the predicates aren't the same, we can't share them.
1365 if (!AllEndWithSamePredicate) break;
1366
1367 // Otherwise we can. Emit it shared now.
1368 OS << " &&\n" << std::string(Indent+4, ' ')
1369 << Patterns.back().second.back().second;
1370 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001371 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001372
1373 OS << ") {\n";
1374 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001375 }
1376
1377 EmitPatterns(Patterns, Indent, OS);
1378
1379 if (isPredicate)
1380 OS << std::string(Indent-2, ' ') << "}\n";
1381}
1382
Chris Lattner6cefb772008-01-05 22:25:12 +00001383static std::string getOpcodeName(Record *Op, CodegenDAGPatterns &CGP) {
1384 return CGP.getSDNodeInfo(Op).getEnumName();
Evan Cheng892aaf82006-11-08 23:01:03 +00001385}
Chris Lattner8bc74722006-01-29 04:25:26 +00001386
Evan Cheng892aaf82006-11-08 23:01:03 +00001387static std::string getLegalCName(std::string OpName) {
1388 std::string::size_type pos = OpName.find("::");
1389 if (pos != std::string::npos)
1390 OpName.replace(pos, 2, "_");
1391 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001392}
1393
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001394void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001395 const CodeGenTarget &Target = CGP->getTargetInfo();
1396
Chris Lattnerf7560ed2006-11-20 18:54:33 +00001397 // Get the namespace to insert instructions into. Make sure not to pick up
1398 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
1399 // instruction or something.
1400 std::string InstNS;
1401 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
1402 e = Target.inst_end(); i != e; ++i) {
1403 InstNS = i->second.Namespace;
1404 if (InstNS != "TargetInstrInfo")
1405 break;
1406 }
1407
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001408 if (!InstNS.empty()) InstNS += "::";
1409
Chris Lattner602f6922006-01-04 00:25:00 +00001410 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001411 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001412 // All unique target node emission functions.
1413 std::map<std::string, unsigned> EmitFunctions;
Chris Lattner6cefb772008-01-05 22:25:12 +00001414 for (CodegenDAGPatterns::ptm_iterator I = CGP->ptm_begin(),
1415 E = CGP->ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001416 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001417
1418 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001419 if (!Node->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001420 PatternsByOpcode[getOpcodeName(Node->getOperator(), *CGP)].
1421 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001422 } else {
1423 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001424 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001425 PatternsByOpcode[getOpcodeName(CGP->getSDNodeNamed("imm"), *CGP)].
1426 push_back(&Pattern);
1427 } else if ((CP = NodeGetComplexPattern(Node, *CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001428 std::vector<Record*> OpNodes = CP->getRootNodes();
1429 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001430 PatternsByOpcode[getOpcodeName(OpNodes[j], *CGP)]
1431 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], *CGP)].begin(),
1432 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001433 }
1434 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001435 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001436 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001437 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001438 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001439 exit(1);
1440 }
1441 }
1442 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001443
1444 // For each opcode, there might be multiple select functions, one per
1445 // ValueType of the node (or its first operand if it doesn't produce a
1446 // non-chain result.
1447 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1448
Chris Lattner602f6922006-01-04 00:25:00 +00001449 // Emit one Select_* method for each top-level opcode. We do this instead of
1450 // emitting one giant switch statement to support compilers where this will
1451 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001452 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001453 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1454 PBOI != E; ++PBOI) {
1455 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001456 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001457 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1458
Chris Lattner602f6922006-01-04 00:25:00 +00001459 // We want to emit all of the matching code now. However, we want to emit
1460 // the matches in order of minimal cost. Sort the patterns so the least
1461 // cost one is at the start.
Chris Lattner706d2d32006-08-09 16:44:44 +00001462 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001463 PatternSortingPredicate(*CGP));
Evan Cheng21ad3922006-02-07 00:37:41 +00001464
Chris Lattner706d2d32006-08-09 16:44:44 +00001465 // Split them into groups by type.
Chris Lattner60d81392008-01-05 22:30:17 +00001466 std::map<MVT::ValueType, std::vector<const PatternToMatch*> >PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001467 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001468 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001469 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner706d2d32006-08-09 16:44:44 +00001470 MVT::ValueType VT = SrcPat->getTypeNum(0);
Chris Lattner60d81392008-01-05 22:30:17 +00001471 std::map<MVT::ValueType,
1472 std::vector<const PatternToMatch*> >::iterator TI =
Chris Lattner706d2d32006-08-09 16:44:44 +00001473 PatternsByType.find(VT);
1474 if (TI != PatternsByType.end())
1475 TI->second.push_back(Pat);
1476 else {
Chris Lattner60d81392008-01-05 22:30:17 +00001477 std::vector<const PatternToMatch*> PVec;
Chris Lattner706d2d32006-08-09 16:44:44 +00001478 PVec.push_back(Pat);
1479 PatternsByType.insert(std::make_pair(VT, PVec));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001480 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001481 }
1482
Chris Lattner60d81392008-01-05 22:30:17 +00001483 for (std::map<MVT::ValueType, std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001484 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1485 ++II) {
1486 MVT::ValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001487 std::vector<const PatternToMatch*> &Patterns = II->second;
Chris Lattner64906972006-09-21 18:28:27 +00001488 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1489 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001490
Chris Lattner60d81392008-01-05 22:30:17 +00001491 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001492 std::vector<std::vector<std::string> > PatternOpcodes;
1493 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001494 std::vector<std::set<std::string> > PatternDecls;
Chris Lattner706d2d32006-08-09 16:44:44 +00001495 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1496 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001497 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001498 std::vector<std::string> TargetOpcodes;
1499 std::vector<std::string> TargetVTs;
1500 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
1501 TargetOpcodes, TargetVTs);
Chris Lattner706d2d32006-08-09 16:44:44 +00001502 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1503 PatternDecls.push_back(GeneratedDecl);
1504 PatternOpcodes.push_back(TargetOpcodes);
1505 PatternVTs.push_back(TargetVTs);
1506 }
1507
1508 // Scan the code to see if all of the patterns are reachable and if it is
1509 // possible that the last one might not match.
1510 bool mightNotMatch = true;
1511 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1512 CodeList &GeneratedCode = CodeForPatterns[i].second;
1513 mightNotMatch = false;
1514
1515 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001516 if (GeneratedCode[j].first == 1) { // predicate.
Chris Lattner706d2d32006-08-09 16:44:44 +00001517 mightNotMatch = true;
1518 break;
1519 }
1520 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001521
Chris Lattner706d2d32006-08-09 16:44:44 +00001522 // If this pattern definitely matches, and if it isn't the last one, the
1523 // patterns after it CANNOT ever match. Error out.
1524 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001525 cerr << "Pattern '";
1526 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1527 cerr << "' is impossible to select!\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001528 exit(1);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001529 }
1530 }
1531
Chris Lattner706d2d32006-08-09 16:44:44 +00001532 // Factor target node emission code (emitted by EmitResultCode) into
1533 // separate functions. Uniquing and share them among all instruction
1534 // selection routines.
1535 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1536 CodeList &GeneratedCode = CodeForPatterns[i].second;
1537 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1538 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001539 std::set<std::string> Decls = PatternDecls[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001540 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001541 int CodeSize = (int)GeneratedCode.size();
1542 int LastPred = -1;
1543 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001544 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001545 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001546 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1547 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001548 }
1549
Evan Cheng9ade2182006-08-26 05:34:46 +00001550 std::string CalleeCode = "(const SDOperand &N";
1551 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001552 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1553 CalleeCode += ", unsigned Opc" + utostr(j);
1554 CallerCode += ", " + TargetOpcodes[j];
1555 }
1556 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
1557 CalleeCode += ", MVT::ValueType VT" + utostr(j);
1558 CallerCode += ", " + TargetVTs[j];
1559 }
Evan Chengf5493192006-08-26 01:02:19 +00001560 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001561 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001562 std::string Name = *I;
Evan Cheng676d7312006-08-26 00:59:04 +00001563 CalleeCode += ", SDOperand &" + Name;
1564 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001565 }
1566 CallerCode += ");";
1567 CalleeCode += ") ";
1568 // Prevent emission routines from being inlined to reduce selection
1569 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001570 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001571 CalleeCode += "{\n";
1572
1573 for (std::vector<std::string>::const_reverse_iterator
1574 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1575 CalleeCode += " " + *I + "\n";
1576
Evan Chengf5493192006-08-26 01:02:19 +00001577 for (int j = LastPred+1; j < CodeSize; ++j)
1578 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001579 for (int j = LastPred+1; j < CodeSize; ++j)
1580 GeneratedCode.pop_back();
1581 CalleeCode += "}\n";
1582
1583 // Uniquing the emission routines.
1584 unsigned EmitFuncNum;
1585 std::map<std::string, unsigned>::iterator EFI =
1586 EmitFunctions.find(CalleeCode);
1587 if (EFI != EmitFunctions.end()) {
1588 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001589 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001590 EmitFuncNum = EmitFunctions.size();
1591 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001592 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001593 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001594
Chris Lattner706d2d32006-08-09 16:44:44 +00001595 // Replace the emission code within selection routines with calls to the
1596 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001597 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001598 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001599 }
1600
Chris Lattner706d2d32006-08-09 16:44:44 +00001601 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001602 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001603 if (OpVT == MVT::iPTR) {
1604 OpVTStr = "_iPTR";
1605 } else if (OpVT == MVT::isVoid) {
1606 // Nodes with a void result actually have a first result type of either
1607 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1608 // void to this case, we handle it specially here.
1609 } else {
1610 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1611 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001612 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1613 OpcodeVTMap.find(OpName);
1614 if (OpVTI == OpcodeVTMap.end()) {
1615 std::vector<std::string> VTSet;
1616 VTSet.push_back(OpVTStr);
1617 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1618 } else
1619 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001620
Evan Cheng892aaf82006-11-08 23:01:03 +00001621 OS << "SDNode *Select_" << getLegalCName(OpName)
Chris Lattner33a40042006-11-14 22:17:10 +00001622 << OpVTStr << "(const SDOperand &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001623
Chris Lattner706d2d32006-08-09 16:44:44 +00001624 // Loop through and reverse all of the CodeList vectors, as we will be
1625 // accessing them from their logical front, but accessing the end of a
1626 // vector is more efficient.
1627 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1628 CodeList &GeneratedCode = CodeForPatterns[i].second;
1629 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001630 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001631
1632 // Next, reverse the list of patterns itself for the same reason.
1633 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1634
1635 // Emit all of the patterns now, grouped together to share code.
1636 EmitPatterns(CodeForPatterns, 2, OS);
1637
Chris Lattner64906972006-09-21 18:28:27 +00001638 // If the last pattern has predicates (which could fail) emit code to
1639 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001640 if (mightNotMatch) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001641 OS << " cerr << \"Cannot yet select: \";\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001642 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1643 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1644 OpName != "ISD::INTRINSIC_VOID") {
Chris Lattner706d2d32006-08-09 16:44:44 +00001645 OS << " N.Val->dump(CurDAG);\n";
1646 } else {
1647 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1648 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001649 << " cerr << \"intrinsic %\"<< "
Chris Lattner706d2d32006-08-09 16:44:44 +00001650 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1651 }
Bill Wendlingf5da1332006-12-07 22:21:48 +00001652 OS << " cerr << '\\n';\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001653 << " abort();\n"
1654 << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001655 }
1656 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001657 }
Chris Lattner602f6922006-01-04 00:25:00 +00001658 }
1659
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001660 // Emit boilerplate.
Evan Cheng9ade2182006-08-26 05:34:46 +00001661 OS << "SDNode *Select_INLINEASM(SDOperand N) {\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001662 << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001663 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n"
1664
1665 << " // Ensure that the asm operands are themselves selected.\n"
1666 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1667 << " AddToISelQueue(Ops[j]);\n\n"
1668
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001669 << " std::vector<MVT::ValueType> VTs;\n"
1670 << " VTs.push_back(MVT::Other);\n"
1671 << " VTs.push_back(MVT::Flag);\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001672 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
1673 "Ops.size());\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001674 << " return New.Val;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001675 << "}\n\n";
1676
Jim Laskeya683f9b2007-01-26 17:29:20 +00001677 OS << "SDNode *Select_LABEL(const SDOperand &N) {\n"
1678 << " SDOperand Chain = N.getOperand(0);\n"
1679 << " SDOperand N1 = N.getOperand(1);\n"
Jim Laskey844b8922007-01-26 23:00:54 +00001680 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1681 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001682 << " AddToISelQueue(Chain);\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001683 << " SDOperand Ops[] = { Tmp, Chain };\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001684 << " return CurDAG->getTargetNode(TargetInstrInfo::LABEL,\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001685 << " MVT::Other, Ops, 2);\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001686 << "}\n\n";
1687
Christopher Lamb08d52072007-07-26 07:48:21 +00001688 OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n"
1689 << " SDOperand N0 = N.getOperand(0);\n"
1690 << " SDOperand N1 = N.getOperand(1);\n"
1691 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1692 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1693 << " AddToISelQueue(N0);\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001694 << " SDOperand Ops[] = { N0, Tmp };\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001695 << " return CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001696 << " N.getValueType(), Ops, 2);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001697 << "}\n\n";
1698
1699 OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n"
1700 << " SDOperand N0 = N.getOperand(0);\n"
1701 << " SDOperand N1 = N.getOperand(1);\n"
1702 << " SDOperand N2 = N.getOperand(2);\n"
1703 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
1704 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1705 << " AddToISelQueue(N1);\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001706 << " SDOperand Ops[] = { N0, N1, Tmp };\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001707 << " if (N0.getOpcode() == ISD::UNDEF) {\n"
Evan Cheng3393f892007-10-12 08:39:02 +00001708 << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001709 << " N.getValueType(), Ops+1, 2);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001710 << " } else {\n"
1711 << " AddToISelQueue(N0);\n"
Evan Cheng3393f892007-10-12 08:39:02 +00001712 << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n"
Chris Lattner44f14762007-10-24 06:25:09 +00001713 << " N.getValueType(), Ops, 3);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001714 << " }\n"
1715 << "}\n\n";
1716
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001717 OS << "// The main instruction selector code.\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001718 << "SDNode *SelectCode(SDOperand N) {\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001719 << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n"
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001720 << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS
Evan Cheng34167212006-02-09 00:37:58 +00001721 << "INSTRUCTION_LIST_END)) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001722 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001723 << " }\n\n"
Evan Cheng892aaf82006-11-08 23:01:03 +00001724 << " MVT::ValueType NVT = N.Val->getValueType(0);\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001725 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001726 << " default: break;\n"
1727 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001728 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001729 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001730 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001731 << " case ISD::TargetConstant:\n"
1732 << " case ISD::TargetConstantPool:\n"
1733 << " case ISD::TargetFrameIndex:\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001734 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001735 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001736 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001737 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001738 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001739 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001740 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001741 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001742 << " AddToISelQueue(N.getOperand(0));\n"
1743 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001744 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001745 << " }\n"
1746 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001747 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001748 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001749 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1750 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001751 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001752 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001753 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001754 << " case ISD::LABEL: return Select_LABEL(N);\n"
1755 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
1756 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001757
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001758
Chris Lattner602f6922006-01-04 00:25:00 +00001759 // Loop over all of the case statements, emiting a call to each method we
1760 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001761 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001762 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1763 PBOI != E; ++PBOI) {
1764 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001765 // Potentially multiple versions of select for this opcode. One for each
1766 // ValueType of the node (or its first true operand if it doesn't produce a
1767 // result.
1768 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1769 OpcodeVTMap.find(OpName);
1770 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001771 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00001772 // Keep track of whether we see a pattern that has an iPtr result.
1773 bool HasPtrPattern = false;
1774 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001775
Evan Cheng425e8c72007-09-04 20:18:28 +00001776 OS << " switch (NVT) {\n";
1777 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1778 std::string &VTStr = OpVTs[i];
1779 if (VTStr.empty()) {
1780 HasDefaultPattern = true;
1781 continue;
1782 }
Chris Lattner717a6112006-11-14 21:50:27 +00001783
Evan Cheng425e8c72007-09-04 20:18:28 +00001784 // If this is a match on iPTR: don't emit it directly, we need special
1785 // code.
1786 if (VTStr == "_iPTR") {
1787 HasPtrPattern = true;
1788 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00001789 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001790 OS << " case MVT::" << VTStr.substr(1) << ":\n"
1791 << " return Select_" << getLegalCName(OpName)
1792 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001793 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001794 OS << " default:\n";
1795
1796 // If there is an iPTR result version of this pattern, emit it here.
1797 if (HasPtrPattern) {
1798 OS << " if (NVT == TLI.getPointerTy())\n";
1799 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
1800 }
1801 if (HasDefaultPattern) {
1802 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
1803 }
1804 OS << " break;\n";
1805 OS << " }\n";
1806 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001807 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00001808 }
Chris Lattner81303322005-09-23 19:36:15 +00001809
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001810 OS << " } // end of big switch.\n\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001811 << " cerr << \"Cannot yet select: \";\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00001812 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
1813 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
1814 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00001815 << " N.Val->dump(CurDAG);\n"
1816 << " } else {\n"
1817 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1818 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001819 << " cerr << \"intrinsic %\"<< "
1820 "Intrinsic::getName((Intrinsic::ID)iid);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00001821 << " }\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001822 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001823 << " abort();\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001824 << " return NULL;\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001825 << "}\n";
1826}
1827
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001828void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001829 CodeGenTarget Target;
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001830 EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() +
1831 " target", OS);
1832
Chris Lattner1f39e292005-09-14 00:09:24 +00001833 OS << "// *** NOTE: This file is #included into the middle of the target\n"
1834 << "// *** instruction selector class. These functions are really "
1835 << "methods.\n\n";
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00001836
Chris Lattner8dc728e2006-08-27 13:16:24 +00001837 OS << "#include \"llvm/Support/Compiler.h\"\n";
Evan Cheng233baf12006-07-26 23:06:27 +00001838
Chris Lattner706d2d32006-08-09 16:44:44 +00001839 OS << "// Instruction selector priority queue:\n"
1840 << "std::vector<SDNode*> ISelQueue;\n";
1841 OS << "/// Keep track of nodes which have already been added to queue.\n"
1842 << "unsigned char *ISelQueued;\n";
1843 OS << "/// Keep track of nodes which have already been selected.\n"
1844 << "unsigned char *ISelSelected;\n";
1845 OS << "/// Dummy parameter to ReplaceAllUsesOfValueWith().\n"
1846 << "std::vector<SDNode*> ISelKilled;\n\n";
Evan Chenge41bf822006-02-05 06:43:12 +00001847
Evan Cheng4326ef52006-10-12 02:08:53 +00001848 OS << "/// IsChainCompatible - Returns true if Chain is Op or Chain does\n";
1849 OS << "/// not reach Op.\n";
1850 OS << "static bool IsChainCompatible(SDNode *Chain, SDNode *Op) {\n";
1851 OS << " if (Chain->getOpcode() == ISD::EntryToken)\n";
1852 OS << " return true;\n";
1853 OS << " else if (Chain->getOpcode() == ISD::TokenFactor)\n";
1854 OS << " return false;\n";
1855 OS << " else if (Chain->getNumOperands() > 0) {\n";
1856 OS << " SDOperand C0 = Chain->getOperand(0);\n";
1857 OS << " if (C0.getValueType() == MVT::Other)\n";
1858 OS << " return C0.Val != Op && IsChainCompatible(C0.Val, Op);\n";
1859 OS << " }\n";
1860 OS << " return true;\n";
1861 OS << "}\n";
1862
Chris Lattner706d2d32006-08-09 16:44:44 +00001863 OS << "/// Sorting functions for the selection queue.\n"
1864 << "struct isel_sort : public std::binary_function"
1865 << "<SDNode*, SDNode*, bool> {\n"
1866 << " bool operator()(const SDNode* left, const SDNode* right) "
1867 << "const {\n"
1868 << " return (left->getNodeId() > right->getNodeId());\n"
1869 << " }\n"
1870 << "};\n\n";
Evan Chenge41bf822006-02-05 06:43:12 +00001871
Chris Lattner706d2d32006-08-09 16:44:44 +00001872 OS << "inline void setQueued(int Id) {\n";
1873 OS << " ISelQueued[Id / 8] |= 1 << (Id % 8);\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001874 OS << "}\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001875 OS << "inline bool isQueued(int Id) {\n";
1876 OS << " return ISelQueued[Id / 8] & (1 << (Id % 8));\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001877 OS << "}\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001878 OS << "inline void setSelected(int Id) {\n";
1879 OS << " ISelSelected[Id / 8] |= 1 << (Id % 8);\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001880 OS << "}\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001881 OS << "inline bool isSelected(int Id) {\n";
1882 OS << " return ISelSelected[Id / 8] & (1 << (Id % 8));\n";
1883 OS << "}\n\n";
Evan Cheng9bdca032006-08-07 22:17:58 +00001884
Chris Lattner8dc728e2006-08-27 13:16:24 +00001885 OS << "void AddToISelQueue(SDOperand N) DISABLE_INLINE {\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001886 OS << " int Id = N.Val->getNodeId();\n";
1887 OS << " if (Id != -1 && !isQueued(Id)) {\n";
1888 OS << " ISelQueue.push_back(N.Val);\n";
1889 OS << " std::push_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
1890 OS << " setQueued(Id);\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001891 OS << " }\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001892 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001893
Chris Lattner706d2d32006-08-09 16:44:44 +00001894 OS << "inline void RemoveKilled() {\n";
1895OS << " unsigned NumKilled = ISelKilled.size();\n";
1896 OS << " if (NumKilled) {\n";
1897 OS << " for (unsigned i = 0; i != NumKilled; ++i) {\n";
1898 OS << " SDNode *Temp = ISelKilled[i];\n";
Evan Cheng4f776162006-10-12 23:18:52 +00001899 OS << " ISelQueue.erase(std::remove(ISelQueue.begin(), ISelQueue.end(), "
1900 << "Temp), ISelQueue.end());\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001901 OS << " };\n";
1902 OS << " std::make_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
1903 OS << " ISelKilled.clear();\n";
1904 OS << " }\n";
1905 OS << "}\n\n";
1906
Chris Lattner8dc728e2006-08-27 13:16:24 +00001907 OS << "void ReplaceUses(SDOperand F, SDOperand T) DISABLE_INLINE {\n";
Chris Lattner01d029b2007-10-15 06:10:22 +00001908 OS << " CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISelKilled);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001909 OS << " setSelected(F.Val->getNodeId());\n";
1910 OS << " RemoveKilled();\n";
Evan Cheng06d64702006-08-11 08:59:35 +00001911 OS << "}\n";
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001912 OS << "void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {\n";
Evan Cheng30729b42007-09-17 22:26:41 +00001913 OS << " unsigned FNumVals = F->getNumValues();\n";
1914 OS << " unsigned TNumVals = T->getNumValues();\n";
1915 OS << " if (FNumVals != TNumVals) {\n";
1916 OS << " for (unsigned i = 0, e = std::min(FNumVals, TNumVals); "
1917 << "i < e; ++i)\n";
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001918 OS << " CurDAG->ReplaceAllUsesOfValueWith(SDOperand(F, i), "
Chris Lattner01d029b2007-10-15 06:10:22 +00001919 << "SDOperand(T, i), &ISelKilled);\n";
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001920 OS << " } else {\n";
1921 OS << " CurDAG->ReplaceAllUsesWith(F, T, &ISelKilled);\n";
1922 OS << " }\n";
Evan Cheng06d64702006-08-11 08:59:35 +00001923 OS << " setSelected(F->getNodeId());\n";
1924 OS << " RemoveKilled();\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001925 OS << "}\n\n";
1926
Evan Chenge41bf822006-02-05 06:43:12 +00001927 OS << "// SelectRoot - Top level entry to DAG isel.\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001928 OS << "SDOperand SelectRoot(SDOperand Root) {\n";
1929 OS << " SelectRootInit();\n";
1930 OS << " unsigned NumBytes = (DAGSize + 7) / 8;\n";
1931 OS << " ISelQueued = new unsigned char[NumBytes];\n";
1932 OS << " ISelSelected = new unsigned char[NumBytes];\n";
1933 OS << " memset(ISelQueued, 0, NumBytes);\n";
1934 OS << " memset(ISelSelected, 0, NumBytes);\n";
1935 OS << "\n";
Chris Lattnerdfb86072006-08-15 23:42:26 +00001936 OS << " // Create a dummy node (which is not added to allnodes), that adds\n"
1937 << " // a reference to the root node, preventing it from being deleted,\n"
1938 << " // and tracking any changes of the root.\n"
1939 << " HandleSDNode Dummy(CurDAG->getRoot());\n"
1940 << " ISelQueue.push_back(CurDAG->getRoot().Val);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001941 OS << " while (!ISelQueue.empty()) {\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001942 OS << " SDNode *Node = ISelQueue.front();\n";
1943 OS << " std::pop_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n";
1944 OS << " ISelQueue.pop_back();\n";
Evan Cheng06d64702006-08-11 08:59:35 +00001945 OS << " if (!isSelected(Node->getNodeId())) {\n";
Evan Cheng9ade2182006-08-26 05:34:46 +00001946 OS << " SDNode *ResNode = Select(SDOperand(Node, 0));\n";
Evan Cheng966fd372006-09-11 02:24:43 +00001947 OS << " if (ResNode != Node) {\n";
1948 OS << " if (ResNode)\n";
1949 OS << " ReplaceUses(Node, ResNode);\n";
Evan Cheng1fae00f2006-10-12 20:35:19 +00001950 OS << " if (Node->use_empty()) { // Don't delete EntryToken, etc.\n";
1951 OS << " CurDAG->RemoveDeadNode(Node, ISelKilled);\n";
1952 OS << " RemoveKilled();\n";
1953 OS << " }\n";
Evan Cheng966fd372006-09-11 02:24:43 +00001954 OS << " }\n";
Evan Cheng06d64702006-08-11 08:59:35 +00001955 OS << " }\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001956 OS << " }\n";
1957 OS << "\n";
1958 OS << " delete[] ISelQueued;\n";
1959 OS << " ISelQueued = NULL;\n";
1960 OS << " delete[] ISelSelected;\n";
1961 OS << " ISelSelected = NULL;\n";
Chris Lattnerdfb86072006-08-15 23:42:26 +00001962 OS << " return Dummy.getValue();\n";
Evan Chenge41bf822006-02-05 06:43:12 +00001963 OS << "}\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00001964
Chris Lattner6cefb772008-01-05 22:25:12 +00001965 CodegenDAGPatterns CGP(Records, OS);
Chris Lattnerb39e4be2005-09-15 02:38:02 +00001966
Chris Lattner6cefb772008-01-05 22:25:12 +00001967 this->CGP = &CGP;
1968
Bill Wendlingf5da1332006-12-07 22:21:48 +00001969 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattner6cefb772008-01-05 22:25:12 +00001970 for (CodegenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
1971 I != E; ++I) {
1972 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
1973 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00001974 DOUT << "\n";
1975 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00001976
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00001977 // At this point, we have full information about the 'Patterns' we need to
1978 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00001979 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001980 EmitInstructionSelector(OS);
1981
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001982}