blob: f4a287c6c65368732835acbe2a2a57707328ebc7 [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 Lattner48e86db2010-03-29 01:40:38 +0000105
Chris Lattner99ce6e82010-02-21 19:22:06 +0000106namespace {
107// PatternSortingPredicate - return true if we prefer to match LHS before RHS.
108// In particular, we want to match maximal patterns first and lowest cost within
109// a particular complexity first.
Chris Lattneradc53472010-03-01 21:49:54 +0000110struct PatternSortingPredicate {
111 PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {}
Chris Lattner99ce6e82010-02-21 19:22:06 +0000112 CodeGenDAGPatterns &CGP;
113
Chris Lattner48e86db2010-03-29 01:40:38 +0000114 bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) {
115 // Otherwise, if the patterns might both match, sort based on complexity,
116 // which means that we prefer to match patterns that cover more nodes in the
117 // input over nodes that cover fewer.
118 unsigned LHSSize = LHS->getPatternComplexity(CGP);
119 unsigned RHSSize = RHS->getPatternComplexity(CGP);
Chris Lattner99ce6e82010-02-21 19:22:06 +0000120 if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost
121 if (LHSSize < RHSSize) return false;
122
123 // If the patterns have equal complexity, compare generated instruction cost
124 unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP);
125 unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP);
126 if (LHSCost < RHSCost) return true;
127 if (LHSCost > RHSCost) return false;
128
Chris Lattner117ccb72010-03-01 22:09:11 +0000129 unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP);
130 unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP);
131 if (LHSPatSize < RHSPatSize) return true;
132 if (LHSPatSize > RHSPatSize) return false;
133
Chris Lattner48e86db2010-03-29 01:40:38 +0000134 // Sort based on the UID of the pattern, giving us a deterministic ordering
135 // if all other sorting conditions fail.
Chris Lattnerd272fee2010-03-02 18:15:02 +0000136 assert(LHS == RHS || LHS->ID != RHS->ID);
Chris Lattner117ccb72010-03-01 22:09:11 +0000137 return LHS->ID < RHS->ID;
Chris Lattner99ce6e82010-02-21 19:22:06 +0000138 }
139};
140}
141
142
Daniel Dunbar1a551802009-07-03 00:10:29 +0000143void DAGISelEmitter::run(raw_ostream &OS) {
Chris Lattner200c57e2008-01-05 22:58:54 +0000144 EmitSourceFileHeader("DAG Instruction Selector for the " +
145 CGP.getTargetInfo().getName() + " target", OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000146
Chris Lattner1f39e292005-09-14 00:09:24 +0000147 OS << "// *** NOTE: This file is #included into the middle of the target\n"
148 << "// *** instruction selector class. These functions are really "
149 << "methods.\n\n";
Chris Lattnerf8dc0612008-02-03 06:49:24 +0000150
Chris Lattner4d0c9312010-03-01 01:54:19 +0000151 DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n";
152 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(),
153 E = CGP.ptm_end(); I != E; ++I) {
154 errs() << "PATTERN: "; I->getSrcPattern()->dump();
155 errs() << "\nRESULT: "; I->getDstPattern()->dump();
156 errs() << "\n";
157 });
158
159 // FIXME: These are being used by hand written code, gross.
Chris Lattnerdc32f982008-01-05 22:43:57 +0000160 EmitPredicateFunctions(OS);
Chris Lattner4d0c9312010-03-01 01:54:19 +0000161
Chris Lattner99ce6e82010-02-21 19:22:06 +0000162 // Add all the patterns to a temporary list so we can sort them.
163 std::vector<const PatternToMatch*> Patterns;
164 for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end();
165 I != E; ++I)
166 Patterns.push_back(&*I);
167
168 // We want to process the matches in order of minimal cost. Sort the patterns
169 // so the least cost one is at the start.
Chris Lattner99ce6e82010-02-21 19:22:06 +0000170 std::stable_sort(Patterns.begin(), Patterns.end(),
Chris Lattneradc53472010-03-01 21:49:54 +0000171 PatternSortingPredicate(CGP));
Chris Lattner99ce6e82010-02-21 19:22:06 +0000172
173
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000174 // Convert each variant of each pattern into a Matcher.
Chris Lattnerd6c84722010-02-25 19:00:39 +0000175 std::vector<Matcher*> PatternMatchers;
Chris Lattnerfa342fa2010-03-01 07:17:40 +0000176 for (unsigned i = 0, e = Patterns.size(); i != e; ++i) {
177 for (unsigned Variant = 0; ; ++Variant) {
178 if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP))
179 PatternMatchers.push_back(M);
180 else
181 break;
182 }
183 }
184
Chris Lattnerd6c84722010-02-25 19:00:39 +0000185 Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0],
186 PatternMatchers.size());
Chris Lattner05446e72010-02-16 23:13:59 +0000187
Chris Lattnerc78f2a32010-02-28 20:49:53 +0000188 TheMatcher = OptimizeMatcher(TheMatcher, CGP);
Chris Lattnerda272d12010-02-15 08:04:42 +0000189 //Matcher->dump();
Chris Lattner4d0c9312010-03-01 01:54:19 +0000190 EmitMatcherTable(TheMatcher, CGP, OS);
Chris Lattnerb21ba712010-02-25 02:04:40 +0000191 delete TheMatcher;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000192}