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" |
| 15 | #include "Record.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/Debug.h" |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MathExtras.h" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Streams.h" |
Jeff Cohen | a48283b | 2005-09-25 19:04:43 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 24 | // DAGISelEmitter implementation |
| 25 | // |
| 26 | |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 28 | /// NodeIsComplexPattern - return true if N is a leaf node and a subclass of |
| 29 | /// ComplexPattern. |
| 30 | static bool NodeIsComplexPattern(TreePatternNode *N) { |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 31 | return (N->isLeaf() && |
| 32 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 33 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 34 | isSubClassOf("ComplexPattern")); |
| 35 | } |
| 36 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 37 | /// NodeGetComplexPattern - return the pointer to the ComplexPattern if N |
| 38 | /// is a leaf node and a subclass of ComplexPattern, else it returns NULL. |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 39 | static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N, |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 40 | CodegenDAGPatterns &CGP) { |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 41 | if (N->isLeaf() && |
| 42 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 43 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 44 | isSubClassOf("ComplexPattern")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 45 | return &CGP.getComplexPattern(static_cast<DefInit*>(N->getLeafValue()) |
| 46 | ->getDef()); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 47 | } |
| 48 | return NULL; |
| 49 | } |
| 50 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 51 | /// getPatternSize - Return the 'size' of this pattern. We want to match large |
| 52 | /// patterns before small ones. This is used to determine the size of a |
| 53 | /// pattern. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 54 | static unsigned getPatternSize(TreePatternNode *P, CodegenDAGPatterns &CGP) { |
| 55 | assert((MVT::isExtIntegerInVTs(P->getExtTypes()) || |
| 56 | MVT::isExtFloatingPointInVTs(P->getExtTypes()) || |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 57 | P->getExtTypeNum(0) == MVT::isVoid || |
| 58 | P->getExtTypeNum(0) == MVT::Flag || |
| 59 | P->getExtTypeNum(0) == MVT::iPTR) && |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 60 | "Not a valid pattern node to size!"); |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 61 | unsigned Size = 3; // The node itself. |
Evan Cheng | 657416c | 2006-02-01 06:06:31 +0000 | [diff] [blame] | 62 | // If the root node is a ConstantSDNode, increases its size. |
| 63 | // e.g. (set R32:$dst, 0). |
| 64 | if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue())) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 65 | Size += 2; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 66 | |
| 67 | // FIXME: This is a hack to statically increase the priority of patterns |
| 68 | // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. |
| 69 | // Later we can allow complexity / cost for each pattern to be (optionally) |
| 70 | // specified. To get best possible pattern match we'll need to dynamically |
| 71 | // calculate the complexity of all patterns a dag can potentially map to. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 72 | const ComplexPattern *AM = NodeGetComplexPattern(P, CGP); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 73 | if (AM) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 74 | Size += AM->getNumOperands() * 3; |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 75 | |
| 76 | // If this node has some predicate function that must match, it adds to the |
| 77 | // complexity of this node. |
| 78 | if (!P->getPredicateFn().empty()) |
| 79 | ++Size; |
| 80 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 81 | // Count children in the count if they are also nodes. |
| 82 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
| 83 | TreePatternNode *Child = P->getChild(i); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 84 | if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 85 | Size += getPatternSize(Child, CGP); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 86 | else if (Child->isLeaf()) { |
| 87 | if (dynamic_cast<IntInit*>(Child->getLeafValue())) |
Evan Cheng | 6cec34e | 2006-09-08 07:26:39 +0000 | [diff] [blame] | 88 | Size += 5; // Matches a ConstantSDNode (+3) and a specific value (+2). |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 89 | else if (NodeIsComplexPattern(Child)) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 90 | Size += getPatternSize(Child, CGP); |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 91 | else if (!Child->getPredicateFn().empty()) |
| 92 | ++Size; |
Chris Lattner | 2f041d4 | 2005-10-19 04:41:05 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | return Size; |
| 97 | } |
| 98 | |
| 99 | /// getResultPatternCost - Compute the number of instructions for this pattern. |
| 100 | /// This is a temporary hack. We should really include the instruction |
| 101 | /// latencies in this calculation. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 102 | static unsigned getResultPatternCost(TreePatternNode *P, |
| 103 | CodegenDAGPatterns &CGP) { |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 104 | if (P->isLeaf()) return 0; |
| 105 | |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 106 | unsigned Cost = 0; |
| 107 | Record *Op = P->getOperator(); |
| 108 | if (Op->isSubClassOf("Instruction")) { |
| 109 | Cost++; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 110 | CodeGenInstruction &II = CGP.getTargetInfo().getInstruction(Op->getName()); |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 111 | if (II.usesCustomDAGSchedInserter) |
| 112 | Cost += 10; |
| 113 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 114 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 115 | Cost += getResultPatternCost(P->getChild(i), CGP); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 116 | return Cost; |
| 117 | } |
| 118 | |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 119 | /// getResultPatternCodeSize - Compute the code size of instructions for this |
| 120 | /// pattern. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 121 | static unsigned getResultPatternSize(TreePatternNode *P, |
| 122 | CodegenDAGPatterns &CGP) { |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 123 | if (P->isLeaf()) return 0; |
| 124 | |
| 125 | unsigned Cost = 0; |
| 126 | Record *Op = P->getOperator(); |
| 127 | if (Op->isSubClassOf("Instruction")) { |
| 128 | Cost += Op->getValueAsInt("CodeSize"); |
| 129 | } |
| 130 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 131 | Cost += getResultPatternSize(P->getChild(i), CGP); |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 132 | return Cost; |
| 133 | } |
| 134 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 135 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 136 | // In particular, we want to match maximal patterns first and lowest cost within |
| 137 | // a particular complexity first. |
| 138 | struct PatternSortingPredicate { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 139 | PatternSortingPredicate(CodegenDAGPatterns &cgp) : CGP(cgp) {} |
| 140 | CodegenDAGPatterns &CGP; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 142 | bool operator()(const PatternToMatch *LHS, |
| 143 | const PatternToMatch *RHS) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 144 | unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), CGP); |
| 145 | unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), CGP); |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 146 | LHSSize += LHS->getAddedComplexity(); |
| 147 | RHSSize += RHS->getAddedComplexity(); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 148 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 149 | if (LHSSize < RHSSize) return false; |
| 150 | |
| 151 | // If the patterns have equal complexity, compare generated instruction cost |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 152 | unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), CGP); |
| 153 | unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), CGP); |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 154 | if (LHSCost < RHSCost) return true; |
| 155 | if (LHSCost > RHSCost) return false; |
| 156 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 157 | return getResultPatternSize(LHS->getDstPattern(), CGP) < |
| 158 | getResultPatternSize(RHS->getDstPattern(), CGP); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 159 | } |
| 160 | }; |
| 161 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 162 | /// getRegisterValueType - Look up and return the first ValueType of specified |
| 163 | /// RegisterClass record |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 164 | static MVT::ValueType getRegisterValueType(Record *R, const CodeGenTarget &T) { |
Chris Lattner | 22faeab | 2005-12-05 02:36:37 +0000 | [diff] [blame] | 165 | if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R)) |
| 166 | return RC->getValueTypeNum(0); |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 167 | return MVT::Other; |
| 168 | } |
| 169 | |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 171 | /// RemoveAllTypes - A quick recursive walk over a pattern which removes all |
| 172 | /// type information from it. |
| 173 | static void RemoveAllTypes(TreePatternNode *N) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 174 | N->removeTypes(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 175 | if (!N->isLeaf()) |
| 176 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 177 | RemoveAllTypes(N->getChild(i)); |
| 178 | } |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 179 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 180 | /// NodeHasProperty - return true if TreePatternNode has the specified |
| 181 | /// property. |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 182 | static bool NodeHasProperty(TreePatternNode *N, SDNP Property, |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 183 | CodegenDAGPatterns &CGP) { |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 184 | if (N->isLeaf()) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 185 | const ComplexPattern *CP = NodeGetComplexPattern(N, CGP); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 186 | if (CP) |
| 187 | return CP->hasProperty(Property); |
| 188 | return false; |
| 189 | } |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 190 | Record *Operator = N->getOperator(); |
| 191 | if (!Operator->isSubClassOf("SDNode")) return false; |
| 192 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 193 | return CGP.getSDNodeInfo(Operator).hasProperty(Property); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 196 | static bool PatternHasProperty(TreePatternNode *N, SDNP Property, |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 197 | CodegenDAGPatterns &CGP) { |
| 198 | if (NodeHasProperty(N, Property, CGP)) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 199 | return true; |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 200 | |
| 201 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 202 | TreePatternNode *Child = N->getChild(i); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 203 | if (PatternHasProperty(Child, Property, CGP)) |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 204 | return true; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 210 | class PatternCodeEmitter { |
| 211 | private: |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 212 | CodegenDAGPatterns &CGP; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 213 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 214 | // Predicates. |
| 215 | ListInit *Predicates; |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 216 | // Pattern cost. |
| 217 | unsigned Cost; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 218 | // Instruction selector pattern. |
| 219 | TreePatternNode *Pattern; |
| 220 | // Matched instruction. |
| 221 | TreePatternNode *Instruction; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 222 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 223 | // Node to name mapping |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 224 | std::map<std::string, std::string> VariableMap; |
| 225 | // Node to operator mapping |
| 226 | std::map<std::string, Record*> OperatorMap; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 227 | // Names of all the folded nodes which produce chains. |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 228 | std::vector<std::pair<std::string, unsigned> > FoldedChains; |
Evan Cheng | 4326ef5 | 2006-10-12 02:08:53 +0000 | [diff] [blame] | 229 | // Original input chain(s). |
| 230 | std::vector<std::pair<std::string, std::string> > OrigChains; |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 231 | std::set<std::string> Duplicates; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 232 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 233 | /// GeneratedCode - This is the buffer that we emit code to. The first int |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 234 | /// indicates whether this is an exit predicate (something that should be |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 235 | /// tested, and if true, the match fails) [when 1], or normal code to emit |
| 236 | /// [when 0], or initialization code to emit [when 2]. |
| 237 | std::vector<std::pair<unsigned, std::string> > &GeneratedCode; |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 238 | /// GeneratedDecl - This is the set of all SDOperand declarations needed for |
| 239 | /// the set of patterns for each top-level opcode. |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 240 | std::set<std::string> &GeneratedDecl; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 241 | /// TargetOpcodes - The target specific opcodes used by the resulting |
| 242 | /// instructions. |
| 243 | std::vector<std::string> &TargetOpcodes; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 244 | std::vector<std::string> &TargetVTs; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 245 | |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 246 | std::string ChainName; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 247 | unsigned TmpNo; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 248 | unsigned OpcNo; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 249 | unsigned VTNo; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 250 | |
| 251 | void emitCheck(const std::string &S) { |
| 252 | if (!S.empty()) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 253 | GeneratedCode.push_back(std::make_pair(1, S)); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 254 | } |
| 255 | void emitCode(const std::string &S) { |
| 256 | if (!S.empty()) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 257 | GeneratedCode.push_back(std::make_pair(0, S)); |
| 258 | } |
| 259 | void emitInit(const std::string &S) { |
| 260 | if (!S.empty()) |
| 261 | GeneratedCode.push_back(std::make_pair(2, S)); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 262 | } |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 263 | void emitDecl(const std::string &S) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 264 | assert(!S.empty() && "Invalid declaration"); |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 265 | GeneratedDecl.insert(S); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 266 | } |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 267 | void emitOpcode(const std::string &Opc) { |
| 268 | TargetOpcodes.push_back(Opc); |
| 269 | OpcNo++; |
| 270 | } |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 271 | void emitVT(const std::string &VT) { |
| 272 | TargetVTs.push_back(VT); |
| 273 | VTNo++; |
| 274 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 275 | public: |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 276 | PatternCodeEmitter(CodegenDAGPatterns &cgp, ListInit *preds, |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 277 | TreePatternNode *pattern, TreePatternNode *instr, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 278 | std::vector<std::pair<unsigned, std::string> > &gc, |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 279 | std::set<std::string> &gd, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 280 | std::vector<std::string> &to, |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 281 | std::vector<std::string> &tv) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 282 | : CGP(cgp), Predicates(preds), Pattern(pattern), Instruction(instr), |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 283 | GeneratedCode(gc), GeneratedDecl(gd), |
| 284 | TargetOpcodes(to), TargetVTs(tv), |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 285 | TmpNo(0), OpcNo(0), VTNo(0) {} |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 286 | |
| 287 | /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo |
| 288 | /// if the match fails. At this point, we already know that the opcode for N |
| 289 | /// matches, and the SDNode for the result has the RootName specified name. |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 290 | void EmitMatchCode(TreePatternNode *N, TreePatternNode *P, |
| 291 | const std::string &RootName, const std::string &ChainSuffix, |
| 292 | bool &FoundChain) { |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 293 | bool isRoot = (P == NULL); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 294 | // Emit instruction predicates. Each predicate is just a string for now. |
| 295 | if (isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 296 | std::string PredicateCheck; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 297 | for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { |
| 298 | if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) { |
| 299 | Record *Def = Pred->getDef(); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 300 | if (!Def->isSubClassOf("Predicate")) { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 301 | #ifndef NDEBUG |
| 302 | Def->dump(); |
| 303 | #endif |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 304 | assert(0 && "Unknown predicate type!"); |
| 305 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 306 | if (!PredicateCheck.empty()) |
Chris Lattner | bc7fa52 | 2006-09-19 00:41:36 +0000 | [diff] [blame] | 307 | PredicateCheck += " && "; |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 308 | PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 311 | |
| 312 | emitCheck(PredicateCheck); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 315 | if (N->isLeaf()) { |
| 316 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 317 | emitCheck("cast<ConstantSDNode>(" + RootName + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 318 | ")->getSignExtended() == " + itostr(II->getValue())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 319 | return; |
| 320 | } else if (!NodeIsComplexPattern(N)) { |
| 321 | assert(0 && "Cannot match this as a leaf value!"); |
| 322 | abort(); |
| 323 | } |
| 324 | } |
| 325 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 326 | // If this node has a name associated with it, capture it in VariableMap. If |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 327 | // we already saw this in the pattern, emit code to verify dagness. |
| 328 | if (!N->getName().empty()) { |
| 329 | std::string &VarMapEntry = VariableMap[N->getName()]; |
| 330 | if (VarMapEntry.empty()) { |
| 331 | VarMapEntry = RootName; |
| 332 | } else { |
| 333 | // If we get here, this is a second reference to a specific name. Since |
| 334 | // we already have checked that the first reference is valid, we don't |
| 335 | // have to recursively match it, just check that it's the same as the |
| 336 | // previously named thing. |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 337 | emitCheck(VarMapEntry + " == " + RootName); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 340 | |
| 341 | if (!N->isLeaf()) |
| 342 | OperatorMap[N->getName()] = N->getOperator(); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | // Emit code to load the child nodes and match their contents recursively. |
| 347 | unsigned OpNo = 0; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 348 | bool NodeHasChain = NodeHasProperty (N, SDNPHasChain, CGP); |
| 349 | bool HasChain = PatternHasProperty(N, SDNPHasChain, CGP); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 350 | bool EmittedUseCheck = false; |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 351 | if (HasChain) { |
Evan Cheng | 76356d9 | 2006-01-20 01:11:03 +0000 | [diff] [blame] | 352 | if (NodeHasChain) |
| 353 | OpNo = 1; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 354 | if (!isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 355 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 356 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 357 | EmittedUseCheck = true; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 358 | if (NodeHasChain) { |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 359 | // If the immediate use can somehow reach this node through another |
| 360 | // path, then can't fold it either or it will create a cycle. |
| 361 | // e.g. In the following diagram, XX can reach ld through YY. If |
| 362 | // ld is folded into XX, then YY is both a predecessor and a successor |
| 363 | // of XX. |
| 364 | // |
| 365 | // [ld] |
| 366 | // ^ ^ |
| 367 | // | | |
| 368 | // / \--- |
| 369 | // / [YY] |
| 370 | // | ^ |
| 371 | // [XX]-------| |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 372 | bool NeedCheck = false; |
| 373 | if (P != Pattern) |
| 374 | NeedCheck = true; |
| 375 | else { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 376 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(P->getOperator()); |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 377 | NeedCheck = |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 378 | P->getOperator() == CGP.get_intrinsic_void_sdnode() || |
| 379 | P->getOperator() == CGP.get_intrinsic_w_chain_sdnode() || |
| 380 | P->getOperator() == CGP.get_intrinsic_wo_chain_sdnode() || |
Evan Cheng | ce1381a | 2006-10-14 08:30:15 +0000 | [diff] [blame] | 381 | PInfo.getNumOperands() > 1 || |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 382 | PInfo.hasProperty(SDNPHasChain) || |
| 383 | PInfo.hasProperty(SDNPInFlag) || |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 384 | PInfo.hasProperty(SDNPOptInFlag); |
| 385 | } |
| 386 | |
| 387 | if (NeedCheck) { |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 388 | std::string ParentName(RootName.begin(), RootName.end()-1); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 389 | emitCheck("CanBeFoldedBy(" + RootName + ".Val, " + ParentName + |
Evan Cheng | ce1381a | 2006-10-14 08:30:15 +0000 | [diff] [blame] | 390 | ".Val, N.Val)"); |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 391 | } |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 392 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 393 | } |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 394 | |
Evan Cheng | c15d18c | 2006-01-27 22:13:45 +0000 | [diff] [blame] | 395 | if (NodeHasChain) { |
Evan Cheng | 4326ef5 | 2006-10-12 02:08:53 +0000 | [diff] [blame] | 396 | if (FoundChain) { |
| 397 | emitCheck("(" + ChainName + ".Val == " + RootName + ".Val || " |
| 398 | "IsChainCompatible(" + ChainName + ".Val, " + |
| 399 | RootName + ".Val))"); |
| 400 | OrigChains.push_back(std::make_pair(ChainName, RootName)); |
| 401 | } else |
Evan Cheng | e638993 | 2006-07-21 22:19:51 +0000 | [diff] [blame] | 402 | FoundChain = true; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 403 | ChainName = "Chain" + ChainSuffix; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 404 | emitInit("SDOperand " + ChainName + " = " + RootName + |
Evan Cheng | e638993 | 2006-07-21 22:19:51 +0000 | [diff] [blame] | 405 | ".getOperand(0);"); |
Evan Cheng | 1cf6db2 | 2006-01-06 00:41:12 +0000 | [diff] [blame] | 406 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 409 | // Don't fold any node which reads or writes a flag and has multiple uses. |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 410 | // FIXME: We really need to separate the concepts of flag and "glue". Those |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 411 | // real flag results, e.g. X86CMP output, can have multiple uses. |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 412 | // FIXME: If the optional incoming flag does not exist. Then it is ok to |
| 413 | // fold it. |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 414 | if (!isRoot && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 415 | (PatternHasProperty(N, SDNPInFlag, CGP) || |
| 416 | PatternHasProperty(N, SDNPOptInFlag, CGP) || |
| 417 | PatternHasProperty(N, SDNPOutFlag, CGP))) { |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 418 | if (!EmittedUseCheck) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 419 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 420 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Evan Cheng | d3eea90 | 2006-10-09 21:02:17 +0000 | [diff] [blame] | 424 | // If there is a node predicate for this, emit the call. |
| 425 | if (!N->getPredicateFn().empty()) |
| 426 | emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)"); |
| 427 | |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 39e73f7 | 2006-10-11 04:05:55 +0000 | [diff] [blame] | 429 | // If this is an 'and R, 1234' where the operation is AND/OR and the RHS is |
| 430 | // a constant without a predicate fn that has more that one bit set, handle |
| 431 | // this as a special case. This is usually for targets that have special |
| 432 | // handling of certain large constants (e.g. alpha with it's 8/16/32-bit |
| 433 | // handling stuff). Using these instructions is often far more efficient |
| 434 | // than materializing the constant. Unfortunately, both the instcombiner |
| 435 | // and the dag combiner can often infer that bits are dead, and thus drop |
| 436 | // them from the mask in the dag. For example, it might turn 'AND X, 255' |
| 437 | // into 'AND X, 254' if it knows the low bit is set. Emit code that checks |
| 438 | // to handle this. |
| 439 | if (!N->isLeaf() && |
| 440 | (N->getOperator()->getName() == "and" || |
| 441 | N->getOperator()->getName() == "or") && |
| 442 | N->getChild(1)->isLeaf() && |
| 443 | N->getChild(1)->getPredicateFn().empty()) { |
| 444 | if (IntInit *II = dynamic_cast<IntInit*>(N->getChild(1)->getLeafValue())) { |
| 445 | if (!isPowerOf2_32(II->getValue())) { // Don't bother with single bits. |
| 446 | emitInit("SDOperand " + RootName + "0" + " = " + |
| 447 | RootName + ".getOperand(" + utostr(0) + ");"); |
| 448 | emitInit("SDOperand " + RootName + "1" + " = " + |
| 449 | RootName + ".getOperand(" + utostr(1) + ");"); |
| 450 | |
| 451 | emitCheck("isa<ConstantSDNode>(" + RootName + "1)"); |
| 452 | const char *MaskPredicate = N->getOperator()->getName() == "or" |
| 453 | ? "CheckOrMask(" : "CheckAndMask("; |
| 454 | emitCheck(MaskPredicate + RootName + "0, cast<ConstantSDNode>(" + |
| 455 | RootName + "1), " + itostr(II->getValue()) + ")"); |
| 456 | |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 457 | EmitChildMatchCode(N->getChild(0), N, RootName + utostr(0), |
Chris Lattner | 39e73f7 | 2006-10-11 04:05:55 +0000 | [diff] [blame] | 458 | ChainSuffix + utostr(0), FoundChain); |
| 459 | return; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 464 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 465 | emitInit("SDOperand " + RootName + utostr(OpNo) + " = " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 466 | RootName + ".getOperand(" +utostr(OpNo) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 467 | |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 468 | EmitChildMatchCode(N->getChild(i), N, RootName + utostr(OpNo), |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 469 | ChainSuffix + utostr(OpNo), FoundChain); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 472 | // Handle cases when root is a complex pattern. |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 473 | const ComplexPattern *CP; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 474 | if (isRoot && N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 475 | std::string Fn = CP->getSelectFunc(); |
| 476 | unsigned NumOps = CP->getNumOperands(); |
| 477 | for (unsigned i = 0; i < NumOps; ++i) { |
| 478 | emitDecl("CPTmp" + utostr(i)); |
| 479 | emitCode("SDOperand CPTmp" + utostr(i) + ";"); |
| 480 | } |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 481 | if (CP->hasProperty(SDNPHasChain)) { |
| 482 | emitDecl("CPInChain"); |
| 483 | emitDecl("Chain" + ChainSuffix); |
| 484 | emitCode("SDOperand CPInChain;"); |
| 485 | emitCode("SDOperand Chain" + ChainSuffix + ";"); |
| 486 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 487 | |
Evan Cheng | 811731e | 2006-11-08 20:31:10 +0000 | [diff] [blame] | 488 | std::string Code = Fn + "(" + RootName + ", " + RootName; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 489 | for (unsigned i = 0; i < NumOps; i++) |
| 490 | Code += ", CPTmp" + utostr(i); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 491 | if (CP->hasProperty(SDNPHasChain)) { |
| 492 | ChainName = "Chain" + ChainSuffix; |
| 493 | Code += ", CPInChain, Chain" + ChainSuffix; |
| 494 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 495 | emitCheck(Code + ")"); |
| 496 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | 39e73f7 | 2006-10-11 04:05:55 +0000 | [diff] [blame] | 498 | |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 499 | void EmitChildMatchCode(TreePatternNode *Child, TreePatternNode *Parent, |
| 500 | const std::string &RootName, |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 501 | const std::string &ChainSuffix, bool &FoundChain) { |
| 502 | if (!Child->isLeaf()) { |
| 503 | // If it's not a leaf, recursively match. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 504 | const SDNodeInfo &CInfo = CGP.getSDNodeInfo(Child->getOperator()); |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 505 | emitCheck(RootName + ".getOpcode() == " + |
| 506 | CInfo.getEnumName()); |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 507 | EmitMatchCode(Child, Parent, RootName, ChainSuffix, FoundChain); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 508 | if (NodeHasProperty(Child, SDNPHasChain, CGP)) |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 509 | FoldedChains.push_back(std::make_pair(RootName, CInfo.getNumResults())); |
| 510 | } else { |
| 511 | // If this child has a name associated with it, capture it in VarMap. If |
| 512 | // we already saw this in the pattern, emit code to verify dagness. |
| 513 | if (!Child->getName().empty()) { |
| 514 | std::string &VarMapEntry = VariableMap[Child->getName()]; |
| 515 | if (VarMapEntry.empty()) { |
| 516 | VarMapEntry = RootName; |
| 517 | } else { |
| 518 | // If we get here, this is a second reference to a specific name. |
| 519 | // Since we already have checked that the first reference is valid, |
| 520 | // we don't have to recursively match it, just check that it's the |
| 521 | // same as the previously named thing. |
| 522 | emitCheck(VarMapEntry + " == " + RootName); |
| 523 | Duplicates.insert(RootName); |
| 524 | return; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Handle leaves of various types. |
| 529 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 530 | Record *LeafRec = DI->getDef(); |
Chris Lattner | 646085d | 2006-11-14 21:18:40 +0000 | [diff] [blame] | 531 | if (LeafRec->isSubClassOf("RegisterClass") || |
| 532 | LeafRec->getName() == "ptr_rc") { |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 533 | // Handle register references. Nothing to do here. |
| 534 | } else if (LeafRec->isSubClassOf("Register")) { |
| 535 | // Handle register references. |
| 536 | } else if (LeafRec->isSubClassOf("ComplexPattern")) { |
| 537 | // Handle complex pattern. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 538 | const ComplexPattern *CP = NodeGetComplexPattern(Child, CGP); |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 539 | std::string Fn = CP->getSelectFunc(); |
| 540 | unsigned NumOps = CP->getNumOperands(); |
| 541 | for (unsigned i = 0; i < NumOps; ++i) { |
| 542 | emitDecl("CPTmp" + utostr(i)); |
| 543 | emitCode("SDOperand CPTmp" + utostr(i) + ";"); |
| 544 | } |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 545 | if (CP->hasProperty(SDNPHasChain)) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 546 | const SDNodeInfo &PInfo = CGP.getSDNodeInfo(Parent->getOperator()); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 547 | FoldedChains.push_back(std::make_pair("CPInChain", |
| 548 | PInfo.getNumResults())); |
| 549 | ChainName = "Chain" + ChainSuffix; |
| 550 | emitDecl("CPInChain"); |
| 551 | emitDecl(ChainName); |
| 552 | emitCode("SDOperand CPInChain;"); |
| 553 | emitCode("SDOperand " + ChainName + ";"); |
| 554 | } |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 555 | |
Evan Cheng | 811731e | 2006-11-08 20:31:10 +0000 | [diff] [blame] | 556 | std::string Code = Fn + "(N, "; |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 557 | if (CP->hasProperty(SDNPHasChain)) { |
| 558 | std::string ParentName(RootName.begin(), RootName.end()-1); |
Evan Cheng | 811731e | 2006-11-08 20:31:10 +0000 | [diff] [blame] | 559 | Code += ParentName + ", "; |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 560 | } |
| 561 | Code += RootName; |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 562 | for (unsigned i = 0; i < NumOps; i++) |
| 563 | Code += ", CPTmp" + utostr(i); |
Evan Cheng | 94b3040 | 2006-10-11 21:02:01 +0000 | [diff] [blame] | 564 | if (CP->hasProperty(SDNPHasChain)) |
| 565 | Code += ", CPInChain, Chain" + ChainSuffix; |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 566 | emitCheck(Code + ")"); |
| 567 | } else if (LeafRec->getName() == "srcvalue") { |
| 568 | // Place holder for SRCVALUE nodes. Nothing to do here. |
| 569 | } else if (LeafRec->isSubClassOf("ValueType")) { |
| 570 | // Make sure this is the specified value type. |
| 571 | emitCheck("cast<VTSDNode>(" + RootName + |
| 572 | ")->getVT() == MVT::" + LeafRec->getName()); |
| 573 | } else if (LeafRec->isSubClassOf("CondCode")) { |
| 574 | // Make sure this is the specified cond code. |
| 575 | emitCheck("cast<CondCodeSDNode>(" + RootName + |
| 576 | ")->get() == ISD::" + LeafRec->getName()); |
| 577 | } else { |
| 578 | #ifndef NDEBUG |
| 579 | Child->dump(); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 580 | cerr << " "; |
Chris Lattner | be8e721 | 2006-10-11 03:35:34 +0000 | [diff] [blame] | 581 | #endif |
| 582 | assert(0 && "Unknown leaf type!"); |
| 583 | } |
| 584 | |
| 585 | // If there is a node predicate for this, emit the call. |
| 586 | if (!Child->getPredicateFn().empty()) |
| 587 | emitCheck(Child->getPredicateFn() + "(" + RootName + |
| 588 | ".Val)"); |
| 589 | } else if (IntInit *II = |
| 590 | dynamic_cast<IntInit*>(Child->getLeafValue())) { |
| 591 | emitCheck("isa<ConstantSDNode>(" + RootName + ")"); |
| 592 | unsigned CTmp = TmpNo++; |
| 593 | emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+ |
| 594 | RootName + ")->getSignExtended();"); |
| 595 | |
| 596 | emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue())); |
| 597 | } else { |
| 598 | #ifndef NDEBUG |
| 599 | Child->dump(); |
| 600 | #endif |
| 601 | assert(0 && "Unknown leaf type!"); |
| 602 | } |
| 603 | } |
| 604 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 605 | |
| 606 | /// EmitResultCode - Emit the action for a pattern. Now that it has matched |
| 607 | /// we actually have to build a DAG! |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 608 | std::vector<std::string> |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 609 | EmitResultCode(TreePatternNode *N, std::vector<Record*> DstRegs, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 610 | bool InFlagDecled, bool ResNodeDecled, |
| 611 | bool LikeLeaf = false, bool isRoot = false) { |
| 612 | // List of arguments of getTargetNode() or SelectNodeTo(). |
| 613 | std::vector<std::string> NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 614 | // This is something selected from the pattern we matched. |
| 615 | if (!N->getName().empty()) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 616 | std::string &Val = VariableMap[N->getName()]; |
| 617 | assert(!Val.empty() && |
| 618 | "Variable referenced but not defined and not caught earlier!"); |
| 619 | if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { |
| 620 | // Already selected this operand, just return the tmpval. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 621 | NodeOps.push_back(Val); |
| 622 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | const ComplexPattern *CP; |
| 626 | unsigned ResNo = TmpNo++; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 627 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 628 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 629 | std::string CastType; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 630 | switch (N->getTypeNum(0)) { |
Chris Lattner | d8a1728 | 2007-01-17 07:45:12 +0000 | [diff] [blame] | 631 | default: |
| 632 | cerr << "Cannot handle " << getEnumName(N->getTypeNum(0)) |
| 633 | << " type as an immediate constant. Aborting\n"; |
| 634 | abort(); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 635 | case MVT::i1: CastType = "bool"; break; |
| 636 | case MVT::i8: CastType = "unsigned char"; break; |
| 637 | case MVT::i16: CastType = "unsigned short"; break; |
| 638 | case MVT::i32: CastType = "unsigned"; break; |
| 639 | case MVT::i64: CastType = "uint64_t"; break; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 640 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 641 | emitCode("SDOperand Tmp" + utostr(ResNo) + |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 642 | " = CurDAG->getTargetConstant(((" + CastType + |
| 643 | ") cast<ConstantSDNode>(" + Val + ")->getValue()), " + |
| 644 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 645 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 646 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 647 | // value if used multiple times by this pattern result. |
| 648 | Val = "Tmp"+utostr(ResNo); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 649 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 650 | Record *Op = OperatorMap[N->getName()]; |
| 651 | // Transform ExternalSymbol to TargetExternalSymbol |
| 652 | if (Op && Op->getName() == "externalsym") { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 653 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 654 | "ExternalSymbol(cast<ExternalSymbolSDNode>(" + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 655 | Val + ")->getSymbol(), " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 656 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 657 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
Chris Lattner | 6490697 | 2006-09-21 18:28:27 +0000 | [diff] [blame] | 658 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select |
| 659 | // this value if used multiple times by this pattern result. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 660 | Val = "Tmp"+utostr(ResNo); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 661 | } else { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 662 | NodeOps.push_back(Val); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 663 | } |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 664 | } else if (!N->isLeaf() && (N->getOperator()->getName() == "tglobaladdr" |
| 665 | || N->getOperator()->getName() == "tglobaltlsaddr")) { |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 666 | Record *Op = OperatorMap[N->getName()]; |
| 667 | // Transform GlobalAddress to TargetGlobalAddress |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 668 | if (Op && (Op->getName() == "globaladdr" || |
| 669 | Op->getName() == "globaltlsaddr")) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 670 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 671 | "GlobalAddress(cast<GlobalAddressSDNode>(" + Val + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 672 | ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 673 | ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 674 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
Chris Lattner | 6490697 | 2006-09-21 18:28:27 +0000 | [diff] [blame] | 675 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select |
| 676 | // this value if used multiple times by this pattern result. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 677 | Val = "Tmp"+utostr(ResNo); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 678 | } else { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 679 | NodeOps.push_back(Val); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 680 | } |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 681 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 682 | NodeOps.push_back(Val); |
| 683 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 684 | // value if used multiple times by this pattern result. |
| 685 | Val = "Tmp"+utostr(ResNo); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 686 | } else if (!N->isLeaf() && N->getOperator()->getName() == "tconstpool") { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 687 | NodeOps.push_back(Val); |
| 688 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 689 | // value if used multiple times by this pattern result. |
| 690 | Val = "Tmp"+utostr(ResNo); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 691 | } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, CGP))) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 692 | for (unsigned i = 0; i < CP->getNumOperands(); ++i) { |
| 693 | emitCode("AddToISelQueue(CPTmp" + utostr(i) + ");"); |
| 694 | NodeOps.push_back("CPTmp" + utostr(i)); |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 695 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 696 | } else { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 697 | // This node, probably wrapped in a SDNodeXForm, behaves like a leaf |
Evan Cheng | 863bf5a | 2006-03-20 22:53:06 +0000 | [diff] [blame] | 698 | // node even if it isn't one. Don't select it. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 699 | if (!LikeLeaf) { |
| 700 | emitCode("AddToISelQueue(" + Val + ");"); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 701 | if (isRoot && N->isLeaf()) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 702 | emitCode("ReplaceUses(N, " + Val + ");"); |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 703 | emitCode("return NULL;"); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 704 | } |
Evan Cheng | 83e1a6a | 2006-03-23 02:35:32 +0000 | [diff] [blame] | 705 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 706 | NodeOps.push_back(Val); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 707 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 708 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 709 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 710 | if (N->isLeaf()) { |
| 711 | // If this is an explicit register reference, handle it. |
| 712 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 713 | unsigned ResNo = TmpNo++; |
| 714 | if (DI->getDef()->isSubClassOf("Register")) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 715 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" + |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 716 | getQualifiedName(DI->getDef()) + ", " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 717 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 718 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 719 | return NodeOps; |
Evan Cheng | 7774be4 | 2007-07-05 07:19:45 +0000 | [diff] [blame] | 720 | } else if (DI->getDef()->getName() == "zero_reg") { |
| 721 | emitCode("SDOperand Tmp" + utostr(ResNo) + |
| 722 | " = CurDAG->getRegister(0, " + |
| 723 | getEnumName(N->getTypeNum(0)) + ");"); |
| 724 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 725 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 726 | } |
| 727 | } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 728 | unsigned ResNo = TmpNo++; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 729 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 730 | emitCode("SDOperand Tmp" + utostr(ResNo) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 731 | " = CurDAG->getTargetConstant(" + itostr(II->getValue()) + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 732 | ", " + getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 733 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 734 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 737 | #ifndef NDEBUG |
| 738 | N->dump(); |
| 739 | #endif |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 740 | assert(0 && "Unknown leaf type!"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 741 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | Record *Op = N->getOperator(); |
| 745 | if (Op->isSubClassOf("Instruction")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 746 | const CodeGenTarget &CGT = CGP.getTargetInfo(); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 747 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 748 | const DAGInstruction &Inst = CGP.getInstruction(Op); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 749 | TreePattern *InstPat = Inst.getPattern(); |
Evan Cheng | d23aa5a | 2007-09-25 01:48:59 +0000 | [diff] [blame] | 750 | // FIXME: Assume actual pattern comes before "implicit". |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 751 | TreePatternNode *InstPatNode = |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 752 | isRoot ? (InstPat ? InstPat->getTree(0) : Pattern) |
| 753 | : (InstPat ? InstPat->getTree(0) : NULL); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 754 | if (InstPatNode && InstPatNode->getOperator()->getName() == "set") { |
Evan Cheng | aeb7d4d | 2007-09-11 19:52:18 +0000 | [diff] [blame] | 755 | InstPatNode = InstPatNode->getChild(InstPatNode->getNumChildren()-1); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 756 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 757 | bool HasVarOps = isRoot && II.hasVariableNumberOfOperands; |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 758 | // FIXME: fix how we deal with physical register operands. |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 759 | bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0; |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 760 | bool HasImpResults = isRoot && DstRegs.size() > 0; |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 761 | bool NodeHasOptInFlag = isRoot && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 762 | PatternHasProperty(Pattern, SDNPOptInFlag, CGP); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 763 | bool NodeHasInFlag = isRoot && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 764 | PatternHasProperty(Pattern, SDNPInFlag, CGP); |
Evan Cheng | ef61ed3 | 2007-09-07 23:59:02 +0000 | [diff] [blame] | 765 | bool NodeHasOutFlag = isRoot && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 766 | PatternHasProperty(Pattern, SDNPOutFlag, CGP); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 767 | bool NodeHasChain = InstPatNode && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 768 | PatternHasProperty(InstPatNode, SDNPHasChain, CGP); |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 769 | bool InputHasChain = isRoot && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 770 | NodeHasProperty(Pattern, SDNPHasChain, CGP); |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 771 | unsigned NumResults = Inst.getNumResults(); |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 772 | unsigned NumDstRegs = HasImpResults ? DstRegs.size() : 0; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 773 | |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 774 | if (NodeHasOptInFlag) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 775 | emitCode("bool HasInFlag = " |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 776 | "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);"); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 777 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 778 | if (HasVarOps) |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 779 | emitCode("SmallVector<SDOperand, 8> Ops" + utostr(OpcNo) + ";"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 780 | |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 781 | // How many results is this pattern expected to produce? |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 782 | unsigned NumPatResults = 0; |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 783 | for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) { |
| 784 | MVT::ValueType VT = Pattern->getTypeNum(i); |
| 785 | if (VT != MVT::isVoid && VT != MVT::Flag) |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 786 | NumPatResults++; |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Evan Cheng | 4326ef5 | 2006-10-12 02:08:53 +0000 | [diff] [blame] | 789 | if (OrigChains.size() > 0) { |
| 790 | // The original input chain is being ignored. If it is not just |
| 791 | // pointing to the op that's being folded, we should create a |
| 792 | // TokenFactor with it and the chain of the folded op as the new chain. |
| 793 | // We could potentially be doing multiple levels of folding, in that |
| 794 | // case, the TokenFactor can have more operands. |
| 795 | emitCode("SmallVector<SDOperand, 8> InChains;"); |
| 796 | for (unsigned i = 0, e = OrigChains.size(); i < e; ++i) { |
| 797 | emitCode("if (" + OrigChains[i].first + ".Val != " + |
| 798 | OrigChains[i].second + ".Val) {"); |
| 799 | emitCode(" AddToISelQueue(" + OrigChains[i].first + ");"); |
| 800 | emitCode(" InChains.push_back(" + OrigChains[i].first + ");"); |
| 801 | emitCode("}"); |
| 802 | } |
| 803 | emitCode("AddToISelQueue(" + ChainName + ");"); |
| 804 | emitCode("InChains.push_back(" + ChainName + ");"); |
| 805 | emitCode(ChainName + " = CurDAG->getNode(ISD::TokenFactor, MVT::Other, " |
| 806 | "&InChains[0], InChains.size());"); |
| 807 | } |
| 808 | |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 809 | // Loop over all of the operands of the instruction pattern, emitting code |
| 810 | // to fill them all in. The node 'N' usually has number children equal to |
| 811 | // the number of input operands of the instruction. However, in cases |
| 812 | // where there are predicate operands for an instruction, we need to fill |
| 813 | // in the 'execute always' values. Match up the node operands to the |
| 814 | // instruction operands to do this. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 815 | std::vector<std::string> AllOps; |
Evan Cheng | 39376d0 | 2007-05-15 01:19:51 +0000 | [diff] [blame] | 816 | unsigned NumEAInputs = 0; // # of synthesized 'execute always' inputs. |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 817 | for (unsigned ChildNo = 0, InstOpNo = NumResults; |
| 818 | InstOpNo != II.OperandList.size(); ++InstOpNo) { |
| 819 | std::vector<std::string> Ops; |
| 820 | |
Evan Cheng | 5903963 | 2007-05-08 21:04:07 +0000 | [diff] [blame] | 821 | // If this is a normal operand or a predicate operand without |
| 822 | // 'execute always', emit it. |
| 823 | Record *OperandNode = II.OperandList[InstOpNo].Rec; |
Evan Cheng | a955939 | 2007-07-06 01:05:26 +0000 | [diff] [blame] | 824 | if ((!OperandNode->isSubClassOf("PredicateOperand") && |
| 825 | !OperandNode->isSubClassOf("OptionalDefOperand")) || |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 826 | CGP.getDefaultOperand(OperandNode).DefaultOps.empty()) { |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 827 | Ops = EmitResultCode(N->getChild(ChildNo), DstRegs, |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 828 | InFlagDecled, ResNodeDecled); |
| 829 | AllOps.insert(AllOps.end(), Ops.begin(), Ops.end()); |
| 830 | ++ChildNo; |
| 831 | } else { |
Evan Cheng | a955939 | 2007-07-06 01:05:26 +0000 | [diff] [blame] | 832 | // Otherwise, this is a predicate or optional def operand, emit the |
| 833 | // 'default ops' operands. |
| 834 | const DAGDefaultOperand &DefaultOp = |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 835 | CGP.getDefaultOperand(II.OperandList[InstOpNo].Rec); |
Evan Cheng | a955939 | 2007-07-06 01:05:26 +0000 | [diff] [blame] | 836 | for (unsigned i = 0, e = DefaultOp.DefaultOps.size(); i != e; ++i) { |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 837 | Ops = EmitResultCode(DefaultOp.DefaultOps[i], DstRegs, |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 838 | InFlagDecled, ResNodeDecled); |
| 839 | AllOps.insert(AllOps.end(), Ops.begin(), Ops.end()); |
Evan Cheng | 39376d0 | 2007-05-15 01:19:51 +0000 | [diff] [blame] | 840 | NumEAInputs += Ops.size(); |
Chris Lattner | efe9f4a | 2006-11-04 05:12:02 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 845 | // Emit all the chain and CopyToReg stuff. |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 846 | bool ChainEmitted = NodeHasChain; |
| 847 | if (NodeHasChain) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 848 | emitCode("AddToISelQueue(" + ChainName + ");"); |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 849 | if (NodeHasInFlag || HasImpInputs) |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 850 | EmitInFlagSelectCode(Pattern, "N", ChainEmitted, |
| 851 | InFlagDecled, ResNodeDecled, true); |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 852 | if (NodeHasOptInFlag || NodeHasInFlag || HasImpInputs) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 853 | if (!InFlagDecled) { |
| 854 | emitCode("SDOperand InFlag(0, 0);"); |
| 855 | InFlagDecled = true; |
| 856 | } |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 857 | if (NodeHasOptInFlag) { |
| 858 | emitCode("if (HasInFlag) {"); |
| 859 | emitCode(" InFlag = N.getOperand(N.getNumOperands()-1);"); |
| 860 | emitCode(" AddToISelQueue(InFlag);"); |
| 861 | emitCode("}"); |
| 862 | } |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 863 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 864 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 865 | unsigned ResNo = TmpNo++; |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 866 | if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag || |
Evan Cheng | ef61ed3 | 2007-09-07 23:59:02 +0000 | [diff] [blame] | 867 | NodeHasOptInFlag || HasImpResults) { |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 868 | std::string Code; |
| 869 | std::string Code2; |
| 870 | std::string NodeName; |
| 871 | if (!isRoot) { |
| 872 | NodeName = "Tmp" + utostr(ResNo); |
Dan Gohman | a6a1ab3 | 2007-07-24 22:58:00 +0000 | [diff] [blame] | 873 | Code2 = "SDOperand " + NodeName + "("; |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 874 | } else { |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 875 | NodeName = "ResNode"; |
Lauro Ramos Venancio | 195c6c2 | 2007-04-26 17:03:22 +0000 | [diff] [blame] | 876 | if (!ResNodeDecled) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 877 | Code2 = "SDNode *" + NodeName + " = "; |
Lauro Ramos Venancio | 195c6c2 | 2007-04-26 17:03:22 +0000 | [diff] [blame] | 878 | ResNodeDecled = true; |
| 879 | } else |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 880 | Code2 = NodeName + " = "; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 881 | } |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 882 | |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 883 | Code += "CurDAG->getTargetNode(Opc" + utostr(OpcNo); |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 884 | unsigned OpsNo = OpcNo; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 885 | emitOpcode(II.Namespace + "::" + II.TheDef->getName()); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 886 | |
| 887 | // Output order: results, chain, flags |
| 888 | // Result types. |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 889 | if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) { |
| 890 | Code += ", VT" + utostr(VTNo); |
| 891 | emitVT(getEnumName(N->getTypeNum(0))); |
| 892 | } |
Evan Cheng | ef61ed3 | 2007-09-07 23:59:02 +0000 | [diff] [blame] | 893 | // Add types for implicit results in physical registers, scheduler will |
| 894 | // care of adding copyfromreg nodes. |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 895 | for (unsigned i = 0; i < NumDstRegs; i++) { |
| 896 | Record *RR = DstRegs[i]; |
| 897 | if (RR->isSubClassOf("Register")) { |
| 898 | MVT::ValueType RVT = getRegisterValueType(RR, CGT); |
| 899 | Code += ", " + getEnumName(RVT); |
Evan Cheng | ef61ed3 | 2007-09-07 23:59:02 +0000 | [diff] [blame] | 900 | } |
| 901 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 902 | if (NodeHasChain) |
| 903 | Code += ", MVT::Other"; |
| 904 | if (NodeHasOutFlag) |
| 905 | Code += ", MVT::Flag"; |
| 906 | |
Chris Lattner | 7c3a96b | 2006-11-14 18:41:38 +0000 | [diff] [blame] | 907 | // Figure out how many fixed inputs the node has. This is important to |
| 908 | // know which inputs are the variable ones if present. |
| 909 | unsigned NumInputs = AllOps.size(); |
| 910 | NumInputs += NodeHasChain; |
| 911 | |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 912 | // Inputs. |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 913 | if (HasVarOps) { |
| 914 | for (unsigned i = 0, e = AllOps.size(); i != e; ++i) |
| 915 | emitCode("Ops" + utostr(OpsNo) + ".push_back(" + AllOps[i] + ");"); |
| 916 | AllOps.clear(); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | if (HasVarOps) { |
Chris Lattner | 7c3a96b | 2006-11-14 18:41:38 +0000 | [diff] [blame] | 920 | // Figure out whether any operands at the end of the op list are not |
| 921 | // part of the variable section. |
| 922 | std::string EndAdjust; |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 923 | if (NodeHasInFlag || HasImpInputs) |
Chris Lattner | 7c3a96b | 2006-11-14 18:41:38 +0000 | [diff] [blame] | 924 | EndAdjust = "-1"; // Always has one flag. |
| 925 | else if (NodeHasOptInFlag) |
| 926 | EndAdjust = "-(HasInFlag?1:0)"; // May have a flag. |
| 927 | |
Evan Cheng | 39376d0 | 2007-05-15 01:19:51 +0000 | [diff] [blame] | 928 | emitCode("for (unsigned i = " + utostr(NumInputs - NumEAInputs) + |
Chris Lattner | 7c3a96b | 2006-11-14 18:41:38 +0000 | [diff] [blame] | 929 | ", e = N.getNumOperands()" + EndAdjust + "; i != e; ++i) {"); |
| 930 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 931 | emitCode(" AddToISelQueue(N.getOperand(i));"); |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 932 | emitCode(" Ops" + utostr(OpsNo) + ".push_back(N.getOperand(i));"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 933 | emitCode("}"); |
| 934 | } |
| 935 | |
| 936 | if (NodeHasChain) { |
| 937 | if (HasVarOps) |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 938 | emitCode("Ops" + utostr(OpsNo) + ".push_back(" + ChainName + ");"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 939 | else |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 940 | AllOps.push_back(ChainName); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 943 | if (HasVarOps) { |
| 944 | if (NodeHasInFlag || HasImpInputs) |
| 945 | emitCode("Ops" + utostr(OpsNo) + ".push_back(InFlag);"); |
| 946 | else if (NodeHasOptInFlag) { |
| 947 | emitCode("if (HasInFlag)"); |
| 948 | emitCode(" Ops" + utostr(OpsNo) + ".push_back(InFlag);"); |
| 949 | } |
| 950 | Code += ", &Ops" + utostr(OpsNo) + "[0], Ops" + utostr(OpsNo) + |
| 951 | ".size()"; |
| 952 | } else if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs) |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 953 | AllOps.push_back("InFlag"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 954 | |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 955 | unsigned NumOps = AllOps.size(); |
| 956 | if (NumOps) { |
| 957 | if (!NodeHasOptInFlag && NumOps < 4) { |
| 958 | for (unsigned i = 0; i != NumOps; ++i) |
| 959 | Code += ", " + AllOps[i]; |
| 960 | } else { |
| 961 | std::string OpsCode = "SDOperand Ops" + utostr(OpsNo) + "[] = { "; |
| 962 | for (unsigned i = 0; i != NumOps; ++i) { |
| 963 | OpsCode += AllOps[i]; |
| 964 | if (i != NumOps-1) |
| 965 | OpsCode += ", "; |
| 966 | } |
| 967 | emitCode(OpsCode + " };"); |
| 968 | Code += ", Ops" + utostr(OpsNo) + ", "; |
| 969 | if (NodeHasOptInFlag) { |
| 970 | Code += "HasInFlag ? "; |
| 971 | Code += utostr(NumOps) + " : " + utostr(NumOps-1); |
| 972 | } else |
| 973 | Code += utostr(NumOps); |
| 974 | } |
| 975 | } |
| 976 | |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 977 | if (!isRoot) |
| 978 | Code += "), 0"; |
| 979 | emitCode(Code2 + Code + ");"); |
| 980 | |
| 981 | if (NodeHasChain) |
| 982 | // Remember which op produces the chain. |
| 983 | if (!isRoot) |
| 984 | emitCode(ChainName + " = SDOperand(" + NodeName + |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 985 | ".Val, " + utostr(NumResults+NumDstRegs) + ");"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 986 | else |
| 987 | emitCode(ChainName + " = SDOperand(" + NodeName + |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 988 | ", " + utostr(NumResults+NumDstRegs) + ");"); |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 989 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 990 | if (!isRoot) { |
| 991 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
| 992 | return NodeOps; |
| 993 | } |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 994 | |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 995 | bool NeedReplace = false; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 996 | if (NodeHasOutFlag) { |
| 997 | if (!InFlagDecled) { |
Dan Gohman | a6a1ab3 | 2007-07-24 22:58:00 +0000 | [diff] [blame] | 998 | emitCode("SDOperand InFlag(ResNode, " + |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 999 | utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1000 | InFlagDecled = true; |
| 1001 | } else |
| 1002 | emitCode("InFlag = SDOperand(ResNode, " + |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1003 | utostr(NumResults+NumDstRegs+(unsigned)NodeHasChain) + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1004 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 1005 | |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 1006 | if (FoldedChains.size() > 0) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1007 | std::string Code; |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 1008 | for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1009 | emitCode("ReplaceUses(SDOperand(" + |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 1010 | FoldedChains[j].first + ".Val, " + |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1011 | utostr(FoldedChains[j].second) + "), SDOperand(ResNode, " + |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1012 | utostr(NumResults+NumDstRegs) + "));"); |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1013 | NeedReplace = true; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1014 | } |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 1015 | |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1016 | if (NodeHasOutFlag) { |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1017 | emitCode("ReplaceUses(SDOperand(N.Val, " + |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1018 | utostr(NumPatResults + (unsigned)InputHasChain) |
| 1019 | +"), InFlag);"); |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1020 | NeedReplace = true; |
| 1021 | } |
| 1022 | |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1023 | if (NeedReplace && InputHasChain) |
| 1024 | emitCode("ReplaceUses(SDOperand(N.Val, " + |
| 1025 | utostr(NumPatResults) + "), SDOperand(" + ChainName |
| 1026 | + ".Val, " + ChainName + ".ResNo" + "));"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 1027 | |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 1028 | // User does not expect the instruction would produce a chain! |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1029 | if ((!InputHasChain && NodeHasChain) && NodeHasOutFlag) { |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1030 | ; |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 1031 | } else if (InputHasChain && !NodeHasChain) { |
| 1032 | // One of the inner node produces a chain. |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1033 | if (NodeHasOutFlag) |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1034 | emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults+1) + |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1035 | "), SDOperand(ResNode, N.ResNo-1));"); |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1036 | emitCode("ReplaceUses(SDOperand(N.Val, " + utostr(NumPatResults) + |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1037 | "), " + ChainName + ");"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 1038 | } |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1039 | |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1040 | emitCode("return ResNode;"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1041 | } else { |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1042 | std::string Code = "return CurDAG->SelectNodeTo(N.Val, Opc" + |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 1043 | utostr(OpcNo); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1044 | if (N->getTypeNum(0) != MVT::isVoid) |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 1045 | Code += ", VT" + utostr(VTNo); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1046 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1047 | Code += ", MVT::Flag"; |
Evan Cheng | f037ca6 | 2006-08-27 08:11:28 +0000 | [diff] [blame] | 1048 | |
| 1049 | if (NodeHasInFlag || NodeHasOptInFlag || HasImpInputs) |
| 1050 | AllOps.push_back("InFlag"); |
| 1051 | |
| 1052 | unsigned NumOps = AllOps.size(); |
| 1053 | if (NumOps) { |
| 1054 | if (!NodeHasOptInFlag && NumOps < 4) { |
| 1055 | for (unsigned i = 0; i != NumOps; ++i) |
| 1056 | Code += ", " + AllOps[i]; |
| 1057 | } else { |
| 1058 | std::string OpsCode = "SDOperand Ops" + utostr(OpcNo) + "[] = { "; |
| 1059 | for (unsigned i = 0; i != NumOps; ++i) { |
| 1060 | OpsCode += AllOps[i]; |
| 1061 | if (i != NumOps-1) |
| 1062 | OpsCode += ", "; |
| 1063 | } |
| 1064 | emitCode(OpsCode + " };"); |
| 1065 | Code += ", Ops" + utostr(OpcNo) + ", "; |
| 1066 | Code += utostr(NumOps); |
| 1067 | } |
| 1068 | } |
Evan Cheng | 95514ba | 2006-08-26 08:00:10 +0000 | [diff] [blame] | 1069 | emitCode(Code + ");"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1070 | emitOpcode(II.Namespace + "::" + II.TheDef->getName()); |
| 1071 | if (N->getTypeNum(0) != MVT::isVoid) |
| 1072 | emitVT(getEnumName(N->getTypeNum(0))); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1073 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 1074 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1075 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1076 | } else if (Op->isSubClassOf("SDNodeXForm")) { |
| 1077 | assert(N->getNumChildren() == 1 && "node xform should have one child!"); |
Evan Cheng | 863bf5a | 2006-03-20 22:53:06 +0000 | [diff] [blame] | 1078 | // PatLeaf node - the operand may or may not be a leaf node. But it should |
| 1079 | // behave like one. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1080 | std::vector<std::string> Ops = |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1081 | EmitResultCode(N->getChild(0), DstRegs, InFlagDecled, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1082 | ResNodeDecled, true); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1083 | unsigned ResNo = TmpNo++; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1084 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName() |
| 1085 | + "(" + Ops.back() + ".Val);"); |
| 1086 | NodeOps.push_back("Tmp" + utostr(ResNo)); |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1087 | if (isRoot) |
| 1088 | emitCode("return Tmp" + utostr(ResNo) + ".Val;"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1089 | return NodeOps; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1090 | } else { |
| 1091 | N->dump(); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1092 | cerr << "\n"; |
Chris Lattner | 7893f13 | 2006-01-11 01:33:49 +0000 | [diff] [blame] | 1093 | throw std::string("Unknown node in result pattern!"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1097 | /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' |
| 1098 | /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1099 | /// 'Pat' may be missing types. If we find an unresolved type to add a check |
| 1100 | /// for, this returns true otherwise false if Pat has all types. |
| 1101 | bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1102 | const std::string &Prefix, bool isRoot = false) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1103 | // Did we find one? |
Evan Cheng | d15531b | 2006-05-19 07:24:32 +0000 | [diff] [blame] | 1104 | if (Pat->getExtTypes() != Other->getExtTypes()) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1105 | // Move a type over from 'other' to 'pat'. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1106 | Pat->setTypes(Other->getExtTypes()); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1107 | // The top level node type is checked outside of the select function. |
| 1108 | if (!isRoot) |
| 1109 | emitCheck(Prefix + ".Val->getValueType(0) == " + |
| 1110 | getName(Pat->getTypeNum(0))); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1111 | return true; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1114 | unsigned OpNo = |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1115 | (unsigned) NodeHasProperty(Pat, SDNPHasChain, CGP); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1116 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo) |
| 1117 | if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i), |
| 1118 | Prefix + utostr(OpNo))) |
| 1119 | return true; |
| 1120 | return false; |
| 1121 | } |
| 1122 | |
| 1123 | private: |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1124 | /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1125 | /// being built. |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1126 | void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1127 | bool &ChainEmitted, bool &InFlagDecled, |
| 1128 | bool &ResNodeDecled, bool isRoot = false) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1129 | const CodeGenTarget &T = CGP.getTargetInfo(); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1130 | unsigned OpNo = |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1131 | (unsigned) NodeHasProperty(N, SDNPHasChain, CGP); |
| 1132 | bool HasInFlag = NodeHasProperty(N, SDNPInFlag, CGP); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1133 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 1134 | TreePatternNode *Child = N->getChild(i); |
| 1135 | if (!Child->isLeaf()) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1136 | EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted, |
| 1137 | InFlagDecled, ResNodeDecled); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1138 | } else { |
| 1139 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 1140 | if (!Child->getName().empty()) { |
| 1141 | std::string Name = RootName + utostr(OpNo); |
| 1142 | if (Duplicates.find(Name) != Duplicates.end()) |
| 1143 | // A duplicate! Do not emit a copy for this node. |
| 1144 | continue; |
| 1145 | } |
| 1146 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1147 | Record *RR = DI->getDef(); |
| 1148 | if (RR->isSubClassOf("Register")) { |
| 1149 | MVT::ValueType RVT = getRegisterValueType(RR, T); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1150 | if (RVT == MVT::Flag) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1151 | if (!InFlagDecled) { |
| 1152 | emitCode("SDOperand InFlag = " + RootName + utostr(OpNo) + ";"); |
| 1153 | InFlagDecled = true; |
| 1154 | } else |
| 1155 | emitCode("InFlag = " + RootName + utostr(OpNo) + ";"); |
| 1156 | emitCode("AddToISelQueue(InFlag);"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 1157 | } else { |
| 1158 | if (!ChainEmitted) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1159 | emitCode("SDOperand Chain = CurDAG->getEntryNode();"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 1160 | ChainName = "Chain"; |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 1161 | ChainEmitted = true; |
| 1162 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1163 | emitCode("AddToISelQueue(" + RootName + utostr(OpNo) + ");"); |
| 1164 | if (!InFlagDecled) { |
| 1165 | emitCode("SDOperand InFlag(0, 0);"); |
| 1166 | InFlagDecled = true; |
| 1167 | } |
| 1168 | std::string Decl = (!ResNodeDecled) ? "SDNode *" : ""; |
| 1169 | emitCode(Decl + "ResNode = CurDAG->getCopyToReg(" + ChainName + |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1170 | ", " + getQualifiedName(RR) + |
Evan Cheng | 7a33db0 | 2006-08-26 07:39:28 +0000 | [diff] [blame] | 1171 | ", " + RootName + utostr(OpNo) + ", InFlag).Val;"); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1172 | ResNodeDecled = true; |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 1173 | emitCode(ChainName + " = SDOperand(ResNode, 0);"); |
| 1174 | emitCode("InFlag = SDOperand(ResNode, 1);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1180 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1181 | if (HasInFlag) { |
| 1182 | if (!InFlagDecled) { |
| 1183 | emitCode("SDOperand InFlag = " + RootName + |
| 1184 | ".getOperand(" + utostr(OpNo) + ");"); |
| 1185 | InFlagDecled = true; |
| 1186 | } else |
| 1187 | emitCode("InFlag = " + RootName + |
| 1188 | ".getOperand(" + utostr(OpNo) + ");"); |
| 1189 | emitCode("AddToISelQueue(InFlag);"); |
| 1190 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1191 | } |
| 1192 | }; |
| 1193 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1194 | /// EmitCodeForPattern - Given a pattern to match, emit code to the specified |
| 1195 | /// stream to match the pattern, and generate the code for the match if it |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 1196 | /// succeeds. Returns true if the pattern is not guaranteed to match. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1197 | void DAGISelEmitter::GenerateCodeForPattern(const PatternToMatch &Pattern, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1198 | std::vector<std::pair<unsigned, std::string> > &GeneratedCode, |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1199 | std::set<std::string> &GeneratedDecl, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 1200 | std::vector<std::string> &TargetOpcodes, |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1201 | std::vector<std::string> &TargetVTs) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1202 | PatternCodeEmitter Emitter(*CGP, Pattern.getPredicates(), |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1203 | Pattern.getSrcPattern(), Pattern.getDstPattern(), |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 1204 | GeneratedCode, GeneratedDecl, |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1205 | TargetOpcodes, TargetVTs); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1207 | // Emit the matcher, capturing named arguments in VariableMap. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1208 | bool FoundChain = false; |
Evan Cheng | 13e9e9c | 2006-10-16 06:33:44 +0000 | [diff] [blame] | 1209 | Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", FoundChain); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1210 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1211 | // TP - Get *SOME* tree pattern, we don't care which. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1212 | TreePattern &TP = *CGP->pf_begin()->second; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1213 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1214 | // At this point, we know that we structurally match the pattern, but the |
| 1215 | // types of the nodes may not match. Figure out the fewest number of type |
| 1216 | // comparisons we need to emit. For example, if there is only one integer |
| 1217 | // type supported by a target, there should be no type comparisons at all for |
| 1218 | // integer patterns! |
| 1219 | // |
| 1220 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 1221 | // remove the types, then perform type inference on the pattern as a whole. |
| 1222 | // If there are unresolved types, emit an explicit check for those types, |
| 1223 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 1224 | // types are resolved. |
| 1225 | // |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1226 | TreePatternNode *Pat = Pattern.getSrcPattern()->clone(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1227 | RemoveAllTypes(Pat); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1228 | |
| 1229 | do { |
| 1230 | // Resolve/propagate as many types as possible. |
| 1231 | try { |
| 1232 | bool MadeChange = true; |
| 1233 | while (MadeChange) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1234 | MadeChange = Pat->ApplyTypeConstraints(TP, |
| 1235 | true/*Ignore reg constraints*/); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1236 | } catch (...) { |
| 1237 | assert(0 && "Error: could not find consistent types for something we" |
| 1238 | " already decided was ok!"); |
| 1239 | abort(); |
| 1240 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 1242 | // Insert a check for an unresolved type and add it to the tree. If we find |
| 1243 | // an unresolved type to add a check for, this returns true and we iterate, |
| 1244 | // otherwise we are done. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1245 | } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N", true)); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1246 | |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1247 | Emitter.EmitResultCode(Pattern.getDstPattern(), Pattern.getDstRegs(), |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1248 | false, false, false, true); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1249 | delete Pat; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1252 | /// EraseCodeLine - Erase one code line from all of the patterns. If removing |
| 1253 | /// a line causes any of them to be empty, remove them and return true when |
| 1254 | /// done. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1255 | static bool EraseCodeLine(std::vector<std::pair<const PatternToMatch*, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1256 | std::vector<std::pair<unsigned, std::string> > > > |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1257 | &Patterns) { |
| 1258 | bool ErasedPatterns = false; |
| 1259 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1260 | Patterns[i].second.pop_back(); |
| 1261 | if (Patterns[i].second.empty()) { |
| 1262 | Patterns.erase(Patterns.begin()+i); |
| 1263 | --i; --e; |
| 1264 | ErasedPatterns = true; |
| 1265 | } |
| 1266 | } |
| 1267 | return ErasedPatterns; |
| 1268 | } |
| 1269 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1270 | /// EmitPatterns - Emit code for at least one pattern, but try to group common |
| 1271 | /// code together between the patterns. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1272 | void DAGISelEmitter::EmitPatterns(std::vector<std::pair<const PatternToMatch*, |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1273 | std::vector<std::pair<unsigned, std::string> > > > |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1274 | &Patterns, unsigned Indent, |
| 1275 | std::ostream &OS) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1276 | typedef std::pair<unsigned, std::string> CodeLine; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1277 | typedef std::vector<CodeLine> CodeList; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1278 | typedef std::vector<std::pair<const PatternToMatch*, CodeList> > PatternList; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1279 | |
| 1280 | if (Patterns.empty()) return; |
| 1281 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1282 | // Figure out how many patterns share the next code line. Explicitly copy |
| 1283 | // FirstCodeLine so that we don't invalidate a reference when changing |
| 1284 | // Patterns. |
| 1285 | const CodeLine FirstCodeLine = Patterns.back().second.back(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1286 | unsigned LastMatch = Patterns.size()-1; |
| 1287 | while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine) |
| 1288 | --LastMatch; |
| 1289 | |
| 1290 | // If not all patterns share this line, split the list into two pieces. The |
| 1291 | // first chunk will use this line, the second chunk won't. |
| 1292 | if (LastMatch != 0) { |
| 1293 | PatternList Shared(Patterns.begin()+LastMatch, Patterns.end()); |
| 1294 | PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch); |
| 1295 | |
| 1296 | // FIXME: Emit braces? |
| 1297 | if (Shared.size() == 1) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1298 | const PatternToMatch &Pattern = *Shared.back().first; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1299 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 1300 | Pattern.getSrcPattern()->print(OS); |
| 1301 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 1302 | Pattern.getDstPattern()->print(OS); |
| 1303 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1304 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1305 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1306 | << getPatternSize(Pattern.getSrcPattern(), *CGP) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1307 | << " cost = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1308 | << getResultPatternCost(Pattern.getDstPattern(), *CGP) |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 1309 | << " size = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1310 | << getResultPatternSize(Pattern.getDstPattern(), *CGP) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1311 | } |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1312 | if (FirstCodeLine.first != 1) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1313 | OS << std::string(Indent, ' ') << "{\n"; |
| 1314 | Indent += 2; |
| 1315 | } |
| 1316 | EmitPatterns(Shared, Indent, OS); |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1317 | if (FirstCodeLine.first != 1) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1318 | Indent -= 2; |
| 1319 | OS << std::string(Indent, ' ') << "}\n"; |
| 1320 | } |
| 1321 | |
| 1322 | if (Other.size() == 1) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1323 | const PatternToMatch &Pattern = *Other.back().first; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1324 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 1325 | Pattern.getSrcPattern()->print(OS); |
| 1326 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 1327 | Pattern.getDstPattern()->print(OS); |
| 1328 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1329 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1330 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1331 | << getPatternSize(Pattern.getSrcPattern(), *CGP) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1332 | << " cost = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1333 | << getResultPatternCost(Pattern.getDstPattern(), *CGP) |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1334 | << " size = " |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1335 | << getResultPatternSize(Pattern.getDstPattern(), *CGP) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1336 | } |
| 1337 | EmitPatterns(Other, Indent, OS); |
| 1338 | return; |
| 1339 | } |
| 1340 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1341 | // Remove this code from all of the patterns that share it. |
| 1342 | bool ErasedPatterns = EraseCodeLine(Patterns); |
| 1343 | |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1344 | bool isPredicate = FirstCodeLine.first == 1; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Otherwise, every pattern in the list has this line. Emit it. |
| 1347 | if (!isPredicate) { |
| 1348 | // Normal code. |
| 1349 | OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n"; |
| 1350 | } else { |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1351 | OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second; |
| 1352 | |
| 1353 | // If the next code line is another predicate, and if all of the pattern |
| 1354 | // in this group share the same next line, emit it inline now. Do this |
| 1355 | // until we run out of common predicates. |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1356 | while (!ErasedPatterns && Patterns.back().second.back().first == 1) { |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1357 | // Check that all of fhe patterns in Patterns end with the same predicate. |
| 1358 | bool AllEndWithSamePredicate = true; |
| 1359 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) |
| 1360 | if (Patterns[i].second.back() != Patterns.back().second.back()) { |
| 1361 | AllEndWithSamePredicate = false; |
| 1362 | break; |
| 1363 | } |
| 1364 | // If all of the predicates aren't the same, we can't share them. |
| 1365 | if (!AllEndWithSamePredicate) break; |
| 1366 | |
| 1367 | // Otherwise we can. Emit it shared now. |
| 1368 | OS << " &&\n" << std::string(Indent+4, ' ') |
| 1369 | << Patterns.back().second.back().second; |
| 1370 | ErasedPatterns = EraseCodeLine(Patterns); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1371 | } |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 1372 | |
| 1373 | OS << ") {\n"; |
| 1374 | Indent += 2; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | EmitPatterns(Patterns, Indent, OS); |
| 1378 | |
| 1379 | if (isPredicate) |
| 1380 | OS << std::string(Indent-2, ' ') << "}\n"; |
| 1381 | } |
| 1382 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1383 | static std::string getOpcodeName(Record *Op, CodegenDAGPatterns &CGP) { |
| 1384 | return CGP.getSDNodeInfo(Op).getEnumName(); |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1385 | } |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 1386 | |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1387 | static std::string getLegalCName(std::string OpName) { |
| 1388 | std::string::size_type pos = OpName.find("::"); |
| 1389 | if (pos != std::string::npos) |
| 1390 | OpName.replace(pos, 2, "_"); |
| 1391 | return OpName; |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1394 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1395 | const CodeGenTarget &Target = CGP->getTargetInfo(); |
| 1396 | |
Chris Lattner | f7560ed | 2006-11-20 18:54:33 +0000 | [diff] [blame] | 1397 | // Get the namespace to insert instructions into. Make sure not to pick up |
| 1398 | // "TargetInstrInfo" by accidentally getting the namespace off the PHI |
| 1399 | // instruction or something. |
| 1400 | std::string InstNS; |
| 1401 | for (CodeGenTarget::inst_iterator i = Target.inst_begin(), |
| 1402 | e = Target.inst_end(); i != e; ++i) { |
| 1403 | InstNS = i->second.Namespace; |
| 1404 | if (InstNS != "TargetInstrInfo") |
| 1405 | break; |
| 1406 | } |
| 1407 | |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 1408 | if (!InstNS.empty()) InstNS += "::"; |
| 1409 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1410 | // Group the patterns by their top-level opcodes. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1411 | std::map<std::string, std::vector<const PatternToMatch*> > PatternsByOpcode; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 1412 | // All unique target node emission functions. |
| 1413 | std::map<std::string, unsigned> EmitFunctions; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1414 | for (CodegenDAGPatterns::ptm_iterator I = CGP->ptm_begin(), |
| 1415 | E = CGP->ptm_end(); I != E; ++I) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1416 | const PatternToMatch &Pattern = *I; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1417 | |
| 1418 | TreePatternNode *Node = Pattern.getSrcPattern(); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1419 | if (!Node->isLeaf()) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1420 | PatternsByOpcode[getOpcodeName(Node->getOperator(), *CGP)]. |
| 1421 | push_back(&Pattern); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1422 | } else { |
| 1423 | const ComplexPattern *CP; |
Chris Lattner | 9c5d4de | 2006-11-03 01:11:05 +0000 | [diff] [blame] | 1424 | if (dynamic_cast<IntInit*>(Node->getLeafValue())) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1425 | PatternsByOpcode[getOpcodeName(CGP->getSDNodeNamed("imm"), *CGP)]. |
| 1426 | push_back(&Pattern); |
| 1427 | } else if ((CP = NodeGetComplexPattern(Node, *CGP))) { |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1428 | std::vector<Record*> OpNodes = CP->getRootNodes(); |
| 1429 | for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1430 | PatternsByOpcode[getOpcodeName(OpNodes[j], *CGP)] |
| 1431 | .insert(PatternsByOpcode[getOpcodeName(OpNodes[j], *CGP)].begin(), |
| 1432 | &Pattern); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1433 | } |
| 1434 | } else { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1435 | cerr << "Unrecognized opcode '"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1436 | Node->dump(); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1437 | cerr << "' on tree pattern '"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1438 | cerr << Pattern.getDstPattern()->getOperator()->getName() << "'!\n"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1439 | exit(1); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1443 | |
| 1444 | // For each opcode, there might be multiple select functions, one per |
| 1445 | // ValueType of the node (or its first operand if it doesn't produce a |
| 1446 | // non-chain result. |
| 1447 | std::map<std::string, std::vector<std::string> > OpcodeVTMap; |
| 1448 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1449 | // Emit one Select_* method for each top-level opcode. We do this instead of |
| 1450 | // emitting one giant switch statement to support compilers where this will |
| 1451 | // result in the recursive functions taking less stack space. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1452 | for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1453 | PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end(); |
| 1454 | PBOI != E; ++PBOI) { |
| 1455 | const std::string &OpName = PBOI->first; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1456 | std::vector<const PatternToMatch*> &PatternsOfOp = PBOI->second; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1457 | assert(!PatternsOfOp.empty() && "No patterns but map has entry?"); |
| 1458 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1459 | // We want to emit all of the matching code now. However, we want to emit |
| 1460 | // the matches in order of minimal cost. Sort the patterns so the least |
| 1461 | // cost one is at the start. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1462 | std::stable_sort(PatternsOfOp.begin(), PatternsOfOp.end(), |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1463 | PatternSortingPredicate(*CGP)); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1465 | // Split them into groups by type. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1466 | std::map<MVT::ValueType, std::vector<const PatternToMatch*> >PatternsByType; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1467 | for (unsigned i = 0, e = PatternsOfOp.size(); i != e; ++i) { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1468 | const PatternToMatch *Pat = PatternsOfOp[i]; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1469 | TreePatternNode *SrcPat = Pat->getSrcPattern(); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1470 | MVT::ValueType VT = SrcPat->getTypeNum(0); |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1471 | std::map<MVT::ValueType, |
| 1472 | std::vector<const PatternToMatch*> >::iterator TI = |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1473 | PatternsByType.find(VT); |
| 1474 | if (TI != PatternsByType.end()) |
| 1475 | TI->second.push_back(Pat); |
| 1476 | else { |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1477 | std::vector<const PatternToMatch*> PVec; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1478 | PVec.push_back(Pat); |
| 1479 | PatternsByType.insert(std::make_pair(VT, PVec)); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1480 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1483 | for (std::map<MVT::ValueType, std::vector<const PatternToMatch*> >::iterator |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1484 | II = PatternsByType.begin(), EE = PatternsByType.end(); II != EE; |
| 1485 | ++II) { |
| 1486 | MVT::ValueType OpVT = II->first; |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1487 | std::vector<const PatternToMatch*> &Patterns = II->second; |
Chris Lattner | 6490697 | 2006-09-21 18:28:27 +0000 | [diff] [blame] | 1488 | typedef std::vector<std::pair<unsigned,std::string> > CodeList; |
| 1489 | typedef std::vector<std::pair<unsigned,std::string> >::iterator CodeListI; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1490 | |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1491 | std::vector<std::pair<const PatternToMatch*, CodeList> > CodeForPatterns; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1492 | std::vector<std::vector<std::string> > PatternOpcodes; |
| 1493 | std::vector<std::vector<std::string> > PatternVTs; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1494 | std::vector<std::set<std::string> > PatternDecls; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1495 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1496 | CodeList GeneratedCode; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1497 | std::set<std::string> GeneratedDecl; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1498 | std::vector<std::string> TargetOpcodes; |
| 1499 | std::vector<std::string> TargetVTs; |
| 1500 | GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl, |
| 1501 | TargetOpcodes, TargetVTs); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1502 | CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode)); |
| 1503 | PatternDecls.push_back(GeneratedDecl); |
| 1504 | PatternOpcodes.push_back(TargetOpcodes); |
| 1505 | PatternVTs.push_back(TargetVTs); |
| 1506 | } |
| 1507 | |
| 1508 | // Scan the code to see if all of the patterns are reachable and if it is |
| 1509 | // possible that the last one might not match. |
| 1510 | bool mightNotMatch = true; |
| 1511 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1512 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1513 | mightNotMatch = false; |
| 1514 | |
| 1515 | for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1516 | if (GeneratedCode[j].first == 1) { // predicate. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1517 | mightNotMatch = true; |
| 1518 | break; |
| 1519 | } |
| 1520 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1522 | // If this pattern definitely matches, and if it isn't the last one, the |
| 1523 | // patterns after it CANNOT ever match. Error out. |
| 1524 | if (mightNotMatch == false && i != CodeForPatterns.size()-1) { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1525 | cerr << "Pattern '"; |
| 1526 | CodeForPatterns[i].first->getSrcPattern()->print(*cerr.stream()); |
| 1527 | cerr << "' is impossible to select!\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1528 | exit(1); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1532 | // Factor target node emission code (emitted by EmitResultCode) into |
| 1533 | // separate functions. Uniquing and share them among all instruction |
| 1534 | // selection routines. |
| 1535 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1536 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1537 | std::vector<std::string> &TargetOpcodes = PatternOpcodes[i]; |
| 1538 | std::vector<std::string> &TargetVTs = PatternVTs[i]; |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1539 | std::set<std::string> Decls = PatternDecls[i]; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1540 | std::vector<std::string> AddedInits; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1541 | int CodeSize = (int)GeneratedCode.size(); |
| 1542 | int LastPred = -1; |
| 1543 | for (int j = CodeSize-1; j >= 0; --j) { |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1544 | if (LastPred == -1 && GeneratedCode[j].first == 1) |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1545 | LastPred = j; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1546 | else if (LastPred != -1 && GeneratedCode[j].first == 2) |
| 1547 | AddedInits.push_back(GeneratedCode[j].second); |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1550 | std::string CalleeCode = "(const SDOperand &N"; |
| 1551 | std::string CallerCode = "(N"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1552 | for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) { |
| 1553 | CalleeCode += ", unsigned Opc" + utostr(j); |
| 1554 | CallerCode += ", " + TargetOpcodes[j]; |
| 1555 | } |
| 1556 | for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) { |
| 1557 | CalleeCode += ", MVT::ValueType VT" + utostr(j); |
| 1558 | CallerCode += ", " + TargetVTs[j]; |
| 1559 | } |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1560 | for (std::set<std::string>::iterator |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1561 | I = Decls.begin(), E = Decls.end(); I != E; ++I) { |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1562 | std::string Name = *I; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1563 | CalleeCode += ", SDOperand &" + Name; |
| 1564 | CallerCode += ", " + Name; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1565 | } |
| 1566 | CallerCode += ");"; |
| 1567 | CalleeCode += ") "; |
| 1568 | // Prevent emission routines from being inlined to reduce selection |
| 1569 | // routines stack frame sizes. |
Chris Lattner | 8dc728e | 2006-08-27 13:16:24 +0000 | [diff] [blame] | 1570 | CalleeCode += "DISABLE_INLINE "; |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1571 | CalleeCode += "{\n"; |
| 1572 | |
| 1573 | for (std::vector<std::string>::const_reverse_iterator |
| 1574 | I = AddedInits.rbegin(), E = AddedInits.rend(); I != E; ++I) |
| 1575 | CalleeCode += " " + *I + "\n"; |
| 1576 | |
Evan Cheng | f549319 | 2006-08-26 01:02:19 +0000 | [diff] [blame] | 1577 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 1578 | CalleeCode += " " + GeneratedCode[j].second + "\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1579 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 1580 | GeneratedCode.pop_back(); |
| 1581 | CalleeCode += "}\n"; |
| 1582 | |
| 1583 | // Uniquing the emission routines. |
| 1584 | unsigned EmitFuncNum; |
| 1585 | std::map<std::string, unsigned>::iterator EFI = |
| 1586 | EmitFunctions.find(CalleeCode); |
| 1587 | if (EFI != EmitFunctions.end()) { |
| 1588 | EmitFuncNum = EFI->second; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1589 | } else { |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1590 | EmitFuncNum = EmitFunctions.size(); |
| 1591 | EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum)); |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1592 | OS << "SDNode *Emit_" << utostr(EmitFuncNum) << CalleeCode; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1593 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1594 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1595 | // Replace the emission code within selection routines with calls to the |
| 1596 | // emission functions. |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1597 | CallerCode = "return Emit_" + utostr(EmitFuncNum) + CallerCode; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1598 | GeneratedCode.push_back(std::make_pair(false, CallerCode)); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1601 | // Print function. |
Chris Lattner | ab51ddd | 2006-11-14 21:32:01 +0000 | [diff] [blame] | 1602 | std::string OpVTStr; |
Chris Lattner | 33a4004 | 2006-11-14 22:17:10 +0000 | [diff] [blame] | 1603 | if (OpVT == MVT::iPTR) { |
| 1604 | OpVTStr = "_iPTR"; |
| 1605 | } else if (OpVT == MVT::isVoid) { |
| 1606 | // Nodes with a void result actually have a first result type of either |
| 1607 | // Other (a chain) or Flag. Since there is no one-to-one mapping from |
| 1608 | // void to this case, we handle it specially here. |
| 1609 | } else { |
| 1610 | OpVTStr = "_" + getEnumName(OpVT).substr(5); // Skip 'MVT::' |
| 1611 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1612 | std::map<std::string, std::vector<std::string> >::iterator OpVTI = |
| 1613 | OpcodeVTMap.find(OpName); |
| 1614 | if (OpVTI == OpcodeVTMap.end()) { |
| 1615 | std::vector<std::string> VTSet; |
| 1616 | VTSet.push_back(OpVTStr); |
| 1617 | OpcodeVTMap.insert(std::make_pair(OpName, VTSet)); |
| 1618 | } else |
| 1619 | OpVTI->second.push_back(OpVTStr); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1620 | |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1621 | OS << "SDNode *Select_" << getLegalCName(OpName) |
Chris Lattner | 33a4004 | 2006-11-14 22:17:10 +0000 | [diff] [blame] | 1622 | << OpVTStr << "(const SDOperand &N) {\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1624 | // Loop through and reverse all of the CodeList vectors, as we will be |
| 1625 | // accessing them from their logical front, but accessing the end of a |
| 1626 | // vector is more efficient. |
| 1627 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 1628 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 1629 | std::reverse(GeneratedCode.begin(), GeneratedCode.end()); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1630 | } |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1631 | |
| 1632 | // Next, reverse the list of patterns itself for the same reason. |
| 1633 | std::reverse(CodeForPatterns.begin(), CodeForPatterns.end()); |
| 1634 | |
| 1635 | // Emit all of the patterns now, grouped together to share code. |
| 1636 | EmitPatterns(CodeForPatterns, 2, OS); |
| 1637 | |
Chris Lattner | 6490697 | 2006-09-21 18:28:27 +0000 | [diff] [blame] | 1638 | // If the last pattern has predicates (which could fail) emit code to |
| 1639 | // catch the case where nothing handles a pattern. |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1640 | if (mightNotMatch) { |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1641 | OS << " cerr << \"Cannot yet select: \";\n"; |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1642 | if (OpName != "ISD::INTRINSIC_W_CHAIN" && |
| 1643 | OpName != "ISD::INTRINSIC_WO_CHAIN" && |
| 1644 | OpName != "ISD::INTRINSIC_VOID") { |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1645 | OS << " N.Val->dump(CurDAG);\n"; |
| 1646 | } else { |
| 1647 | OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand(" |
| 1648 | "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1649 | << " cerr << \"intrinsic %\"<< " |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1650 | "Intrinsic::getName((Intrinsic::ID)iid);\n"; |
| 1651 | } |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1652 | OS << " cerr << '\\n';\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1653 | << " abort();\n" |
| 1654 | << " return NULL;\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1655 | } |
| 1656 | OS << "}\n\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1657 | } |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1660 | // Emit boilerplate. |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1661 | OS << "SDNode *Select_INLINEASM(SDOperand N) {\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1662 | << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n" |
Chris Lattner | 4ef9b11 | 2007-05-15 01:36:44 +0000 | [diff] [blame] | 1663 | << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n\n" |
| 1664 | |
| 1665 | << " // Ensure that the asm operands are themselves selected.\n" |
| 1666 | << " for (unsigned j = 0, e = Ops.size(); j != e; ++j)\n" |
| 1667 | << " AddToISelQueue(Ops[j]);\n\n" |
| 1668 | |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1669 | << " std::vector<MVT::ValueType> VTs;\n" |
| 1670 | << " VTs.push_back(MVT::Other);\n" |
| 1671 | << " VTs.push_back(MVT::Flag);\n" |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1672 | << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, &Ops[0], " |
| 1673 | "Ops.size());\n" |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1674 | << " return New.Val;\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1675 | << "}\n\n"; |
| 1676 | |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1677 | OS << "SDNode *Select_LABEL(const SDOperand &N) {\n" |
| 1678 | << " SDOperand Chain = N.getOperand(0);\n" |
| 1679 | << " SDOperand N1 = N.getOperand(1);\n" |
Jim Laskey | 844b892 | 2007-01-26 23:00:54 +0000 | [diff] [blame] | 1680 | << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n" |
| 1681 | << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n" |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1682 | << " AddToISelQueue(Chain);\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1683 | << " SDOperand Ops[] = { Tmp, Chain };\n" |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1684 | << " return CurDAG->getTargetNode(TargetInstrInfo::LABEL,\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1685 | << " MVT::Other, Ops, 2);\n" |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1686 | << "}\n\n"; |
| 1687 | |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1688 | OS << "SDNode *Select_EXTRACT_SUBREG(const SDOperand &N) {\n" |
| 1689 | << " SDOperand N0 = N.getOperand(0);\n" |
| 1690 | << " SDOperand N1 = N.getOperand(1);\n" |
| 1691 | << " unsigned C = cast<ConstantSDNode>(N1)->getValue();\n" |
| 1692 | << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n" |
| 1693 | << " AddToISelQueue(N0);\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1694 | << " SDOperand Ops[] = { N0, Tmp };\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1695 | << " return CurDAG->getTargetNode(TargetInstrInfo::EXTRACT_SUBREG,\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1696 | << " N.getValueType(), Ops, 2);\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1697 | << "}\n\n"; |
| 1698 | |
| 1699 | OS << "SDNode *Select_INSERT_SUBREG(const SDOperand &N) {\n" |
| 1700 | << " SDOperand N0 = N.getOperand(0);\n" |
| 1701 | << " SDOperand N1 = N.getOperand(1);\n" |
| 1702 | << " SDOperand N2 = N.getOperand(2);\n" |
| 1703 | << " unsigned C = cast<ConstantSDNode>(N2)->getValue();\n" |
| 1704 | << " SDOperand Tmp = CurDAG->getTargetConstant(C, MVT::i32);\n" |
| 1705 | << " AddToISelQueue(N1);\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1706 | << " SDOperand Ops[] = { N0, N1, Tmp };\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1707 | << " if (N0.getOpcode() == ISD::UNDEF) {\n" |
Evan Cheng | 3393f89 | 2007-10-12 08:39:02 +0000 | [diff] [blame] | 1708 | << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1709 | << " N.getValueType(), Ops+1, 2);\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1710 | << " } else {\n" |
| 1711 | << " AddToISelQueue(N0);\n" |
Evan Cheng | 3393f89 | 2007-10-12 08:39:02 +0000 | [diff] [blame] | 1712 | << " return CurDAG->getTargetNode(TargetInstrInfo::INSERT_SUBREG,\n" |
Chris Lattner | 44f1476 | 2007-10-24 06:25:09 +0000 | [diff] [blame] | 1713 | << " N.getValueType(), Ops, 3);\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1714 | << " }\n" |
| 1715 | << "}\n\n"; |
| 1716 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1717 | OS << "// The main instruction selector code.\n" |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1718 | << "SDNode *SelectCode(SDOperand N) {\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1719 | << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 1720 | << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1721 | << "INSTRUCTION_LIST_END)) {\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1722 | << " return NULL; // Already selected.\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1723 | << " }\n\n" |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1724 | << " MVT::ValueType NVT = N.Val->getValueType(0);\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1725 | << " switch (N.getOpcode()) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1726 | << " default: break;\n" |
| 1727 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
Chris Lattner | 5216c69 | 2005-12-18 21:05:44 +0000 | [diff] [blame] | 1728 | << " case ISD::BasicBlock:\n" |
Chris Lattner | 8020a52 | 2006-01-11 19:52:27 +0000 | [diff] [blame] | 1729 | << " case ISD::Register:\n" |
Evan Cheng | 0a83ed5 | 2006-02-05 08:46:14 +0000 | [diff] [blame] | 1730 | << " case ISD::HANDLENODE:\n" |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame] | 1731 | << " case ISD::TargetConstant:\n" |
| 1732 | << " case ISD::TargetConstantPool:\n" |
| 1733 | << " case ISD::TargetFrameIndex:\n" |
Chris Lattner | 4ef9b11 | 2007-05-15 01:36:44 +0000 | [diff] [blame] | 1734 | << " case ISD::TargetExternalSymbol:\n" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 1735 | << " case ISD::TargetJumpTable:\n" |
Lauro Ramos Venancio | b3a0417 | 2007-04-20 21:38:10 +0000 | [diff] [blame] | 1736 | << " case ISD::TargetGlobalTLSAddress:\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1737 | << " case ISD::TargetGlobalAddress: {\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1738 | << " return NULL;\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 1739 | << " }\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1740 | << " case ISD::AssertSext:\n" |
Chris Lattner | fab3728 | 2005-09-26 22:10:24 +0000 | [diff] [blame] | 1741 | << " case ISD::AssertZext: {\n" |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1742 | << " AddToISelQueue(N.getOperand(0));\n" |
| 1743 | << " ReplaceUses(N, N.getOperand(0));\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1744 | << " return NULL;\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 1745 | << " }\n" |
| 1746 | << " case ISD::TokenFactor:\n" |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1747 | << " case ISD::CopyFromReg:\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1748 | << " case ISD::CopyToReg: {\n" |
Evan Cheng | 676d731 | 2006-08-26 00:59:04 +0000 | [diff] [blame] | 1749 | << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n" |
| 1750 | << " AddToISelQueue(N.getOperand(i));\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1751 | << " return NULL;\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1752 | << " }\n" |
Jim Laskey | a683f9b | 2007-01-26 17:29:20 +0000 | [diff] [blame] | 1753 | << " case ISD::INLINEASM: return Select_INLINEASM(N);\n" |
Christopher Lamb | 08d5207 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 1754 | << " case ISD::LABEL: return Select_LABEL(N);\n" |
| 1755 | << " case ISD::EXTRACT_SUBREG: return Select_EXTRACT_SUBREG(N);\n" |
| 1756 | << " case ISD::INSERT_SUBREG: return Select_INSERT_SUBREG(N);\n"; |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 1757 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1758 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 1759 | // Loop over all of the case statements, emiting a call to each method we |
| 1760 | // emitted above. |
Chris Lattner | 60d8139 | 2008-01-05 22:30:17 +0000 | [diff] [blame^] | 1761 | for (std::map<std::string, std::vector<const PatternToMatch*> >::iterator |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1762 | PBOI = PatternsByOpcode.begin(), E = PatternsByOpcode.end(); |
| 1763 | PBOI != E; ++PBOI) { |
| 1764 | const std::string &OpName = PBOI->first; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1765 | // Potentially multiple versions of select for this opcode. One for each |
| 1766 | // ValueType of the node (or its first true operand if it doesn't produce a |
| 1767 | // result. |
| 1768 | std::map<std::string, std::vector<std::string> >::iterator OpVTI = |
| 1769 | OpcodeVTMap.find(OpName); |
| 1770 | std::vector<std::string> &OpVTs = OpVTI->second; |
Evan Cheng | 892aaf8 | 2006-11-08 23:01:03 +0000 | [diff] [blame] | 1771 | OS << " case " << OpName << ": {\n"; |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1772 | // Keep track of whether we see a pattern that has an iPtr result. |
| 1773 | bool HasPtrPattern = false; |
| 1774 | bool HasDefaultPattern = false; |
Chris Lattner | 717a611 | 2006-11-14 21:50:27 +0000 | [diff] [blame] | 1775 | |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1776 | OS << " switch (NVT) {\n"; |
| 1777 | for (unsigned i = 0, e = OpVTs.size(); i < e; ++i) { |
| 1778 | std::string &VTStr = OpVTs[i]; |
| 1779 | if (VTStr.empty()) { |
| 1780 | HasDefaultPattern = true; |
| 1781 | continue; |
| 1782 | } |
Chris Lattner | 717a611 | 2006-11-14 21:50:27 +0000 | [diff] [blame] | 1783 | |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1784 | // If this is a match on iPTR: don't emit it directly, we need special |
| 1785 | // code. |
| 1786 | if (VTStr == "_iPTR") { |
| 1787 | HasPtrPattern = true; |
| 1788 | continue; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1789 | } |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1790 | OS << " case MVT::" << VTStr.substr(1) << ":\n" |
| 1791 | << " return Select_" << getLegalCName(OpName) |
| 1792 | << VTStr << "(N);\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1793 | } |
Evan Cheng | 425e8c7 | 2007-09-04 20:18:28 +0000 | [diff] [blame] | 1794 | OS << " default:\n"; |
| 1795 | |
| 1796 | // If there is an iPTR result version of this pattern, emit it here. |
| 1797 | if (HasPtrPattern) { |
| 1798 | OS << " if (NVT == TLI.getPointerTy())\n"; |
| 1799 | OS << " return Select_" << getLegalCName(OpName) <<"_iPTR(N);\n"; |
| 1800 | } |
| 1801 | if (HasDefaultPattern) { |
| 1802 | OS << " return Select_" << getLegalCName(OpName) << "(N);\n"; |
| 1803 | } |
| 1804 | OS << " break;\n"; |
| 1805 | OS << " }\n"; |
| 1806 | OS << " break;\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1807 | OS << " }\n"; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1808 | } |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1810 | OS << " } // end of big switch.\n\n" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1811 | << " cerr << \"Cannot yet select: \";\n" |
Chris Lattner | b026e70 | 2006-03-28 00:41:33 +0000 | [diff] [blame] | 1812 | << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n" |
| 1813 | << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n" |
| 1814 | << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n" |
Chris Lattner | 9bf2d3e | 2006-03-25 06:47:53 +0000 | [diff] [blame] | 1815 | << " N.Val->dump(CurDAG);\n" |
| 1816 | << " } else {\n" |
| 1817 | << " unsigned iid = cast<ConstantSDNode>(N.getOperand(" |
| 1818 | "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1819 | << " cerr << \"intrinsic %\"<< " |
| 1820 | "Intrinsic::getName((Intrinsic::ID)iid);\n" |
Chris Lattner | 9bf2d3e | 2006-03-25 06:47:53 +0000 | [diff] [blame] | 1821 | << " }\n" |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1822 | << " cerr << '\\n';\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1823 | << " abort();\n" |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1824 | << " return NULL;\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1825 | << "}\n"; |
| 1826 | } |
| 1827 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1828 | void DAGISelEmitter::run(std::ostream &OS) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1829 | CodeGenTarget Target; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1830 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 1831 | " target", OS); |
| 1832 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1833 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 1834 | << "// *** instruction selector class. These functions are really " |
| 1835 | << "methods.\n\n"; |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | 8dc728e | 2006-08-27 13:16:24 +0000 | [diff] [blame] | 1837 | OS << "#include \"llvm/Support/Compiler.h\"\n"; |
Evan Cheng | 233baf1 | 2006-07-26 23:06:27 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1839 | OS << "// Instruction selector priority queue:\n" |
| 1840 | << "std::vector<SDNode*> ISelQueue;\n"; |
| 1841 | OS << "/// Keep track of nodes which have already been added to queue.\n" |
| 1842 | << "unsigned char *ISelQueued;\n"; |
| 1843 | OS << "/// Keep track of nodes which have already been selected.\n" |
| 1844 | << "unsigned char *ISelSelected;\n"; |
| 1845 | OS << "/// Dummy parameter to ReplaceAllUsesOfValueWith().\n" |
| 1846 | << "std::vector<SDNode*> ISelKilled;\n\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 1847 | |
Evan Cheng | 4326ef5 | 2006-10-12 02:08:53 +0000 | [diff] [blame] | 1848 | OS << "/// IsChainCompatible - Returns true if Chain is Op or Chain does\n"; |
| 1849 | OS << "/// not reach Op.\n"; |
| 1850 | OS << "static bool IsChainCompatible(SDNode *Chain, SDNode *Op) {\n"; |
| 1851 | OS << " if (Chain->getOpcode() == ISD::EntryToken)\n"; |
| 1852 | OS << " return true;\n"; |
| 1853 | OS << " else if (Chain->getOpcode() == ISD::TokenFactor)\n"; |
| 1854 | OS << " return false;\n"; |
| 1855 | OS << " else if (Chain->getNumOperands() > 0) {\n"; |
| 1856 | OS << " SDOperand C0 = Chain->getOperand(0);\n"; |
| 1857 | OS << " if (C0.getValueType() == MVT::Other)\n"; |
| 1858 | OS << " return C0.Val != Op && IsChainCompatible(C0.Val, Op);\n"; |
| 1859 | OS << " }\n"; |
| 1860 | OS << " return true;\n"; |
| 1861 | OS << "}\n"; |
| 1862 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1863 | OS << "/// Sorting functions for the selection queue.\n" |
| 1864 | << "struct isel_sort : public std::binary_function" |
| 1865 | << "<SDNode*, SDNode*, bool> {\n" |
| 1866 | << " bool operator()(const SDNode* left, const SDNode* right) " |
| 1867 | << "const {\n" |
| 1868 | << " return (left->getNodeId() > right->getNodeId());\n" |
| 1869 | << " }\n" |
| 1870 | << "};\n\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 1871 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1872 | OS << "inline void setQueued(int Id) {\n"; |
| 1873 | OS << " ISelQueued[Id / 8] |= 1 << (Id % 8);\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1874 | OS << "}\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1875 | OS << "inline bool isQueued(int Id) {\n"; |
| 1876 | OS << " return ISelQueued[Id / 8] & (1 << (Id % 8));\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1877 | OS << "}\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1878 | OS << "inline void setSelected(int Id) {\n"; |
| 1879 | OS << " ISelSelected[Id / 8] |= 1 << (Id % 8);\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1880 | OS << "}\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1881 | OS << "inline bool isSelected(int Id) {\n"; |
| 1882 | OS << " return ISelSelected[Id / 8] & (1 << (Id % 8));\n"; |
| 1883 | OS << "}\n\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 1884 | |
Chris Lattner | 8dc728e | 2006-08-27 13:16:24 +0000 | [diff] [blame] | 1885 | OS << "void AddToISelQueue(SDOperand N) DISABLE_INLINE {\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1886 | OS << " int Id = N.Val->getNodeId();\n"; |
| 1887 | OS << " if (Id != -1 && !isQueued(Id)) {\n"; |
| 1888 | OS << " ISelQueue.push_back(N.Val);\n"; |
| 1889 | OS << " std::push_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n"; |
| 1890 | OS << " setQueued(Id);\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1891 | OS << " }\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1892 | OS << "}\n\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame] | 1893 | |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1894 | OS << "inline void RemoveKilled() {\n"; |
| 1895 | OS << " unsigned NumKilled = ISelKilled.size();\n"; |
| 1896 | OS << " if (NumKilled) {\n"; |
| 1897 | OS << " for (unsigned i = 0; i != NumKilled; ++i) {\n"; |
| 1898 | OS << " SDNode *Temp = ISelKilled[i];\n"; |
Evan Cheng | 4f77616 | 2006-10-12 23:18:52 +0000 | [diff] [blame] | 1899 | OS << " ISelQueue.erase(std::remove(ISelQueue.begin(), ISelQueue.end(), " |
| 1900 | << "Temp), ISelQueue.end());\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1901 | OS << " };\n"; |
| 1902 | OS << " std::make_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n"; |
| 1903 | OS << " ISelKilled.clear();\n"; |
| 1904 | OS << " }\n"; |
| 1905 | OS << "}\n\n"; |
| 1906 | |
Chris Lattner | 8dc728e | 2006-08-27 13:16:24 +0000 | [diff] [blame] | 1907 | OS << "void ReplaceUses(SDOperand F, SDOperand T) DISABLE_INLINE {\n"; |
Chris Lattner | 01d029b | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 1908 | OS << " CurDAG->ReplaceAllUsesOfValueWith(F, T, &ISelKilled);\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1909 | OS << " setSelected(F.Val->getNodeId());\n"; |
| 1910 | OS << " RemoveKilled();\n"; |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1911 | OS << "}\n"; |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1912 | OS << "void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {\n"; |
Evan Cheng | 30729b4 | 2007-09-17 22:26:41 +0000 | [diff] [blame] | 1913 | OS << " unsigned FNumVals = F->getNumValues();\n"; |
| 1914 | OS << " unsigned TNumVals = T->getNumValues();\n"; |
| 1915 | OS << " if (FNumVals != TNumVals) {\n"; |
| 1916 | OS << " for (unsigned i = 0, e = std::min(FNumVals, TNumVals); " |
| 1917 | << "i < e; ++i)\n"; |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1918 | OS << " CurDAG->ReplaceAllUsesOfValueWith(SDOperand(F, i), " |
Chris Lattner | 01d029b | 2007-10-15 06:10:22 +0000 | [diff] [blame] | 1919 | << "SDOperand(T, i), &ISelKilled);\n"; |
Evan Cheng | 85dbe1a | 2007-09-12 23:30:14 +0000 | [diff] [blame] | 1920 | OS << " } else {\n"; |
| 1921 | OS << " CurDAG->ReplaceAllUsesWith(F, T, &ISelKilled);\n"; |
| 1922 | OS << " }\n"; |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1923 | OS << " setSelected(F->getNodeId());\n"; |
| 1924 | OS << " RemoveKilled();\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1925 | OS << "}\n\n"; |
| 1926 | |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 1927 | OS << "// SelectRoot - Top level entry to DAG isel.\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1928 | OS << "SDOperand SelectRoot(SDOperand Root) {\n"; |
| 1929 | OS << " SelectRootInit();\n"; |
| 1930 | OS << " unsigned NumBytes = (DAGSize + 7) / 8;\n"; |
| 1931 | OS << " ISelQueued = new unsigned char[NumBytes];\n"; |
| 1932 | OS << " ISelSelected = new unsigned char[NumBytes];\n"; |
| 1933 | OS << " memset(ISelQueued, 0, NumBytes);\n"; |
| 1934 | OS << " memset(ISelSelected, 0, NumBytes);\n"; |
| 1935 | OS << "\n"; |
Chris Lattner | dfb8607 | 2006-08-15 23:42:26 +0000 | [diff] [blame] | 1936 | OS << " // Create a dummy node (which is not added to allnodes), that adds\n" |
| 1937 | << " // a reference to the root node, preventing it from being deleted,\n" |
| 1938 | << " // and tracking any changes of the root.\n" |
| 1939 | << " HandleSDNode Dummy(CurDAG->getRoot());\n" |
| 1940 | << " ISelQueue.push_back(CurDAG->getRoot().Val);\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1941 | OS << " while (!ISelQueue.empty()) {\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1942 | OS << " SDNode *Node = ISelQueue.front();\n"; |
| 1943 | OS << " std::pop_heap(ISelQueue.begin(), ISelQueue.end(), isel_sort());\n"; |
| 1944 | OS << " ISelQueue.pop_back();\n"; |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1945 | OS << " if (!isSelected(Node->getNodeId())) {\n"; |
Evan Cheng | 9ade218 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1946 | OS << " SDNode *ResNode = Select(SDOperand(Node, 0));\n"; |
Evan Cheng | 966fd37 | 2006-09-11 02:24:43 +0000 | [diff] [blame] | 1947 | OS << " if (ResNode != Node) {\n"; |
| 1948 | OS << " if (ResNode)\n"; |
| 1949 | OS << " ReplaceUses(Node, ResNode);\n"; |
Evan Cheng | 1fae00f | 2006-10-12 20:35:19 +0000 | [diff] [blame] | 1950 | OS << " if (Node->use_empty()) { // Don't delete EntryToken, etc.\n"; |
| 1951 | OS << " CurDAG->RemoveDeadNode(Node, ISelKilled);\n"; |
| 1952 | OS << " RemoveKilled();\n"; |
| 1953 | OS << " }\n"; |
Evan Cheng | 966fd37 | 2006-09-11 02:24:43 +0000 | [diff] [blame] | 1954 | OS << " }\n"; |
Evan Cheng | 06d6470 | 2006-08-11 08:59:35 +0000 | [diff] [blame] | 1955 | OS << " }\n"; |
Chris Lattner | 706d2d3 | 2006-08-09 16:44:44 +0000 | [diff] [blame] | 1956 | OS << " }\n"; |
| 1957 | OS << "\n"; |
| 1958 | OS << " delete[] ISelQueued;\n"; |
| 1959 | OS << " ISelQueued = NULL;\n"; |
| 1960 | OS << " delete[] ISelSelected;\n"; |
| 1961 | OS << " ISelSelected = NULL;\n"; |
Chris Lattner | dfb8607 | 2006-08-15 23:42:26 +0000 | [diff] [blame] | 1962 | OS << " return Dummy.getValue();\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 1963 | OS << "}\n"; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1964 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1965 | CodegenDAGPatterns CGP(Records, OS); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1966 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1967 | this->CGP = &CGP; |
| 1968 | |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1969 | DOUT << "\n\nALL PATTERNS TO MATCH:\n\n"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1970 | for (CodegenDAGPatterns::ptm_iterator I = CGP.ptm_begin(), E = CGP.ptm_end(); |
| 1971 | I != E; ++I) { |
| 1972 | DOUT << "PATTERN: "; DEBUG(I->getSrcPattern()->dump()); |
| 1973 | DOUT << "\nRESULT: "; DEBUG(I->getDstPattern()->dump()); |
Bill Wendling | f5da133 | 2006-12-07 22:21:48 +0000 | [diff] [blame] | 1974 | DOUT << "\n"; |
| 1975 | } |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1976 | |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1977 | // At this point, we have full information about the 'Patterns' we need to |
| 1978 | // parse, both implicitly from instructions as well as from explicit pattern |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1979 | // definitions. Emit the resultant instruction selector. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1980 | EmitInstructionSelector(OS); |
| 1981 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1982 | } |