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