Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1 | //===- DAGISelEmitter.cpp - Generate an instruction selector --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits a DAG instruction selector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DAGISelEmitter.h" |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 15 | #include "DAGISelMatcher.h" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 16 | #include "Record.h" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 21 | // DAGISelEmitter Helper methods |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 22 | // |
| 23 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 24 | /// 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 Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 27 | static unsigned getResultPatternCost(TreePatternNode *P, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 28 | CodeGenDAGPatterns &CGP) { |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 29 | if (P->isLeaf()) return 0; |
| 30 | |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 31 | unsigned Cost = 0; |
| 32 | Record *Op = P->getOperator(); |
| 33 | if (Op->isSubClassOf("Instruction")) { |
| 34 | Cost++; |
Chris Lattner | f30187a | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 35 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); |
Dan Gohman | 533297b | 2009-10-29 18:10:34 +0000 | [diff] [blame] | 36 | if (II.usesCustomInserter) |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 37 | Cost += 10; |
| 38 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 39 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 40 | Cost += getResultPatternCost(P->getChild(i), CGP); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 41 | return Cost; |
| 42 | } |
| 43 | |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 44 | /// getResultPatternCodeSize - Compute the code size of instructions for this |
| 45 | /// pattern. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 46 | static unsigned getResultPatternSize(TreePatternNode *P, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 47 | CodeGenDAGPatterns &CGP) { |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 48 | 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 Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 56 | Cost += getResultPatternSize(P->getChild(i), CGP); |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 57 | return Cost; |
| 58 | } |
| 59 | |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 60 | //===----------------------------------------------------------------------===// |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 61 | // Predicate emitter implementation. |
| 62 | // |
| 63 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 64 | void DAGISelEmitter::EmitPredicateFunctions(raw_ostream &OS) { |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 65 | 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 Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 72 | for (CodeGenDAGPatterns::pf_iterator I = CGP.pf_begin(), E = CGP.pf_end(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 73 | 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 Lattner | ccba15f | 2010-02-16 07:26:36 +0000 | [diff] [blame] | 88 | << "(SDNode *N) const {\n"; |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 89 | else { |
| 90 | std::string ClassName = |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 91 | CGP.getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 92 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 93 | |
| 94 | OS << "inline bool Predicate_" << PatFragRecord->getName() |
Chris Lattner | ccba15f | 2010-02-16 07:26:36 +0000 | [diff] [blame] | 95 | << "(SDNode *" << C2 << ") const {\n"; |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 96 | 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 Lattner | a040124 | 2010-03-29 01:56:19 +0000 | [diff] [blame^] | 105 | /// 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. |
| 109 | static 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 | /// |
| 182 | static 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 Lattner | 48e86db | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 206 | namespace { |
| 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 Lattner | adc5347 | 2010-03-01 21:49:54 +0000 | [diff] [blame] | 210 | struct PatternSortingPredicate { |
| 211 | PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {} |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 212 | CodeGenDAGPatterns &CGP; |
| 213 | |
Chris Lattner | 48e86db | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 214 | bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) { |
Chris Lattner | a040124 | 2010-03-29 01:56:19 +0000 | [diff] [blame^] | 215 | 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 Lattner | 48e86db | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 237 | // 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 Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 242 | 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 Lattner | 117ccb7 | 2010-03-01 22:09:11 +0000 | [diff] [blame] | 251 | 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 Lattner | 48e86db | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 256 | // Sort based on the UID of the pattern, giving us a deterministic ordering |
| 257 | // if all other sorting conditions fail. |
Chris Lattner | d272fee | 2010-03-02 18:15:02 +0000 | [diff] [blame] | 258 | assert(LHS == RHS || LHS->ID != RHS->ID); |
Chris Lattner | 117ccb7 | 2010-03-01 22:09:11 +0000 | [diff] [blame] | 259 | return LHS->ID < RHS->ID; |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 260 | } |
| 261 | }; |
| 262 | } |
| 263 | |
| 264 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 265 | void DAGISelEmitter::run(raw_ostream &OS) { |
Chris Lattner | 200c57e | 2008-01-05 22:58:54 +0000 | [diff] [blame] | 266 | EmitSourceFileHeader("DAG Instruction Selector for the " + |
| 267 | CGP.getTargetInfo().getName() + " target", OS); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 269 | 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 Lattner | f8dc061 | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 4d0c931 | 2010-03-01 01:54:19 +0000 | [diff] [blame] | 273 | 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 Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 282 | EmitPredicateFunctions(OS); |
Chris Lattner | 4d0c931 | 2010-03-01 01:54:19 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 284 | // 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 Lattner | a040124 | 2010-03-29 01:56:19 +0000 | [diff] [blame^] | 292 | std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP)); |
Chris Lattner | 99ce6e8 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 293 | |
| 294 | |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 295 | // Convert each variant of each pattern into a Matcher. |
Chris Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 296 | std::vector<Matcher*> PatternMatchers; |
Chris Lattner | fa342fa | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 297 | 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 Lattner | d6c8472 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 306 | Matcher *TheMatcher = new ScopeMatcher(&PatternMatchers[0], |
| 307 | PatternMatchers.size()); |
Chris Lattner | 05446e7 | 2010-02-16 23:13:59 +0000 | [diff] [blame] | 308 | |
Chris Lattner | c78f2a3 | 2010-02-28 20:49:53 +0000 | [diff] [blame] | 309 | TheMatcher = OptimizeMatcher(TheMatcher, CGP); |
Chris Lattner | da272d1 | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 310 | //Matcher->dump(); |
Chris Lattner | 4d0c931 | 2010-03-01 01:54:19 +0000 | [diff] [blame] | 311 | EmitMatcherTable(TheMatcher, CGP, OS); |
Chris Lattner | b21ba71 | 2010-02-25 02:04:40 +0000 | [diff] [blame] | 312 | delete TheMatcher; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 313 | } |