blob: 6ca56573ab220c1c76cffa1f6ddbfddce094197d [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000018#include "llvm/Support/MathExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000019#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000020#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000021#include <deque>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000022using namespace llvm;
23
Chris Lattnerca559d02005-09-08 21:03:01 +000024//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000025// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000026//
27
Chris Lattner6cefb772008-01-05 22:25:12 +000028/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
29/// ComplexPattern.
30static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000031 return (N->isLeaf() &&
32 dynamic_cast<DefInit*>(N->getLeafValue()) &&
33 static_cast<DefInit*>(N->getLeafValue())->getDef()->
34 isSubClassOf("ComplexPattern"));
35}
36
Chris Lattner6cefb772008-01-05 22:25:12 +000037/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
38/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000039static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000040 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000041 if (N->isLeaf() &&
42 dynamic_cast<DefInit*>(N->getLeafValue()) &&
43 static_cast<DefInit*>(N->getLeafValue())->getDef()->
44 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000045 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
46 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000047 }
48 return NULL;
49}
50
Chris Lattner05814af2005-09-28 17:57:56 +000051/// getPatternSize - Return the 'size' of this pattern. We want to match large
52/// patterns before small ones. This is used to determine the size of a
53/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000054static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000055 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
56 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000057 P->getExtTypeNum(0) == MVT::isVoid ||
58 P->getExtTypeNum(0) == MVT::Flag ||
Mon P Wange3b3a722008-07-30 04:36:53 +000059 P->getExtTypeNum(0) == MVT::iPTR ||
60 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000061 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000062 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000063 // If the root node is a ConstantSDNode, increases its size.
64 // e.g. (set R32:$dst, 0).
65 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000066 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000067
68 // FIXME: This is a hack to statically increase the priority of patterns
69 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
70 // Later we can allow complexity / cost for each pattern to be (optionally)
71 // specified. To get best possible pattern match we'll need to dynamically
72 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000073 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000074 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000075 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000076
77 // If this node has some predicate function that must match, it adds to the
78 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000079 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000080 ++Size;
81
Chris Lattner05814af2005-09-28 17:57:56 +000082 // Count children in the count if they are also nodes.
83 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
84 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000085 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000086 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000087 else if (Child->isLeaf()) {
88 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000089 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000090 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000091 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +000092 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000093 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000094 }
Chris Lattner05814af2005-09-28 17:57:56 +000095 }
96
97 return Size;
98}
99
100/// getResultPatternCost - Compute the number of instructions for this pattern.
101/// This is a temporary hack. We should really include the instruction
102/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000103static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000104 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000105 if (P->isLeaf()) return 0;
106
Evan Chengfbad7082006-02-18 02:33:09 +0000107 unsigned Cost = 0;
108 Record *Op = P->getOperator();
109 if (Op->isSubClassOf("Instruction")) {
110 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000111 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000112 if (II.usesCustomDAGSchedInserter)
113 Cost += 10;
114 }
Chris Lattner05814af2005-09-28 17:57:56 +0000115 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000116 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000117 return Cost;
118}
119
Evan Chenge6f32032006-07-19 00:24:41 +0000120/// getResultPatternCodeSize - Compute the code size of instructions for this
121/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000122static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000123 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000124 if (P->isLeaf()) return 0;
125
126 unsigned Cost = 0;
127 Record *Op = P->getOperator();
128 if (Op->isSubClassOf("Instruction")) {
129 Cost += Op->getValueAsInt("CodeSize");
130 }
131 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000132 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000133 return Cost;
134}
135
Chris Lattner05814af2005-09-28 17:57:56 +0000136// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
137// In particular, we want to match maximal patterns first and lowest cost within
138// a particular complexity first.
139struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000140 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
141 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000142
Dan Gohman0540e172008-10-15 06:17:21 +0000143 typedef std::pair<unsigned, std::string> CodeLine;
144 typedef std::vector<CodeLine> CodeList;
145 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
146
147 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
148 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
149 const PatternToMatch *LHS = LHSPair.first;
150 const PatternToMatch *RHS = RHSPair.first;
151
Chris Lattner6cefb772008-01-05 22:25:12 +0000152 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
153 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000154 LHSSize += LHS->getAddedComplexity();
155 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000156 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
157 if (LHSSize < RHSSize) return false;
158
159 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000160 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
161 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000162 if (LHSCost < RHSCost) return true;
163 if (LHSCost > RHSCost) return false;
164
Chris Lattner6cefb772008-01-05 22:25:12 +0000165 return getResultPatternSize(LHS->getDstPattern(), CGP) <
166 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000167 }
168};
169
Nate Begeman6510b222005-12-01 04:51:06 +0000170/// getRegisterValueType - Look up and return the first ValueType of specified
171/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000172static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000173 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
174 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000175 return MVT::Other;
176}
177
Chris Lattner72fe91c2005-09-24 00:40:24 +0000178
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000179/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
180/// type information from it.
181static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000182 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000183 if (!N->isLeaf())
184 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
185 RemoveAllTypes(N->getChild(i));
186}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000187
Evan Cheng51fecc82006-01-09 18:27:06 +0000188/// NodeHasProperty - return true if TreePatternNode has the specified
189/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000190static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000191 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000192 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000193 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000194 if (CP)
195 return CP->hasProperty(Property);
196 return false;
197 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000198 Record *Operator = N->getOperator();
199 if (!Operator->isSubClassOf("SDNode")) return false;
200
Chris Lattner6cefb772008-01-05 22:25:12 +0000201 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000202}
203
Evan Cheng94b30402006-10-11 21:02:01 +0000204static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000205 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000206 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000207 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000208
209 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
210 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000211 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000212 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000213 }
214
215 return false;
216}
217
Evan Chengf9d03182008-07-03 08:39:51 +0000218static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
219 return CGP.getSDNodeInfo(Op).getEnumName();
220}
221
222static
223bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
224 bool isStore = !N->isLeaf() &&
225 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
226 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
227 return false;
228
229 bool HasChain = false;
230 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
231 TreePatternNode *Child = N->getChild(i);
232 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
233 HasChain = true;
234 break;
235 }
236 }
237 return HasChain;
238}
239
Chris Lattnerdc32f982008-01-05 22:43:57 +0000240//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000241// Node Transformation emitter implementation.
242//
243void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
244 // Walk the pattern fragments, adding them to a map, which sorts them by
245 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000246 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000247 NXsByNameTy NXsByName;
248
Chris Lattnerfe718932008-01-06 01:10:31 +0000249 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000250 I != E; ++I)
251 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
252
253 OS << "\n// Node transformations.\n";
254
255 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
256 I != E; ++I) {
257 Record *SDNode = I->second.first;
258 std::string Code = I->second.second;
259
260 if (Code.empty()) continue; // Empty code? Skip it.
261
Chris Lattner200c57e2008-01-05 22:58:54 +0000262 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000263 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
264
Dan Gohman475871a2008-07-27 21:46:04 +0000265 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000266 << ") {\n";
267 if (ClassName != "SDNode")
268 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
269 OS << Code << "\n}\n";
270 }
271}
272
273//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000274// Predicate emitter implementation.
275//
276
277void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
278 OS << "\n// Predicate functions.\n";
279
280 // Walk the pattern fragments, adding them to a map, which sorts them by
281 // name.
282 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
283 PFsByNameTy PFsByName;
284
Chris Lattnerfe718932008-01-06 01:10:31 +0000285 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000286 I != E; ++I)
287 PFsByName.insert(std::make_pair(I->first->getName(), *I));
288
289
290 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
291 I != E; ++I) {
292 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
293 TreePattern *P = I->second.second;
294
295 // If there is a code init for this fragment, emit the predicate code.
296 std::string Code = PatFragRecord->getValueAsCode("Predicate");
297 if (Code.empty()) continue;
298
299 if (P->getOnlyTree()->isLeaf())
300 OS << "inline bool Predicate_" << PatFragRecord->getName()
301 << "(SDNode *N) {\n";
302 else {
303 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000304 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000305 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
306
307 OS << "inline bool Predicate_" << PatFragRecord->getName()
308 << "(SDNode *" << C2 << ") {\n";
309 if (ClassName != "SDNode")
310 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
311 }
312 OS << Code << "\n}\n";
313 }
314
315 OS << "\n\n";
316}
317
318
319//===----------------------------------------------------------------------===//
320// PatternCodeEmitter implementation.
321//
Evan Chengb915f312005-12-09 22:45:35 +0000322class PatternCodeEmitter {
323private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000324 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000325
Evan Cheng58e84a62005-12-14 22:02:59 +0000326 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000327 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000328 // Pattern cost.
329 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000330 // Instruction selector pattern.
331 TreePatternNode *Pattern;
332 // Matched instruction.
333 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000334
Evan Chengb915f312005-12-09 22:45:35 +0000335 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000336 std::map<std::string, std::string> VariableMap;
337 // Node to operator mapping
338 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000339 // Name of the folded node which produces a flag.
340 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000341 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000342 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000343 // Original input chain(s).
344 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000345 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000346
Dan Gohman69de1932008-02-06 22:27:42 +0000347 /// LSI - Load/Store information.
348 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
349 /// for each memory access. This facilitates the use of AliasAnalysis in
350 /// the backend.
351 std::vector<std::string> LSI;
352
Evan Cheng676d7312006-08-26 00:59:04 +0000353 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000354 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000355 /// tested, and if true, the match fails) [when 1], or normal code to emit
356 /// [when 0], or initialization code to emit [when 2].
357 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000358 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000359 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000360 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000361 /// TargetOpcodes - The target specific opcodes used by the resulting
362 /// instructions.
363 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000364 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000365 /// OutputIsVariadic - Records whether the instruction output pattern uses
366 /// variable_ops. This requires that the Emit function be passed an
367 /// additional argument to indicate where the input varargs operands
368 /// begin.
369 bool &OutputIsVariadic;
370 /// NumInputRootOps - Records the number of operands the root node of the
371 /// input pattern has. This information is used in the generated code to
372 /// pass to Emit functions when variable_ops processing is needed.
373 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000374
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000375 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000376 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000377 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000378 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000379
380 void emitCheck(const std::string &S) {
381 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000382 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000383 }
384 void emitCode(const std::string &S) {
385 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000386 GeneratedCode.push_back(std::make_pair(0, S));
387 }
388 void emitInit(const std::string &S) {
389 if (!S.empty())
390 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000391 }
Evan Chengf5493192006-08-26 01:02:19 +0000392 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000393 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000394 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000395 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000396 void emitOpcode(const std::string &Opc) {
397 TargetOpcodes.push_back(Opc);
398 OpcNo++;
399 }
Evan Chengf8729402006-07-16 06:12:52 +0000400 void emitVT(const std::string &VT) {
401 TargetVTs.push_back(VT);
402 VTNo++;
403 }
Evan Chengb915f312005-12-09 22:45:35 +0000404public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000405 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000406 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000407 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000408 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000409 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000410 std::vector<std::string> &tv,
411 bool &oiv,
412 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000413 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000414 GeneratedCode(gc), GeneratedDecl(gd),
415 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000416 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000417 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000418
419 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
420 /// if the match fails. At this point, we already know that the opcode for N
421 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000422 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
423 const std::string &RootName, const std::string &ChainSuffix,
424 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000425
426 // Save loads/stores matched by a pattern.
427 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000428 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000429 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000430 }
431
Evan Chenge41bf822006-02-05 06:43:12 +0000432 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000433 // Emit instruction predicates. Each predicate is just a string for now.
434 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000435 // Record input varargs info.
436 NumInputRootOps = N->getNumChildren();
437
Evan Chengf9d03182008-07-03 08:39:51 +0000438 if (DisablePatternForFastISel(N, CGP))
Dan Gohmanea9587b2008-08-13 19:55:00 +0000439 emitCheck("!Fast");
Evan Chengf9d03182008-07-03 08:39:51 +0000440
Chris Lattner8a0604b2006-01-28 20:31:24 +0000441 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000442 }
443
Evan Chengb915f312005-12-09 22:45:35 +0000444 if (N->isLeaf()) {
445 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000446 emitCheck("cast<ConstantSDNode>(" + RootName +
Dan Gohman7810bfe2008-09-26 21:54:37 +0000447 ")->getSExtValue() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000448 return;
449 } else if (!NodeIsComplexPattern(N)) {
450 assert(0 && "Cannot match this as a leaf value!");
451 abort();
452 }
453 }
454
Chris Lattner488580c2006-01-28 19:06:51 +0000455 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000456 // we already saw this in the pattern, emit code to verify dagness.
457 if (!N->getName().empty()) {
458 std::string &VarMapEntry = VariableMap[N->getName()];
459 if (VarMapEntry.empty()) {
460 VarMapEntry = RootName;
461 } else {
462 // If we get here, this is a second reference to a specific name. Since
463 // we already have checked that the first reference is valid, we don't
464 // have to recursively match it, just check that it's the same as the
465 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000466 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000467 return;
468 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000469
470 if (!N->isLeaf())
471 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000472 }
473
474
475 // Emit code to load the child nodes and match their contents recursively.
476 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000477 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
478 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000479 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000480 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000481 if (NodeHasChain)
482 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000483 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000484 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000485 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000486 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000487 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000488 // If the immediate use can somehow reach this node through another
489 // path, then can't fold it either or it will create a cycle.
490 // e.g. In the following diagram, XX can reach ld through YY. If
491 // ld is folded into XX, then YY is both a predecessor and a successor
492 // of XX.
493 //
494 // [ld]
495 // ^ ^
496 // | |
497 // / \---
498 // / [YY]
499 // | ^
500 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000501 bool NeedCheck = P != Pattern;
502 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000503 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000504 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000505 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
506 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
507 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000508 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000509 PInfo.hasProperty(SDNPHasChain) ||
510 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000511 PInfo.hasProperty(SDNPOptInFlag);
512 }
513
514 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000515 std::string ParentName(RootName.begin(), RootName.end()-1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000516 emitCheck("CanBeFoldedBy(" + RootName + ".getNode(), " + ParentName +
517 ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000518 }
Evan Chenge41bf822006-02-05 06:43:12 +0000519 }
Evan Chengb915f312005-12-09 22:45:35 +0000520 }
Evan Chenge41bf822006-02-05 06:43:12 +0000521
Evan Chengc15d18c2006-01-27 22:13:45 +0000522 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000523 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000524 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
525 "IsChainCompatible(" + ChainName + ".getNode(), " +
526 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000527 OrigChains.push_back(std::make_pair(ChainName, RootName));
528 } else
Evan Chenge6389932006-07-21 22:19:51 +0000529 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000530 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000531 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000532 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000533 }
Evan Chengb915f312005-12-09 22:45:35 +0000534 }
535
Evan Cheng54597732006-01-26 00:22:25 +0000536 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000537 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000538 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000539 // FIXME: If the optional incoming flag does not exist. Then it is ok to
540 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000541 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000542 (PatternHasProperty(N, SDNPInFlag, CGP) ||
543 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
544 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000545 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000546 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000547 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000548 }
549 }
550
Dan Gohman0540e172008-10-15 06:17:21 +0000551 // If there are node predicates for this, emit the calls.
552 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
553 emitCheck(N->getPredicateFns()[i] + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000554
Chris Lattner39e73f72006-10-11 04:05:55 +0000555 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
556 // a constant without a predicate fn that has more that one bit set, handle
557 // this as a special case. This is usually for targets that have special
558 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
559 // handling stuff). Using these instructions is often far more efficient
560 // than materializing the constant. Unfortunately, both the instcombiner
561 // and the dag combiner can often infer that bits are dead, and thus drop
562 // them from the mask in the dag. For example, it might turn 'AND X, 255'
563 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
564 // to handle this.
565 if (!N->isLeaf() &&
566 (N->getOperator()->getName() == "and" ||
567 N->getOperator()->getName() == "or") &&
568 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000569 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000570 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
571 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000572 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000573 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000574 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000575 RootName + ".getOperand(" + utostr(1) + ");");
576
577 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
578 const char *MaskPredicate = N->getOperator()->getName() == "or"
579 ? "CheckOrMask(" : "CheckAndMask(";
580 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
581 RootName + "1), " + itostr(II->getValue()) + ")");
582
Christopher Lamb85356242008-01-31 07:27:46 +0000583 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000584 ChainSuffix + utostr(0), FoundChain);
585 return;
586 }
587 }
588 }
589
Evan Chengb915f312005-12-09 22:45:35 +0000590 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000591 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000592 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000593
Christopher Lamb85356242008-01-31 07:27:46 +0000594 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000595 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000596 }
597
Evan Cheng676d7312006-08-26 00:59:04 +0000598 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000599 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000600 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000601 std::string Fn = CP->getSelectFunc();
602 unsigned NumOps = CP->getNumOperands();
603 for (unsigned i = 0; i < NumOps; ++i) {
604 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000605 emitCode("SDValue CPTmp" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000606 }
Evan Cheng94b30402006-10-11 21:02:01 +0000607 if (CP->hasProperty(SDNPHasChain)) {
608 emitDecl("CPInChain");
609 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000610 emitCode("SDValue CPInChain;");
611 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000612 }
Evan Cheng676d7312006-08-26 00:59:04 +0000613
Evan Cheng811731e2006-11-08 20:31:10 +0000614 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000615 for (unsigned i = 0; i < NumOps; i++)
616 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000617 if (CP->hasProperty(SDNPHasChain)) {
618 ChainName = "Chain" + ChainSuffix;
619 Code += ", CPInChain, Chain" + ChainSuffix;
620 }
Evan Cheng676d7312006-08-26 00:59:04 +0000621 emitCheck(Code + ")");
622 }
Evan Chengb915f312005-12-09 22:45:35 +0000623 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000624
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000625 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000626 const std::string &RootName,
627 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000628 const std::string &ChainSuffix, bool &FoundChain) {
629 if (!Child->isLeaf()) {
630 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000631 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000632 emitCheck(RootName + ".getOpcode() == " +
633 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000634 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000635 bool HasChain = false;
636 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
637 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000638 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000639 }
640 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
641 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
642 "Pattern folded multiple nodes which produce flags?");
643 FoldedFlag = std::make_pair(RootName,
644 CInfo.getNumResults() + (unsigned)HasChain);
645 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000646 } else {
647 // If this child has a name associated with it, capture it in VarMap. If
648 // we already saw this in the pattern, emit code to verify dagness.
649 if (!Child->getName().empty()) {
650 std::string &VarMapEntry = VariableMap[Child->getName()];
651 if (VarMapEntry.empty()) {
652 VarMapEntry = RootName;
653 } else {
654 // If we get here, this is a second reference to a specific name.
655 // Since we already have checked that the first reference is valid,
656 // we don't have to recursively match it, just check that it's the
657 // same as the previously named thing.
658 emitCheck(VarMapEntry + " == " + RootName);
659 Duplicates.insert(RootName);
660 return;
661 }
662 }
663
664 // Handle leaves of various types.
665 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
666 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000667 if (LeafRec->isSubClassOf("RegisterClass") ||
668 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000669 // Handle register references. Nothing to do here.
670 } else if (LeafRec->isSubClassOf("Register")) {
671 // Handle register references.
672 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
673 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000674 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000675 std::string Fn = CP->getSelectFunc();
676 unsigned NumOps = CP->getNumOperands();
677 for (unsigned i = 0; i < NumOps; ++i) {
678 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000679 emitCode("SDValue CPTmp" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000680 }
Evan Cheng94b30402006-10-11 21:02:01 +0000681 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000682 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000683 FoldedChains.push_back(std::make_pair("CPInChain",
684 PInfo.getNumResults()));
685 ChainName = "Chain" + ChainSuffix;
686 emitDecl("CPInChain");
687 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000688 emitCode("SDValue CPInChain;");
689 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000690 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000691
Christopher Lamb85356242008-01-31 07:27:46 +0000692 std::string Code = Fn + "(";
693 if (CP->hasAttribute(CPAttrParentAsRoot)) {
694 Code += ParentRootName + ", ";
695 } else {
696 Code += "N, ";
697 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000698 if (CP->hasProperty(SDNPHasChain)) {
699 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000700 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000701 }
702 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000703 for (unsigned i = 0; i < NumOps; i++)
704 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000705 if (CP->hasProperty(SDNPHasChain))
706 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000707 emitCheck(Code + ")");
708 } else if (LeafRec->getName() == "srcvalue") {
709 // Place holder for SRCVALUE nodes. Nothing to do here.
710 } else if (LeafRec->isSubClassOf("ValueType")) {
711 // Make sure this is the specified value type.
712 emitCheck("cast<VTSDNode>(" + RootName +
713 ")->getVT() == MVT::" + LeafRec->getName());
714 } else if (LeafRec->isSubClassOf("CondCode")) {
715 // Make sure this is the specified cond code.
716 emitCheck("cast<CondCodeSDNode>(" + RootName +
717 ")->get() == ISD::" + LeafRec->getName());
718 } else {
719#ifndef NDEBUG
720 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000721 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000722#endif
723 assert(0 && "Unknown leaf type!");
724 }
725
Dan Gohman0540e172008-10-15 06:17:21 +0000726 // If there are node predicates for this, emit the calls.
727 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
728 emitCheck(Child->getPredicateFns()[i] + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000729 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000730 } else if (IntInit *II =
731 dynamic_cast<IntInit*>(Child->getLeafValue())) {
732 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
733 unsigned CTmp = TmpNo++;
734 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
Dan Gohman7810bfe2008-09-26 21:54:37 +0000735 RootName + ")->getSExtValue();");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000736
737 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
738 } else {
739#ifndef NDEBUG
740 Child->dump();
741#endif
742 assert(0 && "Unknown leaf type!");
743 }
744 }
745 }
Evan Chengb915f312005-12-09 22:45:35 +0000746
747 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
748 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000749 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000750 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000751 bool InFlagDecled, bool ResNodeDecled,
752 bool LikeLeaf = false, bool isRoot = false) {
753 // List of arguments of getTargetNode() or SelectNodeTo().
754 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000755 // This is something selected from the pattern we matched.
756 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000757 const std::string &VarName = N->getName();
758 std::string Val = VariableMap[VarName];
759 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000760 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000761 cerr << "Variable '" << VarName << " referenced but not defined "
762 << "and not caught earlier!\n";
763 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000764 }
Evan Chengb915f312005-12-09 22:45:35 +0000765 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
766 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000767 NodeOps.push_back(Val);
768 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000769 }
770
771 const ComplexPattern *CP;
772 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000773 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000774 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000775 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000776 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000777 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000778 default:
779 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
780 << " type as an immediate constant. Aborting\n";
781 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000782 case MVT::i1: CastType = "bool"; break;
783 case MVT::i8: CastType = "unsigned char"; break;
784 case MVT::i16: CastType = "unsigned short"; break;
785 case MVT::i32: CastType = "unsigned"; break;
786 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000787 }
Dan Gohman475871a2008-07-27 21:46:04 +0000788 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000789 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000790 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000791 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000792 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
793 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000794 Val = TmpVar;
795 ModifiedVal = true;
796 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000797 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
798 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
799 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000800 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000801 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
802 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
803 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000804 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
805 // value if used multiple times by this pattern result.
806 Val = TmpVar;
807 ModifiedVal = true;
808 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000809 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000810 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000811 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000812 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000813 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000814 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000815 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000816 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000817 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000818 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
819 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000820 Val = TmpVar;
821 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000822 }
Scott Michel6be48d42008-01-29 02:29:31 +0000823 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000824 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
825 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000826 Record *Op = OperatorMap[N->getName()];
827 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000828 if (Op && (Op->getName() == "globaladdr" ||
829 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000830 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000831 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000832 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000833 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000834 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000835 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
836 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000837 Val = TmpVar;
838 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000839 }
Evan Cheng676d7312006-08-26 00:59:04 +0000840 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000841 } else if (!N->isLeaf()
842 && (N->getOperator()->getName() == "texternalsym"
843 || N->getOperator()->getName() == "tconstpool")) {
844 // Do not rewrite the variable name, since we don't generate a new
845 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000846 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000847 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000848 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
849 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
850 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000851 }
Evan Chengb915f312005-12-09 22:45:35 +0000852 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000853 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000854 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000855 if (!LikeLeaf) {
856 emitCode("AddToISelQueue(" + Val + ");");
Chris Lattner706d2d32006-08-09 16:44:44 +0000857 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000858 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000859 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000860 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000861 }
Evan Cheng676d7312006-08-26 00:59:04 +0000862 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000863 }
Scott Michel6be48d42008-01-29 02:29:31 +0000864
865 if (ModifiedVal) {
866 VariableMap[VarName] = Val;
867 }
Evan Cheng676d7312006-08-26 00:59:04 +0000868 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000869 }
Evan Chengb915f312005-12-09 22:45:35 +0000870 if (N->isLeaf()) {
871 // If this is an explicit register reference, handle it.
872 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
873 unsigned ResNo = TmpNo++;
874 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000875 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000876 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000877 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000878 NodeOps.push_back("Tmp" + utostr(ResNo));
879 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000880 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000881 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000882 " = CurDAG->getRegister(0, " +
883 getEnumName(N->getTypeNum(0)) + ");");
884 NodeOps.push_back("Tmp" + utostr(ResNo));
885 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000886 }
887 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
888 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000889 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000890 emitCode("SDValue Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000891 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
892 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000893 NodeOps.push_back("Tmp" + utostr(ResNo));
894 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000895 }
896
Jim Laskey16d42c62006-07-11 18:25:13 +0000897#ifndef NDEBUG
898 N->dump();
899#endif
Evan Chengb915f312005-12-09 22:45:35 +0000900 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000901 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000902 }
903
904 Record *Op = N->getOperator();
905 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000906 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000907 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000908 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000909 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000910 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000911 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000912 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
913 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000914 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000915 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000916 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000917 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000918 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000919 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000920 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000921 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000922 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000923 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000924 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000925 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000926 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000927 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000928 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000929 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000930 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000931 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000932 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000933
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000934 // Record output varargs info.
935 OutputIsVariadic = IsVariadic;
936
Evan Chengfceb57a2006-07-15 08:45:20 +0000937 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000938 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000939 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000940 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000941 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000942 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000943
Evan Cheng823b7522006-01-19 21:57:10 +0000944 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000945 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000946 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000947 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000948 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000949 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000950 }
951
Evan Cheng4326ef52006-10-12 02:08:53 +0000952 if (OrigChains.size() > 0) {
953 // The original input chain is being ignored. If it is not just
954 // pointing to the op that's being folded, we should create a
955 // TokenFactor with it and the chain of the folded op as the new chain.
956 // We could potentially be doing multiple levels of folding, in that
957 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000958 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +0000959 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000960 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
961 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +0000962 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
963 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
964 emitCode("}");
965 }
966 emitCode("AddToISelQueue(" + ChainName + ");");
967 emitCode("InChains.push_back(" + ChainName + ");");
968 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
969 "&InChains[0], InChains.size());");
970 }
971
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000972 // Loop over all of the operands of the instruction pattern, emitting code
973 // to fill them all in. The node 'N' usually has number children equal to
974 // the number of input operands of the instruction. However, in cases
975 // where there are predicate operands for an instruction, we need to fill
976 // in the 'execute always' values. Match up the node operands to the
977 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000978 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000979 for (unsigned ChildNo = 0, InstOpNo = NumResults;
980 InstOpNo != II.OperandList.size(); ++InstOpNo) {
981 std::vector<std::string> Ops;
982
Dan Gohmand35121a2008-05-29 19:57:41 +0000983 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000984 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000985 if ((OperandNode->isSubClassOf("PredicateOperand") ||
986 OperandNode->isSubClassOf("OptionalDefOperand")) &&
987 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000988 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000989 // 'default ops' operands.
990 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +0000991 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +0000992 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +0000993 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000994 InFlagDecled, ResNodeDecled);
995 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
996 }
Dan Gohmand35121a2008-05-29 19:57:41 +0000997 } else {
998 // Otherwise this is a normal operand or a predicate operand without
999 // 'execute always'; emit it.
1000 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1001 InFlagDecled, ResNodeDecled);
1002 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1003 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001004 }
Evan Chengb915f312005-12-09 22:45:35 +00001005 }
1006
Evan Chengb915f312005-12-09 22:45:35 +00001007 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001008 bool ChainEmitted = NodeHasChain;
1009 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +00001010 emitCode("AddToISelQueue(" + ChainName + ");");
Evan Chengbc6b86a2006-06-14 19:27:50 +00001011 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001012 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1013 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +00001014 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001015 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001016 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001017 InFlagDecled = true;
1018 }
Evan Chengf037ca62006-08-27 08:11:28 +00001019 if (NodeHasOptInFlag) {
1020 emitCode("if (HasInFlag) {");
1021 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
1022 emitCode(" AddToISelQueue(InFlag);");
1023 emitCode("}");
1024 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001025 }
Evan Chengb915f312005-12-09 22:45:35 +00001026
Evan Chengb915f312005-12-09 22:45:35 +00001027 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001028
Dan Gohman95d11092008-07-07 21:00:17 +00001029 unsigned OpsNo = OpcNo;
1030 std::string CodePrefix;
1031 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1032 std::deque<std::string> After;
1033 std::string NodeName;
1034 if (!isRoot) {
1035 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001036 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001037 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001038 NodeName = "ResNode";
1039 if (!ResNodeDecled) {
1040 CodePrefix = "SDNode *" + NodeName + " = ";
1041 ResNodeDecled = true;
1042 } else
1043 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001044 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001045
Dan Gohman95d11092008-07-07 21:00:17 +00001046 std::string Code = "Opc" + utostr(OpcNo);
1047
1048 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1049
1050 // Output order: results, chain, flags
1051 // Result types.
1052 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1053 Code += ", VT" + utostr(VTNo);
1054 emitVT(getEnumName(N->getTypeNum(0)));
1055 }
1056 // Add types for implicit results in physical registers, scheduler will
1057 // care of adding copyfromreg nodes.
1058 for (unsigned i = 0; i < NumDstRegs; i++) {
1059 Record *RR = DstRegs[i];
1060 if (RR->isSubClassOf("Register")) {
1061 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
1062 Code += ", " + getEnumName(RVT);
1063 }
1064 }
1065 if (NodeHasChain)
1066 Code += ", MVT::Other";
1067 if (NodeHasOutFlag)
1068 Code += ", MVT::Flag";
1069
1070 // Inputs.
1071 if (IsVariadic) {
1072 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1073 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1074 AllOps.clear();
1075
1076 // Figure out whether any operands at the end of the op list are not
1077 // part of the variable section.
1078 std::string EndAdjust;
1079 if (NodeHasInFlag || HasImpInputs)
1080 EndAdjust = "-1"; // Always has one flag.
1081 else if (NodeHasOptInFlag)
1082 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1083
1084 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1085 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1086
1087 emitCode(" AddToISelQueue(N.getOperand(i));");
1088 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1089 emitCode("}");
1090 }
1091
1092 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1093 // this pattern.
1094 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1095 std::vector<std::string>::const_iterator mi, mie;
1096 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
Dan Gohman475871a2008-07-27 21:46:04 +00001097 emitCode("SDValue LSI_" + *mi + " = "
Dan Gohman95d11092008-07-07 21:00:17 +00001098 "CurDAG->getMemOperand(cast<MemSDNode>(" +
1099 *mi + ")->getMemOperand());");
1100 if (IsVariadic)
1101 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1102 else
1103 AllOps.push_back("LSI_" + *mi);
1104 }
1105 }
1106
1107 if (NodeHasChain) {
1108 if (IsVariadic)
1109 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1110 else
1111 AllOps.push_back(ChainName);
1112 }
1113
1114 if (IsVariadic) {
1115 if (NodeHasInFlag || HasImpInputs)
1116 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1117 else if (NodeHasOptInFlag) {
1118 emitCode("if (HasInFlag)");
1119 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1120 }
1121 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1122 ".size()";
1123 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1124 AllOps.push_back("InFlag");
1125
1126 unsigned NumOps = AllOps.size();
1127 if (NumOps) {
1128 if (!NodeHasOptInFlag && NumOps < 4) {
1129 for (unsigned i = 0; i != NumOps; ++i)
1130 Code += ", " + AllOps[i];
1131 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001132 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001133 for (unsigned i = 0; i != NumOps; ++i) {
1134 OpsCode += AllOps[i];
1135 if (i != NumOps-1)
1136 OpsCode += ", ";
1137 }
1138 emitCode(OpsCode + " };");
1139 Code += ", Ops" + utostr(OpsNo) + ", ";
1140 if (NodeHasOptInFlag) {
1141 Code += "HasInFlag ? ";
1142 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1143 } else
1144 Code += utostr(NumOps);
1145 }
1146 }
1147
1148 if (!isRoot)
1149 Code += "), 0";
1150
Dan Gohmane8be6c62008-07-17 19:10:17 +00001151 std::vector<std::string> ReplaceFroms;
1152 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001153 if (!isRoot) {
1154 NodeOps.push_back("Tmp" + utostr(ResNo));
1155 } else {
1156
1157 if (NodeHasOutFlag) {
1158 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001159 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001160 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1161 ");");
1162 InFlagDecled = true;
1163 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001164 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001165 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1166 ");");
1167 }
1168
1169 if (FoldedChains.size() > 0) {
1170 std::string Code;
Dan Gohmane8be6c62008-07-17 19:10:17 +00001171 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
Dan Gohman475871a2008-07-27 21:46:04 +00001172 ReplaceFroms.push_back("SDValue(" +
Gabor Greifba36cb52008-08-28 21:40:38 +00001173 FoldedChains[j].first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001174 utostr(FoldedChains[j].second) +
1175 ")");
Dan Gohman475871a2008-07-27 21:46:04 +00001176 ReplaceTos.push_back("SDValue(ResNode, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001177 utostr(NumResults+NumDstRegs) + ")");
1178 }
Dan Gohman95d11092008-07-07 21:00:17 +00001179 }
1180
1181 if (NodeHasOutFlag) {
1182 if (FoldedFlag.first != "") {
Gabor Greifba36cb52008-08-28 21:40:38 +00001183 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001184 utostr(FoldedFlag.second) + ")");
1185 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001186 } else {
1187 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001188 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001189 utostr(NumPatResults + (unsigned)InputHasChain)
1190 + ")");
1191 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001192 }
Dan Gohman95d11092008-07-07 21:00:17 +00001193 }
1194
Dan Gohmane8be6c62008-07-17 19:10:17 +00001195 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001196 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001197 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001198 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001199 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001200 ChainAssignmentNeeded |= NodeHasChain;
1201 }
1202
1203 // User does not expect the instruction would produce a chain!
1204 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
1205 ;
1206 } else if (InputHasChain && !NodeHasChain) {
1207 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001208 if (NodeHasOutFlag) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001209 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001210 utostr(NumPatResults+1) +
1211 ")");
Gabor Greif99a6cb92008-08-26 22:36:50 +00001212 ReplaceTos.push_back("SDValue(ResNode, N.getResNo()-1)");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001213 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001214 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001215 utostr(NumPatResults) + ")");
1216 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001217 }
1218 }
1219
1220 if (ChainAssignmentNeeded) {
1221 // Remember which op produces the chain.
1222 std::string ChainAssign;
1223 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001224 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001225 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001226 else
Dan Gohman475871a2008-07-27 21:46:04 +00001227 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001228 ", " + utostr(NumResults+NumDstRegs) + ");";
1229
1230 After.push_front(ChainAssign);
1231 }
1232
Dan Gohmane8be6c62008-07-17 19:10:17 +00001233 if (ReplaceFroms.size() == 1) {
1234 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1235 ReplaceTos[0] + ");");
1236 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001237 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001238 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1239 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1240 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001241 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001242 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1243 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1244 After.push_back("};");
1245 After.push_back("ReplaceUses(Froms, Tos, " +
1246 itostr(ReplaceFroms.size()) + ");");
1247 }
1248
1249 // We prefer to use SelectNodeTo since it avoids allocation when
1250 // possible and it avoids CSE map recalculation for the node's
1251 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001252 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001253 // We also don't use if the pattern replacement is being used to
1254 // jettison a chain result, since morphing the node in place
1255 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001256 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001257 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001258 Code = "CurDAG->getTargetNode(" + Code;
1259 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001260 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001261 }
1262 if (isRoot) {
1263 if (After.empty())
1264 CodePrefix = "return ";
1265 else
1266 After.push_back("return ResNode;");
1267 }
1268
1269 emitCode(CodePrefix + Code + ");");
1270 for (unsigned i = 0, e = After.size(); i != e; ++i)
1271 emitCode(After[i]);
1272
Evan Cheng676d7312006-08-26 00:59:04 +00001273 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001274 }
1275 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001276 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001277 // PatLeaf node - the operand may or may not be a leaf node. But it should
1278 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001279 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001280 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001281 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001282 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001283 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001284 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001285 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001286 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001287 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001288 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001289 }
Dan Gohman0540e172008-10-15 06:17:21 +00001290
1291 N->dump();
1292 cerr << "\n";
1293 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001294 }
1295
Chris Lattner488580c2006-01-28 19:06:51 +00001296 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1297 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001298 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1299 /// for, this returns true otherwise false if Pat has all types.
1300 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001301 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001302 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001303 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001304 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001305 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001306 // The top level node type is checked outside of the select function.
1307 if (!isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001308 emitCheck(Prefix + ".getNode()->getValueType(0) == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001309 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001310 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001311 }
1312
Evan Cheng51fecc82006-01-09 18:27:06 +00001313 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001314 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001315 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1316 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1317 Prefix + utostr(OpNo)))
1318 return true;
1319 return false;
1320 }
1321
1322private:
Evan Cheng54597732006-01-26 00:22:25 +00001323 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001324 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001325 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001326 bool &ChainEmitted, bool &InFlagDecled,
1327 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001328 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001329 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001330 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1331 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001332 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1333 TreePatternNode *Child = N->getChild(i);
1334 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001335 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1336 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001337 } else {
1338 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001339 if (!Child->getName().empty()) {
1340 std::string Name = RootName + utostr(OpNo);
1341 if (Duplicates.find(Name) != Duplicates.end())
1342 // A duplicate! Do not emit a copy for this node.
1343 continue;
1344 }
1345
Evan Chengb915f312005-12-09 22:45:35 +00001346 Record *RR = DI->getDef();
1347 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001348 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001349 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001350 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001351 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001352 InFlagDecled = true;
1353 } else
1354 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1355 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001356 } else {
1357 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001358 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001359 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001360 ChainEmitted = true;
1361 }
Evan Cheng676d7312006-08-26 00:59:04 +00001362 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1363 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001364 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001365 InFlagDecled = true;
1366 }
1367 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1368 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001369 ", " + getQualifiedName(RR) +
Gabor Greifba36cb52008-08-28 21:40:38 +00001370 ", " + RootName + utostr(OpNo) + ", InFlag).getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001371 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001372 emitCode(ChainName + " = SDValue(ResNode, 0);");
1373 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001374 }
1375 }
1376 }
1377 }
1378 }
Evan Cheng54597732006-01-26 00:22:25 +00001379
Evan Cheng676d7312006-08-26 00:59:04 +00001380 if (HasInFlag) {
1381 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001382 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001383 ".getOperand(" + utostr(OpNo) + ");");
1384 InFlagDecled = true;
1385 } else
1386 emitCode("InFlag = " + RootName +
1387 ".getOperand(" + utostr(OpNo) + ");");
1388 emitCode("AddToISelQueue(InFlag);");
1389 }
Evan Chengb915f312005-12-09 22:45:35 +00001390 }
1391};
1392
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001393/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1394/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001395/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001396void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001397 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001398 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001399 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001400 std::vector<std::string> &TargetVTs,
1401 bool &OutputIsVariadic,
1402 unsigned &NumInputRootOps) {
1403 OutputIsVariadic = false;
1404 NumInputRootOps = 0;
1405
Dan Gohman22bb3112008-08-22 00:20:26 +00001406 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001407 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001408 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001409 TargetOpcodes, TargetVTs,
1410 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001411
Chris Lattner8fc35682005-09-23 23:16:51 +00001412 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001413 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001414 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001415
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001416 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001417 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001418
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001419 // At this point, we know that we structurally match the pattern, but the
1420 // types of the nodes may not match. Figure out the fewest number of type
1421 // comparisons we need to emit. For example, if there is only one integer
1422 // type supported by a target, there should be no type comparisons at all for
1423 // integer patterns!
1424 //
1425 // To figure out the fewest number of type checks needed, clone the pattern,
1426 // remove the types, then perform type inference on the pattern as a whole.
1427 // If there are unresolved types, emit an explicit check for those types,
1428 // apply the type to the tree, then rerun type inference. Iterate until all
1429 // types are resolved.
1430 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001431 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001432 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001433
1434 do {
1435 // Resolve/propagate as many types as possible.
1436 try {
1437 bool MadeChange = true;
1438 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001439 MadeChange = Pat->ApplyTypeConstraints(TP,
1440 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001441 } catch (...) {
1442 assert(0 && "Error: could not find consistent types for something we"
1443 " already decided was ok!");
1444 abort();
1445 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001446
Chris Lattner7e82f132005-10-15 21:34:21 +00001447 // Insert a check for an unresolved type and add it to the tree. If we find
1448 // an unresolved type to add a check for, this returns true and we iterate,
1449 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001450 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001451
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001452 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001453 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001454 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001455}
1456
Chris Lattner24e00a42006-01-29 04:41:05 +00001457/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1458/// a line causes any of them to be empty, remove them and return true when
1459/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001460static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001461 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001462 &Patterns) {
1463 bool ErasedPatterns = false;
1464 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1465 Patterns[i].second.pop_back();
1466 if (Patterns[i].second.empty()) {
1467 Patterns.erase(Patterns.begin()+i);
1468 --i; --e;
1469 ErasedPatterns = true;
1470 }
1471 }
1472 return ErasedPatterns;
1473}
1474
Chris Lattner8bc74722006-01-29 04:25:26 +00001475/// EmitPatterns - Emit code for at least one pattern, but try to group common
1476/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001477void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001478 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001479 &Patterns, unsigned Indent,
1480 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001481 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001482 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001483 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001484
1485 if (Patterns.empty()) return;
1486
Chris Lattner24e00a42006-01-29 04:41:05 +00001487 // Figure out how many patterns share the next code line. Explicitly copy
1488 // FirstCodeLine so that we don't invalidate a reference when changing
1489 // Patterns.
1490 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001491 unsigned LastMatch = Patterns.size()-1;
1492 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1493 --LastMatch;
1494
1495 // If not all patterns share this line, split the list into two pieces. The
1496 // first chunk will use this line, the second chunk won't.
1497 if (LastMatch != 0) {
1498 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1499 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1500
1501 // FIXME: Emit braces?
1502 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001503 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001504 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1505 Pattern.getSrcPattern()->print(OS);
1506 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1507 Pattern.getDstPattern()->print(OS);
1508 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001509 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001510 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001511 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001512 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001513 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001514 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001515 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001516 }
Evan Cheng676d7312006-08-26 00:59:04 +00001517 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001518 OS << std::string(Indent, ' ') << "{\n";
1519 Indent += 2;
1520 }
1521 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001522 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001523 Indent -= 2;
1524 OS << std::string(Indent, ' ') << "}\n";
1525 }
1526
1527 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001528 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001529 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1530 Pattern.getSrcPattern()->print(OS);
1531 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1532 Pattern.getDstPattern()->print(OS);
1533 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001534 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001535 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001536 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001537 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001538 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001539 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001540 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001541 }
1542 EmitPatterns(Other, Indent, OS);
1543 return;
1544 }
1545
Chris Lattner24e00a42006-01-29 04:41:05 +00001546 // Remove this code from all of the patterns that share it.
1547 bool ErasedPatterns = EraseCodeLine(Patterns);
1548
Evan Cheng676d7312006-08-26 00:59:04 +00001549 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001550
1551 // Otherwise, every pattern in the list has this line. Emit it.
1552 if (!isPredicate) {
1553 // Normal code.
1554 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1555 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001556 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1557
1558 // If the next code line is another predicate, and if all of the pattern
1559 // in this group share the same next line, emit it inline now. Do this
1560 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001561 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001562 // Check that all of fhe patterns in Patterns end with the same predicate.
1563 bool AllEndWithSamePredicate = true;
1564 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1565 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1566 AllEndWithSamePredicate = false;
1567 break;
1568 }
1569 // If all of the predicates aren't the same, we can't share them.
1570 if (!AllEndWithSamePredicate) break;
1571
1572 // Otherwise we can. Emit it shared now.
1573 OS << " &&\n" << std::string(Indent+4, ' ')
1574 << Patterns.back().second.back().second;
1575 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001576 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001577
1578 OS << ") {\n";
1579 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001580 }
1581
1582 EmitPatterns(Patterns, Indent, OS);
1583
1584 if (isPredicate)
1585 OS << std::string(Indent-2, ' ') << "}\n";
1586}
1587
Evan Cheng892aaf82006-11-08 23:01:03 +00001588static std::string getLegalCName(std::string OpName) {
1589 std::string::size_type pos = OpName.find("::");
1590 if (pos != std::string::npos)
1591 OpName.replace(pos, 2, "_");
1592 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001593}
1594
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001595void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001596 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001597
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001598 // Get the namespace to insert instructions into.
1599 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001600 if (!InstNS.empty()) InstNS += "::";
1601
Chris Lattner602f6922006-01-04 00:25:00 +00001602 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001603 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001604 // All unique target node emission functions.
1605 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001606 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001607 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001608 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001609
1610 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001611 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001612 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001613 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001614 } else {
1615 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001616 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001617 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001618 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001619 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001620 std::vector<Record*> OpNodes = CP->getRootNodes();
1621 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001622 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1623 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001624 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001625 }
1626 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001627 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001628 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001629 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001630 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001631 exit(1);
1632 }
1633 }
1634 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001635
1636 // For each opcode, there might be multiple select functions, one per
1637 // ValueType of the node (or its first operand if it doesn't produce a
1638 // non-chain result.
1639 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1640
Chris Lattner602f6922006-01-04 00:25:00 +00001641 // Emit one Select_* method for each top-level opcode. We do this instead of
1642 // emitting one giant switch statement to support compilers where this will
1643 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001644 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001645 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1646 PBOI != E; ++PBOI) {
1647 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001648 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001649 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1650
Chris Lattner706d2d32006-08-09 16:44:44 +00001651 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001652 std::map<MVT::SimpleValueType,
1653 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001654 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001655 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001656 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001657 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001658 }
1659
Duncan Sands83ec4b62008-06-06 12:08:01 +00001660 for (std::map<MVT::SimpleValueType,
1661 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001662 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1663 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001664 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001665 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001666 typedef std::pair<unsigned, std::string> CodeLine;
1667 typedef std::vector<CodeLine> CodeList;
1668 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001669
Chris Lattner60d81392008-01-05 22:30:17 +00001670 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001671 std::vector<std::vector<std::string> > PatternOpcodes;
1672 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001673 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001674 std::vector<bool> OutputIsVariadicFlags;
1675 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001676 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1677 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001678 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001679 std::vector<std::string> TargetOpcodes;
1680 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001681 bool OutputIsVariadic;
1682 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001683 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001684 TargetOpcodes, TargetVTs,
1685 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001686 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1687 PatternDecls.push_back(GeneratedDecl);
1688 PatternOpcodes.push_back(TargetOpcodes);
1689 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001690 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1691 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001692 }
1693
Chris Lattner706d2d32006-08-09 16:44:44 +00001694 // Factor target node emission code (emitted by EmitResultCode) into
1695 // separate functions. Uniquing and share them among all instruction
1696 // selection routines.
1697 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1698 CodeList &GeneratedCode = CodeForPatterns[i].second;
1699 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1700 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001701 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001702 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1703 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001704 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001705 int CodeSize = (int)GeneratedCode.size();
1706 int LastPred = -1;
1707 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001708 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001709 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001710 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1711 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001712 }
1713
Dan Gohman475871a2008-07-27 21:46:04 +00001714 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001715 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001716 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1717 CalleeCode += ", unsigned Opc" + utostr(j);
1718 CallerCode += ", " + TargetOpcodes[j];
1719 }
1720 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001722 CallerCode += ", " + TargetVTs[j];
1723 }
Evan Chengf5493192006-08-26 01:02:19 +00001724 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001725 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001726 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001727 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001728 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001729 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001730
1731 if (OutputIsVariadic) {
1732 CalleeCode += ", unsigned NumInputRootOps";
1733 CallerCode += ", " + utostr(NumInputRootOps);
1734 }
1735
Chris Lattner706d2d32006-08-09 16:44:44 +00001736 CallerCode += ");";
1737 CalleeCode += ") ";
1738 // Prevent emission routines from being inlined to reduce selection
1739 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001740 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001741 CalleeCode += "{\n";
1742
1743 for (std::vector<std::string>::const_reverse_iterator
1744 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1745 CalleeCode += " " + *I + "\n";
1746
Evan Chengf5493192006-08-26 01:02:19 +00001747 for (int j = LastPred+1; j < CodeSize; ++j)
1748 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001749 for (int j = LastPred+1; j < CodeSize; ++j)
1750 GeneratedCode.pop_back();
1751 CalleeCode += "}\n";
1752
1753 // Uniquing the emission routines.
1754 unsigned EmitFuncNum;
1755 std::map<std::string, unsigned>::iterator EFI =
1756 EmitFunctions.find(CalleeCode);
1757 if (EFI != EmitFunctions.end()) {
1758 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001759 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001760 EmitFuncNum = EmitFunctions.size();
1761 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001762 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001763 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001764
Chris Lattner706d2d32006-08-09 16:44:44 +00001765 // Replace the emission code within selection routines with calls to the
1766 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001767 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001768 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001769 }
1770
Chris Lattner706d2d32006-08-09 16:44:44 +00001771 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001772 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001773 if (OpVT == MVT::iPTR) {
1774 OpVTStr = "_iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +00001775 } else if (OpVT == MVT::iPTRAny) {
1776 OpVTStr = "_iPTRAny";
Chris Lattner33a40042006-11-14 22:17:10 +00001777 } else if (OpVT == MVT::isVoid) {
1778 // Nodes with a void result actually have a first result type of either
1779 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1780 // void to this case, we handle it specially here.
1781 } else {
1782 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1783 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001784 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1785 OpcodeVTMap.find(OpName);
1786 if (OpVTI == OpcodeVTMap.end()) {
1787 std::vector<std::string> VTSet;
1788 VTSet.push_back(OpVTStr);
1789 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1790 } else
1791 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001792
Evan Cheng892aaf82006-11-08 23:01:03 +00001793 OS << "SDNode *Select_" << getLegalCName(OpName)
Dan Gohman475871a2008-07-27 21:46:04 +00001794 << OpVTStr << "(const SDValue &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001795
Dan Gohman0540e172008-10-15 06:17:21 +00001796 // We want to emit all of the matching code now. However, we want to emit
1797 // the matches in order of minimal cost. Sort the patterns so the least
1798 // cost one is at the start.
1799 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1800 PatternSortingPredicate(CGP));
1801
1802 // Scan the code to see if all of the patterns are reachable and if it is
1803 // possible that the last one might not match.
1804 bool mightNotMatch = true;
1805 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1806 CodeList &GeneratedCode = CodeForPatterns[i].second;
1807 mightNotMatch = false;
1808
1809 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1810 if (GeneratedCode[j].first == 1) { // predicate.
1811 mightNotMatch = true;
1812 break;
1813 }
1814 }
1815
1816 // If this pattern definitely matches, and if it isn't the last one, the
1817 // patterns after it CANNOT ever match. Error out.
1818 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
1819 cerr << "Pattern '";
1820 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1821 cerr << "' is impossible to select!\n";
1822 exit(1);
1823 }
1824 }
1825
Chris Lattner706d2d32006-08-09 16:44:44 +00001826 // Loop through and reverse all of the CodeList vectors, as we will be
1827 // accessing them from their logical front, but accessing the end of a
1828 // vector is more efficient.
1829 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1830 CodeList &GeneratedCode = CodeForPatterns[i].second;
1831 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001832 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001833
1834 // Next, reverse the list of patterns itself for the same reason.
1835 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1836
1837 // Emit all of the patterns now, grouped together to share code.
1838 EmitPatterns(CodeForPatterns, 2, OS);
1839
Chris Lattner64906972006-09-21 18:28:27 +00001840 // If the last pattern has predicates (which could fail) emit code to
1841 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001842 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001843 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001844 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1845 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001846 OpName != "ISD::INTRINSIC_VOID")
1847 OS << " CannotYetSelect(N);\n";
1848 else
1849 OS << " CannotYetSelectIntrinsic(N);\n";
1850
1851 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001852 }
1853 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001854 }
Chris Lattner602f6922006-01-04 00:25:00 +00001855 }
1856
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001857 // Emit boilerplate.
Dan Gohman475871a2008-07-27 21:46:04 +00001858 OS << "SDNode *Select_INLINEASM(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001859 << " std::vector<SDValue> Ops(N.getNode()->op_begin(), N.getNode()->op_end());\n"
Dan Gohmanf350b272008-08-23 02:25:05 +00001860 << " SelectInlineAsmMemoryOperands(Ops);\n\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001861
1862 << " // Ensure that the asm operands are themselves selected.\n"
1863 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1864 << " AddToISelQueue(Ops[j]);\n\n"
1865
Duncan Sands83ec4b62008-06-06 12:08:01 +00001866 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001867 << " VTs.push_back(MVT::Other);\n"
1868 << " VTs.push_back(MVT::Flag);\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001869 << " SDValue New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
Chris Lattner706d2d32006-08-09 16:44:44 +00001870 "Ops.size());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001871 << " return New.getNode();\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001872 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001873
Dan Gohman475871a2008-07-27 21:46:04 +00001874 OS << "SDNode *Select_UNDEF(const SDValue &N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001875 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::IMPLICIT_DEF,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001876 << " N.getValueType());\n"
1877 << "}\n\n";
1878
Dan Gohman475871a2008-07-27 21:46:04 +00001879 OS << "SDNode *Select_DBG_LABEL(const SDValue &N) {\n"
1880 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001881 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001882 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001883 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001884 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DBG_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001885 << " MVT::Other, Tmp, Chain);\n"
1886 << "}\n\n";
1887
Dan Gohman475871a2008-07-27 21:46:04 +00001888 OS << "SDNode *Select_EH_LABEL(const SDValue &N) {\n"
1889 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001890 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001891 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001892 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001893 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EH_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001894 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001895 << "}\n\n";
1896
Dan Gohman475871a2008-07-27 21:46:04 +00001897 OS << "SDNode *Select_DECLARE(const SDValue &N) {\n"
1898 << " SDValue Chain = N.getOperand(0);\n"
1899 << " SDValue N1 = N.getOperand(1);\n"
1900 << " SDValue N2 = N.getOperand(2);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001901 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00001902 << " CannotYetSelect(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001903 << " }\n"
1904 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1905 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001906 << " SDValue Tmp1 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001907 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001908 << " SDValue Tmp2 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001909 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1910 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001911 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DECLARE,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001912 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001913 << "}\n\n";
1914
Dan Gohman475871a2008-07-27 21:46:04 +00001915 OS << "SDNode *Select_EXTRACT_SUBREG(const SDValue &N) {\n"
1916 << " SDValue N0 = N.getOperand(0);\n"
1917 << " SDValue N1 = N.getOperand(1);\n"
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001918 << " unsigned C = cast<ConstantSDNode>(N1)->getZExtValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001919 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001920 << " AddToISelQueue(N0);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001921 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EXTRACT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001922 << " N.getValueType(), N0, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001923 << "}\n\n";
1924
Dan Gohman475871a2008-07-27 21:46:04 +00001925 OS << "SDNode *Select_INSERT_SUBREG(const SDValue &N) {\n"
1926 << " SDValue N0 = N.getOperand(0);\n"
1927 << " SDValue N1 = N.getOperand(1);\n"
1928 << " SDValue N2 = N.getOperand(2);\n"
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001929 << " unsigned C = cast<ConstantSDNode>(N2)->getZExtValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001930 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001931 << " AddToISelQueue(N1);\n"
Christopher Lamb6634e262008-03-13 05:47:01 +00001932 << " AddToISelQueue(N0);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001933 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::INSERT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001934 << " N.getValueType(), N0, N1, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001935 << "}\n\n";
1936
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001937 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001938 << "SDNode *SelectCode(SDValue N) {\n"
Dan Gohmane8be6c62008-07-17 19:10:17 +00001939 << " if (N.isMachineOpcode()) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001940 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001941 << " }\n\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001943 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001944 << " default: break;\n"
1945 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001946 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001947 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001948 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001949 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001950 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001951 << " case ISD::TargetConstantPool:\n"
1952 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00001953 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001954 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001955 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001956 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001957 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001958 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001959 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001960 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001961 << " AddToISelQueue(N.getOperand(0));\n"
1962 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001963 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001964 << " }\n"
1965 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001966 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001967 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001968 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1969 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001970 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001971 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001972 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001973 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
1974 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001975 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001976 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001977 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1978 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001979
Chris Lattner602f6922006-01-04 00:25:00 +00001980 // Loop over all of the case statements, emiting a call to each method we
1981 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001982 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001983 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1984 PBOI != E; ++PBOI) {
1985 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001986 // Potentially multiple versions of select for this opcode. One for each
1987 // ValueType of the node (or its first true operand if it doesn't produce a
1988 // result.
1989 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1990 OpcodeVTMap.find(OpName);
1991 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001992 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00001993 // Keep track of whether we see a pattern that has an iPtr result.
1994 bool HasPtrPattern = false;
1995 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001996
Evan Cheng425e8c72007-09-04 20:18:28 +00001997 OS << " switch (NVT) {\n";
1998 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1999 std::string &VTStr = OpVTs[i];
2000 if (VTStr.empty()) {
2001 HasDefaultPattern = true;
2002 continue;
2003 }
Chris Lattner717a6112006-11-14 21:50:27 +00002004
Evan Cheng425e8c72007-09-04 20:18:28 +00002005 // If this is a match on iPTR: don't emit it directly, we need special
2006 // code.
2007 if (VTStr == "_iPTR") {
2008 HasPtrPattern = true;
2009 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002010 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002011 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2012 << " return Select_" << getLegalCName(OpName)
2013 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002014 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002015 OS << " default:\n";
2016
2017 // If there is an iPTR result version of this pattern, emit it here.
2018 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002019 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002020 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2021 }
2022 if (HasDefaultPattern) {
2023 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2024 }
2025 OS << " break;\n";
2026 OS << " }\n";
2027 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002028 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002029 }
Chris Lattner81303322005-09-23 19:36:15 +00002030
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002031 OS << " } // end of big switch.\n\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002032 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2033 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2034 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002035 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002036 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002037 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002038 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002039 << " return NULL;\n"
2040 << "}\n\n";
2041
2042 OS << "void CannotYetSelect(SDValue N) DISABLE_INLINE {\n"
2043 << " cerr << \"Cannot yet select: \";\n"
2044 << " N.getNode()->dump(CurDAG);\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002045 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002046 << " abort();\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002047 << "}\n\n";
2048
2049 OS << "void CannotYetSelectIntrinsic(SDValue N) DISABLE_INLINE {\n"
2050 << " cerr << \"Cannot yet select: \";\n"
2051 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2052 << "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n"
2053 << " cerr << \"intrinsic %\"<< "
2054 << "Intrinsic::getName((Intrinsic::ID)iid);\n"
2055 << " cerr << '\\n';\n"
2056 << " abort();\n"
2057 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002058}
2059
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002060void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002061 EmitSourceFileHeader("DAG Instruction Selector for the " +
2062 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002063
Chris Lattner1f39e292005-09-14 00:09:24 +00002064 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2065 << "// *** instruction selector class. These functions are really "
2066 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002067
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002068 OS << "// Include standard, target-independent definitions and methods used\n"
2069 << "// by the instruction selector.\n";
2070 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002071
Chris Lattner443e3f92008-01-05 22:54:53 +00002072 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002073 EmitPredicateFunctions(OS);
2074
Bill Wendlingf5da1332006-12-07 22:21:48 +00002075 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002076 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002077 I != E; ++I) {
2078 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2079 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002080 DOUT << "\n";
2081 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002082
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002083 // At this point, we have full information about the 'Patterns' we need to
2084 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002085 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002086 EmitInstructionSelector(OS);
2087
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002088}