blob: 4f920119df2d1609153f40ef3cdc6645827e218a [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"
Bill Wendlingf5da1332006-12-07 22:21:48 +000021#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000022#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000023#include <deque>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024using namespace llvm;
25
David Greene8ad4c002008-10-27 21:56:29 +000026namespace {
27 cl::opt<bool>
28 GenDebug("gen-debug", cl::desc("Generate debug code"),
29 cl::init(false));
30}
31
Chris Lattnerca559d02005-09-08 21:03:01 +000032//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000033// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000034//
35
Chris Lattner6cefb772008-01-05 22:25:12 +000036/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
37/// ComplexPattern.
38static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000039 return (N->isLeaf() &&
40 dynamic_cast<DefInit*>(N->getLeafValue()) &&
41 static_cast<DefInit*>(N->getLeafValue())->getDef()->
42 isSubClassOf("ComplexPattern"));
43}
44
Chris Lattner6cefb772008-01-05 22:25:12 +000045/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
46/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000047static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000048 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000049 if (N->isLeaf() &&
50 dynamic_cast<DefInit*>(N->getLeafValue()) &&
51 static_cast<DefInit*>(N->getLeafValue())->getDef()->
52 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000053 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
54 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000055 }
56 return NULL;
57}
58
Chris Lattner05814af2005-09-28 17:57:56 +000059/// getPatternSize - Return the 'size' of this pattern. We want to match large
60/// patterns before small ones. This is used to determine the size of a
61/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000062static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000063 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
64 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000065 P->getExtTypeNum(0) == MVT::isVoid ||
66 P->getExtTypeNum(0) == MVT::Flag ||
Mon P Wange3b3a722008-07-30 04:36:53 +000067 P->getExtTypeNum(0) == MVT::iPTR ||
68 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000069 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000070 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000071 // If the root node is a ConstantSDNode, increases its size.
72 // e.g. (set R32:$dst, 0).
73 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000074 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000075
76 // FIXME: This is a hack to statically increase the priority of patterns
77 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
78 // Later we can allow complexity / cost for each pattern to be (optionally)
79 // specified. To get best possible pattern match we'll need to dynamically
80 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000081 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000082 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000083 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000084
85 // If this node has some predicate function that must match, it adds to the
86 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000087 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000088 ++Size;
89
Chris Lattner05814af2005-09-28 17:57:56 +000090 // Count children in the count if they are also nodes.
91 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
92 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000093 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000094 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000095 else if (Child->isLeaf()) {
96 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000097 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000098 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000099 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +0000100 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +0000101 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +0000102 }
Chris Lattner05814af2005-09-28 17:57:56 +0000103 }
104
105 return Size;
106}
107
108/// getResultPatternCost - Compute the number of instructions for this pattern.
109/// This is a temporary hack. We should really include the instruction
110/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000111static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000112 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000113 if (P->isLeaf()) return 0;
114
Evan Chengfbad7082006-02-18 02:33:09 +0000115 unsigned Cost = 0;
116 Record *Op = P->getOperator();
117 if (Op->isSubClassOf("Instruction")) {
118 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000119 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000120 if (II.usesCustomDAGSchedInserter)
121 Cost += 10;
122 }
Chris Lattner05814af2005-09-28 17:57:56 +0000123 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000124 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000125 return Cost;
126}
127
Evan Chenge6f32032006-07-19 00:24:41 +0000128/// getResultPatternCodeSize - Compute the code size of instructions for this
129/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000130static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000131 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000132 if (P->isLeaf()) return 0;
133
134 unsigned Cost = 0;
135 Record *Op = P->getOperator();
136 if (Op->isSubClassOf("Instruction")) {
137 Cost += Op->getValueAsInt("CodeSize");
138 }
139 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000140 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000141 return Cost;
142}
143
Chris Lattner05814af2005-09-28 17:57:56 +0000144// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
145// In particular, we want to match maximal patterns first and lowest cost within
146// a particular complexity first.
147struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000148 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
149 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000150
Dan Gohman0540e172008-10-15 06:17:21 +0000151 typedef std::pair<unsigned, std::string> CodeLine;
152 typedef std::vector<CodeLine> CodeList;
153 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
154
155 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
156 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
157 const PatternToMatch *LHS = LHSPair.first;
158 const PatternToMatch *RHS = RHSPair.first;
159
Chris Lattner6cefb772008-01-05 22:25:12 +0000160 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
161 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000162 LHSSize += LHS->getAddedComplexity();
163 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000164 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
165 if (LHSSize < RHSSize) return false;
166
167 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000168 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
169 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000170 if (LHSCost < RHSCost) return true;
171 if (LHSCost > RHSCost) return false;
172
Chris Lattner6cefb772008-01-05 22:25:12 +0000173 return getResultPatternSize(LHS->getDstPattern(), CGP) <
174 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000175 }
176};
177
Jim Grosbach54f30222009-03-25 23:28:33 +0000178/// getRegisterValueType - Look up and return the ValueType of the specified
179/// register. If the register is a member of multiple register classes which
180/// have different associated types, return MVT::Other.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000182 bool FoundRC = false;
Jim Grosbach54f30222009-03-25 23:28:33 +0000183 MVT::SimpleValueType VT = MVT::Other;
184 const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses();
185 std::vector<CodeGenRegisterClass>::const_iterator RC;
186 std::vector<Record*>::const_iterator Element;
187
188 for (RC = RCs.begin() ; RC != RCs.end() ; RC++) {
189 Element = find((*RC).Elements.begin(), (*RC).Elements.end(), R);
190 if (Element != (*RC).Elements.end()) {
191 if (!FoundRC) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000192 FoundRC = true;
Jim Grosbach54f30222009-03-25 23:28:33 +0000193 VT = (*RC).getValueTypeNum(0);
194 } else {
195 // In multiple RC's
196 if (VT != (*RC).getValueTypeNum(0)) {
197 // Types of the RC's do not agree. Return MVT::Other. The
198 // target is responsible for handling this.
199 return MVT::Other;
200 }
201 }
202 }
203 }
204 return VT;
Evan Cheng66a48bb2005-12-01 00:18:45 +0000205}
206
Chris Lattner72fe91c2005-09-24 00:40:24 +0000207
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000208/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
209/// type information from it.
210static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000211 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000212 if (!N->isLeaf())
213 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
214 RemoveAllTypes(N->getChild(i));
215}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000216
Evan Cheng51fecc82006-01-09 18:27:06 +0000217/// NodeHasProperty - return true if TreePatternNode has the specified
218/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000219static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000220 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000221 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000222 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000223 if (CP)
224 return CP->hasProperty(Property);
225 return false;
226 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000227 Record *Operator = N->getOperator();
228 if (!Operator->isSubClassOf("SDNode")) return false;
229
Chris Lattner6cefb772008-01-05 22:25:12 +0000230 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000231}
232
Evan Cheng94b30402006-10-11 21:02:01 +0000233static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000234 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000235 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000236 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000237
238 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
239 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000240 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000241 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000242 }
243
244 return false;
245}
246
Evan Chengf9d03182008-07-03 08:39:51 +0000247static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
248 return CGP.getSDNodeInfo(Op).getEnumName();
249}
250
251static
252bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
253 bool isStore = !N->isLeaf() &&
254 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
255 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
256 return false;
257
258 bool HasChain = false;
259 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
260 TreePatternNode *Child = N->getChild(i);
261 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
262 HasChain = true;
263 break;
264 }
265 }
266 return HasChain;
267}
268
Chris Lattnerdc32f982008-01-05 22:43:57 +0000269//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000270// Node Transformation emitter implementation.
271//
272void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
273 // Walk the pattern fragments, adding them to a map, which sorts them by
274 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000275 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000276 NXsByNameTy NXsByName;
277
Chris Lattnerfe718932008-01-06 01:10:31 +0000278 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000279 I != E; ++I)
280 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
281
282 OS << "\n// Node transformations.\n";
283
284 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
285 I != E; ++I) {
286 Record *SDNode = I->second.first;
287 std::string Code = I->second.second;
288
289 if (Code.empty()) continue; // Empty code? Skip it.
290
Chris Lattner200c57e2008-01-05 22:58:54 +0000291 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000292 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
293
Dan Gohman475871a2008-07-27 21:46:04 +0000294 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000295 << ") {\n";
296 if (ClassName != "SDNode")
297 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
298 OS << Code << "\n}\n";
299 }
300}
301
302//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000303// Predicate emitter implementation.
304//
305
306void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
307 OS << "\n// Predicate functions.\n";
308
309 // Walk the pattern fragments, adding them to a map, which sorts them by
310 // name.
311 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
312 PFsByNameTy PFsByName;
313
Chris Lattnerfe718932008-01-06 01:10:31 +0000314 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000315 I != E; ++I)
316 PFsByName.insert(std::make_pair(I->first->getName(), *I));
317
318
319 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
320 I != E; ++I) {
321 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
322 TreePattern *P = I->second.second;
323
324 // If there is a code init for this fragment, emit the predicate code.
325 std::string Code = PatFragRecord->getValueAsCode("Predicate");
326 if (Code.empty()) continue;
327
328 if (P->getOnlyTree()->isLeaf())
329 OS << "inline bool Predicate_" << PatFragRecord->getName()
330 << "(SDNode *N) {\n";
331 else {
332 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000333 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000334 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
335
336 OS << "inline bool Predicate_" << PatFragRecord->getName()
337 << "(SDNode *" << C2 << ") {\n";
338 if (ClassName != "SDNode")
339 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
340 }
341 OS << Code << "\n}\n";
342 }
343
344 OS << "\n\n";
345}
346
347
348//===----------------------------------------------------------------------===//
349// PatternCodeEmitter implementation.
350//
Evan Chengb915f312005-12-09 22:45:35 +0000351class PatternCodeEmitter {
352private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000353 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000354
Evan Cheng58e84a62005-12-14 22:02:59 +0000355 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000356 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000357 // Pattern cost.
358 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000359 // Instruction selector pattern.
360 TreePatternNode *Pattern;
361 // Matched instruction.
362 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000363
Evan Chengb915f312005-12-09 22:45:35 +0000364 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000365 std::map<std::string, std::string> VariableMap;
366 // Node to operator mapping
367 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000368 // Name of the folded node which produces a flag.
369 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000370 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000371 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000372 // Original input chain(s).
373 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000374 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000375
Dan Gohman69de1932008-02-06 22:27:42 +0000376 /// LSI - Load/Store information.
377 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
378 /// for each memory access. This facilitates the use of AliasAnalysis in
379 /// the backend.
380 std::vector<std::string> LSI;
381
Evan Cheng676d7312006-08-26 00:59:04 +0000382 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000383 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000384 /// tested, and if true, the match fails) [when 1], or normal code to emit
385 /// [when 0], or initialization code to emit [when 2].
386 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000387 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000388 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000389 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000390 /// TargetOpcodes - The target specific opcodes used by the resulting
391 /// instructions.
392 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000393 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000394 /// OutputIsVariadic - Records whether the instruction output pattern uses
395 /// variable_ops. This requires that the Emit function be passed an
396 /// additional argument to indicate where the input varargs operands
397 /// begin.
398 bool &OutputIsVariadic;
399 /// NumInputRootOps - Records the number of operands the root node of the
400 /// input pattern has. This information is used in the generated code to
401 /// pass to Emit functions when variable_ops processing is needed.
402 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000403
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000404 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000405 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000406 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000407 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000408
409 void emitCheck(const std::string &S) {
410 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000411 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000412 }
413 void emitCode(const std::string &S) {
414 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000415 GeneratedCode.push_back(std::make_pair(0, S));
416 }
417 void emitInit(const std::string &S) {
418 if (!S.empty())
419 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000420 }
Evan Chengf5493192006-08-26 01:02:19 +0000421 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000422 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000423 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000424 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000425 void emitOpcode(const std::string &Opc) {
426 TargetOpcodes.push_back(Opc);
427 OpcNo++;
428 }
Evan Chengf8729402006-07-16 06:12:52 +0000429 void emitVT(const std::string &VT) {
430 TargetVTs.push_back(VT);
431 VTNo++;
432 }
Evan Chengb915f312005-12-09 22:45:35 +0000433public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000434 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000435 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000436 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000437 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000438 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000439 std::vector<std::string> &tv,
440 bool &oiv,
441 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000442 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000443 GeneratedCode(gc), GeneratedDecl(gd),
444 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000445 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000446 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000447
448 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
449 /// if the match fails. At this point, we already know that the opcode for N
450 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000451 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
452 const std::string &RootName, const std::string &ChainSuffix,
453 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000454
455 // Save loads/stores matched by a pattern.
456 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000457 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000458 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000459 }
460
Evan Chenge41bf822006-02-05 06:43:12 +0000461 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000462 // Emit instruction predicates. Each predicate is just a string for now.
463 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000464 // Record input varargs info.
465 NumInputRootOps = N->getNumChildren();
466
Evan Chengf9d03182008-07-03 08:39:51 +0000467 if (DisablePatternForFastISel(N, CGP))
Bill Wendling98a366d2009-04-29 23:29:43 +0000468 emitCheck("OptLevel != CodeGenOpt::None");
Evan Chengf9d03182008-07-03 08:39:51 +0000469
Chris Lattner8a0604b2006-01-28 20:31:24 +0000470 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000471 }
472
Evan Chengb915f312005-12-09 22:45:35 +0000473 if (N->isLeaf()) {
474 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000475 emitCheck("cast<ConstantSDNode>(" + RootName +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000476 ")->getSExtValue() == INT64_C(" +
477 itostr(II->getValue()) + ")");
Evan Chengb915f312005-12-09 22:45:35 +0000478 return;
479 } else if (!NodeIsComplexPattern(N)) {
480 assert(0 && "Cannot match this as a leaf value!");
481 abort();
482 }
483 }
484
Chris Lattner488580c2006-01-28 19:06:51 +0000485 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000486 // we already saw this in the pattern, emit code to verify dagness.
487 if (!N->getName().empty()) {
488 std::string &VarMapEntry = VariableMap[N->getName()];
489 if (VarMapEntry.empty()) {
490 VarMapEntry = RootName;
491 } else {
492 // If we get here, this is a second reference to a specific name. Since
493 // we already have checked that the first reference is valid, we don't
494 // have to recursively match it, just check that it's the same as the
495 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000496 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000497 return;
498 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000499
500 if (!N->isLeaf())
501 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000502 }
503
504
505 // Emit code to load the child nodes and match their contents recursively.
506 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000507 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
508 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000509 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000510 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000511 if (NodeHasChain)
512 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000513 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000514 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000515 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000516 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000517 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000518 // If the immediate use can somehow reach this node through another
519 // path, then can't fold it either or it will create a cycle.
520 // e.g. In the following diagram, XX can reach ld through YY. If
521 // ld is folded into XX, then YY is both a predecessor and a successor
522 // of XX.
523 //
524 // [ld]
525 // ^ ^
526 // | |
527 // / \---
528 // / [YY]
529 // | ^
530 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000531 bool NeedCheck = P != Pattern;
532 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000533 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000534 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000535 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
536 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
537 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000538 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000539 PInfo.hasProperty(SDNPHasChain) ||
540 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000541 PInfo.hasProperty(SDNPOptInFlag);
542 }
543
544 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000545 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng884c70c2008-11-27 00:49:46 +0000546 emitCheck("IsLegalAndProfitableToFold(" + RootName +
547 ".getNode(), " + ParentName + ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000548 }
Evan Chenge41bf822006-02-05 06:43:12 +0000549 }
Evan Chengb915f312005-12-09 22:45:35 +0000550 }
Evan Chenge41bf822006-02-05 06:43:12 +0000551
Evan Chengc15d18c2006-01-27 22:13:45 +0000552 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000553 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000554 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
555 "IsChainCompatible(" + ChainName + ".getNode(), " +
556 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000557 OrigChains.push_back(std::make_pair(ChainName, RootName));
558 } else
Evan Chenge6389932006-07-21 22:19:51 +0000559 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000560 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000561 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000562 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000563 }
Evan Chengb915f312005-12-09 22:45:35 +0000564 }
565
Evan Cheng54597732006-01-26 00:22:25 +0000566 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000567 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000568 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000569 // FIXME: If the optional incoming flag does not exist. Then it is ok to
570 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000571 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000572 (PatternHasProperty(N, SDNPInFlag, CGP) ||
573 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
574 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000575 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000576 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000577 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000578 }
579 }
580
Dan Gohman0540e172008-10-15 06:17:21 +0000581 // If there are node predicates for this, emit the calls.
582 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
583 emitCheck(N->getPredicateFns()[i] + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000584
Chris Lattner39e73f72006-10-11 04:05:55 +0000585 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
586 // a constant without a predicate fn that has more that one bit set, handle
587 // this as a special case. This is usually for targets that have special
588 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
589 // handling stuff). Using these instructions is often far more efficient
590 // than materializing the constant. Unfortunately, both the instcombiner
591 // and the dag combiner can often infer that bits are dead, and thus drop
592 // them from the mask in the dag. For example, it might turn 'AND X, 255'
593 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
594 // to handle this.
595 if (!N->isLeaf() &&
596 (N->getOperator()->getName() == "and" ||
597 N->getOperator()->getName() == "or") &&
598 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000599 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000600 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
601 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000602 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000603 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000604 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000605 RootName + ".getOperand(" + utostr(1) + ");");
606
Dan Gohman0b53d982008-12-19 18:13:39 +0000607 unsigned NTmp = TmpNo++;
608 emitCode("ConstantSDNode *Tmp" + utostr(NTmp) +
609 " = dyn_cast<ConstantSDNode>(" + RootName + "1);");
610 emitCheck("Tmp" + utostr(NTmp));
Chris Lattner39e73f72006-10-11 04:05:55 +0000611 const char *MaskPredicate = N->getOperator()->getName() == "or"
612 ? "CheckOrMask(" : "CheckAndMask(";
Dan Gohman0b53d982008-12-19 18:13:39 +0000613 emitCheck(MaskPredicate + RootName + "0, Tmp" + utostr(NTmp) +
614 ", INT64_C(" + itostr(II->getValue()) + "))");
Chris Lattner39e73f72006-10-11 04:05:55 +0000615
Christopher Lamb85356242008-01-31 07:27:46 +0000616 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000617 ChainSuffix + utostr(0), FoundChain);
618 return;
619 }
620 }
621 }
622
Evan Chengb915f312005-12-09 22:45:35 +0000623 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000624 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000625 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000626
Christopher Lamb85356242008-01-31 07:27:46 +0000627 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000628 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000629 }
630
Evan Cheng676d7312006-08-26 00:59:04 +0000631 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000632 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000633 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000634 std::string Fn = CP->getSelectFunc();
635 unsigned NumOps = CP->getNumOperands();
636 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000637 emitDecl("CPTmp" + RootName + "_" + utostr(i));
638 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000639 }
Evan Cheng94b30402006-10-11 21:02:01 +0000640 if (CP->hasProperty(SDNPHasChain)) {
641 emitDecl("CPInChain");
642 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000643 emitCode("SDValue CPInChain;");
644 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000645 }
Evan Cheng676d7312006-08-26 00:59:04 +0000646
Evan Cheng811731e2006-11-08 20:31:10 +0000647 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000648 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000649 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000650 if (CP->hasProperty(SDNPHasChain)) {
651 ChainName = "Chain" + ChainSuffix;
652 Code += ", CPInChain, Chain" + ChainSuffix;
653 }
Evan Cheng676d7312006-08-26 00:59:04 +0000654 emitCheck(Code + ")");
655 }
Evan Chengb915f312005-12-09 22:45:35 +0000656 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000657
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000658 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000659 const std::string &RootName,
660 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000661 const std::string &ChainSuffix, bool &FoundChain) {
662 if (!Child->isLeaf()) {
663 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000664 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000665 emitCheck(RootName + ".getOpcode() == " +
666 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000667 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000668 bool HasChain = false;
669 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
670 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000671 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000672 }
Dale Johannesen4150d832009-06-01 23:27:20 +0000673 if (NodeHasProperty(Child, SDNPOutFlag, CGP) ||
674 NodeHasProperty(Child, SDNPOutI1, CGP)) {
Evan Chenga58891f2008-02-05 22:50:29 +0000675 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
676 "Pattern folded multiple nodes which produce flags?");
677 FoldedFlag = std::make_pair(RootName,
678 CInfo.getNumResults() + (unsigned)HasChain);
679 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000680 } else {
681 // If this child has a name associated with it, capture it in VarMap. If
682 // we already saw this in the pattern, emit code to verify dagness.
683 if (!Child->getName().empty()) {
684 std::string &VarMapEntry = VariableMap[Child->getName()];
685 if (VarMapEntry.empty()) {
686 VarMapEntry = RootName;
687 } else {
688 // If we get here, this is a second reference to a specific name.
689 // Since we already have checked that the first reference is valid,
690 // we don't have to recursively match it, just check that it's the
691 // same as the previously named thing.
692 emitCheck(VarMapEntry + " == " + RootName);
693 Duplicates.insert(RootName);
694 return;
695 }
696 }
697
698 // Handle leaves of various types.
699 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
700 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000701 if (LeafRec->isSubClassOf("RegisterClass") ||
702 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000703 // Handle register references. Nothing to do here.
704 } else if (LeafRec->isSubClassOf("Register")) {
705 // Handle register references.
706 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
707 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000708 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000709 std::string Fn = CP->getSelectFunc();
710 unsigned NumOps = CP->getNumOperands();
711 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000712 emitDecl("CPTmp" + RootName + "_" + utostr(i));
713 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000714 }
Evan Cheng94b30402006-10-11 21:02:01 +0000715 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000716 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000717 FoldedChains.push_back(std::make_pair("CPInChain",
718 PInfo.getNumResults()));
719 ChainName = "Chain" + ChainSuffix;
720 emitDecl("CPInChain");
721 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000722 emitCode("SDValue CPInChain;");
723 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000724 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000725
Christopher Lamb85356242008-01-31 07:27:46 +0000726 std::string Code = Fn + "(";
727 if (CP->hasAttribute(CPAttrParentAsRoot)) {
728 Code += ParentRootName + ", ";
729 } else {
730 Code += "N, ";
731 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000732 if (CP->hasProperty(SDNPHasChain)) {
733 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000734 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000735 }
736 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000737 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000738 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000739 if (CP->hasProperty(SDNPHasChain))
740 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000741 emitCheck(Code + ")");
742 } else if (LeafRec->getName() == "srcvalue") {
743 // Place holder for SRCVALUE nodes. Nothing to do here.
744 } else if (LeafRec->isSubClassOf("ValueType")) {
745 // Make sure this is the specified value type.
746 emitCheck("cast<VTSDNode>(" + RootName +
747 ")->getVT() == MVT::" + LeafRec->getName());
748 } else if (LeafRec->isSubClassOf("CondCode")) {
749 // Make sure this is the specified cond code.
750 emitCheck("cast<CondCodeSDNode>(" + RootName +
751 ")->get() == ISD::" + LeafRec->getName());
752 } else {
753#ifndef NDEBUG
754 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000755 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000756#endif
757 assert(0 && "Unknown leaf type!");
758 }
759
Dan Gohman0540e172008-10-15 06:17:21 +0000760 // If there are node predicates for this, emit the calls.
761 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
762 emitCheck(Child->getPredicateFns()[i] + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000763 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000764 } else if (IntInit *II =
765 dynamic_cast<IntInit*>(Child->getLeafValue())) {
Dan Gohman0b53d982008-12-19 18:13:39 +0000766 unsigned NTmp = TmpNo++;
767 emitCode("ConstantSDNode *Tmp"+ utostr(NTmp) +
768 " = dyn_cast<ConstantSDNode>("+
769 RootName + ");");
770 emitCheck("Tmp" + utostr(NTmp));
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000771 unsigned CTmp = TmpNo++;
Dan Gohman0b53d982008-12-19 18:13:39 +0000772 emitCode("int64_t CN"+ utostr(CTmp) +
773 " = Tmp" + utostr(NTmp) + "->getSExtValue();");
Dan Gohman63f97202008-10-17 01:33:43 +0000774 emitCheck("CN" + utostr(CTmp) + " == "
775 "INT64_C(" +itostr(II->getValue()) + ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000776 } else {
777#ifndef NDEBUG
778 Child->dump();
779#endif
780 assert(0 && "Unknown leaf type!");
781 }
782 }
783 }
Evan Chengb915f312005-12-09 22:45:35 +0000784
785 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
786 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000787 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000788 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000789 bool InFlagDecled, bool ResNodeDecled,
790 bool LikeLeaf = false, bool isRoot = false) {
791 // List of arguments of getTargetNode() or SelectNodeTo().
792 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000793 // This is something selected from the pattern we matched.
794 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000795 const std::string &VarName = N->getName();
796 std::string Val = VariableMap[VarName];
797 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000798 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000799 cerr << "Variable '" << VarName << " referenced but not defined "
800 << "and not caught earlier!\n";
801 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000802 }
Evan Chengb915f312005-12-09 22:45:35 +0000803 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
804 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000805 NodeOps.push_back(Val);
806 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000807 }
808
809 const ComplexPattern *CP;
810 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000811 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000812 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000813 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000814 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000815 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000816 default:
817 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
818 << " type as an immediate constant. Aborting\n";
819 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000820 case MVT::i1: CastType = "bool"; break;
821 case MVT::i8: CastType = "unsigned char"; break;
822 case MVT::i16: CastType = "unsigned short"; break;
823 case MVT::i32: CastType = "unsigned"; break;
824 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000825 }
Dan Gohman475871a2008-07-27 21:46:04 +0000826 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000827 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000828 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000829 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000830 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
831 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000832 Val = TmpVar;
833 ModifiedVal = true;
834 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000835 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
836 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
837 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000838 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000839 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
840 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
841 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000842 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
843 // value if used multiple times by this pattern result.
844 Val = TmpVar;
845 ModifiedVal = true;
846 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000847 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000848 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000849 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000850 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000851 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000852 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000853 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000854 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000855 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000856 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
857 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000858 Val = TmpVar;
859 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000860 }
Scott Michel6be48d42008-01-29 02:29:31 +0000861 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000862 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
863 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000864 Record *Op = OperatorMap[N->getName()];
865 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000866 if (Op && (Op->getName() == "globaladdr" ||
867 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000868 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000869 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000870 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000871 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000872 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000873 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
874 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000875 Val = TmpVar;
876 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000877 }
Evan Cheng676d7312006-08-26 00:59:04 +0000878 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000879 } else if (!N->isLeaf()
880 && (N->getOperator()->getName() == "texternalsym"
881 || N->getOperator()->getName() == "tconstpool")) {
882 // Do not rewrite the variable name, since we don't generate a new
883 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000884 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000885 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000886 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000887 NodeOps.push_back("CPTmp" + Val + "_" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000888 }
Evan Chengb915f312005-12-09 22:45:35 +0000889 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000890 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000891 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000892 if (!LikeLeaf) {
Chris Lattner706d2d32006-08-09 16:44:44 +0000893 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000894 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000895 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000896 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000897 }
Evan Cheng676d7312006-08-26 00:59:04 +0000898 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000899 }
Scott Michel6be48d42008-01-29 02:29:31 +0000900
901 if (ModifiedVal) {
902 VariableMap[VarName] = Val;
903 }
Evan Cheng676d7312006-08-26 00:59:04 +0000904 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000905 }
Evan Chengb915f312005-12-09 22:45:35 +0000906 if (N->isLeaf()) {
907 // If this is an explicit register reference, handle it.
908 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
909 unsigned ResNo = TmpNo++;
910 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000911 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000912 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000913 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000914 NodeOps.push_back("Tmp" + utostr(ResNo));
915 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000916 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000917 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000918 " = CurDAG->getRegister(0, " +
919 getEnumName(N->getTypeNum(0)) + ");");
920 NodeOps.push_back("Tmp" + utostr(ResNo));
921 return NodeOps;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000922 } else if (DI->getDef()->isSubClassOf("RegisterClass")) {
923 // Handle a reference to a register class. This is used
924 // in COPY_TO_SUBREG instructions.
925 emitCode("SDValue Tmp" + utostr(ResNo) +
926 " = CurDAG->getTargetConstant(" +
927 getQualifiedName(DI->getDef()) + "RegClassID, " +
928 "MVT::i32);");
929 NodeOps.push_back("Tmp" + utostr(ResNo));
930 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000931 }
932 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
933 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000934 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000935 emitCode("SDValue Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000936 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
937 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000938 NodeOps.push_back("Tmp" + utostr(ResNo));
939 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000940 }
941
Jim Laskey16d42c62006-07-11 18:25:13 +0000942#ifndef NDEBUG
943 N->dump();
944#endif
Evan Chengb915f312005-12-09 22:45:35 +0000945 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000946 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000947 }
948
949 Record *Op = N->getOperator();
950 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000951 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000952 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000953 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000954 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000955 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000956 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000957 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
958 : (InstPat ? InstPat->getTree(0) : NULL);
Dan Gohmanfebf71d2009-01-16 21:30:55 +0000959 if (InstPatNode && !InstPatNode->isLeaf() &&
960 InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000961 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000962 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000963 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000964 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000965 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000966 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000967 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000968 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000969 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000970 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000971 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000972 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Dale Johannesen4150d832009-06-01 23:27:20 +0000973 bool NodeHasInI1 = isRoot &&
974 PatternHasProperty(Pattern, SDNPInI1, CGP);
975 bool NodeHasOutI1 = isRoot &&
976 PatternHasProperty(Pattern, SDNPOutI1, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000977 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000978 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000979 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000980 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000981 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000982 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000983
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000984 // Record output varargs info.
985 OutputIsVariadic = IsVariadic;
986
Evan Chengfceb57a2006-07-15 08:45:20 +0000987 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000988 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000989 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000990 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000991 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000992 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000993
Evan Cheng823b7522006-01-19 21:57:10 +0000994 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000995 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000996 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000997 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000998 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000999 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +00001000 }
1001
Evan Cheng4326ef52006-10-12 02:08:53 +00001002 if (OrigChains.size() > 0) {
1003 // The original input chain is being ignored. If it is not just
1004 // pointing to the op that's being folded, we should create a
1005 // TokenFactor with it and the chain of the folded op as the new chain.
1006 // We could potentially be doing multiple levels of folding, in that
1007 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001008 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +00001009 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001010 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
1011 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +00001012 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
1013 emitCode("}");
1014 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001015 emitCode("InChains.push_back(" + ChainName + ");");
Dale Johannesened2eee62009-02-06 01:31:28 +00001016 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, "
1017 "N.getDebugLoc(), MVT::Other, "
Evan Cheng4326ef52006-10-12 02:08:53 +00001018 "&InChains[0], InChains.size());");
David Greene8ad4c002008-10-27 21:56:29 +00001019 if (GenDebug) {
1020 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
1021 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
1022 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001023 }
1024
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001025 // Loop over all of the operands of the instruction pattern, emitting code
1026 // to fill them all in. The node 'N' usually has number children equal to
1027 // the number of input operands of the instruction. However, in cases
1028 // where there are predicate operands for an instruction, we need to fill
1029 // in the 'execute always' values. Match up the node operands to the
1030 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +00001031 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001032 for (unsigned ChildNo = 0, InstOpNo = NumResults;
1033 InstOpNo != II.OperandList.size(); ++InstOpNo) {
1034 std::vector<std::string> Ops;
1035
Dan Gohmand35121a2008-05-29 19:57:41 +00001036 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +00001037 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001038 if ((OperandNode->isSubClassOf("PredicateOperand") ||
1039 OperandNode->isSubClassOf("OptionalDefOperand")) &&
1040 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +00001041 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +00001042 // 'default ops' operands.
1043 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001044 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001045 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001046 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001047 InFlagDecled, ResNodeDecled);
1048 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1049 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001050 } else {
1051 // Otherwise this is a normal operand or a predicate operand without
1052 // 'execute always'; emit it.
1053 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1054 InFlagDecled, ResNodeDecled);
1055 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1056 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001057 }
Evan Chengb915f312005-12-09 22:45:35 +00001058 }
1059
Evan Chengb915f312005-12-09 22:45:35 +00001060 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001061 bool ChainEmitted = NodeHasChain;
Dale Johannesen4150d832009-06-01 23:27:20 +00001062 // InFlag and InI1 cannot both be set (checked in
1063 // CodeGenDAGPatterns), so use the same variables for both.
1064 if (NodeHasInFlag || HasImpInputs || NodeHasInI1)
Evan Cheng676d7312006-08-26 00:59:04 +00001065 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1066 InFlagDecled, ResNodeDecled, true);
Dale Johannesen4150d832009-06-01 23:27:20 +00001067 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs ||
1068 NodeHasInI1) {
Evan Cheng676d7312006-08-26 00:59:04 +00001069 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001070 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001071 InFlagDecled = true;
1072 }
Evan Chengf037ca62006-08-27 08:11:28 +00001073 if (NodeHasOptInFlag) {
1074 emitCode("if (HasInFlag) {");
1075 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
Evan Chengf037ca62006-08-27 08:11:28 +00001076 emitCode("}");
1077 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001078 }
Evan Chengb915f312005-12-09 22:45:35 +00001079
Evan Chengb915f312005-12-09 22:45:35 +00001080 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001081
Dan Gohman95d11092008-07-07 21:00:17 +00001082 unsigned OpsNo = OpcNo;
1083 std::string CodePrefix;
1084 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1085 std::deque<std::string> After;
1086 std::string NodeName;
1087 if (!isRoot) {
1088 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001089 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001090 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001091 NodeName = "ResNode";
1092 if (!ResNodeDecled) {
1093 CodePrefix = "SDNode *" + NodeName + " = ";
1094 ResNodeDecled = true;
1095 } else
1096 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001097 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001098
Dan Gohman95d11092008-07-07 21:00:17 +00001099 std::string Code = "Opc" + utostr(OpcNo);
1100
Bill Wendling6e1bb382009-01-29 05:27:31 +00001101 if (!isRoot || (InputHasChain && !NodeHasChain))
Bill Wendlingca641832009-01-29 23:19:43 +00001102 // For call to "getTargetNode()".
Bill Wendling6e1bb382009-01-29 05:27:31 +00001103 Code += ", N.getDebugLoc()";
1104
Dan Gohman95d11092008-07-07 21:00:17 +00001105 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1106
1107 // Output order: results, chain, flags
1108 // Result types.
1109 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1110 Code += ", VT" + utostr(VTNo);
1111 emitVT(getEnumName(N->getTypeNum(0)));
1112 }
1113 // Add types for implicit results in physical registers, scheduler will
1114 // care of adding copyfromreg nodes.
1115 for (unsigned i = 0; i < NumDstRegs; i++) {
1116 Record *RR = DstRegs[i];
1117 if (RR->isSubClassOf("Register")) {
1118 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
1119 Code += ", " + getEnumName(RVT);
1120 }
1121 }
1122 if (NodeHasChain)
1123 Code += ", MVT::Other";
Dale Johannesen4150d832009-06-01 23:27:20 +00001124 if (NodeHasOutFlag || (NodeHasOutI1 && !CGT.supportsHasI1()))
Dan Gohman95d11092008-07-07 21:00:17 +00001125 Code += ", MVT::Flag";
1126
1127 // Inputs.
1128 if (IsVariadic) {
1129 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1130 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1131 AllOps.clear();
1132
1133 // Figure out whether any operands at the end of the op list are not
1134 // part of the variable section.
1135 std::string EndAdjust;
1136 if (NodeHasInFlag || HasImpInputs)
1137 EndAdjust = "-1"; // Always has one flag.
1138 else if (NodeHasOptInFlag)
1139 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1140
1141 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1142 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1143
Dan Gohman95d11092008-07-07 21:00:17 +00001144 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1145 emitCode("}");
1146 }
1147
1148 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1149 // this pattern.
Dan Gohman41474ba2008-12-03 02:30:17 +00001150 if (II.mayLoad | II.mayStore) {
Dan Gohman95d11092008-07-07 21:00:17 +00001151 std::vector<std::string>::const_iterator mi, mie;
1152 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
David Greene8ad4c002008-10-27 21:56:29 +00001153 std::string LSIName = "LSI_" + *mi;
1154 emitCode("SDValue " + LSIName + " = "
Dan Gohman95d11092008-07-07 21:00:17 +00001155 "CurDAG->getMemOperand(cast<MemSDNode>(" +
1156 *mi + ")->getMemOperand());");
David Greene8ad4c002008-10-27 21:56:29 +00001157 if (GenDebug) {
1158 emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"yellow\");");
1159 emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"black\");");
1160 }
Dan Gohman95d11092008-07-07 21:00:17 +00001161 if (IsVariadic)
David Greene8ad4c002008-10-27 21:56:29 +00001162 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + LSIName + ");");
Dan Gohman95d11092008-07-07 21:00:17 +00001163 else
David Greene8ad4c002008-10-27 21:56:29 +00001164 AllOps.push_back(LSIName);
Dan Gohman95d11092008-07-07 21:00:17 +00001165 }
1166 }
1167
1168 if (NodeHasChain) {
1169 if (IsVariadic)
1170 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1171 else
1172 AllOps.push_back(ChainName);
1173 }
1174
1175 if (IsVariadic) {
1176 if (NodeHasInFlag || HasImpInputs)
1177 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1178 else if (NodeHasOptInFlag) {
1179 emitCode("if (HasInFlag)");
1180 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1181 }
1182 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1183 ".size()";
Dale Johannesen4150d832009-06-01 23:27:20 +00001184 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs ||
1185 NodeHasInI1)
Dan Gohman95d11092008-07-07 21:00:17 +00001186 AllOps.push_back("InFlag");
1187
1188 unsigned NumOps = AllOps.size();
1189 if (NumOps) {
1190 if (!NodeHasOptInFlag && NumOps < 4) {
1191 for (unsigned i = 0; i != NumOps; ++i)
1192 Code += ", " + AllOps[i];
1193 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001194 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001195 for (unsigned i = 0; i != NumOps; ++i) {
1196 OpsCode += AllOps[i];
1197 if (i != NumOps-1)
1198 OpsCode += ", ";
1199 }
1200 emitCode(OpsCode + " };");
1201 Code += ", Ops" + utostr(OpsNo) + ", ";
1202 if (NodeHasOptInFlag) {
1203 Code += "HasInFlag ? ";
1204 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1205 } else
1206 Code += utostr(NumOps);
1207 }
1208 }
1209
1210 if (!isRoot)
1211 Code += "), 0";
1212
Dan Gohmane8be6c62008-07-17 19:10:17 +00001213 std::vector<std::string> ReplaceFroms;
1214 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001215 if (!isRoot) {
1216 NodeOps.push_back("Tmp" + utostr(ResNo));
1217 } else {
1218
Dale Johannesen4150d832009-06-01 23:27:20 +00001219 if (NodeHasOutFlag || NodeHasOutI1) {
Dan Gohman95d11092008-07-07 21:00:17 +00001220 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001221 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001222 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1223 ");");
1224 InFlagDecled = true;
1225 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001226 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001227 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1228 ");");
1229 }
1230
Dan Gohman1eb49a02009-01-05 19:31:28 +00001231 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
1232 ReplaceFroms.push_back("SDValue(" +
1233 FoldedChains[j].first + ".getNode(), " +
1234 utostr(FoldedChains[j].second) +
1235 ")");
1236 ReplaceTos.push_back("SDValue(ResNode, " +
1237 utostr(NumResults+NumDstRegs) + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001238 }
1239
Dale Johannesen4150d832009-06-01 23:27:20 +00001240 if (NodeHasOutFlag || NodeHasOutI1) {
Dan Gohman95d11092008-07-07 21:00:17 +00001241 if (FoldedFlag.first != "") {
Dale Johannesen4150d832009-06-01 23:27:20 +00001242 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first +
1243 ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001244 utostr(FoldedFlag.second) + ")");
1245 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001246 } else {
Dale Johannesen4150d832009-06-01 23:27:20 +00001247 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP) ||
1248 NodeHasProperty(Pattern, SDNPOutI1, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001249 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001250 utostr(NumPatResults + (unsigned)InputHasChain)
1251 + ")");
1252 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001253 }
Dan Gohman95d11092008-07-07 21:00:17 +00001254 }
1255
Dan Gohmane8be6c62008-07-17 19:10:17 +00001256 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001257 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001258 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001259 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001260 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001261 ChainAssignmentNeeded |= NodeHasChain;
1262 }
1263
1264 // User does not expect the instruction would produce a chain!
Dale Johannesen4150d832009-06-01 23:27:20 +00001265 if ((!InputHasChain && NodeHasChain) &&
1266 (NodeHasOutFlag || NodeHasOutI1)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001267 ;
1268 } else if (InputHasChain && !NodeHasChain) {
1269 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001270 if (NodeHasOutFlag) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001271 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001272 utostr(NumPatResults+1) +
1273 ")");
Gabor Greif99a6cb92008-08-26 22:36:50 +00001274 ReplaceTos.push_back("SDValue(ResNode, N.getResNo()-1)");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001275 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001276 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001277 utostr(NumPatResults) + ")");
1278 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001279 }
1280 }
1281
1282 if (ChainAssignmentNeeded) {
1283 // Remember which op produces the chain.
1284 std::string ChainAssign;
1285 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001286 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001287 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001288 else
Dan Gohman475871a2008-07-27 21:46:04 +00001289 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001290 ", " + utostr(NumResults+NumDstRegs) + ");";
1291
1292 After.push_front(ChainAssign);
1293 }
1294
Dan Gohmane8be6c62008-07-17 19:10:17 +00001295 if (ReplaceFroms.size() == 1) {
1296 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1297 ReplaceTos[0] + ");");
1298 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001299 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001300 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1301 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1302 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001303 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001304 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1305 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1306 After.push_back("};");
1307 After.push_back("ReplaceUses(Froms, Tos, " +
1308 itostr(ReplaceFroms.size()) + ");");
1309 }
1310
1311 // We prefer to use SelectNodeTo since it avoids allocation when
1312 // possible and it avoids CSE map recalculation for the node's
1313 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001314 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001315 // We also don't use if the pattern replacement is being used to
1316 // jettison a chain result, since morphing the node in place
1317 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001318 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001319 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001320 Code = "CurDAG->getTargetNode(" + Code;
1321 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001322 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001323 }
1324 if (isRoot) {
1325 if (After.empty())
1326 CodePrefix = "return ";
1327 else
1328 After.push_back("return ResNode;");
1329 }
1330
1331 emitCode(CodePrefix + Code + ");");
David Greene8ad4c002008-10-27 21:56:29 +00001332
1333 if (GenDebug) {
1334 if (!isRoot) {
1335 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
1336 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
1337 }
1338 else {
1339 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
1340 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
1341 }
1342 }
1343
Dan Gohman95d11092008-07-07 21:00:17 +00001344 for (unsigned i = 0, e = After.size(); i != e; ++i)
1345 emitCode(After[i]);
1346
Evan Cheng676d7312006-08-26 00:59:04 +00001347 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001348 }
1349 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001350 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001351 // PatLeaf node - the operand may or may not be a leaf node. But it should
1352 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001353 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001354 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001355 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001356 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001357 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001358 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001359 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001360 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001361 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001362 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001363 }
Dan Gohman0540e172008-10-15 06:17:21 +00001364
1365 N->dump();
1366 cerr << "\n";
1367 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001368 }
1369
Chris Lattner488580c2006-01-28 19:06:51 +00001370 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1371 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001372 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1373 /// for, this returns true otherwise false if Pat has all types.
1374 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001375 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001376 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001377 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001378 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001379 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001380 // The top level node type is checked outside of the select function.
1381 if (!isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001382 emitCheck(Prefix + ".getNode()->getValueType(0) == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001383 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001384 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001385 }
1386
Evan Cheng51fecc82006-01-09 18:27:06 +00001387 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001388 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001389 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1390 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1391 Prefix + utostr(OpNo)))
1392 return true;
1393 return false;
1394 }
1395
1396private:
Evan Cheng54597732006-01-26 00:22:25 +00001397 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001398 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001399 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001400 bool &ChainEmitted, bool &InFlagDecled,
1401 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001402 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001403 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001404 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1405 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Dale Johannesen4150d832009-06-01 23:27:20 +00001406 bool HasInI1 = NodeHasProperty(N, SDNPInI1, CGP);
1407 bool InFlagDefined = false;
Evan Chengb915f312005-12-09 22:45:35 +00001408 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1409 TreePatternNode *Child = N->getChild(i);
1410 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001411 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1412 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001413 } else {
1414 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001415 if (!Child->getName().empty()) {
1416 std::string Name = RootName + utostr(OpNo);
1417 if (Duplicates.find(Name) != Duplicates.end())
1418 // A duplicate! Do not emit a copy for this node.
1419 continue;
1420 }
1421
Evan Chengb915f312005-12-09 22:45:35 +00001422 Record *RR = DI->getDef();
1423 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001424 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001425 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001426 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001427 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001428 InFlagDecled = true;
1429 } else
1430 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
Evan Chengb2c6d492006-01-11 22:16:13 +00001431 } else {
1432 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001433 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001434 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001435 ChainEmitted = true;
1436 }
Evan Cheng676d7312006-08-26 00:59:04 +00001437 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001438 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001439 InFlagDecled = true;
1440 }
Dale Johannesen4150d832009-06-01 23:27:20 +00001441 if (HasInI1) {
1442 if (!ResNodeDecled) {
1443 emitCode("SDNode * ResNode;");
1444 }
1445 if (T.supportsHasI1())
1446 emitCode("ResNode = CurDAG->getCopyToReg(" + ChainName +
1447 ", " + RootName + ".getDebugLoc()" +
1448 ", " + getEnumName(RVT) +
1449 ", " + getQualifiedName(RR) +
1450 ", " + RootName + utostr(OpNo) + ").getNode();");
1451 else
1452 emitCode("ResNode = CurDAG->getCopyToReg(" + ChainName +
1453 ", " + RootName + ".getDebugLoc()" +
1454 ", " + getQualifiedName(RR) +
1455 ", " + RootName + utostr(OpNo) +
1456 ", InFlag).getNode();");
1457 InFlagDefined = true;
1458 } else {
1459 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1460 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Dale Johannesena05dca42009-02-04 23:02:30 +00001461 ", " + RootName + ".getDebugLoc()" +
Chris Lattner6cefb772008-01-05 22:25:12 +00001462 ", " + getQualifiedName(RR) +
Dale Johannesen4150d832009-06-01 23:27:20 +00001463 ", " + RootName + utostr(OpNo) +
1464 ", InFlag).getNode();");
1465 }
Dan Gohman475871a2008-07-27 21:46:04 +00001466 emitCode(ChainName + " = SDValue(ResNode, 0);");
1467 emitCode("InFlag = SDValue(ResNode, 1);");
Dale Johannesen4150d832009-06-01 23:27:20 +00001468 ResNodeDecled = true;
Evan Chengb915f312005-12-09 22:45:35 +00001469 }
1470 }
1471 }
1472 }
1473 }
Evan Cheng54597732006-01-26 00:22:25 +00001474
Dale Johannesen4150d832009-06-01 23:27:20 +00001475 if (HasInFlag || (HasInI1 && !InFlagDefined)) {
Evan Cheng676d7312006-08-26 00:59:04 +00001476 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001477 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001478 ".getOperand(" + utostr(OpNo) + ");");
1479 InFlagDecled = true;
1480 } else
1481 emitCode("InFlag = " + RootName +
1482 ".getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001483 }
Evan Chengb915f312005-12-09 22:45:35 +00001484 }
1485};
1486
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001487/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1488/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001489/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001490void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001491 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001492 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001493 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001494 std::vector<std::string> &TargetVTs,
1495 bool &OutputIsVariadic,
1496 unsigned &NumInputRootOps) {
1497 OutputIsVariadic = false;
1498 NumInputRootOps = 0;
1499
Dan Gohman22bb3112008-08-22 00:20:26 +00001500 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001501 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001502 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001503 TargetOpcodes, TargetVTs,
1504 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001505
Chris Lattner8fc35682005-09-23 23:16:51 +00001506 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001507 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001508 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001509
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001510 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001511 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001512
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001513 // At this point, we know that we structurally match the pattern, but the
1514 // types of the nodes may not match. Figure out the fewest number of type
1515 // comparisons we need to emit. For example, if there is only one integer
1516 // type supported by a target, there should be no type comparisons at all for
1517 // integer patterns!
1518 //
1519 // To figure out the fewest number of type checks needed, clone the pattern,
1520 // remove the types, then perform type inference on the pattern as a whole.
1521 // If there are unresolved types, emit an explicit check for those types,
1522 // apply the type to the tree, then rerun type inference. Iterate until all
1523 // types are resolved.
1524 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001525 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001526 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001527
1528 do {
1529 // Resolve/propagate as many types as possible.
1530 try {
1531 bool MadeChange = true;
1532 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001533 MadeChange = Pat->ApplyTypeConstraints(TP,
1534 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001535 } catch (...) {
1536 assert(0 && "Error: could not find consistent types for something we"
1537 " already decided was ok!");
1538 abort();
1539 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001540
Chris Lattner7e82f132005-10-15 21:34:21 +00001541 // Insert a check for an unresolved type and add it to the tree. If we find
1542 // an unresolved type to add a check for, this returns true and we iterate,
1543 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001544 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001545
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001546 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001547 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001548 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001549}
1550
Chris Lattner24e00a42006-01-29 04:41:05 +00001551/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1552/// a line causes any of them to be empty, remove them and return true when
1553/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001554static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001555 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001556 &Patterns) {
1557 bool ErasedPatterns = false;
1558 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1559 Patterns[i].second.pop_back();
1560 if (Patterns[i].second.empty()) {
1561 Patterns.erase(Patterns.begin()+i);
1562 --i; --e;
1563 ErasedPatterns = true;
1564 }
1565 }
1566 return ErasedPatterns;
1567}
1568
Chris Lattner8bc74722006-01-29 04:25:26 +00001569/// EmitPatterns - Emit code for at least one pattern, but try to group common
1570/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001571void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001572 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001573 &Patterns, unsigned Indent,
1574 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001575 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001576 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001577 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001578
1579 if (Patterns.empty()) return;
1580
Chris Lattner24e00a42006-01-29 04:41:05 +00001581 // Figure out how many patterns share the next code line. Explicitly copy
1582 // FirstCodeLine so that we don't invalidate a reference when changing
1583 // Patterns.
1584 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001585 unsigned LastMatch = Patterns.size()-1;
1586 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1587 --LastMatch;
1588
1589 // If not all patterns share this line, split the list into two pieces. The
1590 // first chunk will use this line, the second chunk won't.
1591 if (LastMatch != 0) {
1592 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1593 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1594
1595 // FIXME: Emit braces?
1596 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001597 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001598 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1599 Pattern.getSrcPattern()->print(OS);
1600 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1601 Pattern.getDstPattern()->print(OS);
1602 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001603 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001604 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001605 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001606 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001607 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001608 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001609 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001610 }
Evan Cheng676d7312006-08-26 00:59:04 +00001611 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001612 OS << std::string(Indent, ' ') << "{\n";
1613 Indent += 2;
1614 }
1615 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001616 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001617 Indent -= 2;
1618 OS << std::string(Indent, ' ') << "}\n";
1619 }
1620
1621 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001622 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001623 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1624 Pattern.getSrcPattern()->print(OS);
1625 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1626 Pattern.getDstPattern()->print(OS);
1627 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001628 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001629 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001630 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001631 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001632 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001633 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001634 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001635 }
1636 EmitPatterns(Other, Indent, OS);
1637 return;
1638 }
1639
Chris Lattner24e00a42006-01-29 04:41:05 +00001640 // Remove this code from all of the patterns that share it.
1641 bool ErasedPatterns = EraseCodeLine(Patterns);
1642
Evan Cheng676d7312006-08-26 00:59:04 +00001643 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001644
1645 // Otherwise, every pattern in the list has this line. Emit it.
1646 if (!isPredicate) {
1647 // Normal code.
1648 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1649 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001650 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1651
1652 // If the next code line is another predicate, and if all of the pattern
1653 // in this group share the same next line, emit it inline now. Do this
1654 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001655 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Jim Grosbachda4231f2009-03-26 16:17:51 +00001656 // Check that all of the patterns in Patterns end with the same predicate.
Chris Lattner24e00a42006-01-29 04:41:05 +00001657 bool AllEndWithSamePredicate = true;
1658 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1659 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1660 AllEndWithSamePredicate = false;
1661 break;
1662 }
1663 // If all of the predicates aren't the same, we can't share them.
1664 if (!AllEndWithSamePredicate) break;
1665
1666 // Otherwise we can. Emit it shared now.
1667 OS << " &&\n" << std::string(Indent+4, ' ')
1668 << Patterns.back().second.back().second;
1669 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001670 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001671
1672 OS << ") {\n";
1673 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001674 }
1675
1676 EmitPatterns(Patterns, Indent, OS);
1677
1678 if (isPredicate)
1679 OS << std::string(Indent-2, ' ') << "}\n";
1680}
1681
Evan Cheng892aaf82006-11-08 23:01:03 +00001682static std::string getLegalCName(std::string OpName) {
1683 std::string::size_type pos = OpName.find("::");
1684 if (pos != std::string::npos)
1685 OpName.replace(pos, 2, "_");
1686 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001687}
1688
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001689void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001690 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001691
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001692 // Get the namespace to insert instructions into.
1693 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001694 if (!InstNS.empty()) InstNS += "::";
1695
Chris Lattner602f6922006-01-04 00:25:00 +00001696 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001697 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001698 // All unique target node emission functions.
1699 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001700 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001701 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001702 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001703
1704 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001705 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001706 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001707 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001708 } else {
1709 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001710 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001711 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001712 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001713 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001714 std::vector<Record*> OpNodes = CP->getRootNodes();
1715 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001716 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1717 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001718 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001719 }
1720 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001721 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001722 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001723 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001724 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001725 exit(1);
1726 }
1727 }
1728 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001729
1730 // For each opcode, there might be multiple select functions, one per
1731 // ValueType of the node (or its first operand if it doesn't produce a
1732 // non-chain result.
1733 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1734
Chris Lattner602f6922006-01-04 00:25:00 +00001735 // Emit one Select_* method for each top-level opcode. We do this instead of
1736 // emitting one giant switch statement to support compilers where this will
1737 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001738 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001739 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1740 PBOI != E; ++PBOI) {
1741 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001742 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001743 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1744
Chris Lattner706d2d32006-08-09 16:44:44 +00001745 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001746 std::map<MVT::SimpleValueType,
1747 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001748 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001749 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001750 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001751 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001752 }
1753
Duncan Sands83ec4b62008-06-06 12:08:01 +00001754 for (std::map<MVT::SimpleValueType,
1755 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001756 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1757 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001758 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001759 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001760 typedef std::pair<unsigned, std::string> CodeLine;
1761 typedef std::vector<CodeLine> CodeList;
1762 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001763
Chris Lattner60d81392008-01-05 22:30:17 +00001764 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001765 std::vector<std::vector<std::string> > PatternOpcodes;
1766 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001767 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001768 std::vector<bool> OutputIsVariadicFlags;
1769 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001770 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1771 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001772 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001773 std::vector<std::string> TargetOpcodes;
1774 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001775 bool OutputIsVariadic;
1776 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001777 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001778 TargetOpcodes, TargetVTs,
1779 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001780 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1781 PatternDecls.push_back(GeneratedDecl);
1782 PatternOpcodes.push_back(TargetOpcodes);
1783 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001784 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1785 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001786 }
1787
Chris Lattner706d2d32006-08-09 16:44:44 +00001788 // Factor target node emission code (emitted by EmitResultCode) into
1789 // separate functions. Uniquing and share them among all instruction
1790 // selection routines.
1791 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1792 CodeList &GeneratedCode = CodeForPatterns[i].second;
1793 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1794 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001795 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001796 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1797 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001798 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001799 int CodeSize = (int)GeneratedCode.size();
1800 int LastPred = -1;
1801 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001802 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001803 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001804 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1805 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001806 }
1807
Dan Gohman475871a2008-07-27 21:46:04 +00001808 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001809 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001810 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1811 CalleeCode += ", unsigned Opc" + utostr(j);
1812 CallerCode += ", " + TargetOpcodes[j];
1813 }
1814 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001815 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001816 CallerCode += ", " + TargetVTs[j];
1817 }
Evan Chengf5493192006-08-26 01:02:19 +00001818 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001819 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001820 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001821 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001822 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001823 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001824
1825 if (OutputIsVariadic) {
1826 CalleeCode += ", unsigned NumInputRootOps";
1827 CallerCode += ", " + utostr(NumInputRootOps);
1828 }
1829
Chris Lattner706d2d32006-08-09 16:44:44 +00001830 CallerCode += ");";
1831 CalleeCode += ") ";
1832 // Prevent emission routines from being inlined to reduce selection
1833 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001834 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001835 CalleeCode += "{\n";
1836
1837 for (std::vector<std::string>::const_reverse_iterator
1838 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1839 CalleeCode += " " + *I + "\n";
1840
Evan Chengf5493192006-08-26 01:02:19 +00001841 for (int j = LastPred+1; j < CodeSize; ++j)
1842 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001843 for (int j = LastPred+1; j < CodeSize; ++j)
1844 GeneratedCode.pop_back();
1845 CalleeCode += "}\n";
1846
1847 // Uniquing the emission routines.
1848 unsigned EmitFuncNum;
1849 std::map<std::string, unsigned>::iterator EFI =
1850 EmitFunctions.find(CalleeCode);
1851 if (EFI != EmitFunctions.end()) {
1852 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001853 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001854 EmitFuncNum = EmitFunctions.size();
1855 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001856 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001857 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001858
Chris Lattner706d2d32006-08-09 16:44:44 +00001859 // Replace the emission code within selection routines with calls to the
1860 // emission functions.
David Greene8ad4c002008-10-27 21:56:29 +00001861 if (GenDebug) {
1862 GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"red\");"));
1863 }
1864 CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
1865 GeneratedCode.push_back(std::make_pair(3, CallerCode));
1866 if (GenDebug) {
1867 GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
1868 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"yellow\");"));
1869 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"black\");"));
1870 GeneratedCode.push_back(std::make_pair(0, "}"));
1871 //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"black\");"));
1872 }
1873 GeneratedCode.push_back(std::make_pair(0, "return Result;"));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001874 }
1875
Chris Lattner706d2d32006-08-09 16:44:44 +00001876 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001877 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001878 if (OpVT == MVT::iPTR) {
1879 OpVTStr = "_iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +00001880 } else if (OpVT == MVT::iPTRAny) {
1881 OpVTStr = "_iPTRAny";
Chris Lattner33a40042006-11-14 22:17:10 +00001882 } else if (OpVT == MVT::isVoid) {
1883 // Nodes with a void result actually have a first result type of either
1884 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1885 // void to this case, we handle it specially here.
1886 } else {
1887 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1888 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001889 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1890 OpcodeVTMap.find(OpName);
1891 if (OpVTI == OpcodeVTMap.end()) {
1892 std::vector<std::string> VTSet;
1893 VTSet.push_back(OpVTStr);
1894 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1895 } else
1896 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001897
Dan Gohman0540e172008-10-15 06:17:21 +00001898 // We want to emit all of the matching code now. However, we want to emit
1899 // the matches in order of minimal cost. Sort the patterns so the least
1900 // cost one is at the start.
1901 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1902 PatternSortingPredicate(CGP));
1903
1904 // Scan the code to see if all of the patterns are reachable and if it is
1905 // possible that the last one might not match.
1906 bool mightNotMatch = true;
1907 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1908 CodeList &GeneratedCode = CodeForPatterns[i].second;
1909 mightNotMatch = false;
1910
1911 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1912 if (GeneratedCode[j].first == 1) { // predicate.
1913 mightNotMatch = true;
1914 break;
1915 }
1916 }
1917
1918 // If this pattern definitely matches, and if it isn't the last one, the
1919 // patterns after it CANNOT ever match. Error out.
1920 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
1921 cerr << "Pattern '";
1922 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1923 cerr << "' is impossible to select!\n";
1924 exit(1);
1925 }
1926 }
1927
Chris Lattner706d2d32006-08-09 16:44:44 +00001928 // Loop through and reverse all of the CodeList vectors, as we will be
1929 // accessing them from their logical front, but accessing the end of a
1930 // vector is more efficient.
1931 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1932 CodeList &GeneratedCode = CodeForPatterns[i].second;
1933 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001934 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001935
1936 // Next, reverse the list of patterns itself for the same reason.
1937 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1938
Dan Gohman63e3e632009-01-29 01:37:18 +00001939 OS << "SDNode *Select_" << getLegalCName(OpName)
1940 << OpVTStr << "(const SDValue &N) {\n";
1941
Chris Lattner706d2d32006-08-09 16:44:44 +00001942 // Emit all of the patterns now, grouped together to share code.
1943 EmitPatterns(CodeForPatterns, 2, OS);
1944
Chris Lattner64906972006-09-21 18:28:27 +00001945 // If the last pattern has predicates (which could fail) emit code to
1946 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001947 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001948 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001949 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1950 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001951 OpName != "ISD::INTRINSIC_VOID")
1952 OS << " CannotYetSelect(N);\n";
1953 else
1954 OS << " CannotYetSelectIntrinsic(N);\n";
1955
1956 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001957 }
1958 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001959 }
Chris Lattner602f6922006-01-04 00:25:00 +00001960 }
1961
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001962 // Emit boilerplate.
Dan Gohman475871a2008-07-27 21:46:04 +00001963 OS << "SDNode *Select_INLINEASM(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001964 << " std::vector<SDValue> Ops(N.getNode()->op_begin(), N.getNode()->op_end());\n"
Dan Gohmanf350b272008-08-23 02:25:05 +00001965 << " SelectInlineAsmMemoryOperands(Ops);\n\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001966
Duncan Sands83ec4b62008-06-06 12:08:01 +00001967 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001968 << " VTs.push_back(MVT::Other);\n"
1969 << " VTs.push_back(MVT::Flag);\n"
Dale Johannesen3484c092009-02-05 22:07:54 +00001970 << " SDValue New = CurDAG->getNode(ISD::INLINEASM, N.getDebugLoc(), "
1971 "VTs, &Ops[0], Ops.size());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001972 << " return New.getNode();\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001973 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001974
Dan Gohman475871a2008-07-27 21:46:04 +00001975 OS << "SDNode *Select_UNDEF(const SDValue &N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001976 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::IMPLICIT_DEF,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001977 << " N.getValueType());\n"
1978 << "}\n\n";
1979
Dan Gohman475871a2008-07-27 21:46:04 +00001980 OS << "SDNode *Select_DBG_LABEL(const SDValue &N) {\n"
1981 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001982 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001983 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001984 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DBG_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001985 << " MVT::Other, Tmp, Chain);\n"
1986 << "}\n\n";
1987
Dan Gohman475871a2008-07-27 21:46:04 +00001988 OS << "SDNode *Select_EH_LABEL(const SDValue &N) {\n"
1989 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001990 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001991 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001992 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EH_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001993 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001994 << "}\n\n";
1995
Dan Gohman475871a2008-07-27 21:46:04 +00001996 OS << "SDNode *Select_DECLARE(const SDValue &N) {\n"
1997 << " SDValue Chain = N.getOperand(0);\n"
1998 << " SDValue N1 = N.getOperand(1);\n"
1999 << " SDValue N2 = N.getOperand(2);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00002000 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002001 << " CannotYetSelect(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00002002 << " }\n"
2003 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
2004 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00002005 << " SDValue Tmp1 = "
Evan Chenga844bde2008-02-02 04:07:54 +00002006 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
Dan Gohman475871a2008-07-27 21:46:04 +00002007 << " SDValue Tmp2 = "
Evan Chenga844bde2008-02-02 04:07:54 +00002008 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00002009 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DECLARE,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00002010 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00002011 << "}\n\n";
2012
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002013 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00002014 << "SDNode *SelectCode(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00002015 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00002016 << " switch (N.getOpcode()) {\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00002017 << " default:\n"
2018 << " assert(!N.isMachineOpcode() && \"Node already selected!\");\n"
2019 << " break;\n"
2020 << " case ISD::EntryToken: // These nodes remain the same.\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00002021 << " case ISD::MEMOPERAND:\n"
Chris Lattner5216c692005-12-18 21:05:44 +00002022 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00002023 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00002024 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00002025 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00002026 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00002027 << " case ISD::TargetConstantPool:\n"
2028 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00002029 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00002030 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002031 << " case ISD::TargetGlobalTLSAddress:\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00002032 << " case ISD::TargetGlobalAddress:\n"
2033 << " case ISD::TokenFactor:\n"
2034 << " case ISD::CopyFromReg:\n"
2035 << " case ISD::CopyToReg: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002036 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00002037 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002038 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00002039 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00002040 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002041 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00002042 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00002043 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00002044 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
2045 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00002046 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00002047 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00002048
Chris Lattner602f6922006-01-04 00:25:00 +00002049 // Loop over all of the case statements, emiting a call to each method we
2050 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00002051 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00002052 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
2053 PBOI != E; ++PBOI) {
2054 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00002055 // Potentially multiple versions of select for this opcode. One for each
2056 // ValueType of the node (or its first true operand if it doesn't produce a
2057 // result.
2058 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
2059 OpcodeVTMap.find(OpName);
2060 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00002061 OS << " case " << OpName << ": {\n";
Dale Johannesen3b895cf2009-05-12 22:32:29 +00002062 // If we have only one variant and it's the default, elide the
2063 // switch. Marginally faster, and makes MSVC happier.
2064 if (OpVTs.size()==1 && OpVTs[0].empty()) {
2065 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2066 OS << " break;\n";
2067 OS << " }\n";
2068 continue;
2069 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002070 // Keep track of whether we see a pattern that has an iPtr result.
2071 bool HasPtrPattern = false;
2072 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00002073
Evan Cheng425e8c72007-09-04 20:18:28 +00002074 OS << " switch (NVT) {\n";
2075 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
2076 std::string &VTStr = OpVTs[i];
2077 if (VTStr.empty()) {
2078 HasDefaultPattern = true;
2079 continue;
2080 }
Chris Lattner717a6112006-11-14 21:50:27 +00002081
Evan Cheng425e8c72007-09-04 20:18:28 +00002082 // If this is a match on iPTR: don't emit it directly, we need special
2083 // code.
2084 if (VTStr == "_iPTR") {
2085 HasPtrPattern = true;
2086 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002087 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002088 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2089 << " return Select_" << getLegalCName(OpName)
2090 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002091 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002092 OS << " default:\n";
2093
2094 // If there is an iPTR result version of this pattern, emit it here.
2095 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002096 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002097 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2098 }
2099 if (HasDefaultPattern) {
2100 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2101 }
2102 OS << " break;\n";
2103 OS << " }\n";
2104 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002105 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002106 }
Chris Lattner81303322005-09-23 19:36:15 +00002107
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002108 OS << " } // end of big switch.\n\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002109 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2110 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2111 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002112 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002113 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002114 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002115 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002116 << " return NULL;\n"
2117 << "}\n\n";
2118
2119 OS << "void CannotYetSelect(SDValue N) DISABLE_INLINE {\n"
2120 << " cerr << \"Cannot yet select: \";\n"
2121 << " N.getNode()->dump(CurDAG);\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002122 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002123 << " abort();\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002124 << "}\n\n";
2125
2126 OS << "void CannotYetSelectIntrinsic(SDValue N) DISABLE_INLINE {\n"
2127 << " cerr << \"Cannot yet select: \";\n"
2128 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2129 << "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n"
2130 << " cerr << \"intrinsic %\"<< "
2131 << "Intrinsic::getName((Intrinsic::ID)iid);\n"
2132 << " cerr << '\\n';\n"
2133 << " abort();\n"
2134 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002135}
2136
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002137void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002138 EmitSourceFileHeader("DAG Instruction Selector for the " +
2139 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002140
Chris Lattner1f39e292005-09-14 00:09:24 +00002141 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2142 << "// *** instruction selector class. These functions are really "
2143 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002144
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002145 OS << "// Include standard, target-independent definitions and methods used\n"
2146 << "// by the instruction selector.\n";
Mike Stumpfe095f32009-05-04 18:40:41 +00002147 OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002148
Chris Lattner443e3f92008-01-05 22:54:53 +00002149 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002150 EmitPredicateFunctions(OS);
2151
Bill Wendlingf5da1332006-12-07 22:21:48 +00002152 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002153 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002154 I != E; ++I) {
2155 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2156 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002157 DOUT << "\n";
2158 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002159
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002160 // At this point, we have full information about the 'Patterns' we need to
2161 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002162 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002163 EmitInstructionSelector(OS);
2164
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002165}