blob: c6cac7d16a8bd40355a62ac5b9f47e71d8210302 [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"
17#include "llvm/Support/Debug.h"
Chris Lattnerbe8e7212006-10-11 03:35:34 +000018#include "llvm/Support/MathExtras.h"
Bill Wendlingf5da1332006-12-07 22:21:48 +000019#include "llvm/Support/Streams.h"
Jeff Cohena48283b2005-09-25 19:04:43 +000020#include <algorithm>
Chris Lattner54cb8fd2005-09-07 23:44:43 +000021using namespace llvm;
22
Chris Lattnerca559d02005-09-08 21:03:01 +000023//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000024// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000025//
26
Chris Lattner6cefb772008-01-05 22:25:12 +000027/// NodeIsComplexPattern - return true if N is a leaf node and a subclass of
28/// ComplexPattern.
29static bool NodeIsComplexPattern(TreePatternNode *N) {
Evan Cheng0fc71982005-12-08 02:00:36 +000030 return (N->isLeaf() &&
31 dynamic_cast<DefInit*>(N->getLeafValue()) &&
32 static_cast<DefInit*>(N->getLeafValue())->getDef()->
33 isSubClassOf("ComplexPattern"));
34}
35
Chris Lattner6cefb772008-01-05 22:25:12 +000036/// NodeGetComplexPattern - return the pointer to the ComplexPattern if N
37/// is a leaf node and a subclass of ComplexPattern, else it returns NULL.
Evan Cheng0fc71982005-12-08 02:00:36 +000038static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerfe718932008-01-06 01:10:31 +000039 CodeGenDAGPatterns &CGP) {
Evan Cheng0fc71982005-12-08 02:00:36 +000040 if (N->isLeaf() &&
41 dynamic_cast<DefInit*>(N->getLeafValue()) &&
42 static_cast<DefInit*>(N->getLeafValue())->getDef()->
43 isSubClassOf("ComplexPattern")) {
Chris Lattner6cefb772008-01-05 22:25:12 +000044 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
45 ->getDef());
Evan Cheng0fc71982005-12-08 02:00:36 +000046 }
47 return NULL;
48}
49
Chris Lattner05814af2005-09-28 17:57:56 +000050/// getPatternSize - Return the 'size' of this pattern. We want to match large
51/// patterns before small ones. This is used to determine the size of a
52/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000053static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000054 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
55 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Evan Cheng2618d072006-05-17 20:37:59 +000056 P->getExtTypeNum(0) == MVT::isVoid ||
57 P->getExtTypeNum(0) == MVT::Flag ||
58 P->getExtTypeNum(0) == MVT::iPTR) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000059 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000060 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000061 // If the root node is a ConstantSDNode, increases its size.
62 // e.g. (set R32:$dst, 0).
63 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000064 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000065
66 // FIXME: This is a hack to statically increase the priority of patterns
67 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
68 // Later we can allow complexity / cost for each pattern to be (optionally)
69 // specified. To get best possible pattern match we'll need to dynamically
70 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner6cefb772008-01-05 22:25:12 +000071 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000072 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000073 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000074
75 // If this node has some predicate function that must match, it adds to the
76 // complexity of this node.
77 if (!P->getPredicateFn().empty())
78 ++Size;
79
Chris Lattner05814af2005-09-28 17:57:56 +000080 // Count children in the count if they are also nodes.
81 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
82 TreePatternNode *Child = P->getChild(i);
Nate Begemanb73628b2005-12-30 00:12:56 +000083 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000084 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000085 else if (Child->isLeaf()) {
86 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000087 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Evan Cheng4a7c2842006-01-06 22:19:44 +000088 else if (NodeIsComplexPattern(Child))
Chris Lattner6cefb772008-01-05 22:25:12 +000089 Size += getPatternSize(Child, CGP);
Chris Lattner3e179802006-02-03 18:06:02 +000090 else if (!Child->getPredicateFn().empty())
91 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000092 }
Chris Lattner05814af2005-09-28 17:57:56 +000093 }
94
95 return Size;
96}
97
98/// getResultPatternCost - Compute the number of instructions for this pattern.
99/// This is a temporary hack. We should really include the instruction
100/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +0000101static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000102 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +0000103 if (P->isLeaf()) return 0;
104
Evan Chengfbad7082006-02-18 02:33:09 +0000105 unsigned Cost = 0;
106 Record *Op = P->getOperator();
107 if (Op->isSubClassOf("Instruction")) {
108 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +0000109 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Evan Chengfbad7082006-02-18 02:33:09 +0000110 if (II.usesCustomDAGSchedInserter)
111 Cost += 10;
112 }
Chris Lattner05814af2005-09-28 17:57:56 +0000113 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000114 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000115 return Cost;
116}
117
Evan Chenge6f32032006-07-19 00:24:41 +0000118/// getResultPatternCodeSize - Compute the code size of instructions for this
119/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000120static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +0000121 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +0000122 if (P->isLeaf()) return 0;
123
124 unsigned Cost = 0;
125 Record *Op = P->getOperator();
126 if (Op->isSubClassOf("Instruction")) {
127 Cost += Op->getValueAsInt("CodeSize");
128 }
129 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000130 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000131 return Cost;
132}
133
Chris Lattner05814af2005-09-28 17:57:56 +0000134// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
135// In particular, we want to match maximal patterns first and lowest cost within
136// a particular complexity first.
137struct PatternSortingPredicate {
Chris Lattnerfe718932008-01-06 01:10:31 +0000138 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
139 CodeGenDAGPatterns &CGP;
Evan Cheng0fc71982005-12-08 02:00:36 +0000140
Chris Lattner60d81392008-01-05 22:30:17 +0000141 bool operator()(const PatternToMatch *LHS,
142 const PatternToMatch *RHS) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000143 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
144 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Evan Chengc81d2a02006-04-19 20:36:09 +0000145 LHSSize += LHS->getAddedComplexity();
146 RHSSize += RHS->getAddedComplexity();
Chris Lattner05814af2005-09-28 17:57:56 +0000147 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
148 if (LHSSize < RHSSize) return false;
149
150 // If the patterns have equal complexity, compare generated instruction cost
Chris Lattner6cefb772008-01-05 22:25:12 +0000151 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
152 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000153 if (LHSCost < RHSCost) return true;
154 if (LHSCost > RHSCost) return false;
155
Chris Lattner6cefb772008-01-05 22:25:12 +0000156 return getResultPatternSize(LHS->getDstPattern(), CGP) <
157 getResultPatternSize(RHS->getDstPattern(), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +0000158 }
159};
160
Nate Begeman6510b222005-12-01 04:51:06 +0000161/// getRegisterValueType - Look up and return the first ValueType of specified
162/// RegisterClass record
Duncan Sands83ec4b62008-06-06 12:08:01 +0000163static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Chris Lattner22faeab2005-12-05 02:36:37 +0000164 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
165 return RC->getValueTypeNum(0);
Evan Cheng66a48bb2005-12-01 00:18:45 +0000166 return MVT::Other;
167}
168
Chris Lattner72fe91c2005-09-24 00:40:24 +0000169
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000170/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
171/// type information from it.
172static void RemoveAllTypes(TreePatternNode *N) {
Nate Begemanb73628b2005-12-30 00:12:56 +0000173 N->removeTypes();
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000174 if (!N->isLeaf())
175 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
176 RemoveAllTypes(N->getChild(i));
177}
Chris Lattner72fe91c2005-09-24 00:40:24 +0000178
Evan Cheng51fecc82006-01-09 18:27:06 +0000179/// NodeHasProperty - return true if TreePatternNode has the specified
180/// property.
Evan Cheng94b30402006-10-11 21:02:01 +0000181static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000182 CodeGenDAGPatterns &CGP) {
Evan Cheng94b30402006-10-11 21:02:01 +0000183 if (N->isLeaf()) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000184 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Evan Cheng94b30402006-10-11 21:02:01 +0000185 if (CP)
186 return CP->hasProperty(Property);
187 return false;
188 }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000189 Record *Operator = N->getOperator();
190 if (!Operator->isSubClassOf("SDNode")) return false;
191
Chris Lattner6cefb772008-01-05 22:25:12 +0000192 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Evan Cheng7b05bd52005-12-23 22:11:47 +0000193}
194
Evan Cheng94b30402006-10-11 21:02:01 +0000195static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerfe718932008-01-06 01:10:31 +0000196 CodeGenDAGPatterns &CGP) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000197 if (NodeHasProperty(N, Property, CGP))
Evan Cheng7b05bd52005-12-23 22:11:47 +0000198 return true;
Evan Cheng51fecc82006-01-09 18:27:06 +0000199
200 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
201 TreePatternNode *Child = N->getChild(i);
Chris Lattner6cefb772008-01-05 22:25:12 +0000202 if (PatternHasProperty(Child, Property, CGP))
Evan Cheng51fecc82006-01-09 18:27:06 +0000203 return true;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000204 }
205
206 return false;
207}
208
Evan Chengf9d03182008-07-03 08:39:51 +0000209static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
210 return CGP.getSDNodeInfo(Op).getEnumName();
211}
212
213static
214bool DisablePatternForFastISel(TreePatternNode *N, CodeGenDAGPatterns &CGP) {
215 bool isStore = !N->isLeaf() &&
216 getOpcodeName(N->getOperator(), CGP) == "ISD::STORE";
217 if (!isStore && NodeHasProperty(N, SDNPHasChain, CGP))
218 return false;
219
220 bool HasChain = false;
221 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
222 TreePatternNode *Child = N->getChild(i);
223 if (PatternHasProperty(Child, SDNPHasChain, CGP)) {
224 HasChain = true;
225 break;
226 }
227 }
228 return HasChain;
229}
230
Chris Lattnerdc32f982008-01-05 22:43:57 +0000231//===----------------------------------------------------------------------===//
Chris Lattner443e3f92008-01-05 22:54:53 +0000232// Node Transformation emitter implementation.
233//
234void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
235 // Walk the pattern fragments, adding them to a map, which sorts them by
236 // name.
Chris Lattnerfe718932008-01-06 01:10:31 +0000237 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner443e3f92008-01-05 22:54:53 +0000238 NXsByNameTy NXsByName;
239
Chris Lattnerfe718932008-01-06 01:10:31 +0000240 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner443e3f92008-01-05 22:54:53 +0000241 I != E; ++I)
242 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
243
244 OS << "\n// Node transformations.\n";
245
246 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
247 I != E; ++I) {
248 Record *SDNode = I->second.first;
249 std::string Code = I->second.second;
250
251 if (Code.empty()) continue; // Empty code? Skip it.
252
Chris Lattner200c57e2008-01-05 22:58:54 +0000253 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner443e3f92008-01-05 22:54:53 +0000254 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
255
256 OS << "inline SDOperand Transform_" << I->first << "(SDNode *" << C2
257 << ") {\n";
258 if (ClassName != "SDNode")
259 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
260 OS << Code << "\n}\n";
261 }
262}
263
264//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000265// Predicate emitter implementation.
266//
267
268void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
269 OS << "\n// Predicate functions.\n";
270
271 // Walk the pattern fragments, adding them to a map, which sorts them by
272 // name.
273 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
274 PFsByNameTy PFsByName;
275
Chris Lattnerfe718932008-01-06 01:10:31 +0000276 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000277 I != E; ++I)
278 PFsByName.insert(std::make_pair(I->first->getName(), *I));
279
280
281 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
282 I != E; ++I) {
283 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
284 TreePattern *P = I->second.second;
285
286 // If there is a code init for this fragment, emit the predicate code.
287 std::string Code = PatFragRecord->getValueAsCode("Predicate");
288 if (Code.empty()) continue;
289
290 if (P->getOnlyTree()->isLeaf())
291 OS << "inline bool Predicate_" << PatFragRecord->getName()
292 << "(SDNode *N) {\n";
293 else {
294 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000295 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000296 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
297
298 OS << "inline bool Predicate_" << PatFragRecord->getName()
299 << "(SDNode *" << C2 << ") {\n";
300 if (ClassName != "SDNode")
301 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
302 }
303 OS << Code << "\n}\n";
304 }
305
306 OS << "\n\n";
307}
308
309
310//===----------------------------------------------------------------------===//
311// PatternCodeEmitter implementation.
312//
Evan Chengb915f312005-12-09 22:45:35 +0000313class PatternCodeEmitter {
314private:
Chris Lattnerfe718932008-01-06 01:10:31 +0000315 CodeGenDAGPatterns &CGP;
Evan Chengb915f312005-12-09 22:45:35 +0000316
Evan Cheng58e84a62005-12-14 22:02:59 +0000317 // Predicates.
318 ListInit *Predicates;
Evan Cheng59413202006-04-19 18:07:24 +0000319 // Pattern cost.
320 unsigned Cost;
Evan Cheng58e84a62005-12-14 22:02:59 +0000321 // Instruction selector pattern.
322 TreePatternNode *Pattern;
323 // Matched instruction.
324 TreePatternNode *Instruction;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000325
Evan Chengb915f312005-12-09 22:45:35 +0000326 // Node to name mapping
Evan Chengf805c2e2006-01-12 19:35:54 +0000327 std::map<std::string, std::string> VariableMap;
328 // Node to operator mapping
329 std::map<std::string, Record*> OperatorMap;
Evan Chenga58891f2008-02-05 22:50:29 +0000330 // Name of the folded node which produces a flag.
331 std::pair<std::string, unsigned> FoldedFlag;
Evan Chengb915f312005-12-09 22:45:35 +0000332 // Names of all the folded nodes which produce chains.
Evan Cheng1b80f4d2005-12-19 07:18:51 +0000333 std::vector<std::pair<std::string, unsigned> > FoldedChains;
Evan Cheng4326ef52006-10-12 02:08:53 +0000334 // Original input chain(s).
335 std::vector<std::pair<std::string, std::string> > OrigChains;
Evan Chengb4ad33c2006-01-19 01:55:45 +0000336 std::set<std::string> Duplicates;
Evan Chengb915f312005-12-09 22:45:35 +0000337
Dan Gohman69de1932008-02-06 22:27:42 +0000338 /// LSI - Load/Store information.
339 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
340 /// for each memory access. This facilitates the use of AliasAnalysis in
341 /// the backend.
342 std::vector<std::string> LSI;
343
Evan Cheng676d7312006-08-26 00:59:04 +0000344 /// GeneratedCode - This is the buffer that we emit code to. The first int
Chris Lattner8a0604b2006-01-28 20:31:24 +0000345 /// indicates whether this is an exit predicate (something that should be
Evan Cheng676d7312006-08-26 00:59:04 +0000346 /// tested, and if true, the match fails) [when 1], or normal code to emit
347 /// [when 0], or initialization code to emit [when 2].
348 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
Evan Cheng21ad3922006-02-07 00:37:41 +0000349 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
350 /// the set of patterns for each top-level opcode.
Evan Chengf5493192006-08-26 01:02:19 +0000351 std::set<std::string> &GeneratedDecl;
Evan Chengfceb57a2006-07-15 08:45:20 +0000352 /// TargetOpcodes - The target specific opcodes used by the resulting
353 /// instructions.
354 std::vector<std::string> &TargetOpcodes;
Evan Chengf8729402006-07-16 06:12:52 +0000355 std::vector<std::string> &TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000356 /// OutputIsVariadic - Records whether the instruction output pattern uses
357 /// variable_ops. This requires that the Emit function be passed an
358 /// additional argument to indicate where the input varargs operands
359 /// begin.
360 bool &OutputIsVariadic;
361 /// NumInputRootOps - Records the number of operands the root node of the
362 /// input pattern has. This information is used in the generated code to
363 /// pass to Emit functions when variable_ops processing is needed.
364 unsigned &NumInputRootOps;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000365
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000366 std::string ChainName;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000367 unsigned TmpNo;
Evan Chengfceb57a2006-07-15 08:45:20 +0000368 unsigned OpcNo;
Evan Chengf8729402006-07-16 06:12:52 +0000369 unsigned VTNo;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000370
371 void emitCheck(const std::string &S) {
372 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000373 GeneratedCode.push_back(std::make_pair(1, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000374 }
375 void emitCode(const std::string &S) {
376 if (!S.empty())
Evan Cheng676d7312006-08-26 00:59:04 +0000377 GeneratedCode.push_back(std::make_pair(0, S));
378 }
379 void emitInit(const std::string &S) {
380 if (!S.empty())
381 GeneratedCode.push_back(std::make_pair(2, S));
Chris Lattner8a0604b2006-01-28 20:31:24 +0000382 }
Evan Chengf5493192006-08-26 01:02:19 +0000383 void emitDecl(const std::string &S) {
Evan Cheng21ad3922006-02-07 00:37:41 +0000384 assert(!S.empty() && "Invalid declaration");
Evan Chengf5493192006-08-26 01:02:19 +0000385 GeneratedDecl.insert(S);
Evan Cheng21ad3922006-02-07 00:37:41 +0000386 }
Evan Chengfceb57a2006-07-15 08:45:20 +0000387 void emitOpcode(const std::string &Opc) {
388 TargetOpcodes.push_back(Opc);
389 OpcNo++;
390 }
Evan Chengf8729402006-07-16 06:12:52 +0000391 void emitVT(const std::string &VT) {
392 TargetVTs.push_back(VT);
393 VTNo++;
394 }
Evan Chengb915f312005-12-09 22:45:35 +0000395public:
Chris Lattnerfe718932008-01-06 01:10:31 +0000396 PatternCodeEmitter(CodeGenDAGPatterns &cgp, ListInit *preds,
Evan Cheng58e84a62005-12-14 22:02:59 +0000397 TreePatternNode *pattern, TreePatternNode *instr,
Evan Cheng676d7312006-08-26 00:59:04 +0000398 std::vector<std::pair<unsigned, std::string> > &gc,
Evan Chengf5493192006-08-26 01:02:19 +0000399 std::set<std::string> &gd,
Evan Chengfceb57a2006-07-15 08:45:20 +0000400 std::vector<std::string> &to,
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000401 std::vector<std::string> &tv,
402 bool &oiv,
403 unsigned &niro)
Chris Lattner6cefb772008-01-05 22:25:12 +0000404 : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr),
Evan Cheng676d7312006-08-26 00:59:04 +0000405 GeneratedCode(gc), GeneratedDecl(gd),
406 TargetOpcodes(to), TargetVTs(tv),
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000407 OutputIsVariadic(oiv), NumInputRootOps(niro),
Chris Lattner706d2d32006-08-09 16:44:44 +0000408 TmpNo(0), OpcNo(0), VTNo(0) {}
Evan Chengb915f312005-12-09 22:45:35 +0000409
410 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
411 /// if the match fails. At this point, we already know that the opcode for N
412 /// matches, and the SDNode for the result has the RootName specified name.
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000413 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
414 const std::string &RootName, const std::string &ChainSuffix,
415 bool &FoundChain) {
Dan Gohman69de1932008-02-06 22:27:42 +0000416
417 // Save loads/stores matched by a pattern.
418 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang28873102008-06-25 08:15:39 +0000419 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman69de1932008-02-06 22:27:42 +0000420 LSI.push_back(RootName);
Dan Gohman69de1932008-02-06 22:27:42 +0000421 }
422
Evan Chenge41bf822006-02-05 06:43:12 +0000423 bool isRoot = (P == NULL);
Evan Cheng58e84a62005-12-14 22:02:59 +0000424 // Emit instruction predicates. Each predicate is just a string for now.
425 if (isRoot) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000426 // Record input varargs info.
427 NumInputRootOps = N->getNumChildren();
428
Evan Chengf9d03182008-07-03 08:39:51 +0000429 if (DisablePatternForFastISel(N, CGP))
430 emitCheck("!FastISel");
431
Chris Lattner8a0604b2006-01-28 20:31:24 +0000432 std::string PredicateCheck;
Evan Cheng58e84a62005-12-14 22:02:59 +0000433 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
434 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
435 Record *Def = Pred->getDef();
Chris Lattner8a0604b2006-01-28 20:31:24 +0000436 if (!Def->isSubClassOf("Predicate")) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000437#ifndef NDEBUG
438 Def->dump();
439#endif
Evan Cheng58e84a62005-12-14 22:02:59 +0000440 assert(0 && "Unknown predicate type!");
441 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000442 if (!PredicateCheck.empty())
Chris Lattnerbc7fa522006-09-19 00:41:36 +0000443 PredicateCheck += " && ";
Chris Lattner67a202b2006-01-28 20:43:52 +0000444 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
Evan Cheng58e84a62005-12-14 22:02:59 +0000445 }
446 }
Chris Lattner8a0604b2006-01-28 20:31:24 +0000447
448 emitCheck(PredicateCheck);
Evan Cheng58e84a62005-12-14 22:02:59 +0000449 }
450
Evan Chengb915f312005-12-09 22:45:35 +0000451 if (N->isLeaf()) {
452 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000453 emitCheck("cast<ConstantSDNode>(" + RootName +
Chris Lattner67a202b2006-01-28 20:43:52 +0000454 ")->getSignExtended() == " + itostr(II->getValue()));
Evan Chengb915f312005-12-09 22:45:35 +0000455 return;
456 } else if (!NodeIsComplexPattern(N)) {
457 assert(0 && "Cannot match this as a leaf value!");
458 abort();
459 }
460 }
461
Chris Lattner488580c2006-01-28 19:06:51 +0000462 // If this node has a name associated with it, capture it in VariableMap. If
Evan Chengb915f312005-12-09 22:45:35 +0000463 // we already saw this in the pattern, emit code to verify dagness.
464 if (!N->getName().empty()) {
465 std::string &VarMapEntry = VariableMap[N->getName()];
466 if (VarMapEntry.empty()) {
467 VarMapEntry = RootName;
468 } else {
469 // If we get here, this is a second reference to a specific name. Since
470 // we already have checked that the first reference is valid, we don't
471 // have to recursively match it, just check that it's the same as the
472 // previously named thing.
Chris Lattner67a202b2006-01-28 20:43:52 +0000473 emitCheck(VarMapEntry + " == " + RootName);
Evan Chengb915f312005-12-09 22:45:35 +0000474 return;
475 }
Evan Chengf805c2e2006-01-12 19:35:54 +0000476
477 if (!N->isLeaf())
478 OperatorMap[N->getName()] = N->getOperator();
Evan Chengb915f312005-12-09 22:45:35 +0000479 }
480
481
482 // Emit code to load the child nodes and match their contents recursively.
483 unsigned OpNo = 0;
Chris Lattner6cefb772008-01-05 22:25:12 +0000484 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
485 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Evan Cheng1feeeec2006-01-26 19:13:45 +0000486 bool EmittedUseCheck = false;
Evan Cheng86217892005-12-12 19:37:43 +0000487 if (HasChain) {
Evan Cheng76356d92006-01-20 01:11:03 +0000488 if (NodeHasChain)
489 OpNo = 1;
Evan Chengb915f312005-12-09 22:45:35 +0000490 if (!isRoot) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000491 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000492 emitCheck(RootName + ".hasOneUse()");
Evan Cheng1feeeec2006-01-26 19:13:45 +0000493 EmittedUseCheck = true;
Evan Chenge41bf822006-02-05 06:43:12 +0000494 if (NodeHasChain) {
Evan Chenge41bf822006-02-05 06:43:12 +0000495 // If the immediate use can somehow reach this node through another
496 // path, then can't fold it either or it will create a cycle.
497 // e.g. In the following diagram, XX can reach ld through YY. If
498 // ld is folded into XX, then YY is both a predecessor and a successor
499 // of XX.
500 //
501 // [ld]
502 // ^ ^
503 // | |
504 // / \---
505 // / [YY]
506 // | ^
507 // [XX]-------|
Evan Chengf9d03182008-07-03 08:39:51 +0000508 bool NeedCheck = P != Pattern;
509 if (!NeedCheck) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000510 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000511 NeedCheck =
Chris Lattner6cefb772008-01-05 22:25:12 +0000512 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
513 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
514 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Evan Chengce1381a2006-10-14 08:30:15 +0000515 PInfo.getNumOperands() > 1 ||
Evan Cheng94b30402006-10-11 21:02:01 +0000516 PInfo.hasProperty(SDNPHasChain) ||
517 PInfo.hasProperty(SDNPInFlag) ||
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000518 PInfo.hasProperty(SDNPOptInFlag);
519 }
520
521 if (NeedCheck) {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000522 std::string ParentName(RootName.begin(), RootName.end()-1);
Chris Lattner706d2d32006-08-09 16:44:44 +0000523 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
Evan Chengce1381a2006-10-14 08:30:15 +0000524 ".Val, N.Val)");
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000525 }
Evan Chenge41bf822006-02-05 06:43:12 +0000526 }
Evan Chengb915f312005-12-09 22:45:35 +0000527 }
Evan Chenge41bf822006-02-05 06:43:12 +0000528
Evan Chengc15d18c2006-01-27 22:13:45 +0000529 if (NodeHasChain) {
Evan Cheng4326ef52006-10-12 02:08:53 +0000530 if (FoundChain) {
531 emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || "
532 "IsChainCompatible(" + ChainName + ".Val, " +
533 RootName + ".Val))");
534 OrigChains.push_back(std::make_pair(ChainName, RootName));
535 } else
Evan Chenge6389932006-07-21 22:19:51 +0000536 FoundChain = true;
Evan Chenge4a8a6e2006-02-03 06:22:41 +0000537 ChainName = "Chain" + ChainSuffix;
Evan Cheng676d7312006-08-26 00:59:04 +0000538 emitInit("SDOperand " + ChainName + " = " + RootName +
Evan Chenge6389932006-07-21 22:19:51 +0000539 ".getOperand(0);");
Evan Cheng1cf6db22006-01-06 00:41:12 +0000540 }
Evan Chengb915f312005-12-09 22:45:35 +0000541 }
542
Evan Cheng54597732006-01-26 00:22:25 +0000543 // Don't fold any node which reads or writes a flag and has multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000544 // FIXME: We really need to separate the concepts of flag and "glue". Those
Evan Cheng54597732006-01-26 00:22:25 +0000545 // real flag results, e.g. X86CMP output, can have multiple uses.
Evan Chenge41bf822006-02-05 06:43:12 +0000546 // FIXME: If the optional incoming flag does not exist. Then it is ok to
547 // fold it.
Evan Cheng1feeeec2006-01-26 19:13:45 +0000548 if (!isRoot &&
Chris Lattner6cefb772008-01-05 22:25:12 +0000549 (PatternHasProperty(N, SDNPInFlag, CGP) ||
550 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
551 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Evan Cheng1feeeec2006-01-26 19:13:45 +0000552 if (!EmittedUseCheck) {
Chris Lattner8a0604b2006-01-28 20:31:24 +0000553 // Multiple uses of actual result?
Chris Lattner67a202b2006-01-28 20:43:52 +0000554 emitCheck(RootName + ".hasOneUse()");
Evan Cheng54597732006-01-26 00:22:25 +0000555 }
556 }
557
Evan Chengd3eea902006-10-09 21:02:17 +0000558 // If there is a node predicate for this, emit the call.
559 if (!N->getPredicateFn().empty())
560 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
561
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000562
Chris Lattner39e73f72006-10-11 04:05:55 +0000563 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
564 // a constant without a predicate fn that has more that one bit set, handle
565 // this as a special case. This is usually for targets that have special
566 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
567 // handling stuff). Using these instructions is often far more efficient
568 // than materializing the constant. Unfortunately, both the instcombiner
569 // and the dag combiner can often infer that bits are dead, and thus drop
570 // them from the mask in the dag. For example, it might turn 'AND X, 255'
571 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
572 // to handle this.
573 if (!N->isLeaf() &&
574 (N->getOperator()->getName() == "and" ||
575 N->getOperator()->getName() == "or") &&
576 N->getChild(1)->isLeaf() &&
577 N->getChild(1)->getPredicateFn().empty()) {
578 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
579 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
580 emitInit("SDOperand " + RootName + "0" + " = " +
581 RootName + ".getOperand(" + utostr(0) + ");");
582 emitInit("SDOperand " + RootName + "1" + " = " +
583 RootName + ".getOperand(" + utostr(1) + ");");
584
585 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
586 const char *MaskPredicate = N->getOperator()->getName() == "or"
587 ? "CheckOrMask(" : "CheckAndMask(";
588 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
589 RootName + "1), " + itostr(II->getValue()) + ")");
590
Christopher Lamb85356242008-01-31 07:27:46 +0000591 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Chris Lattner39e73f72006-10-11 04:05:55 +0000592 ChainSuffix + utostr(0), FoundChain);
593 return;
594 }
595 }
596 }
597
Evan Chengb915f312005-12-09 22:45:35 +0000598 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
Evan Cheng676d7312006-08-26 00:59:04 +0000599 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000600 RootName + ".getOperand(" +utostr(OpNo) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000601
Christopher Lamb85356242008-01-31 07:27:46 +0000602 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000603 ChainSuffix + utostr(OpNo), FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +0000604 }
605
Evan Cheng676d7312006-08-26 00:59:04 +0000606 // Handle cases when root is a complex pattern.
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000607 const ComplexPattern *CP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000608 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000609 std::string Fn = CP->getSelectFunc();
610 unsigned NumOps = CP->getNumOperands();
611 for (unsigned i = 0; i < NumOps; ++i) {
612 emitDecl("CPTmp" + utostr(i));
613 emitCode("SDOperand CPTmp" + utostr(i) + ";");
614 }
Evan Cheng94b30402006-10-11 21:02:01 +0000615 if (CP->hasProperty(SDNPHasChain)) {
616 emitDecl("CPInChain");
617 emitDecl("Chain" + ChainSuffix);
618 emitCode("SDOperand CPInChain;");
619 emitCode("SDOperand Chain" + ChainSuffix + ";");
620 }
Evan Cheng676d7312006-08-26 00:59:04 +0000621
Evan Cheng811731e2006-11-08 20:31:10 +0000622 std::string Code = Fn + "(" + RootName + ", " + RootName;
Evan Cheng676d7312006-08-26 00:59:04 +0000623 for (unsigned i = 0; i < NumOps; i++)
624 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000625 if (CP->hasProperty(SDNPHasChain)) {
626 ChainName = "Chain" + ChainSuffix;
627 Code += ", CPInChain, Chain" + ChainSuffix;
628 }
Evan Cheng676d7312006-08-26 00:59:04 +0000629 emitCheck(Code + ")");
630 }
Evan Chengb915f312005-12-09 22:45:35 +0000631 }
Chris Lattner39e73f72006-10-11 04:05:55 +0000632
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000633 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb85356242008-01-31 07:27:46 +0000634 const std::string &RootName,
635 const std::string &ParentRootName,
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000636 const std::string &ChainSuffix, bool &FoundChain) {
637 if (!Child->isLeaf()) {
638 // If it's not a leaf, recursively match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000639 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000640 emitCheck(RootName + ".getOpcode() == " +
641 CInfo.getEnumName());
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000642 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Chenga58891f2008-02-05 22:50:29 +0000643 bool HasChain = false;
644 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
645 HasChain = true;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000646 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Chenga58891f2008-02-05 22:50:29 +0000647 }
648 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
649 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
650 "Pattern folded multiple nodes which produce flags?");
651 FoldedFlag = std::make_pair(RootName,
652 CInfo.getNumResults() + (unsigned)HasChain);
653 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000654 } else {
655 // If this child has a name associated with it, capture it in VarMap. If
656 // we already saw this in the pattern, emit code to verify dagness.
657 if (!Child->getName().empty()) {
658 std::string &VarMapEntry = VariableMap[Child->getName()];
659 if (VarMapEntry.empty()) {
660 VarMapEntry = RootName;
661 } else {
662 // If we get here, this is a second reference to a specific name.
663 // Since we already have checked that the first reference is valid,
664 // we don't have to recursively match it, just check that it's the
665 // same as the previously named thing.
666 emitCheck(VarMapEntry + " == " + RootName);
667 Duplicates.insert(RootName);
668 return;
669 }
670 }
671
672 // Handle leaves of various types.
673 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
674 Record *LeafRec = DI->getDef();
Chris Lattner646085d2006-11-14 21:18:40 +0000675 if (LeafRec->isSubClassOf("RegisterClass") ||
676 LeafRec->getName() == "ptr_rc") {
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000677 // Handle register references. Nothing to do here.
678 } else if (LeafRec->isSubClassOf("Register")) {
679 // Handle register references.
680 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
681 // Handle complex pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +0000682 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000683 std::string Fn = CP->getSelectFunc();
684 unsigned NumOps = CP->getNumOperands();
685 for (unsigned i = 0; i < NumOps; ++i) {
686 emitDecl("CPTmp" + utostr(i));
687 emitCode("SDOperand CPTmp" + utostr(i) + ";");
688 }
Evan Cheng94b30402006-10-11 21:02:01 +0000689 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner6cefb772008-01-05 22:25:12 +0000690 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Evan Cheng94b30402006-10-11 21:02:01 +0000691 FoldedChains.push_back(std::make_pair("CPInChain",
692 PInfo.getNumResults()));
693 ChainName = "Chain" + ChainSuffix;
694 emitDecl("CPInChain");
695 emitDecl(ChainName);
696 emitCode("SDOperand CPInChain;");
697 emitCode("SDOperand " + ChainName + ";");
698 }
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000699
Christopher Lamb85356242008-01-31 07:27:46 +0000700 std::string Code = Fn + "(";
701 if (CP->hasAttribute(CPAttrParentAsRoot)) {
702 Code += ParentRootName + ", ";
703 } else {
704 Code += "N, ";
705 }
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000706 if (CP->hasProperty(SDNPHasChain)) {
707 std::string ParentName(RootName.begin(), RootName.end()-1);
Evan Cheng811731e2006-11-08 20:31:10 +0000708 Code += ParentName + ", ";
Evan Cheng13e9e9c2006-10-16 06:33:44 +0000709 }
710 Code += RootName;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000711 for (unsigned i = 0; i < NumOps; i++)
712 Code += ", CPTmp" + utostr(i);
Evan Cheng94b30402006-10-11 21:02:01 +0000713 if (CP->hasProperty(SDNPHasChain))
714 Code += ", CPInChain, Chain" + ChainSuffix;
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000715 emitCheck(Code + ")");
716 } else if (LeafRec->getName() == "srcvalue") {
717 // Place holder for SRCVALUE nodes. Nothing to do here.
718 } else if (LeafRec->isSubClassOf("ValueType")) {
719 // Make sure this is the specified value type.
720 emitCheck("cast<VTSDNode>(" + RootName +
721 ")->getVT() == MVT::" + LeafRec->getName());
722 } else if (LeafRec->isSubClassOf("CondCode")) {
723 // Make sure this is the specified cond code.
724 emitCheck("cast<CondCodeSDNode>(" + RootName +
725 ")->get() == ISD::" + LeafRec->getName());
726 } else {
727#ifndef NDEBUG
728 Child->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +0000729 cerr << " ";
Chris Lattnerbe8e7212006-10-11 03:35:34 +0000730#endif
731 assert(0 && "Unknown leaf type!");
732 }
733
734 // If there is a node predicate for this, emit the call.
735 if (!Child->getPredicateFn().empty())
736 emitCheck(Child->getPredicateFn() + "(" + RootName +
737 ".Val)");
738 } else if (IntInit *II =
739 dynamic_cast<IntInit*>(Child->getLeafValue())) {
740 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
741 unsigned CTmp = TmpNo++;
742 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
743 RootName + ")->getSignExtended();");
744
745 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
746 } else {
747#ifndef NDEBUG
748 Child->dump();
749#endif
750 assert(0 && "Unknown leaf type!");
751 }
752 }
753 }
Evan Chengb915f312005-12-09 22:45:35 +0000754
755 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
756 /// we actually have to build a DAG!
Evan Cheng676d7312006-08-26 00:59:04 +0000757 std::vector<std::string>
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000758 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Evan Cheng676d7312006-08-26 00:59:04 +0000759 bool InFlagDecled, bool ResNodeDecled,
760 bool LikeLeaf = false, bool isRoot = false) {
761 // List of arguments of getTargetNode() or SelectNodeTo().
762 std::vector<std::string> NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000763 // This is something selected from the pattern we matched.
764 if (!N->getName().empty()) {
Scott Michel6be48d42008-01-29 02:29:31 +0000765 const std::string &VarName = N->getName();
766 std::string Val = VariableMap[VarName];
767 bool ModifiedVal = false;
Scott Michel0123b7d2008-02-15 23:05:48 +0000768 if (Val.empty()) {
Bill Wendling27926af2008-02-26 10:45:29 +0000769 cerr << "Variable '" << VarName << " referenced but not defined "
770 << "and not caught earlier!\n";
771 abort();
Scott Michel0123b7d2008-02-15 23:05:48 +0000772 }
Evan Chengb915f312005-12-09 22:45:35 +0000773 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
774 // Already selected this operand, just return the tmpval.
Evan Cheng676d7312006-08-26 00:59:04 +0000775 NodeOps.push_back(Val);
776 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +0000777 }
778
779 const ComplexPattern *CP;
780 unsigned ResNo = TmpNo++;
Evan Chengb915f312005-12-09 22:45:35 +0000781 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
Nate Begemanb73628b2005-12-30 00:12:56 +0000782 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
Chris Lattner78593132006-01-29 20:01:35 +0000783 std::string CastType;
Scott Michel6be48d42008-01-29 02:29:31 +0000784 std::string TmpVar = "Tmp" + utostr(ResNo);
Nate Begemanb73628b2005-12-30 00:12:56 +0000785 switch (N->getTypeNum(0)) {
Chris Lattnerd8a17282007-01-17 07:45:12 +0000786 default:
787 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
788 << " type as an immediate constant. Aborting\n";
789 abort();
Chris Lattner78593132006-01-29 20:01:35 +0000790 case MVT::i1: CastType = "bool"; break;
791 case MVT::i8: CastType = "unsigned char"; break;
792 case MVT::i16: CastType = "unsigned short"; break;
793 case MVT::i32: CastType = "unsigned"; break;
794 case MVT::i64: CastType = "uint64_t"; break;
Evan Chengb915f312005-12-09 22:45:35 +0000795 }
Scott Michel6be48d42008-01-29 02:29:31 +0000796 emitCode("SDOperand " + TmpVar +
Evan Chengfceb57a2006-07-15 08:45:20 +0000797 " = CurDAG->getTargetConstant(((" + CastType +
798 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
799 getEnumName(N->getTypeNum(0)) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +0000800 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
801 // value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000802 Val = TmpVar;
803 ModifiedVal = true;
804 NodeOps.push_back(Val);
Nate Begemane1795842008-02-14 08:57:00 +0000805 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
806 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
807 std::string TmpVar = "Tmp" + utostr(ResNo);
808 emitCode("SDOperand " + TmpVar +
809 " = CurDAG->getTargetConstantFP(cast<ConstantFPSDNode>(" +
810 Val + ")->getValueAPF(), cast<ConstantFPSDNode>(" + Val +
811 ")->getValueType(0));");
812 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
813 // value if used multiple times by this pattern result.
814 Val = TmpVar;
815 ModifiedVal = true;
816 NodeOps.push_back(Val);
Evan Chengbb48e332006-01-12 07:54:57 +0000817 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
Evan Chengf805c2e2006-01-12 19:35:54 +0000818 Record *Op = OperatorMap[N->getName()];
819 // Transform ExternalSymbol to TargetExternalSymbol
820 if (Op && Op->getName() == "externalsym") {
Scott Michel6be48d42008-01-29 02:29:31 +0000821 std::string TmpVar = "Tmp"+utostr(ResNo);
822 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000823 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
Evan Cheng2618d072006-05-17 20:37:59 +0000824 Val + ")->getSymbol(), " +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000825 getEnumName(N->getTypeNum(0)) + ");");
Chris Lattner64906972006-09-21 18:28:27 +0000826 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
827 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000828 Val = TmpVar;
829 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000830 }
Scott Michel6be48d42008-01-29 02:29:31 +0000831 NodeOps.push_back(Val);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000832 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
833 || N->getOperator()->getName() == "tglobaltlsaddr")) {
Evan Chengf805c2e2006-01-12 19:35:54 +0000834 Record *Op = OperatorMap[N->getName()];
835 // Transform GlobalAddress to TargetGlobalAddress
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000836 if (Op && (Op->getName() == "globaladdr" ||
837 Op->getName() == "globaltlsaddr")) {
Scott Michel6be48d42008-01-29 02:29:31 +0000838 std::string TmpVar = "Tmp" + utostr(ResNo);
839 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Chris Lattner8a0604b2006-01-28 20:31:24 +0000840 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
Evan Cheng2618d072006-05-17 20:37:59 +0000841 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
Chris Lattner8a0604b2006-01-28 20:31:24 +0000842 ");");
Chris Lattner64906972006-09-21 18:28:27 +0000843 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
844 // this value if used multiple times by this pattern result.
Scott Michel6be48d42008-01-29 02:29:31 +0000845 Val = TmpVar;
846 ModifiedVal = true;
Chris Lattner8a0604b2006-01-28 20:31:24 +0000847 }
Evan Cheng676d7312006-08-26 00:59:04 +0000848 NodeOps.push_back(Val);
Scott Michel6be48d42008-01-29 02:29:31 +0000849 } else if (!N->isLeaf()
850 && (N->getOperator()->getName() == "texternalsym"
851 || N->getOperator()->getName() == "tconstpool")) {
852 // Do not rewrite the variable name, since we don't generate a new
853 // temporary.
Evan Cheng676d7312006-08-26 00:59:04 +0000854 NodeOps.push_back(Val);
Chris Lattner6cefb772008-01-05 22:25:12 +0000855 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Evan Cheng676d7312006-08-26 00:59:04 +0000856 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
857 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
858 NodeOps.push_back("CPTmp" + utostr(i));
Evan Chengb0793f92006-05-25 00:21:44 +0000859 }
Evan Chengb915f312005-12-09 22:45:35 +0000860 } else {
Evan Cheng676d7312006-08-26 00:59:04 +0000861 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
Evan Cheng863bf5a2006-03-20 22:53:06 +0000862 // node even if it isn't one. Don't select it.
Evan Cheng676d7312006-08-26 00:59:04 +0000863 if (!LikeLeaf) {
864 emitCode("AddToISelQueue(" + Val + ");");
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")) {
Evan Cheng676d7312006-08-26 00:59:04 +0000883 emitCode("SDOperand 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") {
889 emitCode("SDOperand Tmp" + utostr(ResNo) +
890 " = 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!");
Evan Cheng676d7312006-08-26 00:59:04 +0000898 emitCode("SDOperand 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)
Evan Chengf037ca62006-08-27 08:11:28 +0000950 emitCode("SmallVector<SDOperand, 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.
966 emitCode("SmallVector<SDOperand, 8> InChains;");
967 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
968 emitCode("if (" + OrigChains[i].first + ".Val != " +
969 OrigChains[i].second + ".Val) {");
970 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
971 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
972 emitCode("}");
973 }
974 emitCode("AddToISelQueue(" + ChainName + ");");
975 emitCode("InChains.push_back(" + ChainName + ");");
976 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
977 "&InChains[0], InChains.size());");
978 }
979
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000980 // Loop over all of the operands of the instruction pattern, emitting code
981 // to fill them all in. The node 'N' usually has number children equal to
982 // the number of input operands of the instruction. However, in cases
983 // where there are predicate operands for an instruction, we need to fill
984 // in the 'execute always' values. Match up the node operands to the
985 // instruction operands to do this.
Evan Cheng676d7312006-08-26 00:59:04 +0000986 std::vector<std::string> AllOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000987 for (unsigned ChildNo = 0, InstOpNo = NumResults;
988 InstOpNo != II.OperandList.size(); ++InstOpNo) {
989 std::vector<std::string> Ops;
990
Dan Gohmand35121a2008-05-29 19:57:41 +0000991 // Determine what to emit for this operand.
Evan Cheng59039632007-05-08 21:04:07 +0000992 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohmane4c67cd2008-05-31 02:11:25 +0000993 if ((OperandNode->isSubClassOf("PredicateOperand") ||
994 OperandNode->isSubClassOf("OptionalDefOperand")) &&
995 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohmand35121a2008-05-29 19:57:41 +0000996 // This is a predicate or optional def operand; emit the
Evan Chenga9559392007-07-06 01:05:26 +0000997 // 'default ops' operands.
998 const DAGDefaultOperand &DefaultOp =
Chris Lattner6cefb772008-01-05 22:25:12 +0000999 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Evan Chenga9559392007-07-06 01:05:26 +00001000 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Cheng30729b42007-09-17 22:26:41 +00001001 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001002 InFlagDecled, ResNodeDecled);
1003 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1004 }
Dan Gohmand35121a2008-05-29 19:57:41 +00001005 } else {
1006 // Otherwise this is a normal operand or a predicate operand without
1007 // 'execute always'; emit it.
1008 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
1009 InFlagDecled, ResNodeDecled);
1010 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
1011 ++ChildNo;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +00001012 }
Evan Chengb915f312005-12-09 22:45:35 +00001013 }
1014
Evan Chengb915f312005-12-09 22:45:35 +00001015 // Emit all the chain and CopyToReg stuff.
Evan Cheng045953c2006-05-10 00:05:46 +00001016 bool ChainEmitted = NodeHasChain;
1017 if (NodeHasChain)
Evan Cheng676d7312006-08-26 00:59:04 +00001018 emitCode("AddToISelQueue(" + ChainName + ");");
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) {
1024 emitCode("SDOperand InFlag(0, 0);");
1025 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);");
1030 emitCode(" AddToISelQueue(InFlag);");
1031 emitCode("}");
1032 }
Evan Chengbc6b86a2006-06-14 19:27:50 +00001033 }
Evan Chengb915f312005-12-09 22:45:35 +00001034
Evan Chengb915f312005-12-09 22:45:35 +00001035 unsigned ResNo = TmpNo++;
Evan Cheng3eff89b2006-05-10 02:47:57 +00001036 if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag ||
Evan Chengef61ed32007-09-07 23:59:02 +00001037 NodeHasOptInFlag || HasImpResults) {
Evan Chenge945f4d2006-06-14 22:22:20 +00001038 std::string Code;
1039 std::string Code2;
1040 std::string NodeName;
1041 if (!isRoot) {
1042 NodeName = "Tmp" + utostr(ResNo);
Dan Gohmana6a1ab32007-07-24 22:58:00 +00001043 Code2 = "SDOperand " + NodeName + "(";
Evan Cheng9789aaa2006-01-24 20:46:50 +00001044 } else {
Evan Chenge945f4d2006-06-14 22:22:20 +00001045 NodeName = "ResNode";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +00001046 if (!ResNodeDecled) {
Evan Cheng676d7312006-08-26 00:59:04 +00001047 Code2 = "SDNode *" + NodeName + " = ";
Lauro Ramos Venancio195c6c22007-04-26 17:03:22 +00001048 ResNodeDecled = true;
1049 } else
Evan Cheng676d7312006-08-26 00:59:04 +00001050 Code2 = NodeName + " = ";
Evan Chengbcecf332005-12-17 01:19:28 +00001051 }
Evan Chengf037ca62006-08-27 08:11:28 +00001052
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001053 Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
Evan Chengf037ca62006-08-27 08:11:28 +00001054 unsigned OpsNo = OpcNo;
Evan Chengfceb57a2006-07-15 08:45:20 +00001055 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
Evan Chenge945f4d2006-06-14 22:22:20 +00001056
1057 // Output order: results, chain, flags
1058 // Result types.
Evan Chengf8729402006-07-16 06:12:52 +00001059 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1060 Code += ", VT" + utostr(VTNo);
1061 emitVT(getEnumName(N->getTypeNum(0)));
1062 }
Evan Chengef61ed32007-09-07 23:59:02 +00001063 // Add types for implicit results in physical registers, scheduler will
1064 // care of adding copyfromreg nodes.
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001065 for (unsigned i = 0; i < NumDstRegs; i++) {
1066 Record *RR = DstRegs[i];
1067 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001068 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001069 Code += ", " + getEnumName(RVT);
Evan Chengef61ed32007-09-07 23:59:02 +00001070 }
1071 }
Evan Chenge945f4d2006-06-14 22:22:20 +00001072 if (NodeHasChain)
1073 Code += ", MVT::Other";
1074 if (NodeHasOutFlag)
1075 Code += ", MVT::Flag";
1076
1077 // Inputs.
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001078 if (IsVariadic) {
Evan Chengf037ca62006-08-27 08:11:28 +00001079 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1080 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1081 AllOps.clear();
Evan Chenge945f4d2006-06-14 22:22:20 +00001082
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001083 // Figure out whether any operands at the end of the op list are not
1084 // part of the variable section.
1085 std::string EndAdjust;
Evan Chenge945f4d2006-06-14 22:22:20 +00001086 if (NodeHasInFlag || HasImpInputs)
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001087 EndAdjust = "-1"; // Always has one flag.
1088 else if (NodeHasOptInFlag)
1089 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1090
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001091 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
Chris Lattner7c3a96b2006-11-14 18:41:38 +00001092 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1093
Evan Cheng676d7312006-08-26 00:59:04 +00001094 emitCode(" AddToISelQueue(N.getOperand(i));");
Evan Chengf037ca62006-08-27 08:11:28 +00001095 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
Evan Chenge945f4d2006-06-14 22:22:20 +00001096 emitCode("}");
1097 }
1098
Dan Gohman37cdad32008-06-02 17:40:38 +00001099 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1100 // this pattern.
1101 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1102 std::vector<std::string>::const_iterator mi, mie;
1103 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
1104 emitCode("SDOperand LSI_" + *mi + " = "
Mon P Wang28873102008-06-25 08:15:39 +00001105 "CurDAG->getMemOperand(cast<MemSDNode>(" +
Dan Gohman37cdad32008-06-02 17:40:38 +00001106 *mi + ")->getMemOperand());");
1107 if (IsVariadic)
1108 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1109 else
1110 AllOps.push_back("LSI_" + *mi);
1111 }
1112 }
1113
Evan Chenge945f4d2006-06-14 22:22:20 +00001114 if (NodeHasChain) {
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001115 if (IsVariadic)
Evan Chengf037ca62006-08-27 08:11:28 +00001116 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +00001117 else
Evan Chengf037ca62006-08-27 08:11:28 +00001118 AllOps.push_back(ChainName);
Evan Chenge945f4d2006-06-14 22:22:20 +00001119 }
1120
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001121 if (IsVariadic) {
Evan Chengf037ca62006-08-27 08:11:28 +00001122 if (NodeHasInFlag || HasImpInputs)
1123 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1124 else if (NodeHasOptInFlag) {
1125 emitCode("if (HasInFlag)");
1126 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1127 }
1128 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1129 ".size()";
1130 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001131 AllOps.push_back("InFlag");
Evan Chenge945f4d2006-06-14 22:22:20 +00001132
Evan Chengf037ca62006-08-27 08:11:28 +00001133 unsigned NumOps = AllOps.size();
1134 if (NumOps) {
1135 if (!NodeHasOptInFlag && NumOps < 4) {
1136 for (unsigned i = 0; i != NumOps; ++i)
1137 Code += ", " + AllOps[i];
1138 } else {
1139 std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { ";
1140 for (unsigned i = 0; i != NumOps; ++i) {
1141 OpsCode += AllOps[i];
1142 if (i != NumOps-1)
1143 OpsCode += ", ";
1144 }
1145 emitCode(OpsCode + " };");
1146 Code += ", Ops" + utostr(OpsNo) + ", ";
1147 if (NodeHasOptInFlag) {
1148 Code += "HasInFlag ? ";
1149 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1150 } else
1151 Code += utostr(NumOps);
1152 }
1153 }
1154
Evan Chenge945f4d2006-06-14 22:22:20 +00001155 if (!isRoot)
1156 Code += "), 0";
1157 emitCode(Code2 + Code + ");");
1158
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00001159 if (NodeHasChain) {
Evan Chenge945f4d2006-06-14 22:22:20 +00001160 // Remember which op produces the chain.
1161 if (!isRoot)
1162 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001163 ".Val, " + utostr(NumResults+NumDstRegs) + ");");
Evan Chenge945f4d2006-06-14 22:22:20 +00001164 else
1165 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001166 ", " + utostr(NumResults+NumDstRegs) + ");");
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +00001167 }
Evan Cheng1b80f4d2005-12-19 07:18:51 +00001168
Evan Cheng676d7312006-08-26 00:59:04 +00001169 if (!isRoot) {
1170 NodeOps.push_back("Tmp" + utostr(ResNo));
1171 return NodeOps;
1172 }
Evan Cheng045953c2006-05-10 00:05:46 +00001173
Evan Cheng06d64702006-08-11 08:59:35 +00001174 bool NeedReplace = false;
Evan Cheng676d7312006-08-26 00:59:04 +00001175 if (NodeHasOutFlag) {
1176 if (!InFlagDecled) {
Dan Gohmana6a1ab32007-07-24 22:58:00 +00001177 emitCode("SDOperand InFlag(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001178 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001179 InFlagDecled = true;
1180 } else
1181 emitCode("InFlag = SDOperand(ResNode, " +
Evan Cheng30729b42007-09-17 22:26:41 +00001182 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001183 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001184
Evan Cheng97938882005-12-22 02:24:50 +00001185 if (FoldedChains.size() > 0) {
Chris Lattner8a0604b2006-01-28 20:31:24 +00001186 std::string Code;
Evan Cheng1b80f4d2005-12-19 07:18:51 +00001187 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++)
Chris Lattner706d2d32006-08-09 16:44:44 +00001188 emitCode("ReplaceUses(SDOperand(" +
Evan Cheng67212a02006-02-09 22:12:27 +00001189 FoldedChains[j].first + ".Val, " +
Chris Lattner706d2d32006-08-09 16:44:44 +00001190 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001191 utostr(NumResults+NumDstRegs) + "));");
Evan Cheng06d64702006-08-11 08:59:35 +00001192 NeedReplace = true;
Evan Chengb915f312005-12-09 22:45:35 +00001193 }
Evan Chengf9fc25d2005-12-19 22:40:04 +00001194
Evan Cheng06d64702006-08-11 08:59:35 +00001195 if (NodeHasOutFlag) {
Evan Chenga58891f2008-02-05 22:50:29 +00001196 if (FoldedFlag.first != "") {
1197 emitCode("ReplaceUses(SDOperand(" + FoldedFlag.first + ".Val, " +
1198 utostr(FoldedFlag.second) + "), InFlag);");
1199 } else {
1200 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
1201 emitCode("ReplaceUses(SDOperand(N.Val, " +
1202 utostr(NumPatResults + (unsigned)InputHasChain)
1203 +"), InFlag);");
1204 }
Evan Cheng06d64702006-08-11 08:59:35 +00001205 NeedReplace = true;
1206 }
1207
Evan Cheng30729b42007-09-17 22:26:41 +00001208 if (NeedReplace && InputHasChain)
1209 emitCode("ReplaceUses(SDOperand(N.Val, " +
1210 utostr(NumPatResults) + "), SDOperand(" + ChainName
1211 + ".Val, " + ChainName + ".ResNo" + "));");
Evan Cheng97938882005-12-22 02:24:50 +00001212
Evan Chenged66e852006-03-09 08:19:11 +00001213 // User does not expect the instruction would produce a chain!
Evan Cheng06d64702006-08-11 08:59:35 +00001214 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
Evan Cheng9ade2182006-08-26 05:34:46 +00001215 ;
Evan Cheng3eff89b2006-05-10 02:47:57 +00001216 } else if (InputHasChain && !NodeHasChain) {
1217 // One of the inner node produces a chain.
Evan Cheng9ade2182006-08-26 05:34:46 +00001218 if (NodeHasOutFlag)
Bill Wendling27926af2008-02-26 10:45:29 +00001219 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) +
1220 "), SDOperand(ResNode, N.ResNo-1));");
1221 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) +
1222 "), " + ChainName + ");");
Evan Cheng4fba2812005-12-20 07:37:41 +00001223 }
Evan Cheng06d64702006-08-11 08:59:35 +00001224
Evan Cheng30729b42007-09-17 22:26:41 +00001225 emitCode("return ResNode;");
Evan Chengb915f312005-12-09 22:45:35 +00001226 } else {
Evan Cheng9ade2182006-08-26 05:34:46 +00001227 std::string Code = "return CurDAG->SelectNodeTo(N.Val, Opc" +
Evan Chengfceb57a2006-07-15 08:45:20 +00001228 utostr(OpcNo);
Nate Begemanb73628b2005-12-30 00:12:56 +00001229 if (N->getTypeNum(0) != MVT::isVoid)
Evan Chengf8729402006-07-16 06:12:52 +00001230 Code += ", VT" + utostr(VTNo);
Evan Cheng54597732006-01-26 00:22:25 +00001231 if (NodeHasOutFlag)
Chris Lattner8a0604b2006-01-28 20:31:24 +00001232 Code += ", MVT::Flag";
Evan Chengf037ca62006-08-27 08:11:28 +00001233
1234 if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1235 AllOps.push_back("InFlag");
1236
1237 unsigned NumOps = AllOps.size();
1238 if (NumOps) {
1239 if (!NodeHasOptInFlag && NumOps < 4) {
1240 for (unsigned i = 0; i != NumOps; ++i)
1241 Code += ", " + AllOps[i];
1242 } else {
1243 std::string OpsCode = "SDOperand Ops" + utostr(OpcNo) + "[] = { ";
1244 for (unsigned i = 0; i != NumOps; ++i) {
1245 OpsCode += AllOps[i];
1246 if (i != NumOps-1)
1247 OpsCode += ", ";
1248 }
1249 emitCode(OpsCode + " };");
1250 Code += ", Ops" + utostr(OpcNo) + ", ";
1251 Code += utostr(NumOps);
1252 }
1253 }
Evan Cheng95514ba2006-08-26 08:00:10 +00001254 emitCode(Code + ");");
Evan Cheng676d7312006-08-26 00:59:04 +00001255 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1256 if (N->getTypeNum(0) != MVT::isVoid)
1257 emitVT(getEnumName(N->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001258 }
Evan Cheng4fba2812005-12-20 07:37:41 +00001259
Evan Cheng676d7312006-08-26 00:59:04 +00001260 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001261 } else if (Op->isSubClassOf("SDNodeXForm")) {
1262 assert(N->getNumChildren() == 1 && "node xform should have one child!");
Evan Cheng863bf5a2006-03-20 22:53:06 +00001263 // PatLeaf node - the operand may or may not be a leaf node. But it should
1264 // behave like one.
Evan Cheng676d7312006-08-26 00:59:04 +00001265 std::vector<std::string> Ops =
Evan Cheng30729b42007-09-17 22:26:41 +00001266 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Evan Cheng676d7312006-08-26 00:59:04 +00001267 ResNodeDecled, true);
Evan Chengb915f312005-12-09 22:45:35 +00001268 unsigned ResNo = TmpNo++;
Evan Cheng676d7312006-08-26 00:59:04 +00001269 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
1270 + "(" + Ops.back() + ".Val);");
1271 NodeOps.push_back("Tmp" + utostr(ResNo));
Evan Cheng9ade2182006-08-26 05:34:46 +00001272 if (isRoot)
1273 emitCode("return Tmp" + utostr(ResNo) + ".Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001274 return NodeOps;
Evan Chengb915f312005-12-09 22:45:35 +00001275 } else {
1276 N->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001277 cerr << "\n";
Chris Lattner7893f132006-01-11 01:33:49 +00001278 throw std::string("Unknown node in result pattern!");
Evan Chengb915f312005-12-09 22:45:35 +00001279 }
1280 }
1281
Chris Lattner488580c2006-01-28 19:06:51 +00001282 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1283 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
Evan Chengb915f312005-12-09 22:45:35 +00001284 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1285 /// for, this returns true otherwise false if Pat has all types.
1286 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
Chris Lattner706d2d32006-08-09 16:44:44 +00001287 const std::string &Prefix, bool isRoot = false) {
Evan Chengb915f312005-12-09 22:45:35 +00001288 // Did we find one?
Evan Chengd15531b2006-05-19 07:24:32 +00001289 if (Pat->getExtTypes() != Other->getExtTypes()) {
Evan Chengb915f312005-12-09 22:45:35 +00001290 // Move a type over from 'other' to 'pat'.
Nate Begemanb73628b2005-12-30 00:12:56 +00001291 Pat->setTypes(Other->getExtTypes());
Chris Lattner706d2d32006-08-09 16:44:44 +00001292 // The top level node type is checked outside of the select function.
1293 if (!isRoot)
1294 emitCheck(Prefix + ".Val->getValueType(0) == " +
1295 getName(Pat->getTypeNum(0)));
Evan Chengb915f312005-12-09 22:45:35 +00001296 return true;
Evan Chengb915f312005-12-09 22:45:35 +00001297 }
1298
Evan Cheng51fecc82006-01-09 18:27:06 +00001299 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001300 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001301 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1302 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1303 Prefix + utostr(OpNo)))
1304 return true;
1305 return false;
1306 }
1307
1308private:
Evan Cheng54597732006-01-26 00:22:25 +00001309 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
Evan Chengb915f312005-12-09 22:45:35 +00001310 /// being built.
Evan Cheng54597732006-01-26 00:22:25 +00001311 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
Evan Cheng676d7312006-08-26 00:59:04 +00001312 bool &ChainEmitted, bool &InFlagDecled,
1313 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner6cefb772008-01-05 22:25:12 +00001314 const CodeGenTarget &T = CGP.getTargetInfo();
Evan Cheng51fecc82006-01-09 18:27:06 +00001315 unsigned OpNo =
Chris Lattner6cefb772008-01-05 22:25:12 +00001316 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1317 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Evan Chengb915f312005-12-09 22:45:35 +00001318 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1319 TreePatternNode *Child = N->getChild(i);
1320 if (!Child->isLeaf()) {
Evan Cheng676d7312006-08-26 00:59:04 +00001321 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1322 InFlagDecled, ResNodeDecled);
Evan Chengb915f312005-12-09 22:45:35 +00001323 } else {
1324 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
Evan Chengb4ad33c2006-01-19 01:55:45 +00001325 if (!Child->getName().empty()) {
1326 std::string Name = RootName + utostr(OpNo);
1327 if (Duplicates.find(Name) != Duplicates.end())
1328 // A duplicate! Do not emit a copy for this node.
1329 continue;
1330 }
1331
Evan Chengb915f312005-12-09 22:45:35 +00001332 Record *RR = DI->getDef();
1333 if (RR->isSubClassOf("Register")) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001334 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Evan Chengbcecf332005-12-17 01:19:28 +00001335 if (RVT == MVT::Flag) {
Evan Cheng676d7312006-08-26 00:59:04 +00001336 if (!InFlagDecled) {
1337 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
1338 InFlagDecled = true;
1339 } else
1340 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1341 emitCode("AddToISelQueue(InFlag);");
Evan Chengb2c6d492006-01-11 22:16:13 +00001342 } else {
1343 if (!ChainEmitted) {
Evan Cheng676d7312006-08-26 00:59:04 +00001344 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
Evan Chenge4a8a6e2006-02-03 06:22:41 +00001345 ChainName = "Chain";
Evan Chengb2c6d492006-01-11 22:16:13 +00001346 ChainEmitted = true;
1347 }
Evan Cheng676d7312006-08-26 00:59:04 +00001348 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1349 if (!InFlagDecled) {
1350 emitCode("SDOperand InFlag(0, 0);");
1351 InFlagDecled = true;
1352 }
1353 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1354 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner6cefb772008-01-05 22:25:12 +00001355 ", " + getQualifiedName(RR) +
Evan Cheng7a33db02006-08-26 07:39:28 +00001356 ", " + RootName + utostr(OpNo) + ", InFlag).Val;");
Evan Cheng676d7312006-08-26 00:59:04 +00001357 ResNodeDecled = true;
Evan Cheng67212a02006-02-09 22:12:27 +00001358 emitCode(ChainName + " = SDOperand(ResNode, 0);");
1359 emitCode("InFlag = SDOperand(ResNode, 1);");
Evan Chengb915f312005-12-09 22:45:35 +00001360 }
1361 }
1362 }
1363 }
1364 }
Evan Cheng54597732006-01-26 00:22:25 +00001365
Evan Cheng676d7312006-08-26 00:59:04 +00001366 if (HasInFlag) {
1367 if (!InFlagDecled) {
1368 emitCode("SDOperand InFlag = " + RootName +
1369 ".getOperand(" + utostr(OpNo) + ");");
1370 InFlagDecled = true;
1371 } else
1372 emitCode("InFlag = " + RootName +
1373 ".getOperand(" + utostr(OpNo) + ");");
1374 emitCode("AddToISelQueue(InFlag);");
1375 }
Evan Chengb915f312005-12-09 22:45:35 +00001376 }
1377};
1378
Chris Lattnerd1ff35a2005-09-23 21:33:23 +00001379/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1380/// stream to match the pattern, and generate the code for the match if it
Chris Lattner355408b2006-01-29 02:43:35 +00001381/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner60d81392008-01-05 22:30:17 +00001382void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +00001383 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +00001384 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +00001385 std::vector<std::string> &TargetOpcodes,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001386 std::vector<std::string> &TargetVTs,
1387 bool &OutputIsVariadic,
1388 unsigned &NumInputRootOps) {
1389 OutputIsVariadic = false;
1390 NumInputRootOps = 0;
1391
Chris Lattner200c57e2008-01-05 22:58:54 +00001392 PatternCodeEmitter Emitter(CGP, Pattern.getPredicates(),
Evan Cheng58e84a62005-12-14 22:02:59 +00001393 Pattern.getSrcPattern(), Pattern.getDstPattern(),
Evan Chengf8729402006-07-16 06:12:52 +00001394 GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001395 TargetOpcodes, TargetVTs,
1396 OutputIsVariadic, NumInputRootOps);
Evan Chengb915f312005-12-09 22:45:35 +00001397
Chris Lattner8fc35682005-09-23 23:16:51 +00001398 // Emit the matcher, capturing named arguments in VariableMap.
Evan Cheng7b05bd52005-12-23 22:11:47 +00001399 bool FoundChain = false;
Evan Cheng13e9e9c2006-10-16 06:33:44 +00001400 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
Evan Chengb915f312005-12-09 22:45:35 +00001401
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001402 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner200c57e2008-01-05 22:58:54 +00001403 TreePattern &TP = *CGP.pf_begin()->second;
Chris Lattner296dfe32005-09-24 00:50:51 +00001404
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001405 // At this point, we know that we structurally match the pattern, but the
1406 // types of the nodes may not match. Figure out the fewest number of type
1407 // comparisons we need to emit. For example, if there is only one integer
1408 // type supported by a target, there should be no type comparisons at all for
1409 // integer patterns!
1410 //
1411 // To figure out the fewest number of type checks needed, clone the pattern,
1412 // remove the types, then perform type inference on the pattern as a whole.
1413 // If there are unresolved types, emit an explicit check for those types,
1414 // apply the type to the tree, then rerun type inference. Iterate until all
1415 // types are resolved.
1416 //
Evan Cheng58e84a62005-12-14 22:02:59 +00001417 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001418 RemoveAllTypes(Pat);
Chris Lattner7e82f132005-10-15 21:34:21 +00001419
1420 do {
1421 // Resolve/propagate as many types as possible.
1422 try {
1423 bool MadeChange = true;
1424 while (MadeChange)
Chris Lattner488580c2006-01-28 19:06:51 +00001425 MadeChange = Pat->ApplyTypeConstraints(TP,
1426 true/*Ignore reg constraints*/);
Chris Lattner7e82f132005-10-15 21:34:21 +00001427 } catch (...) {
1428 assert(0 && "Error: could not find consistent types for something we"
1429 " already decided was ok!");
1430 abort();
1431 }
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001432
Chris Lattner7e82f132005-10-15 21:34:21 +00001433 // Insert a check for an unresolved type and add it to the tree. If we find
1434 // an unresolved type to add a check for, this returns true and we iterate,
1435 // otherwise we are done.
Chris Lattner706d2d32006-08-09 16:44:44 +00001436 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
Evan Cheng1c3d19e2005-12-04 08:18:16 +00001437
Evan Cheng85dbe1a2007-09-12 23:30:14 +00001438 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Cheng30729b42007-09-17 22:26:41 +00001439 false, false, false, true);
Chris Lattner0ee7cff2005-10-14 04:11:13 +00001440 delete Pat;
Chris Lattner3f7e9142005-09-23 20:52:47 +00001441}
1442
Chris Lattner24e00a42006-01-29 04:41:05 +00001443/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1444/// a line causes any of them to be empty, remove them and return true when
1445/// done.
Chris Lattner60d81392008-01-05 22:30:17 +00001446static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001447 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner24e00a42006-01-29 04:41:05 +00001448 &Patterns) {
1449 bool ErasedPatterns = false;
1450 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1451 Patterns[i].second.pop_back();
1452 if (Patterns[i].second.empty()) {
1453 Patterns.erase(Patterns.begin()+i);
1454 --i; --e;
1455 ErasedPatterns = true;
1456 }
1457 }
1458 return ErasedPatterns;
1459}
1460
Chris Lattner8bc74722006-01-29 04:25:26 +00001461/// EmitPatterns - Emit code for at least one pattern, but try to group common
1462/// code together between the patterns.
Chris Lattner60d81392008-01-05 22:30:17 +00001463void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +00001464 std::vector<std::pair<unsigned, std::string> > > >
Chris Lattner8bc74722006-01-29 04:25:26 +00001465 &Patterns, unsigned Indent,
1466 std::ostream &OS) {
Evan Cheng676d7312006-08-26 00:59:04 +00001467 typedef std::pair<unsigned, std::string> CodeLine;
Chris Lattner8bc74722006-01-29 04:25:26 +00001468 typedef std::vector<CodeLine> CodeList;
Chris Lattner60d81392008-01-05 22:30:17 +00001469 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Chris Lattner8bc74722006-01-29 04:25:26 +00001470
1471 if (Patterns.empty()) return;
1472
Chris Lattner24e00a42006-01-29 04:41:05 +00001473 // Figure out how many patterns share the next code line. Explicitly copy
1474 // FirstCodeLine so that we don't invalidate a reference when changing
1475 // Patterns.
1476 const CodeLine FirstCodeLine = Patterns.back().second.back();
Chris Lattner8bc74722006-01-29 04:25:26 +00001477 unsigned LastMatch = Patterns.size()-1;
1478 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1479 --LastMatch;
1480
1481 // If not all patterns share this line, split the list into two pieces. The
1482 // first chunk will use this line, the second chunk won't.
1483 if (LastMatch != 0) {
1484 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1485 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1486
1487 // FIXME: Emit braces?
1488 if (Shared.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001489 const PatternToMatch &Pattern = *Shared.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001490 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1491 Pattern.getSrcPattern()->print(OS);
1492 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1493 Pattern.getDstPattern()->print(OS);
1494 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001495 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001496 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001497 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001498 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001499 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Evan Chenge6f32032006-07-19 00:24:41 +00001500 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001501 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001502 }
Evan Cheng676d7312006-08-26 00:59:04 +00001503 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001504 OS << std::string(Indent, ' ') << "{\n";
1505 Indent += 2;
1506 }
1507 EmitPatterns(Shared, Indent, OS);
Evan Cheng676d7312006-08-26 00:59:04 +00001508 if (FirstCodeLine.first != 1) {
Chris Lattner8bc74722006-01-29 04:25:26 +00001509 Indent -= 2;
1510 OS << std::string(Indent, ' ') << "}\n";
1511 }
1512
1513 if (Other.size() == 1) {
Chris Lattner60d81392008-01-05 22:30:17 +00001514 const PatternToMatch &Pattern = *Other.back().first;
Chris Lattner8bc74722006-01-29 04:25:26 +00001515 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1516 Pattern.getSrcPattern()->print(OS);
1517 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1518 Pattern.getDstPattern()->print(OS);
1519 OS << "\n";
Evan Chengc81d2a02006-04-19 20:36:09 +00001520 unsigned AddedComplexity = Pattern.getAddedComplexity();
Chris Lattner8bc74722006-01-29 04:25:26 +00001521 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001522 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Evan Cheng59413202006-04-19 18:07:24 +00001523 << " cost = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001524 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Chris Lattner706d2d32006-08-09 16:44:44 +00001525 << " size = "
Chris Lattner200c57e2008-01-05 22:58:54 +00001526 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Chris Lattner8bc74722006-01-29 04:25:26 +00001527 }
1528 EmitPatterns(Other, Indent, OS);
1529 return;
1530 }
1531
Chris Lattner24e00a42006-01-29 04:41:05 +00001532 // Remove this code from all of the patterns that share it.
1533 bool ErasedPatterns = EraseCodeLine(Patterns);
1534
Evan Cheng676d7312006-08-26 00:59:04 +00001535 bool isPredicate = FirstCodeLine.first == 1;
Chris Lattner8bc74722006-01-29 04:25:26 +00001536
1537 // Otherwise, every pattern in the list has this line. Emit it.
1538 if (!isPredicate) {
1539 // Normal code.
1540 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1541 } else {
Chris Lattner24e00a42006-01-29 04:41:05 +00001542 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1543
1544 // If the next code line is another predicate, and if all of the pattern
1545 // in this group share the same next line, emit it inline now. Do this
1546 // until we run out of common predicates.
Evan Cheng676d7312006-08-26 00:59:04 +00001547 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
Chris Lattner24e00a42006-01-29 04:41:05 +00001548 // Check that all of fhe patterns in Patterns end with the same predicate.
1549 bool AllEndWithSamePredicate = true;
1550 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1551 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1552 AllEndWithSamePredicate = false;
1553 break;
1554 }
1555 // If all of the predicates aren't the same, we can't share them.
1556 if (!AllEndWithSamePredicate) break;
1557
1558 // Otherwise we can. Emit it shared now.
1559 OS << " &&\n" << std::string(Indent+4, ' ')
1560 << Patterns.back().second.back().second;
1561 ErasedPatterns = EraseCodeLine(Patterns);
Chris Lattner8bc74722006-01-29 04:25:26 +00001562 }
Chris Lattner24e00a42006-01-29 04:41:05 +00001563
1564 OS << ") {\n";
1565 Indent += 2;
Chris Lattner8bc74722006-01-29 04:25:26 +00001566 }
1567
1568 EmitPatterns(Patterns, Indent, OS);
1569
1570 if (isPredicate)
1571 OS << std::string(Indent-2, ' ') << "}\n";
1572}
1573
Evan Cheng892aaf82006-11-08 23:01:03 +00001574static std::string getLegalCName(std::string OpName) {
1575 std::string::size_type pos = OpName.find("::");
1576 if (pos != std::string::npos)
1577 OpName.replace(pos, 2, "_");
1578 return OpName;
Chris Lattner37481472005-09-26 21:59:35 +00001579}
1580
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001581void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001582 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner6cefb772008-01-05 22:25:12 +00001583
Chris Lattnerf7560ed2006-11-20 18:54:33 +00001584 // Get the namespace to insert instructions into. Make sure not to pick up
1585 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
1586 // instruction or something.
1587 std::string InstNS;
1588 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
1589 e = Target.inst_end(); i != e; ++i) {
1590 InstNS = i->second.Namespace;
1591 if (InstNS != "TargetInstrInfo")
1592 break;
1593 }
1594
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001595 if (!InstNS.empty()) InstNS += "::";
1596
Chris Lattner602f6922006-01-04 00:25:00 +00001597 // Group the patterns by their top-level opcodes.
Chris Lattner60d81392008-01-05 22:30:17 +00001598 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Evan Chengfceb57a2006-07-15 08:45:20 +00001599 // All unique target node emission functions.
1600 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerfe718932008-01-06 01:10:31 +00001601 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001602 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner60d81392008-01-05 22:30:17 +00001603 const PatternToMatch &Pattern = *I;
Chris Lattner6cefb772008-01-05 22:25:12 +00001604
1605 TreePatternNode *Node = Pattern.getSrcPattern();
Chris Lattner602f6922006-01-04 00:25:00 +00001606 if (!Node->isLeaf()) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001607 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001608 push_back(&Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001609 } else {
1610 const ComplexPattern *CP;
Chris Lattner9c5d4de2006-11-03 01:11:05 +00001611 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001612 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner6cefb772008-01-05 22:25:12 +00001613 push_back(&Pattern);
Chris Lattner200c57e2008-01-05 22:58:54 +00001614 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Chris Lattner602f6922006-01-04 00:25:00 +00001615 std::vector<Record*> OpNodes = CP->getRootNodes();
1616 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner200c57e2008-01-05 22:58:54 +00001617 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1618 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner6cefb772008-01-05 22:25:12 +00001619 &Pattern);
Chris Lattner602f6922006-01-04 00:25:00 +00001620 }
1621 } else {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001622 cerr << "Unrecognized opcode '";
Chris Lattner602f6922006-01-04 00:25:00 +00001623 Node->dump();
Bill Wendlingf5da1332006-12-07 22:21:48 +00001624 cerr << "' on tree pattern '";
Chris Lattner6cefb772008-01-05 22:25:12 +00001625 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Chris Lattner602f6922006-01-04 00:25:00 +00001626 exit(1);
1627 }
1628 }
1629 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001630
1631 // For each opcode, there might be multiple select functions, one per
1632 // ValueType of the node (or its first operand if it doesn't produce a
1633 // non-chain result.
1634 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1635
Chris Lattner602f6922006-01-04 00:25:00 +00001636 // Emit one Select_* method for each top-level opcode. We do this instead of
1637 // emitting one giant switch statement to support compilers where this will
1638 // result in the recursive functions taking less stack space.
Chris Lattner60d81392008-01-05 22:30:17 +00001639 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001640 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1641 PBOI != E; ++PBOI) {
1642 const std::string &OpName = PBOI->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001643 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Chris Lattner706d2d32006-08-09 16:44:44 +00001644 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1645
Chris Lattner602f6922006-01-04 00:25:00 +00001646 // We want to emit all of the matching code now. However, we want to emit
1647 // the matches in order of minimal cost. Sort the patterns so the least
1648 // cost one is at the start.
Chris Lattner706d2d32006-08-09 16:44:44 +00001649 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner200c57e2008-01-05 22:58:54 +00001650 PatternSortingPredicate(CGP));
Evan Cheng21ad3922006-02-07 00:37:41 +00001651
Chris Lattner706d2d32006-08-09 16:44:44 +00001652 // Split them into groups by type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001653 std::map<MVT::SimpleValueType,
1654 std::vector<const PatternToMatch*> > PatternsByType;
Chris Lattner706d2d32006-08-09 16:44:44 +00001655 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner60d81392008-01-05 22:30:17 +00001656 const PatternToMatch *Pat = PatternsOfOp[i];
Chris Lattner706d2d32006-08-09 16:44:44 +00001657 TreePatternNode *SrcPat = Pat->getSrcPattern();
Duncan Sands83ec4b62008-06-06 12:08:01 +00001658 MVT::SimpleValueType VT = SrcPat->getTypeNum(0);
1659 std::map<MVT::SimpleValueType,
Chris Lattner60d81392008-01-05 22:30:17 +00001660 std::vector<const PatternToMatch*> >::iterator TI =
Chris Lattner706d2d32006-08-09 16:44:44 +00001661 PatternsByType.find(VT);
1662 if (TI != PatternsByType.end())
1663 TI->second.push_back(Pat);
1664 else {
Chris Lattner60d81392008-01-05 22:30:17 +00001665 std::vector<const PatternToMatch*> PVec;
Chris Lattner706d2d32006-08-09 16:44:44 +00001666 PVec.push_back(Pat);
1667 PatternsByType.insert(std::make_pair(VT, PVec));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001668 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001669 }
1670
Duncan Sands83ec4b62008-06-06 12:08:01 +00001671 for (std::map<MVT::SimpleValueType,
1672 std::vector<const PatternToMatch*> >::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001673 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1674 ++II) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001675 MVT::SimpleValueType OpVT = II->first;
Chris Lattner60d81392008-01-05 22:30:17 +00001676 std::vector<const PatternToMatch*> &Patterns = II->second;
Chris Lattner64906972006-09-21 18:28:27 +00001677 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1678 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
Chris Lattner706d2d32006-08-09 16:44:44 +00001679
Chris Lattner60d81392008-01-05 22:30:17 +00001680 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Chris Lattner706d2d32006-08-09 16:44:44 +00001681 std::vector<std::vector<std::string> > PatternOpcodes;
1682 std::vector<std::vector<std::string> > PatternVTs;
Evan Chengf5493192006-08-26 01:02:19 +00001683 std::vector<std::set<std::string> > PatternDecls;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001684 std::vector<bool> OutputIsVariadicFlags;
1685 std::vector<unsigned> NumInputRootOpsCounts;
Chris Lattner706d2d32006-08-09 16:44:44 +00001686 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1687 CodeList GeneratedCode;
Evan Chengf5493192006-08-26 01:02:19 +00001688 std::set<std::string> GeneratedDecl;
Chris Lattner706d2d32006-08-09 16:44:44 +00001689 std::vector<std::string> TargetOpcodes;
1690 std::vector<std::string> TargetVTs;
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001691 bool OutputIsVariadic;
1692 unsigned NumInputRootOps;
Chris Lattner706d2d32006-08-09 16:44:44 +00001693 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001694 TargetOpcodes, TargetVTs,
1695 OutputIsVariadic, NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001696 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1697 PatternDecls.push_back(GeneratedDecl);
1698 PatternOpcodes.push_back(TargetOpcodes);
1699 PatternVTs.push_back(TargetVTs);
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001700 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1701 NumInputRootOpsCounts.push_back(NumInputRootOps);
Chris Lattner706d2d32006-08-09 16:44:44 +00001702 }
1703
1704 // Scan the code to see if all of the patterns are reachable and if it is
1705 // possible that the last one might not match.
1706 bool mightNotMatch = true;
1707 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1708 CodeList &GeneratedCode = CodeForPatterns[i].second;
1709 mightNotMatch = false;
1710
1711 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001712 if (GeneratedCode[j].first == 1) { // predicate.
Chris Lattner706d2d32006-08-09 16:44:44 +00001713 mightNotMatch = true;
1714 break;
1715 }
1716 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001717
Chris Lattner706d2d32006-08-09 16:44:44 +00001718 // If this pattern definitely matches, and if it isn't the last one, the
1719 // patterns after it CANNOT ever match. Error out.
1720 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001721 cerr << "Pattern '";
1722 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1723 cerr << "' is impossible to select!\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001724 exit(1);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001725 }
1726 }
1727
Chris Lattner706d2d32006-08-09 16:44:44 +00001728 // Factor target node emission code (emitted by EmitResultCode) into
1729 // separate functions. Uniquing and share them among all instruction
1730 // selection routines.
1731 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1732 CodeList &GeneratedCode = CodeForPatterns[i].second;
1733 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1734 std::vector<std::string> &TargetVTs = PatternVTs[i];
Evan Chengf5493192006-08-26 01:02:19 +00001735 std::set<std::string> Decls = PatternDecls[i];
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001736 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1737 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Evan Cheng676d7312006-08-26 00:59:04 +00001738 std::vector<std::string> AddedInits;
Chris Lattner706d2d32006-08-09 16:44:44 +00001739 int CodeSize = (int)GeneratedCode.size();
1740 int LastPred = -1;
1741 for (int j = CodeSize-1; j >= 0; --j) {
Evan Cheng676d7312006-08-26 00:59:04 +00001742 if (LastPred == -1 && GeneratedCode[j].first == 1)
Chris Lattner706d2d32006-08-09 16:44:44 +00001743 LastPred = j;
Evan Cheng676d7312006-08-26 00:59:04 +00001744 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1745 AddedInits.push_back(GeneratedCode[j].second);
Chris Lattner706d2d32006-08-09 16:44:44 +00001746 }
1747
Evan Cheng9ade2182006-08-26 05:34:46 +00001748 std::string CalleeCode = "(const SDOperand &N";
1749 std::string CallerCode = "(N";
Chris Lattner706d2d32006-08-09 16:44:44 +00001750 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1751 CalleeCode += ", unsigned Opc" + utostr(j);
1752 CallerCode += ", " + TargetOpcodes[j];
1753 }
1754 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001755 CalleeCode += ", MVT VT" + utostr(j);
Chris Lattner706d2d32006-08-09 16:44:44 +00001756 CallerCode += ", " + TargetVTs[j];
1757 }
Evan Chengf5493192006-08-26 01:02:19 +00001758 for (std::set<std::string>::iterator
Chris Lattner706d2d32006-08-09 16:44:44 +00001759 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
Evan Chengf5493192006-08-26 01:02:19 +00001760 std::string Name = *I;
Evan Cheng676d7312006-08-26 00:59:04 +00001761 CalleeCode += ", SDOperand &" + Name;
1762 CallerCode += ", " + Name;
Chris Lattner706d2d32006-08-09 16:44:44 +00001763 }
Dan Gohmane4c67cd2008-05-31 02:11:25 +00001764
1765 if (OutputIsVariadic) {
1766 CalleeCode += ", unsigned NumInputRootOps";
1767 CallerCode += ", " + utostr(NumInputRootOps);
1768 }
1769
Chris Lattner706d2d32006-08-09 16:44:44 +00001770 CallerCode += ");";
1771 CalleeCode += ") ";
1772 // Prevent emission routines from being inlined to reduce selection
1773 // routines stack frame sizes.
Chris Lattner8dc728e2006-08-27 13:16:24 +00001774 CalleeCode += "DISABLE_INLINE ";
Evan Cheng676d7312006-08-26 00:59:04 +00001775 CalleeCode += "{\n";
1776
1777 for (std::vector<std::string>::const_reverse_iterator
1778 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1779 CalleeCode += " " + *I + "\n";
1780
Evan Chengf5493192006-08-26 01:02:19 +00001781 for (int j = LastPred+1; j < CodeSize; ++j)
1782 CalleeCode += " " + GeneratedCode[j].second + "\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001783 for (int j = LastPred+1; j < CodeSize; ++j)
1784 GeneratedCode.pop_back();
1785 CalleeCode += "}\n";
1786
1787 // Uniquing the emission routines.
1788 unsigned EmitFuncNum;
1789 std::map<std::string, unsigned>::iterator EFI =
1790 EmitFunctions.find(CalleeCode);
1791 if (EFI != EmitFunctions.end()) {
1792 EmitFuncNum = EFI->second;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001793 } else {
Chris Lattner706d2d32006-08-09 16:44:44 +00001794 EmitFuncNum = EmitFunctions.size();
1795 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
Evan Cheng06d64702006-08-11 08:59:35 +00001796 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001797 }
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001798
Chris Lattner706d2d32006-08-09 16:44:44 +00001799 // Replace the emission code within selection routines with calls to the
1800 // emission functions.
Evan Cheng06d64702006-08-11 08:59:35 +00001801 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
Chris Lattner706d2d32006-08-09 16:44:44 +00001802 GeneratedCode.push_back(std::make_pair(false, CallerCode));
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001803 }
1804
Chris Lattner706d2d32006-08-09 16:44:44 +00001805 // Print function.
Chris Lattnerab51ddd2006-11-14 21:32:01 +00001806 std::string OpVTStr;
Chris Lattner33a40042006-11-14 22:17:10 +00001807 if (OpVT == MVT::iPTR) {
1808 OpVTStr = "_iPTR";
1809 } else if (OpVT == MVT::isVoid) {
1810 // Nodes with a void result actually have a first result type of either
1811 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1812 // void to this case, we handle it specially here.
1813 } else {
1814 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1815 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001816 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1817 OpcodeVTMap.find(OpName);
1818 if (OpVTI == OpcodeVTMap.end()) {
1819 std::vector<std::string> VTSet;
1820 VTSet.push_back(OpVTStr);
1821 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1822 } else
1823 OpVTI->second.push_back(OpVTStr);
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001824
Evan Cheng892aaf82006-11-08 23:01:03 +00001825 OS << "SDNode *Select_" << getLegalCName(OpName)
Chris Lattner33a40042006-11-14 22:17:10 +00001826 << OpVTStr << "(const SDOperand &N) {\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001827
Chris Lattner706d2d32006-08-09 16:44:44 +00001828 // Loop through and reverse all of the CodeList vectors, as we will be
1829 // accessing them from their logical front, but accessing the end of a
1830 // vector is more efficient.
1831 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1832 CodeList &GeneratedCode = CodeForPatterns[i].second;
1833 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001834 }
Chris Lattner706d2d32006-08-09 16:44:44 +00001835
1836 // Next, reverse the list of patterns itself for the same reason.
1837 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1838
1839 // Emit all of the patterns now, grouped together to share code.
1840 EmitPatterns(CodeForPatterns, 2, OS);
1841
Chris Lattner64906972006-09-21 18:28:27 +00001842 // If the last pattern has predicates (which could fail) emit code to
1843 // catch the case where nothing handles a pattern.
Chris Lattner706d2d32006-08-09 16:44:44 +00001844 if (mightNotMatch) {
Bill Wendlingf5da1332006-12-07 22:21:48 +00001845 OS << " cerr << \"Cannot yet select: \";\n";
Evan Cheng892aaf82006-11-08 23:01:03 +00001846 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1847 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1848 OpName != "ISD::INTRINSIC_VOID") {
Chris Lattner706d2d32006-08-09 16:44:44 +00001849 OS << " N.Val->dump(CurDAG);\n";
1850 } else {
1851 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1852 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00001853 << " cerr << \"intrinsic %\"<< "
Chris Lattner706d2d32006-08-09 16:44:44 +00001854 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1855 }
Bill Wendlingf5da1332006-12-07 22:21:48 +00001856 OS << " cerr << '\\n';\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001857 << " abort();\n"
1858 << " return NULL;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00001859 }
1860 OS << "}\n\n";
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001861 }
Chris Lattner602f6922006-01-04 00:25:00 +00001862 }
1863
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001864 // Emit boilerplate.
Evan Cheng9ade2182006-08-26 05:34:46 +00001865 OS << "SDNode *Select_INLINEASM(SDOperand N) {\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001866 << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001867 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n"
1868
1869 << " // Ensure that the asm operands are themselves selected.\n"
1870 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1871 << " AddToISelQueue(Ops[j]);\n\n"
1872
Duncan Sands83ec4b62008-06-06 12:08:01 +00001873 << " std::vector<MVT> VTs;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001874 << " VTs.push_back(MVT::Other);\n"
1875 << " VTs.push_back(MVT::Flag);\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001876 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
1877 "Ops.size());\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001878 << " return New.Val;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001879 << "}\n\n";
Evan Chengda47e6e2008-03-15 00:03:38 +00001880
1881 OS << "SDNode *Select_UNDEF(const SDOperand &N) {\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001882 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::IMPLICIT_DEF,\n"
1883 << " N.getValueType());\n"
1884 << "}\n\n";
1885
1886 OS << "SDNode *Select_DBG_LABEL(const SDOperand &N) {\n"
1887 << " SDOperand Chain = N.getOperand(0);\n"
1888 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
1889 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1890 << " AddToISelQueue(Chain);\n"
1891 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::DBG_LABEL,\n"
1892 << " MVT::Other, Tmp, Chain);\n"
1893 << "}\n\n";
1894
1895 OS << "SDNode *Select_EH_LABEL(const SDOperand &N) {\n"
1896 << " SDOperand Chain = N.getOperand(0);\n"
1897 << " unsigned C = cast<LabelSDNode>(N)->getLabelID();\n"
1898 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1899 << " AddToISelQueue(Chain);\n"
1900 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::EH_LABEL,\n"
1901 << " MVT::Other, Tmp, Chain);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001902 << "}\n\n";
1903
Evan Chenga844bde2008-02-02 04:07:54 +00001904 OS << "SDNode *Select_DECLARE(const SDOperand &N) {\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001905 << " SDOperand Chain = N.getOperand(0);\n"
1906 << " SDOperand N1 = N.getOperand(1);\n"
1907 << " SDOperand N2 = N.getOperand(2);\n"
1908 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
1909 << " cerr << \"Cannot yet select llvm.dbg.declare: \";\n"
1910 << " N.Val->dump(CurDAG);\n"
1911 << " abort();\n"
1912 << " }\n"
1913 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1914 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001915 << " SDOperand Tmp1 = "
1916 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
1917 << " SDOperand Tmp2 = "
1918 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1919 << " AddToISelQueue(Chain);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001920 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::DECLARE,\n"
1921 << " MVT::Other, Tmp1, Tmp2, Chain);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001922 << "}\n\n";
1923
Christopher Lamb08d52072007-07-26 07:48:21 +00001924 OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n"
1925 << " SDOperand N0 = N.getOperand(0);\n"
1926 << " SDOperand N1 = N.getOperand(1);\n"
1927 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1928 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1929 << " AddToISelQueue(N0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001930 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::EXTRACT_SUBREG,\n"
1931 << " N.getValueType(), N0, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001932 << "}\n\n";
1933
1934 OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n"
1935 << " SDOperand N0 = N.getOperand(0);\n"
1936 << " SDOperand N1 = N.getOperand(1);\n"
1937 << " SDOperand N2 = N.getOperand(2);\n"
1938 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
1939 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1940 << " AddToISelQueue(N1);\n"
Christopher Lamb6634e262008-03-13 05:47:01 +00001941 << " AddToISelQueue(N0);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001942 << " return CurDAG->SelectNodeTo(N.Val, TargetInstrInfo::INSERT_SUBREG,\n"
1943 << " N.getValueType(), N0, N1, Tmp);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001944 << "}\n\n";
1945
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001946 OS << "// The main instruction selector code.\n"
Evan Cheng9ade2182006-08-26 05:34:46 +00001947 << "SDNode *SelectCode(SDOperand N) {\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001948 << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n"
Chris Lattnerb277cbc2005-10-18 04:41:01 +00001949 << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS
Evan Cheng34167212006-02-09 00:37:58 +00001950 << "INSTRUCTION_LIST_END)) {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001951 << " return NULL; // Already selected.\n"
Evan Cheng34167212006-02-09 00:37:58 +00001952 << " }\n\n"
Duncan Sands83ec4b62008-06-06 12:08:01 +00001953 << " MVT::SimpleValueType NVT = N.Val->getValueType(0).getSimpleVT();\n"
Chris Lattner547394c2005-09-23 21:53:45 +00001954 << " switch (N.getOpcode()) {\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001955 << " default: break;\n"
1956 << " case ISD::EntryToken: // These leaves remain the same.\n"
Chris Lattner5216c692005-12-18 21:05:44 +00001957 << " case ISD::BasicBlock:\n"
Chris Lattner8020a522006-01-11 19:52:27 +00001958 << " case ISD::Register:\n"
Evan Cheng0a83ed52006-02-05 08:46:14 +00001959 << " case ISD::HANDLENODE:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001960 << " case ISD::TargetConstant:\n"
Nate Begemane1795842008-02-14 08:57:00 +00001961 << " case ISD::TargetConstantFP:\n"
Evan Cheng2216d8a2006-02-05 05:22:18 +00001962 << " case ISD::TargetConstantPool:\n"
1963 << " case ISD::TargetFrameIndex:\n"
Chris Lattner4ef9b112007-05-15 01:36:44 +00001964 << " case ISD::TargetExternalSymbol:\n"
Nate Begeman37efe672006-04-22 18:53:45 +00001965 << " case ISD::TargetJumpTable:\n"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001966 << " case ISD::TargetGlobalTLSAddress:\n"
Evan Cheng34167212006-02-09 00:37:58 +00001967 << " case ISD::TargetGlobalAddress: {\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001968 << " return NULL;\n"
Evan Cheng34167212006-02-09 00:37:58 +00001969 << " }\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001970 << " case ISD::AssertSext:\n"
Chris Lattnerfab37282005-09-26 22:10:24 +00001971 << " case ISD::AssertZext: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001972 << " AddToISelQueue(N.getOperand(0));\n"
1973 << " ReplaceUses(N, N.getOperand(0));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001974 << " return NULL;\n"
Chris Lattnerf071bb52005-10-25 20:35:14 +00001975 << " }\n"
1976 << " case ISD::TokenFactor:\n"
Chris Lattner706d2d32006-08-09 16:44:44 +00001977 << " case ISD::CopyFromReg:\n"
Tanya Lattner8d4ccf02006-08-09 16:41:21 +00001978 << " case ISD::CopyToReg: {\n"
Evan Cheng676d7312006-08-26 00:59:04 +00001979 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1980 << " AddToISelQueue(N.getOperand(i));\n"
Evan Cheng06d64702006-08-11 08:59:35 +00001981 << " return NULL;\n"
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001982 << " }\n"
Jim Laskeya683f9b2007-01-26 17:29:20 +00001983 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Dan Gohmancd920d92008-07-02 23:23:19 +00001984 << " case ISD::DBG_LABEL: return Select_DBG_LABEL(N);\n"
1985 << " case ISD::EH_LABEL: return Select_EH_LABEL(N);\n"
Evan Chenga844bde2008-02-02 04:07:54 +00001986 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb08d52072007-07-26 07:48:21 +00001987 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Chengda47e6e2008-03-15 00:03:38 +00001988 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1989 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Chris Lattnerfabcb7a2006-01-26 23:08:55 +00001990
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001991
Chris Lattner602f6922006-01-04 00:25:00 +00001992 // Loop over all of the case statements, emiting a call to each method we
1993 // emitted above.
Chris Lattner60d81392008-01-05 22:30:17 +00001994 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Evan Cheng892aaf82006-11-08 23:01:03 +00001995 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1996 PBOI != E; ++PBOI) {
1997 const std::string &OpName = PBOI->first;
Chris Lattner706d2d32006-08-09 16:44:44 +00001998 // Potentially multiple versions of select for this opcode. One for each
1999 // ValueType of the node (or its first true operand if it doesn't produce a
2000 // result.
2001 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
2002 OpcodeVTMap.find(OpName);
2003 std::vector<std::string> &OpVTs = OpVTI->second;
Evan Cheng892aaf82006-11-08 23:01:03 +00002004 OS << " case " << OpName << ": {\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002005 // Keep track of whether we see a pattern that has an iPtr result.
2006 bool HasPtrPattern = false;
2007 bool HasDefaultPattern = false;
Chris Lattner717a6112006-11-14 21:50:27 +00002008
Evan Cheng425e8c72007-09-04 20:18:28 +00002009 OS << " switch (NVT) {\n";
2010 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
2011 std::string &VTStr = OpVTs[i];
2012 if (VTStr.empty()) {
2013 HasDefaultPattern = true;
2014 continue;
2015 }
Chris Lattner717a6112006-11-14 21:50:27 +00002016
Evan Cheng425e8c72007-09-04 20:18:28 +00002017 // If this is a match on iPTR: don't emit it directly, we need special
2018 // code.
2019 if (VTStr == "_iPTR") {
2020 HasPtrPattern = true;
2021 continue;
Chris Lattner706d2d32006-08-09 16:44:44 +00002022 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002023 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2024 << " return Select_" << getLegalCName(OpName)
2025 << VTStr << "(N);\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002026 }
Evan Cheng425e8c72007-09-04 20:18:28 +00002027 OS << " default:\n";
2028
2029 // If there is an iPTR result version of this pattern, emit it here.
2030 if (HasPtrPattern) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002031 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Cheng425e8c72007-09-04 20:18:28 +00002032 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2033 }
2034 if (HasDefaultPattern) {
2035 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2036 }
2037 OS << " break;\n";
2038 OS << " }\n";
2039 OS << " break;\n";
Chris Lattner706d2d32006-08-09 16:44:44 +00002040 OS << " }\n";
Chris Lattner81303322005-09-23 19:36:15 +00002041 }
Chris Lattner81303322005-09-23 19:36:15 +00002042
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002043 OS << " } // end of big switch.\n\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002044 << " cerr << \"Cannot yet select: \";\n"
Chris Lattnerb026e702006-03-28 00:41:33 +00002045 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2046 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2047 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002048 << " N.Val->dump(CurDAG);\n"
2049 << " } else {\n"
2050 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2051 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002052 << " cerr << \"intrinsic %\"<< "
2053 "Intrinsic::getName((Intrinsic::ID)iid);\n"
Chris Lattner9bf2d3e2006-03-25 06:47:53 +00002054 << " }\n"
Bill Wendlingf5da1332006-12-07 22:21:48 +00002055 << " cerr << '\\n';\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002056 << " abort();\n"
Evan Cheng06d64702006-08-11 08:59:35 +00002057 << " return NULL;\n"
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002058 << "}\n";
2059}
2060
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002061void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +00002062 EmitSourceFileHeader("DAG Instruction Selector for the " +
2063 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002064
Chris Lattner1f39e292005-09-14 00:09:24 +00002065 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2066 << "// *** instruction selector class. These functions are really "
2067 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +00002068
Roman Levenstein6422e8a2008-05-14 10:17:11 +00002069 OS << "// Include standard, target-independent definitions and methods used\n"
2070 << "// by the instruction selector.\n";
2071 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +00002072
Chris Lattner443e3f92008-01-05 22:54:53 +00002073 EmitNodeTransforms(OS);
Chris Lattnerdc32f982008-01-05 22:43:57 +00002074 EmitPredicateFunctions(OS);
2075
Bill Wendlingf5da1332006-12-07 22:21:48 +00002076 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerfe718932008-01-06 01:10:31 +00002077 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner6cefb772008-01-05 22:25:12 +00002078 I != E; ++I) {
2079 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2080 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Bill Wendlingf5da1332006-12-07 22:21:48 +00002081 DOUT << "\n";
2082 }
Chris Lattnere46e17b2005-09-29 19:28:10 +00002083
Chris Lattnerb9f01eb2005-09-16 00:29:46 +00002084 // At this point, we have full information about the 'Patterns' we need to
2085 // parse, both implicitly from instructions as well as from explicit pattern
Chris Lattnere97603f2005-09-28 19:27:25 +00002086 // definitions. Emit the resultant instruction selector.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002087 EmitInstructionSelector(OS);
2088
Chris Lattner54cb8fd2005-09-07 23:44:43 +00002089}