blob: c3520c1c6c7de969ef23a2e68af54fcf60a9860f [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000017#include "llvm/Support/CommandLine.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000018#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000019#include "llvm/Support/MathExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000020#include "llvm/Support/Debug.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000021#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000022#include <deque>
Daniel Dunbar1a551802009-07-03 00:10:29 +000023#include <iostream>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024using namespace llvm;
25
Chris Lattner3d4ad292009-08-07 22:27:19 +000026static cl::opt<bool>
27GenDebug("gen-debug", cl::desc("Generate debug code"), cl::init(false));
David Greene8ad4c002008-10-27 21:56:29 +000028
Chris Lattnerca559d02005-09-08 21:03:01 +000029//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000030// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000031//
32
Chris Lattner6cefb772008-01-05 22:25:12 +000033/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
34/// ComplexPattern.
35static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000036 return (N->isLeaf() &&
37 dynamic_cast<DefInit*>(N->getLeafValue()) &&
38 static_cast<DefInit*>(N->getLeafValue())->getDef()->
39 isSubClassOf("ComplexPattern"));
40}
41
Chris Lattner6cefb772008-01-05 22:25:12 +000042/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
43/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000044static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000045 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000046 if (N->isLeaf() &&
47 dynamic_cast<DefInit*>(N->getLeafValue()) &&
48 static_cast<DefInit*>(N->getLeafValue())->getDef()->
49 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000050 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
51 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000052 }
53 return NULL;
54}
55
Chris Lattner05814af2005-09-28 17:57:56 +000056/// getPatternSize - Return the 'size' of this pattern. We want to match large
57/// patterns before small ones. This is used to determine the size of a
58/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000059static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Owen Andersone50ed302009-08-10 22:56:29 +000060 assert((EEVT::isExtIntegerInVTs(P->getExtTypes()) ||
61 EEVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +000062 P->getExtTypeNum(0) == MVT::isVoid ||
63 P->getExtTypeNum(0) == MVT::Flag ||
64 P->getExtTypeNum(0) == MVT::iPTR ||
65 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000066 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000067 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000068 // If the root node is a ConstantSDNode, increases its size.
69 // e.g. (set R32:$dst, 0).
70 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000071 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000072
73 // FIXME: This is a hack to statically increase the priority of patterns
74 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
75 // Later we can allow complexity / cost for each pattern to be (optionally)
76 // specified. To get best possible pattern match we'll need to dynamically
77 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000078 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000079 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000080 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000081
82 // If this node has some predicate function that must match, it adds to the
83 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000084 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000085 ++Size;
86
Chris Lattner05814af2005-09-28 17:57:56 +000087 // Count children in the count if they are also nodes.
88 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
89 TreePatternNode *Child = P->getChild(i);
Owen Anderson825b72b2009-08-11 20:47:22 +000090 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000091 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000092 else if (Child->isLeaf()) {
93 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000094 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000095 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000096 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +000097 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000098 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000099 }
Chris Lattner05814af2005-09-28 17:57:56 +0000100 }
101
102 return Size;
103}
104
105/// getResultPatternCost - Compute the number of instructions for this pattern.
106/// This is a temporary hack. We should really include the instruction
107/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000108static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000109 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000110 if (P->isLeaf()) return 0;
111
Evan Chengfbad7082006-02-18 02:33:09 +0000112 unsigned Cost = 0;
113 Record *Op = P->getOperator();
114 if (Op->isSubClassOf("Instruction")) {
115 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000116 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Dan Gohman533297b2009-10-29 18:10:34 +0000117 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +0000118 Cost += 10;
119 }
Chris Lattner05814af2005-09-28 17:57:56 +0000120 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000121 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000122 return Cost;
123}
124
Evan Chenge6f32032006-07-19 00:24:41 +0000125/// getResultPatternCodeSize - Compute the code size of instructions for this
126/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000127static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000128 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000129 if (P->isLeaf()) return 0;
130
131 unsigned Cost = 0;
132 Record *Op = P->getOperator();
133 if (Op->isSubClassOf("Instruction")) {
134 Cost += Op->getValueAsInt("CodeSize");
135 }
136 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000137 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000138 return Cost;
139}
140
Chris Lattner05814af2005-09-28 17:57:56 +0000141// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
142// In particular, we want to match maximal patterns first and lowest cost within
143// a particular complexity first.
144struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000145 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
146 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000147
Dan Gohman0540e172008-10-15 06:17:21 +0000148 typedef std::pair<unsigned, std::string> CodeLine;
149 typedef std::vector<CodeLine> CodeList;
150 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
151
152 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
153 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
154 const PatternToMatch *LHS = LHSPair.first;
155 const PatternToMatch *RHS = RHSPair.first;
156
Chris Lattner6cefb772008-01-05 22:25:12 +0000157 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
158 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000159 LHSSize += LHS->getAddedComplexity();
160 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000161 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
162 if (LHSSize < RHSSize) return false;
163
164 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000165 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
166 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000167 if (LHSCost < RHSCost) return true;
168 if (LHSCost > RHSCost) return false;
169
Chris Lattner6cefb772008-01-05 22:25:12 +0000170 return getResultPatternSize(LHS->getDstPattern(), CGP) <
171 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000172 }
173};
174
Jim Grosbach54f30222009-03-25 23:28:33 +0000175/// getRegisterValueType - Look up and return the ValueType of the specified
176/// register. If the register is a member of multiple register classes which
Owen Anderson825b72b2009-08-11 20:47:22 +0000177/// have different associated types, return MVT::Other.
178static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000179 bool FoundRC = false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 MVT::SimpleValueType VT = MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000181 const std::vector<CodeGenRegisterClass> &RCs = T.getRegisterClasses();
182 std::vector<CodeGenRegisterClass>::const_iterator RC;
183 std::vector<Record*>::const_iterator Element;
184
185 for (RC = RCs.begin() ; RC != RCs.end() ; RC++) {
186 Element = find((*RC).Elements.begin(), (*RC).Elements.end(), R);
187 if (Element != (*RC).Elements.end()) {
188 if (!FoundRC) {
Jim Grosbach866cc602009-03-26 14:45:34 +0000189 FoundRC = true;
Jim Grosbach54f30222009-03-25 23:28:33 +0000190 VT = (*RC).getValueTypeNum(0);
191 } else {
192 // In multiple RC's
193 if (VT != (*RC).getValueTypeNum(0)) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 // Types of the RC's do not agree. Return MVT::Other. The
Jim Grosbach54f30222009-03-25 23:28:33 +0000195 // target is responsible for handling this.
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 return MVT::Other;
Jim Grosbach54f30222009-03-25 23:28:33 +0000197 }
198 }
199 }
200 }
201 return VT;
Evan Cheng66a48bb2005-12-01 00:18:45 +0000202}
203
Chris Lattner72fe91c2005-09-24 00:40:24 +0000204
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000205/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
206/// type information from it.
207static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000208 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000209 if (!N->isLeaf())
210 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
211 RemoveAllTypes(N->getChild(i));
212}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000213
Evan Cheng51fecc82006-01-09 18:27:06 +0000214/// NodeHasProperty - return true if TreePatternNode has the specified
215/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000216static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000217 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000218 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000219 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000220 if (CP)
221 return CP->hasProperty(Property);
222 return false;
223 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000224 Record *Operator = N->getOperator();
225 if (!Operator->isSubClassOf("SDNode")) return false;
226
Chris Lattner6cefb772008-01-05 22:25:12 +0000227 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000228}
229
Evan Cheng94b30402006-10-11 21:02:01 +0000230static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000231 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000232 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000233 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000234
235 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
236 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000237 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000238 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000239 }
240
241 return false;
242}
243
Evan Chengf9d03182008-07-03 08:39:51 +0000244static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
245 return CGP.getSDNodeInfo(Op).getEnumName();
246}
247
248static
249bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
250 bool isStore = !N->isLeaf() &&
251 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
252 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
253 return false;
254
255 bool HasChain = false;
256 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
257 TreePatternNode *Child = N->getChild(i);
258 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
259 HasChain = true;
260 break;
261 }
262 }
263 return HasChain;
264}
265
Chris Lattnerdc32f982008-01-05 22:43:57 +0000266//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000267// Node Transformation emitter implementation.
268//
Daniel Dunbar1a551802009-07-03 00:10:29 +0000269void DAGISelEmitter::EmitNodeTransforms(raw_ostream &OS) {
Chris Lattner443e3f92008-01-05 22:54:53 +0000270 // Walk the pattern fragments, adding them to a map, which sorts them by
271 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000272 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000273 NXsByNameTy NXsByName;
274
Chris Lattnerfe718932008-01-06 01:10:31 +0000275 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000276 I != E; ++I)
277 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
278
279 OS << "\n// Node transformations.\n";
280
281 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
282 I != E; ++I) {
283 Record *SDNode = I->second.first;
284 std::string Code = I->second.second;
285
286 if (Code.empty()) continue; // Empty code? Skip it.
287
Chris Lattner200c57e2008-01-05 22:58:54 +0000288 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000289 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
290
Dan Gohman475871a2008-07-27 21:46:04 +0000291 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000292 << ") {\n";
293 if (ClassName != "SDNode")
294 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
295 OS << Code << "\n}\n";
296 }
297}
298
299//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000300// Predicate emitter implementation.
301//
302
Daniel Dunbar1a551802009-07-03 00:10:29 +0000303void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
Chris Lattnerdc32f982008-01-05 22:43:57 +0000304 OS << "\n// Predicate functions.\n";
305
306 // Walk the pattern fragments, adding them to a map, which sorts them by
307 // name.
308 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
309 PFsByNameTy PFsByName;
310
Chris Lattnerfe718932008-01-06 01:10:31 +0000311 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000312 I != E; ++I)
313 PFsByName.insert(std::make_pair(I->first->getName(), *I));
314
315
316 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
317 I != E; ++I) {
318 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
319 TreePattern *P = I->second.second;
320
321 // If there is a code init for this fragment, emit the predicate code.
322 std::string Code = PatFragRecord->getValueAsCode("Predicate");
323 if (Code.empty()) continue;
324
325 if (P->getOnlyTree()->isLeaf())
326 OS << "inline bool Predicate_" << PatFragRecord->getName()
327 << "(SDNode *N) {\n";
328 else {
329 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000330 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000331 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
332
333 OS << "inline bool Predicate_" << PatFragRecord->getName()
334 << "(SDNode *" << C2 << ") {\n";
335 if (ClassName != "SDNode")
336 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
337 }
338 OS << Code << "\n}\n";
339 }
340
341 OS << "\n\n";
342}
343
344
345//===----------------------------------------------------------------------===//
346// PatternCodeEmitter implementation.
347//
Evan Chengb915f312005-12-09 22:45:35 +0000348class PatternCodeEmitter {
349private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000350 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000351
Evan Cheng58e84a62005-12-14 22:02:59 +0000352 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000353 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000354 // Pattern cost.
355 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000356 // Instruction selector pattern.
357 TreePatternNode *Pattern;
358 // Matched instruction.
359 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000360
Evan Chengb915f312005-12-09 22:45:35 +0000361 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000362 std::map<std::string, std::string> VariableMap;
363 // Node to operator mapping
364 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000365 // Name of the folded node which produces a flag.
366 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000367 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000368 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000369 // Original input chain(s).
370 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000371 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000372
Dan Gohman69de1932008-02-06 22:27:42 +0000373 /// LSI - Load/Store information.
374 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
375 /// for each memory access. This facilitates the use of AliasAnalysis in
376 /// the backend.
377 std::vector<std::string> LSI;
378
Evan Cheng676d7312006-08-26 00:59:04 +0000379 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000380 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000381 /// tested, and if true, the match fails) [when 1], or normal code to emit
382 /// [when 0], or initialization code to emit [when 2].
383 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000384 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000385 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000386 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000387 /// TargetOpcodes - The target specific opcodes used by the resulting
388 /// instructions.
389 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000390 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000391 /// OutputIsVariadic - Records whether the instruction output pattern uses
392 /// variable_ops. This requires that the Emit function be passed an
393 /// additional argument to indicate where the input varargs operands
394 /// begin.
395 bool &OutputIsVariadic;
396 /// NumInputRootOps - Records the number of operands the root node of the
397 /// input pattern has. This information is used in the generated code to
398 /// pass to Emit functions when variable_ops processing is needed.
399 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000400
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000401 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000402 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000403 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000404 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000405
406 void emitCheck(const std::string &S) {
407 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000408 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000409 }
410 void emitCode(const std::string &S) {
411 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000412 GeneratedCode.push_back(std::make_pair(0, S));
413 }
414 void emitInit(const std::string &S) {
415 if (!S.empty())
416 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000417 }
Evan Chengf5493192006-08-26 01:02:19 +0000418 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000419 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000420 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000421 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000422 void emitOpcode(const std::string &Opc) {
423 TargetOpcodes.push_back(Opc);
424 OpcNo++;
425 }
Evan Chengf8729402006-07-16 06:12:52 +0000426 void emitVT(const std::string &VT) {
427 TargetVTs.push_back(VT);
428 VTNo++;
429 }
Evan Chengb915f312005-12-09 22:45:35 +0000430public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000431 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000432 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000433 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000434 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000435 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000436 std::vector<std::string> &tv,
437 bool &oiv,
438 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000439 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000440 GeneratedCode(gc), GeneratedDecl(gd),
441 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000442 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000443 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000444
445 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
446 /// if the match fails. At this point, we already know that the opcode for N
447 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000448 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
449 const std::string &RootName, const std::string &ChainSuffix,
450 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000451
452 // Save loads/stores matched by a pattern.
453 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000454 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000455 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000456 }
457
Evan Chenge41bf822006-02-05 06:43:12 +0000458 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000459 // Emit instruction predicates. Each predicate is just a string for now.
460 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000461 // Record input varargs info.
462 NumInputRootOps = N->getNumChildren();
463
Evan Chengf9d03182008-07-03 08:39:51 +0000464 if (DisablePatternForFastISel(N, CGP))
Bill Wendling98a366d2009-04-29 23:29:43 +0000465 emitCheck("OptLevel != CodeGenOpt::None");
Evan Chengf9d03182008-07-03 08:39:51 +0000466
Chris Lattner8a0604b2006-01-28 20:31:24 +0000467 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000468 }
469
Evan Chengb915f312005-12-09 22:45:35 +0000470 if (N->isLeaf()) {
471 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000472 emitCheck("cast<ConstantSDNode>(" + RootName +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000473 ")->getSExtValue() == INT64_C(" +
474 itostr(II->getValue()) + ")");
Evan Chengb915f312005-12-09 22:45:35 +0000475 return;
476 } else if (!NodeIsComplexPattern(N)) {
477 assert(0 && "Cannot match this as a leaf value!");
478 abort();
479 }
480 }
481
Chris Lattner488580c2006-01-28 19:06:51 +0000482 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000483 // we already saw this in the pattern, emit code to verify dagness.
484 if (!N->getName().empty()) {
485 std::string &VarMapEntry = VariableMap[N->getName()];
486 if (VarMapEntry.empty()) {
487 VarMapEntry = RootName;
488 } else {
489 // If we get here, this is a second reference to a specific name. Since
490 // we already have checked that the first reference is valid, we don't
491 // have to recursively match it, just check that it's the same as the
492 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000493 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000494 return;
495 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000496
497 if (!N->isLeaf())
498 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000499 }
500
501
502 // Emit code to load the child nodes and match their contents recursively.
503 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000504 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
505 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000506 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000507 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000508 if (NodeHasChain)
509 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000510 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000511 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000512 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000513 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000514 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000515 // If the immediate use can somehow reach this node through another
516 // path, then can't fold it either or it will create a cycle.
517 // e.g. In the following diagram, XX can reach ld through YY. If
518 // ld is folded into XX, then YY is both a predecessor and a successor
519 // of XX.
520 //
521 // [ld]
522 // ^ ^
523 // | |
524 // / \---
525 // / [YY]
526 // | ^
527 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000528 bool NeedCheck = P != Pattern;
529 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000530 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000531 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000532 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
533 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
534 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000535 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000536 PInfo.hasProperty(SDNPHasChain) ||
537 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000538 PInfo.hasProperty(SDNPOptInFlag);
539 }
540
541 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000542 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng884c70c2008-11-27 00:49:46 +0000543 emitCheck("IsLegalAndProfitableToFold(" + RootName +
544 ".getNode(), " + ParentName + ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000545 }
Evan Chenge41bf822006-02-05 06:43:12 +0000546 }
Evan Chengb915f312005-12-09 22:45:35 +0000547 }
Evan Chenge41bf822006-02-05 06:43:12 +0000548
Evan Chengc15d18c2006-01-27 22:13:45 +0000549 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000550 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000551 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
552 "IsChainCompatible(" + ChainName + ".getNode(), " +
553 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000554 OrigChains.push_back(std::make_pair(ChainName, RootName));
555 } else
Evan Chenge6389932006-07-21 22:19:51 +0000556 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000557 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000558 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000559 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000560 }
Evan Chengb915f312005-12-09 22:45:35 +0000561 }
562
Evan Cheng54597732006-01-26 00:22:25 +0000563 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000564 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000565 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000566 // FIXME: If the optional incoming flag does not exist. Then it is ok to
567 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000568 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000569 (PatternHasProperty(N, SDNPInFlag, CGP) ||
570 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
571 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000572 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000573 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000574 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000575 }
576 }
577
Dan Gohman0540e172008-10-15 06:17:21 +0000578 // If there are node predicates for this, emit the calls.
579 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
580 emitCheck(N->getPredicateFns()[i] + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000581
Chris Lattner39e73f72006-10-11 04:05:55 +0000582 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
583 // a constant without a predicate fn that has more that one bit set, handle
584 // this as a special case. This is usually for targets that have special
585 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
586 // handling stuff). Using these instructions is often far more efficient
587 // than materializing the constant. Unfortunately, both the instcombiner
588 // and the dag combiner can often infer that bits are dead, and thus drop
589 // them from the mask in the dag. For example, it might turn 'AND X, 255'
590 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
591 // to handle this.
592 if (!N->isLeaf() &&
593 (N->getOperator()->getName() == "and" ||
594 N->getOperator()->getName() == "or") &&
595 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000596 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000597 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
598 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000599 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000600 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000601 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000602 RootName + ".getOperand(" + utostr(1) + ");");
603
Dan Gohman0b53d982008-12-19 18:13:39 +0000604 unsigned NTmp = TmpNo++;
605 emitCode("ConstantSDNode *Tmp" + utostr(NTmp) +
606 " = dyn_cast<ConstantSDNode>(" + RootName + "1);");
607 emitCheck("Tmp" + utostr(NTmp));
Chris Lattner39e73f72006-10-11 04:05:55 +0000608 const char *MaskPredicate = N->getOperator()->getName() == "or"
609 ? "CheckOrMask(" : "CheckAndMask(";
Dan Gohman0b53d982008-12-19 18:13:39 +0000610 emitCheck(MaskPredicate + RootName + "0, Tmp" + utostr(NTmp) +
611 ", INT64_C(" + itostr(II->getValue()) + "))");
Chris Lattner39e73f72006-10-11 04:05:55 +0000612
Christopher Lamb85356242008-01-31 07:27:46 +0000613 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000614 ChainSuffix + utostr(0), FoundChain);
615 return;
616 }
617 }
618 }
619
Evan Chengb915f312005-12-09 22:45:35 +0000620 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000621 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000622 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000623
Christopher Lamb85356242008-01-31 07:27:46 +0000624 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000625 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000626 }
627
Evan Cheng676d7312006-08-26 00:59:04 +0000628 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000629 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000630 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000631 std::string Fn = CP->getSelectFunc();
632 unsigned NumOps = CP->getNumOperands();
633 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000634 emitDecl("CPTmp" + RootName + "_" + utostr(i));
635 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000636 }
Evan Cheng94b30402006-10-11 21:02:01 +0000637 if (CP->hasProperty(SDNPHasChain)) {
638 emitDecl("CPInChain");
639 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000640 emitCode("SDValue CPInChain;");
641 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000642 }
Evan Cheng676d7312006-08-26 00:59:04 +0000643
Evan Cheng811731e2006-11-08 20:31:10 +0000644 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000645 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000646 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000647 if (CP->hasProperty(SDNPHasChain)) {
648 ChainName = "Chain" + ChainSuffix;
649 Code += ", CPInChain, Chain" + ChainSuffix;
650 }
Evan Cheng676d7312006-08-26 00:59:04 +0000651 emitCheck(Code + ")");
652 }
Evan Chengb915f312005-12-09 22:45:35 +0000653 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000654
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000655 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000656 const std::string &RootName,
657 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000658 const std::string &ChainSuffix, bool &FoundChain) {
659 if (!Child->isLeaf()) {
660 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000661 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000662 emitCheck(RootName + ".getOpcode() == " +
663 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000664 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000665 bool HasChain = false;
666 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
667 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000668 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000669 }
Dale Johannesen874ae252009-06-02 03:12:52 +0000670 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
Evan Chenga58891f2008-02-05 22:50:29 +0000671 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
672 "Pattern folded multiple nodes which produce flags?");
673 FoldedFlag = std::make_pair(RootName,
674 CInfo.getNumResults() + (unsigned)HasChain);
675 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000676 } else {
677 // If this child has a name associated with it, capture it in VarMap. If
678 // we already saw this in the pattern, emit code to verify dagness.
679 if (!Child->getName().empty()) {
680 std::string &VarMapEntry = VariableMap[Child->getName()];
681 if (VarMapEntry.empty()) {
682 VarMapEntry = RootName;
683 } else {
684 // If we get here, this is a second reference to a specific name.
685 // Since we already have checked that the first reference is valid,
686 // we don't have to recursively match it, just check that it's the
687 // same as the previously named thing.
688 emitCheck(VarMapEntry + " == " + RootName);
689 Duplicates.insert(RootName);
690 return;
691 }
692 }
693
694 // Handle leaves of various types.
695 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
696 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000697 if (LeafRec->isSubClassOf("RegisterClass") ||
Chris Lattnera938ac62009-07-29 20:43:05 +0000698 LeafRec->isSubClassOf("PointerLikeRegClass")) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000699 // Handle register references. Nothing to do here.
700 } else if (LeafRec->isSubClassOf("Register")) {
701 // Handle register references.
702 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
703 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000704 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000705 std::string Fn = CP->getSelectFunc();
706 unsigned NumOps = CP->getNumOperands();
707 for (unsigned i = 0; i < NumOps; ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000708 emitDecl("CPTmp" + RootName + "_" + utostr(i));
709 emitCode("SDValue CPTmp" + RootName + "_" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000710 }
Evan Cheng94b30402006-10-11 21:02:01 +0000711 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000712 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000713 FoldedChains.push_back(std::make_pair("CPInChain",
714 PInfo.getNumResults()));
715 ChainName = "Chain" + ChainSuffix;
716 emitDecl("CPInChain");
717 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000718 emitCode("SDValue CPInChain;");
719 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000720 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000721
Christopher Lamb85356242008-01-31 07:27:46 +0000722 std::string Code = Fn + "(";
723 if (CP->hasAttribute(CPAttrParentAsRoot)) {
724 Code += ParentRootName + ", ";
725 } else {
726 Code += "N, ";
727 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000728 if (CP->hasProperty(SDNPHasChain)) {
729 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000730 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000731 }
732 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000733 for (unsigned i = 0; i < NumOps; i++)
Dan Gohman05aae182009-01-16 02:05:52 +0000734 Code += ", CPTmp" + RootName + "_" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000735 if (CP->hasProperty(SDNPHasChain))
736 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000737 emitCheck(Code + ")");
738 } else if (LeafRec->getName() == "srcvalue") {
739 // Place holder for SRCVALUE nodes. Nothing to do here.
740 } else if (LeafRec->isSubClassOf("ValueType")) {
741 // Make sure this is the specified value type.
742 emitCheck("cast<VTSDNode>(" + RootName +
Owen Anderson825b72b2009-08-11 20:47:22 +0000743 ")->getVT() == MVT::" + LeafRec->getName());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000744 } else if (LeafRec->isSubClassOf("CondCode")) {
745 // Make sure this is the specified cond code.
746 emitCheck("cast<CondCodeSDNode>(" + RootName +
747 ")->get() == ISD::" + LeafRec->getName());
748 } else {
749#ifndef NDEBUG
750 Child->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +0000751 errs() << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000752#endif
753 assert(0 && "Unknown leaf type!");
754 }
755
Dan Gohman0540e172008-10-15 06:17:21 +0000756 // If there are node predicates for this, emit the calls.
757 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
758 emitCheck(Child->getPredicateFns()[i] + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000759 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000760 } else if (IntInit *II =
761 dynamic_cast<IntInit*>(Child->getLeafValue())) {
Dan Gohman0b53d982008-12-19 18:13:39 +0000762 unsigned NTmp = TmpNo++;
763 emitCode("ConstantSDNode *Tmp"+ utostr(NTmp) +
764 " = dyn_cast<ConstantSDNode>("+
765 RootName + ");");
766 emitCheck("Tmp" + utostr(NTmp));
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000767 unsigned CTmp = TmpNo++;
Dan Gohman0b53d982008-12-19 18:13:39 +0000768 emitCode("int64_t CN"+ utostr(CTmp) +
769 " = Tmp" + utostr(NTmp) + "->getSExtValue();");
Dan Gohman63f97202008-10-17 01:33:43 +0000770 emitCheck("CN" + utostr(CTmp) + " == "
771 "INT64_C(" +itostr(II->getValue()) + ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000772 } else {
773#ifndef NDEBUG
774 Child->dump();
775#endif
776 assert(0 && "Unknown leaf type!");
777 }
778 }
779 }
Evan Chengb915f312005-12-09 22:45:35 +0000780
781 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
782 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000783 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000784 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000785 bool InFlagDecled, bool ResNodeDecled,
786 bool LikeLeaf = false, bool isRoot = false) {
Dan Gohman602b0c82009-09-25 18:54:59 +0000787 // List of arguments of getMachineNode() or SelectNodeTo().
Evan Cheng676d7312006-08-26 00:59:04 +0000788 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000789 // This is something selected from the pattern we matched.
790 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000791 const std::string &VarName = N->getName();
792 std::string Val = VariableMap[VarName];
793 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000794 if (Val.empty()) {
Daniel Dunbar1a551802009-07-03 00:10:29 +0000795 errs() << "Variable '" << VarName << " referenced but not defined "
Bill Wendling27926af2008-02-26 10:45:29 +0000796 << "and not caught earlier!\n";
797 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000798 }
Evan Chengb915f312005-12-09 22:45:35 +0000799 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
800 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000801 NodeOps.push_back(Val);
802 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000803 }
804
805 const ComplexPattern *CP;
806 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000807 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000808 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000809 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000810 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000811 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000812 default:
Daniel Dunbar1a551802009-07-03 00:10:29 +0000813 errs() << "Cannot handle " << getEnumName(N->getTypeNum(0))
Chris Lattnerd8a17282007-01-17 07:45:12 +0000814 << " type as an immediate constant. Aborting\n";
815 abort();
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 case MVT::i1: CastType = "bool"; break;
817 case MVT::i8: CastType = "unsigned char"; break;
818 case MVT::i16: CastType = "unsigned short"; break;
819 case MVT::i32: CastType = "unsigned"; break;
820 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000821 }
Dan Gohman475871a2008-07-27 21:46:04 +0000822 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000823 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000824 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000825 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000826 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
827 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000828 Val = TmpVar;
829 ModifiedVal = true;
830 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000831 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
832 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
833 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000834 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000835 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
836 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
837 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000838 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
839 // value if used multiple times by this pattern result.
840 Val = TmpVar;
841 ModifiedVal = true;
842 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000843 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000844 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000845 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000846 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000847 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000848 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000849 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000850 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000851 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000852 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
853 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000854 Val = TmpVar;
855 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000856 }
Scott Michel6be48d42008-01-29 02:29:31 +0000857 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000858 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
859 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000860 Record *Op = OperatorMap[N->getName()];
861 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000862 if (Op && (Op->getName() == "globaladdr" ||
863 Op->getName() == "globaltlsaddr")) {
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"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000866 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000867 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000868 ");");
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 }
Evan Cheng676d7312006-08-26 00:59:04 +0000874 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000875 } else if (!N->isLeaf()
876 && (N->getOperator()->getName() == "texternalsym"
877 || N->getOperator()->getName() == "tconstpool")) {
878 // Do not rewrite the variable name, since we don't generate a new
879 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000880 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000881 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000882 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
Dan Gohman05aae182009-01-16 02:05:52 +0000883 NodeOps.push_back("CPTmp" + Val + "_" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000884 }
Evan Chengb915f312005-12-09 22:45:35 +0000885 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000886 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000887 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000888 if (!LikeLeaf) {
Chris Lattner706d2d32006-08-09 16:44:44 +0000889 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000890 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000891 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000892 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000893 }
Evan Cheng676d7312006-08-26 00:59:04 +0000894 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000895 }
Scott Michel6be48d42008-01-29 02:29:31 +0000896
897 if (ModifiedVal) {
898 VariableMap[VarName] = Val;
899 }
Evan Cheng676d7312006-08-26 00:59:04 +0000900 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000901 }
Evan Chengb915f312005-12-09 22:45:35 +0000902 if (N->isLeaf()) {
903 // If this is an explicit register reference, handle it.
904 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
905 unsigned ResNo = TmpNo++;
906 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000907 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000908 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000909 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000910 NodeOps.push_back("Tmp" + utostr(ResNo));
911 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000912 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000913 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000914 " = CurDAG->getRegister(0, " +
915 getEnumName(N->getTypeNum(0)) + ");");
916 NodeOps.push_back("Tmp" + utostr(ResNo));
917 return NodeOps;
Dan Gohmanf8c73942009-04-13 15:38:05 +0000918 } else if (DI->getDef()->isSubClassOf("RegisterClass")) {
919 // Handle a reference to a register class. This is used
920 // in COPY_TO_SUBREG instructions.
921 emitCode("SDValue Tmp" + utostr(ResNo) +
922 " = CurDAG->getTargetConstant(" +
923 getQualifiedName(DI->getDef()) + "RegClassID, " +
Owen Anderson825b72b2009-08-11 20:47:22 +0000924 "MVT::i32);");
Dan Gohmanf8c73942009-04-13 15:38:05 +0000925 NodeOps.push_back("Tmp" + utostr(ResNo));
926 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000927 }
928 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
929 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000930 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000931 emitCode("SDValue Tmp" + utostr(ResNo) +
Daniel Dunbarbd17a292009-07-30 18:18:54 +0000932 " = CurDAG->getTargetConstant(0x" +
933 utohexstr((uint64_t) II->getValue()) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000934 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000935 NodeOps.push_back("Tmp" + utostr(ResNo));
936 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000937 }
938
Jim Laskey16d42c62006-07-11 18:25:13 +0000939#ifndef NDEBUG
940 N->dump();
941#endif
Evan Chengb915f312005-12-09 22:45:35 +0000942 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000943 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000944 }
945
946 Record *Op = N->getOperator();
947 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000948 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000949 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000950 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000951 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000952 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000953 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000954 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
955 : (InstPat ? InstPat->getTree(0) : NULL);
Dan Gohmanfebf71d2009-01-16 21:30:55 +0000956 if (InstPatNode && !InstPatNode->isLeaf() &&
957 InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000958 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000959 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000960 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000961 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000962 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000963 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000964 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000965 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000966 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000967 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000968 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000969 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000970 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000971 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000972 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000973 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000974 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000975 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000976
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000977 // Record output varargs info.
978 OutputIsVariadic = IsVariadic;
979
Evan Chengfceb57a2006-07-15 08:45:20 +0000980 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000981 emitCode("bool HasInFlag = "
Owen Anderson825b72b2009-08-11 20:47:22 +0000982 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000983 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000984 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000985 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000986
Evan Cheng823b7522006-01-19 21:57:10 +0000987 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000988 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000989 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000990 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
991 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000992 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000993 }
994
Evan Cheng4326ef52006-10-12 02:08:53 +0000995 if (OrigChains.size() > 0) {
996 // The original input chain is being ignored. If it is not just
997 // pointing to the op that's being folded, we should create a
998 // TokenFactor with it and the chain of the folded op as the new chain.
999 // We could potentially be doing multiple levels of folding, in that
1000 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +00001001 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +00001002 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001003 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
1004 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +00001005 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
1006 emitCode("}");
1007 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001008 emitCode("InChains.push_back(" + ChainName + ");");
Dale Johannesened2eee62009-02-06 01:31:28 +00001009 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, "
Owen Anderson825b72b2009-08-11 20:47:22 +00001010 "N.getDebugLoc(), MVT::Other, "
Evan Cheng4326ef52006-10-12 02:08:53 +00001011 "&InChains[0], InChains.size());");
David Greene8ad4c002008-10-27 21:56:29 +00001012 if (GenDebug) {
1013 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
1014 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
1015 }
Evan Cheng4326ef52006-10-12 02:08:53 +00001016 }
1017
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001018 // Loop over all of the operands of the instruction pattern, emitting code
1019 // to fill them all in. The node 'N' usually has number children equal to
1020 // the number of input operands of the instruction. However, in cases
1021 // where there are predicate operands for an instruction, we need to fill
1022 // in the 'execute always' values. Match up the node operands to the
1023 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +00001024 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001025 for (unsigned ChildNo = 0, InstOpNo = NumResults;
1026 InstOpNo != II.OperandList.size(); ++InstOpNo) {
1027 std::vector<std::string> Ops;
1028
Dan Gohmand35121a2008-05-29 19:57:41 +00001029 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +00001030 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001031 if ((OperandNode->isSubClassOf("PredicateOperand") ||
1032 OperandNode->isSubClassOf("OptionalDefOperand")) &&
1033 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +00001034 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +00001035 // 'default ops' operands.
1036 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001037 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001038 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001039 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001040 InFlagDecled, ResNodeDecled);
1041 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1042 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001043 } else {
1044 // Otherwise this is a normal operand or a predicate operand without
1045 // 'execute always'; emit it.
1046 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1047 InFlagDecled, ResNodeDecled);
1048 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1049 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001050 }
Evan Chengb915f312005-12-09 22:45:35 +00001051 }
1052
Evan Chengb915f312005-12-09 22:45:35 +00001053 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001054 bool ChainEmitted = NodeHasChain;
Dale Johannesen874ae252009-06-02 03:12:52 +00001055 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001056 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1057 InFlagDecled, ResNodeDecled, true);
Dale Johannesen874ae252009-06-02 03:12:52 +00001058 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001059 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001060 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001061 InFlagDecled = true;
1062 }
Evan Chengf037ca62006-08-27 08:11:28 +00001063 if (NodeHasOptInFlag) {
1064 emitCode("if (HasInFlag) {");
1065 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
Evan Chengf037ca62006-08-27 08:11:28 +00001066 emitCode("}");
1067 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001068 }
Evan Chengb915f312005-12-09 22:45:35 +00001069
Evan Chengb915f312005-12-09 22:45:35 +00001070 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001071
Dan Gohman95d11092008-07-07 21:00:17 +00001072 unsigned OpsNo = OpcNo;
1073 std::string CodePrefix;
1074 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1075 std::deque<std::string> After;
1076 std::string NodeName;
1077 if (!isRoot) {
1078 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001079 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001080 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001081 NodeName = "ResNode";
1082 if (!ResNodeDecled) {
1083 CodePrefix = "SDNode *" + NodeName + " = ";
1084 ResNodeDecled = true;
1085 } else
1086 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001087 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001088
Dan Gohman95d11092008-07-07 21:00:17 +00001089 std::string Code = "Opc" + utostr(OpcNo);
1090
Bill Wendling6e1bb382009-01-29 05:27:31 +00001091 if (!isRoot || (InputHasChain && !NodeHasChain))
Dan Gohman602b0c82009-09-25 18:54:59 +00001092 // For call to "getMachineNode()".
Bill Wendling6e1bb382009-01-29 05:27:31 +00001093 Code += ", N.getDebugLoc()";
1094
Dan Gohman95d11092008-07-07 21:00:17 +00001095 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1096
1097 // Output order: results, chain, flags
1098 // Result types.
Owen Anderson825b72b2009-08-11 20:47:22 +00001099 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
Dan Gohman95d11092008-07-07 21:00:17 +00001100 Code += ", VT" + utostr(VTNo);
1101 emitVT(getEnumName(N->getTypeNum(0)));
1102 }
1103 // Add types for implicit results in physical registers, scheduler will
1104 // care of adding copyfromreg nodes.
1105 for (unsigned i = 0; i < NumDstRegs; i++) {
1106 Record *RR = DstRegs[i];
1107 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001108 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Dan Gohman95d11092008-07-07 21:00:17 +00001109 Code += ", " + getEnumName(RVT);
1110 }
1111 }
1112 if (NodeHasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00001113 Code += ", MVT::Other";
Dale Johannesen874ae252009-06-02 03:12:52 +00001114 if (NodeHasOutFlag)
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 Code += ", MVT::Flag";
Dan Gohman95d11092008-07-07 21:00:17 +00001116
1117 // Inputs.
1118 if (IsVariadic) {
1119 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1120 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1121 AllOps.clear();
1122
1123 // Figure out whether any operands at the end of the op list are not
1124 // part of the variable section.
1125 std::string EndAdjust;
1126 if (NodeHasInFlag || HasImpInputs)
1127 EndAdjust = "-1"; // Always has one flag.
1128 else if (NodeHasOptInFlag)
1129 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1130
1131 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1132 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1133
Dan Gohman95d11092008-07-07 21:00:17 +00001134 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1135 emitCode("}");
1136 }
1137
Dan Gohmanc76909a2009-09-25 20:36:54 +00001138 // Populate MemRefs with entries for each memory accesses covered by
Dan Gohman95d11092008-07-07 21:00:17 +00001139 // this pattern.
Dan Gohmanc76909a2009-09-25 20:36:54 +00001140 if (isRoot && !LSI.empty()) {
1141 std::string MemRefs = "MemRefs" + utostr(OpsNo);
1142 emitCode("MachineSDNode::mmo_iterator " + MemRefs + " = "
1143 "MF->allocateMemRefsArray(" + utostr(LSI.size()) + ");");
1144 for (unsigned i = 0, e = LSI.size(); i != e; ++i)
1145 emitCode(MemRefs + "[" + utostr(i) + "] = "
1146 "cast<MemSDNode>(" + LSI[i] + ")->getMemOperand();");
1147 After.push_back("cast<MachineSDNode>(ResNode)->setMemRefs(" +
1148 MemRefs + ", " + MemRefs + " + " + utostr(LSI.size()) +
1149 ");");
Dan Gohman95d11092008-07-07 21:00:17 +00001150 }
1151
1152 if (NodeHasChain) {
1153 if (IsVariadic)
1154 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1155 else
1156 AllOps.push_back(ChainName);
1157 }
1158
1159 if (IsVariadic) {
1160 if (NodeHasInFlag || HasImpInputs)
1161 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1162 else if (NodeHasOptInFlag) {
1163 emitCode("if (HasInFlag)");
1164 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1165 }
1166 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1167 ".size()";
Dale Johannesen874ae252009-06-02 03:12:52 +00001168 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Dan Gohman95d11092008-07-07 21:00:17 +00001169 AllOps.push_back("InFlag");
1170
1171 unsigned NumOps = AllOps.size();
1172 if (NumOps) {
1173 if (!NodeHasOptInFlag && NumOps < 4) {
1174 for (unsigned i = 0; i != NumOps; ++i)
1175 Code += ", " + AllOps[i];
1176 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001177 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001178 for (unsigned i = 0; i != NumOps; ++i) {
1179 OpsCode += AllOps[i];
1180 if (i != NumOps-1)
1181 OpsCode += ", ";
1182 }
1183 emitCode(OpsCode + " };");
1184 Code += ", Ops" + utostr(OpsNo) + ", ";
1185 if (NodeHasOptInFlag) {
1186 Code += "HasInFlag ? ";
1187 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1188 } else
1189 Code += utostr(NumOps);
1190 }
1191 }
1192
1193 if (!isRoot)
1194 Code += "), 0";
1195
Dan Gohmane8be6c62008-07-17 19:10:17 +00001196 std::vector<std::string> ReplaceFroms;
1197 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001198 if (!isRoot) {
1199 NodeOps.push_back("Tmp" + utostr(ResNo));
1200 } else {
1201
Dale Johannesen874ae252009-06-02 03:12:52 +00001202 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001203 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001204 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001205 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1206 ");");
1207 InFlagDecled = true;
1208 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001209 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001210 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1211 ");");
1212 }
1213
Dan Gohman1eb49a02009-01-05 19:31:28 +00001214 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
1215 ReplaceFroms.push_back("SDValue(" +
1216 FoldedChains[j].first + ".getNode(), " +
1217 utostr(FoldedChains[j].second) +
1218 ")");
1219 ReplaceTos.push_back("SDValue(ResNode, " +
1220 utostr(NumResults+NumDstRegs) + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001221 }
1222
Dale Johannesen874ae252009-06-02 03:12:52 +00001223 if (NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001224 if (FoldedFlag.first != "") {
Dale Johannesen874ae252009-06-02 03:12:52 +00001225 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001226 utostr(FoldedFlag.second) + ")");
1227 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001228 } else {
Dale Johannesen874ae252009-06-02 03:12:52 +00001229 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001230 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001231 utostr(NumPatResults + (unsigned)InputHasChain)
1232 + ")");
1233 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001234 }
Dan Gohman95d11092008-07-07 21:00:17 +00001235 }
1236
Dan Gohmane8be6c62008-07-17 19:10:17 +00001237 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001238 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001239 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001240 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001241 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001242 ChainAssignmentNeeded |= NodeHasChain;
1243 }
1244
1245 // User does not expect the instruction would produce a chain!
Dale Johannesen874ae252009-06-02 03:12:52 +00001246 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Dan Gohman95d11092008-07-07 21:00:17 +00001247 ;
1248 } else if (InputHasChain && !NodeHasChain) {
1249 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001250 if (NodeHasOutFlag) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001251 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001252 utostr(NumPatResults+1) +
1253 ")");
Gabor Greif99a6cb92008-08-26 22:36:50 +00001254 ReplaceTos.push_back("SDValue(ResNode, N.getResNo()-1)");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001255 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001256 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001257 utostr(NumPatResults) + ")");
1258 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001259 }
1260 }
1261
1262 if (ChainAssignmentNeeded) {
1263 // Remember which op produces the chain.
1264 std::string ChainAssign;
1265 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001266 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001267 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001268 else
Dan Gohman475871a2008-07-27 21:46:04 +00001269 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001270 ", " + utostr(NumResults+NumDstRegs) + ");";
1271
1272 After.push_front(ChainAssign);
1273 }
1274
Dan Gohmane8be6c62008-07-17 19:10:17 +00001275 if (ReplaceFroms.size() == 1) {
1276 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1277 ReplaceTos[0] + ");");
1278 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001279 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001280 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1281 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1282 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001283 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001284 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1285 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1286 After.push_back("};");
1287 After.push_back("ReplaceUses(Froms, Tos, " +
1288 itostr(ReplaceFroms.size()) + ");");
1289 }
1290
1291 // We prefer to use SelectNodeTo since it avoids allocation when
1292 // possible and it avoids CSE map recalculation for the node's
1293 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001294 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001295 // We also don't use if the pattern replacement is being used to
1296 // jettison a chain result, since morphing the node in place
1297 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001298 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001299 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001300 Code = "CurDAG->getMachineNode(" + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001301 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001302 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001303 }
1304 if (isRoot) {
1305 if (After.empty())
1306 CodePrefix = "return ";
1307 else
1308 After.push_back("return ResNode;");
1309 }
1310
1311 emitCode(CodePrefix + Code + ");");
David Greene8ad4c002008-10-27 21:56:29 +00001312
1313 if (GenDebug) {
1314 if (!isRoot) {
1315 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
1316 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
1317 }
1318 else {
1319 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
1320 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
1321 }
1322 }
1323
Dan Gohman95d11092008-07-07 21:00:17 +00001324 for (unsigned i = 0, e = After.size(); i != e; ++i)
1325 emitCode(After[i]);
1326
Evan Cheng676d7312006-08-26 00:59:04 +00001327 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001328 }
1329 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001330 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001331 // PatLeaf node - the operand may or may not be a leaf node. But it should
1332 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001333 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001334 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001335 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001336 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001337 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001338 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001339 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001340 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001341 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001342 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001343 }
Dan Gohman0540e172008-10-15 06:17:21 +00001344
1345 N->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001346 errs() << "\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001347 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001348 }
1349
Chris Lattner488580c2006-01-28 19:06:51 +00001350 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1351 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001352 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1353 /// for, this returns true otherwise false if Pat has all types.
1354 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001355 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001356 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001357 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001358 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001359 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001360 // The top level node type is checked outside of the select function.
1361 if (!isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001362 emitCheck(Prefix + ".getNode()->getValueType(0) == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001363 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001364 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001365 }
1366
Evan Cheng51fecc82006-01-09 18:27:06 +00001367 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001368 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001369 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1370 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1371 Prefix + utostr(OpNo)))
1372 return true;
1373 return false;
1374 }
1375
1376private:
Evan Cheng54597732006-01-26 00:22:25 +00001377 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001378 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001379 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001380 bool &ChainEmitted, bool &InFlagDecled,
1381 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001382 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001383 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001384 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1385 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001386 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1387 TreePatternNode *Child = N->getChild(i);
1388 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001389 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1390 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001391 } else {
1392 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001393 if (!Child->getName().empty()) {
1394 std::string Name = RootName + utostr(OpNo);
1395 if (Duplicates.find(Name) != Duplicates.end())
1396 // A duplicate! Do not emit a copy for this node.
1397 continue;
1398 }
1399
Evan Chengb915f312005-12-09 22:45:35 +00001400 Record *RR = DI->getDef();
1401 if (RR->isSubClassOf("Register")) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001402 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
1403 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001404 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001405 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001406 InFlagDecled = true;
1407 } else
1408 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
Evan Chengb2c6d492006-01-11 22:16:13 +00001409 } else {
1410 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001411 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001412 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001413 ChainEmitted = true;
1414 }
Evan Cheng676d7312006-08-26 00:59:04 +00001415 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001416 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001417 InFlagDecled = true;
1418 }
Dale Johannesen874ae252009-06-02 03:12:52 +00001419 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1420 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Dale Johannesena05dca42009-02-04 23:02:30 +00001421 ", " + RootName + ".getDebugLoc()" +
Chris Lattner6cefb772008-01-05 22:25:12 +00001422 ", " + getQualifiedName(RR) +
Dale Johannesen874ae252009-06-02 03:12:52 +00001423 ", " + RootName + utostr(OpNo) + ", InFlag).getNode();");
1424 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001425 emitCode(ChainName + " = SDValue(ResNode, 0);");
1426 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001427 }
1428 }
1429 }
1430 }
1431 }
Evan Cheng54597732006-01-26 00:22:25 +00001432
Dale Johannesen874ae252009-06-02 03:12:52 +00001433 if (HasInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001434 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001435 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001436 ".getOperand(" + utostr(OpNo) + ");");
1437 InFlagDecled = true;
1438 } else
1439 emitCode("InFlag = " + RootName +
1440 ".getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001441 }
Evan Chengb915f312005-12-09 22:45:35 +00001442 }
1443};
1444
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001445/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1446/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001447/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001448void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001449 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001450 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001451 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001452 std::vector<std::string> &TargetVTs,
1453 bool &OutputIsVariadic,
1454 unsigned &NumInputRootOps) {
1455 OutputIsVariadic = false;
1456 NumInputRootOps = 0;
1457
Dan Gohman22bb3112008-08-22 00:20:26 +00001458 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001459 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001460 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001461 TargetOpcodes, TargetVTs,
1462 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001463
Chris Lattner8fc35682005-09-23 23:16:51 +00001464 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001465 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001466 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001467
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001468 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001469 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001470
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001471 // At this point, we know that we structurally match the pattern, but the
1472 // types of the nodes may not match. Figure out the fewest number of type
1473 // comparisons we need to emit. For example, if there is only one integer
1474 // type supported by a target, there should be no type comparisons at all for
1475 // integer patterns!
1476 //
1477 // To figure out the fewest number of type checks needed, clone the pattern,
1478 // remove the types, then perform type inference on the pattern as a whole.
1479 // If there are unresolved types, emit an explicit check for those types,
1480 // apply the type to the tree, then rerun type inference. Iterate until all
1481 // types are resolved.
1482 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001483 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001484 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001485
1486 do {
1487 // Resolve/propagate as many types as possible.
1488 try {
1489 bool MadeChange = true;
1490 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001491 MadeChange = Pat->ApplyTypeConstraints(TP,
1492 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001493 } catch (...) {
1494 assert(0 && "Error: could not find consistent types for something we"
1495 " already decided was ok!");
1496 abort();
1497 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001498
Chris Lattner7e82f132005-10-15 21:34:21 +00001499 // Insert a check for an unresolved type and add it to the tree. If we find
1500 // an unresolved type to add a check for, this returns true and we iterate,
1501 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001502 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001503
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001504 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001505 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001506 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001507}
1508
Chris Lattner24e00a42006-01-29 04:41:05 +00001509/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1510/// a line causes any of them to be empty, remove them and return true when
1511/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001512static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001513 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001514 &Patterns) {
1515 bool ErasedPatterns = false;
1516 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1517 Patterns[i].second.pop_back();
1518 if (Patterns[i].second.empty()) {
1519 Patterns.erase(Patterns.begin()+i);
1520 --i; --e;
1521 ErasedPatterns = true;
1522 }
1523 }
1524 return ErasedPatterns;
1525}
1526
Chris Lattner8bc74722006-01-29 04:25:26 +00001527/// EmitPatterns - Emit code for at least one pattern, but try to group common
1528/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001529void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001530 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001531 &Patterns, unsigned Indent,
Daniel Dunbar1a551802009-07-03 00:10:29 +00001532 raw_ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001533 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001534 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001535 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001536
1537 if (Patterns.empty()) return;
1538
Chris Lattner24e00a42006-01-29 04:41:05 +00001539 // Figure out how many patterns share the next code line. Explicitly copy
1540 // FirstCodeLine so that we don't invalidate a reference when changing
1541 // Patterns.
1542 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001543 unsigned LastMatch = Patterns.size()-1;
1544 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1545 --LastMatch;
1546
1547 // If not all patterns share this line, split the list into two pieces. The
1548 // first chunk will use this line, the second chunk won't.
1549 if (LastMatch != 0) {
1550 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1551 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1552
1553 // FIXME: Emit braces?
1554 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001555 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001556 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1557 Pattern.getSrcPattern()->print(OS);
1558 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1559 Pattern.getDstPattern()->print(OS);
1560 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001561 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001562 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001563 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001564 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001565 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001566 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001567 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001568 }
Evan Cheng676d7312006-08-26 00:59:04 +00001569 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001570 OS << std::string(Indent, ' ') << "{\n";
1571 Indent += 2;
1572 }
1573 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001574 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001575 Indent -= 2;
1576 OS << std::string(Indent, ' ') << "}\n";
1577 }
1578
1579 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001580 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001581 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1582 Pattern.getSrcPattern()->print(OS);
1583 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1584 Pattern.getDstPattern()->print(OS);
1585 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001586 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001587 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001588 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001589 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001590 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001591 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001592 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001593 }
1594 EmitPatterns(Other, Indent, OS);
1595 return;
1596 }
1597
Chris Lattner24e00a42006-01-29 04:41:05 +00001598 // Remove this code from all of the patterns that share it.
1599 bool ErasedPatterns = EraseCodeLine(Patterns);
1600
Evan Cheng676d7312006-08-26 00:59:04 +00001601 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001602
1603 // Otherwise, every pattern in the list has this line. Emit it.
1604 if (!isPredicate) {
1605 // Normal code.
1606 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1607 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001608 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1609
1610 // If the next code line is another predicate, and if all of the pattern
1611 // in this group share the same next line, emit it inline now. Do this
1612 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001613 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Jim Grosbachda4231f2009-03-26 16:17:51 +00001614 // Check that all of the patterns in Patterns end with the same predicate.
Chris Lattner24e00a42006-01-29 04:41:05 +00001615 bool AllEndWithSamePredicate = true;
1616 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1617 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1618 AllEndWithSamePredicate = false;
1619 break;
1620 }
1621 // If all of the predicates aren't the same, we can't share them.
1622 if (!AllEndWithSamePredicate) break;
1623
1624 // Otherwise we can. Emit it shared now.
1625 OS << " &&\n" << std::string(Indent+4, ' ')
1626 << Patterns.back().second.back().second;
1627 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001628 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001629
1630 OS << ") {\n";
1631 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001632 }
1633
1634 EmitPatterns(Patterns, Indent, OS);
1635
1636 if (isPredicate)
1637 OS << std::string(Indent-2, ' ') << "}\n";
1638}
1639
Evan Cheng892aaf82006-11-08 23:01:03 +00001640static std::string getLegalCName(std::string OpName) {
1641 std::string::size_type pos = OpName.find("::");
1642 if (pos != std::string::npos)
1643 OpName.replace(pos, 2, "_");
1644 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001645}
1646
Daniel Dunbar1a551802009-07-03 00:10:29 +00001647void DAGISelEmitter::EmitInstructionSelector(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001648 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001649
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001650 // Get the namespace to insert instructions into.
1651 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001652 if (!InstNS.empty()) InstNS += "::";
1653
Chris Lattner602f6922006-01-04 00:25:00 +00001654 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001655 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001656 // All unique target node emission functions.
1657 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001658 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001659 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001660 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001661
1662 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001663 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001664 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001665 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001666 } else {
1667 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001668 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001669 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001670 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001671 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001672 std::vector<Record*> OpNodes = CP->getRootNodes();
1673 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001674 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1675 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001676 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001677 }
1678 } else {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001679 errs() << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001680 Node->dump();
Daniel Dunbar1a551802009-07-03 00:10:29 +00001681 errs() << "' on tree pattern '";
1682 errs() << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001683 exit(1);
1684 }
1685 }
1686 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001687
1688 // For each opcode, there might be multiple select functions, one per
1689 // ValueType of the node (or its first operand if it doesn't produce a
1690 // non-chain result.
1691 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1692
Chris Lattner602f6922006-01-04 00:25:00 +00001693 // Emit one Select_* method for each top-level opcode. We do this instead of
1694 // emitting one giant switch statement to support compilers where this will
1695 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001696 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001697 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1698 PBOI != E; ++PBOI) {
1699 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001700 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001701 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1702
Chris Lattner706d2d32006-08-09 16:44:44 +00001703 // Split them into groups by type.
Owen Anderson825b72b2009-08-11 20:47:22 +00001704 std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001705 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001706 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001707 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001708 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001709 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001710 }
1711
Owen Anderson825b72b2009-08-11 20:47:22 +00001712 for (std::map<MVT::SimpleValueType,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001713 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001714 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1715 ++II) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001716 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001717 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001718 typedef std::pair<unsigned, std::string> CodeLine;
1719 typedef std::vector<CodeLine> CodeList;
1720 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001721
Chris Lattner60d81392008-01-05 22:30:17 +00001722 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001723 std::vector<std::vector<std::string> > PatternOpcodes;
1724 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001725 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001726 std::vector<bool> OutputIsVariadicFlags;
1727 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001728 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1729 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001730 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001731 std::vector<std::string> TargetOpcodes;
1732 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001733 bool OutputIsVariadic;
1734 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001735 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001736 TargetOpcodes, TargetVTs,
1737 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001738 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1739 PatternDecls.push_back(GeneratedDecl);
1740 PatternOpcodes.push_back(TargetOpcodes);
1741 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001742 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1743 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001744 }
1745
Chris Lattner706d2d32006-08-09 16:44:44 +00001746 // Factor target node emission code (emitted by EmitResultCode) into
1747 // separate functions. Uniquing and share them among all instruction
1748 // selection routines.
1749 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1750 CodeList &GeneratedCode = CodeForPatterns[i].second;
1751 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1752 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001753 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001754 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1755 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001756 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001757 int CodeSize = (int)GeneratedCode.size();
1758 int LastPred = -1;
1759 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001760 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001761 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001762 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1763 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001764 }
1765
Dan Gohman475871a2008-07-27 21:46:04 +00001766 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001767 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001768 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1769 CalleeCode += ", unsigned Opc" + utostr(j);
1770 CallerCode += ", " + TargetOpcodes[j];
1771 }
1772 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Owen Anderson69110c92009-09-11 09:01:57 +00001773 CalleeCode += ", MVT::SimpleValueType VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001774 CallerCode += ", " + TargetVTs[j];
1775 }
Evan Chengf5493192006-08-26 01:02:19 +00001776 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001777 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001778 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001779 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001780 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001781 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001782
1783 if (OutputIsVariadic) {
1784 CalleeCode += ", unsigned NumInputRootOps";
1785 CallerCode += ", " + utostr(NumInputRootOps);
1786 }
1787
Chris Lattner706d2d32006-08-09 16:44:44 +00001788 CallerCode += ");";
1789 CalleeCode += ") ";
1790 // Prevent emission routines from being inlined to reduce selection
1791 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001792 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001793 CalleeCode += "{\n";
1794
1795 for (std::vector<std::string>::const_reverse_iterator
1796 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1797 CalleeCode += " " + *I + "\n";
1798
Evan Chengf5493192006-08-26 01:02:19 +00001799 for (int j = LastPred+1; j < CodeSize; ++j)
1800 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001801 for (int j = LastPred+1; j < CodeSize; ++j)
1802 GeneratedCode.pop_back();
1803 CalleeCode += "}\n";
1804
1805 // Uniquing the emission routines.
1806 unsigned EmitFuncNum;
1807 std::map<std::string, unsigned>::iterator EFI =
1808 EmitFunctions.find(CalleeCode);
1809 if (EFI != EmitFunctions.end()) {
1810 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001811 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001812 EmitFuncNum = EmitFunctions.size();
1813 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001814 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001815 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001816
Chris Lattner706d2d32006-08-09 16:44:44 +00001817 // Replace the emission code within selection routines with calls to the
1818 // emission functions.
David Greene8ad4c002008-10-27 21:56:29 +00001819 if (GenDebug) {
1820 GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"red\");"));
1821 }
1822 CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
1823 GeneratedCode.push_back(std::make_pair(3, CallerCode));
1824 if (GenDebug) {
1825 GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
1826 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"yellow\");"));
1827 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"black\");"));
1828 GeneratedCode.push_back(std::make_pair(0, "}"));
1829 //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"black\");"));
1830 }
1831 GeneratedCode.push_back(std::make_pair(0, "return Result;"));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001832 }
1833
Chris Lattner706d2d32006-08-09 16:44:44 +00001834 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001835 std::string OpVTStr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001836 if (OpVT == MVT::iPTR) {
Chris Lattner33a40042006-11-14 22:17:10 +00001837 OpVTStr = "_iPTR";
Owen Anderson825b72b2009-08-11 20:47:22 +00001838 } else if (OpVT == MVT::iPTRAny) {
Mon P Wange3b3a722008-07-30 04:36:53 +00001839 OpVTStr = "_iPTRAny";
Owen Anderson825b72b2009-08-11 20:47:22 +00001840 } else if (OpVT == MVT::isVoid) {
Chris Lattner33a40042006-11-14 22:17:10 +00001841 // Nodes with a void result actually have a first result type of either
1842 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1843 // void to this case, we handle it specially here.
1844 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001845 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
Chris Lattner33a40042006-11-14 22:17:10 +00001846 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001847 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1848 OpcodeVTMap.find(OpName);
1849 if (OpVTI == OpcodeVTMap.end()) {
1850 std::vector<std::string> VTSet;
1851 VTSet.push_back(OpVTStr);
1852 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1853 } else
1854 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001855
Dan Gohman0540e172008-10-15 06:17:21 +00001856 // We want to emit all of the matching code now. However, we want to emit
1857 // the matches in order of minimal cost. Sort the patterns so the least
1858 // cost one is at the start.
1859 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1860 PatternSortingPredicate(CGP));
1861
1862 // Scan the code to see if all of the patterns are reachable and if it is
1863 // possible that the last one might not match.
1864 bool mightNotMatch = true;
1865 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1866 CodeList &GeneratedCode = CodeForPatterns[i].second;
1867 mightNotMatch = false;
1868
1869 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1870 if (GeneratedCode[j].first == 1) { // predicate.
1871 mightNotMatch = true;
1872 break;
1873 }
1874 }
1875
1876 // If this pattern definitely matches, and if it isn't the last one, the
1877 // patterns after it CANNOT ever match. Error out.
1878 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Daniel Dunbar1a551802009-07-03 00:10:29 +00001879 errs() << "Pattern '";
1880 CodeForPatterns[i].first->getSrcPattern()->print(errs());
1881 errs() << "' is impossible to select!\n";
Dan Gohman0540e172008-10-15 06:17:21 +00001882 exit(1);
1883 }
1884 }
1885
Chris Lattner706d2d32006-08-09 16:44:44 +00001886 // Loop through and reverse all of the CodeList vectors, as we will be
1887 // accessing them from their logical front, but accessing the end of a
1888 // vector is more efficient.
1889 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1890 CodeList &GeneratedCode = CodeForPatterns[i].second;
1891 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001892 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001893
1894 // Next, reverse the list of patterns itself for the same reason.
1895 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1896
Dan Gohman63e3e632009-01-29 01:37:18 +00001897 OS << "SDNode *Select_" << getLegalCName(OpName)
1898 << OpVTStr << "(const SDValue &N) {\n";
1899
Chris Lattner706d2d32006-08-09 16:44:44 +00001900 // Emit all of the patterns now, grouped together to share code.
1901 EmitPatterns(CodeForPatterns, 2, OS);
1902
Chris Lattner64906972006-09-21 18:28:27 +00001903 // If the last pattern has predicates (which could fail) emit code to
1904 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001905 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001906 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001907 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1908 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001909 OpName != "ISD::INTRINSIC_VOID")
1910 OS << " CannotYetSelect(N);\n";
1911 else
1912 OS << " CannotYetSelectIntrinsic(N);\n";
1913
1914 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001915 }
1916 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001917 }
Chris Lattner602f6922006-01-04 00:25:00 +00001918 }
1919
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001920 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001921 << "SDNode *SelectCode(SDValue N) {\n"
Owen Anderson825b72b2009-08-11 20:47:22 +00001922 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT().SimpleTy;\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001923 << " switch (N.getOpcode()) {\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00001924 << " default:\n"
1925 << " assert(!N.isMachineOpcode() && \"Node already selected!\");\n"
1926 << " break;\n"
1927 << " case ISD::EntryToken: // These nodes remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001928 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001929 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001930 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001931 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001932 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001933 << " case ISD::TargetConstantPool:\n"
1934 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00001935 << " case ISD::TargetExternalSymbol:\n"
Dan Gohman8c2b5252009-10-30 01:27:03 +00001936 << " case ISD::TargetBlockAddress:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001937 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001938 << " case ISD::TargetGlobalTLSAddress:\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001939 << " case ISD::TargetGlobalAddress:\n"
1940 << " case ISD::TokenFactor:\n"
1941 << " case ISD::CopyFromReg:\n"
1942 << " case ISD::CopyToReg: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001943 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001944 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001945 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001946 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001947 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001948 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001949 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001950 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001951 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
1952 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001953 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001954
Chris Lattner602f6922006-01-04 00:25:00 +00001955 // Loop over all of the case statements, emiting a call to each method we
1956 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001957 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001958 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1959 PBOI != E; ++PBOI) {
1960 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001961 // Potentially multiple versions of select for this opcode. One for each
1962 // ValueType of the node (or its first true operand if it doesn't produce a
1963 // result.
1964 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1965 OpcodeVTMap.find(OpName);
1966 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00001967 OS << " case " << OpName << ": {\n";
Dale Johannesen3b895cf2009-05-12 22:32:29 +00001968 // If we have only one variant and it's the default, elide the
1969 // switch. Marginally faster, and makes MSVC happier.
1970 if (OpVTs.size()==1 && OpVTs[0].empty()) {
1971 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
1972 OS << " break;\n";
1973 OS << " }\n";
1974 continue;
1975 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001976 // Keep track of whether we see a pattern that has an iPtr result.
1977 bool HasPtrPattern = false;
1978 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00001979
Evan Cheng425e8c72007-09-04 20:18:28 +00001980 OS << " switch (NVT) {\n";
1981 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1982 std::string &VTStr = OpVTs[i];
1983 if (VTStr.empty()) {
1984 HasDefaultPattern = true;
1985 continue;
1986 }
Chris Lattner717a6112006-11-14 21:50:27 +00001987
Evan Cheng425e8c72007-09-04 20:18:28 +00001988 // If this is a match on iPTR: don't emit it directly, we need special
1989 // code.
1990 if (VTStr == "_iPTR") {
1991 HasPtrPattern = true;
1992 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00001993 }
Owen Anderson825b72b2009-08-11 20:47:22 +00001994 OS << " case MVT::" << VTStr.substr(1) << ":\n"
Evan Cheng425e8c72007-09-04 20:18:28 +00001995 << " return Select_" << getLegalCName(OpName)
1996 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001997 }
Evan Cheng425e8c72007-09-04 20:18:28 +00001998 OS << " default:\n";
1999
2000 // If there is an iPTR result version of this pattern, emit it here.
2001 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002002 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002003 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2004 }
2005 if (HasDefaultPattern) {
2006 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2007 }
2008 OS << " break;\n";
2009 OS << " }\n";
2010 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002011 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002012 }
Chris Lattner81303322005-09-23 19:36:15 +00002013
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002014 OS << " } // end of big switch.\n\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002015 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2016 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2017 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002018 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002019 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002020 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002021 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002022 << " return NULL;\n"
2023 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002024}
2025
Daniel Dunbar1a551802009-07-03 00:10:29 +00002026void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002027 EmitSourceFileHeader("DAG Instruction Selector for the " +
2028 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002029
Chris Lattner1f39e292005-09-14 00:09:24 +00002030 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2031 << "// *** instruction selector class. These functions are really "
2032 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002033
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002034 OS << "// Include standard, target-independent definitions and methods used\n"
2035 << "// by the instruction selector.\n";
Mike Stumpfe095f32009-05-04 18:40:41 +00002036 OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002037
Chris Lattner443e3f92008-01-05 22:54:53 +00002038 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002039 EmitPredicateFunctions(OS);
2040
Chris Lattner569f1212009-08-23 04:44:11 +00002041 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n");
Chris Lattnerfe718932008-01-06 01:10:31 +00002042 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002043 I != E; ++I) {
Chris Lattner569f1212009-08-23 04:44:11 +00002044 DEBUG(errs() << "PATTERN: "; I->getSrcPattern()->dump());
2045 DEBUG(errs() << "\nRESULT: "; I->getDstPattern()->dump());
2046 DEBUG(errs() << "\n");
Bill Wendlingf5da1332006-12-07 22:21:48 +00002047 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002048
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002049 // At this point, we have full information about the 'Patterns' we need to
2050 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002051 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002052 EmitInstructionSelector(OS);
2053
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002054}