blob: 36e18ab5ca2979211503652523ee3ab726704bce [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.
79 if (!P->getPredicateFn().empty())
80 ++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);
Chris Lattner3e179802006-02-03 18:06:02 +000092 else if (!Child->getPredicateFn().empty())
93 ++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
Chris Lattner60d81392008-01-05 22:30:17 +0000143 bool operator()(const PatternToMatch *LHS,
144 const PatternToMatch *RHS) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000145 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
146 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000147 LHSSize += LHS->getAddedComplexity();
148 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000149 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
150 if (LHSSize < RHSSize) return false;
151
152 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000153 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
154 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000155 if (LHSCost < RHSCost) return true;
156 if (LHSCost > RHSCost) return false;
157
Chris Lattner6cefb772008-01-05 22:25:12 +0000158 return getResultPatternSize(LHS->getDstPattern(), CGP) <
159 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000160 }
161};
162
Nate Begeman6510b222005-12-01 04:51:06 +0000163/// getRegisterValueType - Look up and return the first ValueType of specified
164/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000165static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000166 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
167 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000168 return MVT::Other;
169}
170
Chris Lattner72fe91c2005-09-24 00:40:24 +0000171
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000172/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
173/// type information from it.
174static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000175 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000176 if (!N->isLeaf())
177 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
178 RemoveAllTypes(N->getChild(i));
179}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000180
Evan Cheng51fecc82006-01-09 18:27:06 +0000181/// NodeHasProperty - return true if TreePatternNode has the specified
182/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000183static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000184 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000185 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000186 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000187 if (CP)
188 return CP->hasProperty(Property);
189 return false;
190 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000191 Record *Operator = N->getOperator();
192 if (!Operator->isSubClassOf("SDNode")) return false;
193
Chris Lattner6cefb772008-01-05 22:25:12 +0000194 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000195}
196
Evan Cheng94b30402006-10-11 21:02:01 +0000197static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000198 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000199 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000200 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000201
202 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
203 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000204 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000205 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000206 }
207
208 return false;
209}
210
Evan Chengf9d03182008-07-03 08:39:51 +0000211static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
212 return CGP.getSDNodeInfo(Op).getEnumName();
213}
214
215static
216bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
217 bool isStore = !N->isLeaf() &&
218 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
219 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
220 return false;
221
222 bool HasChain = false;
223 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
224 TreePatternNode *Child = N->getChild(i);
225 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
226 HasChain = true;
227 break;
228 }
229 }
230 return HasChain;
231}
232
Chris Lattnerdc32f982008-01-05 22:43:57 +0000233//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000234// Node Transformation emitter implementation.
235//
236void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
237 // Walk the pattern fragments, adding them to a map, which sorts them by
238 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000239 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000240 NXsByNameTy NXsByName;
241
Chris Lattnerfe718932008-01-06 01:10:31 +0000242 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000243 I != E; ++I)
244 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
245
246 OS << "\n// Node transformations.\n";
247
248 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
249 I != E; ++I) {
250 Record *SDNode = I->second.first;
251 std::string Code = I->second.second;
252
253 if (Code.empty()) continue; // Empty code? Skip it.
254
Chris Lattner200c57e2008-01-05 22:58:54 +0000255 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000256 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
257
Dan Gohman475871a2008-07-27 21:46:04 +0000258 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000259 << ") {\n";
260 if (ClassName != "SDNode")
261 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
262 OS << Code << "\n}\n";
263 }
264}
265
266//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000267// Predicate emitter implementation.
268//
269
270void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
271 OS << "\n// Predicate functions.\n";
272
273 // Walk the pattern fragments, adding them to a map, which sorts them by
274 // name.
275 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
276 PFsByNameTy PFsByName;
277
Chris Lattnerfe718932008-01-06 01:10:31 +0000278 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000279 I != E; ++I)
280 PFsByName.insert(std::make_pair(I->first->getName(), *I));
281
282
283 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
284 I != E; ++I) {
285 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
286 TreePattern *P = I->second.second;
287
288 // If there is a code init for this fragment, emit the predicate code.
289 std::string Code = PatFragRecord->getValueAsCode("Predicate");
290 if (Code.empty()) continue;
291
292 if (P->getOnlyTree()->isLeaf())
293 OS << "inline bool Predicate_" << PatFragRecord->getName()
294 << "(SDNode *N) {\n";
295 else {
296 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000297 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000298 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
299
300 OS << "inline bool Predicate_" << PatFragRecord->getName()
301 << "(SDNode *" << C2 << ") {\n";
302 if (ClassName != "SDNode")
303 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
304 }
305 OS << Code << "\n}\n";
306 }
307
308 OS << "\n\n";
309}
310
311
312//===----------------------------------------------------------------------===//
313// PatternCodeEmitter implementation.
314//
Evan Chengb915f312005-12-09 22:45:35 +0000315class PatternCodeEmitter {
316private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000317 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000318
Evan Cheng58e84a62005-12-14 22:02:59 +0000319 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000320 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000321 // Pattern cost.
322 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000323 // Instruction selector pattern.
324 TreePatternNode *Pattern;
325 // Matched instruction.
326 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000327
Evan Chengb915f312005-12-09 22:45:35 +0000328 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000329 std::map<std::string, std::string> VariableMap;
330 // Node to operator mapping
331 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000332 // Name of the folded node which produces a flag.
333 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000334 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000335 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000336 // Original input chain(s).
337 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000338 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000339
Dan Gohman69de1932008-02-06 22:27:42 +0000340 /// LSI - Load/Store information.
341 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
342 /// for each memory access. This facilitates the use of AliasAnalysis in
343 /// the backend.
344 std::vector<std::string> LSI;
345
Evan Cheng676d7312006-08-26 00:59:04 +0000346 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000347 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000348 /// tested, and if true, the match fails) [when 1], or normal code to emit
349 /// [when 0], or initialization code to emit [when 2].
350 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000351 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000352 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000353 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000354 /// TargetOpcodes - The target specific opcodes used by the resulting
355 /// instructions.
356 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000357 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000358 /// OutputIsVariadic - Records whether the instruction output pattern uses
359 /// variable_ops. This requires that the Emit function be passed an
360 /// additional argument to indicate where the input varargs operands
361 /// begin.
362 bool &OutputIsVariadic;
363 /// NumInputRootOps - Records the number of operands the root node of the
364 /// input pattern has. This information is used in the generated code to
365 /// pass to Emit functions when variable_ops processing is needed.
366 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000367
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000368 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000369 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000370 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000371 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000372
373 void emitCheck(const std::string &S) {
374 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000375 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000376 }
377 void emitCode(const std::string &S) {
378 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000379 GeneratedCode.push_back(std::make_pair(0, S));
380 }
381 void emitInit(const std::string &S) {
382 if (!S.empty())
383 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000384 }
Evan Chengf5493192006-08-26 01:02:19 +0000385 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000386 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000387 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000388 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000389 void emitOpcode(const std::string &Opc) {
390 TargetOpcodes.push_back(Opc);
391 OpcNo++;
392 }
Evan Chengf8729402006-07-16 06:12:52 +0000393 void emitVT(const std::string &VT) {
394 TargetVTs.push_back(VT);
395 VTNo++;
396 }
Evan Chengb915f312005-12-09 22:45:35 +0000397public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000398 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000399 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000400 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000401 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000402 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000403 std::vector<std::string> &tv,
404 bool &oiv,
405 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000406 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000407 GeneratedCode(gc), GeneratedDecl(gd),
408 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000409 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000410 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000411
412 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
413 /// if the match fails. At this point, we already know that the opcode for N
414 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000415 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
416 const std::string &RootName, const std::string &ChainSuffix,
417 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000418
419 // Save loads/stores matched by a pattern.
420 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000421 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000422 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000423 }
424
Evan Chenge41bf822006-02-05 06:43:12 +0000425 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000426 // Emit instruction predicates. Each predicate is just a string for now.
427 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000428 // Record input varargs info.
429 NumInputRootOps = N->getNumChildren();
430
Evan Chengf9d03182008-07-03 08:39:51 +0000431 if (DisablePatternForFastISel(N, CGP))
Dan Gohmanea9587b2008-08-13 19:55:00 +0000432 emitCheck("!Fast");
Evan Chengf9d03182008-07-03 08:39:51 +0000433
Chris Lattner8a0604b2006-01-28 20:31:24 +0000434 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000435 }
436
Evan Chengb915f312005-12-09 22:45:35 +0000437 if (N->isLeaf()) {
438 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000439 emitCheck("cast<ConstantSDNode>(" + RootName +
Chris Lattner67a202b2006-01-28 20:43:52 +0000440 ")->getSignExtended() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000441 return;
442 } else if (!NodeIsComplexPattern(N)) {
443 assert(0 && "Cannot match this as a leaf value!");
444 abort();
445 }
446 }
447
Chris Lattner488580c2006-01-28 19:06:51 +0000448 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000449 // we already saw this in the pattern, emit code to verify dagness.
450 if (!N->getName().empty()) {
451 std::string &VarMapEntry = VariableMap[N->getName()];
452 if (VarMapEntry.empty()) {
453 VarMapEntry = RootName;
454 } else {
455 // If we get here, this is a second reference to a specific name. Since
456 // we already have checked that the first reference is valid, we don't
457 // have to recursively match it, just check that it's the same as the
458 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000459 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000460 return;
461 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000462
463 if (!N->isLeaf())
464 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000465 }
466
467
468 // Emit code to load the child nodes and match their contents recursively.
469 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000470 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
471 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000472 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000473 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000474 if (NodeHasChain)
475 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000476 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000477 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000478 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000479 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000480 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000481 // If the immediate use can somehow reach this node through another
482 // path, then can't fold it either or it will create a cycle.
483 // e.g. In the following diagram, XX can reach ld through YY. If
484 // ld is folded into XX, then YY is both a predecessor and a successor
485 // of XX.
486 //
487 // [ld]
488 // ^ ^
489 // | |
490 // / \---
491 // / [YY]
492 // | ^
493 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000494 bool NeedCheck = P != Pattern;
495 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000496 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000497 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000498 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
499 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
500 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000501 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000502 PInfo.hasProperty(SDNPHasChain) ||
503 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000504 PInfo.hasProperty(SDNPOptInFlag);
505 }
506
507 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000508 std::string ParentName(RootName.begin(), RootName.end()-1);
Gabor Greifba36cb52008-08-28 21:40:38 +0000509 emitCheck("CanBeFoldedBy(" + RootName + ".getNode(), " + ParentName +
510 ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000511 }
Evan Chenge41bf822006-02-05 06:43:12 +0000512 }
Evan Chengb915f312005-12-09 22:45:35 +0000513 }
Evan Chenge41bf822006-02-05 06:43:12 +0000514
Evan Chengc15d18c2006-01-27 22:13:45 +0000515 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000516 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000517 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
518 "IsChainCompatible(" + ChainName + ".getNode(), " +
519 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000520 OrigChains.push_back(std::make_pair(ChainName, RootName));
521 } else
Evan Chenge6389932006-07-21 22:19:51 +0000522 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000523 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000524 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000525 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000526 }
Evan Chengb915f312005-12-09 22:45:35 +0000527 }
528
Evan Cheng54597732006-01-26 00:22:25 +0000529 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000530 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000531 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000532 // FIXME: If the optional incoming flag does not exist. Then it is ok to
533 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000534 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000535 (PatternHasProperty(N, SDNPInFlag, CGP) ||
536 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
537 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000538 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000539 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000540 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000541 }
542 }
543
Evan Chengd3eea902006-10-09 21:02:17 +0000544 // If there is a node predicate for this, emit the call.
545 if (!N->getPredicateFn().empty())
Gabor Greifba36cb52008-08-28 21:40:38 +0000546 emitCheck(N->getPredicateFn() + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000547
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000548
Chris Lattner39e73f72006-10-11 04:05:55 +0000549 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
550 // a constant without a predicate fn that has more that one bit set, handle
551 // this as a special case. This is usually for targets that have special
552 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
553 // handling stuff). Using these instructions is often far more efficient
554 // than materializing the constant. Unfortunately, both the instcombiner
555 // and the dag combiner can often infer that bits are dead, and thus drop
556 // them from the mask in the dag. For example, it might turn 'AND X, 255'
557 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
558 // to handle this.
559 if (!N->isLeaf() &&
560 (N->getOperator()->getName() == "and" ||
561 N->getOperator()->getName() == "or") &&
562 N->getChild(1)->isLeaf() &&
563 N->getChild(1)->getPredicateFn().empty()) {
564 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
565 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000566 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000567 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000568 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000569 RootName + ".getOperand(" + utostr(1) + ");");
570
571 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
572 const char *MaskPredicate = N->getOperator()->getName() == "or"
573 ? "CheckOrMask(" : "CheckAndMask(";
574 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
575 RootName + "1), " + itostr(II->getValue()) + ")");
576
Christopher Lamb85356242008-01-31 07:27:46 +0000577 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000578 ChainSuffix + utostr(0), FoundChain);
579 return;
580 }
581 }
582 }
583
Evan Chengb915f312005-12-09 22:45:35 +0000584 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000585 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000586 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000587
Christopher Lamb85356242008-01-31 07:27:46 +0000588 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000589 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000590 }
591
Evan Cheng676d7312006-08-26 00:59:04 +0000592 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000593 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000594 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000595 std::string Fn = CP->getSelectFunc();
596 unsigned NumOps = CP->getNumOperands();
597 for (unsigned i = 0; i < NumOps; ++i) {
598 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000599 emitCode("SDValue CPTmp" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000600 }
Evan Cheng94b30402006-10-11 21:02:01 +0000601 if (CP->hasProperty(SDNPHasChain)) {
602 emitDecl("CPInChain");
603 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000604 emitCode("SDValue CPInChain;");
605 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000606 }
Evan Cheng676d7312006-08-26 00:59:04 +0000607
Evan Cheng811731e2006-11-08 20:31:10 +0000608 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000609 for (unsigned i = 0; i < NumOps; i++)
610 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000611 if (CP->hasProperty(SDNPHasChain)) {
612 ChainName = "Chain" + ChainSuffix;
613 Code += ", CPInChain, Chain" + ChainSuffix;
614 }
Evan Cheng676d7312006-08-26 00:59:04 +0000615 emitCheck(Code + ")");
616 }
Evan Chengb915f312005-12-09 22:45:35 +0000617 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000618
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000619 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000620 const std::string &RootName,
621 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000622 const std::string &ChainSuffix, bool &FoundChain) {
623 if (!Child->isLeaf()) {
624 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000625 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000626 emitCheck(RootName + ".getOpcode() == " +
627 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000628 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000629 bool HasChain = false;
630 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
631 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000632 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000633 }
634 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
635 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
636 "Pattern folded multiple nodes which produce flags?");
637 FoldedFlag = std::make_pair(RootName,
638 CInfo.getNumResults() + (unsigned)HasChain);
639 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000640 } else {
641 // If this child has a name associated with it, capture it in VarMap. If
642 // we already saw this in the pattern, emit code to verify dagness.
643 if (!Child->getName().empty()) {
644 std::string &VarMapEntry = VariableMap[Child->getName()];
645 if (VarMapEntry.empty()) {
646 VarMapEntry = RootName;
647 } else {
648 // If we get here, this is a second reference to a specific name.
649 // Since we already have checked that the first reference is valid,
650 // we don't have to recursively match it, just check that it's the
651 // same as the previously named thing.
652 emitCheck(VarMapEntry + " == " + RootName);
653 Duplicates.insert(RootName);
654 return;
655 }
656 }
657
658 // Handle leaves of various types.
659 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
660 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000661 if (LeafRec->isSubClassOf("RegisterClass") ||
662 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000663 // Handle register references. Nothing to do here.
664 } else if (LeafRec->isSubClassOf("Register")) {
665 // Handle register references.
666 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
667 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000668 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000669 std::string Fn = CP->getSelectFunc();
670 unsigned NumOps = CP->getNumOperands();
671 for (unsigned i = 0; i < NumOps; ++i) {
672 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000673 emitCode("SDValue CPTmp" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000674 }
Evan Cheng94b30402006-10-11 21:02:01 +0000675 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000676 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000677 FoldedChains.push_back(std::make_pair("CPInChain",
678 PInfo.getNumResults()));
679 ChainName = "Chain" + ChainSuffix;
680 emitDecl("CPInChain");
681 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000682 emitCode("SDValue CPInChain;");
683 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000684 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000685
Christopher Lamb85356242008-01-31 07:27:46 +0000686 std::string Code = Fn + "(";
687 if (CP->hasAttribute(CPAttrParentAsRoot)) {
688 Code += ParentRootName + ", ";
689 } else {
690 Code += "N, ";
691 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000692 if (CP->hasProperty(SDNPHasChain)) {
693 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000694 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000695 }
696 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000697 for (unsigned i = 0; i < NumOps; i++)
698 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000699 if (CP->hasProperty(SDNPHasChain))
700 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000701 emitCheck(Code + ")");
702 } else if (LeafRec->getName() == "srcvalue") {
703 // Place holder for SRCVALUE nodes. Nothing to do here.
704 } else if (LeafRec->isSubClassOf("ValueType")) {
705 // Make sure this is the specified value type.
706 emitCheck("cast<VTSDNode>(" + RootName +
707 ")->getVT() == MVT::" + LeafRec->getName());
708 } else if (LeafRec->isSubClassOf("CondCode")) {
709 // Make sure this is the specified cond code.
710 emitCheck("cast<CondCodeSDNode>(" + RootName +
711 ")->get() == ISD::" + LeafRec->getName());
712 } else {
713#ifndef NDEBUG
714 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000715 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000716#endif
717 assert(0 && "Unknown leaf type!");
718 }
719
720 // If there is a node predicate for this, emit the call.
721 if (!Child->getPredicateFn().empty())
722 emitCheck(Child->getPredicateFn() + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000723 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000724 } else if (IntInit *II =
725 dynamic_cast<IntInit*>(Child->getLeafValue())) {
726 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
727 unsigned CTmp = TmpNo++;
728 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
729 RootName + ")->getSignExtended();");
730
731 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
732 } else {
733#ifndef NDEBUG
734 Child->dump();
735#endif
736 assert(0 && "Unknown leaf type!");
737 }
738 }
739 }
Evan Chengb915f312005-12-09 22:45:35 +0000740
741 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
742 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000743 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000744 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000745 bool InFlagDecled, bool ResNodeDecled,
746 bool LikeLeaf = false, bool isRoot = false) {
747 // List of arguments of getTargetNode() or SelectNodeTo().
748 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000749 // This is something selected from the pattern we matched.
750 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000751 const std::string &VarName = N->getName();
752 std::string Val = VariableMap[VarName];
753 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000754 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000755 cerr << "Variable '" << VarName << " referenced but not defined "
756 << "and not caught earlier!\n";
757 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000758 }
Evan Chengb915f312005-12-09 22:45:35 +0000759 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
760 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000761 NodeOps.push_back(Val);
762 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000763 }
764
765 const ComplexPattern *CP;
766 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000767 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000768 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000769 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000770 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000771 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000772 default:
773 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
774 << " type as an immediate constant. Aborting\n";
775 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000776 case MVT::i1: CastType = "bool"; break;
777 case MVT::i8: CastType = "unsigned char"; break;
778 case MVT::i16: CastType = "unsigned short"; break;
779 case MVT::i32: CastType = "unsigned"; break;
780 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000781 }
Dan Gohman475871a2008-07-27 21:46:04 +0000782 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000783 " = CurDAG->getTargetConstant(((" + CastType +
784 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
785 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000786 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
787 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000788 Val = TmpVar;
789 ModifiedVal = true;
790 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000791 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
792 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
793 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000794 emitCode("SDValue " + TmpVar +
Nate Begemane1795842008-02-14 08:57:00 +0000795 " = CurDAG->getTargetConstantFP(cast<ConstantFPSDNode>(" +
796 Val + ")->getValueAPF(), cast<ConstantFPSDNode>(" + Val +
797 ")->getValueType(0));");
798 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
799 // value if used multiple times by this pattern result.
800 Val = TmpVar;
801 ModifiedVal = true;
802 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000803 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000804 Record *Op = OperatorMap[N->getName()];
805 // Transform ExternalSymbol to TargetExternalSymbol
806 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000807 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000808 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000809 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000810 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000811 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000812 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
813 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000814 Val = TmpVar;
815 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000816 }
Scott Michel6be48d42008-01-29 02:29:31 +0000817 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000818 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
819 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000820 Record *Op = OperatorMap[N->getName()];
821 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000822 if (Op && (Op->getName() == "globaladdr" ||
823 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000824 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000825 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000826 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000827 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000828 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000829 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
830 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000831 Val = TmpVar;
832 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000833 }
Evan Cheng676d7312006-08-26 00:59:04 +0000834 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000835 } else if (!N->isLeaf()
836 && (N->getOperator()->getName() == "texternalsym"
837 || N->getOperator()->getName() == "tconstpool")) {
838 // Do not rewrite the variable name, since we don't generate a new
839 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000840 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000841 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000842 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
843 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
844 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000845 }
Evan Chengb915f312005-12-09 22:45:35 +0000846 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000847 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000848 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000849 if (!LikeLeaf) {
850 emitCode("AddToISelQueue(" + Val + ");");
Chris Lattner706d2d32006-08-09 16:44:44 +0000851 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000852 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000853 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000854 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000855 }
Evan Cheng676d7312006-08-26 00:59:04 +0000856 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000857 }
Scott Michel6be48d42008-01-29 02:29:31 +0000858
859 if (ModifiedVal) {
860 VariableMap[VarName] = Val;
861 }
Evan Cheng676d7312006-08-26 00:59:04 +0000862 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000863 }
Evan Chengb915f312005-12-09 22:45:35 +0000864 if (N->isLeaf()) {
865 // If this is an explicit register reference, handle it.
866 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
867 unsigned ResNo = TmpNo++;
868 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000869 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000870 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000871 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000872 NodeOps.push_back("Tmp" + utostr(ResNo));
873 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000874 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000875 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000876 " = CurDAG->getRegister(0, " +
877 getEnumName(N->getTypeNum(0)) + ");");
878 NodeOps.push_back("Tmp" + utostr(ResNo));
879 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000880 }
881 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
882 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000883 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000884 emitCode("SDValue Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000885 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
886 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000887 NodeOps.push_back("Tmp" + utostr(ResNo));
888 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000889 }
890
Jim Laskey16d42c62006-07-11 18:25:13 +0000891#ifndef NDEBUG
892 N->dump();
893#endif
Evan Chengb915f312005-12-09 22:45:35 +0000894 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000895 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000896 }
897
898 Record *Op = N->getOperator();
899 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000900 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000901 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000902 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000903 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000904 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000905 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000906 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
907 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000908 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000909 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000910 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000911 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000912 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000913 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000914 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000915 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000916 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000917 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000918 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000919 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000920 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000921 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000922 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000923 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000924 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000925 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000926 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000927
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000928 // Record output varargs info.
929 OutputIsVariadic = IsVariadic;
930
Evan Chengfceb57a2006-07-15 08:45:20 +0000931 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000932 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000933 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000934 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000935 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000936 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000937
Evan Cheng823b7522006-01-19 21:57:10 +0000938 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000939 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000940 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000942 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000943 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000944 }
945
Evan Cheng4326ef52006-10-12 02:08:53 +0000946 if (OrigChains.size() > 0) {
947 // The original input chain is being ignored. If it is not just
948 // pointing to the op that's being folded, we should create a
949 // TokenFactor with it and the chain of the folded op as the new chain.
950 // We could potentially be doing multiple levels of folding, in that
951 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000952 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +0000953 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000954 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
955 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +0000956 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
957 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
958 emitCode("}");
959 }
960 emitCode("AddToISelQueue(" + ChainName + ");");
961 emitCode("InChains.push_back(" + ChainName + ");");
962 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
963 "&InChains[0], InChains.size());");
964 }
965
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000966 // Loop over all of the operands of the instruction pattern, emitting code
967 // to fill them all in. The node 'N' usually has number children equal to
968 // the number of input operands of the instruction. However, in cases
969 // where there are predicate operands for an instruction, we need to fill
970 // in the 'execute always' values. Match up the node operands to the
971 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000972 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000973 for (unsigned ChildNo = 0, InstOpNo = NumResults;
974 InstOpNo != II.OperandList.size(); ++InstOpNo) {
975 std::vector<std::string> Ops;
976
Dan Gohmand35121a2008-05-29 19:57:41 +0000977 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000978 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000979 if ((OperandNode->isSubClassOf("PredicateOperand") ||
980 OperandNode->isSubClassOf("OptionalDefOperand")) &&
981 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000982 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000983 // 'default ops' operands.
984 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +0000985 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +0000986 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +0000987 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000988 InFlagDecled, ResNodeDecled);
989 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
990 }
Dan Gohmand35121a2008-05-29 19:57:41 +0000991 } else {
992 // Otherwise this is a normal operand or a predicate operand without
993 // 'execute always'; emit it.
994 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
995 InFlagDecled, ResNodeDecled);
996 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
997 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000998 }
Evan Chengb915f312005-12-09 22:45:35 +0000999 }
1000
Evan Chengb915f312005-12-09 22:45:35 +00001001 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001002 bool ChainEmitted = NodeHasChain;
1003 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +00001004 emitCode("AddToISelQueue(" + ChainName + ");");
Evan Chengbc6b86a2006-06-14 19:27:50 +00001005 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001006 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1007 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +00001008 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001009 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001010 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001011 InFlagDecled = true;
1012 }
Evan Chengf037ca62006-08-27 08:11:28 +00001013 if (NodeHasOptInFlag) {
1014 emitCode("if (HasInFlag) {");
1015 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
1016 emitCode(" AddToISelQueue(InFlag);");
1017 emitCode("}");
1018 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001019 }
Evan Chengb915f312005-12-09 22:45:35 +00001020
Evan Chengb915f312005-12-09 22:45:35 +00001021 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001022
Dan Gohman95d11092008-07-07 21:00:17 +00001023 unsigned OpsNo = OpcNo;
1024 std::string CodePrefix;
1025 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1026 std::deque<std::string> After;
1027 std::string NodeName;
1028 if (!isRoot) {
1029 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001030 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001031 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001032 NodeName = "ResNode";
1033 if (!ResNodeDecled) {
1034 CodePrefix = "SDNode *" + NodeName + " = ";
1035 ResNodeDecled = true;
1036 } else
1037 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001038 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001039
Dan Gohman95d11092008-07-07 21:00:17 +00001040 std::string Code = "Opc" + utostr(OpcNo);
1041
1042 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1043
1044 // Output order: results, chain, flags
1045 // Result types.
1046 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1047 Code += ", VT" + utostr(VTNo);
1048 emitVT(getEnumName(N->getTypeNum(0)));
1049 }
1050 // Add types for implicit results in physical registers, scheduler will
1051 // care of adding copyfromreg nodes.
1052 for (unsigned i = 0; i < NumDstRegs; i++) {
1053 Record *RR = DstRegs[i];
1054 if (RR->isSubClassOf("Register")) {
1055 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
1056 Code += ", " + getEnumName(RVT);
1057 }
1058 }
1059 if (NodeHasChain)
1060 Code += ", MVT::Other";
1061 if (NodeHasOutFlag)
1062 Code += ", MVT::Flag";
1063
1064 // Inputs.
1065 if (IsVariadic) {
1066 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1067 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1068 AllOps.clear();
1069
1070 // Figure out whether any operands at the end of the op list are not
1071 // part of the variable section.
1072 std::string EndAdjust;
1073 if (NodeHasInFlag || HasImpInputs)
1074 EndAdjust = "-1"; // Always has one flag.
1075 else if (NodeHasOptInFlag)
1076 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1077
1078 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1079 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1080
1081 emitCode(" AddToISelQueue(N.getOperand(i));");
1082 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1083 emitCode("}");
1084 }
1085
1086 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1087 // this pattern.
1088 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1089 std::vector<std::string>::const_iterator mi, mie;
1090 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
Dan Gohman475871a2008-07-27 21:46:04 +00001091 emitCode("SDValue LSI_" + *mi + " = "
Dan Gohman95d11092008-07-07 21:00:17 +00001092 "CurDAG->getMemOperand(cast<MemSDNode>(" +
1093 *mi + ")->getMemOperand());");
1094 if (IsVariadic)
1095 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1096 else
1097 AllOps.push_back("LSI_" + *mi);
1098 }
1099 }
1100
1101 if (NodeHasChain) {
1102 if (IsVariadic)
1103 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1104 else
1105 AllOps.push_back(ChainName);
1106 }
1107
1108 if (IsVariadic) {
1109 if (NodeHasInFlag || HasImpInputs)
1110 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1111 else if (NodeHasOptInFlag) {
1112 emitCode("if (HasInFlag)");
1113 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1114 }
1115 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1116 ".size()";
1117 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1118 AllOps.push_back("InFlag");
1119
1120 unsigned NumOps = AllOps.size();
1121 if (NumOps) {
1122 if (!NodeHasOptInFlag && NumOps < 4) {
1123 for (unsigned i = 0; i != NumOps; ++i)
1124 Code += ", " + AllOps[i];
1125 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001126 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001127 for (unsigned i = 0; i != NumOps; ++i) {
1128 OpsCode += AllOps[i];
1129 if (i != NumOps-1)
1130 OpsCode += ", ";
1131 }
1132 emitCode(OpsCode + " };");
1133 Code += ", Ops" + utostr(OpsNo) + ", ";
1134 if (NodeHasOptInFlag) {
1135 Code += "HasInFlag ? ";
1136 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1137 } else
1138 Code += utostr(NumOps);
1139 }
1140 }
1141
1142 if (!isRoot)
1143 Code += "), 0";
1144
Dan Gohmane8be6c62008-07-17 19:10:17 +00001145 std::vector<std::string> ReplaceFroms;
1146 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001147 if (!isRoot) {
1148 NodeOps.push_back("Tmp" + utostr(ResNo));
1149 } else {
1150
1151 if (NodeHasOutFlag) {
1152 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001153 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001154 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1155 ");");
1156 InFlagDecled = true;
1157 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001158 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001159 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1160 ");");
1161 }
1162
1163 if (FoldedChains.size() > 0) {
1164 std::string Code;
Dan Gohmane8be6c62008-07-17 19:10:17 +00001165 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
Dan Gohman475871a2008-07-27 21:46:04 +00001166 ReplaceFroms.push_back("SDValue(" +
Gabor Greifba36cb52008-08-28 21:40:38 +00001167 FoldedChains[j].first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001168 utostr(FoldedChains[j].second) +
1169 ")");
Dan Gohman475871a2008-07-27 21:46:04 +00001170 ReplaceTos.push_back("SDValue(ResNode, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001171 utostr(NumResults+NumDstRegs) + ")");
1172 }
Dan Gohman95d11092008-07-07 21:00:17 +00001173 }
1174
1175 if (NodeHasOutFlag) {
1176 if (FoldedFlag.first != "") {
Gabor Greifba36cb52008-08-28 21:40:38 +00001177 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001178 utostr(FoldedFlag.second) + ")");
1179 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001180 } else {
1181 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001182 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001183 utostr(NumPatResults + (unsigned)InputHasChain)
1184 + ")");
1185 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001186 }
Dan Gohman95d11092008-07-07 21:00:17 +00001187 }
1188
Dan Gohmane8be6c62008-07-17 19:10:17 +00001189 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001190 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001191 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001192 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001193 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001194 ChainAssignmentNeeded |= NodeHasChain;
1195 }
1196
1197 // User does not expect the instruction would produce a chain!
1198 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
1199 ;
1200 } else if (InputHasChain && !NodeHasChain) {
1201 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001202 if (NodeHasOutFlag) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001203 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001204 utostr(NumPatResults+1) +
1205 ")");
Gabor Greif99a6cb92008-08-26 22:36:50 +00001206 ReplaceTos.push_back("SDValue(ResNode, N.getResNo()-1)");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001207 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001208 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001209 utostr(NumPatResults) + ")");
1210 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001211 }
1212 }
1213
1214 if (ChainAssignmentNeeded) {
1215 // Remember which op produces the chain.
1216 std::string ChainAssign;
1217 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001218 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001219 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001220 else
Dan Gohman475871a2008-07-27 21:46:04 +00001221 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001222 ", " + utostr(NumResults+NumDstRegs) + ");";
1223
1224 After.push_front(ChainAssign);
1225 }
1226
Dan Gohmane8be6c62008-07-17 19:10:17 +00001227 if (ReplaceFroms.size() == 1) {
1228 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1229 ReplaceTos[0] + ");");
1230 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001231 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001232 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1233 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1234 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001235 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001236 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1237 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1238 After.push_back("};");
1239 After.push_back("ReplaceUses(Froms, Tos, " +
1240 itostr(ReplaceFroms.size()) + ");");
1241 }
1242
1243 // We prefer to use SelectNodeTo since it avoids allocation when
1244 // possible and it avoids CSE map recalculation for the node's
1245 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001246 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001247 // We also don't use if the pattern replacement is being used to
1248 // jettison a chain result, since morphing the node in place
1249 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001250 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001251 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001252 Code = "CurDAG->getTargetNode(" + Code;
1253 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001254 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001255 }
1256 if (isRoot) {
1257 if (After.empty())
1258 CodePrefix = "return ";
1259 else
1260 After.push_back("return ResNode;");
1261 }
1262
1263 emitCode(CodePrefix + Code + ");");
1264 for (unsigned i = 0, e = After.size(); i != e; ++i)
1265 emitCode(After[i]);
1266
Evan Cheng676d7312006-08-26 00:59:04 +00001267 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001268 } else if (Op->isSubClassOf("SDNodeXForm")) {
1269 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001270 // PatLeaf node - the operand may or may not be a leaf node. But it should
1271 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001272 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001273 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001274 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001275 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001276 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001277 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001278 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001279 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001281 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001282 } else {
1283 N->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001284 cerr << "\n";
Chris Lattner7893f132006-01-11 01:33:49 +00001285 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001286 }
1287 }
1288
Chris Lattner488580c2006-01-28 19:06:51 +00001289 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1290 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001291 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1292 /// for, this returns true otherwise false if Pat has all types.
1293 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001294 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001295 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001296 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001297 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001298 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001299 // The top level node type is checked outside of the select function.
1300 if (!isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001301 emitCheck(Prefix + ".getNode()->getValueType(0) == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001302 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001303 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001304 }
1305
Evan Cheng51fecc82006-01-09 18:27:06 +00001306 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001307 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001308 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1309 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1310 Prefix + utostr(OpNo)))
1311 return true;
1312 return false;
1313 }
1314
1315private:
Evan Cheng54597732006-01-26 00:22:25 +00001316 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001317 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001318 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001319 bool &ChainEmitted, bool &InFlagDecled,
1320 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001321 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001322 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001323 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1324 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001325 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1326 TreePatternNode *Child = N->getChild(i);
1327 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001328 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1329 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001330 } else {
1331 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001332 if (!Child->getName().empty()) {
1333 std::string Name = RootName + utostr(OpNo);
1334 if (Duplicates.find(Name) != Duplicates.end())
1335 // A duplicate! Do not emit a copy for this node.
1336 continue;
1337 }
1338
Evan Chengb915f312005-12-09 22:45:35 +00001339 Record *RR = DI->getDef();
1340 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001341 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001342 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001343 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001344 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001345 InFlagDecled = true;
1346 } else
1347 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1348 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001349 } else {
1350 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001351 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001352 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001353 ChainEmitted = true;
1354 }
Evan Cheng676d7312006-08-26 00:59:04 +00001355 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1356 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001357 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001358 InFlagDecled = true;
1359 }
1360 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1361 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001362 ", " + getQualifiedName(RR) +
Gabor Greifba36cb52008-08-28 21:40:38 +00001363 ", " + RootName + utostr(OpNo) + ", InFlag).getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001364 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001365 emitCode(ChainName + " = SDValue(ResNode, 0);");
1366 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001367 }
1368 }
1369 }
1370 }
1371 }
Evan Cheng54597732006-01-26 00:22:25 +00001372
Evan Cheng676d7312006-08-26 00:59:04 +00001373 if (HasInFlag) {
1374 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001375 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001376 ".getOperand(" + utostr(OpNo) + ");");
1377 InFlagDecled = true;
1378 } else
1379 emitCode("InFlag = " + RootName +
1380 ".getOperand(" + utostr(OpNo) + ");");
1381 emitCode("AddToISelQueue(InFlag);");
1382 }
Evan Chengb915f312005-12-09 22:45:35 +00001383 }
1384};
1385
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001386/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1387/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001388/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001389void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001390 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001391 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001392 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001393 std::vector<std::string> &TargetVTs,
1394 bool &OutputIsVariadic,
1395 unsigned &NumInputRootOps) {
1396 OutputIsVariadic = false;
1397 NumInputRootOps = 0;
1398
Dan Gohman22bb3112008-08-22 00:20:26 +00001399 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001400 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001401 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001402 TargetOpcodes, TargetVTs,
1403 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001404
Chris Lattner8fc35682005-09-23 23:16:51 +00001405 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001406 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001407 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001408
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001409 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001410 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001411
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001412 // At this point, we know that we structurally match the pattern, but the
1413 // types of the nodes may not match. Figure out the fewest number of type
1414 // comparisons we need to emit. For example, if there is only one integer
1415 // type supported by a target, there should be no type comparisons at all for
1416 // integer patterns!
1417 //
1418 // To figure out the fewest number of type checks needed, clone the pattern,
1419 // remove the types, then perform type inference on the pattern as a whole.
1420 // If there are unresolved types, emit an explicit check for those types,
1421 // apply the type to the tree, then rerun type inference. Iterate until all
1422 // types are resolved.
1423 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001424 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001425 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001426
1427 do {
1428 // Resolve/propagate as many types as possible.
1429 try {
1430 bool MadeChange = true;
1431 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001432 MadeChange = Pat->ApplyTypeConstraints(TP,
1433 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001434 } catch (...) {
1435 assert(0 && "Error: could not find consistent types for something we"
1436 " already decided was ok!");
1437 abort();
1438 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001439
Chris Lattner7e82f132005-10-15 21:34:21 +00001440 // Insert a check for an unresolved type and add it to the tree. If we find
1441 // an unresolved type to add a check for, this returns true and we iterate,
1442 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001443 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001444
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001445 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001446 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001447 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001448}
1449
Chris Lattner24e00a42006-01-29 04:41:05 +00001450/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1451/// a line causes any of them to be empty, remove them and return true when
1452/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001453static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001454 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001455 &Patterns) {
1456 bool ErasedPatterns = false;
1457 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1458 Patterns[i].second.pop_back();
1459 if (Patterns[i].second.empty()) {
1460 Patterns.erase(Patterns.begin()+i);
1461 --i; --e;
1462 ErasedPatterns = true;
1463 }
1464 }
1465 return ErasedPatterns;
1466}
1467
Chris Lattner8bc74722006-01-29 04:25:26 +00001468/// EmitPatterns - Emit code for at least one pattern, but try to group common
1469/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001470void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001471 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001472 &Patterns, unsigned Indent,
1473 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001474 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001475 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001476 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001477
1478 if (Patterns.empty()) return;
1479
Chris Lattner24e00a42006-01-29 04:41:05 +00001480 // Figure out how many patterns share the next code line. Explicitly copy
1481 // FirstCodeLine so that we don't invalidate a reference when changing
1482 // Patterns.
1483 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001484 unsigned LastMatch = Patterns.size()-1;
1485 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1486 --LastMatch;
1487
1488 // If not all patterns share this line, split the list into two pieces. The
1489 // first chunk will use this line, the second chunk won't.
1490 if (LastMatch != 0) {
1491 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1492 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1493
1494 // FIXME: Emit braces?
1495 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001496 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001497 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1498 Pattern.getSrcPattern()->print(OS);
1499 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1500 Pattern.getDstPattern()->print(OS);
1501 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001502 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001503 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001504 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001505 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001506 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001507 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001508 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001509 }
Evan Cheng676d7312006-08-26 00:59:04 +00001510 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001511 OS << std::string(Indent, ' ') << "{\n";
1512 Indent += 2;
1513 }
1514 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001515 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001516 Indent -= 2;
1517 OS << std::string(Indent, ' ') << "}\n";
1518 }
1519
1520 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001521 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001522 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1523 Pattern.getSrcPattern()->print(OS);
1524 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1525 Pattern.getDstPattern()->print(OS);
1526 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001527 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001528 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001529 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001530 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001531 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001532 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001533 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001534 }
1535 EmitPatterns(Other, Indent, OS);
1536 return;
1537 }
1538
Chris Lattner24e00a42006-01-29 04:41:05 +00001539 // Remove this code from all of the patterns that share it.
1540 bool ErasedPatterns = EraseCodeLine(Patterns);
1541
Evan Cheng676d7312006-08-26 00:59:04 +00001542 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001543
1544 // Otherwise, every pattern in the list has this line. Emit it.
1545 if (!isPredicate) {
1546 // Normal code.
1547 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1548 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001549 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1550
1551 // If the next code line is another predicate, and if all of the pattern
1552 // in this group share the same next line, emit it inline now. Do this
1553 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001554 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001555 // Check that all of fhe patterns in Patterns end with the same predicate.
1556 bool AllEndWithSamePredicate = true;
1557 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1558 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1559 AllEndWithSamePredicate = false;
1560 break;
1561 }
1562 // If all of the predicates aren't the same, we can't share them.
1563 if (!AllEndWithSamePredicate) break;
1564
1565 // Otherwise we can. Emit it shared now.
1566 OS << " &&\n" << std::string(Indent+4, ' ')
1567 << Patterns.back().second.back().second;
1568 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001569 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001570
1571 OS << ") {\n";
1572 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001573 }
1574
1575 EmitPatterns(Patterns, Indent, OS);
1576
1577 if (isPredicate)
1578 OS << std::string(Indent-2, ' ') << "}\n";
1579}
1580
Evan Cheng892aaf82006-11-08 23:01:03 +00001581static std::string getLegalCName(std::string OpName) {
1582 std::string::size_type pos = OpName.find("::");
1583 if (pos != std::string::npos)
1584 OpName.replace(pos, 2, "_");
1585 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001586}
1587
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001588void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001589 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001590
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001591 // Get the namespace to insert instructions into.
1592 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001593 if (!InstNS.empty()) InstNS += "::";
1594
Chris Lattner602f6922006-01-04 00:25:00 +00001595 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001596 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001597 // All unique target node emission functions.
1598 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001599 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001600 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001601 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001602
1603 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001604 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001605 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001606 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001607 } else {
1608 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001609 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001610 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001611 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001612 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001613 std::vector<Record*> OpNodes = CP->getRootNodes();
1614 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001615 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1616 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001617 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001618 }
1619 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001620 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001621 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001622 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001623 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001624 exit(1);
1625 }
1626 }
1627 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001628
1629 // For each opcode, there might be multiple select functions, one per
1630 // ValueType of the node (or its first operand if it doesn't produce a
1631 // non-chain result.
1632 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1633
Chris Lattner602f6922006-01-04 00:25:00 +00001634 // Emit one Select_* method for each top-level opcode. We do this instead of
1635 // emitting one giant switch statement to support compilers where this will
1636 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001637 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001638 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1639 PBOI != E; ++PBOI) {
1640 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001641 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001642 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1643
Chris Lattner602f6922006-01-04 00:25:00 +00001644 // We want to emit all of the matching code now. However, we want to emit
1645 // the matches in order of minimal cost. Sort the patterns so the least
1646 // cost one is at the start.
Chris Lattner706d2d32006-08-09 16:44:44 +00001647 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001648 PatternSortingPredicate(CGP));
Evan Cheng21ad3922006-02-07 00:37:41 +00001649
Chris Lattner706d2d32006-08-09 16:44:44 +00001650 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001651 std::map<MVT::SimpleValueType,
1652 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001653 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001654 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001655 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001656 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001657 }
1658
Duncan Sands83ec4b62008-06-06 12:08:01 +00001659 for (std::map<MVT::SimpleValueType,
1660 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001661 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1662 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001663 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001664 std::vector<const PatternToMatch*> &Patterns = II->second;
Chris Lattner64906972006-09-21 18:28:27 +00001665 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1666 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001667
Chris Lattner60d81392008-01-05 22:30:17 +00001668 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001669 std::vector<std::vector<std::string> > PatternOpcodes;
1670 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001671 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001672 std::vector<bool> OutputIsVariadicFlags;
1673 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001674 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1675 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001676 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001677 std::vector<std::string> TargetOpcodes;
1678 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001679 bool OutputIsVariadic;
1680 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001681 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001682 TargetOpcodes, TargetVTs,
1683 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001684 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1685 PatternDecls.push_back(GeneratedDecl);
1686 PatternOpcodes.push_back(TargetOpcodes);
1687 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001688 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1689 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001690 }
1691
1692 // Scan the code to see if all of the patterns are reachable and if it is
1693 // possible that the last one might not match.
1694 bool mightNotMatch = true;
1695 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1696 CodeList &GeneratedCode = CodeForPatterns[i].second;
1697 mightNotMatch = false;
1698
1699 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001700 if (GeneratedCode[j].first == 1) { // predicate.
Chris Lattner706d2d32006-08-09 16:44:44 +00001701 mightNotMatch = true;
1702 break;
1703 }
1704 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001705
Chris Lattner706d2d32006-08-09 16:44:44 +00001706 // If this pattern definitely matches, and if it isn't the last one, the
1707 // patterns after it CANNOT ever match. Error out.
1708 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001709 cerr << "Pattern '";
1710 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1711 cerr << "' is impossible to select!\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001712 exit(1);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001713 }
1714 }
1715
Chris Lattner706d2d32006-08-09 16:44:44 +00001716 // Factor target node emission code (emitted by EmitResultCode) into
1717 // separate functions. Uniquing and share them among all instruction
1718 // selection routines.
1719 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1720 CodeList &GeneratedCode = CodeForPatterns[i].second;
1721 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1722 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001723 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001724 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1725 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001726 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001727 int CodeSize = (int)GeneratedCode.size();
1728 int LastPred = -1;
1729 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001730 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001731 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001732 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1733 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001734 }
1735
Dan Gohman475871a2008-07-27 21:46:04 +00001736 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001737 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001738 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1739 CalleeCode += ", unsigned Opc" + utostr(j);
1740 CallerCode += ", " + TargetOpcodes[j];
1741 }
1742 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001743 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001744 CallerCode += ", " + TargetVTs[j];
1745 }
Evan Chengf5493192006-08-26 01:02:19 +00001746 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001747 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001748 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001749 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001750 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001751 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001752
1753 if (OutputIsVariadic) {
1754 CalleeCode += ", unsigned NumInputRootOps";
1755 CallerCode += ", " + utostr(NumInputRootOps);
1756 }
1757
Chris Lattner706d2d32006-08-09 16:44:44 +00001758 CallerCode += ");";
1759 CalleeCode += ") ";
1760 // Prevent emission routines from being inlined to reduce selection
1761 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001762 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001763 CalleeCode += "{\n";
1764
1765 for (std::vector<std::string>::const_reverse_iterator
1766 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1767 CalleeCode += " " + *I + "\n";
1768
Evan Chengf5493192006-08-26 01:02:19 +00001769 for (int j = LastPred+1; j < CodeSize; ++j)
1770 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001771 for (int j = LastPred+1; j < CodeSize; ++j)
1772 GeneratedCode.pop_back();
1773 CalleeCode += "}\n";
1774
1775 // Uniquing the emission routines.
1776 unsigned EmitFuncNum;
1777 std::map<std::string, unsigned>::iterator EFI =
1778 EmitFunctions.find(CalleeCode);
1779 if (EFI != EmitFunctions.end()) {
1780 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001781 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001782 EmitFuncNum = EmitFunctions.size();
1783 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001784 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001785 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001786
Chris Lattner706d2d32006-08-09 16:44:44 +00001787 // Replace the emission code within selection routines with calls to the
1788 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001789 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001790 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001791 }
1792
Chris Lattner706d2d32006-08-09 16:44:44 +00001793 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001794 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001795 if (OpVT == MVT::iPTR) {
1796 OpVTStr = "_iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +00001797 } else if (OpVT == MVT::iPTRAny) {
1798 OpVTStr = "_iPTRAny";
Chris Lattner33a40042006-11-14 22:17:10 +00001799 } else if (OpVT == MVT::isVoid) {
1800 // Nodes with a void result actually have a first result type of either
1801 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1802 // void to this case, we handle it specially here.
1803 } else {
1804 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1805 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001806 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1807 OpcodeVTMap.find(OpName);
1808 if (OpVTI == OpcodeVTMap.end()) {
1809 std::vector<std::string> VTSet;
1810 VTSet.push_back(OpVTStr);
1811 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1812 } else
1813 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001814
Evan Cheng892aaf82006-11-08 23:01:03 +00001815 OS << "SDNode *Select_" << getLegalCName(OpName)
Dan Gohman475871a2008-07-27 21:46:04 +00001816 << OpVTStr << "(const SDValue &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001817
Chris Lattner706d2d32006-08-09 16:44:44 +00001818 // Loop through and reverse all of the CodeList vectors, as we will be
1819 // accessing them from their logical front, but accessing the end of a
1820 // vector is more efficient.
1821 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1822 CodeList &GeneratedCode = CodeForPatterns[i].second;
1823 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001824 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001825
1826 // Next, reverse the list of patterns itself for the same reason.
1827 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1828
1829 // Emit all of the patterns now, grouped together to share code.
1830 EmitPatterns(CodeForPatterns, 2, OS);
1831
Chris Lattner64906972006-09-21 18:28:27 +00001832 // If the last pattern has predicates (which could fail) emit code to
1833 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001834 if (mightNotMatch) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001835 OS << " cerr << \"Cannot yet select: \";\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001836 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1837 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1838 OpName != "ISD::INTRINSIC_VOID") {
Gabor Greifba36cb52008-08-28 21:40:38 +00001839 OS << " N.getNode()->dump(CurDAG);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001840 } else {
1841 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1842 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001843 << " cerr << \"intrinsic %\"<< "
Chris Lattner706d2d32006-08-09 16:44:44 +00001844 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1845 }
Bill Wendlingf5da1332006-12-07 22:21:48 +00001846 OS << " cerr << '\\n';\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001847 << " abort();\n"
1848 << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001849 }
1850 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001851 }
Chris Lattner602f6922006-01-04 00:25:00 +00001852 }
1853
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001854 // Emit boilerplate.
Dan Gohman475871a2008-07-27 21:46:04 +00001855 OS << "SDNode *Select_INLINEASM(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001856 << " std::vector<SDValue> Ops(N.getNode()->op_begin(), N.getNode()->op_end());\n"
Dan Gohmanf350b272008-08-23 02:25:05 +00001857 << " SelectInlineAsmMemoryOperands(Ops);\n\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001858
1859 << " // Ensure that the asm operands are themselves selected.\n"
1860 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1861 << " AddToISelQueue(Ops[j]);\n\n"
1862
Duncan Sands83ec4b62008-06-06 12:08:01 +00001863 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001864 << " VTs.push_back(MVT::Other);\n"
1865 << " VTs.push_back(MVT::Flag);\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001866 << " SDValue New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
Chris Lattner706d2d32006-08-09 16:44:44 +00001867 "Ops.size());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001868 << " return New.getNode();\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001869 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001870
Dan Gohman475871a2008-07-27 21:46:04 +00001871 OS << "SDNode *Select_UNDEF(const SDValue &N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001872 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::IMPLICIT_DEF,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001873 << " N.getValueType());\n"
1874 << "}\n\n";
1875
Dan Gohman475871a2008-07-27 21:46:04 +00001876 OS << "SDNode *Select_DBG_LABEL(const SDValue &N) {\n"
1877 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001878 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001879 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001880 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001881 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DBG_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001882 << " MVT::Other, Tmp, Chain);\n"
1883 << "}\n\n";
1884
Dan Gohman475871a2008-07-27 21:46:04 +00001885 OS << "SDNode *Select_EH_LABEL(const SDValue &N) {\n"
1886 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001887 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001888 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001889 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001890 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EH_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001891 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001892 << "}\n\n";
1893
Dan Gohman475871a2008-07-27 21:46:04 +00001894 OS << "SDNode *Select_DECLARE(const SDValue &N) {\n"
1895 << " SDValue Chain = N.getOperand(0);\n"
1896 << " SDValue N1 = N.getOperand(1);\n"
1897 << " SDValue N2 = N.getOperand(2);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001898 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
1899 << " cerr << \"Cannot yet select llvm.dbg.declare: \";\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001900 << " N.getNode()->dump(CurDAG);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001901 << " abort();\n"
1902 << " }\n"
1903 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1904 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001905 << " SDValue Tmp1 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001906 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001907 << " SDValue Tmp2 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001908 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1909 << " AddToISelQueue(Chain);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001910 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DECLARE,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001911 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001912 << "}\n\n";
1913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 OS << "SDNode *Select_EXTRACT_SUBREG(const SDValue &N) {\n"
1915 << " SDValue N0 = N.getOperand(0);\n"
1916 << " SDValue N1 = N.getOperand(1);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001917 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001918 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001919 << " AddToISelQueue(N0);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001920 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EXTRACT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001921 << " N.getValueType(), N0, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001922 << "}\n\n";
1923
Dan Gohman475871a2008-07-27 21:46:04 +00001924 OS << "SDNode *Select_INSERT_SUBREG(const SDValue &N) {\n"
1925 << " SDValue N0 = N.getOperand(0);\n"
1926 << " SDValue N1 = N.getOperand(1);\n"
1927 << " SDValue N2 = N.getOperand(2);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001928 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001929 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001930 << " AddToISelQueue(N1);\n"
Christopher Lamb6634e262008-03-13 05:47:01 +00001931 << " AddToISelQueue(N0);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001932 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::INSERT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001933 << " N.getValueType(), N0, N1, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001934 << "}\n\n";
1935
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001936 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001937 << "SDNode *SelectCode(SDValue N) {\n"
Dan Gohmane8be6c62008-07-17 19:10:17 +00001938 << " if (N.isMachineOpcode()) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001939 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001940 << " }\n\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001941 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001942 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001943 << " default: break;\n"
1944 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001945 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001946 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001947 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001948 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001949 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001950 << " case ISD::TargetConstantPool:\n"
1951 << " case ISD::TargetFrameIndex:\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001952 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001953 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001954 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001955 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001956 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001957 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001958 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001959 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001960 << " AddToISelQueue(N.getOperand(0));\n"
1961 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001962 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001963 << " }\n"
1964 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001965 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001966 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001967 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1968 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001969 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001970 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001971 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001972 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
1973 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001974 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001975 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001976 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1977 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001978
Chris Lattner54cb8fd2005-09-07 23:44:43 +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"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002032 << " cerr << \"Cannot yet select: \";\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002033 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2034 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2035 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00002036 << " N.getNode()->dump(CurDAG);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002037 << " } else {\n"
2038 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2039 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002040 << " cerr << \"intrinsic %\"<< "
2041 "Intrinsic::getName((Intrinsic::ID)iid);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002042 << " }\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002043 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002044 << " abort();\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002045 << " return NULL;\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002046 << "}\n";
2047}
2048
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002049void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002050 EmitSourceFileHeader("DAG Instruction Selector for the " +
2051 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002052
Chris Lattner1f39e292005-09-14 00:09:24 +00002053 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2054 << "// *** instruction selector class. These functions are really "
2055 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002056
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002057 OS << "// Include standard, target-independent definitions and methods used\n"
2058 << "// by the instruction selector.\n";
2059 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002060
Chris Lattner443e3f92008-01-05 22:54:53 +00002061 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002062 EmitPredicateFunctions(OS);
2063
Bill Wendlingf5da1332006-12-07 22:21:48 +00002064 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002065 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002066 I != E; ++I) {
2067 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2068 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002069 DOUT << "\n";
2070 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002071
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002072 // At this point, we have full information about the 'Patterns' we need to
2073 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002074 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002075 EmitInstructionSelector(OS);
2076
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002077}