blob: 3674a136c9cef5251aa55b6f79b3544482b19b88 [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
15#include "Record.h"
16#include "llvm/ADT/StringExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000017#include "llvm/Support/CommandLine.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000018#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000019#include "llvm/Support/MathExtras.h"
David Greene8ad4c002008-10-27 21:56:29 +000020#include "llvm/Support/Debug.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000021#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000022#include <algorithm>
Dan Gohman95d11092008-07-07 21:00:17 +000023#include <deque>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024using namespace llvm;
25
David Greene8ad4c002008-10-27 21:56:29 +000026namespace {
27 cl::opt<bool>
28 GenDebug("gen-debug", cl::desc("Generate debug code"),
29 cl::init(false));
30}
31
Chris Lattnerca559d02005-09-08 21:03:01 +000032//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000033// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000034//
35
Chris Lattner6cefb772008-01-05 22:25:12 +000036/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
37/// ComplexPattern.
38static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000039 return (N->isLeaf() &&
40 dynamic_cast<DefInit*>(N->getLeafValue()) &&
41 static_cast<DefInit*>(N->getLeafValue())->getDef()->
42 isSubClassOf("ComplexPattern"));
43}
44
Chris Lattner6cefb772008-01-05 22:25:12 +000045/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
46/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000047static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000048 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000049 if (N->isLeaf() &&
50 dynamic_cast<DefInit*>(N->getLeafValue()) &&
51 static_cast<DefInit*>(N->getLeafValue())->getDef()->
52 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000053 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
54 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000055 }
56 return NULL;
57}
58
Chris Lattner05814af2005-09-28 17:57:56 +000059/// getPatternSize - Return the 'size' of this pattern. We want to match large
60/// patterns before small ones. This is used to determine the size of a
61/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000062static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000063 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
64 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000065 P->getExtTypeNum(0) == MVT::isVoid ||
66 P->getExtTypeNum(0) == MVT::Flag ||
Mon P Wange3b3a722008-07-30 04:36:53 +000067 P->getExtTypeNum(0) == MVT::iPTR ||
68 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000069 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000070 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000071 // If the root node is a ConstantSDNode, increases its size.
72 // e.g. (set R32:$dst, 0).
73 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000074 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000075
76 // FIXME: This is a hack to statically increase the priority of patterns
77 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
78 // Later we can allow complexity / cost for each pattern to be (optionally)
79 // specified. To get best possible pattern match we'll need to dynamically
80 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000081 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000082 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000083 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000084
85 // If this node has some predicate function that must match, it adds to the
86 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000087 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000088 ++Size;
89
Chris Lattner05814af2005-09-28 17:57:56 +000090 // Count children in the count if they are also nodes.
91 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
92 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000093 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000094 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000095 else if (Child->isLeaf()) {
96 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000097 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000098 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000099 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +0000100 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +0000101 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +0000102 }
Chris Lattner05814af2005-09-28 17:57:56 +0000103 }
104
105 return Size;
106}
107
108/// getResultPatternCost - Compute the number of instructions for this pattern.
109/// This is a temporary hack. We should really include the instruction
110/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000111static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000112 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000113 if (P->isLeaf()) return 0;
114
Evan Chengfbad7082006-02-18 02:33:09 +0000115 unsigned Cost = 0;
116 Record *Op = P->getOperator();
117 if (Op->isSubClassOf("Instruction")) {
118 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000119 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000120 if (II.usesCustomDAGSchedInserter)
121 Cost += 10;
122 }
Chris Lattner05814af2005-09-28 17:57:56 +0000123 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000124 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000125 return Cost;
126}
127
Evan Chenge6f32032006-07-19 00:24:41 +0000128/// getResultPatternCodeSize - Compute the code size of instructions for this
129/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000130static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000131 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000132 if (P->isLeaf()) return 0;
133
134 unsigned Cost = 0;
135 Record *Op = P->getOperator();
136 if (Op->isSubClassOf("Instruction")) {
137 Cost += Op->getValueAsInt("CodeSize");
138 }
139 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000140 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000141 return Cost;
142}
143
Chris Lattner05814af2005-09-28 17:57:56 +0000144// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
145// In particular, we want to match maximal patterns first and lowest cost within
146// a particular complexity first.
147struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000148 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
149 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000150
Dan Gohman0540e172008-10-15 06:17:21 +0000151 typedef std::pair<unsigned, std::string> CodeLine;
152 typedef std::vector<CodeLine> CodeList;
153 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
154
155 bool operator()(const std::pair<const PatternToMatch*, CodeList> &LHSPair,
156 const std::pair<const PatternToMatch*, CodeList> &RHSPair) {
157 const PatternToMatch *LHS = LHSPair.first;
158 const PatternToMatch *RHS = RHSPair.first;
159
Chris Lattner6cefb772008-01-05 22:25:12 +0000160 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
161 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000162 LHSSize += LHS->getAddedComplexity();
163 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000164 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
165 if (LHSSize < RHSSize) return false;
166
167 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000168 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
169 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000170 if (LHSCost < RHSCost) return true;
171 if (LHSCost > RHSCost) return false;
172
Chris Lattner6cefb772008-01-05 22:25:12 +0000173 return getResultPatternSize(LHS->getDstPattern(), CGP) <
174 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000175 }
176};
177
Nate Begeman6510b222005-12-01 04:51:06 +0000178/// getRegisterValueType - Look up and return the first ValueType of specified
179/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000180static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000181 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
182 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000183 return MVT::Other;
184}
185
Chris Lattner72fe91c2005-09-24 00:40:24 +0000186
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000187/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
188/// type information from it.
189static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000190 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000191 if (!N->isLeaf())
192 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
193 RemoveAllTypes(N->getChild(i));
194}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000195
Evan Cheng51fecc82006-01-09 18:27:06 +0000196/// NodeHasProperty - return true if TreePatternNode has the specified
197/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000198static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000199 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000200 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000201 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000202 if (CP)
203 return CP->hasProperty(Property);
204 return false;
205 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000206 Record *Operator = N->getOperator();
207 if (!Operator->isSubClassOf("SDNode")) return false;
208
Chris Lattner6cefb772008-01-05 22:25:12 +0000209 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000210}
211
Evan Cheng94b30402006-10-11 21:02:01 +0000212static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000213 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000214 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000215 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000216
217 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
218 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000219 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000220 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000221 }
222
223 return false;
224}
225
Evan Chengf9d03182008-07-03 08:39:51 +0000226static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
227 return CGP.getSDNodeInfo(Op).getEnumName();
228}
229
230static
231bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
232 bool isStore = !N->isLeaf() &&
233 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
234 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
235 return false;
236
237 bool HasChain = false;
238 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
239 TreePatternNode *Child = N->getChild(i);
240 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
241 HasChain = true;
242 break;
243 }
244 }
245 return HasChain;
246}
247
Chris Lattnerdc32f982008-01-05 22:43:57 +0000248//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000249// Node Transformation emitter implementation.
250//
251void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
252 // Walk the pattern fragments, adding them to a map, which sorts them by
253 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000254 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000255 NXsByNameTy NXsByName;
256
Chris Lattnerfe718932008-01-06 01:10:31 +0000257 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000258 I != E; ++I)
259 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
260
261 OS << "\n// Node transformations.\n";
262
263 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
264 I != E; ++I) {
265 Record *SDNode = I->second.first;
266 std::string Code = I->second.second;
267
268 if (Code.empty()) continue; // Empty code? Skip it.
269
Chris Lattner200c57e2008-01-05 22:58:54 +0000270 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000271 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
272
Dan Gohman475871a2008-07-27 21:46:04 +0000273 OS << "inline SDValue Transform_" << I->first << "(SDNode *" << C2
Chris Lattner443e3f92008-01-05 22:54:53 +0000274 << ") {\n";
275 if (ClassName != "SDNode")
276 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
277 OS << Code << "\n}\n";
278 }
279}
280
281//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000282// Predicate emitter implementation.
283//
284
285void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
286 OS << "\n// Predicate functions.\n";
287
288 // Walk the pattern fragments, adding them to a map, which sorts them by
289 // name.
290 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
291 PFsByNameTy PFsByName;
292
Chris Lattnerfe718932008-01-06 01:10:31 +0000293 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000294 I != E; ++I)
295 PFsByName.insert(std::make_pair(I->first->getName(), *I));
296
297
298 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
299 I != E; ++I) {
300 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
301 TreePattern *P = I->second.second;
302
303 // If there is a code init for this fragment, emit the predicate code.
304 std::string Code = PatFragRecord->getValueAsCode("Predicate");
305 if (Code.empty()) continue;
306
307 if (P->getOnlyTree()->isLeaf())
308 OS << "inline bool Predicate_" << PatFragRecord->getName()
309 << "(SDNode *N) {\n";
310 else {
311 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000312 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000313 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
314
315 OS << "inline bool Predicate_" << PatFragRecord->getName()
316 << "(SDNode *" << C2 << ") {\n";
317 if (ClassName != "SDNode")
318 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
319 }
320 OS << Code << "\n}\n";
321 }
322
323 OS << "\n\n";
324}
325
326
327//===----------------------------------------------------------------------===//
328// PatternCodeEmitter implementation.
329//
Evan Chengb915f312005-12-09 22:45:35 +0000330class PatternCodeEmitter {
331private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000332 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000333
Evan Cheng58e84a62005-12-14 22:02:59 +0000334 // Predicates.
Dan Gohman22bb3112008-08-22 00:20:26 +0000335 std::string PredicateCheck;
Evan Cheng59413202006-04-19 18:07:24 +0000336 // Pattern cost.
337 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000338 // Instruction selector pattern.
339 TreePatternNode *Pattern;
340 // Matched instruction.
341 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000342
Evan Chengb915f312005-12-09 22:45:35 +0000343 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000344 std::map<std::string, std::string> VariableMap;
345 // Node to operator mapping
346 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000347 // Name of the folded node which produces a flag.
348 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000349 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000350 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000351 // Original input chain(s).
352 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000353 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000354
Dan Gohman69de1932008-02-06 22:27:42 +0000355 /// LSI - Load/Store information.
356 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
357 /// for each memory access. This facilitates the use of AliasAnalysis in
358 /// the backend.
359 std::vector<std::string> LSI;
360
Evan Cheng676d7312006-08-26 00:59:04 +0000361 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000362 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000363 /// tested, and if true, the match fails) [when 1], or normal code to emit
364 /// [when 0], or initialization code to emit [when 2].
365 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Dan Gohman475871a2008-07-27 21:46:04 +0000366 /// GeneratedDecl - This is the set of all SDValue declarations needed for
Evan Cheng21ad3922006-02-07 00:37:41 +0000367 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000368 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000369 /// TargetOpcodes - The target specific opcodes used by the resulting
370 /// instructions.
371 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000372 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000373 /// OutputIsVariadic - Records whether the instruction output pattern uses
374 /// variable_ops. This requires that the Emit function be passed an
375 /// additional argument to indicate where the input varargs operands
376 /// begin.
377 bool &OutputIsVariadic;
378 /// NumInputRootOps - Records the number of operands the root node of the
379 /// input pattern has. This information is used in the generated code to
380 /// pass to Emit functions when variable_ops processing is needed.
381 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000382
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000383 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000384 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000385 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000386 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000387
388 void emitCheck(const std::string &S) {
389 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000390 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000391 }
392 void emitCode(const std::string &S) {
393 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000394 GeneratedCode.push_back(std::make_pair(0, S));
395 }
396 void emitInit(const std::string &S) {
397 if (!S.empty())
398 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000399 }
Evan Chengf5493192006-08-26 01:02:19 +0000400 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000401 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000402 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000403 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000404 void emitOpcode(const std::string &Opc) {
405 TargetOpcodes.push_back(Opc);
406 OpcNo++;
407 }
Evan Chengf8729402006-07-16 06:12:52 +0000408 void emitVT(const std::string &VT) {
409 TargetVTs.push_back(VT);
410 VTNo++;
411 }
Evan Chengb915f312005-12-09 22:45:35 +0000412public:
Dan Gohman22bb3112008-08-22 00:20:26 +0000413 PatternCodeEmitter(CodeGenDAGPatterns &cgp, std::string predcheck,
Evan Cheng58e84a62005-12-14 22:02:59 +0000414 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000415 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000416 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000417 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000418 std::vector<std::string> &tv,
419 bool &oiv,
420 unsigned &niro)
Dan Gohman22bb3112008-08-22 00:20:26 +0000421 : CGP(cgp), PredicateCheck(predcheck), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000422 GeneratedCode(gc), GeneratedDecl(gd),
423 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000424 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000425 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000426
427 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
428 /// if the match fails. At this point, we already know that the opcode for N
429 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000430 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
431 const std::string &RootName, const std::string &ChainSuffix,
432 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000433
434 // Save loads/stores matched by a pattern.
435 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000436 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000437 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000438 }
439
Evan Chenge41bf822006-02-05 06:43:12 +0000440 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000441 // Emit instruction predicates. Each predicate is just a string for now.
442 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000443 // Record input varargs info.
444 NumInputRootOps = N->getNumChildren();
445
Evan Chengf9d03182008-07-03 08:39:51 +0000446 if (DisablePatternForFastISel(N, CGP))
Dan Gohmanea9587b2008-08-13 19:55:00 +0000447 emitCheck("!Fast");
Evan Chengf9d03182008-07-03 08:39:51 +0000448
Chris Lattner8a0604b2006-01-28 20:31:24 +0000449 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000450 }
451
Evan Chengb915f312005-12-09 22:45:35 +0000452 if (N->isLeaf()) {
453 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000454 emitCheck("cast<ConstantSDNode>(" + RootName +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000455 ")->getSExtValue() == INT64_C(" +
456 itostr(II->getValue()) + ")");
Evan Chengb915f312005-12-09 22:45:35 +0000457 return;
458 } else if (!NodeIsComplexPattern(N)) {
459 assert(0 && "Cannot match this as a leaf value!");
460 abort();
461 }
462 }
463
Chris Lattner488580c2006-01-28 19:06:51 +0000464 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000465 // we already saw this in the pattern, emit code to verify dagness.
466 if (!N->getName().empty()) {
467 std::string &VarMapEntry = VariableMap[N->getName()];
468 if (VarMapEntry.empty()) {
469 VarMapEntry = RootName;
470 } else {
471 // If we get here, this is a second reference to a specific name. Since
472 // we already have checked that the first reference is valid, we don't
473 // have to recursively match it, just check that it's the same as the
474 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000475 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000476 return;
477 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000478
479 if (!N->isLeaf())
480 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000481 }
482
483
484 // Emit code to load the child nodes and match their contents recursively.
485 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000486 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
487 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000488 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000489 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000490 if (NodeHasChain)
491 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000492 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000493 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000494 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000495 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000496 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000497 // If the immediate use can somehow reach this node through another
498 // path, then can't fold it either or it will create a cycle.
499 // e.g. In the following diagram, XX can reach ld through YY. If
500 // ld is folded into XX, then YY is both a predecessor and a successor
501 // of XX.
502 //
503 // [ld]
504 // ^ ^
505 // | |
506 // / \---
507 // / [YY]
508 // | ^
509 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000510 bool NeedCheck = P != Pattern;
511 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000512 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000513 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000514 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
515 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
516 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000517 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000518 PInfo.hasProperty(SDNPHasChain) ||
519 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000520 PInfo.hasProperty(SDNPOptInFlag);
521 }
522
523 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000524 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng884c70c2008-11-27 00:49:46 +0000525 emitCheck("IsLegalAndProfitableToFold(" + RootName +
526 ".getNode(), " + ParentName + ".getNode(), N.getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000527 }
Evan Chenge41bf822006-02-05 06:43:12 +0000528 }
Evan Chengb915f312005-12-09 22:45:35 +0000529 }
Evan Chenge41bf822006-02-05 06:43:12 +0000530
Evan Chengc15d18c2006-01-27 22:13:45 +0000531 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000532 if (FoundChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000533 emitCheck("(" + ChainName + ".getNode() == " + RootName + ".getNode() || "
534 "IsChainCompatible(" + ChainName + ".getNode(), " +
535 RootName + ".getNode()))");
Evan Cheng4326ef52006-10-12 02:08:53 +0000536 OrigChains.push_back(std::make_pair(ChainName, RootName));
537 } else
Evan Chenge6389932006-07-21 22:19:51 +0000538 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000539 ChainName = "Chain" + ChainSuffix;
Dan Gohman475871a2008-07-27 21:46:04 +0000540 emitInit("SDValue " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000541 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000542 }
Evan Chengb915f312005-12-09 22:45:35 +0000543 }
544
Evan Cheng54597732006-01-26 00:22:25 +0000545 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000546 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000547 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000548 // FIXME: If the optional incoming flag does not exist. Then it is ok to
549 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000550 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000551 (PatternHasProperty(N, SDNPInFlag, CGP) ||
552 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
553 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000554 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000555 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000556 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000557 }
558 }
559
Dan Gohman0540e172008-10-15 06:17:21 +0000560 // If there are node predicates for this, emit the calls.
561 for (unsigned i = 0, e = N->getPredicateFns().size(); i != e; ++i)
562 emitCheck(N->getPredicateFns()[i] + "(" + RootName + ".getNode())");
Evan Chengd3eea902006-10-09 21:02:17 +0000563
Chris Lattner39e73f72006-10-11 04:05:55 +0000564 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
565 // a constant without a predicate fn that has more that one bit set, handle
566 // this as a special case. This is usually for targets that have special
567 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
568 // handling stuff). Using these instructions is often far more efficient
569 // than materializing the constant. Unfortunately, both the instcombiner
570 // and the dag combiner can often infer that bits are dead, and thus drop
571 // them from the mask in the dag. For example, it might turn 'AND X, 255'
572 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
573 // to handle this.
574 if (!N->isLeaf() &&
575 (N->getOperator()->getName() == "and" ||
576 N->getOperator()->getName() == "or") &&
577 N->getChild(1)->isLeaf() &&
Dan Gohman0540e172008-10-15 06:17:21 +0000578 N->getChild(1)->getPredicateFns().empty()) {
Chris Lattner39e73f72006-10-11 04:05:55 +0000579 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
580 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
Dan Gohman475871a2008-07-27 21:46:04 +0000581 emitInit("SDValue " + RootName + "0" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000582 RootName + ".getOperand(" + utostr(0) + ");");
Dan Gohman475871a2008-07-27 21:46:04 +0000583 emitInit("SDValue " + RootName + "1" + " = " +
Chris Lattner39e73f72006-10-11 04:05:55 +0000584 RootName + ".getOperand(" + utostr(1) + ");");
585
586 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
587 const char *MaskPredicate = N->getOperator()->getName() == "or"
588 ? "CheckOrMask(" : "CheckAndMask(";
589 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
Dan Gohmanb2a14322008-10-17 04:40:39 +0000590 RootName + "1), INT64_C(" + itostr(II->getValue()) + "))");
Chris Lattner39e73f72006-10-11 04:05:55 +0000591
Christopher Lamb85356242008-01-31 07:27:46 +0000592 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000593 ChainSuffix + utostr(0), FoundChain);
594 return;
595 }
596 }
597 }
598
Evan Chengb915f312005-12-09 22:45:35 +0000599 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Dan Gohman475871a2008-07-27 21:46:04 +0000600 emitInit("SDValue " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000601 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000602
Christopher Lamb85356242008-01-31 07:27:46 +0000603 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000604 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000605 }
606
Evan Cheng676d7312006-08-26 00:59:04 +0000607 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000608 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000609 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000610 std::string Fn = CP->getSelectFunc();
611 unsigned NumOps = CP->getNumOperands();
612 for (unsigned i = 0; i < NumOps; ++i) {
613 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000614 emitCode("SDValue CPTmp" + utostr(i) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +0000615 }
Evan Cheng94b30402006-10-11 21:02:01 +0000616 if (CP->hasProperty(SDNPHasChain)) {
617 emitDecl("CPInChain");
618 emitDecl("Chain" + ChainSuffix);
Dan Gohman475871a2008-07-27 21:46:04 +0000619 emitCode("SDValue CPInChain;");
620 emitCode("SDValue Chain" + ChainSuffix + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000621 }
Evan Cheng676d7312006-08-26 00:59:04 +0000622
Evan Cheng811731e2006-11-08 20:31:10 +0000623 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000624 for (unsigned i = 0; i < NumOps; i++)
625 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000626 if (CP->hasProperty(SDNPHasChain)) {
627 ChainName = "Chain" + ChainSuffix;
628 Code += ", CPInChain, Chain" + ChainSuffix;
629 }
Evan Cheng676d7312006-08-26 00:59:04 +0000630 emitCheck(Code + ")");
631 }
Evan Chengb915f312005-12-09 22:45:35 +0000632 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000633
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000634 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000635 const std::string &RootName,
636 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000637 const std::string &ChainSuffix, bool &FoundChain) {
638 if (!Child->isLeaf()) {
639 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000640 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000641 emitCheck(RootName + ".getOpcode() == " +
642 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000643 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000644 bool HasChain = false;
645 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
646 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000647 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000648 }
649 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
650 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
651 "Pattern folded multiple nodes which produce flags?");
652 FoldedFlag = std::make_pair(RootName,
653 CInfo.getNumResults() + (unsigned)HasChain);
654 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000655 } else {
656 // If this child has a name associated with it, capture it in VarMap. If
657 // we already saw this in the pattern, emit code to verify dagness.
658 if (!Child->getName().empty()) {
659 std::string &VarMapEntry = VariableMap[Child->getName()];
660 if (VarMapEntry.empty()) {
661 VarMapEntry = RootName;
662 } else {
663 // If we get here, this is a second reference to a specific name.
664 // Since we already have checked that the first reference is valid,
665 // we don't have to recursively match it, just check that it's the
666 // same as the previously named thing.
667 emitCheck(VarMapEntry + " == " + RootName);
668 Duplicates.insert(RootName);
669 return;
670 }
671 }
672
673 // Handle leaves of various types.
674 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
675 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000676 if (LeafRec->isSubClassOf("RegisterClass") ||
677 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000678 // Handle register references. Nothing to do here.
679 } else if (LeafRec->isSubClassOf("Register")) {
680 // Handle register references.
681 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
682 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000683 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000684 std::string Fn = CP->getSelectFunc();
685 unsigned NumOps = CP->getNumOperands();
686 for (unsigned i = 0; i < NumOps; ++i) {
687 emitDecl("CPTmp" + utostr(i));
Dan Gohman475871a2008-07-27 21:46:04 +0000688 emitCode("SDValue CPTmp" + utostr(i) + ";");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000689 }
Evan Cheng94b30402006-10-11 21:02:01 +0000690 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000691 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000692 FoldedChains.push_back(std::make_pair("CPInChain",
693 PInfo.getNumResults()));
694 ChainName = "Chain" + ChainSuffix;
695 emitDecl("CPInChain");
696 emitDecl(ChainName);
Dan Gohman475871a2008-07-27 21:46:04 +0000697 emitCode("SDValue CPInChain;");
698 emitCode("SDValue " + ChainName + ";");
Evan Cheng94b30402006-10-11 21:02:01 +0000699 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000700
Christopher Lamb85356242008-01-31 07:27:46 +0000701 std::string Code = Fn + "(";
702 if (CP->hasAttribute(CPAttrParentAsRoot)) {
703 Code += ParentRootName + ", ";
704 } else {
705 Code += "N, ";
706 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000707 if (CP->hasProperty(SDNPHasChain)) {
708 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000709 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000710 }
711 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000712 for (unsigned i = 0; i < NumOps; i++)
713 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000714 if (CP->hasProperty(SDNPHasChain))
715 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000716 emitCheck(Code + ")");
717 } else if (LeafRec->getName() == "srcvalue") {
718 // Place holder for SRCVALUE nodes. Nothing to do here.
719 } else if (LeafRec->isSubClassOf("ValueType")) {
720 // Make sure this is the specified value type.
721 emitCheck("cast<VTSDNode>(" + RootName +
722 ")->getVT() == MVT::" + LeafRec->getName());
723 } else if (LeafRec->isSubClassOf("CondCode")) {
724 // Make sure this is the specified cond code.
725 emitCheck("cast<CondCodeSDNode>(" + RootName +
726 ")->get() == ISD::" + LeafRec->getName());
727 } else {
728#ifndef NDEBUG
729 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000730 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000731#endif
732 assert(0 && "Unknown leaf type!");
733 }
734
Dan Gohman0540e172008-10-15 06:17:21 +0000735 // If there are node predicates for this, emit the calls.
736 for (unsigned i = 0, e = Child->getPredicateFns().size(); i != e; ++i)
737 emitCheck(Child->getPredicateFns()[i] + "(" + RootName +
Gabor Greifba36cb52008-08-28 21:40:38 +0000738 ".getNode())");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000739 } else if (IntInit *II =
740 dynamic_cast<IntInit*>(Child->getLeafValue())) {
741 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
742 unsigned CTmp = TmpNo++;
743 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
Dan Gohman7810bfe2008-09-26 21:54:37 +0000744 RootName + ")->getSExtValue();");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000745
Dan Gohman63f97202008-10-17 01:33:43 +0000746 emitCheck("CN" + utostr(CTmp) + " == "
747 "INT64_C(" +itostr(II->getValue()) + ")");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000748 } else {
749#ifndef NDEBUG
750 Child->dump();
751#endif
752 assert(0 && "Unknown leaf type!");
753 }
754 }
755 }
Evan Chengb915f312005-12-09 22:45:35 +0000756
757 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
758 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000759 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000760 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000761 bool InFlagDecled, bool ResNodeDecled,
762 bool LikeLeaf = false, bool isRoot = false) {
763 // List of arguments of getTargetNode() or SelectNodeTo().
764 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000765 // This is something selected from the pattern we matched.
766 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000767 const std::string &VarName = N->getName();
768 std::string Val = VariableMap[VarName];
769 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000770 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000771 cerr << "Variable '" << VarName << " referenced but not defined "
772 << "and not caught earlier!\n";
773 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000774 }
Evan Chengb915f312005-12-09 22:45:35 +0000775 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
776 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000777 NodeOps.push_back(Val);
778 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000779 }
780
781 const ComplexPattern *CP;
782 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000783 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000784 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000785 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000786 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000787 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000788 default:
789 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
790 << " type as an immediate constant. Aborting\n";
791 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000792 case MVT::i1: CastType = "bool"; break;
793 case MVT::i8: CastType = "unsigned char"; break;
794 case MVT::i16: CastType = "unsigned short"; break;
795 case MVT::i32: CastType = "unsigned"; break;
796 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000797 }
Dan Gohman475871a2008-07-27 21:46:04 +0000798 emitCode("SDValue " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000799 " = CurDAG->getTargetConstant(((" + CastType +
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000800 ") cast<ConstantSDNode>(" + Val + ")->getZExtValue()), " +
Evan Chengfceb57a2006-07-15 08:45:20 +0000801 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000802 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
803 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000804 Val = TmpVar;
805 ModifiedVal = true;
806 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000807 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
808 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
809 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000810 emitCode("SDValue " + TmpVar +
Dan Gohman4fbd7962008-09-12 18:08:03 +0000811 " = CurDAG->getTargetConstantFP(*cast<ConstantFPSDNode>(" +
812 Val + ")->getConstantFPValue(), cast<ConstantFPSDNode>(" +
813 Val + ")->getValueType(0));");
Nate Begemane1795842008-02-14 08:57:00 +0000814 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
815 // value if used multiple times by this pattern result.
816 Val = TmpVar;
817 ModifiedVal = true;
818 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000819 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000820 Record *Op = OperatorMap[N->getName()];
Bill Wendling056292f2008-09-16 21:48:12 +0000821 // Transform ExternalSymbol to TargetExternalSymbol
Evan Chengf805c2e2006-01-12 19:35:54 +0000822 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000823 std::string TmpVar = "Tmp"+utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000824 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Bill Wendling056292f2008-09-16 21:48:12 +0000825 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000826 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000827 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000828 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
829 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000830 Val = TmpVar;
831 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000832 }
Scott Michel6be48d42008-01-29 02:29:31 +0000833 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000834 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
835 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000836 Record *Op = OperatorMap[N->getName()];
837 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000838 if (Op && (Op->getName() == "globaladdr" ||
839 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000840 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +0000841 emitCode("SDValue " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000842 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000843 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000844 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000845 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
846 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000847 Val = TmpVar;
848 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000849 }
Evan Cheng676d7312006-08-26 00:59:04 +0000850 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000851 } else if (!N->isLeaf()
852 && (N->getOperator()->getName() == "texternalsym"
853 || N->getOperator()->getName() == "tconstpool")) {
854 // Do not rewrite the variable name, since we don't generate a new
855 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000856 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000857 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000858 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
Evan Cheng676d7312006-08-26 00:59:04 +0000859 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000860 }
Evan Chengb915f312005-12-09 22:45:35 +0000861 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000862 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000863 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000864 if (!LikeLeaf) {
Chris Lattner706d2d32006-08-09 16:44:44 +0000865 if (isRoot && N->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +0000866 emitCode("ReplaceUses(N, " + Val + ");");
Evan Cheng06d64702006-08-11 08:59:35 +0000867 emitCode("return NULL;");
Chris Lattner706d2d32006-08-09 16:44:44 +0000868 }
Evan Cheng83e1a6a2006-03-23 02:35:32 +0000869 }
Evan Cheng676d7312006-08-26 00:59:04 +0000870 NodeOps.push_back(Val);
Evan Chengb915f312005-12-09 22:45:35 +0000871 }
Scott Michel6be48d42008-01-29 02:29:31 +0000872
873 if (ModifiedVal) {
874 VariableMap[VarName] = Val;
875 }
Evan Cheng676d7312006-08-26 00:59:04 +0000876 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000877 }
Evan Chengb915f312005-12-09 22:45:35 +0000878 if (N->isLeaf()) {
879 // If this is an explicit register reference, handle it.
880 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
881 unsigned ResNo = TmpNo++;
882 if (DI->getDef()->isSubClassOf("Register")) {
Dan Gohman475871a2008-07-27 21:46:04 +0000883 emitCode("SDValue Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner6cefb772008-01-05 22:25:12 +0000884 getQualifiedName(DI->getDef()) + ", " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000885 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000886 NodeOps.push_back("Tmp" + utostr(ResNo));
887 return NodeOps;
Evan Cheng7774be42007-07-05 07:19:45 +0000888 } else if (DI->getDef()->getName() == "zero_reg") {
Dan Gohman475871a2008-07-27 21:46:04 +0000889 emitCode("SDValue Tmp" + utostr(ResNo) +
Evan Cheng7774be42007-07-05 07:19:45 +0000890 " = CurDAG->getRegister(0, " +
891 getEnumName(N->getTypeNum(0)) + ");");
892 NodeOps.push_back("Tmp" + utostr(ResNo));
893 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000894 }
895 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
896 unsigned ResNo = TmpNo++;
Nate Begemanb73628b2005-12-30 00:12:56 +0000897 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Dan Gohman475871a2008-07-27 21:46:04 +0000898 emitCode("SDValue Tmp" + utostr(ResNo) +
Scott Michel0123b7d2008-02-15 23:05:48 +0000899 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
900 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000901 NodeOps.push_back("Tmp" + utostr(ResNo));
902 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000903 }
904
Jim Laskey16d42c62006-07-11 18:25:13 +0000905#ifndef NDEBUG
906 N->dump();
907#endif
Evan Chengb915f312005-12-09 22:45:35 +0000908 assert(0 && "Unknown leaf type!");
Evan Cheng676d7312006-08-26 00:59:04 +0000909 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000910 }
911
912 Record *Op = N->getOperator();
913 if (Op->isSubClassOf("Instruction")) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000914 const CodeGenTarget &CGT = CGP.getTargetInfo();
Evan Cheng7b05bd52005-12-23 22:11:47 +0000915 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner6cefb772008-01-05 22:25:12 +0000916 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000917 const TreePattern *InstPat = Inst.getPattern();
Evan Chengd23aa5a2007-09-25 01:48:59 +0000918 // FIXME: Assume actual pattern comes before "implicit".
Evan Cheng045953c2006-05-10 00:05:46 +0000919 TreePatternNode *InstPatNode =
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000920 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
921 : (InstPat ? InstPat->getTree(0) : NULL);
Evan Cheng045953c2006-05-10 00:05:46 +0000922 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengaeb7d4d2007-09-11 19:52:18 +0000923 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Evan Cheng045953c2006-05-10 00:05:46 +0000924 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000925 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000926 // FIXME: fix how we deal with physical register operands.
Evan Cheng045953c2006-05-10 00:05:46 +0000927 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000928 bool HasImpResults = isRoot && DstRegs.size() > 0;
Evan Cheng045953c2006-05-10 00:05:46 +0000929 bool NodeHasOptInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000930 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000931 bool NodeHasInFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000932 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengef61ed32007-09-07 23:59:02 +0000933 bool NodeHasOutFlag = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000934 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Evan Cheng045953c2006-05-10 00:05:46 +0000935 bool NodeHasChain = InstPatNode &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000936 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Evan Cheng3eff89b2006-05-10 02:47:57 +0000937 bool InputHasChain = isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000938 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000939 unsigned NumResults = Inst.getNumResults();
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000940 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Evan Cheng4fba2812005-12-20 07:37:41 +0000941
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000942 // Record output varargs info.
943 OutputIsVariadic = IsVariadic;
944
Evan Chengfceb57a2006-07-15 08:45:20 +0000945 if (NodeHasOptInFlag) {
Evan Cheng676d7312006-08-26 00:59:04 +0000946 emitCode("bool HasInFlag = "
Evan Chengf8729402006-07-16 06:12:52 +0000947 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
Evan Chengfceb57a2006-07-15 08:45:20 +0000948 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000949 if (IsVariadic)
Dan Gohman475871a2008-07-27 21:46:04 +0000950 emitCode("SmallVector<SDValue, 8> Ops" + utostr(OpcNo) + ";");
Evan Cheng4fba2812005-12-20 07:37:41 +0000951
Evan Cheng823b7522006-01-19 21:57:10 +0000952 // How many results is this pattern expected to produce?
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000953 unsigned NumPatResults = 0;
Evan Cheng823b7522006-01-19 21:57:10 +0000954 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000955 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Evan Cheng823b7522006-01-19 21:57:10 +0000956 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000957 NumPatResults++;
Evan Cheng823b7522006-01-19 21:57:10 +0000958 }
959
Evan Cheng4326ef52006-10-12 02:08:53 +0000960 if (OrigChains.size() > 0) {
961 // The original input chain is being ignored. If it is not just
962 // pointing to the op that's being folded, we should create a
963 // TokenFactor with it and the chain of the folded op as the new chain.
964 // We could potentially be doing multiple levels of folding, in that
965 // case, the TokenFactor can have more operands.
Dan Gohman475871a2008-07-27 21:46:04 +0000966 emitCode("SmallVector<SDValue, 8> InChains;");
Evan Cheng4326ef52006-10-12 02:08:53 +0000967 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000968 emitCode("if (" + OrigChains[i].first + ".getNode() != " +
969 OrigChains[i].second + ".getNode()) {");
Evan Cheng4326ef52006-10-12 02:08:53 +0000970 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
971 emitCode("}");
972 }
Evan Cheng4326ef52006-10-12 02:08:53 +0000973 emitCode("InChains.push_back(" + ChainName + ");");
974 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
975 "&InChains[0], InChains.size());");
David Greene8ad4c002008-10-27 21:56:29 +0000976 if (GenDebug) {
977 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"yellow\");");
978 emitCode("CurDAG->setSubgraphColor(" + ChainName +".getNode(), \"black\");");
979 }
Evan Cheng4326ef52006-10-12 02:08:53 +0000980 }
981
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000982 // Loop over all of the operands of the instruction pattern, emitting code
983 // to fill them all in. The node 'N' usually has number children equal to
984 // the number of input operands of the instruction. However, in cases
985 // where there are predicate operands for an instruction, we need to fill
986 // in the 'execute always' values. Match up the node operands to the
987 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000988 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000989 for (unsigned ChildNo = 0, InstOpNo = NumResults;
990 InstOpNo != II.OperandList.size(); ++InstOpNo) {
991 std::vector<std::string> Ops;
992
Dan Gohmand35121a2008-05-29 19:57:41 +0000993 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000994 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000995 if ((OperandNode->isSubClassOf("PredicateOperand") ||
996 OperandNode->isSubClassOf("OptionalDefOperand")) &&
997 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000998 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000999 // 'default ops' operands.
1000 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +00001001 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001002 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001003 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001004 InFlagDecled, ResNodeDecled);
1005 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1006 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001007 } else {
1008 // Otherwise this is a normal operand or a predicate operand without
1009 // 'execute always'; emit it.
1010 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1011 InFlagDecled, ResNodeDecled);
1012 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1013 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001014 }
Evan Chengb915f312005-12-09 22:45:35 +00001015 }
1016
Evan Chengb915f312005-12-09 22:45:35 +00001017 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001018 bool ChainEmitted = NodeHasChain;
Evan Chengbc6b86a2006-06-14 19:27:50 +00001019 if (NodeHasInFlag || HasImpInputs)
Evan Cheng676d7312006-08-26 00:59:04 +00001020 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
1021 InFlagDecled, ResNodeDecled, true);
Evan Chengf037ca62006-08-27 08:11:28 +00001022 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
Evan Cheng676d7312006-08-26 00:59:04 +00001023 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001024 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001025 InFlagDecled = true;
1026 }
Evan Chengf037ca62006-08-27 08:11:28 +00001027 if (NodeHasOptInFlag) {
1028 emitCode("if (HasInFlag) {");
1029 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
Evan Chengf037ca62006-08-27 08:11:28 +00001030 emitCode("}");
1031 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001032 }
Evan Chengb915f312005-12-09 22:45:35 +00001033
Evan Chengb915f312005-12-09 22:45:35 +00001034 unsigned ResNo = TmpNo++;
Evan Chengf037ca62006-08-27 08:11:28 +00001035
Dan Gohman95d11092008-07-07 21:00:17 +00001036 unsigned OpsNo = OpcNo;
1037 std::string CodePrefix;
1038 bool ChainAssignmentNeeded = NodeHasChain && !isRoot;
1039 std::deque<std::string> After;
1040 std::string NodeName;
1041 if (!isRoot) {
1042 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman475871a2008-07-27 21:46:04 +00001043 CodePrefix = "SDValue " + NodeName + "(";
Evan Chengb915f312005-12-09 22:45:35 +00001044 } else {
Dan Gohman95d11092008-07-07 21:00:17 +00001045 NodeName = "ResNode";
1046 if (!ResNodeDecled) {
1047 CodePrefix = "SDNode *" + NodeName + " = ";
1048 ResNodeDecled = true;
1049 } else
1050 CodePrefix = NodeName + " = ";
Evan Chengb915f312005-12-09 22:45:35 +00001051 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001052
Dan Gohman95d11092008-07-07 21:00:17 +00001053 std::string Code = "Opc" + utostr(OpcNo);
1054
1055 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1056
1057 // Output order: results, chain, flags
1058 // Result types.
1059 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1060 Code += ", VT" + utostr(VTNo);
1061 emitVT(getEnumName(N->getTypeNum(0)));
1062 }
1063 // Add types for implicit results in physical registers, scheduler will
1064 // care of adding copyfromreg nodes.
1065 for (unsigned i = 0; i < NumDstRegs; i++) {
1066 Record *RR = DstRegs[i];
1067 if (RR->isSubClassOf("Register")) {
1068 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
1069 Code += ", " + getEnumName(RVT);
1070 }
1071 }
1072 if (NodeHasChain)
1073 Code += ", MVT::Other";
1074 if (NodeHasOutFlag)
1075 Code += ", MVT::Flag";
1076
1077 // Inputs.
1078 if (IsVariadic) {
1079 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1080 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1081 AllOps.clear();
1082
1083 // Figure out whether any operands at the end of the op list are not
1084 // part of the variable section.
1085 std::string EndAdjust;
1086 if (NodeHasInFlag || HasImpInputs)
1087 EndAdjust = "-1"; // Always has one flag.
1088 else if (NodeHasOptInFlag)
1089 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1090
1091 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
1092 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1093
Dan Gohman95d11092008-07-07 21:00:17 +00001094 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1095 emitCode("}");
1096 }
1097
1098 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1099 // this pattern.
1100 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1101 std::vector<std::string>::const_iterator mi, mie;
1102 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
David Greene8ad4c002008-10-27 21:56:29 +00001103 std::string LSIName = "LSI_" + *mi;
1104 emitCode("SDValue " + LSIName + " = "
Dan Gohman95d11092008-07-07 21:00:17 +00001105 "CurDAG->getMemOperand(cast<MemSDNode>(" +
1106 *mi + ")->getMemOperand());");
David Greene8ad4c002008-10-27 21:56:29 +00001107 if (GenDebug) {
1108 emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"yellow\");");
1109 emitCode("CurDAG->setSubgraphColor(" + LSIName +".getNode(), \"black\");");
1110 }
Dan Gohman95d11092008-07-07 21:00:17 +00001111 if (IsVariadic)
David Greene8ad4c002008-10-27 21:56:29 +00001112 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + LSIName + ");");
Dan Gohman95d11092008-07-07 21:00:17 +00001113 else
David Greene8ad4c002008-10-27 21:56:29 +00001114 AllOps.push_back(LSIName);
Dan Gohman95d11092008-07-07 21:00:17 +00001115 }
1116 }
1117
1118 if (NodeHasChain) {
1119 if (IsVariadic)
1120 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1121 else
1122 AllOps.push_back(ChainName);
1123 }
1124
1125 if (IsVariadic) {
1126 if (NodeHasInFlag || HasImpInputs)
1127 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1128 else if (NodeHasOptInFlag) {
1129 emitCode("if (HasInFlag)");
1130 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1131 }
1132 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1133 ".size()";
1134 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1135 AllOps.push_back("InFlag");
1136
1137 unsigned NumOps = AllOps.size();
1138 if (NumOps) {
1139 if (!NodeHasOptInFlag && NumOps < 4) {
1140 for (unsigned i = 0; i != NumOps; ++i)
1141 Code += ", " + AllOps[i];
1142 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001143 std::string OpsCode = "SDValue Ops" + utostr(OpsNo) + "[] = { ";
Dan Gohman95d11092008-07-07 21:00:17 +00001144 for (unsigned i = 0; i != NumOps; ++i) {
1145 OpsCode += AllOps[i];
1146 if (i != NumOps-1)
1147 OpsCode += ", ";
1148 }
1149 emitCode(OpsCode + " };");
1150 Code += ", Ops" + utostr(OpsNo) + ", ";
1151 if (NodeHasOptInFlag) {
1152 Code += "HasInFlag ? ";
1153 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1154 } else
1155 Code += utostr(NumOps);
1156 }
1157 }
1158
1159 if (!isRoot)
1160 Code += "), 0";
1161
Dan Gohmane8be6c62008-07-17 19:10:17 +00001162 std::vector<std::string> ReplaceFroms;
1163 std::vector<std::string> ReplaceTos;
Dan Gohman95d11092008-07-07 21:00:17 +00001164 if (!isRoot) {
1165 NodeOps.push_back("Tmp" + utostr(ResNo));
1166 } else {
1167
1168 if (NodeHasOutFlag) {
1169 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001170 After.push_back("SDValue InFlag(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001171 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1172 ");");
1173 InFlagDecled = true;
1174 } else
Dan Gohman475871a2008-07-27 21:46:04 +00001175 After.push_back("InFlag = SDValue(ResNode, " +
Dan Gohman95d11092008-07-07 21:00:17 +00001176 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) +
1177 ");");
1178 }
1179
1180 if (FoldedChains.size() > 0) {
1181 std::string Code;
Dan Gohmane8be6c62008-07-17 19:10:17 +00001182 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) {
Dan Gohman475871a2008-07-27 21:46:04 +00001183 ReplaceFroms.push_back("SDValue(" +
Gabor Greifba36cb52008-08-28 21:40:38 +00001184 FoldedChains[j].first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001185 utostr(FoldedChains[j].second) +
1186 ")");
Dan Gohman475871a2008-07-27 21:46:04 +00001187 ReplaceTos.push_back("SDValue(ResNode, " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001188 utostr(NumResults+NumDstRegs) + ")");
1189 }
Dan Gohman95d11092008-07-07 21:00:17 +00001190 }
1191
1192 if (NodeHasOutFlag) {
1193 if (FoldedFlag.first != "") {
Gabor Greifba36cb52008-08-28 21:40:38 +00001194 ReplaceFroms.push_back("SDValue(" + FoldedFlag.first + ".getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001195 utostr(FoldedFlag.second) + ")");
1196 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001197 } else {
1198 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
Gabor Greifba36cb52008-08-28 21:40:38 +00001199 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001200 utostr(NumPatResults + (unsigned)InputHasChain)
1201 + ")");
1202 ReplaceTos.push_back("InFlag");
Dan Gohman95d11092008-07-07 21:00:17 +00001203 }
Dan Gohman95d11092008-07-07 21:00:17 +00001204 }
1205
Dan Gohmane8be6c62008-07-17 19:10:17 +00001206 if (!ReplaceFroms.empty() && InputHasChain) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001207 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001208 utostr(NumPatResults) + ")");
Gabor Greifba36cb52008-08-28 21:40:38 +00001209 ReplaceTos.push_back("SDValue(" + ChainName + ".getNode(), " +
Gabor Greif99a6cb92008-08-26 22:36:50 +00001210 ChainName + ".getResNo()" + ")");
Dan Gohman95d11092008-07-07 21:00:17 +00001211 ChainAssignmentNeeded |= NodeHasChain;
1212 }
1213
1214 // User does not expect the instruction would produce a chain!
1215 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
1216 ;
1217 } else if (InputHasChain && !NodeHasChain) {
1218 // One of the inner node produces a chain.
Dan Gohmane8be6c62008-07-17 19:10:17 +00001219 if (NodeHasOutFlag) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001220 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001221 utostr(NumPatResults+1) +
1222 ")");
Gabor Greif99a6cb92008-08-26 22:36:50 +00001223 ReplaceTos.push_back("SDValue(ResNode, N.getResNo()-1)");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001224 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001225 ReplaceFroms.push_back("SDValue(N.getNode(), " +
Dan Gohmane8be6c62008-07-17 19:10:17 +00001226 utostr(NumPatResults) + ")");
1227 ReplaceTos.push_back(ChainName);
Dan Gohman95d11092008-07-07 21:00:17 +00001228 }
1229 }
1230
1231 if (ChainAssignmentNeeded) {
1232 // Remember which op produces the chain.
1233 std::string ChainAssign;
1234 if (!isRoot)
Dan Gohman475871a2008-07-27 21:46:04 +00001235 ChainAssign = ChainName + " = SDValue(" + NodeName +
Gabor Greifba36cb52008-08-28 21:40:38 +00001236 ".getNode(), " + utostr(NumResults+NumDstRegs) + ");";
Dan Gohman95d11092008-07-07 21:00:17 +00001237 else
Dan Gohman475871a2008-07-27 21:46:04 +00001238 ChainAssign = ChainName + " = SDValue(" + NodeName +
Dan Gohman95d11092008-07-07 21:00:17 +00001239 ", " + utostr(NumResults+NumDstRegs) + ");";
1240
1241 After.push_front(ChainAssign);
1242 }
1243
Dan Gohmane8be6c62008-07-17 19:10:17 +00001244 if (ReplaceFroms.size() == 1) {
1245 After.push_back("ReplaceUses(" + ReplaceFroms[0] + ", " +
1246 ReplaceTos[0] + ");");
1247 } else if (!ReplaceFroms.empty()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001248 After.push_back("const SDValue Froms[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001249 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1250 After.push_back(" " + ReplaceFroms[i] + (i + 1 != e ? "," : ""));
1251 After.push_back("};");
Dan Gohman475871a2008-07-27 21:46:04 +00001252 After.push_back("const SDValue Tos[] = {");
Dan Gohmane8be6c62008-07-17 19:10:17 +00001253 for (unsigned i = 0, e = ReplaceFroms.size(); i != e; ++i)
1254 After.push_back(" " + ReplaceTos[i] + (i + 1 != e ? "," : ""));
1255 After.push_back("};");
1256 After.push_back("ReplaceUses(Froms, Tos, " +
1257 itostr(ReplaceFroms.size()) + ");");
1258 }
1259
1260 // We prefer to use SelectNodeTo since it avoids allocation when
1261 // possible and it avoids CSE map recalculation for the node's
1262 // users, however it's tricky to use in a non-root context.
Dan Gohman95d11092008-07-07 21:00:17 +00001263 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001264 // We also don't use if the pattern replacement is being used to
1265 // jettison a chain result, since morphing the node in place
1266 // would leave users of the chain dangling.
Dan Gohman95d11092008-07-07 21:00:17 +00001267 //
Dan Gohmane8be6c62008-07-17 19:10:17 +00001268 if (!isRoot || (InputHasChain && !NodeHasChain)) {
Dan Gohman95d11092008-07-07 21:00:17 +00001269 Code = "CurDAG->getTargetNode(" + Code;
1270 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00001271 Code = "CurDAG->SelectNodeTo(N.getNode(), " + Code;
Dan Gohman95d11092008-07-07 21:00:17 +00001272 }
1273 if (isRoot) {
1274 if (After.empty())
1275 CodePrefix = "return ";
1276 else
1277 After.push_back("return ResNode;");
1278 }
1279
1280 emitCode(CodePrefix + Code + ");");
David Greene8ad4c002008-10-27 21:56:29 +00001281
1282 if (GenDebug) {
1283 if (!isRoot) {
1284 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"yellow\");");
1285 emitCode("CurDAG->setSubgraphColor(" + NodeName +".getNode(), \"black\");");
1286 }
1287 else {
1288 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"yellow\");");
1289 emitCode("CurDAG->setSubgraphColor(" + NodeName +", \"black\");");
1290 }
1291 }
1292
Dan Gohman95d11092008-07-07 21:00:17 +00001293 for (unsigned i = 0, e = After.size(); i != e; ++i)
1294 emitCode(After[i]);
1295
Evan Cheng676d7312006-08-26 00:59:04 +00001296 return NodeOps;
Dan Gohman0540e172008-10-15 06:17:21 +00001297 }
1298 if (Op->isSubClassOf("SDNodeXForm")) {
Evan Chengb915f312005-12-09 22:45:35 +00001299 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001300 // PatLeaf node - the operand may or may not be a leaf node. But it should
1301 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001302 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001303 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001304 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001305 unsigned ResNo = TmpNo++;
Dan Gohman475871a2008-07-27 21:46:04 +00001306 emitCode("SDValue Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
Gabor Greifba36cb52008-08-28 21:40:38 +00001307 + "(" + Ops.back() + ".getNode());");
Evan Cheng676d7312006-08-26 00:59:04 +00001308 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001309 if (isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001310 emitCode("return Tmp" + utostr(ResNo) + ".getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001311 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001312 }
Dan Gohman0540e172008-10-15 06:17:21 +00001313
1314 N->dump();
1315 cerr << "\n";
1316 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001317 }
1318
Chris Lattner488580c2006-01-28 19:06:51 +00001319 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1320 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001321 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1322 /// for, this returns true otherwise false if Pat has all types.
1323 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001324 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001325 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001326 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001327 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001328 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001329 // The top level node type is checked outside of the select function.
1330 if (!isRoot)
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 emitCheck(Prefix + ".getNode()->getValueType(0) == " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001332 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001333 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001334 }
1335
Evan Cheng51fecc82006-01-09 18:27:06 +00001336 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001337 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001338 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1339 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1340 Prefix + utostr(OpNo)))
1341 return true;
1342 return false;
1343 }
1344
1345private:
Evan Cheng54597732006-01-26 00:22:25 +00001346 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001347 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001348 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001349 bool &ChainEmitted, bool &InFlagDecled,
1350 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001351 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001352 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001353 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1354 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001355 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1356 TreePatternNode *Child = N->getChild(i);
1357 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001358 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1359 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001360 } else {
1361 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001362 if (!Child->getName().empty()) {
1363 std::string Name = RootName + utostr(OpNo);
1364 if (Duplicates.find(Name) != Duplicates.end())
1365 // A duplicate! Do not emit a copy for this node.
1366 continue;
1367 }
1368
Evan Chengb915f312005-12-09 22:45:35 +00001369 Record *RR = DI->getDef();
1370 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001371 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001372 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001373 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001374 emitCode("SDValue InFlag = " + RootName + utostr(OpNo) + ";");
Evan Cheng676d7312006-08-26 00:59:04 +00001375 InFlagDecled = true;
1376 } else
1377 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
Evan Chengb2c6d492006-01-11 22:16:13 +00001378 } else {
1379 if (!ChainEmitted) {
Dan Gohman475871a2008-07-27 21:46:04 +00001380 emitCode("SDValue Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001381 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001382 ChainEmitted = true;
1383 }
Evan Cheng676d7312006-08-26 00:59:04 +00001384 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001385 emitCode("SDValue InFlag(0, 0);");
Evan Cheng676d7312006-08-26 00:59:04 +00001386 InFlagDecled = true;
1387 }
1388 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1389 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001390 ", " + getQualifiedName(RR) +
Gabor Greifba36cb52008-08-28 21:40:38 +00001391 ", " + RootName + utostr(OpNo) + ", InFlag).getNode();");
Evan Cheng676d7312006-08-26 00:59:04 +00001392 ResNodeDecled = true;
Dan Gohman475871a2008-07-27 21:46:04 +00001393 emitCode(ChainName + " = SDValue(ResNode, 0);");
1394 emitCode("InFlag = SDValue(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001395 }
1396 }
1397 }
1398 }
1399 }
Evan Cheng54597732006-01-26 00:22:25 +00001400
Evan Cheng676d7312006-08-26 00:59:04 +00001401 if (HasInFlag) {
1402 if (!InFlagDecled) {
Dan Gohman475871a2008-07-27 21:46:04 +00001403 emitCode("SDValue InFlag = " + RootName +
Evan Cheng676d7312006-08-26 00:59:04 +00001404 ".getOperand(" + utostr(OpNo) + ");");
1405 InFlagDecled = true;
1406 } else
1407 emitCode("InFlag = " + RootName +
1408 ".getOperand(" + utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001409 }
Evan Chengb915f312005-12-09 22:45:35 +00001410 }
1411};
1412
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001413/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1414/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001415/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001416void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001417 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001418 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001419 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001420 std::vector<std::string> &TargetVTs,
1421 bool &OutputIsVariadic,
1422 unsigned &NumInputRootOps) {
1423 OutputIsVariadic = false;
1424 NumInputRootOps = 0;
1425
Dan Gohman22bb3112008-08-22 00:20:26 +00001426 PatternCodeEmitter Emitter(CGP, Pattern.getPredicateCheck(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001427 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001428 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001429 TargetOpcodes, TargetVTs,
1430 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001431
Chris Lattner8fc35682005-09-23 23:16:51 +00001432 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001433 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001434 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001435
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001436 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001437 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001438
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001439 // At this point, we know that we structurally match the pattern, but the
1440 // types of the nodes may not match. Figure out the fewest number of type
1441 // comparisons we need to emit. For example, if there is only one integer
1442 // type supported by a target, there should be no type comparisons at all for
1443 // integer patterns!
1444 //
1445 // To figure out the fewest number of type checks needed, clone the pattern,
1446 // remove the types, then perform type inference on the pattern as a whole.
1447 // If there are unresolved types, emit an explicit check for those types,
1448 // apply the type to the tree, then rerun type inference. Iterate until all
1449 // types are resolved.
1450 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001451 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001452 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001453
1454 do {
1455 // Resolve/propagate as many types as possible.
1456 try {
1457 bool MadeChange = true;
1458 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001459 MadeChange = Pat->ApplyTypeConstraints(TP,
1460 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001461 } catch (...) {
1462 assert(0 && "Error: could not find consistent types for something we"
1463 " already decided was ok!");
1464 abort();
1465 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001466
Chris Lattner7e82f132005-10-15 21:34:21 +00001467 // Insert a check for an unresolved type and add it to the tree. If we find
1468 // an unresolved type to add a check for, this returns true and we iterate,
1469 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001470 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001471
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001472 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001473 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001474 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001475}
1476
Chris Lattner24e00a42006-01-29 04:41:05 +00001477/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1478/// a line causes any of them to be empty, remove them and return true when
1479/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001480static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001481 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001482 &Patterns) {
1483 bool ErasedPatterns = false;
1484 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1485 Patterns[i].second.pop_back();
1486 if (Patterns[i].second.empty()) {
1487 Patterns.erase(Patterns.begin()+i);
1488 --i; --e;
1489 ErasedPatterns = true;
1490 }
1491 }
1492 return ErasedPatterns;
1493}
1494
Chris Lattner8bc74722006-01-29 04:25:26 +00001495/// EmitPatterns - Emit code for at least one pattern, but try to group common
1496/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001497void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001498 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001499 &Patterns, unsigned Indent,
1500 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001501 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001502 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001503 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001504
1505 if (Patterns.empty()) return;
1506
Chris Lattner24e00a42006-01-29 04:41:05 +00001507 // Figure out how many patterns share the next code line. Explicitly copy
1508 // FirstCodeLine so that we don't invalidate a reference when changing
1509 // Patterns.
1510 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001511 unsigned LastMatch = Patterns.size()-1;
1512 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1513 --LastMatch;
1514
1515 // If not all patterns share this line, split the list into two pieces. The
1516 // first chunk will use this line, the second chunk won't.
1517 if (LastMatch != 0) {
1518 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1519 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1520
1521 // FIXME: Emit braces?
1522 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001523 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001524 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1525 Pattern.getSrcPattern()->print(OS);
1526 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1527 Pattern.getDstPattern()->print(OS);
1528 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001529 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001530 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001531 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001532 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001533 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001534 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001535 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001536 }
Evan Cheng676d7312006-08-26 00:59:04 +00001537 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001538 OS << std::string(Indent, ' ') << "{\n";
1539 Indent += 2;
1540 }
1541 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001542 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001543 Indent -= 2;
1544 OS << std::string(Indent, ' ') << "}\n";
1545 }
1546
1547 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001548 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001549 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1550 Pattern.getSrcPattern()->print(OS);
1551 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1552 Pattern.getDstPattern()->print(OS);
1553 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001554 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001555 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001556 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001557 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001558 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001559 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001560 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001561 }
1562 EmitPatterns(Other, Indent, OS);
1563 return;
1564 }
1565
Chris Lattner24e00a42006-01-29 04:41:05 +00001566 // Remove this code from all of the patterns that share it.
1567 bool ErasedPatterns = EraseCodeLine(Patterns);
1568
Evan Cheng676d7312006-08-26 00:59:04 +00001569 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001570
1571 // Otherwise, every pattern in the list has this line. Emit it.
1572 if (!isPredicate) {
1573 // Normal code.
1574 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1575 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001576 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1577
1578 // If the next code line is another predicate, and if all of the pattern
1579 // in this group share the same next line, emit it inline now. Do this
1580 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001581 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001582 // Check that all of fhe patterns in Patterns end with the same predicate.
1583 bool AllEndWithSamePredicate = true;
1584 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1585 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1586 AllEndWithSamePredicate = false;
1587 break;
1588 }
1589 // If all of the predicates aren't the same, we can't share them.
1590 if (!AllEndWithSamePredicate) break;
1591
1592 // Otherwise we can. Emit it shared now.
1593 OS << " &&\n" << std::string(Indent+4, ' ')
1594 << Patterns.back().second.back().second;
1595 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001596 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001597
1598 OS << ") {\n";
1599 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001600 }
1601
1602 EmitPatterns(Patterns, Indent, OS);
1603
1604 if (isPredicate)
1605 OS << std::string(Indent-2, ' ') << "}\n";
1606}
1607
Evan Cheng892aaf82006-11-08 23:01:03 +00001608static std::string getLegalCName(std::string OpName) {
1609 std::string::size_type pos = OpName.find("::");
1610 if (pos != std::string::npos)
1611 OpName.replace(pos, 2, "_");
1612 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001613}
1614
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001615void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001616 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001617
Dan Gohman1e0ee4b2008-08-20 21:45:57 +00001618 // Get the namespace to insert instructions into.
1619 std::string InstNS = Target.getInstNamespace();
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001620 if (!InstNS.empty()) InstNS += "::";
1621
Chris Lattner602f6922006-01-04 00:25:00 +00001622 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001623 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001624 // All unique target node emission functions.
1625 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001626 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001627 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001628 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001629
1630 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001631 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001632 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001633 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001634 } else {
1635 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001636 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001637 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001638 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001639 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001640 std::vector<Record*> OpNodes = CP->getRootNodes();
1641 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001642 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1643 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001644 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001645 }
1646 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001647 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001648 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001649 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001650 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001651 exit(1);
1652 }
1653 }
1654 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001655
1656 // For each opcode, there might be multiple select functions, one per
1657 // ValueType of the node (or its first operand if it doesn't produce a
1658 // non-chain result.
1659 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1660
Chris Lattner602f6922006-01-04 00:25:00 +00001661 // Emit one Select_* method for each top-level opcode. We do this instead of
1662 // emitting one giant switch statement to support compilers where this will
1663 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001664 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001665 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1666 PBOI != E; ++PBOI) {
1667 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001668 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001669 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1670
Chris Lattner706d2d32006-08-09 16:44:44 +00001671 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001672 std::map<MVT::SimpleValueType,
1673 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001674 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001675 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001676 TreePatternNode *SrcPat = Pat->getSrcPattern();
Chris Lattner9783d622008-08-26 07:01:28 +00001677 PatternsByType[SrcPat->getTypeNum(0)].push_back(Pat);
Chris Lattner706d2d32006-08-09 16:44:44 +00001678 }
1679
Duncan Sands83ec4b62008-06-06 12:08:01 +00001680 for (std::map<MVT::SimpleValueType,
1681 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001682 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1683 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001684 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001685 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohman0540e172008-10-15 06:17:21 +00001686 typedef std::pair<unsigned, std::string> CodeLine;
1687 typedef std::vector<CodeLine> CodeList;
1688 typedef CodeList::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001689
Chris Lattner60d81392008-01-05 22:30:17 +00001690 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001691 std::vector<std::vector<std::string> > PatternOpcodes;
1692 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001693 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001694 std::vector<bool> OutputIsVariadicFlags;
1695 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001696 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1697 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001698 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001699 std::vector<std::string> TargetOpcodes;
1700 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001701 bool OutputIsVariadic;
1702 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001703 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001704 TargetOpcodes, TargetVTs,
1705 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001706 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1707 PatternDecls.push_back(GeneratedDecl);
1708 PatternOpcodes.push_back(TargetOpcodes);
1709 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001710 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1711 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001712 }
1713
Chris Lattner706d2d32006-08-09 16:44:44 +00001714 // Factor target node emission code (emitted by EmitResultCode) into
1715 // separate functions. Uniquing and share them among all instruction
1716 // selection routines.
1717 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1718 CodeList &GeneratedCode = CodeForPatterns[i].second;
1719 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1720 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001721 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001722 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1723 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001724 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001725 int CodeSize = (int)GeneratedCode.size();
1726 int LastPred = -1;
1727 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001728 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001729 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001730 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1731 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001732 }
1733
Dan Gohman475871a2008-07-27 21:46:04 +00001734 std::string CalleeCode = "(const SDValue &N";
Evan Cheng9ade2182006-08-26 05:34:46 +00001735 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001736 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1737 CalleeCode += ", unsigned Opc" + utostr(j);
1738 CallerCode += ", " + TargetOpcodes[j];
1739 }
1740 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001741 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001742 CallerCode += ", " + TargetVTs[j];
1743 }
Evan Chengf5493192006-08-26 01:02:19 +00001744 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001745 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001746 std::string Name = *I;
Dan Gohman475871a2008-07-27 21:46:04 +00001747 CalleeCode += ", SDValue &" + Name;
Evan Cheng676d7312006-08-26 00:59:04 +00001748 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001749 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001750
1751 if (OutputIsVariadic) {
1752 CalleeCode += ", unsigned NumInputRootOps";
1753 CallerCode += ", " + utostr(NumInputRootOps);
1754 }
1755
Chris Lattner706d2d32006-08-09 16:44:44 +00001756 CallerCode += ");";
1757 CalleeCode += ") ";
1758 // Prevent emission routines from being inlined to reduce selection
1759 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001760 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001761 CalleeCode += "{\n";
1762
1763 for (std::vector<std::string>::const_reverse_iterator
1764 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1765 CalleeCode += " " + *I + "\n";
1766
Evan Chengf5493192006-08-26 01:02:19 +00001767 for (int j = LastPred+1; j < CodeSize; ++j)
1768 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001769 for (int j = LastPred+1; j < CodeSize; ++j)
1770 GeneratedCode.pop_back();
1771 CalleeCode += "}\n";
1772
1773 // Uniquing the emission routines.
1774 unsigned EmitFuncNum;
1775 std::map<std::string, unsigned>::iterator EFI =
1776 EmitFunctions.find(CalleeCode);
1777 if (EFI != EmitFunctions.end()) {
1778 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001779 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001780 EmitFuncNum = EmitFunctions.size();
1781 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001782 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001783 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001784
Chris Lattner706d2d32006-08-09 16:44:44 +00001785 // Replace the emission code within selection routines with calls to the
1786 // emission functions.
David Greene8ad4c002008-10-27 21:56:29 +00001787 if (GenDebug) {
1788 GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"red\");"));
1789 }
1790 CallerCode = "SDNode *Result = Emit_" + utostr(EmitFuncNum) + CallerCode;
1791 GeneratedCode.push_back(std::make_pair(3, CallerCode));
1792 if (GenDebug) {
1793 GeneratedCode.push_back(std::make_pair(0, "if(Result) {"));
1794 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"yellow\");"));
1795 GeneratedCode.push_back(std::make_pair(0, " CurDAG->setSubgraphColor(Result, \"black\");"));
1796 GeneratedCode.push_back(std::make_pair(0, "}"));
1797 //GeneratedCode.push_back(std::make_pair(0, "CurDAG->setSubgraphColor(N.getNode(), \"black\");"));
1798 }
1799 GeneratedCode.push_back(std::make_pair(0, "return Result;"));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001800 }
1801
Chris Lattner706d2d32006-08-09 16:44:44 +00001802 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001803 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001804 if (OpVT == MVT::iPTR) {
1805 OpVTStr = "_iPTR";
Mon P Wange3b3a722008-07-30 04:36:53 +00001806 } else if (OpVT == MVT::iPTRAny) {
1807 OpVTStr = "_iPTRAny";
Chris Lattner33a40042006-11-14 22:17:10 +00001808 } else if (OpVT == MVT::isVoid) {
1809 // Nodes with a void result actually have a first result type of either
1810 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1811 // void to this case, we handle it specially here.
1812 } else {
1813 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1814 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001815 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1816 OpcodeVTMap.find(OpName);
1817 if (OpVTI == OpcodeVTMap.end()) {
1818 std::vector<std::string> VTSet;
1819 VTSet.push_back(OpVTStr);
1820 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1821 } else
1822 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001823
Evan Cheng892aaf82006-11-08 23:01:03 +00001824 OS << "SDNode *Select_" << getLegalCName(OpName)
Dan Gohman475871a2008-07-27 21:46:04 +00001825 << OpVTStr << "(const SDValue &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001826
Dan Gohman0540e172008-10-15 06:17:21 +00001827 // We want to emit all of the matching code now. However, we want to emit
1828 // the matches in order of minimal cost. Sort the patterns so the least
1829 // cost one is at the start.
1830 std::stable_sort(CodeForPatterns.begin(), CodeForPatterns.end(),
1831 PatternSortingPredicate(CGP));
1832
1833 // Scan the code to see if all of the patterns are reachable and if it is
1834 // possible that the last one might not match.
1835 bool mightNotMatch = true;
1836 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1837 CodeList &GeneratedCode = CodeForPatterns[i].second;
1838 mightNotMatch = false;
1839
1840 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1841 if (GeneratedCode[j].first == 1) { // predicate.
1842 mightNotMatch = true;
1843 break;
1844 }
1845 }
1846
1847 // If this pattern definitely matches, and if it isn't the last one, the
1848 // patterns after it CANNOT ever match. Error out.
1849 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
1850 cerr << "Pattern '";
1851 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1852 cerr << "' is impossible to select!\n";
1853 exit(1);
1854 }
1855 }
1856
Chris Lattner706d2d32006-08-09 16:44:44 +00001857 // Loop through and reverse all of the CodeList vectors, as we will be
1858 // accessing them from their logical front, but accessing the end of a
1859 // vector is more efficient.
1860 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1861 CodeList &GeneratedCode = CodeForPatterns[i].second;
1862 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001863 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001864
1865 // Next, reverse the list of patterns itself for the same reason.
1866 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1867
1868 // Emit all of the patterns now, grouped together to share code.
1869 EmitPatterns(CodeForPatterns, 2, OS);
1870
Chris Lattner64906972006-09-21 18:28:27 +00001871 // If the last pattern has predicates (which could fail) emit code to
1872 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001873 if (mightNotMatch) {
Dan Gohman31bd42b2008-09-27 23:53:14 +00001874 OS << "\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001875 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1876 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
Dan Gohman31bd42b2008-09-27 23:53:14 +00001877 OpName != "ISD::INTRINSIC_VOID")
1878 OS << " CannotYetSelect(N);\n";
1879 else
1880 OS << " CannotYetSelectIntrinsic(N);\n";
1881
1882 OS << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001883 }
1884 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001885 }
Chris Lattner602f6922006-01-04 00:25:00 +00001886 }
1887
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001888 // Emit boilerplate.
Dan Gohman475871a2008-07-27 21:46:04 +00001889 OS << "SDNode *Select_INLINEASM(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001890 << " std::vector<SDValue> Ops(N.getNode()->op_begin(), N.getNode()->op_end());\n"
Dan Gohmanf350b272008-08-23 02:25:05 +00001891 << " SelectInlineAsmMemoryOperands(Ops);\n\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001892
Duncan Sands83ec4b62008-06-06 12:08:01 +00001893 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001894 << " VTs.push_back(MVT::Other);\n"
1895 << " VTs.push_back(MVT::Flag);\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001896 << " SDValue New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
Chris Lattner706d2d32006-08-09 16:44:44 +00001897 "Ops.size());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001898 << " return New.getNode();\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001899 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001900
Dan Gohman475871a2008-07-27 21:46:04 +00001901 OS << "SDNode *Select_UNDEF(const SDValue &N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001902 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::IMPLICIT_DEF,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001903 << " N.getValueType());\n"
1904 << "}\n\n";
1905
Dan Gohman475871a2008-07-27 21:46:04 +00001906 OS << "SDNode *Select_DBG_LABEL(const SDValue &N) {\n"
1907 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001908 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001909 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001910 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DBG_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001911 << " MVT::Other, Tmp, Chain);\n"
1912 << "}\n\n";
1913
Dan Gohman475871a2008-07-27 21:46:04 +00001914 OS << "SDNode *Select_EH_LABEL(const SDValue &N) {\n"
1915 << " SDValue Chain = N.getOperand(0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001916 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001917 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EH_LABEL,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001919 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001920 << "}\n\n";
1921
Dan Gohman475871a2008-07-27 21:46:04 +00001922 OS << "SDNode *Select_DECLARE(const SDValue &N) {\n"
1923 << " SDValue Chain = N.getOperand(0);\n"
1924 << " SDValue N1 = N.getOperand(1);\n"
1925 << " SDValue N2 = N.getOperand(2);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001926 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00001927 << " CannotYetSelect(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001928 << " }\n"
1929 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1930 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001931 << " SDValue Tmp1 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001932 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001933 << " SDValue Tmp2 = "
Evan Chenga844bde2008-02-02 04:07:54 +00001934 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001935 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::DECLARE,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001936 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001937 << "}\n\n";
1938
Dan Gohman475871a2008-07-27 21:46:04 +00001939 OS << "SDNode *Select_EXTRACT_SUBREG(const SDValue &N) {\n"
1940 << " SDValue N0 = N.getOperand(0);\n"
1941 << " SDValue N1 = N.getOperand(1);\n"
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001942 << " unsigned C = cast<ConstantSDNode>(N1)->getZExtValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001943 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001944 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::EXTRACT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001945 << " N.getValueType(), N0, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001946 << "}\n\n";
1947
Dan Gohman475871a2008-07-27 21:46:04 +00001948 OS << "SDNode *Select_INSERT_SUBREG(const SDValue &N) {\n"
1949 << " SDValue N0 = N.getOperand(0);\n"
1950 << " SDValue N1 = N.getOperand(1);\n"
1951 << " SDValue N2 = N.getOperand(2);\n"
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001952 << " unsigned C = cast<ConstantSDNode>(N2)->getZExtValue();\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001953 << " SDValue Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001954 << " return CurDAG->SelectNodeTo(N.getNode(), TargetInstrInfo::INSERT_SUBREG,\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001955 << " N.getValueType(), N0, N1, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001956 << "}\n\n";
1957
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001958 OS << "// The main instruction selector code.\n"
Dan Gohman475871a2008-07-27 21:46:04 +00001959 << "SDNode *SelectCode(SDValue N) {\n"
Gabor Greifba36cb52008-08-28 21:40:38 +00001960 << " MVT::SimpleValueType NVT = N.getNode()->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001961 << " switch (N.getOpcode()) {\n"
Dan Gohman28c04da2008-11-05 18:30:52 +00001962 << " default:\n"
1963 << " assert(!N.isMachineOpcode() && \"Node already selected!\");\n"
1964 << " break;\n"
1965 << " case ISD::EntryToken: // These nodes remain the same.\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001966 << " case ISD::MEMOPERAND:\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001967 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001968 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001969 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001970 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001971 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001972 << " case ISD::TargetConstantPool:\n"
1973 << " case ISD::TargetFrameIndex:\n"
Bill Wendling056292f2008-09-16 21:48:12 +00001974 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001975 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001976 << " case ISD::TargetGlobalTLSAddress:\n"
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001977 << " case ISD::TargetGlobalAddress:\n"
1978 << " case ISD::TokenFactor:\n"
1979 << " case ISD::CopyFromReg:\n"
1980 << " case ISD::CopyToReg: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001981 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001982 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001983 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001984 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001985 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001986 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001987 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001988 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001989 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
1990 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001991 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001992 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001993 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1994 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001995
Chris Lattner602f6922006-01-04 00:25:00 +00001996 // Loop over all of the case statements, emiting a call to each method we
1997 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001998 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001999 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
2000 PBOI != E; ++PBOI) {
2001 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00002002 // Potentially multiple versions of select for this opcode. One for each
2003 // ValueType of the node (or its first true operand if it doesn't produce a
2004 // result.
2005 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
2006 OpcodeVTMap.find(OpName);
2007 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00002008 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002009 // Keep track of whether we see a pattern that has an iPtr result.
2010 bool HasPtrPattern = false;
2011 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00002012
Evan Cheng425e8c72007-09-04 20:18:28 +00002013 OS << " switch (NVT) {\n";
2014 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
2015 std::string &VTStr = OpVTs[i];
2016 if (VTStr.empty()) {
2017 HasDefaultPattern = true;
2018 continue;
2019 }
Chris Lattner717a6112006-11-14 21:50:27 +00002020
Evan Cheng425e8c72007-09-04 20:18:28 +00002021 // If this is a match on iPTR: don't emit it directly, we need special
2022 // code.
2023 if (VTStr == "_iPTR") {
2024 HasPtrPattern = true;
2025 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002026 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002027 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2028 << " return Select_" << getLegalCName(OpName)
2029 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002030 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002031 OS << " default:\n";
2032
2033 // If there is an iPTR result version of this pattern, emit it here.
2034 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002035 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002036 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2037 }
2038 if (HasDefaultPattern) {
2039 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2040 }
2041 OS << " break;\n";
2042 OS << " }\n";
2043 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002044 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002045 }
Chris Lattner81303322005-09-23 19:36:15 +00002046
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002047 OS << " } // end of big switch.\n\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002048 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2049 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2050 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002051 << " CannotYetSelect(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002052 << " } else {\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002053 << " CannotYetSelectIntrinsic(N);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002054 << " }\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002055 << " return NULL;\n"
2056 << "}\n\n";
2057
2058 OS << "void CannotYetSelect(SDValue N) DISABLE_INLINE {\n"
2059 << " cerr << \"Cannot yet select: \";\n"
2060 << " N.getNode()->dump(CurDAG);\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002061 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002062 << " abort();\n"
Dan Gohman31bd42b2008-09-27 23:53:14 +00002063 << "}\n\n";
2064
2065 OS << "void CannotYetSelectIntrinsic(SDValue N) DISABLE_INLINE {\n"
2066 << " cerr << \"Cannot yet select: \";\n"
2067 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2068 << "N.getOperand(0).getValueType() == MVT::Other))->getZExtValue();\n"
2069 << " cerr << \"intrinsic %\"<< "
2070 << "Intrinsic::getName((Intrinsic::ID)iid);\n"
2071 << " cerr << '\\n';\n"
2072 << " abort();\n"
2073 << "}\n\n";
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002074}
2075
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002076void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002077 EmitSourceFileHeader("DAG Instruction Selector for the " +
2078 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002079
Chris Lattner1f39e292005-09-14 00:09:24 +00002080 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2081 << "// *** instruction selector class. These functions are really "
2082 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002083
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002084 OS << "// Include standard, target-independent definitions and methods used\n"
2085 << "// by the instruction selector.\n";
2086 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002087
Chris Lattner443e3f92008-01-05 22:54:53 +00002088 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002089 EmitPredicateFunctions(OS);
2090
Bill Wendlingf5da1332006-12-07 22:21:48 +00002091 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002092 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002093 I != E; ++I) {
2094 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2095 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002096 DOUT << "\n";
2097 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002098
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002099 // At this point, we have full information about the 'Patterns' we need to
2100 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002101 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002102 EmitInstructionSelector(OS);
2103
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002104}