Chris Lattner | d2a5b36 | 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 | 8adcd9f | 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 | d2a5b36 | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits a DAG instruction selector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 14 | #include "CodeGenDAGPatterns.h" |
Chris Lattner | b02cdaa | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 15 | #include "DAGISelMatcher.h" |
Chris Lattner | d2a5b36 | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 17 | #include "llvm/TableGen/Record.h" |
| 18 | #include "llvm/TableGen/TableGenBackend.h" |
Chris Lattner | d2a5b36 | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "dag-isel-emitter" |
| 22 | |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 23 | namespace { |
| 24 | /// DAGISelEmitter - The top-level class which coordinates construction |
| 25 | /// and emission of the instruction selector. |
| 26 | class DAGISelEmitter { |
| 27 | CodeGenDAGPatterns CGP; |
| 28 | public: |
| 29 | explicit DAGISelEmitter(RecordKeeper &R) : CGP(R) {} |
| 30 | void run(raw_ostream &OS); |
| 31 | }; |
| 32 | } // End anonymous namespace |
| 33 | |
Chris Lattner | 35bcd14 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 34 | //===----------------------------------------------------------------------===// |
Chris Lattner | e7170df | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 35 | // DAGISelEmitter Helper methods |
Chris Lattner | d2a5b36 | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 36 | // |
| 37 | |
Chris Lattner | 3622f15 | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 38 | /// getResultPatternCost - Compute the number of instructions for this pattern. |
| 39 | /// This is a temporary hack. We should really include the instruction |
| 40 | /// latencies in this calculation. |
Chris Lattner | 8cab021 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 41 | static unsigned getResultPatternCost(TreePatternNode *P, |
Chris Lattner | ab3242f | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 42 | CodeGenDAGPatterns &CGP) { |
Chris Lattner | 3622f15 | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 43 | if (P->isLeaf()) return 0; |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 44 | |
Evan Cheng | 33f4156 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 45 | unsigned Cost = 0; |
| 46 | Record *Op = P->getOperator(); |
| 47 | if (Op->isSubClassOf("Instruction")) { |
| 48 | Cost++; |
Chris Lattner | 9aec14b | 2010-03-19 00:07:20 +0000 | [diff] [blame] | 49 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op); |
Dan Gohman | 453d64c | 2009-10-29 18:10:34 +0000 | [diff] [blame] | 50 | if (II.usesCustomInserter) |
Evan Cheng | 33f4156 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 51 | Cost += 10; |
| 52 | } |
Chris Lattner | 3622f15 | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 8cab021 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 54 | Cost += getResultPatternCost(P->getChild(i), CGP); |
Chris Lattner | 3622f15 | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 55 | return Cost; |
| 56 | } |
| 57 | |
Evan Cheng | c767acd | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 58 | /// getResultPatternCodeSize - Compute the code size of instructions for this |
| 59 | /// pattern. |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 60 | static unsigned getResultPatternSize(TreePatternNode *P, |
Chris Lattner | ab3242f | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 61 | CodeGenDAGPatterns &CGP) { |
Evan Cheng | c767acd | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 62 | if (P->isLeaf()) return 0; |
| 63 | |
| 64 | unsigned Cost = 0; |
| 65 | Record *Op = P->getOperator(); |
| 66 | if (Op->isSubClassOf("Instruction")) { |
| 67 | Cost += Op->getValueAsInt("CodeSize"); |
| 68 | } |
| 69 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 8cab021 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 70 | Cost += getResultPatternSize(P->getChild(i), CGP); |
Evan Cheng | c767acd | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 71 | return Cost; |
| 72 | } |
| 73 | |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 74 | namespace { |
| 75 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 76 | // In particular, we want to match maximal patterns first and lowest cost within |
| 77 | // a particular complexity first. |
Chris Lattner | 33758dc | 2010-03-01 21:49:54 +0000 | [diff] [blame] | 78 | struct PatternSortingPredicate { |
| 79 | PatternSortingPredicate(CodeGenDAGPatterns &cgp) : CGP(cgp) {} |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 80 | CodeGenDAGPatterns &CGP; |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 05925fe | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 82 | bool operator()(const PatternToMatch *LHS, const PatternToMatch *RHS) { |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 83 | const TreePatternNode *LT = LHS->getSrcPattern(); |
| 84 | const TreePatternNode *RT = RHS->getSrcPattern(); |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 85 | |
Krzysztof Parzyszek | 779d98e | 2017-09-14 16:56:21 +0000 | [diff] [blame] | 86 | MVT LHSVT = LT->getNumTypes() != 0 ? LT->getSimpleType(0) : MVT::Other; |
| 87 | MVT RHSVT = RT->getNumTypes() != 0 ? RT->getSimpleType(0) : MVT::Other; |
Richard Sandiford | 28942d4 | 2013-10-01 09:49:01 +0000 | [diff] [blame] | 88 | if (LHSVT.isVector() != RHSVT.isVector()) |
| 89 | return RHSVT.isVector(); |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 90 | |
Richard Sandiford | 28942d4 | 2013-10-01 09:49:01 +0000 | [diff] [blame] | 91 | if (LHSVT.isFloatingPoint() != RHSVT.isFloatingPoint()) |
| 92 | return RHSVT.isFloatingPoint(); |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 05925fe | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 94 | // Otherwise, if the patterns might both match, sort based on complexity, |
| 95 | // which means that we prefer to match patterns that cover more nodes in the |
| 96 | // input over nodes that cover fewer. |
Tom Stellard | 6655dd6 | 2014-08-01 00:32:36 +0000 | [diff] [blame] | 97 | int LHSSize = LHS->getPatternComplexity(CGP); |
| 98 | int RHSSize = RHS->getPatternComplexity(CGP); |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 99 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 100 | if (LHSSize < RHSSize) return false; |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 102 | // If the patterns have equal complexity, compare generated instruction cost |
| 103 | unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP); |
| 104 | unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP); |
| 105 | if (LHSCost < RHSCost) return true; |
| 106 | if (LHSCost > RHSCost) return false; |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 107 | |
Chris Lattner | d39f75b | 2010-03-01 22:09:11 +0000 | [diff] [blame] | 108 | unsigned LHSPatSize = getResultPatternSize(LHS->getDstPattern(), CGP); |
| 109 | unsigned RHSPatSize = getResultPatternSize(RHS->getDstPattern(), CGP); |
| 110 | if (LHSPatSize < RHSPatSize) return true; |
| 111 | if (LHSPatSize > RHSPatSize) return false; |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 05925fe | 2010-03-29 01:40:38 +0000 | [diff] [blame] | 113 | // Sort based on the UID of the pattern, giving us a deterministic ordering |
| 114 | // if all other sorting conditions fail. |
Chris Lattner | fb8c2b2 | 2010-03-02 18:15:02 +0000 | [diff] [blame] | 115 | assert(LHS == RHS || LHS->ID != RHS->ID); |
Chris Lattner | d39f75b | 2010-03-01 22:09:11 +0000 | [diff] [blame] | 116 | return LHS->ID < RHS->ID; |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 117 | } |
| 118 | }; |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 119 | } // End anonymous namespace |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 120 | |
| 121 | |
Daniel Dunbar | 38a22bf | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 122 | void DAGISelEmitter::run(raw_ostream &OS) { |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 123 | emitSourceFileHeader("DAG Instruction Selector for the " + |
Matthias Braun | 4a86d45 | 2016-12-04 05:48:16 +0000 | [diff] [blame] | 124 | CGP.getTargetInfo().getName().str() + " target", OS); |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4c7b604 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 126 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 127 | << "// *** instruction selector class. These functions are really " |
| 128 | << "methods.\n\n"; |
Chris Lattner | b2b9d6f | 2008-02-03 06:49:24 +0000 | [diff] [blame] | 129 | |
Krzysztof Parzyszek | b8c68c6 | 2017-11-10 18:36:04 +0000 | [diff] [blame] | 130 | OS << "// If GET_DAGISEL_DECL is #defined with any value, only function\n" |
| 131 | "// declarations will be included when this file is included.\n" |
| 132 | "// If GET_DAGISEL_BODY is #defined, its value should be the name of\n" |
| 133 | "// the instruction selector class. Function bodies will be emitted\n" |
| 134 | "// and each function's name will be qualified with the name of the\n" |
| 135 | "// class.\n" |
| 136 | "//\n" |
| 137 | "// When neither of the GET_DAGISEL* macros is defined, the functions\n" |
| 138 | "// are emitted inline.\n\n"; |
| 139 | |
Chris Lattner | 58e7dad | 2010-03-01 01:54:19 +0000 | [diff] [blame] | 140 | DEBUG(errs() << "\n\nALL PATTERNS TO MATCH:\n\n"; |
| 141 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), |
| 142 | E = CGP.ptm_end(); I != E; ++I) { |
| 143 | errs() << "PATTERN: "; I->getSrcPattern()->dump(); |
| 144 | errs() << "\nRESULT: "; I->getDstPattern()->dump(); |
| 145 | errs() << "\n"; |
| 146 | }); |
| 147 | |
Chris Lattner | 8f40097 | 2010-02-21 19:22:06 +0000 | [diff] [blame] | 148 | // Add all the patterns to a temporary list so we can sort them. |
| 149 | std::vector<const PatternToMatch*> Patterns; |
| 150 | for (CodeGenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end(); |
| 151 | I != E; ++I) |
| 152 | Patterns.push_back(&*I); |
| 153 | |
| 154 | // We want to process the matches in order of minimal cost. Sort the patterns |
| 155 | // so the least cost one is at the start. |
Chris Lattner | f91869b | 2010-03-29 02:02:45 +0000 | [diff] [blame] | 156 | std::sort(Patterns.begin(), Patterns.end(), PatternSortingPredicate(CGP)); |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 157 | |
| 158 | |
Chris Lattner | 053a28a | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 159 | // Convert each variant of each pattern into a Matcher. |
Chris Lattner | f7fc2d8 | 2010-02-25 19:00:39 +0000 | [diff] [blame] | 160 | std::vector<Matcher*> PatternMatchers; |
Chris Lattner | 053a28a | 2010-03-01 07:17:40 +0000 | [diff] [blame] | 161 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 162 | for (unsigned Variant = 0; ; ++Variant) { |
| 163 | if (Matcher *M = ConvertPatternToMatcher(*Patterns[i], Variant, CGP)) |
| 164 | PatternMatchers.push_back(M); |
| 165 | else |
| 166 | break; |
| 167 | } |
| 168 | } |
Jim Grosbach | 621818a | 2011-03-01 01:39:05 +0000 | [diff] [blame] | 169 | |
Craig Topper | 574f6d4 | 2014-12-15 00:40:07 +0000 | [diff] [blame] | 170 | std::unique_ptr<Matcher> TheMatcher = |
| 171 | llvm::make_unique<ScopeMatcher>(PatternMatchers); |
Chris Lattner | e83e248 | 2010-02-16 23:13:59 +0000 | [diff] [blame] | 172 | |
Craig Topper | 574f6d4 | 2014-12-15 00:40:07 +0000 | [diff] [blame] | 173 | OptimizeMatcher(TheMatcher, CGP); |
Chris Lattner | b02cdaa | 2010-02-15 08:04:42 +0000 | [diff] [blame] | 174 | //Matcher->dump(); |
Craig Topper | 574f6d4 | 2014-12-15 00:40:07 +0000 | [diff] [blame] | 175 | EmitMatcherTable(TheMatcher.get(), CGP, OS); |
Chris Lattner | d2a5b36 | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 176 | } |
Jakob Stoklund Olesen | e6aed13 | 2012-06-11 15:37:55 +0000 | [diff] [blame] | 177 | |
| 178 | namespace llvm { |
| 179 | |
| 180 | void EmitDAGISel(RecordKeeper &RK, raw_ostream &OS) { |
| 181 | DAGISelEmitter(RK).run(OS); |
| 182 | } |
| 183 | |
| 184 | } // End llvm namespace |