blob: 03a12cd13d90261d847fe4568de2ba3a68de855c [file] [log] [blame]
Chris Lattner54cb8fd2005-09-07 23:44:43 +00001//===- DAGISelEmitter.cpp - Generate an instruction selector --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner54cb8fd2005-09-07 23:44:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DAGISelEmitter.h"
Chris Lattnerda272d12010-02-15 08:04:42 +000015#include "DAGISelMatcher.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000016#include "Record.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000017#include "llvm/Support/Debug.h"
Chris Lattner54cb8fd2005-09-07 23:44:43 +000018using namespace llvm;
19
Chris Lattnerca559d02005-09-08 21:03:01 +000020//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000021// DAGISelEmitter Helper methods
Chris Lattner54cb8fd2005-09-07 23:44:43 +000022//
23
Chris Lattner05814af2005-09-28 17:57:56 +000024/// getPatternSize - Return the 'size' of this pattern. We want to match large
25/// patterns before small ones. This is used to determine the size of a
26/// pattern.
Chris Lattnerfe718932008-01-06 01:10:31 +000027static unsigned getPatternSize(TreePatternNode *P, CodeGenDAGPatterns &CGP) {
Owen Andersone50ed302009-08-10 22:56:29 +000028 assert((EEVT::isExtIntegerInVTs(P->getExtTypes()) ||
29 EEVT::isExtFloatingPointInVTs(P->getExtTypes()) ||
Owen Anderson825b72b2009-08-11 20:47:22 +000030 P->getExtTypeNum(0) == MVT::isVoid ||
31 P->getExtTypeNum(0) == MVT::Flag ||
32 P->getExtTypeNum(0) == MVT::iPTR ||
33 P->getExtTypeNum(0) == MVT::iPTRAny) &&
Evan Cheng4a7c2842006-01-06 22:19:44 +000034 "Not a valid pattern node to size!");
Evan Cheng6cec34e2006-09-08 07:26:39 +000035 unsigned Size = 3; // The node itself.
Evan Cheng657416c2006-02-01 06:06:31 +000036 // If the root node is a ConstantSDNode, increases its size.
37 // e.g. (set R32:$dst, 0).
38 if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000039 Size += 2;
Evan Cheng0fc71982005-12-08 02:00:36 +000040
41 // FIXME: This is a hack to statically increase the priority of patterns
42 // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD.
43 // Later we can allow complexity / cost for each pattern to be (optionally)
44 // specified. To get best possible pattern match we'll need to dynamically
45 // calculate the complexity of all patterns a dag can potentially map to.
Chris Lattner47661322010-02-14 22:22:58 +000046 const ComplexPattern *AM = P->getComplexPatternInfo(CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000047 if (AM)
Evan Cheng6cec34e2006-09-08 07:26:39 +000048 Size += AM->getNumOperands() * 3;
Chris Lattner3e179802006-02-03 18:06:02 +000049
50 // If this node has some predicate function that must match, it adds to the
51 // complexity of this node.
Dan Gohman0540e172008-10-15 06:17:21 +000052 if (!P->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000053 ++Size;
54
Chris Lattner05814af2005-09-28 17:57:56 +000055 // Count children in the count if they are also nodes.
56 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) {
57 TreePatternNode *Child = P->getChild(i);
Owen Anderson825b72b2009-08-11 20:47:22 +000058 if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other)
Chris Lattner6cefb772008-01-05 22:25:12 +000059 Size += getPatternSize(Child, CGP);
Evan Cheng0fc71982005-12-08 02:00:36 +000060 else if (Child->isLeaf()) {
61 if (dynamic_cast<IntInit*>(Child->getLeafValue()))
Evan Cheng6cec34e2006-09-08 07:26:39 +000062 Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2).
Chris Lattner05446e72010-02-16 23:13:59 +000063 else if (Child->getComplexPatternInfo(CGP))
Chris Lattner6cefb772008-01-05 22:25:12 +000064 Size += getPatternSize(Child, CGP);
Dan Gohman0540e172008-10-15 06:17:21 +000065 else if (!Child->getPredicateFns().empty())
Chris Lattner3e179802006-02-03 18:06:02 +000066 ++Size;
Chris Lattner2f041d42005-10-19 04:41:05 +000067 }
Chris Lattner05814af2005-09-28 17:57:56 +000068 }
69
70 return Size;
71}
72
73/// getResultPatternCost - Compute the number of instructions for this pattern.
74/// This is a temporary hack. We should really include the instruction
75/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +000076static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000077 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +000078 if (P->isLeaf()) return 0;
79
Evan Chengfbad7082006-02-18 02:33:09 +000080 unsigned Cost = 0;
81 Record *Op = P->getOperator();
82 if (Op->isSubClassOf("Instruction")) {
83 Cost++;
Chris Lattner6cefb772008-01-05 22:25:12 +000084 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName());
Dan Gohman533297b2009-10-29 18:10:34 +000085 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +000086 Cost += 10;
87 }
Chris Lattner05814af2005-09-28 17:57:56 +000088 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +000089 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +000090 return Cost;
91}
92
Evan Chenge6f32032006-07-19 00:24:41 +000093/// getResultPatternCodeSize - Compute the code size of instructions for this
94/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +000095static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000096 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +000097 if (P->isLeaf()) return 0;
98
99 unsigned Cost = 0;
100 Record *Op = P->getOperator();
101 if (Op->isSubClassOf("Instruction")) {
102 Cost += Op->getValueAsInt("CodeSize");
103 }
104 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +0000105 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +0000106 return Cost;
107}
108
Chris Lattner443e3f92008-01-05 22:54:53 +0000109//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +0000110// Predicate emitter implementation.
111//
112
Daniel Dunbar1a551802009-07-03 00:10:29 +0000113void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
Chris Lattnerdc32f982008-01-05 22:43:57 +0000114 OS << "\n// Predicate functions.\n";
115
116 // Walk the pattern fragments, adding them to a map, which sorts them by
117 // name.
118 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
119 PFsByNameTy PFsByName;
120
Chris Lattnerfe718932008-01-06 01:10:31 +0000121 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000122 I != E; ++I)
123 PFsByName.insert(std::make_pair(I->first->getName(), *I));
124
125
126 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
127 I != E; ++I) {
128 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
129 TreePattern *P = I->second.second;
130
131 // If there is a code init for this fragment, emit the predicate code.
132 std::string Code = PatFragRecord->getValueAsCode("Predicate");
133 if (Code.empty()) continue;
134
135 if (P->getOnlyTree()->isLeaf())
136 OS << "inline bool Predicate_" << PatFragRecord->getName()
Chris Lattnerccba15f2010-02-16 07:26:36 +0000137 << "(SDNode *N) const {\n";
Chris Lattnerdc32f982008-01-05 22:43:57 +0000138 else {
139 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +0000140 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000141 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
142
143 OS << "inline bool Predicate_" << PatFragRecord->getName()
Chris Lattnerccba15f2010-02-16 07:26:36 +0000144 << "(SDNode *" << C2 << ") const {\n";
Chris Lattnerdc32f982008-01-05 22:43:57 +0000145 if (ClassName != "SDNode")
146 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
147 }
148 OS << Code << "\n}\n";
149 }
150
151 OS << "\n\n";
152}
153
Chris Lattner99ce6e82010-02-21 19:22:06 +0000154namespace {
155// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
156// In particular, we want to match maximal patterns first and lowest cost within
157// a particular complexity first.
Chris Lattneradc53472010-03-01 21:49:54 +0000158struct PatternSortingPredicate {
159 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
Chris Lattner99ce6e82010-02-21 19:22:06 +0000160 CodeGenDAGPatterns &CGP;
161
162 bool operator()(const PatternToMatch *LHS,
163 const PatternToMatch *RHS) {
164 unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP);
165 unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP);
166 LHSSize += LHS->getAddedComplexity();
167 RHSSize += RHS->getAddedComplexity();
168 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
169 if (LHSSize < RHSSize) return false;
170
171 // If the patterns have equal complexity, compare generated instruction cost
172 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
173 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
174 if (LHSCost < RHSCost) return true;
175 if (LHSCost > RHSCost) return false;
176
Chris Lattner117ccb72010-03-01 22:09:11 +0000177 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
178 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
179 if (LHSPatSize < RHSPatSize) return true;
180 if (LHSPatSize > RHSPatSize) return false;
181
182 // Sort based on the UID of the pattern, giving us a deterministic ordering.
183 assert(LHS->ID != RHS->ID);
184 return LHS->ID < RHS->ID;
Chris Lattner99ce6e82010-02-21 19:22:06 +0000185 }
186};
187}
188
189
Daniel Dunbar1a551802009-07-03 00:10:29 +0000190void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +0000191 EmitSourceFileHeader("DAG Instruction Selector for the " +
192 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000193
Chris Lattner1f39e292005-09-14 00:09:24 +0000194 OS << "// *** NOTE: This file is #included into the middle of the target\n"
195 << "// *** instruction selector class. These functions are really "
196 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000197
Roman Levenstein6422e8a2008-05-14 10:17:11 +0000198 OS << "// Include standard, target-independent definitions and methods used\n"
199 << "// by the instruction selector.\n";
Mike Stumpfe095f32009-05-04 18:40:41 +0000200 OS << "#include \"llvm/CodeGen/DAGISelHeader.h\"\n\n";
Chris Lattner296dfe32005-09-24 00:50:51 +0000201
Chris Lattner4d0c9312010-03-01 01:54:19 +0000202 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
203 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
204 E = CGP.ptm_end(); I != E; ++I) {
205 errs() << "PATTERN: "; I->getSrcPattern()->dump();
206 errs() << "\nRESULT: "; I->getDstPattern()->dump();
207 errs() << "\n";
208 });
209
210 // FIXME: These are being used by hand written code, gross.
Chris Lattnerdc32f982008-01-05 22:43:57 +0000211 EmitPredicateFunctions(OS);
Chris Lattner4d0c9312010-03-01 01:54:19 +0000212
Chris Lattner99ce6e82010-02-21 19:22:06 +0000213 // Add all the patterns to a temporary list so we can sort them.
214 std::vector<const PatternToMatch*> Patterns;
215 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
216 I != E; ++I)
217 Patterns.push_back(&*I);
218
219 // We want to process the matches in order of minimal cost. Sort the patterns
220 // so the least cost one is at the start.
Chris Lattner99ce6e82010-02-21 19:22:06 +0000221 std::stable_sort(Patterns.begin(), Patterns.end(),
Chris Lattneradc53472010-03-01 21:49:54 +0000222 PatternSortingPredicate(CGP));
Chris Lattner99ce6e82010-02-21 19:22:06 +0000223
224
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000225 // Convert each variant of each pattern into a Matcher.
Chris Lattnerd6c84722010-02-25 19:00:39 +0000226 std::vector<Matcher*> PatternMatchers;
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000227 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
228 for (unsigned Variant = 0; ; ++Variant) {
229 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
230 PatternMatchers.push_back(M);
231 else
232 break;
233 }
234 }
235
Chris Lattnerd6c84722010-02-25 19:00:39 +0000236
237 Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0],
238 PatternMatchers.size());
Chris Lattner05446e72010-02-16 23:13:59 +0000239
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000240 TheMatcher = OptimizeMatcher(TheMatcher, CGP);
Chris Lattnerda272d12010-02-15 08:04:42 +0000241 //Matcher->dump();
Chris Lattner4d0c9312010-03-01 01:54:19 +0000242 EmitMatcherTable(TheMatcher, CGP, OS);
Chris Lattnerb21ba712010-02-25 02:04:40 +0000243 delete TheMatcher;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000244}