blob: c97582b30b598555b3c826f79d7128814ee78e37 [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000017#include "llvm/Support/CommandLine.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000018#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000019#include "llvm/Support/MathExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000020#include "llvm/Support/Debug.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000021#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000022#include <deque>
Daniel Dunbar1a551802009-07-03 00:10:29 +000023#include <iostream>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024using namespace llvm;
25
Chris Lattner3d4ad292009-08-07 22:27:19 +000026static cl::opt<bool>
27GenDebug("gen-debug", cl::desc("Generate debug code"), cl::init(false));
David Greene8ad4c002008-10-27 21:56:29 +000028
Chris Lattnerca559d02005-09-08 21:03:01 +000029//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000030// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000031//
32
Dan Gohmaneeb3a002010-01-05 01:24:18 +000033/// getNodeName - The top level Select_* functions have an "SDNode* N"
34/// argument. When expanding the pattern-matching code, the intermediate
35/// variables have type SDValue. This function provides a uniform way to
36/// reference the underlying "SDNode *" for both cases.
37static std::string getNodeName(const std::string &S) {
38 if (S == "N") return S;
39 return S + ".getNode()";
40}
41
42/// getNodeValue - Similar to getNodeName, except it provides a uniform
43/// way to access the SDValue for both cases.
44static std::string getValueName(const std::string &S) {
45 if (S == "N") return "SDValue(N, 0)";
46 return S;
47}
48
Chris Lattner6cefb772008-01-05 22:25:12 +000049/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
50/// ComplexPattern.
51static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000052 return (N->isLeaf() &&
53 dynamic_cast<DefInit*>(N->getLeafValue()) &&
54 static_cast<DefInit*>(N->getLeafValue())->getDef()->
55 isSubClassOf("ComplexPattern"));
56}
57
Chris Lattner6cefb772008-01-05 22:25:12 +000058/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
59/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000060static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000061 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000062 if (N->isLeaf() &&
63 dynamic_cast<DefInit*>(N->getLeafValue()) &&
64 static_cast<DefInit*>(N->getLeafValue())->getDef()->
65 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000066 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
67 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000068 }
69 return NULL;
70}
71
Chris Lattner05814af2005-09-28 17:57:56 +000072/// getPatternSize - Return the 'size' of this pattern. We want to match large
73/// patterns before small ones. This is used to determine the size of a
74/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000075static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Owen Andersone50ed302009-08-10 22:56:29 +000076 assert((EEVT::isExtIntegerInVTs(P->getExtTypes()) ||
77 EEVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +000078 P->getExtTypeNum(0) == MVT::isVoid ||
79 P->getExtTypeNum(0) == MVT::Flag ||
80 P->getExtTypeNum(0) == MVT::iPTR ||
81 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000082 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000083 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000084 // If the root node is a ConstantSDNode, increases its size.
85 // e.g. (set R32:$dst, 0).
86 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000087 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000088
89 // FIXME: This is a hack to statically increase the priority of patterns
90 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
91 // Later we can allow complexity / cost for each pattern to be (optionally)
92 // specified. To get best possible pattern match we'll need to dynamically
93 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000094 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000095 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000096 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000097
98 // If this node has some predicate function that must match, it adds to the
99 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +0000100 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +0000101 ++Size;
102
Chris Lattner05814af2005-09-28 17:57:56 +0000103 // Count children in the count if they are also nodes.
104 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
105 TreePatternNode *Child = P->getChild(i);
Owen Anderson825b72b2009-08-11 20:47:22 +0000106 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +0000107 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +0000108 else if (Child->isLeaf()) {
109 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +0000110 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +0000111 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +0000112 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +0000113 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +0000114 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +0000115 }
Chris Lattner05814af2005-09-28 17:57:56 +0000116 }
117
118 return Size;
119}
120
121/// getResultPatternCost - Compute the number of instructions for this pattern.
122/// This is a temporary hack. We should really include the instruction
123/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000124static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000125 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000126 if (P->isLeaf()) return 0;
127
Evan Chengfbad7082006-02-18 02:33:09 +0000128 unsigned Cost = 0;
129 Record *Op = P->getOperator();
130 if (Op->isSubClassOf("Instruction")) {
131 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000132 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Dan Gohman533297b2009-10-29 18:10:34 +0000133 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +0000134 Cost += 10;
135 }
Chris Lattner05814af2005-09-28 17:57:56 +0000136 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000137 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000138 return Cost;
139}
140
Evan Chenge6f32032006-07-19 00:24:41 +0000141/// getResultPatternCodeSize - Compute the code size of instructions for this
142/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000143static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000144 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000145 if (P->isLeaf()) return 0;
146
147 unsigned Cost = 0;
148 Record *Op = P->getOperator();
149 if (Op->isSubClassOf("Instruction")) {
150 Cost += Op->getValueAsInt("CodeSize");
151 }
152 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000153 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000154 return Cost;
155}
156
Chris Lattner05814af2005-09-28 17:57:56 +0000157// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
158// In particular, we want to match maximal patterns first and lowest cost within
159// a particular complexity first.
160struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000161 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
162 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000163
Dan Gohman0540e172008-10-15 06:17:21 +0000164 typedef std::pair<unsigned, std::string> CodeLine;
165 typedef std::vector<CodeLine> CodeList;
166 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
167
168 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
169 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
170 const PatternToMatch *LHS = LHSPair.first;
171 const PatternToMatch *RHS = RHSPair.first;
172
Chris Lattner6cefb772008-01-05 22:25:12 +0000173 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
174 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000175 LHSSize += LHS->getAddedComplexity();
176 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000177 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
178 if (LHSSize < RHSSize) return false;
179
180 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000181 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
182 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000183 if (LHSCost < RHSCost) return true;
184 if (LHSCost > RHSCost) return false;
185
Chris Lattner6cefb772008-01-05 22:25:12 +0000186 return getResultPatternSize(LHS->getDstPattern(), CGP) <
187 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000188 }
189};
190
Jim Grosbach54f30222009-03-25 23:28:33 +0000191/// getRegisterValueType - Look up and return the ValueType of the specified
192/// register. If the register is a member of multiple register classes which
Owen Anderson825b72b2009-08-11 20:47:22 +0000193/// have different associated types, return MVT::Other.
194static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000195 bool FoundRC = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 MVT::SimpleValueType VT = MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000197 const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses();
198 std::vector<CodeGenRegisterClass>::const_iterator RC;
199 std::vector<Record*>::const_iterator Element;
200
201 for (RC = RCs.begin() ; RC != RCs.end() ; RC++) {
202 Element = find((*RC).Elements.begin(), (*RC).Elements.end(), R);
203 if (Element != (*RC).Elements.end()) {
204 if (!FoundRC) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000205 FoundRC = true;
Jim Grosbach54f30222009-03-25 23:28:33 +0000206 VT = (*RC).getValueTypeNum(0);
207 } else {
208 // In multiple RC's
209 if (VT != (*RC).getValueTypeNum(0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000210 // Types of the RC's do not agree. Return MVT::Other. The
Jim Grosbach54f30222009-03-25 23:28:33 +0000211 // target is responsible for handling this.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 return MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000213 }
214 }
215 }
216 }
217 return VT;
Evan Cheng66a48bb2005-12-01 00:18:45 +0000218}
219
Chris Lattner72fe91c2005-09-24 00:40:24 +0000220
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000221/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
222/// type information from it.
223static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000224 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000225 if (!N->isLeaf())
226 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
227 RemoveAllTypes(N->getChild(i));
228}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000229
Evan Cheng51fecc82006-01-09 18:27:06 +0000230/// NodeHasProperty - return true if TreePatternNode has the specified
231/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000232static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000233 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000234 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000235 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000236 if (CP)
237 return CP->hasProperty(Property);
238 return false;
239 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000240 Record *Operator = N->getOperator();
241 if (!Operator->isSubClassOf("SDNode")) return false;
242
Chris Lattner6cefb772008-01-05 22:25:12 +0000243 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000244}
245
Evan Cheng94b30402006-10-11 21:02:01 +0000246static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000247 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000248 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000249 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000250
251 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
252 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000253 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000254 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000255 }
256
257 return false;
258}
259
Evan Chengf9d03182008-07-03 08:39:51 +0000260static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
261 return CGP.getSDNodeInfo(Op).getEnumName();
262}
263
264static
265bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
266 bool isStore = !N->isLeaf() &&
267 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
268 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
269 return false;
270
271 bool HasChain = false;
272 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
273 TreePatternNode *Child = N->getChild(i);
274 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
275 HasChain = true;
276 break;
277 }
278 }
279 return HasChain;
280}
281
Chris Lattnerdc32f982008-01-05 22:43:57 +0000282//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000283// Node Transformation emitter implementation.
284//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000285void DAGISelEmitter::EmitNodeTransforms(raw_ostream &OS) {
Chris Lattner443e3f92008-01-05 22:54:53 +0000286 // Walk the pattern fragments, adding them to a map, which sorts them by
287 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000288 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000289 NXsByNameTy NXsByName;
290
Chris Lattnerfe718932008-01-06 01:10:31 +0000291 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000292 I != E; ++I)
293 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
294
295 OS << "\n// Node transformations.\n";
296
297 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
298 I != E; ++I) {
299 Record *SDNode = I->second.first;
300 std::string Code = I->second.second;
301
302 if (Code.empty()) continue; // Empty code? Skip it.
303
Chris Lattner200c57e2008-01-05 22:58:54 +0000304 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000305 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
306
Dan Gohman475871a2008-07-27 21:46:04 +0000307 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000308 << ") {\n";
309 if (ClassName != "SDNode")
310 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
311 OS << Code << "\n}\n";
312 }
313}
314
315//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000316// Predicate emitter implementation.
317//
318
Daniel Dunbar1a551802009-07-03 00:10:29 +0000319void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
Chris Lattnerdc32f982008-01-05 22:43:57 +0000320 OS << "\n// Predicate functions.\n";
321
322 // Walk the pattern fragments, adding them to a map, which sorts them by
323 // name.
324 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
325 PFsByNameTy PFsByName;
326
Chris Lattnerfe718932008-01-06 01:10:31 +0000327 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000328 I != E; ++I)
329 PFsByName.insert(std::make_pair(I->first->getName(), *I));
330
331
332 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
333 I != E; ++I) {
334 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
335 TreePattern *P = I->second.second;
336
337 // If there is a code init for this fragment, emit the predicate code.
338 std::string Code = PatFragRecord->getValueAsCode("Predicate");
339 if (Code.empty()) continue;
340
341 if (P->getOnlyTree()->isLeaf())
342 OS << "inline bool Predicate_" << PatFragRecord->getName()
343 << "(SDNode *N) {\n";
344 else {
345 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000346 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000347 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
348
349 OS << "inline bool Predicate_" << PatFragRecord->getName()
350 << "(SDNode *" << C2 << ") {\n";
351 if (ClassName != "SDNode")
352 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
353 }
354 OS << Code << "\n}\n";
355 }
356
357 OS << "\n\n";
358}
359
360
361//===----------------------------------------------------------------------===//
362// PatternCodeEmitter implementation.
363//
Evan Chengb915f312005-12-09 22:45:35 +0000364class PatternCodeEmitter {
365private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000366 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000367
Evan Cheng58e84a62005-12-14 22:02:59 +0000368 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000369 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000370 // Pattern cost.
371 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000372 // Instruction selector pattern.
373 TreePatternNode *Pattern;
374 // Matched instruction.
375 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000376
Evan Chengb915f312005-12-09 22:45:35 +0000377 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000378 std::map<std::string, std::string> VariableMap;
379 // Node to operator mapping
380 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000381 // Name of the folded node which produces a flag.
382 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000383 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000384 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000385 // Original input chain(s).
386 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000387 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000388
Dan Gohman69de1932008-02-06 22:27:42 +0000389 /// LSI - Load/Store information.
390 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
391 /// for each memory access. This facilitates the use of AliasAnalysis in
392 /// the backend.
393 std::vector<std::string> LSI;
394
Evan Cheng676d7312006-08-26 00:59:04 +0000395 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000396 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000397 /// tested, and if true, the match fails) [when 1], or normal code to emit
398 /// [when 0], or initialization code to emit [when 2].
399 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000400 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000401 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000402 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000403 /// TargetOpcodes - The target specific opcodes used by the resulting
404 /// instructions.
405 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000406 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000407 /// OutputIsVariadic - Records whether the instruction output pattern uses
408 /// variable_ops. This requires that the Emit function be passed an
409 /// additional argument to indicate where the input varargs operands
410 /// begin.
411 bool &OutputIsVariadic;
412 /// NumInputRootOps - Records the number of operands the root node of the
413 /// input pattern has. This information is used in the generated code to
414 /// pass to Emit functions when variable_ops processing is needed.
415 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000416
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000417 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000418 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000419 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000420 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000421
422 void emitCheck(const std::string &S) {
423 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000424 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000425 }
426 void emitCode(const std::string &S) {
427 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000428 GeneratedCode.push_back(std::make_pair(0, S));
429 }
430 void emitInit(const std::string &S) {
431 if (!S.empty())
432 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000433 }
Evan Chengf5493192006-08-26 01:02:19 +0000434 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000435 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000436 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000437 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000438 void emitOpcode(const std::string &Opc) {
439 TargetOpcodes.push_back(Opc);
440 OpcNo++;
441 }
Evan Chengf8729402006-07-16 06:12:52 +0000442 void emitVT(const std::string &VT) {
443 TargetVTs.push_back(VT);
444 VTNo++;
445 }
Evan Chengb915f312005-12-09 22:45:35 +0000446public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000447 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000448 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000449 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000450 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000451 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000452 std::vector<std::string> &tv,
453 bool &oiv,
454 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000455 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000456 GeneratedCode(gc), GeneratedDecl(gd),
457 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000458 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000459 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000460
461 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
462 /// if the match fails. At this point, we already know that the opcode for N
463 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000464 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
465 const std::string &RootName, const std::string &ChainSuffix,
466 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000467
468 // Save loads/stores matched by a pattern.
469 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000470 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000471 LSI.push_back(getNodeName(RootName));
Dan Gohman69de1932008-02-06 22:27:42 +0000472 }
473
Evan Chenge41bf822006-02-05 06:43:12 +0000474 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000475 // Emit instruction predicates. Each predicate is just a string for now.
476 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000477 // Record input varargs info.
478 NumInputRootOps = N->getNumChildren();
479
Evan Chengf9d03182008-07-03 08:39:51 +0000480 if (DisablePatternForFastISel(N, CGP))
Bill Wendling98a366d2009-04-29 23:29:43 +0000481 emitCheck("OptLevel != CodeGenOpt::None");
Evan Chengf9d03182008-07-03 08:39:51 +0000482
Chris Lattner8a0604b2006-01-28 20:31:24 +0000483 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000484 }
485
Evan Chengb915f312005-12-09 22:45:35 +0000486 if (N->isLeaf()) {
487 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000488 emitCheck("cast<ConstantSDNode>(" + getNodeName(RootName) +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000489 ")->getSExtValue() == INT64_C(" +
490 itostr(II->getValue()) + ")");
Evan Chengb915f312005-12-09 22:45:35 +0000491 return;
492 } else if (!NodeIsComplexPattern(N)) {
493 assert(0 && "Cannot match this as a leaf value!");
494 abort();
495 }
496 }
497
Chris Lattner488580c2006-01-28 19:06:51 +0000498 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000499 // we already saw this in the pattern, emit code to verify dagness.
500 if (!N->getName().empty()) {
501 std::string &VarMapEntry = VariableMap[N->getName()];
502 if (VarMapEntry.empty()) {
503 VarMapEntry = RootName;
504 } else {
505 // If we get here, this is a second reference to a specific name. Since
506 // we already have checked that the first reference is valid, we don't
507 // have to recursively match it, just check that it's the same as the
508 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000509 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000510 return;
511 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000512
513 if (!N->isLeaf())
514 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000515 }
516
517
518 // Emit code to load the child nodes and match their contents recursively.
519 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000520 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
521 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000522 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000523 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000524 if (NodeHasChain)
525 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000526 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000527 // Multiple uses of actual result?
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000528 emitCheck(getValueName(RootName) + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000529 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000530 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000531 // If the immediate use can somehow reach this node through another
532 // path, then can't fold it either or it will create a cycle.
533 // e.g. In the following diagram, XX can reach ld through YY. If
534 // ld is folded into XX, then YY is both a predecessor and a successor
535 // of XX.
536 //
537 // [ld]
538 // ^ ^
539 // | |
540 // / \---
541 // / [YY]
542 // | ^
543 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000544 bool NeedCheck = P != Pattern;
545 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000546 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000547 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000548 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
549 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
550 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000551 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000552 PInfo.hasProperty(SDNPHasChain) ||
553 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000554 PInfo.hasProperty(SDNPOptInFlag);
555 }
556
557 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000558 std::string ParentName(RootName.begin(), RootName.end()-1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000559 emitCheck("IsLegalAndProfitableToFold(" + getNodeName(RootName) +
560 ", " + getNodeName(ParentName) + ", N)");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000561 }
Evan Chenge41bf822006-02-05 06:43:12 +0000562 }
Evan Chengb915f312005-12-09 22:45:35 +0000563 }
Evan Chenge41bf822006-02-05 06:43:12 +0000564
Evan Chengc15d18c2006-01-27 22:13:45 +0000565 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000566 if (FoundChain) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000567 emitCheck("(" + ChainName + ".getNode() == " +
568 getNodeName(RootName) + " || "
Gabor Greifba36cb52008-08-28 21:40:38 +0000569 "IsChainCompatible(" + ChainName + ".getNode(), " +
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000570 getNodeName(RootName) + "))");
571 OrigChains.push_back(std::make_pair(ChainName,
572 getValueName(RootName)));
Evan Cheng4326ef52006-10-12 02:08:53 +0000573 } else
Evan Chenge6389932006-07-21 22:19:51 +0000574 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000575 ChainName = "Chain" + ChainSuffix;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000576 emitInit("SDValue " + ChainName + " = " + getNodeName(RootName) +
577 "->getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000578 }
Evan Chengb915f312005-12-09 22:45:35 +0000579 }
580
Evan Cheng54597732006-01-26 00:22:25 +0000581 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000582 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000583 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000584 // FIXME: If the optional incoming flag does not exist. Then it is ok to
585 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000586 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000587 (PatternHasProperty(N, SDNPInFlag, CGP) ||
588 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
589 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000590 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000591 // Multiple uses of actual result?
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000592 emitCheck(getValueName(RootName) + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000593 }
594 }
595
Dan Gohman0540e172008-10-15 06:17:21 +0000596 // If there are node predicates for this, emit the calls.
597 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000598 emitCheck(N->getPredicateFns()[i] + "(" + getNodeName(RootName) + ")");
Evan Chengd3eea902006-10-09 21:02:17 +0000599
Chris Lattner39e73f72006-10-11 04:05:55 +0000600 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
601 // a constant without a predicate fn that has more that one bit set, handle
602 // this as a special case. This is usually for targets that have special
603 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
604 // handling stuff). Using these instructions is often far more efficient
605 // than materializing the constant. Unfortunately, both the instcombiner
606 // and the dag combiner can often infer that bits are dead, and thus drop
607 // them from the mask in the dag. For example, it might turn 'AND X, 255'
608 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
609 // to handle this.
610 if (!N->isLeaf() &&
611 (N->getOperator()->getName() == "and" ||
612 N->getOperator()->getName() == "or") &&
613 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000614 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000615 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
616 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000617 emitInit("SDValue " + RootName + "0" + " = " +
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000618 getNodeName(RootName) + "->getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000619 emitInit("SDValue " + RootName + "1" + " = " +
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000620 getNodeName(RootName) + "->getOperand(" + utostr(1) + ");");
Chris Lattner39e73f72006-10-11 04:05:55 +0000621
Dan Gohman0b53d982008-12-19 18:13:39 +0000622 unsigned NTmp = TmpNo++;
623 emitCode("ConstantSDNode *Tmp" + utostr(NTmp) +
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000624 " = dyn_cast<ConstantSDNode>(" +
625 getNodeName(RootName + "1") + ");");
Dan Gohman0b53d982008-12-19 18:13:39 +0000626 emitCheck("Tmp" + utostr(NTmp));
Chris Lattner39e73f72006-10-11 04:05:55 +0000627 const char *MaskPredicate = N->getOperator()->getName() == "or"
628 ? "CheckOrMask(" : "CheckAndMask(";
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000629 emitCheck(MaskPredicate + getValueName(RootName + "0") +
630 ", Tmp" + utostr(NTmp) +
Dan Gohman0b53d982008-12-19 18:13:39 +0000631 ", INT64_C(" + itostr(II->getValue()) + "))");
Chris Lattner39e73f72006-10-11 04:05:55 +0000632
Dan Gohman537ab902010-01-04 20:31:55 +0000633 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0),
Chris Lattner39e73f72006-10-11 04:05:55 +0000634 ChainSuffix + utostr(0), FoundChain);
635 return;
636 }
637 }
638 }
639
Evan Chengb915f312005-12-09 22:45:35 +0000640 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000641 emitInit("SDValue " + getValueName(RootName + utostr(OpNo)) + " = " +
642 getNodeName(RootName) + "->getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000643
Dan Gohman537ab902010-01-04 20:31:55 +0000644 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo),
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000645 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000646 }
647
Evan Cheng676d7312006-08-26 00:59:04 +0000648 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000649 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000650 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000651 std::string Fn = CP->getSelectFunc();
652 unsigned NumOps = CP->getNumOperands();
653 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000654 emitDecl("CPTmp" + RootName + "_" + utostr(i));
655 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000656 }
Evan Cheng94b30402006-10-11 21:02:01 +0000657 if (CP->hasProperty(SDNPHasChain)) {
658 emitDecl("CPInChain");
659 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000660 emitCode("SDValue CPInChain;");
661 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000662 }
Evan Cheng676d7312006-08-26 00:59:04 +0000663
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000664 std::string Code = Fn + "(" +
665 getNodeName(RootName) + ", " +
666 getValueName(RootName);
Evan Cheng676d7312006-08-26 00:59:04 +0000667 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000668 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000669 if (CP->hasProperty(SDNPHasChain)) {
670 ChainName = "Chain" + ChainSuffix;
671 Code += ", CPInChain, Chain" + ChainSuffix;
672 }
Evan Cheng676d7312006-08-26 00:59:04 +0000673 emitCheck(Code + ")");
674 }
Evan Chengb915f312005-12-09 22:45:35 +0000675 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000676
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000677 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000678 const std::string &RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000679 const std::string &ChainSuffix, bool &FoundChain) {
680 if (!Child->isLeaf()) {
681 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000682 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000683 emitCheck(getNodeName(RootName) + "->getOpcode() == " +
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000684 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000685 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000686 bool HasChain = false;
687 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
688 HasChain = true;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000689 FoldedChains.push_back(std::make_pair(getValueName(RootName),
690 CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000691 }
Dale Johannesen874ae252009-06-02 03:12:52 +0000692 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
Evan Chenga58891f2008-02-05 22:50:29 +0000693 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
694 "Pattern folded multiple nodes which produce flags?");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000695 FoldedFlag = std::make_pair(getValueName(RootName),
Evan Chenga58891f2008-02-05 22:50:29 +0000696 CInfo.getNumResults() + (unsigned)HasChain);
697 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000698 } else {
699 // If this child has a name associated with it, capture it in VarMap. If
700 // we already saw this in the pattern, emit code to verify dagness.
701 if (!Child->getName().empty()) {
702 std::string &VarMapEntry = VariableMap[Child->getName()];
703 if (VarMapEntry.empty()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000704 VarMapEntry = getValueName(RootName);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000705 } else {
706 // If we get here, this is a second reference to a specific name.
707 // Since we already have checked that the first reference is valid,
708 // we don't have to recursively match it, just check that it's the
709 // same as the previously named thing.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000710 emitCheck(VarMapEntry + " == " + getValueName(RootName));
711 Duplicates.insert(getValueName(RootName));
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000712 return;
713 }
714 }
715
716 // Handle leaves of various types.
717 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
718 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000719 if (LeafRec->isSubClassOf("RegisterClass") ||
Chris Lattnera938ac62009-07-29 20:43:05 +0000720 LeafRec->isSubClassOf("PointerLikeRegClass")) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000721 // Handle register references. Nothing to do here.
722 } else if (LeafRec->isSubClassOf("Register")) {
723 // Handle register references.
724 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
725 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000726 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000727 std::string Fn = CP->getSelectFunc();
728 unsigned NumOps = CP->getNumOperands();
729 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000730 emitDecl("CPTmp" + RootName + "_" + utostr(i));
731 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000732 }
Evan Cheng94b30402006-10-11 21:02:01 +0000733 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000734 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000735 FoldedChains.push_back(std::make_pair("CPInChain",
736 PInfo.getNumResults()));
737 ChainName = "Chain" + ChainSuffix;
738 emitDecl("CPInChain");
739 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000740 emitCode("SDValue CPInChain;");
741 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000742 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000743
Dan Gohman537ab902010-01-04 20:31:55 +0000744 std::string Code = Fn + "(N, ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000745 if (CP->hasProperty(SDNPHasChain)) {
746 std::string ParentName(RootName.begin(), RootName.end()-1);
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000747 Code += getValueName(ParentName) + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000748 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000749 Code += getValueName(RootName);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000750 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000751 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000752 if (CP->hasProperty(SDNPHasChain))
753 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000754 emitCheck(Code + ")");
755 } else if (LeafRec->getName() == "srcvalue") {
756 // Place holder for SRCVALUE nodes. Nothing to do here.
757 } else if (LeafRec->isSubClassOf("ValueType")) {
758 // Make sure this is the specified value type.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000759 emitCheck("cast<VTSDNode>(" + getNodeName(RootName) +
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 ")->getVT() == MVT::" + LeafRec->getName());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000761 } else if (LeafRec->isSubClassOf("CondCode")) {
762 // Make sure this is the specified cond code.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000763 emitCheck("cast<CondCodeSDNode>(" + getNodeName(RootName) +
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000764 ")->get() == ISD::" + LeafRec->getName());
765 } else {
766#ifndef NDEBUG
767 Child->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +0000768 errs() << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000769#endif
770 assert(0 && "Unknown leaf type!");
771 }
772
Dan Gohman0540e172008-10-15 06:17:21 +0000773 // If there are node predicates for this, emit the calls.
774 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000775 emitCheck(Child->getPredicateFns()[i] + "(" + getNodeName(RootName) +
776 ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000777 } else if (IntInit *II =
778 dynamic_cast<IntInit*>(Child->getLeafValue())) {
Dan Gohman0b53d982008-12-19 18:13:39 +0000779 unsigned NTmp = TmpNo++;
780 emitCode("ConstantSDNode *Tmp"+ utostr(NTmp) +
781 " = dyn_cast<ConstantSDNode>("+
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000782 getNodeName(RootName) + ");");
Dan Gohman0b53d982008-12-19 18:13:39 +0000783 emitCheck("Tmp" + utostr(NTmp));
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000784 unsigned CTmp = TmpNo++;
Dan Gohman0b53d982008-12-19 18:13:39 +0000785 emitCode("int64_t CN"+ utostr(CTmp) +
786 " = Tmp" + utostr(NTmp) + "->getSExtValue();");
Dan Gohman63f97202008-10-17 01:33:43 +0000787 emitCheck("CN" + utostr(CTmp) + " == "
788 "INT64_C(" +itostr(II->getValue()) + ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000789 } else {
790#ifndef NDEBUG
791 Child->dump();
792#endif
793 assert(0 && "Unknown leaf type!");
794 }
795 }
796 }
Evan Chengb915f312005-12-09 22:45:35 +0000797
798 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
799 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000800 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000801 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000802 bool InFlagDecled, bool ResNodeDecled,
803 bool LikeLeaf = false, bool isRoot = false) {
Dan Gohman602b0c82009-09-25 18:54:59 +0000804 // List of arguments of getMachineNode() or SelectNodeTo().
Evan Cheng676d7312006-08-26 00:59:04 +0000805 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000806 // This is something selected from the pattern we matched.
807 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000808 const std::string &VarName = N->getName();
809 std::string Val = VariableMap[VarName];
810 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000811 if (Val.empty()) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000812 errs() << "Variable '" << VarName << " referenced but not defined "
Bill Wendling27926af2008-02-26 10:45:29 +0000813 << "and not caught earlier!\n";
814 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000815 }
Evan Chengb915f312005-12-09 22:45:35 +0000816 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
817 // Already selected this operand, just return the tmpval.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000818 NodeOps.push_back(getValueName(Val));
Evan Cheng676d7312006-08-26 00:59:04 +0000819 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000820 }
821
822 const ComplexPattern *CP;
823 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000824 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000825 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000826 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000827 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000828 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000829 default:
Daniel Dunbar1a551802009-07-03 00:10:29 +0000830 errs() << "Cannot handle " << getEnumName(N->getTypeNum(0))
Chris Lattnerd8a17282007-01-17 07:45:12 +0000831 << " type as an immediate constant. Aborting\n";
832 abort();
Owen Anderson825b72b2009-08-11 20:47:22 +0000833 case MVT::i1: CastType = "bool"; break;
834 case MVT::i8: CastType = "unsigned char"; break;
835 case MVT::i16: CastType = "unsigned short"; break;
836 case MVT::i32: CastType = "unsigned"; break;
837 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000838 }
Dan Gohman475871a2008-07-27 21:46:04 +0000839 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000840 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000841 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000842 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000843 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
844 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000845 Val = TmpVar;
846 ModifiedVal = true;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000847 NodeOps.push_back(getValueName(Val));
Nate Begemane1795842008-02-14 08:57:00 +0000848 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
849 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
850 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000851 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000852 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
853 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
854 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000855 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
856 // value if used multiple times by this pattern result.
857 Val = TmpVar;
858 ModifiedVal = true;
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000859 NodeOps.push_back(getValueName(Val));
Evan Chengbb48e332006-01-12 07:54:57 +0000860 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000861 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000862 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000863 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000864 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000865 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000866 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000867 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000868 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000869 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
870 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000871 Val = TmpVar;
872 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000873 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000874 NodeOps.push_back(getValueName(Val));
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000875 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
876 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000877 Record *Op = OperatorMap[N->getName()];
878 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000879 if (Op && (Op->getName() == "globaladdr" ||
880 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000881 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000882 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000883 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000884 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000885 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000886 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
887 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000888 Val = TmpVar;
889 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000890 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000891 NodeOps.push_back(getValueName(Val));
Scott Michel6be48d42008-01-29 02:29:31 +0000892 } else if (!N->isLeaf()
893 && (N->getOperator()->getName() == "texternalsym"
894 || N->getOperator()->getName() == "tconstpool")) {
895 // Do not rewrite the variable name, since we don't generate a new
896 // temporary.
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000897 NodeOps.push_back(getValueName(Val));
Chris Lattner6cefb772008-01-05 22:25:12 +0000898 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000899 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000900 NodeOps.push_back(getValueName("CPTmp" + Val + "_" + utostr(i)));
Evan Chengb0793f92006-05-25 00:21:44 +0000901 }
Evan Chengb915f312005-12-09 22:45:35 +0000902 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000903 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000904 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000905 if (!LikeLeaf) {
Chris Lattner706d2d32006-08-09 16:44:44 +0000906 if (isRoot && N->isLeaf()) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000907 emitCode("ReplaceUses(SDValue(N, 0), " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000908 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000909 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000910 }
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000911 NodeOps.push_back(getValueName(Val));
Evan Chengb915f312005-12-09 22:45:35 +0000912 }
Scott Michel6be48d42008-01-29 02:29:31 +0000913
914 if (ModifiedVal) {
915 VariableMap[VarName] = Val;
916 }
Evan Cheng676d7312006-08-26 00:59:04 +0000917 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000918 }
Evan Chengb915f312005-12-09 22:45:35 +0000919 if (N->isLeaf()) {
920 // If this is an explicit register reference, handle it.
921 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
922 unsigned ResNo = TmpNo++;
923 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000924 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000925 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000926 getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000927 NodeOps.push_back(getValueName("Tmp" + utostr(ResNo)));
Evan Cheng676d7312006-08-26 00:59:04 +0000928 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000929 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000930 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000931 " = CurDAG->getRegister(0, " +
932 getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000933 NodeOps.push_back(getValueName("Tmp" + utostr(ResNo)));
Evan Cheng7774be42007-07-05 07:19:45 +0000934 return NodeOps;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000935 } else if (DI->getDef()->isSubClassOf("RegisterClass")) {
936 // Handle a reference to a register class. This is used
937 // in COPY_TO_SUBREG instructions.
938 emitCode("SDValue Tmp" + utostr(ResNo) +
939 " = CurDAG->getTargetConstant(" +
940 getQualifiedName(DI->getDef()) + "RegClassID, " +
Owen Anderson825b72b2009-08-11 20:47:22 +0000941 "MVT::i32);");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000942 NodeOps.push_back(getValueName("Tmp" + utostr(ResNo)));
Dan Gohmanf8c73942009-04-13 15:38:05 +0000943 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000944 }
945 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
946 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000947 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000948 emitCode("SDValue Tmp" + utostr(ResNo) +
Daniel Dunbarbd17a292009-07-30 18:18:54 +0000949 " = CurDAG->getTargetConstant(0x" +
950 utohexstr((uint64_t) II->getValue()) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000951 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000952 NodeOps.push_back(getValueName("Tmp" + utostr(ResNo)));
Evan Cheng676d7312006-08-26 00:59:04 +0000953 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000954 }
955
Jim Laskey16d42c62006-07-11 18:25:13 +0000956#ifndef NDEBUG
957 N->dump();
958#endif
Evan Chengb915f312005-12-09 22:45:35 +0000959 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000960 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000961 }
962
963 Record *Op = N->getOperator();
964 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000965 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000966 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000967 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000968 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000969 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000970 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000971 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
972 : (InstPat ? InstPat->getTree(0) : NULL);
Dan Gohmanfebf71d2009-01-16 21:30:55 +0000973 if (InstPatNode && !InstPatNode->isLeaf() &&
974 InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000975 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000976 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000977 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000978 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000979 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000980 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000981 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000982 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000983 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000984 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000985 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000986 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000987 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000988 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000989 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000990 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000991 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000992 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000993
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000994 // Record output varargs info.
995 OutputIsVariadic = IsVariadic;
996
Evan Chengfceb57a2006-07-15 08:45:20 +0000997 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000998 emitCode("bool HasInFlag = "
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000999 "(N->getOperand(N->getNumOperands()-1).getValueType() == "
1000 "MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +00001001 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001002 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +00001003 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +00001004
Evan Cheng823b7522006-01-19 21:57:10 +00001005 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001006 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +00001007 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001008 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
1009 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001010 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +00001011 }
1012
Evan Cheng4326ef52006-10-12 02:08:53 +00001013 if (OrigChains.size() > 0) {
1014 // The original input chain is being ignored. If it is not just
1015 // pointing to the op that's being folded, we should create a
1016 // TokenFactor with it and the chain of the folded op as the new chain.
1017 // We could potentially be doing multiple levels of folding, in that
1018 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001019 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +00001020 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
1022 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +00001023 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
1024 emitCode("}");
1025 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001026 emitCode("InChains.push_back(" + ChainName + ");");
Dale Johannesened2eee62009-02-06 01:31:28 +00001027 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, "
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001028 "N->getDebugLoc(), MVT::Other, "
Evan Cheng4326ef52006-10-12 02:08:53 +00001029 "&InChains[0], InChains.size());");
David Greene8ad4c002008-10-27 21:56:29 +00001030 if (GenDebug) {
1031 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
1032 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
1033 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001034 }
1035
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001036 // Loop over all of the operands of the instruction pattern, emitting code
1037 // to fill them all in. The node 'N' usually has number children equal to
1038 // the number of input operands of the instruction. However, in cases
1039 // where there are predicate operands for an instruction, we need to fill
1040 // in the 'execute always' values. Match up the node operands to the
1041 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +00001042 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001043 for (unsigned ChildNo = 0, InstOpNo = NumResults;
1044 InstOpNo != II.OperandList.size(); ++InstOpNo) {
1045 std::vector<std::string> Ops;
1046
Dan Gohmand35121a2008-05-29 19:57:41 +00001047 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +00001048 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001049 if ((OperandNode->isSubClassOf("PredicateOperand") ||
1050 OperandNode->isSubClassOf("OptionalDefOperand")) &&
1051 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +00001052 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +00001053 // 'default ops' operands.
1054 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001055 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001056 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001057 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001058 InFlagDecled, ResNodeDecled);
1059 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1060 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001061 } else {
1062 // Otherwise this is a normal operand or a predicate operand without
1063 // 'execute always'; emit it.
1064 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1065 InFlagDecled, ResNodeDecled);
1066 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1067 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001068 }
Evan Chengb915f312005-12-09 22:45:35 +00001069 }
1070
Evan Chengb915f312005-12-09 22:45:35 +00001071 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001072 bool ChainEmitted = NodeHasChain;
Dale Johannesen874ae252009-06-02 03:12:52 +00001073 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001074 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1075 InFlagDecled, ResNodeDecled, true);
Dale Johannesen874ae252009-06-02 03:12:52 +00001076 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001077 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001078 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001079 InFlagDecled = true;
1080 }
Evan Chengf037ca62006-08-27 08:11:28 +00001081 if (NodeHasOptInFlag) {
1082 emitCode("if (HasInFlag) {");
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001083 emitCode(" InFlag = N->getOperand(N->getNumOperands()-1);");
Evan Chengf037ca62006-08-27 08:11:28 +00001084 emitCode("}");
1085 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001086 }
Evan Chengb915f312005-12-09 22:45:35 +00001087
Evan Chengb915f312005-12-09 22:45:35 +00001088 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001089
Dan Gohman95d11092008-07-07 21:00:17 +00001090 unsigned OpsNo = OpcNo;
1091 std::string CodePrefix;
1092 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1093 std::deque<std::string> After;
1094 std::string NodeName;
1095 if (!isRoot) {
1096 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001097 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001098 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001099 NodeName = "ResNode";
1100 if (!ResNodeDecled) {
1101 CodePrefix = "SDNode *" + NodeName + " = ";
1102 ResNodeDecled = true;
1103 } else
1104 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001105 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001106
Dan Gohman95d11092008-07-07 21:00:17 +00001107 std::string Code = "Opc" + utostr(OpcNo);
1108
Bill Wendling6e1bb382009-01-29 05:27:31 +00001109 if (!isRoot || (InputHasChain && !NodeHasChain))
Dan Gohman602b0c82009-09-25 18:54:59 +00001110 // For call to "getMachineNode()".
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001111 Code += ", N->getDebugLoc()";
Bill Wendling6e1bb382009-01-29 05:27:31 +00001112
Dan Gohman95d11092008-07-07 21:00:17 +00001113 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1114
1115 // Output order: results, chain, flags
1116 // Result types.
Owen Anderson825b72b2009-08-11 20:47:22 +00001117 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
Dan Gohman95d11092008-07-07 21:00:17 +00001118 Code += ", VT" + utostr(VTNo);
1119 emitVT(getEnumName(N->getTypeNum(0)));
1120 }
1121 // Add types for implicit results in physical registers, scheduler will
1122 // care of adding copyfromreg nodes.
1123 for (unsigned i = 0; i < NumDstRegs; i++) {
1124 Record *RR = DstRegs[i];
1125 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001126 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Dan Gohman95d11092008-07-07 21:00:17 +00001127 Code += ", " + getEnumName(RVT);
1128 }
1129 }
1130 if (NodeHasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00001131 Code += ", MVT::Other";
Dale Johannesen874ae252009-06-02 03:12:52 +00001132 if (NodeHasOutFlag)
Owen Anderson825b72b2009-08-11 20:47:22 +00001133 Code += ", MVT::Flag";
Dan Gohman95d11092008-07-07 21:00:17 +00001134
1135 // Inputs.
1136 if (IsVariadic) {
1137 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1138 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1139 AllOps.clear();
1140
1141 // Figure out whether any operands at the end of the op list are not
1142 // part of the variable section.
1143 std::string EndAdjust;
1144 if (NodeHasInFlag || HasImpInputs)
1145 EndAdjust = "-1"; // Always has one flag.
1146 else if (NodeHasOptInFlag)
1147 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1148
1149 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001150 ", e = N->getNumOperands()" + EndAdjust + "; i != e; ++i) {");
Dan Gohman95d11092008-07-07 21:00:17 +00001151
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001152 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N->getOperand(i));");
Dan Gohman95d11092008-07-07 21:00:17 +00001153 emitCode("}");
1154 }
1155
Dan Gohmanc76909a2009-09-25 20:36:54 +00001156 // Populate MemRefs with entries for each memory accesses covered by
Dan Gohman95d11092008-07-07 21:00:17 +00001157 // this pattern.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001158 if (isRoot && !LSI.empty()) {
1159 std::string MemRefs = "MemRefs" + utostr(OpsNo);
1160 emitCode("MachineSDNode::mmo_iterator " + MemRefs + " = "
1161 "MF->allocateMemRefsArray(" + utostr(LSI.size()) + ");");
1162 for (unsigned i = 0, e = LSI.size(); i != e; ++i)
1163 emitCode(MemRefs + "[" + utostr(i) + "] = "
1164 "cast<MemSDNode>(" + LSI[i] + ")->getMemOperand();");
1165 After.push_back("cast<MachineSDNode>(ResNode)->setMemRefs(" +
1166 MemRefs + ", " + MemRefs + " + " + utostr(LSI.size()) +
1167 ");");
Dan Gohman95d11092008-07-07 21:00:17 +00001168 }
1169
1170 if (NodeHasChain) {
1171 if (IsVariadic)
1172 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1173 else
1174 AllOps.push_back(ChainName);
1175 }
1176
1177 if (IsVariadic) {
1178 if (NodeHasInFlag || HasImpInputs)
1179 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1180 else if (NodeHasOptInFlag) {
1181 emitCode("if (HasInFlag)");
1182 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1183 }
1184 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1185 ".size()";
Dale Johannesen874ae252009-06-02 03:12:52 +00001186 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Dan Gohman95d11092008-07-07 21:00:17 +00001187 AllOps.push_back("InFlag");
1188
1189 unsigned NumOps = AllOps.size();
1190 if (NumOps) {
1191 if (!NodeHasOptInFlag && NumOps < 4) {
1192 for (unsigned i = 0; i != NumOps; ++i)
1193 Code += ", " + AllOps[i];
1194 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001195 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001196 for (unsigned i = 0; i != NumOps; ++i) {
1197 OpsCode += AllOps[i];
1198 if (i != NumOps-1)
1199 OpsCode += ", ";
1200 }
1201 emitCode(OpsCode + " };");
1202 Code += ", Ops" + utostr(OpsNo) + ", ";
1203 if (NodeHasOptInFlag) {
1204 Code += "HasInFlag ? ";
1205 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1206 } else
1207 Code += utostr(NumOps);
1208 }
1209 }
1210
1211 if (!isRoot)
1212 Code += "), 0";
1213
Dan Gohmane8be6c62008-07-17 19:10:17 +00001214 std::vector<std::string> ReplaceFroms;
1215 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001216 if (!isRoot) {
1217 NodeOps.push_back("Tmp" + utostr(ResNo));
1218 } else {
1219
Dale Johannesen874ae252009-06-02 03:12:52 +00001220 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001221 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001222 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001223 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1224 ");");
1225 InFlagDecled = true;
1226 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001227 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001228 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1229 ");");
1230 }
1231
Dan Gohman1eb49a02009-01-05 19:31:28 +00001232 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
1233 ReplaceFroms.push_back("SDValue(" +
1234 FoldedChains[j].first + ".getNode(), " +
1235 utostr(FoldedChains[j].second) +
1236 ")");
1237 ReplaceTos.push_back("SDValue(ResNode, " +
1238 utostr(NumResults+NumDstRegs) + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001239 }
1240
Dale Johannesen874ae252009-06-02 03:12:52 +00001241 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001242 if (FoldedFlag.first != "") {
Dale Johannesen874ae252009-06-02 03:12:52 +00001243 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".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 Johannesen874ae252009-06-02 03:12:52 +00001247 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001248 ReplaceFroms.push_back("SDValue(N, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001249 utostr(NumPatResults + (unsigned)InputHasChain)
1250 + ")");
1251 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001252 }
Dan Gohman95d11092008-07-07 21:00:17 +00001253 }
1254
Dan Gohmane8be6c62008-07-17 19:10:17 +00001255 if (!ReplaceFroms.empty() && InputHasChain) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001256 ReplaceFroms.push_back("SDValue(N, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001257 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001258 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001259 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001260 ChainAssignmentNeeded |= NodeHasChain;
1261 }
1262
1263 // User does not expect the instruction would produce a chain!
Dale Johannesen874ae252009-06-02 03:12:52 +00001264 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001265 ;
1266 } else if (InputHasChain && !NodeHasChain) {
1267 // One of the inner node produces a chain.
Dan Gohmanba7a6622010-01-04 20:36:57 +00001268 assert(!NodeHasOutFlag && "Node has flag but not chain!");
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001269 ReplaceFroms.push_back("SDValue(N, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001270 utostr(NumPatResults) + ")");
1271 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001272 }
1273 }
1274
1275 if (ChainAssignmentNeeded) {
1276 // Remember which op produces the chain.
1277 std::string ChainAssign;
1278 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001279 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001280 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001281 else
Dan Gohman475871a2008-07-27 21:46:04 +00001282 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001283 ", " + utostr(NumResults+NumDstRegs) + ");";
1284
1285 After.push_front(ChainAssign);
1286 }
1287
Dan Gohmane8be6c62008-07-17 19:10:17 +00001288 if (ReplaceFroms.size() == 1) {
1289 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1290 ReplaceTos[0] + ");");
1291 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001292 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001293 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1294 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1295 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001296 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001297 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1298 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1299 After.push_back("};");
1300 After.push_back("ReplaceUses(Froms, Tos, " +
1301 itostr(ReplaceFroms.size()) + ");");
1302 }
1303
1304 // We prefer to use SelectNodeTo since it avoids allocation when
1305 // possible and it avoids CSE map recalculation for the node's
1306 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001307 //
Dan Gohman2929e112009-12-19 01:46:09 +00001308 // We also don't use SelectNodeTo if the pattern replacement is being
1309 // used to jettison a chain result, since morphing the node in place
Dan Gohmane8be6c62008-07-17 19:10:17 +00001310 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001311 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001312 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001313 Code = "CurDAG->getMachineNode(" + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001314 } else {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001315 Code = "CurDAG->SelectNodeTo(N, " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001316 }
1317 if (isRoot) {
1318 if (After.empty())
1319 CodePrefix = "return ";
1320 else
1321 After.push_back("return ResNode;");
1322 }
1323
1324 emitCode(CodePrefix + Code + ");");
David Greene8ad4c002008-10-27 21:56:29 +00001325
1326 if (GenDebug) {
1327 if (!isRoot) {
1328 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
1329 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
1330 }
1331 else {
1332 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
1333 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
1334 }
1335 }
1336
Dan Gohman95d11092008-07-07 21:00:17 +00001337 for (unsigned i = 0, e = After.size(); i != e; ++i)
1338 emitCode(After[i]);
1339
Evan Cheng676d7312006-08-26 00:59:04 +00001340 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001341 }
1342 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001343 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001344 // PatLeaf node - the operand may or may not be a leaf node. But it should
1345 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001346 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001347 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001348 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001349 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001350 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001351 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001352 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001353 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001354 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001355 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001356 }
Dan Gohman0540e172008-10-15 06:17:21 +00001357
1358 N->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001359 errs() << "\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001360 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001361 }
1362
Chris Lattner488580c2006-01-28 19:06:51 +00001363 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1364 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001365 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1366 /// for, this returns true otherwise false if Pat has all types.
1367 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001368 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001369 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001370 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001371 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001372 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001373 // The top level node type is checked outside of the select function.
1374 if (!isRoot)
Anton Korobeynikovc2fd9192009-11-08 12:14:54 +00001375 emitCheck(Prefix + ".getValueType() == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001376 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001377 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001378 }
1379
Evan Cheng51fecc82006-01-09 18:27:06 +00001380 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001381 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001382 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1383 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1384 Prefix + utostr(OpNo)))
1385 return true;
1386 return false;
1387 }
1388
1389private:
Evan Cheng54597732006-01-26 00:22:25 +00001390 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001391 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001392 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001393 bool &ChainEmitted, bool &InFlagDecled,
1394 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001395 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001396 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001397 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1398 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001399 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1400 TreePatternNode *Child = N->getChild(i);
1401 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001402 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1403 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001404 } else {
1405 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001406 if (!Child->getName().empty()) {
1407 std::string Name = RootName + utostr(OpNo);
1408 if (Duplicates.find(Name) != Duplicates.end())
1409 // A duplicate! Do not emit a copy for this node.
1410 continue;
1411 }
1412
Evan Chengb915f312005-12-09 22:45:35 +00001413 Record *RR = DI->getDef();
1414 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001415 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
1416 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001417 if (!InFlagDecled) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001418 emitCode("SDValue InFlag = " +
1419 getValueName(RootName + utostr(OpNo)) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001420 InFlagDecled = true;
1421 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001422 emitCode("InFlag = " +
1423 getValueName(RootName + utostr(OpNo)) + ";");
Evan Chengb2c6d492006-01-11 22:16:13 +00001424 } else {
1425 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001426 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001427 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001428 ChainEmitted = true;
1429 }
Evan Cheng676d7312006-08-26 00:59:04 +00001430 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001431 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001432 InFlagDecled = true;
1433 }
Dale Johannesen874ae252009-06-02 03:12:52 +00001434 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1435 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001436 ", " + getNodeName(RootName) + "->getDebugLoc()" +
Chris Lattner6cefb772008-01-05 22:25:12 +00001437 ", " + getQualifiedName(RR) +
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001438 ", " + getValueName(RootName + utostr(OpNo)) +
1439 ", InFlag).getNode();");
Dale Johannesen874ae252009-06-02 03:12:52 +00001440 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001441 emitCode(ChainName + " = SDValue(ResNode, 0);");
1442 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001443 }
1444 }
1445 }
1446 }
1447 }
Evan Cheng54597732006-01-26 00:22:25 +00001448
Dale Johannesen874ae252009-06-02 03:12:52 +00001449 if (HasInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001450 if (!InFlagDecled) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001451 emitCode("SDValue InFlag = " + getNodeName(RootName) +
1452 "->getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001453 InFlagDecled = true;
1454 } else
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001455 emitCode("InFlag = " + getNodeName(RootName) +
1456 "->getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001457 }
Evan Chengb915f312005-12-09 22:45:35 +00001458 }
1459};
1460
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001461/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1462/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001463/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001464void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001465 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001466 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001467 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001468 std::vector<std::string> &TargetVTs,
1469 bool &OutputIsVariadic,
1470 unsigned &NumInputRootOps) {
1471 OutputIsVariadic = false;
1472 NumInputRootOps = 0;
1473
Dan Gohman22bb3112008-08-22 00:20:26 +00001474 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001475 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001476 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001477 TargetOpcodes, TargetVTs,
1478 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001479
Chris Lattner8fc35682005-09-23 23:16:51 +00001480 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001481 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001482 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001483
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001484 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001485 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001486
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001487 // At this point, we know that we structurally match the pattern, but the
1488 // types of the nodes may not match. Figure out the fewest number of type
1489 // comparisons we need to emit. For example, if there is only one integer
1490 // type supported by a target, there should be no type comparisons at all for
1491 // integer patterns!
1492 //
1493 // To figure out the fewest number of type checks needed, clone the pattern,
1494 // remove the types, then perform type inference on the pattern as a whole.
1495 // If there are unresolved types, emit an explicit check for those types,
1496 // apply the type to the tree, then rerun type inference. Iterate until all
1497 // types are resolved.
1498 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001499 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001500 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001501
1502 do {
1503 // Resolve/propagate as many types as possible.
1504 try {
1505 bool MadeChange = true;
1506 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001507 MadeChange = Pat->ApplyTypeConstraints(TP,
1508 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001509 } catch (...) {
1510 assert(0 && "Error: could not find consistent types for something we"
1511 " already decided was ok!");
1512 abort();
1513 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001514
Chris Lattner7e82f132005-10-15 21:34:21 +00001515 // Insert a check for an unresolved type and add it to the tree. If we find
1516 // an unresolved type to add a check for, this returns true and we iterate,
1517 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001518 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001519
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001520 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001521 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001522 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001523}
1524
Chris Lattner24e00a42006-01-29 04:41:05 +00001525/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1526/// a line causes any of them to be empty, remove them and return true when
1527/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001528static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001529 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001530 &Patterns) {
1531 bool ErasedPatterns = false;
1532 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1533 Patterns[i].second.pop_back();
1534 if (Patterns[i].second.empty()) {
1535 Patterns.erase(Patterns.begin()+i);
1536 --i; --e;
1537 ErasedPatterns = true;
1538 }
1539 }
1540 return ErasedPatterns;
1541}
1542
Chris Lattner8bc74722006-01-29 04:25:26 +00001543/// EmitPatterns - Emit code for at least one pattern, but try to group common
1544/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001545void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001546 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001547 &Patterns, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001548 raw_ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001549 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001550 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001551 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001552
1553 if (Patterns.empty()) return;
1554
Chris Lattner24e00a42006-01-29 04:41:05 +00001555 // Figure out how many patterns share the next code line. Explicitly copy
1556 // FirstCodeLine so that we don't invalidate a reference when changing
1557 // Patterns.
1558 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001559 unsigned LastMatch = Patterns.size()-1;
1560 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1561 --LastMatch;
1562
1563 // If not all patterns share this line, split the list into two pieces. The
1564 // first chunk will use this line, the second chunk won't.
1565 if (LastMatch != 0) {
1566 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1567 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1568
1569 // FIXME: Emit braces?
1570 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001571 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001572 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1573 Pattern.getSrcPattern()->print(OS);
1574 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1575 Pattern.getDstPattern()->print(OS);
1576 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001577 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001578 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001579 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001580 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001581 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001582 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001583 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001584 }
Evan Cheng676d7312006-08-26 00:59:04 +00001585 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001586 OS << std::string(Indent, ' ') << "{\n";
1587 Indent += 2;
1588 }
1589 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001590 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001591 Indent -= 2;
1592 OS << std::string(Indent, ' ') << "}\n";
1593 }
1594
1595 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001596 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001597 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1598 Pattern.getSrcPattern()->print(OS);
1599 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1600 Pattern.getDstPattern()->print(OS);
1601 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001602 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001603 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001604 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001605 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001606 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001607 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001608 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001609 }
1610 EmitPatterns(Other, Indent, OS);
1611 return;
1612 }
1613
Chris Lattner24e00a42006-01-29 04:41:05 +00001614 // Remove this code from all of the patterns that share it.
1615 bool ErasedPatterns = EraseCodeLine(Patterns);
1616
Evan Cheng676d7312006-08-26 00:59:04 +00001617 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001618
1619 // Otherwise, every pattern in the list has this line. Emit it.
1620 if (!isPredicate) {
1621 // Normal code.
1622 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1623 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001624 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1625
1626 // If the next code line is another predicate, and if all of the pattern
1627 // in this group share the same next line, emit it inline now. Do this
1628 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001629 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Jim Grosbachda4231f2009-03-26 16:17:51 +00001630 // Check that all of the patterns in Patterns end with the same predicate.
Chris Lattner24e00a42006-01-29 04:41:05 +00001631 bool AllEndWithSamePredicate = true;
1632 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1633 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1634 AllEndWithSamePredicate = false;
1635 break;
1636 }
1637 // If all of the predicates aren't the same, we can't share them.
1638 if (!AllEndWithSamePredicate) break;
1639
1640 // Otherwise we can. Emit it shared now.
1641 OS << " &&\n" << std::string(Indent+4, ' ')
1642 << Patterns.back().second.back().second;
1643 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001644 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001645
1646 OS << ") {\n";
1647 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001648 }
1649
1650 EmitPatterns(Patterns, Indent, OS);
1651
1652 if (isPredicate)
1653 OS << std::string(Indent-2, ' ') << "}\n";
1654}
1655
Evan Cheng892aaf82006-11-08 23:01:03 +00001656static std::string getLegalCName(std::string OpName) {
1657 std::string::size_type pos = OpName.find("::");
1658 if (pos != std::string::npos)
1659 OpName.replace(pos, 2, "_");
1660 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001661}
1662
Daniel Dunbar1a551802009-07-03 00:10:29 +00001663void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001664 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001665
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001666 // Get the namespace to insert instructions into.
1667 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001668 if (!InstNS.empty()) InstNS += "::";
1669
Chris Lattner602f6922006-01-04 00:25:00 +00001670 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001671 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001672 // All unique target node emission functions.
1673 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001674 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001675 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001676 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001677
1678 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001679 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001680 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001681 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001682 } else {
1683 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001684 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001685 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001686 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001687 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001688 std::vector<Record*> OpNodes = CP->getRootNodes();
1689 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001690 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1691 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001692 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001693 }
1694 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001695 errs() << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001696 Node->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001697 errs() << "' on tree pattern '";
1698 errs() << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001699 exit(1);
1700 }
1701 }
1702 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001703
1704 // For each opcode, there might be multiple select functions, one per
1705 // ValueType of the node (or its first operand if it doesn't produce a
1706 // non-chain result.
1707 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1708
Chris Lattner602f6922006-01-04 00:25:00 +00001709 // Emit one Select_* method for each top-level opcode. We do this instead of
1710 // emitting one giant switch statement to support compilers where this will
1711 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001712 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001713 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1714 PBOI != E; ++PBOI) {
1715 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001716 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001717 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1718
Chris Lattner706d2d32006-08-09 16:44:44 +00001719 // Split them into groups by type.
Owen Anderson825b72b2009-08-11 20:47:22 +00001720 std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001721 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001722 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001723 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001724 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001725 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001726 }
1727
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 for (std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001729 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001730 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1731 ++II) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001732 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001733 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001734 typedef std::pair<unsigned, std::string> CodeLine;
1735 typedef std::vector<CodeLine> CodeList;
1736 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001737
Chris Lattner60d81392008-01-05 22:30:17 +00001738 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001739 std::vector<std::vector<std::string> > PatternOpcodes;
1740 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001741 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001742 std::vector<bool> OutputIsVariadicFlags;
1743 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001744 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1745 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001746 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001747 std::vector<std::string> TargetOpcodes;
1748 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001749 bool OutputIsVariadic;
1750 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001751 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001752 TargetOpcodes, TargetVTs,
1753 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001754 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1755 PatternDecls.push_back(GeneratedDecl);
1756 PatternOpcodes.push_back(TargetOpcodes);
1757 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001758 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1759 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001760 }
1761
Chris Lattner706d2d32006-08-09 16:44:44 +00001762 // Factor target node emission code (emitted by EmitResultCode) into
1763 // separate functions. Uniquing and share them among all instruction
1764 // selection routines.
1765 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1766 CodeList &GeneratedCode = CodeForPatterns[i].second;
1767 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1768 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001769 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001770 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1771 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001772 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001773 int CodeSize = (int)GeneratedCode.size();
1774 int LastPred = -1;
1775 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001776 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001777 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001778 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1779 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001780 }
1781
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001782 std::string CalleeCode = "(SDNode *N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001783 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001784 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1785 CalleeCode += ", unsigned Opc" + utostr(j);
1786 CallerCode += ", " + TargetOpcodes[j];
1787 }
1788 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Owen Anderson69110c92009-09-11 09:01:57 +00001789 CalleeCode += ", MVT::SimpleValueType VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001790 CallerCode += ", " + TargetVTs[j];
1791 }
Evan Chengf5493192006-08-26 01:02:19 +00001792 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001793 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001794 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001795 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001796 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001797 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001798
1799 if (OutputIsVariadic) {
1800 CalleeCode += ", unsigned NumInputRootOps";
1801 CallerCode += ", " + utostr(NumInputRootOps);
1802 }
1803
Chris Lattner706d2d32006-08-09 16:44:44 +00001804 CallerCode += ");";
Benjamin Kramerf2a39bd2009-11-14 16:37:18 +00001805 CalleeCode += ") {\n";
Evan Cheng676d7312006-08-26 00:59:04 +00001806
1807 for (std::vector<std::string>::const_reverse_iterator
1808 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1809 CalleeCode += " " + *I + "\n";
1810
Evan Chengf5493192006-08-26 01:02:19 +00001811 for (int j = LastPred+1; j < CodeSize; ++j)
1812 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001813 for (int j = LastPred+1; j < CodeSize; ++j)
1814 GeneratedCode.pop_back();
1815 CalleeCode += "}\n";
1816
1817 // Uniquing the emission routines.
1818 unsigned EmitFuncNum;
1819 std::map<std::string, unsigned>::iterator EFI =
1820 EmitFunctions.find(CalleeCode);
1821 if (EFI != EmitFunctions.end()) {
1822 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001823 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001824 EmitFuncNum = EmitFunctions.size();
1825 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Benjamin Kramerf2a39bd2009-11-14 16:37:18 +00001826 // Prevent emission routines from being inlined to reduce selection
1827 // routines stack frame sizes.
1828 OS << "DISABLE_INLINE ";
Evan Cheng06d64702006-08-11 08:59:35 +00001829 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001830 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001831
Chris Lattner706d2d32006-08-09 16:44:44 +00001832 // Replace the emission code within selection routines with calls to the
1833 // emission functions.
David Greene8ad4c002008-10-27 21:56:29 +00001834 if (GenDebug) {
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001835 GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N, \"red\");"));
David Greene8ad4c002008-10-27 21:56:29 +00001836 }
1837 CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
1838 GeneratedCode.push_back(std::make_pair(3, CallerCode));
1839 if (GenDebug) {
1840 GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
1841 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"yellow\");"));
1842 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"black\");"));
1843 GeneratedCode.push_back(std::make_pair(0, "}"));
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001844 //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N, \"black\");"));
David Greene8ad4c002008-10-27 21:56:29 +00001845 }
1846 GeneratedCode.push_back(std::make_pair(0, "return Result;"));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001847 }
1848
Chris Lattner706d2d32006-08-09 16:44:44 +00001849 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001850 std::string OpVTStr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001851 if (OpVT == MVT::iPTR) {
Chris Lattner33a40042006-11-14 22:17:10 +00001852 OpVTStr = "_iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +00001853 } else if (OpVT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +00001854 OpVTStr = "_iPTRAny";
Owen Anderson825b72b2009-08-11 20:47:22 +00001855 } else if (OpVT == MVT::isVoid) {
Chris Lattner33a40042006-11-14 22:17:10 +00001856 // Nodes with a void result actually have a first result type of either
1857 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1858 // void to this case, we handle it specially here.
1859 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001860 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
Chris Lattner33a40042006-11-14 22:17:10 +00001861 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001862 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1863 OpcodeVTMap.find(OpName);
1864 if (OpVTI == OpcodeVTMap.end()) {
1865 std::vector<std::string> VTSet;
1866 VTSet.push_back(OpVTStr);
1867 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1868 } else
1869 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001870
Dan Gohman0540e172008-10-15 06:17:21 +00001871 // We want to emit all of the matching code now. However, we want to emit
1872 // the matches in order of minimal cost. Sort the patterns so the least
1873 // cost one is at the start.
1874 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1875 PatternSortingPredicate(CGP));
1876
1877 // Scan the code to see if all of the patterns are reachable and if it is
1878 // possible that the last one might not match.
1879 bool mightNotMatch = true;
1880 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1881 CodeList &GeneratedCode = CodeForPatterns[i].second;
1882 mightNotMatch = false;
1883
1884 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1885 if (GeneratedCode[j].first == 1) { // predicate.
1886 mightNotMatch = true;
1887 break;
1888 }
1889 }
1890
1891 // If this pattern definitely matches, and if it isn't the last one, the
1892 // patterns after it CANNOT ever match. Error out.
1893 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001894 errs() << "Pattern '";
1895 CodeForPatterns[i].first->getSrcPattern()->print(errs());
1896 errs() << "' is impossible to select!\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001897 exit(1);
1898 }
1899 }
1900
Chris Lattner706d2d32006-08-09 16:44:44 +00001901 // Loop through and reverse all of the CodeList vectors, as we will be
1902 // accessing them from their logical front, but accessing the end of a
1903 // vector is more efficient.
1904 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1905 CodeList &GeneratedCode = CodeForPatterns[i].second;
1906 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001907 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001908
1909 // Next, reverse the list of patterns itself for the same reason.
1910 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1911
Dan Gohman63e3e632009-01-29 01:37:18 +00001912 OS << "SDNode *Select_" << getLegalCName(OpName)
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001913 << OpVTStr << "(SDNode *N) {\n";
Dan Gohman63e3e632009-01-29 01:37:18 +00001914
Chris Lattner706d2d32006-08-09 16:44:44 +00001915 // Emit all of the patterns now, grouped together to share code.
1916 EmitPatterns(CodeForPatterns, 2, OS);
1917
Chris Lattner64906972006-09-21 18:28:27 +00001918 // If the last pattern has predicates (which could fail) emit code to
1919 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001920 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001921 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001922 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1923 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001924 OpName != "ISD::INTRINSIC_VOID")
1925 OS << " CannotYetSelect(N);\n";
1926 else
1927 OS << " CannotYetSelectIntrinsic(N);\n";
1928
1929 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001930 }
1931 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001932 }
Chris Lattner602f6922006-01-04 00:25:00 +00001933 }
1934
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001935 OS << "// The main instruction selector code.\n"
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001936 << "SDNode *SelectCode(SDNode *N) {\n"
1937 << " MVT::SimpleValueType NVT = N->getValueType(0).getSimpleVT().SimpleTy;\n"
1938 << " switch (N->getOpcode()) {\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00001939 << " default:\n"
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001940 << " assert(!N->isMachineOpcode() && \"Node already selected!\");\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00001941 << " break;\n"
1942 << " case ISD::EntryToken: // These nodes remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001943 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001944 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001945 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001946 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001947 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001948 << " case ISD::TargetConstantPool:\n"
1949 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00001950 << " case ISD::TargetExternalSymbol:\n"
Dan Gohman8c2b5252009-10-30 01:27:03 +00001951 << " case ISD::TargetBlockAddress:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001952 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001953 << " case ISD::TargetGlobalTLSAddress:\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001954 << " case ISD::TargetGlobalAddress:\n"
1955 << " case ISD::TokenFactor:\n"
1956 << " case ISD::CopyFromReg:\n"
1957 << " case ISD::CopyToReg: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001958 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001959 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001960 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001961 << " case ISD::AssertZext: {\n"
Dan Gohmaneeb3a002010-01-05 01:24:18 +00001962 << " ReplaceUses(SDValue(N, 0), N->getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001963 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001964 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001965 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001966 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001967 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001968
Chris Lattner602f6922006-01-04 00:25:00 +00001969 // Loop over all of the case statements, emiting a call to each method we
1970 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001971 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001972 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1973 PBOI != E; ++PBOI) {
1974 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001975 // Potentially multiple versions of select for this opcode. One for each
1976 // ValueType of the node (or its first true operand if it doesn't produce a
1977 // result.
1978 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1979 OpcodeVTMap.find(OpName);
1980 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001981 OS << " case " << OpName << ": {\n";
Dale Johannesen3b895cf2009-05-12 22:32:29 +00001982 // If we have only one variant and it's the default, elide the
1983 // switch. Marginally faster, and makes MSVC happier.
1984 if (OpVTs.size()==1 && OpVTs[0].empty()) {
1985 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
1986 OS << " break;\n";
1987 OS << " }\n";
1988 continue;
1989 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001990 // Keep track of whether we see a pattern that has an iPtr result.
1991 bool HasPtrPattern = false;
1992 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001993
Evan Cheng425e8c72007-09-04 20:18:28 +00001994 OS << " switch (NVT) {\n";
1995 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1996 std::string &VTStr = OpVTs[i];
1997 if (VTStr.empty()) {
1998 HasDefaultPattern = true;
1999 continue;
2000 }
Chris Lattner717a6112006-11-14 21:50:27 +00002001
Evan Cheng425e8c72007-09-04 20:18:28 +00002002 // If this is a match on iPTR: don't emit it directly, we need special
2003 // code.
2004 if (VTStr == "_iPTR") {
2005 HasPtrPattern = true;
2006 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002007 }
Owen Anderson825b72b2009-08-11 20:47:22 +00002008 OS << " case MVT::" << VTStr.substr(1) << ":\n"
Evan Cheng425e8c72007-09-04 20:18:28 +00002009 << " return Select_" << getLegalCName(OpName)
2010 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002011 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002012 OS << " default:\n";
2013
2014 // If there is an iPTR result version of this pattern, emit it here.
2015 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002016 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002017 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2018 }
2019 if (HasDefaultPattern) {
2020 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2021 }
2022 OS << " break;\n";
2023 OS << " }\n";
2024 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002025 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002026 }
Chris Lattner81303322005-09-23 19:36:15 +00002027
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002028 OS << " } // end of big switch.\n\n"
Dan Gohmaneeb3a002010-01-05 01:24:18 +00002029 << " if (N->getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2030 << " N->getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2031 << " N->getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002032 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002033 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002034 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002035 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002036 << " return NULL;\n"
2037 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002038}
2039
Daniel Dunbar1a551802009-07-03 00:10:29 +00002040void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002041 EmitSourceFileHeader("DAG Instruction Selector for the " +
2042 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002043
Chris Lattner1f39e292005-09-14 00:09:24 +00002044 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2045 << "// *** instruction selector class. These functions are really "
2046 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002047
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002048 OS << "// Include standard, target-independent definitions and methods used\n"
2049 << "// by the instruction selector.\n";
Mike Stumpfe095f32009-05-04 18:40:41 +00002050 OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002051
Chris Lattner443e3f92008-01-05 22:54:53 +00002052 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002053 EmitPredicateFunctions(OS);
2054
Chris Lattner569f1212009-08-23 04:44:11 +00002055 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n");
Chris Lattnerfe718932008-01-06 01:10:31 +00002056 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002057 I != E; ++I) {
Chris Lattner569f1212009-08-23 04:44:11 +00002058 DEBUG(errs() << "PATTERN: "; I->getSrcPattern()->dump());
2059 DEBUG(errs() << "\nRESULT: "; I->getDstPattern()->dump());
2060 DEBUG(errs() << "\n");
Bill Wendlingf5da1332006-12-07 22:21:48 +00002061 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002062
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002063 // At this point, we have full information about the 'Patterns' we need to
2064 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002065 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002066 EmitInstructionSelector(OS);
2067
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002068}