blob: 60383f49c388e2bc00b5d3285bd5d2c29be13c4a [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"
David Greene8ad4c002008-10-27 21:56:29 +000017#include "llvm/Support/CommandLine.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000018#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000019#include "llvm/Support/MathExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000020#include "llvm/Support/Debug.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000021#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000022#include <deque>
Daniel Dunbar1a551802009-07-03 00:10:29 +000023#include <iostream>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024using namespace llvm;
25
Chris Lattner3d4ad292009-08-07 22:27:19 +000026static cl::opt<bool>
27GenDebug("gen-debug", cl::desc("Generate debug code"), cl::init(false));
David Greene8ad4c002008-10-27 21:56:29 +000028
Chris Lattnerca559d02005-09-08 21:03:01 +000029//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000030// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000031//
32
Chris Lattner6cefb772008-01-05 22:25:12 +000033/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
34/// ComplexPattern.
35static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000036 return (N->isLeaf() &&
37 dynamic_cast<DefInit*>(N->getLeafValue()) &&
38 static_cast<DefInit*>(N->getLeafValue())->getDef()->
39 isSubClassOf("ComplexPattern"));
40}
41
Chris Lattner6cefb772008-01-05 22:25:12 +000042/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
43/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000044static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000045 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000046 if (N->isLeaf() &&
47 dynamic_cast<DefInit*>(N->getLeafValue()) &&
48 static_cast<DefInit*>(N->getLeafValue())->getDef()->
49 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000050 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
51 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000052 }
53 return NULL;
54}
55
Chris Lattner05814af2005-09-28 17:57:56 +000056/// getPatternSize - Return the 'size' of this pattern. We want to match large
57/// patterns before small ones. This is used to determine the size of a
58/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000059static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Owen Andersone50ed302009-08-10 22:56:29 +000060 assert((EEVT::isExtIntegerInVTs(P->getExtTypes()) ||
61 EEVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +000062 P->getExtTypeNum(0) == MVT::isVoid ||
63 P->getExtTypeNum(0) == MVT::Flag ||
64 P->getExtTypeNum(0) == MVT::iPTR ||
65 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000066 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000067 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000068 // If the root node is a ConstantSDNode, increases its size.
69 // e.g. (set R32:$dst, 0).
70 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000071 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000072
73 // FIXME: This is a hack to statically increase the priority of patterns
74 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
75 // Later we can allow complexity / cost for each pattern to be (optionally)
76 // specified. To get best possible pattern match we'll need to dynamically
77 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000078 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000079 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000080 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000081
82 // If this node has some predicate function that must match, it adds to the
83 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000084 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000085 ++Size;
86
Chris Lattner05814af2005-09-28 17:57:56 +000087 // Count children in the count if they are also nodes.
88 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
89 TreePatternNode *Child = P->getChild(i);
Owen Anderson825b72b2009-08-11 20:47:22 +000090 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000091 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000092 else if (Child->isLeaf()) {
93 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000094 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000095 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000096 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +000097 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000098 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000099 }
Chris Lattner05814af2005-09-28 17:57:56 +0000100 }
101
102 return Size;
103}
104
105/// getResultPatternCost - Compute the number of instructions for this pattern.
106/// This is a temporary hack. We should really include the instruction
107/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000108static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000109 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000110 if (P->isLeaf()) return 0;
111
Evan Chengfbad7082006-02-18 02:33:09 +0000112 unsigned Cost = 0;
113 Record *Op = P->getOperator();
114 if (Op->isSubClassOf("Instruction")) {
115 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000116 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Dan Gohman533297b2009-10-29 18:10:34 +0000117 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +0000118 Cost += 10;
119 }
Chris Lattner05814af2005-09-28 17:57:56 +0000120 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000121 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000122 return Cost;
123}
124
Evan Chenge6f32032006-07-19 00:24:41 +0000125/// getResultPatternCodeSize - Compute the code size of instructions for this
126/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000127static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000128 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000129 if (P->isLeaf()) return 0;
130
131 unsigned Cost = 0;
132 Record *Op = P->getOperator();
133 if (Op->isSubClassOf("Instruction")) {
134 Cost += Op->getValueAsInt("CodeSize");
135 }
136 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000137 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000138 return Cost;
139}
140
Chris Lattner05814af2005-09-28 17:57:56 +0000141// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
142// In particular, we want to match maximal patterns first and lowest cost within
143// a particular complexity first.
144struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000145 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
146 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000147
Dan Gohman0540e172008-10-15 06:17:21 +0000148 typedef std::pair<unsigned, std::string> CodeLine;
149 typedef std::vector<CodeLine> CodeList;
150 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
151
152 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
153 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
154 const PatternToMatch *LHS = LHSPair.first;
155 const PatternToMatch *RHS = RHSPair.first;
156
Chris Lattner6cefb772008-01-05 22:25:12 +0000157 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
158 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000159 LHSSize += LHS->getAddedComplexity();
160 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000161 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
162 if (LHSSize < RHSSize) return false;
163
164 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000165 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
166 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000167 if (LHSCost < RHSCost) return true;
168 if (LHSCost > RHSCost) return false;
169
Chris Lattner6cefb772008-01-05 22:25:12 +0000170 return getResultPatternSize(LHS->getDstPattern(), CGP) <
171 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000172 }
173};
174
Jim Grosbach54f30222009-03-25 23:28:33 +0000175/// getRegisterValueType - Look up and return the ValueType of the specified
176/// register. If the register is a member of multiple register classes which
Owen Anderson825b72b2009-08-11 20:47:22 +0000177/// have different associated types, return MVT::Other.
178static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000179 bool FoundRC = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 MVT::SimpleValueType VT = MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000181 const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses();
182 std::vector<CodeGenRegisterClass>::const_iterator RC;
183 std::vector<Record*>::const_iterator Element;
184
185 for (RC = RCs.begin() ; RC != RCs.end() ; RC++) {
186 Element = find((*RC).Elements.begin(), (*RC).Elements.end(), R);
187 if (Element != (*RC).Elements.end()) {
188 if (!FoundRC) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000189 FoundRC = true;
Jim Grosbach54f30222009-03-25 23:28:33 +0000190 VT = (*RC).getValueTypeNum(0);
191 } else {
192 // In multiple RC's
193 if (VT != (*RC).getValueTypeNum(0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 // Types of the RC's do not agree. Return MVT::Other. The
Jim Grosbach54f30222009-03-25 23:28:33 +0000195 // target is responsible for handling this.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 return MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000197 }
198 }
199 }
200 }
201 return VT;
Evan Cheng66a48bb2005-12-01 00:18:45 +0000202}
203
Chris Lattner72fe91c2005-09-24 00:40:24 +0000204
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000205/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
206/// type information from it.
207static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000208 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000209 if (!N->isLeaf())
210 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
211 RemoveAllTypes(N->getChild(i));
212}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000213
Evan Cheng51fecc82006-01-09 18:27:06 +0000214/// NodeHasProperty - return true if TreePatternNode has the specified
215/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000216static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000217 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000218 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000219 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000220 if (CP)
221 return CP->hasProperty(Property);
222 return false;
223 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000224 Record *Operator = N->getOperator();
225 if (!Operator->isSubClassOf("SDNode")) return false;
226
Chris Lattner6cefb772008-01-05 22:25:12 +0000227 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000228}
229
Evan Cheng94b30402006-10-11 21:02:01 +0000230static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000231 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000232 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000233 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000234
235 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
236 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000237 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000238 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000239 }
240
241 return false;
242}
243
Evan Chengf9d03182008-07-03 08:39:51 +0000244static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
245 return CGP.getSDNodeInfo(Op).getEnumName();
246}
247
248static
249bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
250 bool isStore = !N->isLeaf() &&
251 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
252 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
253 return false;
254
255 bool HasChain = false;
256 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
257 TreePatternNode *Child = N->getChild(i);
258 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
259 HasChain = true;
260 break;
261 }
262 }
263 return HasChain;
264}
265
Chris Lattnerdc32f982008-01-05 22:43:57 +0000266//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000267// Node Transformation emitter implementation.
268//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000269void DAGISelEmitter::EmitNodeTransforms(raw_ostream &OS) {
Chris Lattner443e3f92008-01-05 22:54:53 +0000270 // Walk the pattern fragments, adding them to a map, which sorts them by
271 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000272 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000273 NXsByNameTy NXsByName;
274
Chris Lattnerfe718932008-01-06 01:10:31 +0000275 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000276 I != E; ++I)
277 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
278
279 OS << "\n// Node transformations.\n";
280
281 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
282 I != E; ++I) {
283 Record *SDNode = I->second.first;
284 std::string Code = I->second.second;
285
286 if (Code.empty()) continue; // Empty code? Skip it.
287
Chris Lattner200c57e2008-01-05 22:58:54 +0000288 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000289 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
290
Dan Gohman475871a2008-07-27 21:46:04 +0000291 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000292 << ") {\n";
293 if (ClassName != "SDNode")
294 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
295 OS << Code << "\n}\n";
296 }
297}
298
299//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000300// Predicate emitter implementation.
301//
302
Daniel Dunbar1a551802009-07-03 00:10:29 +0000303void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
Chris Lattnerdc32f982008-01-05 22:43:57 +0000304 OS << "\n// Predicate functions.\n";
305
306 // Walk the pattern fragments, adding them to a map, which sorts them by
307 // name.
308 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
309 PFsByNameTy PFsByName;
310
Chris Lattnerfe718932008-01-06 01:10:31 +0000311 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000312 I != E; ++I)
313 PFsByName.insert(std::make_pair(I->first->getName(), *I));
314
315
316 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
317 I != E; ++I) {
318 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
319 TreePattern *P = I->second.second;
320
321 // If there is a code init for this fragment, emit the predicate code.
322 std::string Code = PatFragRecord->getValueAsCode("Predicate");
323 if (Code.empty()) continue;
324
325 if (P->getOnlyTree()->isLeaf())
326 OS << "inline bool Predicate_" << PatFragRecord->getName()
327 << "(SDNode *N) {\n";
328 else {
329 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000330 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000331 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
332
333 OS << "inline bool Predicate_" << PatFragRecord->getName()
334 << "(SDNode *" << C2 << ") {\n";
335 if (ClassName != "SDNode")
336 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
337 }
338 OS << Code << "\n}\n";
339 }
340
341 OS << "\n\n";
342}
343
344
345//===----------------------------------------------------------------------===//
346// PatternCodeEmitter implementation.
347//
Evan Chengb915f312005-12-09 22:45:35 +0000348class PatternCodeEmitter {
349private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000350 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000351
Evan Cheng58e84a62005-12-14 22:02:59 +0000352 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000353 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000354 // Pattern cost.
355 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000356 // Instruction selector pattern.
357 TreePatternNode *Pattern;
358 // Matched instruction.
359 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000360
Evan Chengb915f312005-12-09 22:45:35 +0000361 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000362 std::map<std::string, std::string> VariableMap;
363 // Node to operator mapping
364 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000365 // Name of the folded node which produces a flag.
366 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000367 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000368 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000369 // Original input chain(s).
370 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000371 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000372
Dan Gohman69de1932008-02-06 22:27:42 +0000373 /// LSI - Load/Store information.
374 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
375 /// for each memory access. This facilitates the use of AliasAnalysis in
376 /// the backend.
377 std::vector<std::string> LSI;
378
Evan Cheng676d7312006-08-26 00:59:04 +0000379 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000380 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000381 /// tested, and if true, the match fails) [when 1], or normal code to emit
382 /// [when 0], or initialization code to emit [when 2].
383 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000384 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000385 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000386 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000387 /// TargetOpcodes - The target specific opcodes used by the resulting
388 /// instructions.
389 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000390 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000391 /// OutputIsVariadic - Records whether the instruction output pattern uses
392 /// variable_ops. This requires that the Emit function be passed an
393 /// additional argument to indicate where the input varargs operands
394 /// begin.
395 bool &OutputIsVariadic;
396 /// NumInputRootOps - Records the number of operands the root node of the
397 /// input pattern has. This information is used in the generated code to
398 /// pass to Emit functions when variable_ops processing is needed.
399 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000400
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000401 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000402 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000403 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000404 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000405
406 void emitCheck(const std::string &S) {
407 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000408 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000409 }
410 void emitCode(const std::string &S) {
411 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000412 GeneratedCode.push_back(std::make_pair(0, S));
413 }
414 void emitInit(const std::string &S) {
415 if (!S.empty())
416 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000417 }
Evan Chengf5493192006-08-26 01:02:19 +0000418 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000419 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000420 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000421 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000422 void emitOpcode(const std::string &Opc) {
423 TargetOpcodes.push_back(Opc);
424 OpcNo++;
425 }
Evan Chengf8729402006-07-16 06:12:52 +0000426 void emitVT(const std::string &VT) {
427 TargetVTs.push_back(VT);
428 VTNo++;
429 }
Evan Chengb915f312005-12-09 22:45:35 +0000430public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000431 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000432 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000433 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000434 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000435 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000436 std::vector<std::string> &tv,
437 bool &oiv,
438 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000439 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000440 GeneratedCode(gc), GeneratedDecl(gd),
441 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000442 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000443 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000444
445 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
446 /// if the match fails. At this point, we already know that the opcode for N
447 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000448 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
449 const std::string &RootName, const std::string &ChainSuffix,
450 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000451
452 // Save loads/stores matched by a pattern.
453 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000454 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000455 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000456 }
457
Evan Chenge41bf822006-02-05 06:43:12 +0000458 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000459 // Emit instruction predicates. Each predicate is just a string for now.
460 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000461 // Record input varargs info.
462 NumInputRootOps = N->getNumChildren();
463
Evan Chengf9d03182008-07-03 08:39:51 +0000464 if (DisablePatternForFastISel(N, CGP))
Bill Wendling98a366d2009-04-29 23:29:43 +0000465 emitCheck("OptLevel != CodeGenOpt::None");
Evan Chengf9d03182008-07-03 08:39:51 +0000466
Chris Lattner8a0604b2006-01-28 20:31:24 +0000467 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000468 }
469
Evan Chengb915f312005-12-09 22:45:35 +0000470 if (N->isLeaf()) {
471 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000472 emitCheck("cast<ConstantSDNode>(" + RootName +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000473 ")->getSExtValue() == INT64_C(" +
474 itostr(II->getValue()) + ")");
Evan Chengb915f312005-12-09 22:45:35 +0000475 return;
476 } else if (!NodeIsComplexPattern(N)) {
477 assert(0 && "Cannot match this as a leaf value!");
478 abort();
479 }
480 }
481
Chris Lattner488580c2006-01-28 19:06:51 +0000482 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000483 // we already saw this in the pattern, emit code to verify dagness.
484 if (!N->getName().empty()) {
485 std::string &VarMapEntry = VariableMap[N->getName()];
486 if (VarMapEntry.empty()) {
487 VarMapEntry = RootName;
488 } else {
489 // If we get here, this is a second reference to a specific name. Since
490 // we already have checked that the first reference is valid, we don't
491 // have to recursively match it, just check that it's the same as the
492 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000493 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000494 return;
495 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000496
497 if (!N->isLeaf())
498 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000499 }
500
501
502 // Emit code to load the child nodes and match their contents recursively.
503 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000504 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
505 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000506 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000507 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000508 if (NodeHasChain)
509 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000510 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000511 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000512 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000513 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000514 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000515 // If the immediate use can somehow reach this node through another
516 // path, then can't fold it either or it will create a cycle.
517 // e.g. In the following diagram, XX can reach ld through YY. If
518 // ld is folded into XX, then YY is both a predecessor and a successor
519 // of XX.
520 //
521 // [ld]
522 // ^ ^
523 // | |
524 // / \---
525 // / [YY]
526 // | ^
527 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000528 bool NeedCheck = P != Pattern;
529 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000530 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000531 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000532 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
533 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
534 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000535 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000536 PInfo.hasProperty(SDNPHasChain) ||
537 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000538 PInfo.hasProperty(SDNPOptInFlag);
539 }
540
541 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000542 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng884c70c2008-11-27 00:49:46 +0000543 emitCheck("IsLegalAndProfitableToFold(" + RootName +
544 ".getNode(), " + ParentName + ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000545 }
Evan Chenge41bf822006-02-05 06:43:12 +0000546 }
Evan Chengb915f312005-12-09 22:45:35 +0000547 }
Evan Chenge41bf822006-02-05 06:43:12 +0000548
Evan Chengc15d18c2006-01-27 22:13:45 +0000549 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000550 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000551 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
552 "IsChainCompatible(" + ChainName + ".getNode(), " +
553 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000554 OrigChains.push_back(std::make_pair(ChainName, RootName));
555 } else
Evan Chenge6389932006-07-21 22:19:51 +0000556 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000557 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000558 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000559 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000560 }
Evan Chengb915f312005-12-09 22:45:35 +0000561 }
562
Evan Cheng54597732006-01-26 00:22:25 +0000563 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000564 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000565 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000566 // FIXME: If the optional incoming flag does not exist. Then it is ok to
567 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000568 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000569 (PatternHasProperty(N, SDNPInFlag, CGP) ||
570 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
571 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000572 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000573 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000574 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000575 }
576 }
577
Dan Gohman0540e172008-10-15 06:17:21 +0000578 // If there are node predicates for this, emit the calls.
579 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
580 emitCheck(N->getPredicateFns()[i] + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000581
Chris Lattner39e73f72006-10-11 04:05:55 +0000582 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
583 // a constant without a predicate fn that has more that one bit set, handle
584 // this as a special case. This is usually for targets that have special
585 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
586 // handling stuff). Using these instructions is often far more efficient
587 // than materializing the constant. Unfortunately, both the instcombiner
588 // and the dag combiner can often infer that bits are dead, and thus drop
589 // them from the mask in the dag. For example, it might turn 'AND X, 255'
590 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
591 // to handle this.
592 if (!N->isLeaf() &&
593 (N->getOperator()->getName() == "and" ||
594 N->getOperator()->getName() == "or") &&
595 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000596 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000597 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
598 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000599 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000600 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000601 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000602 RootName + ".getOperand(" + utostr(1) + ");");
603
Dan Gohman0b53d982008-12-19 18:13:39 +0000604 unsigned NTmp = TmpNo++;
605 emitCode("ConstantSDNode *Tmp" + utostr(NTmp) +
606 " = dyn_cast<ConstantSDNode>(" + RootName + "1);");
607 emitCheck("Tmp" + utostr(NTmp));
Chris Lattner39e73f72006-10-11 04:05:55 +0000608 const char *MaskPredicate = N->getOperator()->getName() == "or"
609 ? "CheckOrMask(" : "CheckAndMask(";
Dan Gohman0b53d982008-12-19 18:13:39 +0000610 emitCheck(MaskPredicate + RootName + "0, Tmp" + utostr(NTmp) +
611 ", INT64_C(" + itostr(II->getValue()) + "))");
Chris Lattner39e73f72006-10-11 04:05:55 +0000612
Dan Gohman537ab902010-01-04 20:31:55 +0000613 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0),
Chris Lattner39e73f72006-10-11 04:05:55 +0000614 ChainSuffix + utostr(0), FoundChain);
615 return;
616 }
617 }
618 }
619
Evan Chengb915f312005-12-09 22:45:35 +0000620 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000621 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000622 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000623
Dan Gohman537ab902010-01-04 20:31:55 +0000624 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo),
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000625 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000626 }
627
Evan Cheng676d7312006-08-26 00:59:04 +0000628 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000629 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000630 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000631 std::string Fn = CP->getSelectFunc();
632 unsigned NumOps = CP->getNumOperands();
633 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000634 emitDecl("CPTmp" + RootName + "_" + utostr(i));
635 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000636 }
Evan Cheng94b30402006-10-11 21:02:01 +0000637 if (CP->hasProperty(SDNPHasChain)) {
638 emitDecl("CPInChain");
639 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000640 emitCode("SDValue CPInChain;");
641 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000642 }
Evan Cheng676d7312006-08-26 00:59:04 +0000643
Evan Cheng811731e2006-11-08 20:31:10 +0000644 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000645 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000646 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000647 if (CP->hasProperty(SDNPHasChain)) {
648 ChainName = "Chain" + ChainSuffix;
649 Code += ", CPInChain, Chain" + ChainSuffix;
650 }
Evan Cheng676d7312006-08-26 00:59:04 +0000651 emitCheck(Code + ")");
652 }
Evan Chengb915f312005-12-09 22:45:35 +0000653 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000654
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000655 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000656 const std::string &RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000657 const std::string &ChainSuffix, bool &FoundChain) {
658 if (!Child->isLeaf()) {
659 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000660 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000661 emitCheck(RootName + ".getOpcode() == " +
662 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000663 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000664 bool HasChain = false;
665 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
666 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000667 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000668 }
Dale Johannesen874ae252009-06-02 03:12:52 +0000669 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
Evan Chenga58891f2008-02-05 22:50:29 +0000670 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
671 "Pattern folded multiple nodes which produce flags?");
672 FoldedFlag = std::make_pair(RootName,
673 CInfo.getNumResults() + (unsigned)HasChain);
674 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000675 } else {
676 // If this child has a name associated with it, capture it in VarMap. If
677 // we already saw this in the pattern, emit code to verify dagness.
678 if (!Child->getName().empty()) {
679 std::string &VarMapEntry = VariableMap[Child->getName()];
680 if (VarMapEntry.empty()) {
681 VarMapEntry = RootName;
682 } else {
683 // If we get here, this is a second reference to a specific name.
684 // Since we already have checked that the first reference is valid,
685 // we don't have to recursively match it, just check that it's the
686 // same as the previously named thing.
687 emitCheck(VarMapEntry + " == " + RootName);
688 Duplicates.insert(RootName);
689 return;
690 }
691 }
692
693 // Handle leaves of various types.
694 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
695 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000696 if (LeafRec->isSubClassOf("RegisterClass") ||
Chris Lattnera938ac62009-07-29 20:43:05 +0000697 LeafRec->isSubClassOf("PointerLikeRegClass")) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000698 // Handle register references. Nothing to do here.
699 } else if (LeafRec->isSubClassOf("Register")) {
700 // Handle register references.
701 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
702 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000703 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000704 std::string Fn = CP->getSelectFunc();
705 unsigned NumOps = CP->getNumOperands();
706 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000707 emitDecl("CPTmp" + RootName + "_" + utostr(i));
708 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000709 }
Evan Cheng94b30402006-10-11 21:02:01 +0000710 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000711 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000712 FoldedChains.push_back(std::make_pair("CPInChain",
713 PInfo.getNumResults()));
714 ChainName = "Chain" + ChainSuffix;
715 emitDecl("CPInChain");
716 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000717 emitCode("SDValue CPInChain;");
718 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000719 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000720
Dan Gohman537ab902010-01-04 20:31:55 +0000721 std::string Code = Fn + "(N, ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000722 if (CP->hasProperty(SDNPHasChain)) {
723 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000724 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000725 }
726 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000727 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000728 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000729 if (CP->hasProperty(SDNPHasChain))
730 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000731 emitCheck(Code + ")");
732 } else if (LeafRec->getName() == "srcvalue") {
733 // Place holder for SRCVALUE nodes. Nothing to do here.
734 } else if (LeafRec->isSubClassOf("ValueType")) {
735 // Make sure this is the specified value type.
736 emitCheck("cast<VTSDNode>(" + RootName +
Owen Anderson825b72b2009-08-11 20:47:22 +0000737 ")->getVT() == MVT::" + LeafRec->getName());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000738 } else if (LeafRec->isSubClassOf("CondCode")) {
739 // Make sure this is the specified cond code.
740 emitCheck("cast<CondCodeSDNode>(" + RootName +
741 ")->get() == ISD::" + LeafRec->getName());
742 } else {
743#ifndef NDEBUG
744 Child->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +0000745 errs() << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000746#endif
747 assert(0 && "Unknown leaf type!");
748 }
749
Dan Gohman0540e172008-10-15 06:17:21 +0000750 // If there are node predicates for this, emit the calls.
751 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
752 emitCheck(Child->getPredicateFns()[i] + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000753 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000754 } else if (IntInit *II =
755 dynamic_cast<IntInit*>(Child->getLeafValue())) {
Dan Gohman0b53d982008-12-19 18:13:39 +0000756 unsigned NTmp = TmpNo++;
757 emitCode("ConstantSDNode *Tmp"+ utostr(NTmp) +
758 " = dyn_cast<ConstantSDNode>("+
759 RootName + ");");
760 emitCheck("Tmp" + utostr(NTmp));
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000761 unsigned CTmp = TmpNo++;
Dan Gohman0b53d982008-12-19 18:13:39 +0000762 emitCode("int64_t CN"+ utostr(CTmp) +
763 " = Tmp" + utostr(NTmp) + "->getSExtValue();");
Dan Gohman63f97202008-10-17 01:33:43 +0000764 emitCheck("CN" + utostr(CTmp) + " == "
765 "INT64_C(" +itostr(II->getValue()) + ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000766 } else {
767#ifndef NDEBUG
768 Child->dump();
769#endif
770 assert(0 && "Unknown leaf type!");
771 }
772 }
773 }
Evan Chengb915f312005-12-09 22:45:35 +0000774
775 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
776 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000777 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000778 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000779 bool InFlagDecled, bool ResNodeDecled,
780 bool LikeLeaf = false, bool isRoot = false) {
Dan Gohman602b0c82009-09-25 18:54:59 +0000781 // List of arguments of getMachineNode() or SelectNodeTo().
Evan Cheng676d7312006-08-26 00:59:04 +0000782 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000783 // This is something selected from the pattern we matched.
784 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000785 const std::string &VarName = N->getName();
786 std::string Val = VariableMap[VarName];
787 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000788 if (Val.empty()) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000789 errs() << "Variable '" << VarName << " referenced but not defined "
Bill Wendling27926af2008-02-26 10:45:29 +0000790 << "and not caught earlier!\n";
791 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000792 }
Evan Chengb915f312005-12-09 22:45:35 +0000793 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
794 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000795 NodeOps.push_back(Val);
796 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000797 }
798
799 const ComplexPattern *CP;
800 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000801 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000802 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000803 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000804 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000805 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000806 default:
Daniel Dunbar1a551802009-07-03 00:10:29 +0000807 errs() << "Cannot handle " << getEnumName(N->getTypeNum(0))
Chris Lattnerd8a17282007-01-17 07:45:12 +0000808 << " type as an immediate constant. Aborting\n";
809 abort();
Owen Anderson825b72b2009-08-11 20:47:22 +0000810 case MVT::i1: CastType = "bool"; break;
811 case MVT::i8: CastType = "unsigned char"; break;
812 case MVT::i16: CastType = "unsigned short"; break;
813 case MVT::i32: CastType = "unsigned"; break;
814 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000815 }
Dan Gohman475871a2008-07-27 21:46:04 +0000816 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000817 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000818 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000819 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000820 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
821 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000822 Val = TmpVar;
823 ModifiedVal = true;
824 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000825 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
826 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
827 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000828 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000829 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
830 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
831 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000832 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
833 // value if used multiple times by this pattern result.
834 Val = TmpVar;
835 ModifiedVal = true;
836 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000837 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000838 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000839 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000840 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000841 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000842 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000843 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000844 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000845 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000846 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
847 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000848 Val = TmpVar;
849 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000850 }
Scott Michel6be48d42008-01-29 02:29:31 +0000851 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000852 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
853 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000854 Record *Op = OperatorMap[N->getName()];
855 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000856 if (Op && (Op->getName() == "globaladdr" ||
857 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000858 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000859 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000860 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000861 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000862 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000863 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
864 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000865 Val = TmpVar;
866 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000867 }
Evan Cheng676d7312006-08-26 00:59:04 +0000868 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000869 } else if (!N->isLeaf()
870 && (N->getOperator()->getName() == "texternalsym"
871 || N->getOperator()->getName() == "tconstpool")) {
872 // Do not rewrite the variable name, since we don't generate a new
873 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000874 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000875 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000876 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000877 NodeOps.push_back("CPTmp" + Val + "_" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000878 }
Evan Chengb915f312005-12-09 22:45:35 +0000879 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000880 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000881 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000882 if (!LikeLeaf) {
Chris Lattner706d2d32006-08-09 16:44:44 +0000883 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000884 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000885 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000886 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000887 }
Evan Cheng676d7312006-08-26 00:59:04 +0000888 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000889 }
Scott Michel6be48d42008-01-29 02:29:31 +0000890
891 if (ModifiedVal) {
892 VariableMap[VarName] = Val;
893 }
Evan Cheng676d7312006-08-26 00:59:04 +0000894 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000895 }
Evan Chengb915f312005-12-09 22:45:35 +0000896 if (N->isLeaf()) {
897 // If this is an explicit register reference, handle it.
898 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
899 unsigned ResNo = TmpNo++;
900 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000901 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000902 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000903 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000904 NodeOps.push_back("Tmp" + utostr(ResNo));
905 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000906 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000907 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000908 " = CurDAG->getRegister(0, " +
909 getEnumName(N->getTypeNum(0)) + ");");
910 NodeOps.push_back("Tmp" + utostr(ResNo));
911 return NodeOps;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000912 } else if (DI->getDef()->isSubClassOf("RegisterClass")) {
913 // Handle a reference to a register class. This is used
914 // in COPY_TO_SUBREG instructions.
915 emitCode("SDValue Tmp" + utostr(ResNo) +
916 " = CurDAG->getTargetConstant(" +
917 getQualifiedName(DI->getDef()) + "RegClassID, " +
Owen Anderson825b72b2009-08-11 20:47:22 +0000918 "MVT::i32);");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000919 NodeOps.push_back("Tmp" + utostr(ResNo));
920 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000921 }
922 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
923 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000924 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000925 emitCode("SDValue Tmp" + utostr(ResNo) +
Daniel Dunbarbd17a292009-07-30 18:18:54 +0000926 " = CurDAG->getTargetConstant(0x" +
927 utohexstr((uint64_t) II->getValue()) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000928 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000929 NodeOps.push_back("Tmp" + utostr(ResNo));
930 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000931 }
932
Jim Laskey16d42c62006-07-11 18:25:13 +0000933#ifndef NDEBUG
934 N->dump();
935#endif
Evan Chengb915f312005-12-09 22:45:35 +0000936 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000937 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000938 }
939
940 Record *Op = N->getOperator();
941 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000942 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000943 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000944 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000945 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000946 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000947 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000948 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
949 : (InstPat ? InstPat->getTree(0) : NULL);
Dan Gohmanfebf71d2009-01-16 21:30:55 +0000950 if (InstPatNode && !InstPatNode->isLeaf() &&
951 InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000952 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000953 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000954 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000955 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000956 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000957 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000958 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000959 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000960 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000961 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000962 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000963 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000964 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000965 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000966 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000967 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000968 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000969 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000970
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000971 // Record output varargs info.
972 OutputIsVariadic = IsVariadic;
973
Evan Chengfceb57a2006-07-15 08:45:20 +0000974 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000975 emitCode("bool HasInFlag = "
Owen Anderson825b72b2009-08-11 20:47:22 +0000976 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000977 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000978 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000979 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000980
Evan Cheng823b7522006-01-19 21:57:10 +0000981 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000982 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000983 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000984 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
985 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000986 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000987 }
988
Evan Cheng4326ef52006-10-12 02:08:53 +0000989 if (OrigChains.size() > 0) {
990 // The original input chain is being ignored. If it is not just
991 // pointing to the op that's being folded, we should create a
992 // TokenFactor with it and the chain of the folded op as the new chain.
993 // We could potentially be doing multiple levels of folding, in that
994 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000995 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +0000996 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000997 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
998 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +0000999 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
1000 emitCode("}");
1001 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001002 emitCode("InChains.push_back(" + ChainName + ");");
Dale Johannesened2eee62009-02-06 01:31:28 +00001003 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, "
Owen Anderson825b72b2009-08-11 20:47:22 +00001004 "N.getDebugLoc(), MVT::Other, "
Evan Cheng4326ef52006-10-12 02:08:53 +00001005 "&InChains[0], InChains.size());");
David Greene8ad4c002008-10-27 21:56:29 +00001006 if (GenDebug) {
1007 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
1008 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
1009 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001010 }
1011
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001012 // Loop over all of the operands of the instruction pattern, emitting code
1013 // to fill them all in. The node 'N' usually has number children equal to
1014 // the number of input operands of the instruction. However, in cases
1015 // where there are predicate operands for an instruction, we need to fill
1016 // in the 'execute always' values. Match up the node operands to the
1017 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +00001018 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001019 for (unsigned ChildNo = 0, InstOpNo = NumResults;
1020 InstOpNo != II.OperandList.size(); ++InstOpNo) {
1021 std::vector<std::string> Ops;
1022
Dan Gohmand35121a2008-05-29 19:57:41 +00001023 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +00001024 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001025 if ((OperandNode->isSubClassOf("PredicateOperand") ||
1026 OperandNode->isSubClassOf("OptionalDefOperand")) &&
1027 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +00001028 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +00001029 // 'default ops' operands.
1030 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001031 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001032 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001033 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001034 InFlagDecled, ResNodeDecled);
1035 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1036 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001037 } else {
1038 // Otherwise this is a normal operand or a predicate operand without
1039 // 'execute always'; emit it.
1040 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1041 InFlagDecled, ResNodeDecled);
1042 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1043 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001044 }
Evan Chengb915f312005-12-09 22:45:35 +00001045 }
1046
Evan Chengb915f312005-12-09 22:45:35 +00001047 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001048 bool ChainEmitted = NodeHasChain;
Dale Johannesen874ae252009-06-02 03:12:52 +00001049 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001050 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1051 InFlagDecled, ResNodeDecled, true);
Dale Johannesen874ae252009-06-02 03:12:52 +00001052 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001053 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001054 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001055 InFlagDecled = true;
1056 }
Evan Chengf037ca62006-08-27 08:11:28 +00001057 if (NodeHasOptInFlag) {
1058 emitCode("if (HasInFlag) {");
1059 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
Evan Chengf037ca62006-08-27 08:11:28 +00001060 emitCode("}");
1061 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001062 }
Evan Chengb915f312005-12-09 22:45:35 +00001063
Evan Chengb915f312005-12-09 22:45:35 +00001064 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001065
Dan Gohman95d11092008-07-07 21:00:17 +00001066 unsigned OpsNo = OpcNo;
1067 std::string CodePrefix;
1068 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1069 std::deque<std::string> After;
1070 std::string NodeName;
1071 if (!isRoot) {
1072 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001073 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001074 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001075 NodeName = "ResNode";
1076 if (!ResNodeDecled) {
1077 CodePrefix = "SDNode *" + NodeName + " = ";
1078 ResNodeDecled = true;
1079 } else
1080 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001081 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001082
Dan Gohman95d11092008-07-07 21:00:17 +00001083 std::string Code = "Opc" + utostr(OpcNo);
1084
Bill Wendling6e1bb382009-01-29 05:27:31 +00001085 if (!isRoot || (InputHasChain && !NodeHasChain))
Dan Gohman602b0c82009-09-25 18:54:59 +00001086 // For call to "getMachineNode()".
Bill Wendling6e1bb382009-01-29 05:27:31 +00001087 Code += ", N.getDebugLoc()";
1088
Dan Gohman95d11092008-07-07 21:00:17 +00001089 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1090
1091 // Output order: results, chain, flags
1092 // Result types.
Owen Anderson825b72b2009-08-11 20:47:22 +00001093 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
Dan Gohman95d11092008-07-07 21:00:17 +00001094 Code += ", VT" + utostr(VTNo);
1095 emitVT(getEnumName(N->getTypeNum(0)));
1096 }
1097 // Add types for implicit results in physical registers, scheduler will
1098 // care of adding copyfromreg nodes.
1099 for (unsigned i = 0; i < NumDstRegs; i++) {
1100 Record *RR = DstRegs[i];
1101 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001102 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Dan Gohman95d11092008-07-07 21:00:17 +00001103 Code += ", " + getEnumName(RVT);
1104 }
1105 }
1106 if (NodeHasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00001107 Code += ", MVT::Other";
Dale Johannesen874ae252009-06-02 03:12:52 +00001108 if (NodeHasOutFlag)
Owen Anderson825b72b2009-08-11 20:47:22 +00001109 Code += ", MVT::Flag";
Dan Gohman95d11092008-07-07 21:00:17 +00001110
1111 // Inputs.
1112 if (IsVariadic) {
1113 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1114 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1115 AllOps.clear();
1116
1117 // Figure out whether any operands at the end of the op list are not
1118 // part of the variable section.
1119 std::string EndAdjust;
1120 if (NodeHasInFlag || HasImpInputs)
1121 EndAdjust = "-1"; // Always has one flag.
1122 else if (NodeHasOptInFlag)
1123 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1124
1125 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1126 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1127
Dan Gohman95d11092008-07-07 21:00:17 +00001128 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1129 emitCode("}");
1130 }
1131
Dan Gohmanc76909a2009-09-25 20:36:54 +00001132 // Populate MemRefs with entries for each memory accesses covered by
Dan Gohman95d11092008-07-07 21:00:17 +00001133 // this pattern.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001134 if (isRoot && !LSI.empty()) {
1135 std::string MemRefs = "MemRefs" + utostr(OpsNo);
1136 emitCode("MachineSDNode::mmo_iterator " + MemRefs + " = "
1137 "MF->allocateMemRefsArray(" + utostr(LSI.size()) + ");");
1138 for (unsigned i = 0, e = LSI.size(); i != e; ++i)
1139 emitCode(MemRefs + "[" + utostr(i) + "] = "
1140 "cast<MemSDNode>(" + LSI[i] + ")->getMemOperand();");
1141 After.push_back("cast<MachineSDNode>(ResNode)->setMemRefs(" +
1142 MemRefs + ", " + MemRefs + " + " + utostr(LSI.size()) +
1143 ");");
Dan Gohman95d11092008-07-07 21:00:17 +00001144 }
1145
1146 if (NodeHasChain) {
1147 if (IsVariadic)
1148 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1149 else
1150 AllOps.push_back(ChainName);
1151 }
1152
1153 if (IsVariadic) {
1154 if (NodeHasInFlag || HasImpInputs)
1155 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1156 else if (NodeHasOptInFlag) {
1157 emitCode("if (HasInFlag)");
1158 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1159 }
1160 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1161 ".size()";
Dale Johannesen874ae252009-06-02 03:12:52 +00001162 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Dan Gohman95d11092008-07-07 21:00:17 +00001163 AllOps.push_back("InFlag");
1164
1165 unsigned NumOps = AllOps.size();
1166 if (NumOps) {
1167 if (!NodeHasOptInFlag && NumOps < 4) {
1168 for (unsigned i = 0; i != NumOps; ++i)
1169 Code += ", " + AllOps[i];
1170 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001171 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001172 for (unsigned i = 0; i != NumOps; ++i) {
1173 OpsCode += AllOps[i];
1174 if (i != NumOps-1)
1175 OpsCode += ", ";
1176 }
1177 emitCode(OpsCode + " };");
1178 Code += ", Ops" + utostr(OpsNo) + ", ";
1179 if (NodeHasOptInFlag) {
1180 Code += "HasInFlag ? ";
1181 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1182 } else
1183 Code += utostr(NumOps);
1184 }
1185 }
1186
1187 if (!isRoot)
1188 Code += "), 0";
1189
Dan Gohmane8be6c62008-07-17 19:10:17 +00001190 std::vector<std::string> ReplaceFroms;
1191 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001192 if (!isRoot) {
1193 NodeOps.push_back("Tmp" + utostr(ResNo));
1194 } else {
1195
Dale Johannesen874ae252009-06-02 03:12:52 +00001196 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001197 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001198 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001199 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1200 ");");
1201 InFlagDecled = true;
1202 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001203 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001204 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1205 ");");
1206 }
1207
Dan Gohman1eb49a02009-01-05 19:31:28 +00001208 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
1209 ReplaceFroms.push_back("SDValue(" +
1210 FoldedChains[j].first + ".getNode(), " +
1211 utostr(FoldedChains[j].second) +
1212 ")");
1213 ReplaceTos.push_back("SDValue(ResNode, " +
1214 utostr(NumResults+NumDstRegs) + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001215 }
1216
Dale Johannesen874ae252009-06-02 03:12:52 +00001217 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001218 if (FoldedFlag.first != "") {
Dale Johannesen874ae252009-06-02 03:12:52 +00001219 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001220 utostr(FoldedFlag.second) + ")");
1221 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001222 } else {
Dale Johannesen874ae252009-06-02 03:12:52 +00001223 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001224 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001225 utostr(NumPatResults + (unsigned)InputHasChain)
1226 + ")");
1227 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001228 }
Dan Gohman95d11092008-07-07 21:00:17 +00001229 }
1230
Dan Gohmane8be6c62008-07-17 19:10:17 +00001231 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001232 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001233 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001234 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001235 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001236 ChainAssignmentNeeded |= NodeHasChain;
1237 }
1238
1239 // User does not expect the instruction would produce a chain!
Dale Johannesen874ae252009-06-02 03:12:52 +00001240 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001241 ;
1242 } else if (InputHasChain && !NodeHasChain) {
1243 // One of the inner node produces a chain.
Dan Gohmanba7a6622010-01-04 20:36:57 +00001244 assert(!NodeHasOutFlag && "Node has flag but not chain!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001245 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001246 utostr(NumPatResults) + ")");
1247 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001248 }
1249 }
1250
1251 if (ChainAssignmentNeeded) {
1252 // Remember which op produces the chain.
1253 std::string ChainAssign;
1254 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001255 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001256 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001257 else
Dan Gohman475871a2008-07-27 21:46:04 +00001258 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001259 ", " + utostr(NumResults+NumDstRegs) + ");";
1260
1261 After.push_front(ChainAssign);
1262 }
1263
Dan Gohmane8be6c62008-07-17 19:10:17 +00001264 if (ReplaceFroms.size() == 1) {
1265 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1266 ReplaceTos[0] + ");");
1267 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001268 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001269 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1270 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1271 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001272 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001273 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1274 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1275 After.push_back("};");
1276 After.push_back("ReplaceUses(Froms, Tos, " +
1277 itostr(ReplaceFroms.size()) + ");");
1278 }
1279
1280 // We prefer to use SelectNodeTo since it avoids allocation when
1281 // possible and it avoids CSE map recalculation for the node's
1282 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001283 //
Dan Gohman2929e112009-12-19 01:46:09 +00001284 // We also don't use SelectNodeTo if the pattern replacement is being
1285 // used to jettison a chain result, since morphing the node in place
Dan Gohmane8be6c62008-07-17 19:10:17 +00001286 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001287 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001288 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001289 Code = "CurDAG->getMachineNode(" + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001290 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001291 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001292 }
1293 if (isRoot) {
1294 if (After.empty())
1295 CodePrefix = "return ";
1296 else
1297 After.push_back("return ResNode;");
1298 }
1299
1300 emitCode(CodePrefix + Code + ");");
David Greene8ad4c002008-10-27 21:56:29 +00001301
1302 if (GenDebug) {
1303 if (!isRoot) {
1304 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
1305 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
1306 }
1307 else {
1308 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
1309 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
1310 }
1311 }
1312
Dan Gohman95d11092008-07-07 21:00:17 +00001313 for (unsigned i = 0, e = After.size(); i != e; ++i)
1314 emitCode(After[i]);
1315
Evan Cheng676d7312006-08-26 00:59:04 +00001316 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001317 }
1318 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001319 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001320 // PatLeaf node - the operand may or may not be a leaf node. But it should
1321 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001322 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001323 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001324 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001325 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001326 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001327 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001328 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001329 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001330 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001331 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001332 }
Dan Gohman0540e172008-10-15 06:17:21 +00001333
1334 N->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001335 errs() << "\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001336 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001337 }
1338
Chris Lattner488580c2006-01-28 19:06:51 +00001339 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1340 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001341 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1342 /// for, this returns true otherwise false if Pat has all types.
1343 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001344 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001345 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001346 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001347 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001348 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001349 // The top level node type is checked outside of the select function.
1350 if (!isRoot)
Anton Korobeynikovc2fd9192009-11-08 12:14:54 +00001351 emitCheck(Prefix + ".getValueType() == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001352 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001353 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001354 }
1355
Evan Cheng51fecc82006-01-09 18:27:06 +00001356 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001357 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001358 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1359 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1360 Prefix + utostr(OpNo)))
1361 return true;
1362 return false;
1363 }
1364
1365private:
Evan Cheng54597732006-01-26 00:22:25 +00001366 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001367 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001368 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001369 bool &ChainEmitted, bool &InFlagDecled,
1370 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001371 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001372 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001373 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1374 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001375 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1376 TreePatternNode *Child = N->getChild(i);
1377 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001378 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1379 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001380 } else {
1381 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001382 if (!Child->getName().empty()) {
1383 std::string Name = RootName + utostr(OpNo);
1384 if (Duplicates.find(Name) != Duplicates.end())
1385 // A duplicate! Do not emit a copy for this node.
1386 continue;
1387 }
1388
Evan Chengb915f312005-12-09 22:45:35 +00001389 Record *RR = DI->getDef();
1390 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
1392 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001393 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001394 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001395 InFlagDecled = true;
1396 } else
1397 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
Evan Chengb2c6d492006-01-11 22:16:13 +00001398 } else {
1399 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001400 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001401 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001402 ChainEmitted = true;
1403 }
Evan Cheng676d7312006-08-26 00:59:04 +00001404 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001405 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001406 InFlagDecled = true;
1407 }
Dale Johannesen874ae252009-06-02 03:12:52 +00001408 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1409 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Dale Johannesena05dca42009-02-04 23:02:30 +00001410 ", " + RootName + ".getDebugLoc()" +
Chris Lattner6cefb772008-01-05 22:25:12 +00001411 ", " + getQualifiedName(RR) +
Dale Johannesen874ae252009-06-02 03:12:52 +00001412 ", " + RootName + utostr(OpNo) + ", InFlag).getNode();");
1413 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001414 emitCode(ChainName + " = SDValue(ResNode, 0);");
1415 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001416 }
1417 }
1418 }
1419 }
1420 }
Evan Cheng54597732006-01-26 00:22:25 +00001421
Dale Johannesen874ae252009-06-02 03:12:52 +00001422 if (HasInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001423 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001424 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001425 ".getOperand(" + utostr(OpNo) + ");");
1426 InFlagDecled = true;
1427 } else
1428 emitCode("InFlag = " + RootName +
1429 ".getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001430 }
Evan Chengb915f312005-12-09 22:45:35 +00001431 }
1432};
1433
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001434/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1435/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001436/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001437void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001438 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001439 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001440 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001441 std::vector<std::string> &TargetVTs,
1442 bool &OutputIsVariadic,
1443 unsigned &NumInputRootOps) {
1444 OutputIsVariadic = false;
1445 NumInputRootOps = 0;
1446
Dan Gohman22bb3112008-08-22 00:20:26 +00001447 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001448 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001449 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001450 TargetOpcodes, TargetVTs,
1451 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001452
Chris Lattner8fc35682005-09-23 23:16:51 +00001453 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001454 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001455 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001456
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001457 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001458 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001459
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001460 // At this point, we know that we structurally match the pattern, but the
1461 // types of the nodes may not match. Figure out the fewest number of type
1462 // comparisons we need to emit. For example, if there is only one integer
1463 // type supported by a target, there should be no type comparisons at all for
1464 // integer patterns!
1465 //
1466 // To figure out the fewest number of type checks needed, clone the pattern,
1467 // remove the types, then perform type inference on the pattern as a whole.
1468 // If there are unresolved types, emit an explicit check for those types,
1469 // apply the type to the tree, then rerun type inference. Iterate until all
1470 // types are resolved.
1471 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001472 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001473 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001474
1475 do {
1476 // Resolve/propagate as many types as possible.
1477 try {
1478 bool MadeChange = true;
1479 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001480 MadeChange = Pat->ApplyTypeConstraints(TP,
1481 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001482 } catch (...) {
1483 assert(0 && "Error: could not find consistent types for something we"
1484 " already decided was ok!");
1485 abort();
1486 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001487
Chris Lattner7e82f132005-10-15 21:34:21 +00001488 // Insert a check for an unresolved type and add it to the tree. If we find
1489 // an unresolved type to add a check for, this returns true and we iterate,
1490 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001491 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001492
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001493 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001494 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001495 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001496}
1497
Chris Lattner24e00a42006-01-29 04:41:05 +00001498/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1499/// a line causes any of them to be empty, remove them and return true when
1500/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001501static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001502 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001503 &Patterns) {
1504 bool ErasedPatterns = false;
1505 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1506 Patterns[i].second.pop_back();
1507 if (Patterns[i].second.empty()) {
1508 Patterns.erase(Patterns.begin()+i);
1509 --i; --e;
1510 ErasedPatterns = true;
1511 }
1512 }
1513 return ErasedPatterns;
1514}
1515
Chris Lattner8bc74722006-01-29 04:25:26 +00001516/// EmitPatterns - Emit code for at least one pattern, but try to group common
1517/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001518void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001519 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001520 &Patterns, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001521 raw_ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001522 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001523 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001524 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001525
1526 if (Patterns.empty()) return;
1527
Chris Lattner24e00a42006-01-29 04:41:05 +00001528 // Figure out how many patterns share the next code line. Explicitly copy
1529 // FirstCodeLine so that we don't invalidate a reference when changing
1530 // Patterns.
1531 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001532 unsigned LastMatch = Patterns.size()-1;
1533 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1534 --LastMatch;
1535
1536 // If not all patterns share this line, split the list into two pieces. The
1537 // first chunk will use this line, the second chunk won't.
1538 if (LastMatch != 0) {
1539 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1540 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1541
1542 // FIXME: Emit braces?
1543 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001544 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001545 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1546 Pattern.getSrcPattern()->print(OS);
1547 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1548 Pattern.getDstPattern()->print(OS);
1549 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001550 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001551 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001552 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001553 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001554 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001555 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001556 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001557 }
Evan Cheng676d7312006-08-26 00:59:04 +00001558 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001559 OS << std::string(Indent, ' ') << "{\n";
1560 Indent += 2;
1561 }
1562 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001563 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001564 Indent -= 2;
1565 OS << std::string(Indent, ' ') << "}\n";
1566 }
1567
1568 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001569 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001570 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1571 Pattern.getSrcPattern()->print(OS);
1572 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1573 Pattern.getDstPattern()->print(OS);
1574 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001575 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001576 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001577 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001578 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001579 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001580 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001581 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001582 }
1583 EmitPatterns(Other, Indent, OS);
1584 return;
1585 }
1586
Chris Lattner24e00a42006-01-29 04:41:05 +00001587 // Remove this code from all of the patterns that share it.
1588 bool ErasedPatterns = EraseCodeLine(Patterns);
1589
Evan Cheng676d7312006-08-26 00:59:04 +00001590 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001591
1592 // Otherwise, every pattern in the list has this line. Emit it.
1593 if (!isPredicate) {
1594 // Normal code.
1595 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1596 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001597 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1598
1599 // If the next code line is another predicate, and if all of the pattern
1600 // in this group share the same next line, emit it inline now. Do this
1601 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001602 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Jim Grosbachda4231f2009-03-26 16:17:51 +00001603 // Check that all of the patterns in Patterns end with the same predicate.
Chris Lattner24e00a42006-01-29 04:41:05 +00001604 bool AllEndWithSamePredicate = true;
1605 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1606 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1607 AllEndWithSamePredicate = false;
1608 break;
1609 }
1610 // If all of the predicates aren't the same, we can't share them.
1611 if (!AllEndWithSamePredicate) break;
1612
1613 // Otherwise we can. Emit it shared now.
1614 OS << " &&\n" << std::string(Indent+4, ' ')
1615 << Patterns.back().second.back().second;
1616 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001617 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001618
1619 OS << ") {\n";
1620 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001621 }
1622
1623 EmitPatterns(Patterns, Indent, OS);
1624
1625 if (isPredicate)
1626 OS << std::string(Indent-2, ' ') << "}\n";
1627}
1628
Evan Cheng892aaf82006-11-08 23:01:03 +00001629static std::string getLegalCName(std::string OpName) {
1630 std::string::size_type pos = OpName.find("::");
1631 if (pos != std::string::npos)
1632 OpName.replace(pos, 2, "_");
1633 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001634}
1635
Daniel Dunbar1a551802009-07-03 00:10:29 +00001636void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001637 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001638
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001639 // Get the namespace to insert instructions into.
1640 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001641 if (!InstNS.empty()) InstNS += "::";
1642
Chris Lattner602f6922006-01-04 00:25:00 +00001643 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001644 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001645 // All unique target node emission functions.
1646 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001647 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001648 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001649 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001650
1651 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001652 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001653 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001654 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001655 } else {
1656 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001657 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001658 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001659 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001660 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001661 std::vector<Record*> OpNodes = CP->getRootNodes();
1662 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001663 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1664 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001665 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001666 }
1667 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001668 errs() << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001669 Node->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001670 errs() << "' on tree pattern '";
1671 errs() << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001672 exit(1);
1673 }
1674 }
1675 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001676
1677 // For each opcode, there might be multiple select functions, one per
1678 // ValueType of the node (or its first operand if it doesn't produce a
1679 // non-chain result.
1680 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1681
Chris Lattner602f6922006-01-04 00:25:00 +00001682 // Emit one Select_* method for each top-level opcode. We do this instead of
1683 // emitting one giant switch statement to support compilers where this will
1684 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001685 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001686 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1687 PBOI != E; ++PBOI) {
1688 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001689 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001690 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1691
Chris Lattner706d2d32006-08-09 16:44:44 +00001692 // Split them into groups by type.
Owen Anderson825b72b2009-08-11 20:47:22 +00001693 std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001694 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001695 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001696 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001697 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001698 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001699 }
1700
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 for (std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001702 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001703 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1704 ++II) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001705 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001706 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001707 typedef std::pair<unsigned, std::string> CodeLine;
1708 typedef std::vector<CodeLine> CodeList;
1709 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001710
Chris Lattner60d81392008-01-05 22:30:17 +00001711 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001712 std::vector<std::vector<std::string> > PatternOpcodes;
1713 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001714 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001715 std::vector<bool> OutputIsVariadicFlags;
1716 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001717 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1718 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001719 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001720 std::vector<std::string> TargetOpcodes;
1721 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001722 bool OutputIsVariadic;
1723 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001724 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001725 TargetOpcodes, TargetVTs,
1726 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001727 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1728 PatternDecls.push_back(GeneratedDecl);
1729 PatternOpcodes.push_back(TargetOpcodes);
1730 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001731 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1732 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001733 }
1734
Chris Lattner706d2d32006-08-09 16:44:44 +00001735 // Factor target node emission code (emitted by EmitResultCode) into
1736 // separate functions. Uniquing and share them among all instruction
1737 // selection routines.
1738 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1739 CodeList &GeneratedCode = CodeForPatterns[i].second;
1740 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1741 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001742 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001743 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1744 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001745 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001746 int CodeSize = (int)GeneratedCode.size();
1747 int LastPred = -1;
1748 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001749 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001750 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001751 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1752 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001753 }
1754
Dan Gohman475871a2008-07-27 21:46:04 +00001755 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001756 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001757 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1758 CalleeCode += ", unsigned Opc" + utostr(j);
1759 CallerCode += ", " + TargetOpcodes[j];
1760 }
1761 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Owen Anderson69110c92009-09-11 09:01:57 +00001762 CalleeCode += ", MVT::SimpleValueType VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001763 CallerCode += ", " + TargetVTs[j];
1764 }
Evan Chengf5493192006-08-26 01:02:19 +00001765 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001766 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001767 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001768 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001769 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001770 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001771
1772 if (OutputIsVariadic) {
1773 CalleeCode += ", unsigned NumInputRootOps";
1774 CallerCode += ", " + utostr(NumInputRootOps);
1775 }
1776
Chris Lattner706d2d32006-08-09 16:44:44 +00001777 CallerCode += ");";
Benjamin Kramerf2a39bd2009-11-14 16:37:18 +00001778 CalleeCode += ") {\n";
Evan Cheng676d7312006-08-26 00:59:04 +00001779
1780 for (std::vector<std::string>::const_reverse_iterator
1781 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1782 CalleeCode += " " + *I + "\n";
1783
Evan Chengf5493192006-08-26 01:02:19 +00001784 for (int j = LastPred+1; j < CodeSize; ++j)
1785 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001786 for (int j = LastPred+1; j < CodeSize; ++j)
1787 GeneratedCode.pop_back();
1788 CalleeCode += "}\n";
1789
1790 // Uniquing the emission routines.
1791 unsigned EmitFuncNum;
1792 std::map<std::string, unsigned>::iterator EFI =
1793 EmitFunctions.find(CalleeCode);
1794 if (EFI != EmitFunctions.end()) {
1795 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001796 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001797 EmitFuncNum = EmitFunctions.size();
1798 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Benjamin Kramerf2a39bd2009-11-14 16:37:18 +00001799 // Prevent emission routines from being inlined to reduce selection
1800 // routines stack frame sizes.
1801 OS << "DISABLE_INLINE ";
Evan Cheng06d64702006-08-11 08:59:35 +00001802 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001803 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001804
Chris Lattner706d2d32006-08-09 16:44:44 +00001805 // Replace the emission code within selection routines with calls to the
1806 // emission functions.
David Greene8ad4c002008-10-27 21:56:29 +00001807 if (GenDebug) {
1808 GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"red\");"));
1809 }
1810 CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
1811 GeneratedCode.push_back(std::make_pair(3, CallerCode));
1812 if (GenDebug) {
1813 GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
1814 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"yellow\");"));
1815 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"black\");"));
1816 GeneratedCode.push_back(std::make_pair(0, "}"));
1817 //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"black\");"));
1818 }
1819 GeneratedCode.push_back(std::make_pair(0, "return Result;"));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001820 }
1821
Chris Lattner706d2d32006-08-09 16:44:44 +00001822 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001823 std::string OpVTStr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001824 if (OpVT == MVT::iPTR) {
Chris Lattner33a40042006-11-14 22:17:10 +00001825 OpVTStr = "_iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +00001826 } else if (OpVT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +00001827 OpVTStr = "_iPTRAny";
Owen Anderson825b72b2009-08-11 20:47:22 +00001828 } else if (OpVT == MVT::isVoid) {
Chris Lattner33a40042006-11-14 22:17:10 +00001829 // Nodes with a void result actually have a first result type of either
1830 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1831 // void to this case, we handle it specially here.
1832 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001833 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
Chris Lattner33a40042006-11-14 22:17:10 +00001834 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001835 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1836 OpcodeVTMap.find(OpName);
1837 if (OpVTI == OpcodeVTMap.end()) {
1838 std::vector<std::string> VTSet;
1839 VTSet.push_back(OpVTStr);
1840 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1841 } else
1842 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001843
Dan Gohman0540e172008-10-15 06:17:21 +00001844 // We want to emit all of the matching code now. However, we want to emit
1845 // the matches in order of minimal cost. Sort the patterns so the least
1846 // cost one is at the start.
1847 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1848 PatternSortingPredicate(CGP));
1849
1850 // Scan the code to see if all of the patterns are reachable and if it is
1851 // possible that the last one might not match.
1852 bool mightNotMatch = true;
1853 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1854 CodeList &GeneratedCode = CodeForPatterns[i].second;
1855 mightNotMatch = false;
1856
1857 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1858 if (GeneratedCode[j].first == 1) { // predicate.
1859 mightNotMatch = true;
1860 break;
1861 }
1862 }
1863
1864 // If this pattern definitely matches, and if it isn't the last one, the
1865 // patterns after it CANNOT ever match. Error out.
1866 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001867 errs() << "Pattern '";
1868 CodeForPatterns[i].first->getSrcPattern()->print(errs());
1869 errs() << "' is impossible to select!\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001870 exit(1);
1871 }
1872 }
1873
Chris Lattner706d2d32006-08-09 16:44:44 +00001874 // Loop through and reverse all of the CodeList vectors, as we will be
1875 // accessing them from their logical front, but accessing the end of a
1876 // vector is more efficient.
1877 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1878 CodeList &GeneratedCode = CodeForPatterns[i].second;
1879 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001880 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001881
1882 // Next, reverse the list of patterns itself for the same reason.
1883 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1884
Dan Gohman63e3e632009-01-29 01:37:18 +00001885 OS << "SDNode *Select_" << getLegalCName(OpName)
1886 << OpVTStr << "(const SDValue &N) {\n";
1887
Chris Lattner706d2d32006-08-09 16:44:44 +00001888 // Emit all of the patterns now, grouped together to share code.
1889 EmitPatterns(CodeForPatterns, 2, OS);
1890
Chris Lattner64906972006-09-21 18:28:27 +00001891 // If the last pattern has predicates (which could fail) emit code to
1892 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001893 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001894 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001895 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1896 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001897 OpName != "ISD::INTRINSIC_VOID")
1898 OS << " CannotYetSelect(N);\n";
1899 else
1900 OS << " CannotYetSelectIntrinsic(N);\n";
1901
1902 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001903 }
1904 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001905 }
Chris Lattner602f6922006-01-04 00:25:00 +00001906 }
1907
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001908 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001909 << "SDNode *SelectCode(SDValue N) {\n"
Owen Anderson825b72b2009-08-11 20:47:22 +00001910 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT().SimpleTy;\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001911 << " switch (N.getOpcode()) {\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00001912 << " default:\n"
1913 << " assert(!N.isMachineOpcode() && \"Node already selected!\");\n"
1914 << " break;\n"
1915 << " case ISD::EntryToken: // These nodes remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001916 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001917 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001918 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001919 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001920 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001921 << " case ISD::TargetConstantPool:\n"
1922 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00001923 << " case ISD::TargetExternalSymbol:\n"
Dan Gohman8c2b5252009-10-30 01:27:03 +00001924 << " case ISD::TargetBlockAddress:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001925 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001926 << " case ISD::TargetGlobalTLSAddress:\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001927 << " case ISD::TargetGlobalAddress:\n"
1928 << " case ISD::TokenFactor:\n"
1929 << " case ISD::CopyFromReg:\n"
1930 << " case ISD::CopyToReg: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001931 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001932 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001933 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001934 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001935 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001936 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001937 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001938 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001939 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001940 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001941
Chris Lattner602f6922006-01-04 00:25:00 +00001942 // Loop over all of the case statements, emiting a call to each method we
1943 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001944 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001945 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1946 PBOI != E; ++PBOI) {
1947 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001948 // Potentially multiple versions of select for this opcode. One for each
1949 // ValueType of the node (or its first true operand if it doesn't produce a
1950 // result.
1951 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1952 OpcodeVTMap.find(OpName);
1953 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001954 OS << " case " << OpName << ": {\n";
Dale Johannesen3b895cf2009-05-12 22:32:29 +00001955 // If we have only one variant and it's the default, elide the
1956 // switch. Marginally faster, and makes MSVC happier.
1957 if (OpVTs.size()==1 && OpVTs[0].empty()) {
1958 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
1959 OS << " break;\n";
1960 OS << " }\n";
1961 continue;
1962 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001963 // Keep track of whether we see a pattern that has an iPtr result.
1964 bool HasPtrPattern = false;
1965 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001966
Evan Cheng425e8c72007-09-04 20:18:28 +00001967 OS << " switch (NVT) {\n";
1968 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1969 std::string &VTStr = OpVTs[i];
1970 if (VTStr.empty()) {
1971 HasDefaultPattern = true;
1972 continue;
1973 }
Chris Lattner717a6112006-11-14 21:50:27 +00001974
Evan Cheng425e8c72007-09-04 20:18:28 +00001975 // If this is a match on iPTR: don't emit it directly, we need special
1976 // code.
1977 if (VTStr == "_iPTR") {
1978 HasPtrPattern = true;
1979 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00001980 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001981 OS << " case MVT::" << VTStr.substr(1) << ":\n"
Evan Cheng425e8c72007-09-04 20:18:28 +00001982 << " return Select_" << getLegalCName(OpName)
1983 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001984 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001985 OS << " default:\n";
1986
1987 // If there is an iPTR result version of this pattern, emit it here.
1988 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001989 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00001990 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
1991 }
1992 if (HasDefaultPattern) {
1993 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
1994 }
1995 OS << " break;\n";
1996 OS << " }\n";
1997 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001998 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00001999 }
Chris Lattner81303322005-09-23 19:36:15 +00002000
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002001 OS << " } // end of big switch.\n\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002002 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2003 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2004 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002005 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002006 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002007 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002008 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002009 << " return NULL;\n"
2010 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002011}
2012
Daniel Dunbar1a551802009-07-03 00:10:29 +00002013void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002014 EmitSourceFileHeader("DAG Instruction Selector for the " +
2015 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002016
Chris Lattner1f39e292005-09-14 00:09:24 +00002017 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2018 << "// *** instruction selector class. These functions are really "
2019 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002020
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002021 OS << "// Include standard, target-independent definitions and methods used\n"
2022 << "// by the instruction selector.\n";
Mike Stumpfe095f32009-05-04 18:40:41 +00002023 OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002024
Chris Lattner443e3f92008-01-05 22:54:53 +00002025 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002026 EmitPredicateFunctions(OS);
2027
Chris Lattner569f1212009-08-23 04:44:11 +00002028 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n");
Chris Lattnerfe718932008-01-06 01:10:31 +00002029 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002030 I != E; ++I) {
Chris Lattner569f1212009-08-23 04:44:11 +00002031 DEBUG(errs() << "PATTERN: "; I->getSrcPattern()->dump());
2032 DEBUG(errs() << "\nRESULT: "; I->getDstPattern()->dump());
2033 DEBUG(errs() << "\n");
Bill Wendlingf5da1332006-12-07 22:21:48 +00002034 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002035
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002036 // At this point, we have full information about the 'Patterns' we need to
2037 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002038 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002039 EmitInstructionSelector(OS);
2040
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002041}