blob: 9512d430ab689cb1eb2ab104cafe89ee802f6d41 [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/// getResultPatternCost - Compute the number of instructions for this pattern.
25/// This is a temporary hack. We should really include the instruction
26/// latencies in this calculation.
Chris Lattner6cefb772008-01-05 22:25:12 +000027static unsigned getResultPatternCost(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000028 CodeGenDAGPatterns &CGP) {
Chris Lattner05814af2005-09-28 17:57:56 +000029 if (P->isLeaf()) return 0;
30
Evan Chengfbad7082006-02-18 02:33:09 +000031 unsigned Cost = 0;
32 Record *Op = P->getOperator();
33 if (Op->isSubClassOf("Instruction")) {
34 Cost++;
Chris Lattnerf30187a2010-03-19 00:07:20 +000035 CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op);
Dan Gohman533297b2009-10-29 18:10:34 +000036 if (II.usesCustomInserter)
Evan Chengfbad7082006-02-18 02:33:09 +000037 Cost += 10;
38 }
Chris Lattner05814af2005-09-28 17:57:56 +000039 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +000040 Cost += getResultPatternCost(P->getChild(i), CGP);
Chris Lattner05814af2005-09-28 17:57:56 +000041 return Cost;
42}
43
Evan Chenge6f32032006-07-19 00:24:41 +000044/// getResultPatternCodeSize - Compute the code size of instructions for this
45/// pattern.
Chris Lattner6cefb772008-01-05 22:25:12 +000046static unsigned getResultPatternSize(TreePatternNode *P,
Chris Lattnerfe718932008-01-06 01:10:31 +000047 CodeGenDAGPatterns &CGP) {
Evan Chenge6f32032006-07-19 00:24:41 +000048 if (P->isLeaf()) return 0;
49
50 unsigned Cost = 0;
51 Record *Op = P->getOperator();
52 if (Op->isSubClassOf("Instruction")) {
53 Cost += Op->getValueAsInt("CodeSize");
54 }
55 for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i)
Chris Lattner6cefb772008-01-05 22:25:12 +000056 Cost += getResultPatternSize(P->getChild(i), CGP);
Evan Chenge6f32032006-07-19 00:24:41 +000057 return Cost;
58}
59
Chris Lattner443e3f92008-01-05 22:54:53 +000060//===----------------------------------------------------------------------===//
Chris Lattnerdc32f982008-01-05 22:43:57 +000061// Predicate emitter implementation.
62//
63
Daniel Dunbar1a551802009-07-03 00:10:29 +000064void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) {
Chris Lattnerdc32f982008-01-05 22:43:57 +000065 OS << "\n// Predicate functions.\n";
66
67 // Walk the pattern fragments, adding them to a map, which sorts them by
68 // name.
69 typedef std::map<std::string, std::pair<Record*, TreePattern*> > PFsByNameTy;
70 PFsByNameTy PFsByName;
71
Chris Lattnerfe718932008-01-06 01:10:31 +000072 for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end();
Chris Lattnerdc32f982008-01-05 22:43:57 +000073 I != E; ++I)
74 PFsByName.insert(std::make_pair(I->first->getName(), *I));
75
76
77 for (PFsByNameTy::iterator I = PFsByName.begin(), E = PFsByName.end();
78 I != E; ++I) {
79 Record *PatFragRecord = I->second.first;// Record that derives from PatFrag.
80 TreePattern *P = I->second.second;
81
82 // If there is a code init for this fragment, emit the predicate code.
83 std::string Code = PatFragRecord->getValueAsCode("Predicate");
84 if (Code.empty()) continue;
85
86 if (P->getOnlyTree()->isLeaf())
87 OS << "inline bool Predicate_" << PatFragRecord->getName()
Chris Lattnerccba15f2010-02-16 07:26:36 +000088 << "(SDNode *N) const {\n";
Chris Lattnerdc32f982008-01-05 22:43:57 +000089 else {
90 std::string ClassName =
Chris Lattner200c57e2008-01-05 22:58:54 +000091 CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName();
Chris Lattnerdc32f982008-01-05 22:43:57 +000092 const char *C2 = ClassName == "SDNode" ? "N" : "inN";
93
94 OS << "inline bool Predicate_" << PatFragRecord->getName()
Chris Lattnerccba15f2010-02-16 07:26:36 +000095 << "(SDNode *" << C2 << ") const {\n";
Chris Lattnerdc32f982008-01-05 22:43:57 +000096 if (ClassName != "SDNode")
97 OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n";
98 }
99 OS << Code << "\n}\n";
100 }
101
102 OS << "\n\n";
103}
104
Chris Lattnera0401242010-03-29 01:56:19 +0000105/// CouldMatchSameInput - Return true if it is possible for these two patterns
106/// to match the same input. For example, (add reg, reg) and
107/// (add reg, (mul ...)) could both match the same input. Where this is
108/// conservative, it falls back to returning true.
109static bool CouldMatchSameInput(const TreePatternNode *N1,
110 const TreePatternNode *N2) {
111 // If the types of the two nodes differ, they can't match the same thing.
112 if (N1->getNumTypes() != N2->getNumTypes()) return false;
113 for (unsigned i = 0, e = N1->getNumTypes(); i != e; ++i)
114 if (N1->getType(i) != N2->getType(i))
115 return false;
116
117 // Handle the case when at least one is a leaf.
118 if (N1->isLeaf()) {
119 if (N2->isLeaf()) {
120 // Handle leaf/leaf cases. Register operands can match just about
121 // anything, so we can only disambiguate a few things here.
122
123 // If both operands are leaf integer nodes with different values, they
124 // can't match the same thing.
125 if (IntInit *II1 = dynamic_cast<IntInit*>(N1->getLeafValue()))
126 if (IntInit *II2 = dynamic_cast<IntInit*>(N2->getLeafValue()))
127 return II1->getValue() == II2->getValue();
128
129 DefInit *DI1 = dynamic_cast<DefInit*>(N1->getLeafValue());
130 DefInit *DI2 = dynamic_cast<DefInit*>(N2->getLeafValue());
131 if (DI1 != 0 && DI2 != 0) {
132 if (DI1->getDef()->isSubClassOf("ValueType") &&
133 DI2->getDef()->isSubClassOf("ValueType"))
134 return DI1 == DI2;
135 if (DI1->getDef()->isSubClassOf("CondCode") &&
136 DI2->getDef()->isSubClassOf("CondCode"))
137 return DI1 == DI2;
138 }
139
140 // TODO: Regclass cannot match a condcode etc.
141
142 // Otherwise, complex pattern could match anything, so just return a
143 // conservative response.
144 return true;
145 }
146
147 // Conservatively return true. (imm) could match "7" for example, and GPR
148 // can match anything.
149 // TODO: could handle (add ...) != "1" if we cared.
150 return true;
151 }
152
153 // If N2 is a leaf and N1 isn't, check the other way.
154 if (N2->isLeaf())
155 return CouldMatchSameInput(N2, N1);
156
157 // Now we know neither node is a leaf. If the two patterns have different
158 // number of children or different operators, they can't both match.
159 Record *Op1 = N1->getOperator(), *Op2 = N1->getOperator();
160
161 if (Op1 != Op2 || N1->getNumChildren() != N2->getNumChildren())
162 return false;
163
164 // If a child prevents the two patterns from matching, use that.
165 for (unsigned i = 0, e = N1->getNumChildren(); i != e; ++i)
166 if (!CouldMatchSameInput(N1->getChild(i), N2->getChild(i)))
167 return false;
168
169 // Otherwise, it looks like they could both match the same thing.
170 return true;
171}
172
173/// GetSourceMatchPreferenceOrdering - The two input patterns are guaranteed to
174/// not match the same input. Decide which pattern we'd prefer to match first
175/// in order to reduce compile time. This sorting predicate is used to improve
176/// compile time so that we try to match scalar operations before vector
177/// operations since scalar operations are much more common in practice.
178///
179/// This returns -1 if we prefer to match N1 before N2, 1 if we prefer to match
180/// N2 before N1 or 0 if no preference.
181///
182static int GetSourceMatchPreferenceOrdering(const TreePatternNode *N1,
183 const TreePatternNode *N2) {
184 // The primary thing we sort on here is to get ints before floats and scalars
185 // before vectors.
186 for (unsigned i = 0, e = std::min(N1->getNumTypes(), N2->getNumTypes());
187 i != e; ++i)
188 if (N1->getType(i) != N2->getType(i)) {
189 MVT::SimpleValueType V1 = N1->getType(i), V2 = N2->getType(i);
190 if (MVT(V1).isVector() != MVT(V2).isVector())
191 return MVT(V1).isVector() ? 1 : -1;
192
193 if (MVT(V1).isFloatingPoint() != MVT(V2).isFloatingPoint())
194 return MVT(V1).isFloatingPoint() ? 1 : -1;
195 }
196
197 for (unsigned i = 0, e = std::min(N1->getNumChildren(), N2->getNumChildren());
198 i != e; ++i)
199 if (int Res = GetSourceMatchPreferenceOrdering(N1->getChild(i),
200 N2->getChild(i)))
201 return Res;
202 return 0;
203}
204
Chris Lattner48e86db2010-03-29 01:40:38 +0000205
Chris Lattner99ce6e82010-02-21 19:22:06 +0000206namespace {
207// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
208// In particular, we want to match maximal patterns first and lowest cost within
209// a particular complexity first.
Chris Lattneradc53472010-03-01 21:49:54 +0000210struct PatternSortingPredicate {
211 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
Chris Lattner99ce6e82010-02-21 19:22:06 +0000212 CodeGenDAGPatterns &CGP;
213
Chris Lattner48e86db2010-03-29 01:40:38 +0000214 bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
Chris Lattnera0401242010-03-29 01:56:19 +0000215 const TreePatternNode *LHSSrc = LHS->getSrcPattern();
216 const TreePatternNode *RHSSrc = RHS->getSrcPattern();
217
218 // If the patterns are guaranteed to not match at the same time and we
219 // prefer to match one before the other (for compile time reasons) use this
220 // preference as our discriminator.
221 if (0 && !CouldMatchSameInput(LHSSrc, RHSSrc)) {
222 int Ordering = GetSourceMatchPreferenceOrdering(LHSSrc, RHSSrc);
223 if (Ordering != 0) {
224 if (Ordering == -1) {
225 errs() << "SORT: " << *LHSSrc << "\n";
226 errs() << "NEXT: " << *RHSSrc << "\n\n";
227 } else {
228 errs() << "SORT: " << *RHSSrc << "\n";
229 errs() << "NEXT: " << *LHSSrc << "\n\n";
230 }
231 }
232
233 if (Ordering == -1) return true;
234 if (Ordering == 1) return false;
235 }
236
Chris Lattner48e86db2010-03-29 01:40:38 +0000237 // Otherwise, if the patterns might both match, sort based on complexity,
238 // which means that we prefer to match patterns that cover more nodes in the
239 // input over nodes that cover fewer.
240 unsigned LHSSize = LHS->getPatternComplexity(CGP);
241 unsigned RHSSize = RHS->getPatternComplexity(CGP);
Chris Lattner99ce6e82010-02-21 19:22:06 +0000242 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
243 if (LHSSize < RHSSize) return false;
244
245 // If the patterns have equal complexity, compare generated instruction cost
246 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
247 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
248 if (LHSCost < RHSCost) return true;
249 if (LHSCost > RHSCost) return false;
250
Chris Lattner117ccb72010-03-01 22:09:11 +0000251 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
252 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
253 if (LHSPatSize < RHSPatSize) return true;
254 if (LHSPatSize > RHSPatSize) return false;
255
Chris Lattner48e86db2010-03-29 01:40:38 +0000256 // Sort based on the UID of the pattern, giving us a deterministic ordering
257 // if all other sorting conditions fail.
Chris Lattnerd272fee2010-03-02 18:15:02 +0000258 assert(LHS == RHS || LHS->ID != RHS->ID);
Chris Lattner117ccb72010-03-01 22:09:11 +0000259 return LHS->ID < RHS->ID;
Chris Lattner99ce6e82010-02-21 19:22:06 +0000260 }
261};
262}
263
264
Daniel Dunbar1a551802009-07-03 00:10:29 +0000265void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +0000266 EmitSourceFileHeader("DAG Instruction Selector for the " +
267 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000268
Chris Lattner1f39e292005-09-14 00:09:24 +0000269 OS << "// *** NOTE: This file is #included into the middle of the target\n"
270 << "// *** instruction selector class. These functions are really "
271 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000272
Chris Lattner4d0c9312010-03-01 01:54:19 +0000273 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
274 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
275 E = CGP.ptm_end(); I != E; ++I) {
276 errs() << "PATTERN: "; I->getSrcPattern()->dump();
277 errs() << "\nRESULT: "; I->getDstPattern()->dump();
278 errs() << "\n";
279 });
280
281 // FIXME: These are being used by hand written code, gross.
Chris Lattnerdc32f982008-01-05 22:43:57 +0000282 EmitPredicateFunctions(OS);
Chris Lattner4d0c9312010-03-01 01:54:19 +0000283
Chris Lattner99ce6e82010-02-21 19:22:06 +0000284 // Add all the patterns to a temporary list so we can sort them.
285 std::vector<const PatternToMatch*> Patterns;
286 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
287 I != E; ++I)
288 Patterns.push_back(&*I);
289
290 // We want to process the matches in order of minimal cost. Sort the patterns
291 // so the least cost one is at the start.
Chris Lattnera0401242010-03-29 01:56:19 +0000292 std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP));
Chris Lattner99ce6e82010-02-21 19:22:06 +0000293
294
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000295 // Convert each variant of each pattern into a Matcher.
Chris Lattnerd6c84722010-02-25 19:00:39 +0000296 std::vector<Matcher*> PatternMatchers;
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000297 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
298 for (unsigned Variant = 0; ; ++Variant) {
299 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
300 PatternMatchers.push_back(M);
301 else
302 break;
303 }
304 }
305
Chris Lattnerd6c84722010-02-25 19:00:39 +0000306 Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0],
307 PatternMatchers.size());
Chris Lattner05446e72010-02-16 23:13:59 +0000308
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000309 TheMatcher = OptimizeMatcher(TheMatcher, CGP);
Chris Lattnerda272d12010-02-15 08:04:42 +0000310 //Matcher->dump();
Chris Lattner4d0c9312010-03-01 01:54:19 +0000311 EmitMatcherTable(TheMatcher, CGP, OS);
Chris Lattnerb21ba712010-02-25 02:04:40 +0000312 delete TheMatcher;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000313}