blob: 5075d483b2cf4f335f6b25cb5068b6dbf4a12990 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfd6c2f02007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
18#include "llvm/Support/MathExtras.h"
19#include "llvm/Support/Streams.h"
20#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Chris Lattner7fdd9342008-01-05 22:43:57 +000024// DAGISelEmitter Helper methods
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025//
26
Chris Lattner4ca8ff02008-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) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030 return (N->isLeaf() &&
31 dynamic_cast<DefInit*>(N->getLeafValue()) &&
32 static_cast<DefInit*>(N->getLeafValue())->getDef()->
33 isSubClassOf("ComplexPattern"));
34}
35
Chris Lattner4ca8ff02008-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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N,
Chris Lattnerae506702008-01-06 01:10:31 +000039 CodeGenDAGPatterns &CGP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 if (N->isLeaf() &&
41 dynamic_cast<DefInit*>(N->getLeafValue()) &&
42 static_cast<DefInit*>(N->getLeafValue())->getDef()->
43 isSubClassOf("ComplexPattern")) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +000044 return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue())
45 ->getDef());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 }
47 return NULL;
48}
49
50/// 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 Lattnerae506702008-01-06 01:10:31 +000053static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Duncan Sands92c43912008-06-06 12:08:01 +000054 assert((EMVT::isExtIntegerInVTs(P->getExtTypes()) ||
55 EMVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 P->getExtTypeNum(0) == MVT::isVoid ||
57 P->getExtTypeNum(0) == MVT::Flag ||
58 P->getExtTypeNum(0) == MVT::iPTR) &&
59 "Not a valid pattern node to size!");
60 unsigned Size = 3; // The node itself.
61 // 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()))
64 Size += 2;
65
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 Lattner4ca8ff02008-01-05 22:25:12 +000071 const ComplexPattern *AM = NodeGetComplexPattern(P, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 if (AM)
73 Size += AM->getNumOperands() * 3;
74
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
80 // 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);
83 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner4ca8ff02008-01-05 22:25:12 +000084 Size += getPatternSize(Child, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 else if (Child->isLeaf()) {
86 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
87 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
88 else if (NodeIsComplexPattern(Child))
Chris Lattner4ca8ff02008-01-05 22:25:12 +000089 Size += getPatternSize(Child, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 else if (!Child->getPredicateFn().empty())
91 ++Size;
92 }
93 }
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 Lattner4ca8ff02008-01-05 22:25:12 +0000101static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerae506702008-01-06 01:10:31 +0000102 CodeGenDAGPatterns &CGP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103 if (P->isLeaf()) return 0;
104
105 unsigned Cost = 0;
106 Record *Op = P->getOperator();
107 if (Op->isSubClassOf("Instruction")) {
108 Cost++;
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000109 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 if (II.usesCustomDAGSchedInserter)
111 Cost += 10;
112 }
113 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000114 Cost += getResultPatternCost(P->getChild(i), CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 return Cost;
116}
117
118/// getResultPatternCodeSize - Compute the code size of instructions for this
119/// pattern.
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000120static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerae506702008-01-06 01:10:31 +0000121 CodeGenDAGPatterns &CGP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Lattner4ca8ff02008-01-05 22:25:12 +0000130 Cost += getResultPatternSize(P->getChild(i), CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 return Cost;
132}
133
134// 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 Lattnerae506702008-01-06 01:10:31 +0000138 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
139 CodeGenDAGPatterns &CGP;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
Chris Lattner81915752008-01-05 22:30:17 +0000141 bool operator()(const PatternToMatch *LHS,
142 const PatternToMatch *RHS) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000143 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
144 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 LHSSize += LHS->getAddedComplexity();
146 RHSSize += RHS->getAddedComplexity();
147 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 Lattner4ca8ff02008-01-05 22:25:12 +0000151 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
152 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 if (LHSCost < RHSCost) return true;
154 if (LHSCost > RHSCost) return false;
155
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000156 return getResultPatternSize(LHS->getDstPattern(), CGP) <
157 getResultPatternSize(RHS->getDstPattern(), CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
159};
160
161/// getRegisterValueType - Look up and return the first ValueType of specified
162/// RegisterClass record
Duncan Sands92c43912008-06-06 12:08:01 +0000163static MVT::SimpleValueType getRegisterValueType(Record *R, const CodeGenTarget &T) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R))
165 return RC->getValueTypeNum(0);
166 return MVT::Other;
167}
168
169
170/// RemoveAllTypes - A quick recursive walk over a pattern which removes all
171/// type information from it.
172static void RemoveAllTypes(TreePatternNode *N) {
173 N->removeTypes();
174 if (!N->isLeaf())
175 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
176 RemoveAllTypes(N->getChild(i));
177}
178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179/// NodeHasProperty - return true if TreePatternNode has the specified
180/// property.
181static bool NodeHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerae506702008-01-06 01:10:31 +0000182 CodeGenDAGPatterns &CGP) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 if (N->isLeaf()) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000184 const ComplexPattern *CP = NodeGetComplexPattern(N, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if (CP)
186 return CP->hasProperty(Property);
187 return false;
188 }
189 Record *Operator = N->getOperator();
190 if (!Operator->isSubClassOf("SDNode")) return false;
191
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000192 return CGP.getSDNodeInfo(Operator).hasProperty(Property);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193}
194
195static bool PatternHasProperty(TreePatternNode *N, SDNP Property,
Chris Lattnerae506702008-01-06 01:10:31 +0000196 CodeGenDAGPatterns &CGP) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000197 if (NodeHasProperty(N, Property, CGP))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 return true;
199
200 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) {
201 TreePatternNode *Child = N->getChild(i);
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000202 if (PatternHasProperty(Child, Property, CGP))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 return true;
204 }
205
206 return false;
207}
208
Chris Lattner7fdd9342008-01-05 22:43:57 +0000209//===----------------------------------------------------------------------===//
Chris Lattner227da452008-01-05 22:54:53 +0000210// Node Transformation emitter implementation.
211//
212void DAGISelEmitter::EmitNodeTransforms(std::ostream &OS) {
213 // Walk the pattern fragments, adding them to a map, which sorts them by
214 // name.
Chris Lattnerae506702008-01-06 01:10:31 +0000215 typedef std::map<std::string, CodeGenDAGPatterns::NodeXForm> NXsByNameTy;
Chris Lattner227da452008-01-05 22:54:53 +0000216 NXsByNameTy NXsByName;
217
Chris Lattnerae506702008-01-06 01:10:31 +0000218 for (CodeGenDAGPatterns::nx_iterator I = CGP.nx_begin(), E = CGP.nx_end();
Chris Lattner227da452008-01-05 22:54:53 +0000219 I != E; ++I)
220 NXsByName.insert(std::make_pair(I->first->getName(), I->second));
221
222 OS << "\n// Node transformations.\n";
223
224 for (NXsByNameTy::iterator I = NXsByName.begin(), E = NXsByName.end();
225 I != E; ++I) {
226 Record *SDNode = I->second.first;
227 std::string Code = I->second.second;
228
229 if (Code.empty()) continue; // Empty code? Skip it.
230
Chris Lattner14948ea2008-01-05 22:58:54 +0000231 std::string ClassName = CGP.getSDNodeInfo(SDNode).getSDClassName();
Chris Lattner227da452008-01-05 22:54:53 +0000232 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
233
234 OS << "inline SDOperand Transform_" << I->first << "(SDNode *" << C2
235 << ") {\n";
236 if (ClassName != "SDNode")
237 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
238 OS << Code << "\n}\n";
239 }
240}
241
242//===----------------------------------------------------------------------===//
Chris Lattner7fdd9342008-01-05 22:43:57 +0000243// Predicate emitter implementation.
244//
245
246void DAGISelEmitter::EmitPredicateFunctions(std::ostream &OS) {
247 OS << "\n// Predicate functions.\n";
248
249 // Walk the pattern fragments, adding them to a map, which sorts them by
250 // name.
251 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
252 PFsByNameTy PFsByName;
253
Chris Lattnerae506702008-01-06 01:10:31 +0000254 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattner7fdd9342008-01-05 22:43:57 +0000255 I != E; ++I)
256 PFsByName.insert(std::make_pair(I->first->getName(), *I));
257
258
259 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
260 I != E; ++I) {
261 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
262 TreePattern *P = I->second.second;
263
264 // If there is a code init for this fragment, emit the predicate code.
265 std::string Code = PatFragRecord->getValueAsCode("Predicate");
266 if (Code.empty()) continue;
267
268 if (P->getOnlyTree()->isLeaf())
269 OS << "inline bool Predicate_" << PatFragRecord->getName()
270 << "(SDNode *N) {\n";
271 else {
272 std::string ClassName =
Chris Lattner14948ea2008-01-05 22:58:54 +0000273 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattner7fdd9342008-01-05 22:43:57 +0000274 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
275
276 OS << "inline bool Predicate_" << PatFragRecord->getName()
277 << "(SDNode *" << C2 << ") {\n";
278 if (ClassName != "SDNode")
279 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
280 }
281 OS << Code << "\n}\n";
282 }
283
284 OS << "\n\n";
285}
286
287
288//===----------------------------------------------------------------------===//
289// PatternCodeEmitter implementation.
290//
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291class PatternCodeEmitter {
292private:
Chris Lattnerae506702008-01-06 01:10:31 +0000293 CodeGenDAGPatterns &CGP;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294
295 // Predicates.
296 ListInit *Predicates;
297 // Pattern cost.
298 unsigned Cost;
299 // Instruction selector pattern.
300 TreePatternNode *Pattern;
301 // Matched instruction.
302 TreePatternNode *Instruction;
303
304 // Node to name mapping
305 std::map<std::string, std::string> VariableMap;
306 // Node to operator mapping
307 std::map<std::string, Record*> OperatorMap;
Evan Cheng07f307d2008-02-05 22:50:29 +0000308 // Name of the folded node which produces a flag.
309 std::pair<std::string, unsigned> FoldedFlag;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 // Names of all the folded nodes which produce chains.
311 std::vector<std::pair<std::string, unsigned> > FoldedChains;
312 // Original input chain(s).
313 std::vector<std::pair<std::string, std::string> > OrigChains;
314 std::set<std::string> Duplicates;
315
Dan Gohman12a9c082008-02-06 22:27:42 +0000316 /// LSI - Load/Store information.
317 /// Save loads/stores matched by a pattern, and generate a MemOperandSDNode
318 /// for each memory access. This facilitates the use of AliasAnalysis in
319 /// the backend.
320 std::vector<std::string> LSI;
321
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322 /// GeneratedCode - This is the buffer that we emit code to. The first int
323 /// indicates whether this is an exit predicate (something that should be
324 /// tested, and if true, the match fails) [when 1], or normal code to emit
325 /// [when 0], or initialization code to emit [when 2].
326 std::vector<std::pair<unsigned, std::string> > &GeneratedCode;
327 /// GeneratedDecl - This is the set of all SDOperand declarations needed for
328 /// the set of patterns for each top-level opcode.
329 std::set<std::string> &GeneratedDecl;
330 /// TargetOpcodes - The target specific opcodes used by the resulting
331 /// instructions.
332 std::vector<std::string> &TargetOpcodes;
333 std::vector<std::string> &TargetVTs;
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000334 /// OutputIsVariadic - Records whether the instruction output pattern uses
335 /// variable_ops. This requires that the Emit function be passed an
336 /// additional argument to indicate where the input varargs operands
337 /// begin.
338 bool &OutputIsVariadic;
339 /// NumInputRootOps - Records the number of operands the root node of the
340 /// input pattern has. This information is used in the generated code to
341 /// pass to Emit functions when variable_ops processing is needed.
342 unsigned &NumInputRootOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343
344 std::string ChainName;
345 unsigned TmpNo;
346 unsigned OpcNo;
347 unsigned VTNo;
348
349 void emitCheck(const std::string &S) {
350 if (!S.empty())
351 GeneratedCode.push_back(std::make_pair(1, S));
352 }
353 void emitCode(const std::string &S) {
354 if (!S.empty())
355 GeneratedCode.push_back(std::make_pair(0, S));
356 }
357 void emitInit(const std::string &S) {
358 if (!S.empty())
359 GeneratedCode.push_back(std::make_pair(2, S));
360 }
361 void emitDecl(const std::string &S) {
362 assert(!S.empty() && "Invalid declaration");
363 GeneratedDecl.insert(S);
364 }
365 void emitOpcode(const std::string &Opc) {
366 TargetOpcodes.push_back(Opc);
367 OpcNo++;
368 }
369 void emitVT(const std::string &VT) {
370 TargetVTs.push_back(VT);
371 VTNo++;
372 }
373public:
Chris Lattnerae506702008-01-06 01:10:31 +0000374 PatternCodeEmitter(CodeGenDAGPatterns &cgp, ListInit *preds,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 TreePatternNode *pattern, TreePatternNode *instr,
376 std::vector<std::pair<unsigned, std::string> > &gc,
377 std::set<std::string> &gd,
378 std::vector<std::string> &to,
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000379 std::vector<std::string> &tv,
380 bool &oiv,
381 unsigned &niro)
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000382 : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 GeneratedCode(gc), GeneratedDecl(gd),
384 TargetOpcodes(to), TargetVTs(tv),
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000385 OutputIsVariadic(oiv), NumInputRootOps(niro),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 TmpNo(0), OpcNo(0), VTNo(0) {}
387
388 /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo
389 /// if the match fails. At this point, we already know that the opcode for N
390 /// matches, and the SDNode for the result has the RootName specified name.
391 void EmitMatchCode(TreePatternNode *N, TreePatternNode *P,
392 const std::string &RootName, const std::string &ChainSuffix,
393 bool &FoundChain) {
Dan Gohman12a9c082008-02-06 22:27:42 +0000394
395 // Save loads/stores matched by a pattern.
396 if (!N->isLeaf() && N->getName().empty()) {
Mon P Wang6bde9ec2008-06-25 08:15:39 +0000397 if (NodeHasProperty(N, SDNPMemOperand, CGP))
Dan Gohman12a9c082008-02-06 22:27:42 +0000398 LSI.push_back(RootName);
Dan Gohman12a9c082008-02-06 22:27:42 +0000399 }
400
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 bool isRoot = (P == NULL);
402 // Emit instruction predicates. Each predicate is just a string for now.
403 if (isRoot) {
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000404 // Record input varargs info.
405 NumInputRootOps = N->getNumChildren();
406
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 std::string PredicateCheck;
408 for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) {
409 if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) {
410 Record *Def = Pred->getDef();
411 if (!Def->isSubClassOf("Predicate")) {
412#ifndef NDEBUG
413 Def->dump();
414#endif
415 assert(0 && "Unknown predicate type!");
416 }
417 if (!PredicateCheck.empty())
418 PredicateCheck += " && ";
419 PredicateCheck += "(" + Def->getValueAsString("CondString") + ")";
420 }
421 }
422
423 emitCheck(PredicateCheck);
424 }
425
426 if (N->isLeaf()) {
427 if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
428 emitCheck("cast<ConstantSDNode>(" + RootName +
429 ")->getSignExtended() == " + itostr(II->getValue()));
430 return;
431 } else if (!NodeIsComplexPattern(N)) {
432 assert(0 && "Cannot match this as a leaf value!");
433 abort();
434 }
435 }
436
437 // If this node has a name associated with it, capture it in VariableMap. If
438 // we already saw this in the pattern, emit code to verify dagness.
439 if (!N->getName().empty()) {
440 std::string &VarMapEntry = VariableMap[N->getName()];
441 if (VarMapEntry.empty()) {
442 VarMapEntry = RootName;
443 } else {
444 // If we get here, this is a second reference to a specific name. Since
445 // we already have checked that the first reference is valid, we don't
446 // have to recursively match it, just check that it's the same as the
447 // previously named thing.
448 emitCheck(VarMapEntry + " == " + RootName);
449 return;
450 }
451
452 if (!N->isLeaf())
453 OperatorMap[N->getName()] = N->getOperator();
454 }
455
456
457 // Emit code to load the child nodes and match their contents recursively.
458 unsigned OpNo = 0;
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000459 bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP);
460 bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 bool EmittedUseCheck = false;
462 if (HasChain) {
463 if (NodeHasChain)
464 OpNo = 1;
465 if (!isRoot) {
466 // Multiple uses of actual result?
467 emitCheck(RootName + ".hasOneUse()");
468 EmittedUseCheck = true;
469 if (NodeHasChain) {
470 // If the immediate use can somehow reach this node through another
471 // path, then can't fold it either or it will create a cycle.
472 // e.g. In the following diagram, XX can reach ld through YY. If
473 // ld is folded into XX, then YY is both a predecessor and a successor
474 // of XX.
475 //
476 // [ld]
477 // ^ ^
478 // | |
479 // / \---
480 // / [YY]
481 // | ^
482 // [XX]-------|
483 bool NeedCheck = false;
484 if (P != Pattern)
485 NeedCheck = true;
486 else {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000487 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 NeedCheck =
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000489 P->getOperator() == CGP.get_intrinsic_void_sdnode() ||
490 P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() ||
491 P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000492 PInfo.getNumOperands() > 1 ||
493 PInfo.hasProperty(SDNPHasChain) ||
494 PInfo.hasProperty(SDNPInFlag) ||
495 PInfo.hasProperty(SDNPOptInFlag);
496 }
497
498 if (NeedCheck) {
499 std::string ParentName(RootName.begin(), RootName.end()-1);
500 emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName +
501 ".Val, N.Val)");
502 }
503 }
504 }
505
506 if (NodeHasChain) {
507 if (FoundChain) {
508 emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || "
509 "IsChainCompatible(" + ChainName + ".Val, " +
510 RootName + ".Val))");
511 OrigChains.push_back(std::make_pair(ChainName, RootName));
512 } else
513 FoundChain = true;
514 ChainName = "Chain" + ChainSuffix;
515 emitInit("SDOperand " + ChainName + " = " + RootName +
516 ".getOperand(0);");
517 }
518 }
519
520 // Don't fold any node which reads or writes a flag and has multiple uses.
521 // FIXME: We really need to separate the concepts of flag and "glue". Those
522 // real flag results, e.g. X86CMP output, can have multiple uses.
523 // FIXME: If the optional incoming flag does not exist. Then it is ok to
524 // fold it.
525 if (!isRoot &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000526 (PatternHasProperty(N, SDNPInFlag, CGP) ||
527 PatternHasProperty(N, SDNPOptInFlag, CGP) ||
528 PatternHasProperty(N, SDNPOutFlag, CGP))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 if (!EmittedUseCheck) {
530 // Multiple uses of actual result?
531 emitCheck(RootName + ".hasOneUse()");
532 }
533 }
534
535 // If there is a node predicate for this, emit the call.
536 if (!N->getPredicateFn().empty())
537 emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)");
538
539
540 // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is
541 // a constant without a predicate fn that has more that one bit set, handle
542 // this as a special case. This is usually for targets that have special
543 // handling of certain large constants (e.g. alpha with it's 8/16/32-bit
544 // handling stuff). Using these instructions is often far more efficient
545 // than materializing the constant. Unfortunately, both the instcombiner
546 // and the dag combiner can often infer that bits are dead, and thus drop
547 // them from the mask in the dag. For example, it might turn 'AND X, 255'
548 // into 'AND X, 254' if it knows the low bit is set. Emit code that checks
549 // to handle this.
550 if (!N->isLeaf() &&
551 (N->getOperator()->getName() == "and" ||
552 N->getOperator()->getName() == "or") &&
553 N->getChild(1)->isLeaf() &&
554 N->getChild(1)->getPredicateFn().empty()) {
555 if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) {
556 if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits.
557 emitInit("SDOperand " + RootName + "0" + " = " +
558 RootName + ".getOperand(" + utostr(0) + ");");
559 emitInit("SDOperand " + RootName + "1" + " = " +
560 RootName + ".getOperand(" + utostr(1) + ");");
561
562 emitCheck("isa<ConstantSDNode>(" + RootName + "1)");
563 const char *MaskPredicate = N->getOperator()->getName() == "or"
564 ? "CheckOrMask(" : "CheckAndMask(";
565 emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" +
566 RootName + "1), " + itostr(II->getValue()) + ")");
567
Christopher Lamb059c7c92008-01-31 07:27:46 +0000568 EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), RootName,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 ChainSuffix + utostr(0), FoundChain);
570 return;
571 }
572 }
573 }
574
575 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
576 emitInit("SDOperand " + RootName + utostr(OpNo) + " = " +
577 RootName + ".getOperand(" +utostr(OpNo) + ");");
578
Christopher Lamb059c7c92008-01-31 07:27:46 +0000579 EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), RootName,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 ChainSuffix + utostr(OpNo), FoundChain);
581 }
582
583 // Handle cases when root is a complex pattern.
584 const ComplexPattern *CP;
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000585 if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 std::string Fn = CP->getSelectFunc();
587 unsigned NumOps = CP->getNumOperands();
588 for (unsigned i = 0; i < NumOps; ++i) {
589 emitDecl("CPTmp" + utostr(i));
590 emitCode("SDOperand CPTmp" + utostr(i) + ";");
591 }
592 if (CP->hasProperty(SDNPHasChain)) {
593 emitDecl("CPInChain");
594 emitDecl("Chain" + ChainSuffix);
595 emitCode("SDOperand CPInChain;");
596 emitCode("SDOperand Chain" + ChainSuffix + ";");
597 }
598
599 std::string Code = Fn + "(" + RootName + ", " + RootName;
600 for (unsigned i = 0; i < NumOps; i++)
601 Code += ", CPTmp" + utostr(i);
602 if (CP->hasProperty(SDNPHasChain)) {
603 ChainName = "Chain" + ChainSuffix;
604 Code += ", CPInChain, Chain" + ChainSuffix;
605 }
606 emitCheck(Code + ")");
607 }
608 }
609
610 void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent,
Christopher Lamb059c7c92008-01-31 07:27:46 +0000611 const std::string &RootName,
612 const std::string &ParentRootName,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 const std::string &ChainSuffix, bool &FoundChain) {
614 if (!Child->isLeaf()) {
615 // If it's not a leaf, recursively match.
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000616 const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 emitCheck(RootName + ".getOpcode() == " +
618 CInfo.getEnumName());
619 EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain);
Evan Cheng07f307d2008-02-05 22:50:29 +0000620 bool HasChain = false;
621 if (NodeHasProperty(Child, SDNPHasChain, CGP)) {
622 HasChain = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623 FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults()));
Evan Cheng07f307d2008-02-05 22:50:29 +0000624 }
625 if (NodeHasProperty(Child, SDNPOutFlag, CGP)) {
626 assert(FoldedFlag.first == "" && FoldedFlag.second == 0 &&
627 "Pattern folded multiple nodes which produce flags?");
628 FoldedFlag = std::make_pair(RootName,
629 CInfo.getNumResults() + (unsigned)HasChain);
630 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000631 } else {
632 // If this child has a name associated with it, capture it in VarMap. If
633 // we already saw this in the pattern, emit code to verify dagness.
634 if (!Child->getName().empty()) {
635 std::string &VarMapEntry = VariableMap[Child->getName()];
636 if (VarMapEntry.empty()) {
637 VarMapEntry = RootName;
638 } else {
639 // If we get here, this is a second reference to a specific name.
640 // Since we already have checked that the first reference is valid,
641 // we don't have to recursively match it, just check that it's the
642 // same as the previously named thing.
643 emitCheck(VarMapEntry + " == " + RootName);
644 Duplicates.insert(RootName);
645 return;
646 }
647 }
648
649 // Handle leaves of various types.
650 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
651 Record *LeafRec = DI->getDef();
652 if (LeafRec->isSubClassOf("RegisterClass") ||
653 LeafRec->getName() == "ptr_rc") {
654 // Handle register references. Nothing to do here.
655 } else if (LeafRec->isSubClassOf("Register")) {
656 // Handle register references.
657 } else if (LeafRec->isSubClassOf("ComplexPattern")) {
658 // Handle complex pattern.
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000659 const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000660 std::string Fn = CP->getSelectFunc();
661 unsigned NumOps = CP->getNumOperands();
662 for (unsigned i = 0; i < NumOps; ++i) {
663 emitDecl("CPTmp" + utostr(i));
664 emitCode("SDOperand CPTmp" + utostr(i) + ";");
665 }
666 if (CP->hasProperty(SDNPHasChain)) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000667 const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668 FoldedChains.push_back(std::make_pair("CPInChain",
669 PInfo.getNumResults()));
670 ChainName = "Chain" + ChainSuffix;
671 emitDecl("CPInChain");
672 emitDecl(ChainName);
673 emitCode("SDOperand CPInChain;");
674 emitCode("SDOperand " + ChainName + ";");
675 }
676
Christopher Lamb059c7c92008-01-31 07:27:46 +0000677 std::string Code = Fn + "(";
678 if (CP->hasAttribute(CPAttrParentAsRoot)) {
679 Code += ParentRootName + ", ";
680 } else {
681 Code += "N, ";
682 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 if (CP->hasProperty(SDNPHasChain)) {
684 std::string ParentName(RootName.begin(), RootName.end()-1);
685 Code += ParentName + ", ";
686 }
687 Code += RootName;
688 for (unsigned i = 0; i < NumOps; i++)
689 Code += ", CPTmp" + utostr(i);
690 if (CP->hasProperty(SDNPHasChain))
691 Code += ", CPInChain, Chain" + ChainSuffix;
692 emitCheck(Code + ")");
693 } else if (LeafRec->getName() == "srcvalue") {
694 // Place holder for SRCVALUE nodes. Nothing to do here.
695 } else if (LeafRec->isSubClassOf("ValueType")) {
696 // Make sure this is the specified value type.
697 emitCheck("cast<VTSDNode>(" + RootName +
698 ")->getVT() == MVT::" + LeafRec->getName());
699 } else if (LeafRec->isSubClassOf("CondCode")) {
700 // Make sure this is the specified cond code.
701 emitCheck("cast<CondCodeSDNode>(" + RootName +
702 ")->get() == ISD::" + LeafRec->getName());
703 } else {
704#ifndef NDEBUG
705 Child->dump();
706 cerr << " ";
707#endif
708 assert(0 && "Unknown leaf type!");
709 }
710
711 // If there is a node predicate for this, emit the call.
712 if (!Child->getPredicateFn().empty())
713 emitCheck(Child->getPredicateFn() + "(" + RootName +
714 ".Val)");
715 } else if (IntInit *II =
716 dynamic_cast<IntInit*>(Child->getLeafValue())) {
717 emitCheck("isa<ConstantSDNode>(" + RootName + ")");
718 unsigned CTmp = TmpNo++;
719 emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+
720 RootName + ")->getSignExtended();");
721
722 emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue()));
723 } else {
724#ifndef NDEBUG
725 Child->dump();
726#endif
727 assert(0 && "Unknown leaf type!");
728 }
729 }
730 }
731
732 /// EmitResultCode - Emit the action for a pattern. Now that it has matched
733 /// we actually have to build a DAG!
734 std::vector<std::string>
Evan Cheng775baac2007-09-12 23:30:14 +0000735 EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 bool InFlagDecled, bool ResNodeDecled,
737 bool LikeLeaf = false, bool isRoot = false) {
738 // List of arguments of getTargetNode() or SelectNodeTo().
739 std::vector<std::string> NodeOps;
740 // This is something selected from the pattern we matched.
741 if (!N->getName().empty()) {
Scott Michel30124c22008-01-29 02:29:31 +0000742 const std::string &VarName = N->getName();
743 std::string Val = VariableMap[VarName];
744 bool ModifiedVal = false;
Scott Michelac7091c2008-02-15 23:05:48 +0000745 if (Val.empty()) {
Bill Wendling39d33752008-02-26 10:45:29 +0000746 cerr << "Variable '" << VarName << " referenced but not defined "
747 << "and not caught earlier!\n";
748 abort();
Scott Michelac7091c2008-02-15 23:05:48 +0000749 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000750 if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') {
751 // Already selected this operand, just return the tmpval.
752 NodeOps.push_back(Val);
753 return NodeOps;
754 }
755
756 const ComplexPattern *CP;
757 unsigned ResNo = TmpNo++;
758 if (!N->isLeaf() && N->getOperator()->getName() == "imm") {
759 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
760 std::string CastType;
Scott Michel30124c22008-01-29 02:29:31 +0000761 std::string TmpVar = "Tmp" + utostr(ResNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000762 switch (N->getTypeNum(0)) {
763 default:
764 cerr << "Cannot handle " << getEnumName(N->getTypeNum(0))
765 << " type as an immediate constant. Aborting\n";
766 abort();
767 case MVT::i1: CastType = "bool"; break;
768 case MVT::i8: CastType = "unsigned char"; break;
769 case MVT::i16: CastType = "unsigned short"; break;
770 case MVT::i32: CastType = "unsigned"; break;
771 case MVT::i64: CastType = "uint64_t"; break;
772 }
Scott Michel30124c22008-01-29 02:29:31 +0000773 emitCode("SDOperand " + TmpVar +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000774 " = CurDAG->getTargetConstant(((" + CastType +
775 ") cast<ConstantSDNode>(" + Val + ")->getValue()), " +
776 getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000777 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
778 // value if used multiple times by this pattern result.
Scott Michel30124c22008-01-29 02:29:31 +0000779 Val = TmpVar;
780 ModifiedVal = true;
781 NodeOps.push_back(Val);
Nate Begemane2ba64f2008-02-14 08:57:00 +0000782 } else if (!N->isLeaf() && N->getOperator()->getName() == "fpimm") {
783 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
784 std::string TmpVar = "Tmp" + utostr(ResNo);
785 emitCode("SDOperand " + TmpVar +
786 " = CurDAG->getTargetConstantFP(cast<ConstantFPSDNode>(" +
787 Val + ")->getValueAPF(), cast<ConstantFPSDNode>(" + Val +
788 ")->getValueType(0));");
789 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this
790 // value if used multiple times by this pattern result.
791 Val = TmpVar;
792 ModifiedVal = true;
793 NodeOps.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){
795 Record *Op = OperatorMap[N->getName()];
796 // Transform ExternalSymbol to TargetExternalSymbol
797 if (Op && Op->getName() == "externalsym") {
Scott Michel30124c22008-01-29 02:29:31 +0000798 std::string TmpVar = "Tmp"+utostr(ResNo);
799 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000800 "ExternalSymbol(cast<ExternalSymbolSDNode>(" +
801 Val + ")->getSymbol(), " +
802 getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000803 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
804 // this value if used multiple times by this pattern result.
Scott Michel30124c22008-01-29 02:29:31 +0000805 Val = TmpVar;
806 ModifiedVal = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807 }
Scott Michel30124c22008-01-29 02:29:31 +0000808 NodeOps.push_back(Val);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr"
810 || N->getOperator()->getName() == "tglobaltlsaddr")) {
811 Record *Op = OperatorMap[N->getName()];
812 // Transform GlobalAddress to TargetGlobalAddress
813 if (Op && (Op->getName() == "globaladdr" ||
814 Op->getName() == "globaltlsaddr")) {
Scott Michel30124c22008-01-29 02:29:31 +0000815 std::string TmpVar = "Tmp" + utostr(ResNo);
816 emitCode("SDOperand " + TmpVar + " = CurDAG->getTarget"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000817 "GlobalAddress(cast<GlobalAddressSDNode>(" + Val +
818 ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) +
819 ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000820 // Add Tmp<ResNo> to VariableMap, so that we don't multiply select
821 // this value if used multiple times by this pattern result.
Scott Michel30124c22008-01-29 02:29:31 +0000822 Val = TmpVar;
823 ModifiedVal = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000824 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000825 NodeOps.push_back(Val);
Scott Michel30124c22008-01-29 02:29:31 +0000826 } else if (!N->isLeaf()
827 && (N->getOperator()->getName() == "texternalsym"
828 || N->getOperator()->getName() == "tconstpool")) {
829 // Do not rewrite the variable name, since we don't generate a new
830 // temporary.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 NodeOps.push_back(Val);
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000832 } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000833 for (unsigned i = 0; i < CP->getNumOperands(); ++i) {
834 emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");");
835 NodeOps.push_back("CPTmp" + utostr(i));
836 }
837 } else {
838 // This node, probably wrapped in a SDNodeXForm, behaves like a leaf
839 // node even if it isn't one. Don't select it.
840 if (!LikeLeaf) {
841 emitCode("AddToISelQueue(" + Val + ");");
842 if (isRoot && N->isLeaf()) {
843 emitCode("ReplaceUses(N, " + Val + ");");
844 emitCode("return NULL;");
845 }
846 }
847 NodeOps.push_back(Val);
848 }
Scott Michel30124c22008-01-29 02:29:31 +0000849
850 if (ModifiedVal) {
851 VariableMap[VarName] = Val;
852 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000853 return NodeOps;
854 }
855 if (N->isLeaf()) {
856 // If this is an explicit register reference, handle it.
857 if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) {
858 unsigned ResNo = TmpNo++;
859 if (DI->getDef()->isSubClassOf("Register")) {
860 emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" +
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000861 getQualifiedName(DI->getDef()) + ", " +
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 getEnumName(N->getTypeNum(0)) + ");");
863 NodeOps.push_back("Tmp" + utostr(ResNo));
864 return NodeOps;
865 } else if (DI->getDef()->getName() == "zero_reg") {
866 emitCode("SDOperand Tmp" + utostr(ResNo) +
867 " = CurDAG->getRegister(0, " +
868 getEnumName(N->getTypeNum(0)) + ");");
869 NodeOps.push_back("Tmp" + utostr(ResNo));
870 return NodeOps;
871 }
872 } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) {
873 unsigned ResNo = TmpNo++;
874 assert(N->getExtTypes().size() == 1 && "Multiple types not handled!");
875 emitCode("SDOperand Tmp" + utostr(ResNo) +
Scott Michelac7091c2008-02-15 23:05:48 +0000876 " = CurDAG->getTargetConstant(0x" + itohexstr(II->getValue()) +
877 "ULL, " + getEnumName(N->getTypeNum(0)) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000878 NodeOps.push_back("Tmp" + utostr(ResNo));
879 return NodeOps;
880 }
881
882#ifndef NDEBUG
883 N->dump();
884#endif
885 assert(0 && "Unknown leaf type!");
886 return NodeOps;
887 }
888
889 Record *Op = N->getOperator();
890 if (Op->isSubClassOf("Instruction")) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000891 const CodeGenTarget &CGT = CGP.getTargetInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000892 CodeGenInstruction &II = CGT.getInstruction(Op->getName());
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000893 const DAGInstruction &Inst = CGP.getInstruction(Op);
Chris Lattner7c6e5852008-01-06 01:52:22 +0000894 const TreePattern *InstPat = Inst.getPattern();
Evan Chengf031fcb2007-09-25 01:48:59 +0000895 // FIXME: Assume actual pattern comes before "implicit".
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000896 TreePatternNode *InstPatNode =
Evan Cheng775baac2007-09-12 23:30:14 +0000897 isRoot ? (InstPat ? InstPat->getTree(0) : Pattern)
898 : (InstPat ? InstPat->getTree(0) : NULL);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000899 if (InstPatNode && InstPatNode->getOperator()->getName() == "set") {
Evan Chengf37df842007-09-11 19:52:18 +0000900 InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000901 }
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000902 bool IsVariadic = isRoot && II.isVariadic;
Evan Cheng775baac2007-09-12 23:30:14 +0000903 // FIXME: fix how we deal with physical register operands.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000904 bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0;
Evan Cheng775baac2007-09-12 23:30:14 +0000905 bool HasImpResults = isRoot && DstRegs.size() > 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 bool NodeHasOptInFlag = isRoot &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000907 PatternHasProperty(Pattern, SDNPOptInFlag, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000908 bool NodeHasInFlag = isRoot &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000909 PatternHasProperty(Pattern, SDNPInFlag, CGP);
Evan Chengdec1dd12007-09-07 23:59:02 +0000910 bool NodeHasOutFlag = isRoot &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000911 PatternHasProperty(Pattern, SDNPOutFlag, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000912 bool NodeHasChain = InstPatNode &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000913 PatternHasProperty(InstPatNode, SDNPHasChain, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914 bool InputHasChain = isRoot &&
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000915 NodeHasProperty(Pattern, SDNPHasChain, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 unsigned NumResults = Inst.getNumResults();
Evan Cheng775baac2007-09-12 23:30:14 +0000917 unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000918
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000919 // Record output varargs info.
920 OutputIsVariadic = IsVariadic;
921
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000922 if (NodeHasOptInFlag) {
923 emitCode("bool HasInFlag = "
924 "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);");
925 }
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000926 if (IsVariadic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000927 emitCode("SmallVector<SDOperand, 8> Ops" + utostr(OpcNo) + ";");
928
929 // How many results is this pattern expected to produce?
Evan Cheng775baac2007-09-12 23:30:14 +0000930 unsigned NumPatResults = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000931 for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) {
Duncan Sands92c43912008-06-06 12:08:01 +0000932 MVT::SimpleValueType VT = Pattern->getTypeNum(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000933 if (VT != MVT::isVoid && VT != MVT::Flag)
Evan Cheng775baac2007-09-12 23:30:14 +0000934 NumPatResults++;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000935 }
936
937 if (OrigChains.size() > 0) {
938 // The original input chain is being ignored. If it is not just
939 // pointing to the op that's being folded, we should create a
940 // TokenFactor with it and the chain of the folded op as the new chain.
941 // We could potentially be doing multiple levels of folding, in that
942 // case, the TokenFactor can have more operands.
943 emitCode("SmallVector<SDOperand, 8> InChains;");
944 for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) {
945 emitCode("if (" + OrigChains[i].first + ".Val != " +
946 OrigChains[i].second + ".Val) {");
947 emitCode(" AddToISelQueue(" + OrigChains[i].first + ");");
948 emitCode(" InChains.push_back(" + OrigChains[i].first + ");");
949 emitCode("}");
950 }
951 emitCode("AddToISelQueue(" + ChainName + ");");
952 emitCode("InChains.push_back(" + ChainName + ");");
953 emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, "
954 "&InChains[0], InChains.size());");
955 }
956
957 // Loop over all of the operands of the instruction pattern, emitting code
958 // to fill them all in. The node 'N' usually has number children equal to
959 // the number of input operands of the instruction. However, in cases
960 // where there are predicate operands for an instruction, we need to fill
961 // in the 'execute always' values. Match up the node operands to the
962 // instruction operands to do this.
963 std::vector<std::string> AllOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000964 for (unsigned ChildNo = 0, InstOpNo = NumResults;
965 InstOpNo != II.OperandList.size(); ++InstOpNo) {
966 std::vector<std::string> Ops;
967
Dan Gohman3329ffe2008-05-29 19:57:41 +0000968 // Determine what to emit for this operand.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000969 Record *OperandNode = II.OperandList[InstOpNo].Rec;
Dan Gohman2c4be2a2008-05-31 02:11:25 +0000970 if ((OperandNode->isSubClassOf("PredicateOperand") ||
971 OperandNode->isSubClassOf("OptionalDefOperand")) &&
972 !CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) {
Dan Gohman3329ffe2008-05-29 19:57:41 +0000973 // This is a predicate or optional def operand; emit the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000974 // 'default ops' operands.
975 const DAGDefaultOperand &DefaultOp =
Chris Lattner4ca8ff02008-01-05 22:25:12 +0000976 CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000977 for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) {
Evan Chengdb1f2462007-09-17 22:26:41 +0000978 Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000979 InFlagDecled, ResNodeDecled);
980 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000981 }
Dan Gohman3329ffe2008-05-29 19:57:41 +0000982 } else {
983 // Otherwise this is a normal operand or a predicate operand without
984 // 'execute always'; emit it.
985 Ops = EmitResultCode(N->getChild(ChildNo), DstRegs,
986 InFlagDecled, ResNodeDecled);
987 AllOps.insert(AllOps.end(), Ops.begin(), Ops.end());
988 ++ChildNo;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000989 }
990 }
991
992 // Emit all the chain and CopyToReg stuff.
993 bool ChainEmitted = NodeHasChain;
994 if (NodeHasChain)
995 emitCode("AddToISelQueue(" + ChainName + ");");
996 if (NodeHasInFlag || HasImpInputs)
997 EmitInFlagSelectCode(Pattern, "N", ChainEmitted,
998 InFlagDecled, ResNodeDecled, true);
999 if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) {
1000 if (!InFlagDecled) {
1001 emitCode("SDOperand InFlag(0, 0);");
1002 InFlagDecled = true;
1003 }
1004 if (NodeHasOptInFlag) {
1005 emitCode("if (HasInFlag) {");
1006 emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);");
1007 emitCode(" AddToISelQueue(InFlag);");
1008 emitCode("}");
1009 }
1010 }
1011
1012 unsigned ResNo = TmpNo++;
1013 if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag ||
Evan Chengdec1dd12007-09-07 23:59:02 +00001014 NodeHasOptInFlag || HasImpResults) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001015 std::string Code;
1016 std::string Code2;
1017 std::string NodeName;
1018 if (!isRoot) {
1019 NodeName = "Tmp" + utostr(ResNo);
Dan Gohman875770e2007-07-24 22:58:00 +00001020 Code2 = "SDOperand " + NodeName + "(";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001021 } else {
1022 NodeName = "ResNode";
1023 if (!ResNodeDecled) {
1024 Code2 = "SDNode *" + NodeName + " = ";
1025 ResNodeDecled = true;
1026 } else
1027 Code2 = NodeName + " = ";
1028 }
1029
Evan Cheng775baac2007-09-12 23:30:14 +00001030 Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001031 unsigned OpsNo = OpcNo;
1032 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1033
1034 // Output order: results, chain, flags
1035 // Result types.
1036 if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) {
1037 Code += ", VT" + utostr(VTNo);
1038 emitVT(getEnumName(N->getTypeNum(0)));
1039 }
Evan Chengdec1dd12007-09-07 23:59:02 +00001040 // Add types for implicit results in physical registers, scheduler will
1041 // care of adding copyfromreg nodes.
Evan Cheng775baac2007-09-12 23:30:14 +00001042 for (unsigned i = 0; i < NumDstRegs; i++) {
1043 Record *RR = DstRegs[i];
1044 if (RR->isSubClassOf("Register")) {
Duncan Sands92c43912008-06-06 12:08:01 +00001045 MVT::SimpleValueType RVT = getRegisterValueType(RR, CGT);
Evan Cheng775baac2007-09-12 23:30:14 +00001046 Code += ", " + getEnumName(RVT);
Evan Chengdec1dd12007-09-07 23:59:02 +00001047 }
1048 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001049 if (NodeHasChain)
1050 Code += ", MVT::Other";
1051 if (NodeHasOutFlag)
1052 Code += ", MVT::Flag";
1053
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001054 // Inputs.
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001055 if (IsVariadic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001056 for (unsigned i = 0, e = AllOps.size(); i != e; ++i)
1057 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");");
1058 AllOps.clear();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001059
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001060 // Figure out whether any operands at the end of the op list are not
1061 // part of the variable section.
1062 std::string EndAdjust;
1063 if (NodeHasInFlag || HasImpInputs)
1064 EndAdjust = "-1"; // Always has one flag.
1065 else if (NodeHasOptInFlag)
1066 EndAdjust = "-(HasInFlag?1:0)"; // May have a flag.
1067
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001068 emitCode("for (unsigned i = NumInputRootOps + " + utostr(NodeHasChain) +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001069 ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {");
1070
1071 emitCode(" AddToISelQueue(N.getOperand(i));");
1072 emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));");
1073 emitCode("}");
1074 }
1075
Dan Gohman3d6301a2008-06-02 17:40:38 +00001076 // Generate MemOperandSDNodes nodes for each memory accesses covered by
1077 // this pattern.
1078 if (II.isSimpleLoad | II.mayLoad | II.mayStore) {
1079 std::vector<std::string>::const_iterator mi, mie;
1080 for (mi = LSI.begin(), mie = LSI.end(); mi != mie; ++mi) {
1081 emitCode("SDOperand LSI_" + *mi + " = "
Mon P Wang6bde9ec2008-06-25 08:15:39 +00001082 "CurDAG->getMemOperand(cast<MemSDNode>(" +
Dan Gohman3d6301a2008-06-02 17:40:38 +00001083 *mi + ")->getMemOperand());");
1084 if (IsVariadic)
1085 emitCode("Ops" + utostr(OpsNo) + ".push_back(LSI_" + *mi + ");");
1086 else
1087 AllOps.push_back("LSI_" + *mi);
1088 }
1089 }
1090
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001091 if (NodeHasChain) {
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001092 if (IsVariadic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");");
1094 else
1095 AllOps.push_back(ChainName);
1096 }
1097
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001098 if (IsVariadic) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001099 if (NodeHasInFlag || HasImpInputs)
1100 emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1101 else if (NodeHasOptInFlag) {
1102 emitCode("if (HasInFlag)");
1103 emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);");
1104 }
1105 Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) +
1106 ".size()";
1107 } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
Evan Cheng775baac2007-09-12 23:30:14 +00001108 AllOps.push_back("InFlag");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001109
1110 unsigned NumOps = AllOps.size();
1111 if (NumOps) {
1112 if (!NodeHasOptInFlag && NumOps < 4) {
1113 for (unsigned i = 0; i != NumOps; ++i)
1114 Code += ", " + AllOps[i];
1115 } else {
1116 std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { ";
1117 for (unsigned i = 0; i != NumOps; ++i) {
1118 OpsCode += AllOps[i];
1119 if (i != NumOps-1)
1120 OpsCode += ", ";
1121 }
1122 emitCode(OpsCode + " };");
1123 Code += ", Ops" + utostr(OpsNo) + ", ";
1124 if (NodeHasOptInFlag) {
1125 Code += "HasInFlag ? ";
1126 Code += utostr(NumOps) + " : " + utostr(NumOps-1);
1127 } else
1128 Code += utostr(NumOps);
1129 }
1130 }
1131
1132 if (!isRoot)
1133 Code += "), 0";
1134 emitCode(Code2 + Code + ");");
1135
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00001136 if (NodeHasChain) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001137 // Remember which op produces the chain.
1138 if (!isRoot)
1139 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng775baac2007-09-12 23:30:14 +00001140 ".Val, " + utostr(NumResults+NumDstRegs) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001141 else
1142 emitCode(ChainName + " = SDOperand(" + NodeName +
Evan Cheng775baac2007-09-12 23:30:14 +00001143 ", " + utostr(NumResults+NumDstRegs) + ");");
Anton Korobeynikov357a27d2008-02-20 11:08:44 +00001144 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001145
1146 if (!isRoot) {
1147 NodeOps.push_back("Tmp" + utostr(ResNo));
1148 return NodeOps;
1149 }
1150
1151 bool NeedReplace = false;
1152 if (NodeHasOutFlag) {
1153 if (!InFlagDecled) {
Dan Gohman875770e2007-07-24 22:58:00 +00001154 emitCode("SDOperand InFlag(ResNode, " +
Evan Chengdb1f2462007-09-17 22:26:41 +00001155 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001156 InFlagDecled = true;
1157 } else
1158 emitCode("InFlag = SDOperand(ResNode, " +
Evan Chengdb1f2462007-09-17 22:26:41 +00001159 utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001160 }
1161
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001162 if (FoldedChains.size() > 0) {
1163 std::string Code;
1164 for (unsigned j = 0, e = FoldedChains.size(); j < e; j++)
1165 emitCode("ReplaceUses(SDOperand(" +
1166 FoldedChains[j].first + ".Val, " +
1167 utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " +
Evan Cheng775baac2007-09-12 23:30:14 +00001168 utostr(NumResults+NumDstRegs) + "));");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001169 NeedReplace = true;
1170 }
1171
1172 if (NodeHasOutFlag) {
Evan Cheng07f307d2008-02-05 22:50:29 +00001173 if (FoldedFlag.first != "") {
1174 emitCode("ReplaceUses(SDOperand(" + FoldedFlag.first + ".Val, " +
1175 utostr(FoldedFlag.second) + "), InFlag);");
1176 } else {
1177 assert(NodeHasProperty(Pattern, SDNPOutFlag, CGP));
1178 emitCode("ReplaceUses(SDOperand(N.Val, " +
1179 utostr(NumPatResults + (unsigned)InputHasChain)
1180 +"), InFlag);");
1181 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001182 NeedReplace = true;
1183 }
1184
Evan Chengdb1f2462007-09-17 22:26:41 +00001185 if (NeedReplace && InputHasChain)
1186 emitCode("ReplaceUses(SDOperand(N.Val, " +
1187 utostr(NumPatResults) + "), SDOperand(" + ChainName
1188 + ".Val, " + ChainName + ".ResNo" + "));");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001189
1190 // User does not expect the instruction would produce a chain!
1191 if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) {
1192 ;
1193 } else if (InputHasChain && !NodeHasChain) {
1194 // One of the inner node produces a chain.
1195 if (NodeHasOutFlag)
Bill Wendling39d33752008-02-26 10:45:29 +00001196 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) +
1197 "), SDOperand(ResNode, N.ResNo-1));");
1198 emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) +
1199 "), " + ChainName + ");");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001200 }
1201
Evan Chengdb1f2462007-09-17 22:26:41 +00001202 emitCode("return ResNode;");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001203 } else {
1204 std::string Code = "return CurDAG->SelectNodeTo(N.Val, Opc" +
1205 utostr(OpcNo);
1206 if (N->getTypeNum(0) != MVT::isVoid)
1207 Code += ", VT" + utostr(VTNo);
1208 if (NodeHasOutFlag)
1209 Code += ", MVT::Flag";
1210
1211 if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs)
1212 AllOps.push_back("InFlag");
1213
1214 unsigned NumOps = AllOps.size();
1215 if (NumOps) {
1216 if (!NodeHasOptInFlag && NumOps < 4) {
1217 for (unsigned i = 0; i != NumOps; ++i)
1218 Code += ", " + AllOps[i];
1219 } else {
1220 std::string OpsCode = "SDOperand Ops" + utostr(OpcNo) + "[] = { ";
1221 for (unsigned i = 0; i != NumOps; ++i) {
1222 OpsCode += AllOps[i];
1223 if (i != NumOps-1)
1224 OpsCode += ", ";
1225 }
1226 emitCode(OpsCode + " };");
1227 Code += ", Ops" + utostr(OpcNo) + ", ";
1228 Code += utostr(NumOps);
1229 }
1230 }
1231 emitCode(Code + ");");
1232 emitOpcode(II.Namespace + "::" + II.TheDef->getName());
1233 if (N->getTypeNum(0) != MVT::isVoid)
1234 emitVT(getEnumName(N->getTypeNum(0)));
1235 }
1236
1237 return NodeOps;
1238 } else if (Op->isSubClassOf("SDNodeXForm")) {
1239 assert(N->getNumChildren() == 1 && "node xform should have one child!");
1240 // PatLeaf node - the operand may or may not be a leaf node. But it should
1241 // behave like one.
1242 std::vector<std::string> Ops =
Evan Chengdb1f2462007-09-17 22:26:41 +00001243 EmitResultCode(N->getChild(0), DstRegs, InFlagDecled,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001244 ResNodeDecled, true);
1245 unsigned ResNo = TmpNo++;
1246 emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName()
1247 + "(" + Ops.back() + ".Val);");
1248 NodeOps.push_back("Tmp" + utostr(ResNo));
1249 if (isRoot)
1250 emitCode("return Tmp" + utostr(ResNo) + ".Val;");
1251 return NodeOps;
1252 } else {
1253 N->dump();
1254 cerr << "\n";
1255 throw std::string("Unknown node in result pattern!");
1256 }
1257 }
1258
1259 /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat'
1260 /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that
1261 /// 'Pat' may be missing types. If we find an unresolved type to add a check
1262 /// for, this returns true otherwise false if Pat has all types.
1263 bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other,
1264 const std::string &Prefix, bool isRoot = false) {
1265 // Did we find one?
1266 if (Pat->getExtTypes() != Other->getExtTypes()) {
1267 // Move a type over from 'other' to 'pat'.
1268 Pat->setTypes(Other->getExtTypes());
1269 // The top level node type is checked outside of the select function.
1270 if (!isRoot)
1271 emitCheck(Prefix + ".Val->getValueType(0) == " +
1272 getName(Pat->getTypeNum(0)));
1273 return true;
1274 }
1275
1276 unsigned OpNo =
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001277 (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001278 for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo)
1279 if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i),
1280 Prefix + utostr(OpNo)))
1281 return true;
1282 return false;
1283 }
1284
1285private:
1286 /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is
1287 /// being built.
1288 void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName,
1289 bool &ChainEmitted, bool &InFlagDecled,
1290 bool &ResNodeDecled, bool isRoot = false) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001291 const CodeGenTarget &T = CGP.getTargetInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001292 unsigned OpNo =
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001293 (unsigned) NodeHasProperty(N, SDNPHasChain, CGP);
1294 bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001295 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) {
1296 TreePatternNode *Child = N->getChild(i);
1297 if (!Child->isLeaf()) {
1298 EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted,
1299 InFlagDecled, ResNodeDecled);
1300 } else {
1301 if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) {
1302 if (!Child->getName().empty()) {
1303 std::string Name = RootName + utostr(OpNo);
1304 if (Duplicates.find(Name) != Duplicates.end())
1305 // A duplicate! Do not emit a copy for this node.
1306 continue;
1307 }
1308
1309 Record *RR = DI->getDef();
1310 if (RR->isSubClassOf("Register")) {
Duncan Sands92c43912008-06-06 12:08:01 +00001311 MVT::SimpleValueType RVT = getRegisterValueType(RR, T);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 if (RVT == MVT::Flag) {
1313 if (!InFlagDecled) {
1314 emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";");
1315 InFlagDecled = true;
1316 } else
1317 emitCode("InFlag = " + RootName + utostr(OpNo) + ";");
1318 emitCode("AddToISelQueue(InFlag);");
1319 } else {
1320 if (!ChainEmitted) {
1321 emitCode("SDOperand Chain = CurDAG->getEntryNode();");
1322 ChainName = "Chain";
1323 ChainEmitted = true;
1324 }
1325 emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");");
1326 if (!InFlagDecled) {
1327 emitCode("SDOperand InFlag(0, 0);");
1328 InFlagDecled = true;
1329 }
1330 std::string Decl = (!ResNodeDecled) ? "SDNode *" : "";
1331 emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName +
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001332 ", " + getQualifiedName(RR) +
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001333 ", " + RootName + utostr(OpNo) + ", InFlag).Val;");
1334 ResNodeDecled = true;
1335 emitCode(ChainName + " = SDOperand(ResNode, 0);");
1336 emitCode("InFlag = SDOperand(ResNode, 1);");
1337 }
1338 }
1339 }
1340 }
1341 }
1342
1343 if (HasInFlag) {
1344 if (!InFlagDecled) {
1345 emitCode("SDOperand InFlag = " + RootName +
1346 ".getOperand(" + utostr(OpNo) + ");");
1347 InFlagDecled = true;
1348 } else
1349 emitCode("InFlag = " + RootName +
1350 ".getOperand(" + utostr(OpNo) + ");");
1351 emitCode("AddToISelQueue(InFlag);");
1352 }
1353 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354};
1355
1356/// EmitCodeForPattern - Given a pattern to match, emit code to the specified
1357/// stream to match the pattern, and generate the code for the match if it
1358/// succeeds. Returns true if the pattern is not guaranteed to match.
Chris Lattner81915752008-01-05 22:30:17 +00001359void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001360 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
1361 std::set<std::string> &GeneratedDecl,
1362 std::vector<std::string> &TargetOpcodes,
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001363 std::vector<std::string> &TargetVTs,
1364 bool &OutputIsVariadic,
1365 unsigned &NumInputRootOps) {
1366 OutputIsVariadic = false;
1367 NumInputRootOps = 0;
1368
Chris Lattner14948ea2008-01-05 22:58:54 +00001369 PatternCodeEmitter Emitter(CGP, Pattern.getPredicates(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001370 Pattern.getSrcPattern(), Pattern.getDstPattern(),
1371 GeneratedCode, GeneratedDecl,
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001372 TargetOpcodes, TargetVTs,
1373 OutputIsVariadic, NumInputRootOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001374
1375 // Emit the matcher, capturing named arguments in VariableMap.
1376 bool FoundChain = false;
1377 Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain);
1378
1379 // TP - Get *SOME* tree pattern, we don't care which.
Chris Lattner14948ea2008-01-05 22:58:54 +00001380 TreePattern &TP = *CGP.pf_begin()->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001381
1382 // At this point, we know that we structurally match the pattern, but the
1383 // types of the nodes may not match. Figure out the fewest number of type
1384 // comparisons we need to emit. For example, if there is only one integer
1385 // type supported by a target, there should be no type comparisons at all for
1386 // integer patterns!
1387 //
1388 // To figure out the fewest number of type checks needed, clone the pattern,
1389 // remove the types, then perform type inference on the pattern as a whole.
1390 // If there are unresolved types, emit an explicit check for those types,
1391 // apply the type to the tree, then rerun type inference. Iterate until all
1392 // types are resolved.
1393 //
1394 TreePatternNode *Pat = Pattern.getSrcPattern()->clone();
1395 RemoveAllTypes(Pat);
1396
1397 do {
1398 // Resolve/propagate as many types as possible.
1399 try {
1400 bool MadeChange = true;
1401 while (MadeChange)
1402 MadeChange = Pat->ApplyTypeConstraints(TP,
1403 true/*Ignore reg constraints*/);
1404 } catch (...) {
1405 assert(0 && "Error: could not find consistent types for something we"
1406 " already decided was ok!");
1407 abort();
1408 }
1409
1410 // Insert a check for an unresolved type and add it to the tree. If we find
1411 // an unresolved type to add a check for, this returns true and we iterate,
1412 // otherwise we are done.
1413 } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true));
1414
Evan Cheng775baac2007-09-12 23:30:14 +00001415 Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(),
Evan Chengdb1f2462007-09-17 22:26:41 +00001416 false, false, false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 delete Pat;
1418}
1419
1420/// EraseCodeLine - Erase one code line from all of the patterns. If removing
1421/// a line causes any of them to be empty, remove them and return true when
1422/// done.
Chris Lattner81915752008-01-05 22:30:17 +00001423static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001424 std::vector<std::pair<unsigned, std::string> > > >
1425 &Patterns) {
1426 bool ErasedPatterns = false;
1427 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1428 Patterns[i].second.pop_back();
1429 if (Patterns[i].second.empty()) {
1430 Patterns.erase(Patterns.begin()+i);
1431 --i; --e;
1432 ErasedPatterns = true;
1433 }
1434 }
1435 return ErasedPatterns;
1436}
1437
1438/// EmitPatterns - Emit code for at least one pattern, but try to group common
1439/// code together between the patterns.
Chris Lattner81915752008-01-05 22:30:17 +00001440void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001441 std::vector<std::pair<unsigned, std::string> > > >
1442 &Patterns, unsigned Indent,
1443 std::ostream &OS) {
1444 typedef std::pair<unsigned, std::string> CodeLine;
1445 typedef std::vector<CodeLine> CodeList;
Chris Lattner81915752008-01-05 22:30:17 +00001446 typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001447
1448 if (Patterns.empty()) return;
1449
1450 // Figure out how many patterns share the next code line. Explicitly copy
1451 // FirstCodeLine so that we don't invalidate a reference when changing
1452 // Patterns.
1453 const CodeLine FirstCodeLine = Patterns.back().second.back();
1454 unsigned LastMatch = Patterns.size()-1;
1455 while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine)
1456 --LastMatch;
1457
1458 // If not all patterns share this line, split the list into two pieces. The
1459 // first chunk will use this line, the second chunk won't.
1460 if (LastMatch != 0) {
1461 PatternList Shared(Patterns.begin()+LastMatch, Patterns.end());
1462 PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch);
1463
1464 // FIXME: Emit braces?
1465 if (Shared.size() == 1) {
Chris Lattner81915752008-01-05 22:30:17 +00001466 const PatternToMatch &Pattern = *Shared.back().first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001467 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1468 Pattern.getSrcPattern()->print(OS);
1469 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1470 Pattern.getDstPattern()->print(OS);
1471 OS << "\n";
1472 unsigned AddedComplexity = Pattern.getAddedComplexity();
1473 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001474 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 << " cost = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001476 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001477 << " size = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001478 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001479 }
1480 if (FirstCodeLine.first != 1) {
1481 OS << std::string(Indent, ' ') << "{\n";
1482 Indent += 2;
1483 }
1484 EmitPatterns(Shared, Indent, OS);
1485 if (FirstCodeLine.first != 1) {
1486 Indent -= 2;
1487 OS << std::string(Indent, ' ') << "}\n";
1488 }
1489
1490 if (Other.size() == 1) {
Chris Lattner81915752008-01-05 22:30:17 +00001491 const PatternToMatch &Pattern = *Other.back().first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 OS << "\n" << std::string(Indent, ' ') << "// Pattern: ";
1493 Pattern.getSrcPattern()->print(OS);
1494 OS << "\n" << std::string(Indent, ' ') << "// Emits: ";
1495 Pattern.getDstPattern()->print(OS);
1496 OS << "\n";
1497 unsigned AddedComplexity = Pattern.getAddedComplexity();
1498 OS << std::string(Indent, ' ') << "// Pattern complexity = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001499 << getPatternSize(Pattern.getSrcPattern(), CGP) + AddedComplexity
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001500 << " cost = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001501 << getResultPatternCost(Pattern.getDstPattern(), CGP)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001502 << " size = "
Chris Lattner14948ea2008-01-05 22:58:54 +00001503 << getResultPatternSize(Pattern.getDstPattern(), CGP) << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001504 }
1505 EmitPatterns(Other, Indent, OS);
1506 return;
1507 }
1508
1509 // Remove this code from all of the patterns that share it.
1510 bool ErasedPatterns = EraseCodeLine(Patterns);
1511
1512 bool isPredicate = FirstCodeLine.first == 1;
1513
1514 // Otherwise, every pattern in the list has this line. Emit it.
1515 if (!isPredicate) {
1516 // Normal code.
1517 OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n";
1518 } else {
1519 OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second;
1520
1521 // If the next code line is another predicate, and if all of the pattern
1522 // in this group share the same next line, emit it inline now. Do this
1523 // until we run out of common predicates.
1524 while (!ErasedPatterns && Patterns.back().second.back().first == 1) {
1525 // Check that all of fhe patterns in Patterns end with the same predicate.
1526 bool AllEndWithSamePredicate = true;
1527 for (unsigned i = 0, e = Patterns.size(); i != e; ++i)
1528 if (Patterns[i].second.back() != Patterns.back().second.back()) {
1529 AllEndWithSamePredicate = false;
1530 break;
1531 }
1532 // If all of the predicates aren't the same, we can't share them.
1533 if (!AllEndWithSamePredicate) break;
1534
1535 // Otherwise we can. Emit it shared now.
1536 OS << " &&\n" << std::string(Indent+4, ' ')
1537 << Patterns.back().second.back().second;
1538 ErasedPatterns = EraseCodeLine(Patterns);
1539 }
1540
1541 OS << ") {\n";
1542 Indent += 2;
1543 }
1544
1545 EmitPatterns(Patterns, Indent, OS);
1546
1547 if (isPredicate)
1548 OS << std::string(Indent-2, ' ') << "}\n";
1549}
1550
Chris Lattnerae506702008-01-06 01:10:31 +00001551static std::string getOpcodeName(Record *Op, CodeGenDAGPatterns &CGP) {
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001552 return CGP.getSDNodeInfo(Op).getEnumName();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553}
1554
1555static std::string getLegalCName(std::string OpName) {
1556 std::string::size_type pos = OpName.find("::");
1557 if (pos != std::string::npos)
1558 OpName.replace(pos, 2, "_");
1559 return OpName;
1560}
1561
1562void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) {
Chris Lattner14948ea2008-01-05 22:58:54 +00001563 const CodeGenTarget &Target = CGP.getTargetInfo();
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001564
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001565 // Get the namespace to insert instructions into. Make sure not to pick up
1566 // "TargetInstrInfo" by accidentally getting the namespace off the PHI
1567 // instruction or something.
1568 std::string InstNS;
1569 for (CodeGenTarget::inst_iterator i = Target.inst_begin(),
1570 e = Target.inst_end(); i != e; ++i) {
1571 InstNS = i->second.Namespace;
1572 if (InstNS != "TargetInstrInfo")
1573 break;
1574 }
1575
1576 if (!InstNS.empty()) InstNS += "::";
1577
1578 // Group the patterns by their top-level opcodes.
Chris Lattner81915752008-01-05 22:30:17 +00001579 std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001580 // All unique target node emission functions.
1581 std::map<std::string, unsigned> EmitFunctions;
Chris Lattnerae506702008-01-06 01:10:31 +00001582 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
Chris Lattner14948ea2008-01-05 22:58:54 +00001583 E = CGP.ptm_end(); I != E; ++I) {
Chris Lattner81915752008-01-05 22:30:17 +00001584 const PatternToMatch &Pattern = *I;
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001585
1586 TreePatternNode *Node = Pattern.getSrcPattern();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001587 if (!Node->isLeaf()) {
Chris Lattner14948ea2008-01-05 22:58:54 +00001588 PatternsByOpcode[getOpcodeName(Node->getOperator(), CGP)].
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001589 push_back(&Pattern);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001590 } else {
1591 const ComplexPattern *CP;
1592 if (dynamic_cast<IntInit*>(Node->getLeafValue())) {
Chris Lattner14948ea2008-01-05 22:58:54 +00001593 PatternsByOpcode[getOpcodeName(CGP.getSDNodeNamed("imm"), CGP)].
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001594 push_back(&Pattern);
Chris Lattner14948ea2008-01-05 22:58:54 +00001595 } else if ((CP = NodeGetComplexPattern(Node, CGP))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 std::vector<Record*> OpNodes = CP->getRootNodes();
1597 for (unsigned j = 0, e = OpNodes.size(); j != e; j++) {
Chris Lattner14948ea2008-01-05 22:58:54 +00001598 PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)]
1599 .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], CGP)].begin(),
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001600 &Pattern);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001601 }
1602 } else {
1603 cerr << "Unrecognized opcode '";
1604 Node->dump();
1605 cerr << "' on tree pattern '";
Chris Lattner4ca8ff02008-01-05 22:25:12 +00001606 cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 exit(1);
1608 }
1609 }
1610 }
1611
1612 // For each opcode, there might be multiple select functions, one per
1613 // ValueType of the node (or its first operand if it doesn't produce a
1614 // non-chain result.
1615 std::map<std::string, std::vector<std::string> > OpcodeVTMap;
1616
1617 // Emit one Select_* method for each top-level opcode. We do this instead of
1618 // emitting one giant switch statement to support compilers where this will
1619 // result in the recursive functions taking less stack space.
Chris Lattner81915752008-01-05 22:30:17 +00001620 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001621 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1622 PBOI != E; ++PBOI) {
1623 const std::string &OpName = PBOI->first;
Chris Lattner81915752008-01-05 22:30:17 +00001624 std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001625 assert(!PatternsOfOp.empty() && "No patterns but map has entry?");
1626
1627 // We want to emit all of the matching code now. However, we want to emit
1628 // the matches in order of minimal cost. Sort the patterns so the least
1629 // cost one is at the start.
1630 std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(),
Chris Lattner14948ea2008-01-05 22:58:54 +00001631 PatternSortingPredicate(CGP));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001632
1633 // Split them into groups by type.
Duncan Sands92c43912008-06-06 12:08:01 +00001634 std::map<MVT::SimpleValueType,
1635 std::vector<const PatternToMatch*> > PatternsByType;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) {
Chris Lattner81915752008-01-05 22:30:17 +00001637 const PatternToMatch *Pat = PatternsOfOp[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 TreePatternNode *SrcPat = Pat->getSrcPattern();
Duncan Sands92c43912008-06-06 12:08:01 +00001639 MVT::SimpleValueType VT = SrcPat->getTypeNum(0);
1640 std::map<MVT::SimpleValueType,
Chris Lattner81915752008-01-05 22:30:17 +00001641 std::vector<const PatternToMatch*> >::iterator TI =
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001642 PatternsByType.find(VT);
1643 if (TI != PatternsByType.end())
1644 TI->second.push_back(Pat);
1645 else {
Chris Lattner81915752008-01-05 22:30:17 +00001646 std::vector<const PatternToMatch*> PVec;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001647 PVec.push_back(Pat);
1648 PatternsByType.insert(std::make_pair(VT, PVec));
1649 }
1650 }
1651
Duncan Sands92c43912008-06-06 12:08:01 +00001652 for (std::map<MVT::SimpleValueType,
1653 std::vector<const PatternToMatch*> >::iterator
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001654 II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE;
1655 ++II) {
Duncan Sands92c43912008-06-06 12:08:01 +00001656 MVT::SimpleValueType OpVT = II->first;
Chris Lattner81915752008-01-05 22:30:17 +00001657 std::vector<const PatternToMatch*> &Patterns = II->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 typedef std::vector<std::pair<unsigned,std::string> > CodeList;
1659 typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI;
1660
Chris Lattner81915752008-01-05 22:30:17 +00001661 std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001662 std::vector<std::vector<std::string> > PatternOpcodes;
1663 std::vector<std::vector<std::string> > PatternVTs;
1664 std::vector<std::set<std::string> > PatternDecls;
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001665 std::vector<bool> OutputIsVariadicFlags;
1666 std::vector<unsigned> NumInputRootOpsCounts;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
1668 CodeList GeneratedCode;
1669 std::set<std::string> GeneratedDecl;
1670 std::vector<std::string> TargetOpcodes;
1671 std::vector<std::string> TargetVTs;
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001672 bool OutputIsVariadic;
1673 unsigned NumInputRootOps;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl,
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001675 TargetOpcodes, TargetVTs,
1676 OutputIsVariadic, NumInputRootOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001677 CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode));
1678 PatternDecls.push_back(GeneratedDecl);
1679 PatternOpcodes.push_back(TargetOpcodes);
1680 PatternVTs.push_back(TargetVTs);
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001681 OutputIsVariadicFlags.push_back(OutputIsVariadic);
1682 NumInputRootOpsCounts.push_back(NumInputRootOps);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001683 }
1684
1685 // Scan the code to see if all of the patterns are reachable and if it is
1686 // possible that the last one might not match.
1687 bool mightNotMatch = true;
1688 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1689 CodeList &GeneratedCode = CodeForPatterns[i].second;
1690 mightNotMatch = false;
1691
1692 for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) {
1693 if (GeneratedCode[j].first == 1) { // predicate.
1694 mightNotMatch = true;
1695 break;
1696 }
1697 }
1698
1699 // If this pattern definitely matches, and if it isn't the last one, the
1700 // patterns after it CANNOT ever match. Error out.
1701 if (mightNotMatch == false && i != CodeForPatterns.size()-1) {
1702 cerr << "Pattern '";
1703 CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream());
1704 cerr << "' is impossible to select!\n";
1705 exit(1);
1706 }
1707 }
1708
1709 // Factor target node emission code (emitted by EmitResultCode) into
1710 // separate functions. Uniquing and share them among all instruction
1711 // selection routines.
1712 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1713 CodeList &GeneratedCode = CodeForPatterns[i].second;
1714 std::vector<std::string> &TargetOpcodes = PatternOpcodes[i];
1715 std::vector<std::string> &TargetVTs = PatternVTs[i];
1716 std::set<std::string> Decls = PatternDecls[i];
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001717 bool OutputIsVariadic = OutputIsVariadicFlags[i];
1718 unsigned NumInputRootOps = NumInputRootOpsCounts[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001719 std::vector<std::string> AddedInits;
1720 int CodeSize = (int)GeneratedCode.size();
1721 int LastPred = -1;
1722 for (int j = CodeSize-1; j >= 0; --j) {
1723 if (LastPred == -1 && GeneratedCode[j].first == 1)
1724 LastPred = j;
1725 else if (LastPred != -1 && GeneratedCode[j].first == 2)
1726 AddedInits.push_back(GeneratedCode[j].second);
1727 }
1728
1729 std::string CalleeCode = "(const SDOperand &N";
1730 std::string CallerCode = "(N";
1731 for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) {
1732 CalleeCode += ", unsigned Opc" + utostr(j);
1733 CallerCode += ", " + TargetOpcodes[j];
1734 }
1735 for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) {
Duncan Sands92c43912008-06-06 12:08:01 +00001736 CalleeCode += ", MVT VT" + utostr(j);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001737 CallerCode += ", " + TargetVTs[j];
1738 }
1739 for (std::set<std::string>::iterator
1740 I = Decls.begin(), E = Decls.end(); I != E; ++I) {
1741 std::string Name = *I;
1742 CalleeCode += ", SDOperand &" + Name;
1743 CallerCode += ", " + Name;
1744 }
Dan Gohman2c4be2a2008-05-31 02:11:25 +00001745
1746 if (OutputIsVariadic) {
1747 CalleeCode += ", unsigned NumInputRootOps";
1748 CallerCode += ", " + utostr(NumInputRootOps);
1749 }
1750
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001751 CallerCode += ");";
1752 CalleeCode += ") ";
1753 // Prevent emission routines from being inlined to reduce selection
1754 // routines stack frame sizes.
1755 CalleeCode += "DISABLE_INLINE ";
1756 CalleeCode += "{\n";
1757
1758 for (std::vector<std::string>::const_reverse_iterator
1759 I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I)
1760 CalleeCode += " " + *I + "\n";
1761
1762 for (int j = LastPred+1; j < CodeSize; ++j)
1763 CalleeCode += " " + GeneratedCode[j].second + "\n";
1764 for (int j = LastPred+1; j < CodeSize; ++j)
1765 GeneratedCode.pop_back();
1766 CalleeCode += "}\n";
1767
1768 // Uniquing the emission routines.
1769 unsigned EmitFuncNum;
1770 std::map<std::string, unsigned>::iterator EFI =
1771 EmitFunctions.find(CalleeCode);
1772 if (EFI != EmitFunctions.end()) {
1773 EmitFuncNum = EFI->second;
1774 } else {
1775 EmitFuncNum = EmitFunctions.size();
1776 EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum));
1777 OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode;
1778 }
1779
1780 // Replace the emission code within selection routines with calls to the
1781 // emission functions.
1782 CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode;
1783 GeneratedCode.push_back(std::make_pair(false, CallerCode));
1784 }
1785
1786 // Print function.
1787 std::string OpVTStr;
1788 if (OpVT == MVT::iPTR) {
1789 OpVTStr = "_iPTR";
1790 } else if (OpVT == MVT::isVoid) {
1791 // Nodes with a void result actually have a first result type of either
1792 // Other (a chain) or Flag. Since there is no one-to-one mapping from
1793 // void to this case, we handle it specially here.
1794 } else {
1795 OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::'
1796 }
1797 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1798 OpcodeVTMap.find(OpName);
1799 if (OpVTI == OpcodeVTMap.end()) {
1800 std::vector<std::string> VTSet;
1801 VTSet.push_back(OpVTStr);
1802 OpcodeVTMap.insert(std::make_pair(OpName, VTSet));
1803 } else
1804 OpVTI->second.push_back(OpVTStr);
1805
1806 OS << "SDNode *Select_" << getLegalCName(OpName)
1807 << OpVTStr << "(const SDOperand &N) {\n";
1808
1809 // Loop through and reverse all of the CodeList vectors, as we will be
1810 // accessing them from their logical front, but accessing the end of a
1811 // vector is more efficient.
1812 for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) {
1813 CodeList &GeneratedCode = CodeForPatterns[i].second;
1814 std::reverse(GeneratedCode.begin(), GeneratedCode.end());
1815 }
1816
1817 // Next, reverse the list of patterns itself for the same reason.
1818 std::reverse(CodeForPatterns.begin(), CodeForPatterns.end());
1819
1820 // Emit all of the patterns now, grouped together to share code.
1821 EmitPatterns(CodeForPatterns, 2, OS);
1822
1823 // If the last pattern has predicates (which could fail) emit code to
1824 // catch the case where nothing handles a pattern.
1825 if (mightNotMatch) {
1826 OS << " cerr << \"Cannot yet select: \";\n";
1827 if (OpName != "ISD::INTRINSIC_W_CHAIN" &&
1828 OpName != "ISD::INTRINSIC_WO_CHAIN" &&
1829 OpName != "ISD::INTRINSIC_VOID") {
1830 OS << " N.Val->dump(CurDAG);\n";
1831 } else {
1832 OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
1833 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
1834 << " cerr << \"intrinsic %\"<< "
1835 "Intrinsic::getName((Intrinsic::ID)iid);\n";
1836 }
1837 OS << " cerr << '\\n';\n"
1838 << " abort();\n"
1839 << " return NULL;\n";
1840 }
1841 OS << "}\n\n";
1842 }
1843 }
1844
1845 // Emit boilerplate.
1846 OS << "SDNode *Select_INLINEASM(SDOperand N) {\n"
1847 << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n"
1848 << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n"
1849
1850 << " // Ensure that the asm operands are themselves selected.\n"
1851 << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n"
1852 << " AddToISelQueue(Ops[j]);\n\n"
1853
Duncan Sands92c43912008-06-06 12:08:01 +00001854 << " std::vector<MVT> VTs;\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001855 << " VTs.push_back(MVT::Other);\n"
1856 << " VTs.push_back(MVT::Flag);\n"
1857 << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], "
1858 "Ops.size());\n"
1859 << " return New.Val;\n"
1860 << "}\n\n";
Evan Cheng3c0eda52008-03-15 00:03:38 +00001861
1862 OS << "SDNode *Select_UNDEF(const SDOperand &N) {\n"
1863 << " return CurDAG->getTargetNode(TargetInstrInfo::IMPLICIT_DEF,\n"
1864 << " N.getValueType());\n"
1865 << "}\n\n";
1866
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001867 OS << "SDNode *Select_LABEL(const SDOperand &N) {\n"
1868 << " SDOperand Chain = N.getOperand(0);\n"
1869 << " SDOperand N1 = N.getOperand(1);\n"
Evan Cheng13d1c292008-01-31 09:59:15 +00001870 << " SDOperand N2 = N.getOperand(2);\n"
1871 << " unsigned C1 = cast<ConstantSDNode>(N1)->getValue();\n"
1872 << " unsigned C2 = cast<ConstantSDNode>(N2)->getValue();\n"
1873 << " SDOperand Tmp1 = CurDAG->getTargetConstant(C1, MVT::i32);\n"
1874 << " SDOperand Tmp2 = CurDAG->getTargetConstant(C2, MVT::i32);\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001875 << " AddToISelQueue(Chain);\n"
Evan Cheng13d1c292008-01-31 09:59:15 +00001876 << " SDOperand Ops[] = { Tmp1, Tmp2, Chain };\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001877 << " return CurDAG->getTargetNode(TargetInstrInfo::LABEL,\n"
Evan Cheng13d1c292008-01-31 09:59:15 +00001878 << " MVT::Other, Ops, 3);\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001879 << "}\n\n";
1880
Evan Cheng2e28d622008-02-02 04:07:54 +00001881 OS << "SDNode *Select_DECLARE(const SDOperand &N) {\n"
Evan Cheng2e28d622008-02-02 04:07:54 +00001882 << " SDOperand Chain = N.getOperand(0);\n"
1883 << " SDOperand N1 = N.getOperand(1);\n"
1884 << " SDOperand N2 = N.getOperand(2);\n"
1885 << " if (!isa<FrameIndexSDNode>(N1) || !isa<GlobalAddressSDNode>(N2)) {\n"
1886 << " cerr << \"Cannot yet select llvm.dbg.declare: \";\n"
1887 << " N.Val->dump(CurDAG);\n"
1888 << " abort();\n"
1889 << " }\n"
1890 << " int FI = cast<FrameIndexSDNode>(N1)->getIndex();\n"
1891 << " GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();\n"
Evan Cheng2e28d622008-02-02 04:07:54 +00001892 << " SDOperand Tmp1 = "
1893 << "CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());\n"
1894 << " SDOperand Tmp2 = "
1895 << "CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());\n"
1896 << " AddToISelQueue(Chain);\n"
1897 << " SDOperand Ops[] = { Tmp1, Tmp2, Chain };\n"
1898 << " return CurDAG->getTargetNode(TargetInstrInfo::DECLARE,\n"
1899 << " MVT::Other, Ops, 3);\n"
1900 << "}\n\n";
1901
Christopher Lamb071a2a72007-07-26 07:48:21 +00001902 OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n"
1903 << " SDOperand N0 = N.getOperand(0);\n"
1904 << " SDOperand N1 = N.getOperand(1);\n"
1905 << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n"
1906 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1907 << " AddToISelQueue(N0);\n"
Chris Lattner56af5402007-10-24 06:25:09 +00001908 << " SDOperand Ops[] = { N0, Tmp };\n"
Christopher Lamb071a2a72007-07-26 07:48:21 +00001909 << " return CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,\n"
Chris Lattner56af5402007-10-24 06:25:09 +00001910 << " N.getValueType(), Ops, 2);\n"
Christopher Lamb071a2a72007-07-26 07:48:21 +00001911 << "}\n\n";
1912
1913 OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n"
1914 << " SDOperand N0 = N.getOperand(0);\n"
1915 << " SDOperand N1 = N.getOperand(1);\n"
1916 << " SDOperand N2 = N.getOperand(2);\n"
1917 << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n"
1918 << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n"
1919 << " AddToISelQueue(N1);\n"
Chris Lattner56af5402007-10-24 06:25:09 +00001920 << " SDOperand Ops[] = { N0, N1, Tmp };\n"
Christopher Lambb371e032008-03-13 05:47:01 +00001921 << " AddToISelQueue(N0);\n"
1922 << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n"
1923 << " N.getValueType(), Ops, 3);\n"
Christopher Lamb071a2a72007-07-26 07:48:21 +00001924 << "}\n\n";
1925
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001926 OS << "// The main instruction selector code.\n"
1927 << "SDNode *SelectCode(SDOperand N) {\n"
1928 << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n"
1929 << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS
1930 << "INSTRUCTION_LIST_END)) {\n"
1931 << " return NULL; // Already selected.\n"
1932 << " }\n\n"
Duncan Sands92c43912008-06-06 12:08:01 +00001933 << " MVT::SimpleValueType NVT = N.Val->getValueType(0).getSimpleVT();\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001934 << " switch (N.getOpcode()) {\n"
1935 << " default: break;\n"
1936 << " case ISD::EntryToken: // These leaves remain the same.\n"
1937 << " case ISD::BasicBlock:\n"
1938 << " case ISD::Register:\n"
1939 << " case ISD::HANDLENODE:\n"
1940 << " case ISD::TargetConstant:\n"
Nate Begemane2ba64f2008-02-14 08:57:00 +00001941 << " case ISD::TargetConstantFP:\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001942 << " case ISD::TargetConstantPool:\n"
1943 << " case ISD::TargetFrameIndex:\n"
1944 << " case ISD::TargetExternalSymbol:\n"
1945 << " case ISD::TargetJumpTable:\n"
1946 << " case ISD::TargetGlobalTLSAddress:\n"
1947 << " case ISD::TargetGlobalAddress: {\n"
1948 << " return NULL;\n"
1949 << " }\n"
1950 << " case ISD::AssertSext:\n"
1951 << " case ISD::AssertZext: {\n"
1952 << " AddToISelQueue(N.getOperand(0));\n"
1953 << " ReplaceUses(N, N.getOperand(0));\n"
1954 << " return NULL;\n"
1955 << " }\n"
1956 << " case ISD::TokenFactor:\n"
1957 << " case ISD::CopyFromReg:\n"
1958 << " case ISD::CopyToReg: {\n"
1959 << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n"
1960 << " AddToISelQueue(N.getOperand(i));\n"
1961 << " return NULL;\n"
1962 << " }\n"
1963 << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"
Christopher Lamb071a2a72007-07-26 07:48:21 +00001964 << " case ISD::LABEL: return Select_LABEL(N);\n"
Evan Cheng2e28d622008-02-02 04:07:54 +00001965 << " case ISD::DECLARE: return Select_DECLARE(N);\n"
Christopher Lamb071a2a72007-07-26 07:48:21 +00001966 << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n"
Evan Cheng3c0eda52008-03-15 00:03:38 +00001967 << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"
1968 << " case ISD::UNDEF: return Select_UNDEF(N);\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001969
1970
1971 // Loop over all of the case statements, emiting a call to each method we
1972 // emitted above.
Chris Lattner81915752008-01-05 22:30:17 +00001973 for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001974 PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end();
1975 PBOI != E; ++PBOI) {
1976 const std::string &OpName = PBOI->first;
1977 // Potentially multiple versions of select for this opcode. One for each
1978 // ValueType of the node (or its first true operand if it doesn't produce a
1979 // result.
1980 std::map<std::string, std::vector<std::string> >::iterator OpVTI =
1981 OpcodeVTMap.find(OpName);
1982 std::vector<std::string> &OpVTs = OpVTI->second;
1983 OS << " case " << OpName << ": {\n";
Evan Chengb8b6b182007-09-04 20:18:28 +00001984 // Keep track of whether we see a pattern that has an iPtr result.
1985 bool HasPtrPattern = false;
1986 bool HasDefaultPattern = false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001987
Evan Chengb8b6b182007-09-04 20:18:28 +00001988 OS << " switch (NVT) {\n";
1989 for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) {
1990 std::string &VTStr = OpVTs[i];
1991 if (VTStr.empty()) {
1992 HasDefaultPattern = true;
1993 continue;
1994 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001995
Evan Chengb8b6b182007-09-04 20:18:28 +00001996 // If this is a match on iPTR: don't emit it directly, we need special
1997 // code.
1998 if (VTStr == "_iPTR") {
1999 HasPtrPattern = true;
2000 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002001 }
Evan Chengb8b6b182007-09-04 20:18:28 +00002002 OS << " case MVT::" << VTStr.substr(1) << ":\n"
2003 << " return Select_" << getLegalCName(OpName)
2004 << VTStr << "(N);\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002005 }
Evan Chengb8b6b182007-09-04 20:18:28 +00002006 OS << " default:\n";
2007
2008 // If there is an iPTR result version of this pattern, emit it here.
2009 if (HasPtrPattern) {
Duncan Sands92c43912008-06-06 12:08:01 +00002010 OS << " if (TLI.getPointerTy() == NVT)\n";
Evan Chengb8b6b182007-09-04 20:18:28 +00002011 OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n";
2012 }
2013 if (HasDefaultPattern) {
2014 OS << " return Select_" << getLegalCName(OpName) << "(N);\n";
2015 }
2016 OS << " break;\n";
2017 OS << " }\n";
2018 OS << " break;\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002019 OS << " }\n";
2020 }
2021
2022 OS << " } // end of big switch.\n\n"
2023 << " cerr << \"Cannot yet select: \";\n"
2024 << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n"
2025 << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n"
2026 << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n"
2027 << " N.Val->dump(CurDAG);\n"
2028 << " } else {\n"
2029 << " unsigned iid = cast<ConstantSDNode>(N.getOperand("
2030 "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n"
2031 << " cerr << \"intrinsic %\"<< "
2032 "Intrinsic::getName((Intrinsic::ID)iid);\n"
2033 << " }\n"
2034 << " cerr << '\\n';\n"
2035 << " abort();\n"
2036 << " return NULL;\n"
2037 << "}\n";
2038}
2039
2040void DAGISelEmitter::run(std::ostream &OS) {
Chris Lattner14948ea2008-01-05 22:58:54 +00002041 EmitSourceFileHeader("DAG Instruction Selector for the " +
2042 CGP.getTargetInfo().getName() + " target", OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002043
2044 OS << "// *** NOTE: This file is #included into the middle of the target\n"
2045 << "// *** instruction selector class. These functions are really "
2046 << "methods.\n\n";
Chris Lattner7bcb18f2008-02-03 06:49:24 +00002047
Roman Levenstein393ad0f2008-05-14 10:17:11 +00002048 OS << "// Include standard, target-independent definitions and methods used\n"
2049 << "// by the instruction selector.\n";
2050 OS << "#include <llvm/CodeGen/DAGISelHeader.h>\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002051
Chris Lattner227da452008-01-05 22:54:53 +00002052 EmitNodeTransforms(OS);
Chris Lattner7fdd9342008-01-05 22:43:57 +00002053 EmitPredicateFunctions(OS);
2054
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002055 DOUT << "\n\nALL PATTERNS TO MATCH:\n\n";
Chris Lattnerae506702008-01-06 01:10:31 +00002056 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
Chris Lattner4ca8ff02008-01-05 22:25:12 +00002057 I != E; ++I) {
2058 DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump());
2059 DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002060 DOUT << "\n";
2061 }
2062
2063 // At this point, we have full information about the 'Patterns' we need to
2064 // parse, both implicitly from instructions as well as from explicit pattern
2065 // definitions. Emit the resultant instruction selector.
2066 EmitInstructionSelector(OS);
2067
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002068}