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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
Jeff Cohen | a48283b | 2005-09-25 19:04:43 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 19 | #include <set> |
| 20 | using namespace llvm; |
| 21 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 23 | // Helpers for working with extended types. |
| 24 | |
| 25 | /// FilterVTs - Filter a list of VT's according to a predicate. |
| 26 | /// |
| 27 | template<typename T> |
| 28 | static std::vector<MVT::ValueType> |
| 29 | FilterVTs(const std::vector<MVT::ValueType> &InVTs, T Filter) { |
| 30 | std::vector<MVT::ValueType> Result; |
| 31 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 32 | if (Filter(InVTs[i])) |
| 33 | Result.push_back(InVTs[i]); |
| 34 | return Result; |
| 35 | } |
| 36 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 37 | template<typename T> |
| 38 | static std::vector<unsigned char> |
| 39 | FilterEVTs(const std::vector<unsigned char> &InVTs, T Filter) { |
| 40 | std::vector<unsigned char> Result; |
| 41 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 42 | if (Filter((MVT::ValueType)InVTs[i])) |
| 43 | Result.push_back(InVTs[i]); |
| 44 | return Result; |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 47 | static std::vector<unsigned char> |
| 48 | ConvertVTs(const std::vector<MVT::ValueType> &InVTs) { |
| 49 | std::vector<unsigned char> Result; |
| 50 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 51 | Result.push_back(InVTs[i]); |
| 52 | return Result; |
| 53 | } |
| 54 | |
| 55 | static bool LHSIsSubsetOfRHS(const std::vector<unsigned char> &LHS, |
| 56 | const std::vector<unsigned char> &RHS) { |
| 57 | if (LHS.size() > RHS.size()) return false; |
| 58 | for (unsigned i = 0, e = LHS.size(); i != e; ++i) |
Duraid Madina | d47ae09 | 2005-12-30 16:41:48 +0000 | [diff] [blame] | 59 | if (std::find(RHS.begin(), RHS.end(), LHS[i]) == RHS.end()) |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 60 | return false; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /// isExtIntegerVT - Return true if the specified extended value type vector |
| 65 | /// contains isInt or an integer value type. |
Chris Lattner | 697f884 | 2006-03-20 05:39:48 +0000 | [diff] [blame] | 66 | static bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 67 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
| 68 | return EVTs[0] == MVT::isInt || !(FilterEVTs(EVTs, MVT::isInteger).empty()); |
| 69 | } |
| 70 | |
| 71 | /// isExtFloatingPointVT - Return true if the specified extended value type |
| 72 | /// vector contains isFP or a FP value type. |
Chris Lattner | 697f884 | 2006-03-20 05:39:48 +0000 | [diff] [blame] | 73 | static bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 74 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 75 | return EVTs[0] == MVT::isFP || |
| 76 | !(FilterEVTs(EVTs, MVT::isFloatingPoint).empty()); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 80 | // SDTypeConstraint implementation |
| 81 | // |
| 82 | |
| 83 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 84 | OperandNo = R->getValueAsInt("OperandNum"); |
| 85 | |
| 86 | if (R->isSubClassOf("SDTCisVT")) { |
| 87 | ConstraintType = SDTCisVT; |
| 88 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
Chris Lattner | 5b21be7 | 2005-12-09 22:57:42 +0000 | [diff] [blame] | 89 | } else if (R->isSubClassOf("SDTCisPtrTy")) { |
| 90 | ConstraintType = SDTCisPtrTy; |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 91 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 92 | ConstraintType = SDTCisInt; |
| 93 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 94 | ConstraintType = SDTCisFP; |
| 95 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 96 | ConstraintType = SDTCisSameAs; |
| 97 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 98 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 99 | ConstraintType = SDTCisVTSmallerThanOp; |
| 100 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 101 | R->getValueAsInt("OtherOperandNum"); |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 102 | } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { |
| 103 | ConstraintType = SDTCisOpSmallerThanOp; |
| 104 | x.SDTCisOpSmallerThanOp_Info.BigOperandNum = |
| 105 | R->getValueAsInt("BigOperandNum"); |
Chris Lattner | 697f884 | 2006-03-20 05:39:48 +0000 | [diff] [blame] | 106 | } else if (R->isSubClassOf("SDTCisIntVectorOfSameSize")) { |
| 107 | ConstraintType = SDTCisIntVectorOfSameSize; |
| 108 | x.SDTCisIntVectorOfSameSize_Info.OtherOperandNum = |
| 109 | R->getValueAsInt("OtherOpNum"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 110 | } else { |
| 111 | std::cerr << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
| 112 | exit(1); |
| 113 | } |
| 114 | } |
| 115 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 116 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 117 | /// N, which has NumResults results. |
| 118 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 119 | TreePatternNode *N, |
| 120 | unsigned NumResults) const { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 121 | assert(NumResults <= 1 && |
| 122 | "We only work with nodes with zero or one result so far!"); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 123 | |
Evan Cheng | 50c997e | 2006-06-13 21:47:27 +0000 | [diff] [blame] | 124 | if (OpNo >= (NumResults + N->getNumChildren())) { |
| 125 | std::cerr << "Invalid operand number " << OpNo << " "; |
| 126 | N->dump(); |
| 127 | std::cerr << '\n'; |
| 128 | exit(1); |
| 129 | } |
| 130 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 131 | if (OpNo < NumResults) |
| 132 | return N; // FIXME: need value # |
| 133 | else |
| 134 | return N->getChild(OpNo-NumResults); |
| 135 | } |
| 136 | |
| 137 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 138 | /// constraint to the nodes operands. This returns true if it makes a |
| 139 | /// change, false otherwise. If a type contradiction is found, throw an |
| 140 | /// exception. |
| 141 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 142 | const SDNodeInfo &NodeInfo, |
| 143 | TreePattern &TP) const { |
| 144 | unsigned NumResults = NodeInfo.getNumResults(); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 145 | assert(NumResults <= 1 && |
| 146 | "We only work with nodes with zero or one result so far!"); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 8f60d54 | 2006-06-16 18:25:06 +0000 | [diff] [blame] | 148 | // Check that the number of operands is sane. Negative operands -> varargs. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 149 | if (NodeInfo.getNumOperands() >= 0) { |
| 150 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 151 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 152 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 153 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 154 | |
| 155 | const CodeGenTarget &CGT = TP.getDAGISelEmitter().getTargetInfo(); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 156 | |
| 157 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 158 | |
| 159 | switch (ConstraintType) { |
| 160 | default: assert(0 && "Unknown constraint type!"); |
| 161 | case SDTCisVT: |
| 162 | // Operand must be a particular type. |
| 163 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
Chris Lattner | 5b21be7 | 2005-12-09 22:57:42 +0000 | [diff] [blame] | 164 | case SDTCisPtrTy: { |
| 165 | // Operand must be same as target pointer type. |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 166 | return NodeToApply->UpdateNodeType(MVT::iPTR, TP); |
Chris Lattner | 5b21be7 | 2005-12-09 22:57:42 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 168 | case SDTCisInt: { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 169 | // If there is only one integer type supported, this must be it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 170 | std::vector<MVT::ValueType> IntVTs = |
| 171 | FilterVTs(CGT.getLegalValueTypes(), MVT::isInteger); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 172 | |
| 173 | // If we found exactly one supported integer type, apply it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 174 | if (IntVTs.size() == 1) |
| 175 | return NodeToApply->UpdateNodeType(IntVTs[0], TP); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 176 | return NodeToApply->UpdateNodeType(MVT::isInt, TP); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 177 | } |
| 178 | case SDTCisFP: { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 179 | // If there is only one FP type supported, this must be it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 180 | std::vector<MVT::ValueType> FPVTs = |
| 181 | FilterVTs(CGT.getLegalValueTypes(), MVT::isFloatingPoint); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 182 | |
| 183 | // If we found exactly one supported FP type, apply it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 184 | if (FPVTs.size() == 1) |
| 185 | return NodeToApply->UpdateNodeType(FPVTs[0], TP); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 186 | return NodeToApply->UpdateNodeType(MVT::isFP, TP); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 187 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 188 | case SDTCisSameAs: { |
| 189 | TreePatternNode *OtherNode = |
| 190 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 191 | return NodeToApply->UpdateNodeType(OtherNode->getExtTypes(), TP) | |
| 192 | OtherNode->UpdateNodeType(NodeToApply->getExtTypes(), TP); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 193 | } |
| 194 | case SDTCisVTSmallerThanOp: { |
| 195 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 196 | // have an integer type that is smaller than the VT. |
| 197 | if (!NodeToApply->isLeaf() || |
| 198 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 199 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 200 | ->isSubClassOf("ValueType")) |
| 201 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
| 202 | MVT::ValueType VT = |
| 203 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
| 204 | if (!MVT::isInteger(VT)) |
| 205 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 206 | |
| 207 | TreePatternNode *OtherNode = |
| 208 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 209 | |
| 210 | // It must be integer. |
| 211 | bool MadeChange = false; |
| 212 | MadeChange |= OtherNode->UpdateNodeType(MVT::isInt, TP); |
| 213 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 214 | // This code only handles nodes that have one type set. Assert here so |
| 215 | // that we can change this if we ever need to deal with multiple value |
| 216 | // types at this point. |
| 217 | assert(OtherNode->getExtTypes().size() == 1 && "Node has too many types!"); |
| 218 | if (OtherNode->hasTypeSet() && OtherNode->getTypeNum(0) <= VT) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 219 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
| 220 | return false; |
| 221 | } |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 222 | case SDTCisOpSmallerThanOp: { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 223 | TreePatternNode *BigOperand = |
| 224 | getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NumResults); |
| 225 | |
| 226 | // Both operands must be integer or FP, but we don't care which. |
| 227 | bool MadeChange = false; |
| 228 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 229 | // This code does not currently handle nodes which have multiple types, |
| 230 | // where some types are integer, and some are fp. Assert that this is not |
| 231 | // the case. |
| 232 | assert(!(isExtIntegerInVTs(NodeToApply->getExtTypes()) && |
| 233 | isExtFloatingPointInVTs(NodeToApply->getExtTypes())) && |
| 234 | !(isExtIntegerInVTs(BigOperand->getExtTypes()) && |
| 235 | isExtFloatingPointInVTs(BigOperand->getExtTypes())) && |
| 236 | "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); |
| 237 | if (isExtIntegerInVTs(NodeToApply->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 238 | MadeChange |= BigOperand->UpdateNodeType(MVT::isInt, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 239 | else if (isExtFloatingPointInVTs(NodeToApply->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 240 | MadeChange |= BigOperand->UpdateNodeType(MVT::isFP, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 241 | if (isExtIntegerInVTs(BigOperand->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 242 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isInt, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 243 | else if (isExtFloatingPointInVTs(BigOperand->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 244 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isFP, TP); |
| 245 | |
| 246 | std::vector<MVT::ValueType> VTs = CGT.getLegalValueTypes(); |
| 247 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 248 | if (isExtIntegerInVTs(NodeToApply->getExtTypes())) { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 249 | VTs = FilterVTs(VTs, MVT::isInteger); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 250 | } else if (isExtFloatingPointInVTs(NodeToApply->getExtTypes())) { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 251 | VTs = FilterVTs(VTs, MVT::isFloatingPoint); |
| 252 | } else { |
| 253 | VTs.clear(); |
| 254 | } |
| 255 | |
| 256 | switch (VTs.size()) { |
| 257 | default: // Too many VT's to pick from. |
| 258 | case 0: break; // No info yet. |
| 259 | case 1: |
| 260 | // Only one VT of this flavor. Cannot ever satisify the constraints. |
| 261 | return NodeToApply->UpdateNodeType(MVT::Other, TP); // throw |
| 262 | case 2: |
| 263 | // If we have exactly two possible types, the little operand must be the |
| 264 | // small one, the big operand should be the big one. Common with |
| 265 | // float/double for example. |
| 266 | assert(VTs[0] < VTs[1] && "Should be sorted!"); |
| 267 | MadeChange |= NodeToApply->UpdateNodeType(VTs[0], TP); |
| 268 | MadeChange |= BigOperand->UpdateNodeType(VTs[1], TP); |
| 269 | break; |
| 270 | } |
| 271 | return MadeChange; |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 272 | } |
Chris Lattner | 697f884 | 2006-03-20 05:39:48 +0000 | [diff] [blame] | 273 | case SDTCisIntVectorOfSameSize: { |
| 274 | TreePatternNode *OtherOperand = |
| 275 | getOperandNum(x.SDTCisIntVectorOfSameSize_Info.OtherOperandNum, |
| 276 | N, NumResults); |
| 277 | if (OtherOperand->hasTypeSet()) { |
| 278 | if (!MVT::isVector(OtherOperand->getTypeNum(0))) |
| 279 | TP.error(N->getOperator()->getName() + " VT operand must be a vector!"); |
| 280 | MVT::ValueType IVT = OtherOperand->getTypeNum(0); |
| 281 | IVT = MVT::getIntVectorWithNumElements(MVT::getVectorNumElements(IVT)); |
| 282 | return NodeToApply->UpdateNodeType(IVT, TP); |
| 283 | } |
| 284 | return false; |
| 285 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 291 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 292 | // SDNodeInfo implementation |
| 293 | // |
| 294 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 295 | EnumName = R->getValueAsString("Opcode"); |
| 296 | SDClassName = R->getValueAsString("SDClass"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 297 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 298 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 299 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 300 | |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 301 | // Parse the properties. |
| 302 | Properties = 0; |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 303 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 304 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) { |
| 305 | if (PropList[i]->getName() == "SDNPCommutative") { |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 306 | Properties |= 1 << SDNPCommutative; |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 307 | } else if (PropList[i]->getName() == "SDNPAssociative") { |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 308 | Properties |= 1 << SDNPAssociative; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 309 | } else if (PropList[i]->getName() == "SDNPHasChain") { |
| 310 | Properties |= 1 << SDNPHasChain; |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 311 | } else if (PropList[i]->getName() == "SDNPOutFlag") { |
| 312 | Properties |= 1 << SDNPOutFlag; |
| 313 | } else if (PropList[i]->getName() == "SDNPInFlag") { |
| 314 | Properties |= 1 << SDNPInFlag; |
| 315 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 316 | Properties |= 1 << SDNPOptInFlag; |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 317 | } else { |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 318 | std::cerr << "Unknown SD Node property '" << PropList[i]->getName() |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 319 | << "' on node '" << R->getName() << "'!\n"; |
| 320 | exit(1); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 325 | // Parse the type constraints. |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 326 | std::vector<Record*> ConstraintList = |
| 327 | TypeProfile->getValueAsListOfDefs("Constraints"); |
| 328 | TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 330 | |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | // TreePatternNode implementation |
| 333 | // |
| 334 | |
| 335 | TreePatternNode::~TreePatternNode() { |
| 336 | #if 0 // FIXME: implement refcounted tree nodes! |
| 337 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 338 | delete getChild(i); |
| 339 | #endif |
| 340 | } |
| 341 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 342 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 343 | /// information. If N already contains a conflicting type, then throw an |
| 344 | /// exception. This returns true if any information was updated. |
| 345 | /// |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 346 | bool TreePatternNode::UpdateNodeType(const std::vector<unsigned char> &ExtVTs, |
| 347 | TreePattern &TP) { |
| 348 | assert(!ExtVTs.empty() && "Cannot update node type with empty type vector!"); |
| 349 | |
| 350 | if (ExtVTs[0] == MVT::isUnknown || LHSIsSubsetOfRHS(getExtTypes(), ExtVTs)) |
| 351 | return false; |
| 352 | if (isTypeCompletelyUnknown() || LHSIsSubsetOfRHS(ExtVTs, getExtTypes())) { |
| 353 | setTypes(ExtVTs); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 354 | return true; |
| 355 | } |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 356 | |
| 357 | if (getExtTypeNum(0) == MVT::iPTR) { |
| 358 | if (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::isInt) |
| 359 | return false; |
| 360 | if (isExtIntegerInVTs(ExtVTs)) { |
| 361 | std::vector<unsigned char> FVTs = FilterEVTs(ExtVTs, MVT::isInteger); |
| 362 | if (FVTs.size()) { |
| 363 | setTypes(ExtVTs); |
| 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 368 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 369 | if (ExtVTs[0] == MVT::isInt && isExtIntegerInVTs(getExtTypes())) { |
| 370 | assert(hasTypeSet() && "should be handled above!"); |
| 371 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), MVT::isInteger); |
| 372 | if (getExtTypes() == FVTs) |
| 373 | return false; |
| 374 | setTypes(FVTs); |
| 375 | return true; |
| 376 | } |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 377 | if (ExtVTs[0] == MVT::iPTR && isExtIntegerInVTs(getExtTypes())) { |
| 378 | //assert(hasTypeSet() && "should be handled above!"); |
| 379 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), MVT::isInteger); |
| 380 | if (getExtTypes() == FVTs) |
| 381 | return false; |
| 382 | if (FVTs.size()) { |
| 383 | setTypes(FVTs); |
| 384 | return true; |
| 385 | } |
| 386 | } |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 387 | if (ExtVTs[0] == MVT::isFP && isExtFloatingPointInVTs(getExtTypes())) { |
| 388 | assert(hasTypeSet() && "should be handled above!"); |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 389 | std::vector<unsigned char> FVTs = |
| 390 | FilterEVTs(getExtTypes(), MVT::isFloatingPoint); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 391 | if (getExtTypes() == FVTs) |
| 392 | return false; |
| 393 | setTypes(FVTs); |
| 394 | return true; |
| 395 | } |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 396 | |
| 397 | // If we know this is an int or fp type, and we are told it is a specific one, |
| 398 | // take the advice. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 399 | // |
| 400 | // Similarly, we should probably set the type here to the intersection of |
| 401 | // {isInt|isFP} and ExtVTs |
| 402 | if ((getExtTypeNum(0) == MVT::isInt && isExtIntegerInVTs(ExtVTs)) || |
| 403 | (getExtTypeNum(0) == MVT::isFP && isExtFloatingPointInVTs(ExtVTs))) { |
| 404 | setTypes(ExtVTs); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 405 | return true; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 406 | } |
| 407 | if (getExtTypeNum(0) == MVT::isInt && ExtVTs[0] == MVT::iPTR) { |
| 408 | setTypes(ExtVTs); |
| 409 | return true; |
| 410 | } |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 411 | |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 412 | if (isLeaf()) { |
| 413 | dump(); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 414 | std::cerr << " "; |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 415 | TP.error("Type inference contradiction found in node!"); |
| 416 | } else { |
| 417 | TP.error("Type inference contradiction found in node " + |
| 418 | getOperator()->getName() + "!"); |
| 419 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 420 | return true; // unreachable |
| 421 | } |
| 422 | |
| 423 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 424 | void TreePatternNode::print(std::ostream &OS) const { |
| 425 | if (isLeaf()) { |
| 426 | OS << *getLeafValue(); |
| 427 | } else { |
| 428 | OS << "(" << getOperator()->getName(); |
| 429 | } |
| 430 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 431 | // FIXME: At some point we should handle printing all the value types for |
| 432 | // nodes that are multiply typed. |
| 433 | switch (getExtTypeNum(0)) { |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 434 | case MVT::Other: OS << ":Other"; break; |
| 435 | case MVT::isInt: OS << ":isInt"; break; |
| 436 | case MVT::isFP : OS << ":isFP"; break; |
| 437 | case MVT::isUnknown: ; /*OS << ":?";*/ break; |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 438 | case MVT::iPTR: OS << ":iPTR"; break; |
Chris Lattner | 02cdb37 | 2006-06-20 00:56:37 +0000 | [diff] [blame] | 439 | default: { |
| 440 | std::string VTName = llvm::getName(getTypeNum(0)); |
| 441 | // Strip off MVT:: prefix if present. |
| 442 | if (VTName.substr(0,5) == "MVT::") |
| 443 | VTName = VTName.substr(5); |
| 444 | OS << ":" << VTName; |
| 445 | break; |
| 446 | } |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 447 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 448 | |
| 449 | if (!isLeaf()) { |
| 450 | if (getNumChildren() != 0) { |
| 451 | OS << " "; |
| 452 | getChild(0)->print(OS); |
| 453 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 454 | OS << ", "; |
| 455 | getChild(i)->print(OS); |
| 456 | } |
| 457 | } |
| 458 | OS << ")"; |
| 459 | } |
| 460 | |
| 461 | if (!PredicateFn.empty()) |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 462 | OS << "<<P:" << PredicateFn << ">>"; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 463 | if (TransformFn) |
| 464 | OS << "<<X:" << TransformFn->getName() << ">>"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 465 | if (!getName().empty()) |
| 466 | OS << ":$" << getName(); |
| 467 | |
| 468 | } |
| 469 | void TreePatternNode::dump() const { |
| 470 | print(std::cerr); |
| 471 | } |
| 472 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 473 | /// isIsomorphicTo - Return true if this node is recursively isomorphic to |
| 474 | /// the specified node. For this comparison, all of the state of the node |
| 475 | /// is considered, except for the assigned name. Nodes with differing names |
| 476 | /// that are otherwise identical are considered isomorphic. |
| 477 | bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N) const { |
| 478 | if (N == this) return true; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 479 | if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 480 | getPredicateFn() != N->getPredicateFn() || |
| 481 | getTransformFn() != N->getTransformFn()) |
| 482 | return false; |
| 483 | |
| 484 | if (isLeaf()) { |
| 485 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) |
| 486 | if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) |
| 487 | return DI->getDef() == NDI->getDef(); |
| 488 | return getLeafValue() == N->getLeafValue(); |
| 489 | } |
| 490 | |
| 491 | if (N->getOperator() != getOperator() || |
| 492 | N->getNumChildren() != getNumChildren()) return false; |
| 493 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 494 | if (!getChild(i)->isIsomorphicTo(N->getChild(i))) |
| 495 | return false; |
| 496 | return true; |
| 497 | } |
| 498 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 499 | /// clone - Make a copy of this tree and all of its children. |
| 500 | /// |
| 501 | TreePatternNode *TreePatternNode::clone() const { |
| 502 | TreePatternNode *New; |
| 503 | if (isLeaf()) { |
| 504 | New = new TreePatternNode(getLeafValue()); |
| 505 | } else { |
| 506 | std::vector<TreePatternNode*> CChildren; |
| 507 | CChildren.reserve(Children.size()); |
| 508 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 509 | CChildren.push_back(getChild(i)->clone()); |
| 510 | New = new TreePatternNode(getOperator(), CChildren); |
| 511 | } |
| 512 | New->setName(getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 513 | New->setTypes(getExtTypes()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 514 | New->setPredicateFn(getPredicateFn()); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 515 | New->setTransformFn(getTransformFn()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 516 | return New; |
| 517 | } |
| 518 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 519 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 520 | /// with actual values specified by ArgMap. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 521 | void TreePatternNode:: |
| 522 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 523 | if (isLeaf()) return; |
| 524 | |
| 525 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 526 | TreePatternNode *Child = getChild(i); |
| 527 | if (Child->isLeaf()) { |
| 528 | Init *Val = Child->getLeafValue(); |
| 529 | if (dynamic_cast<DefInit*>(Val) && |
| 530 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 531 | // We found a use of a formal argument, replace it with its value. |
| 532 | Child = ArgMap[Child->getName()]; |
| 533 | assert(Child && "Couldn't find formal argument!"); |
| 534 | setChild(i, Child); |
| 535 | } |
| 536 | } else { |
| 537 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | |
| 543 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 544 | /// fragments, inline them into place, giving us a pattern without any |
| 545 | /// PatFrag references. |
| 546 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 547 | if (isLeaf()) return this; // nothing to do. |
| 548 | Record *Op = getOperator(); |
| 549 | |
| 550 | if (!Op->isSubClassOf("PatFrag")) { |
| 551 | // Just recursively inline children nodes. |
| 552 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 553 | setChild(i, getChild(i)->InlinePatternFragments(TP)); |
| 554 | return this; |
| 555 | } |
| 556 | |
| 557 | // Otherwise, we found a reference to a fragment. First, look up its |
| 558 | // TreePattern record. |
| 559 | TreePattern *Frag = TP.getDAGISelEmitter().getPatternFragment(Op); |
| 560 | |
| 561 | // Verify that we are passing the right number of operands. |
| 562 | if (Frag->getNumArgs() != Children.size()) |
| 563 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 564 | utostr(Frag->getNumArgs()) + " operands!"); |
| 565 | |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 566 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 567 | |
| 568 | // Resolve formal arguments to their actual value. |
| 569 | if (Frag->getNumArgs()) { |
| 570 | // Compute the map of formal to actual arguments. |
| 571 | std::map<std::string, TreePatternNode*> ArgMap; |
| 572 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 573 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 574 | |
| 575 | FragTree->SubstituteFormalArguments(ArgMap); |
| 576 | } |
| 577 | |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 578 | FragTree->setName(getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 579 | FragTree->UpdateNodeType(getExtTypes(), TP); |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 580 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 581 | // Get a new copy of this fragment to stitch into here. |
| 582 | //delete this; // FIXME: implement refcounting! |
| 583 | return FragTree; |
| 584 | } |
| 585 | |
Chris Lattner | 52793e2 | 2006-04-06 20:19:52 +0000 | [diff] [blame] | 586 | /// getImplicitType - Check to see if the specified record has an implicit |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 587 | /// type which should be applied to it. This infer the type of register |
| 588 | /// references from the register file information, for example. |
| 589 | /// |
Chris Lattner | 52793e2 | 2006-04-06 20:19:52 +0000 | [diff] [blame] | 590 | static std::vector<unsigned char> getImplicitType(Record *R, bool NotRegisters, |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 591 | TreePattern &TP) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 592 | // Some common return values |
| 593 | std::vector<unsigned char> Unknown(1, MVT::isUnknown); |
| 594 | std::vector<unsigned char> Other(1, MVT::Other); |
| 595 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 596 | // Check to see if this is a register or a register class... |
| 597 | if (R->isSubClassOf("RegisterClass")) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 598 | if (NotRegisters) |
| 599 | return Unknown; |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 600 | const CodeGenRegisterClass &RC = |
| 601 | TP.getDAGISelEmitter().getTargetInfo().getRegisterClass(R); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 602 | return ConvertVTs(RC.getValueTypes()); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 603 | } else if (R->isSubClassOf("PatFrag")) { |
| 604 | // Pattern fragment types will be resolved when they are inlined. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 605 | return Unknown; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 606 | } else if (R->isSubClassOf("Register")) { |
Evan Cheng | 37e9005 | 2006-01-15 10:04:45 +0000 | [diff] [blame] | 607 | if (NotRegisters) |
| 608 | return Unknown; |
Chris Lattner | 22faeab | 2005-12-05 02:36:37 +0000 | [diff] [blame] | 609 | const CodeGenTarget &T = TP.getDAGISelEmitter().getTargetInfo(); |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 610 | return T.getRegisterVTs(R); |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 611 | } else if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) { |
| 612 | // Using a VTSDNode or CondCodeSDNode. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 613 | return Other; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 614 | } else if (R->isSubClassOf("ComplexPattern")) { |
Evan Cheng | 57c517d | 2006-01-17 07:36:41 +0000 | [diff] [blame] | 615 | if (NotRegisters) |
| 616 | return Unknown; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 617 | std::vector<unsigned char> |
| 618 | ComplexPat(1, TP.getDAGISelEmitter().getComplexPattern(R).getValueType()); |
| 619 | return ComplexPat; |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 620 | } else if (R->getName() == "node" || R->getName() == "srcvalue") { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 621 | // Placeholder. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 622 | return Unknown; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | TP.error("Unknown node flavor used in pattern: " + R->getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 626 | return Other; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 629 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 630 | /// this node and its children in the tree. This returns true if it makes a |
| 631 | /// change, false otherwise. If a type contradiction is found, throw an |
| 632 | /// exception. |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 633 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 634 | DAGISelEmitter &ISE = TP.getDAGISelEmitter(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 635 | if (isLeaf()) { |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 636 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 637 | // If it's a regclass or something else known, include the type. |
Chris Lattner | 52793e2 | 2006-04-06 20:19:52 +0000 | [diff] [blame] | 638 | return UpdateNodeType(getImplicitType(DI->getDef(), NotRegisters, TP),TP); |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 639 | } else if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) { |
| 640 | // Int inits are always integers. :) |
| 641 | bool MadeChange = UpdateNodeType(MVT::isInt, TP); |
| 642 | |
| 643 | if (hasTypeSet()) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 644 | // At some point, it may make sense for this tree pattern to have |
| 645 | // multiple types. Assert here that it does not, so we revisit this |
| 646 | // code when appropriate. |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 647 | assert(getExtTypes().size() >= 1 && "TreePattern doesn't have a type!"); |
Evan Cheng | 44a65fa | 2006-05-16 07:05:30 +0000 | [diff] [blame] | 648 | MVT::ValueType VT = getTypeNum(0); |
| 649 | for (unsigned i = 1, e = getExtTypes().size(); i != e; ++i) |
| 650 | assert(getTypeNum(i) == VT && "TreePattern has too many types!"); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 651 | |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 652 | VT = getTypeNum(0); |
| 653 | if (VT != MVT::iPTR) { |
| 654 | unsigned Size = MVT::getSizeInBits(VT); |
| 655 | // Make sure that the value is representable for this type. |
| 656 | if (Size < 32) { |
| 657 | int Val = (II->getValue() << (32-Size)) >> (32-Size); |
| 658 | if (Val != II->getValue()) |
| 659 | TP.error("Sign-extended integer value '" + itostr(II->getValue())+ |
| 660 | "' is out of range for type '" + |
| 661 | getEnumName(getTypeNum(0)) + "'!"); |
| 662 | } |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | |
| 666 | return MadeChange; |
| 667 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 668 | return false; |
| 669 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 670 | |
| 671 | // special handling for set, which isn't really an SDNode. |
| 672 | if (getOperator()->getName() == "set") { |
| 673 | assert (getNumChildren() == 2 && "Only handle 2 operand set's for now!"); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 674 | bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); |
| 675 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 676 | |
| 677 | // Types of operands must match. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 678 | MadeChange |= getChild(0)->UpdateNodeType(getChild(1)->getExtTypes(), TP); |
| 679 | MadeChange |= getChild(1)->UpdateNodeType(getChild(0)->getExtTypes(), TP); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 680 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 681 | return MadeChange; |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 682 | } else if (getOperator() == ISE.get_intrinsic_void_sdnode() || |
| 683 | getOperator() == ISE.get_intrinsic_w_chain_sdnode() || |
| 684 | getOperator() == ISE.get_intrinsic_wo_chain_sdnode()) { |
| 685 | unsigned IID = |
| 686 | dynamic_cast<IntInit*>(getChild(0)->getLeafValue())->getValue(); |
| 687 | const CodeGenIntrinsic &Int = ISE.getIntrinsicInfo(IID); |
| 688 | bool MadeChange = false; |
| 689 | |
| 690 | // Apply the result type to the node. |
| 691 | MadeChange = UpdateNodeType(Int.ArgVTs[0], TP); |
| 692 | |
| 693 | if (getNumChildren() != Int.ArgVTs.size()) |
Chris Lattner | 2c4e65d | 2006-03-27 22:21:18 +0000 | [diff] [blame] | 694 | TP.error("Intrinsic '" + Int.Name + "' expects " + |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 695 | utostr(Int.ArgVTs.size()-1) + " operands, not " + |
| 696 | utostr(getNumChildren()-1) + " operands!"); |
| 697 | |
| 698 | // Apply type info to the intrinsic ID. |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 699 | MadeChange |= getChild(0)->UpdateNodeType(MVT::iPTR, TP); |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 700 | |
| 701 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 702 | MVT::ValueType OpVT = Int.ArgVTs[i]; |
| 703 | MadeChange |= getChild(i)->UpdateNodeType(OpVT, TP); |
| 704 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 705 | } |
| 706 | return MadeChange; |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 707 | } else if (getOperator()->isSubClassOf("SDNode")) { |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 708 | const SDNodeInfo &NI = ISE.getSDNodeInfo(getOperator()); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 709 | |
| 710 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 711 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 712 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 713 | // Branch, etc. do not produce results and top-level forms in instr pattern |
| 714 | // must have void types. |
| 715 | if (NI.getNumResults() == 0) |
| 716 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | 91ded08 | 2006-04-06 20:36:51 +0000 | [diff] [blame] | 717 | |
| 718 | // If this is a vector_shuffle operation, apply types to the build_vector |
| 719 | // operation. The types of the integers don't matter, but this ensures they |
| 720 | // won't get checked. |
| 721 | if (getOperator()->getName() == "vector_shuffle" && |
| 722 | getChild(2)->getOperator()->getName() == "build_vector") { |
| 723 | TreePatternNode *BV = getChild(2); |
| 724 | const std::vector<MVT::ValueType> &LegalVTs |
| 725 | = ISE.getTargetInfo().getLegalValueTypes(); |
| 726 | MVT::ValueType LegalIntVT = MVT::Other; |
| 727 | for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i) |
| 728 | if (MVT::isInteger(LegalVTs[i]) && !MVT::isVector(LegalVTs[i])) { |
| 729 | LegalIntVT = LegalVTs[i]; |
| 730 | break; |
| 731 | } |
| 732 | assert(LegalIntVT != MVT::Other && "No legal integer VT?"); |
| 733 | |
| 734 | for (unsigned i = 0, e = BV->getNumChildren(); i != e; ++i) |
| 735 | MadeChange |= BV->getChild(i)->UpdateNodeType(LegalIntVT, TP); |
| 736 | } |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 737 | return MadeChange; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 738 | } else if (getOperator()->isSubClassOf("Instruction")) { |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 739 | const DAGInstruction &Inst = ISE.getInstruction(getOperator()); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 740 | bool MadeChange = false; |
| 741 | unsigned NumResults = Inst.getNumResults(); |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 742 | |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 743 | assert(NumResults <= 1 && |
| 744 | "Only supports zero or one result instrs!"); |
Evan Cheng | eb1f40d | 2006-07-20 23:36:20 +0000 | [diff] [blame] | 745 | |
| 746 | CodeGenInstruction &InstInfo = |
| 747 | ISE.getTargetInfo().getInstruction(getOperator()->getName()); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 748 | // Apply the result type to the node |
Evan Cheng | eb1f40d | 2006-07-20 23:36:20 +0000 | [diff] [blame] | 749 | if (NumResults == 0 || InstInfo.noResults) { // FIXME: temporary hack... |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 750 | MadeChange = UpdateNodeType(MVT::isVoid, TP); |
| 751 | } else { |
| 752 | Record *ResultNode = Inst.getResult(0); |
| 753 | assert(ResultNode->isSubClassOf("RegisterClass") && |
| 754 | "Operands should be register classes!"); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 755 | |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 756 | const CodeGenRegisterClass &RC = |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 757 | ISE.getTargetInfo().getRegisterClass(ResultNode); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 758 | MadeChange = UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 760 | |
| 761 | if (getNumChildren() != Inst.getNumOperands()) |
| 762 | TP.error("Instruction '" + getOperator()->getName() + " expects " + |
| 763 | utostr(Inst.getNumOperands()) + " operands, not " + |
| 764 | utostr(getNumChildren()) + " operands!"); |
| 765 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 766 | Record *OperandNode = Inst.getOperand(i); |
| 767 | MVT::ValueType VT; |
| 768 | if (OperandNode->isSubClassOf("RegisterClass")) { |
| 769 | const CodeGenRegisterClass &RC = |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 770 | ISE.getTargetInfo().getRegisterClass(OperandNode); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 771 | //VT = RC.getValueTypeNum(0); |
| 772 | MadeChange |=getChild(i)->UpdateNodeType(ConvertVTs(RC.getValueTypes()), |
| 773 | TP); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 774 | } else if (OperandNode->isSubClassOf("Operand")) { |
| 775 | VT = getValueType(OperandNode->getValueAsDef("Type")); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 776 | MadeChange |= getChild(i)->UpdateNodeType(VT, TP); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 777 | } else { |
| 778 | assert(0 && "Unknown operand type!"); |
| 779 | abort(); |
| 780 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 781 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 782 | } |
| 783 | return MadeChange; |
| 784 | } else { |
| 785 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); |
| 786 | |
Evan Cheng | f26ba69 | 2006-03-20 08:09:17 +0000 | [diff] [blame] | 787 | // Node transforms always take one operand. |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 788 | if (getNumChildren() != 1) |
| 789 | TP.error("Node transform '" + getOperator()->getName() + |
| 790 | "' requires one operand!"); |
Chris Lattner | 4e2f54d | 2006-03-21 06:42:58 +0000 | [diff] [blame] | 791 | |
| 792 | // If either the output or input of the xform does not have exact |
| 793 | // type info. We assume they must be the same. Otherwise, it is perfectly |
| 794 | // legal to transform from one type to a completely different type. |
| 795 | if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { |
Evan Cheng | f26ba69 | 2006-03-20 08:09:17 +0000 | [diff] [blame] | 796 | bool MadeChange = UpdateNodeType(getChild(0)->getExtTypes(), TP); |
| 797 | MadeChange |= getChild(0)->UpdateNodeType(getExtTypes(), TP); |
| 798 | return MadeChange; |
| 799 | } |
| 800 | return false; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 801 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 804 | /// canPatternMatch - If it is impossible for this pattern to match on this |
| 805 | /// target, fill in Reason and return false. Otherwise, return true. This is |
| 806 | /// used as a santity check for .td files (to prevent people from writing stuff |
| 807 | /// that can never possibly work), and to prevent the pattern permuter from |
| 808 | /// generating stuff that is useless. |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 809 | bool TreePatternNode::canPatternMatch(std::string &Reason, DAGISelEmitter &ISE){ |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 810 | if (isLeaf()) return true; |
| 811 | |
| 812 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 813 | if (!getChild(i)->canPatternMatch(Reason, ISE)) |
| 814 | return false; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 550525e | 2006-03-24 21:48:51 +0000 | [diff] [blame] | 816 | // If this is an intrinsic, handle cases that would make it not match. For |
| 817 | // example, if an operand is required to be an immediate. |
| 818 | if (getOperator()->isSubClassOf("Intrinsic")) { |
| 819 | // TODO: |
| 820 | return true; |
| 821 | } |
| 822 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 823 | // If this node is a commutative operator, check that the LHS isn't an |
| 824 | // immediate. |
| 825 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(getOperator()); |
| 826 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) { |
| 827 | // Scan all of the operands of the node and make sure that only the last one |
| 828 | // is a constant node. |
| 829 | for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) |
| 830 | if (!getChild(i)->isLeaf() && |
| 831 | getChild(i)->getOperator()->getName() == "imm") { |
| 832 | Reason = "Immediate value must be on the RHS of commutative operators!"; |
| 833 | return false; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | return true; |
| 838 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 840 | //===----------------------------------------------------------------------===// |
| 841 | // TreePattern implementation |
| 842 | // |
| 843 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 844 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 845 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 846 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 847 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
| 848 | Trees.push_back(ParseTreePattern((DagInit*)RawPat->getElement(i))); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 851 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 852 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 853 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 854 | Trees.push_back(ParseTreePattern(Pat)); |
| 855 | } |
| 856 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 857 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 858 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 859 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 860 | Trees.push_back(Pat); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 865 | void TreePattern::error(const std::string &Msg) const { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 866 | dump(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 867 | throw "In " + TheRecord->getName() + ": " + Msg; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 870 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
Chris Lattner | 8c06318 | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 871 | DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator()); |
| 872 | if (!OpDef) error("Pattern has unexpected operator type!"); |
| 873 | Record *Operator = OpDef->getDef(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 874 | |
| 875 | if (Operator->isSubClassOf("ValueType")) { |
| 876 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 877 | // node. |
| 878 | if (Dag->getNumArgs() != 1) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 879 | error("Type cast only takes one operand!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 880 | |
| 881 | Init *Arg = Dag->getArg(0); |
| 882 | TreePatternNode *New; |
| 883 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 884 | Record *R = DI->getDef(); |
| 885 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
Chris Lattner | 8c06318 | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 886 | Dag->setArg(0, new DagInit(DI, |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 887 | std::vector<std::pair<Init*, std::string> >())); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 888 | return ParseTreePattern(Dag); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 889 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 890 | New = new TreePatternNode(DI); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 891 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 892 | New = ParseTreePattern(DI); |
Chris Lattner | 0614b62 | 2005-11-02 06:49:14 +0000 | [diff] [blame] | 893 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 894 | New = new TreePatternNode(II); |
| 895 | if (!Dag->getArgName(0).empty()) |
| 896 | error("Constant int argument should not have a name!"); |
Chris Lattner | ffa4fdc | 2006-03-31 05:25:56 +0000 | [diff] [blame] | 897 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 898 | // Turn this into an IntInit. |
| 899 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 900 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 901 | error("Bits value must be constants!"); |
| 902 | |
| 903 | New = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 904 | if (!Dag->getArgName(0).empty()) |
| 905 | error("Constant int argument should not have a name!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 906 | } else { |
| 907 | Arg->dump(); |
| 908 | error("Unknown leaf value for tree pattern!"); |
| 909 | return 0; |
| 910 | } |
| 911 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 912 | // Apply the type cast. |
| 913 | New->UpdateNodeType(getValueType(Operator), *this); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 914 | New->setName(Dag->getArgName(0)); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 915 | return New; |
| 916 | } |
| 917 | |
| 918 | // Verify that this is something that makes sense for an operator. |
| 919 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 920 | !Operator->isSubClassOf("Instruction") && |
| 921 | !Operator->isSubClassOf("SDNodeXForm") && |
Chris Lattner | 550525e | 2006-03-24 21:48:51 +0000 | [diff] [blame] | 922 | !Operator->isSubClassOf("Intrinsic") && |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 923 | Operator->getName() != "set") |
| 924 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 925 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 926 | // Check to see if this is something that is illegal in an input pattern. |
| 927 | if (isInputPattern && (Operator->isSubClassOf("Instruction") || |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 928 | Operator->isSubClassOf("SDNodeXForm"))) |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 929 | error("Cannot use '" + Operator->getName() + "' in an input pattern!"); |
| 930 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 931 | std::vector<TreePatternNode*> Children; |
| 932 | |
| 933 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 934 | Init *Arg = Dag->getArg(i); |
| 935 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 936 | Children.push_back(ParseTreePattern(DI)); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 937 | if (Children.back()->getName().empty()) |
| 938 | Children.back()->setName(Dag->getArgName(i)); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 939 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 940 | Record *R = DefI->getDef(); |
| 941 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 942 | // TreePatternNode if its own. |
| 943 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
Chris Lattner | 8c06318 | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 944 | Dag->setArg(i, new DagInit(DefI, |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 945 | std::vector<std::pair<Init*, std::string> >())); |
| 946 | --i; // Revisit this node... |
| 947 | } else { |
| 948 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 949 | Node->setName(Dag->getArgName(i)); |
| 950 | Children.push_back(Node); |
| 951 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 952 | // Input argument? |
| 953 | if (R->getName() == "node") { |
| 954 | if (Dag->getArgName(i).empty()) |
| 955 | error("'node' argument requires a name to match with operand list"); |
| 956 | Args.push_back(Dag->getArgName(i)); |
| 957 | } |
| 958 | } |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 959 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 960 | TreePatternNode *Node = new TreePatternNode(II); |
| 961 | if (!Dag->getArgName(i).empty()) |
| 962 | error("Constant int argument should not have a name!"); |
| 963 | Children.push_back(Node); |
Chris Lattner | ffa4fdc | 2006-03-31 05:25:56 +0000 | [diff] [blame] | 964 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 965 | // Turn this into an IntInit. |
| 966 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 967 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 968 | error("Bits value must be constants!"); |
| 969 | |
| 970 | TreePatternNode *Node = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 971 | if (!Dag->getArgName(i).empty()) |
| 972 | error("Constant int argument should not have a name!"); |
| 973 | Children.push_back(Node); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 974 | } else { |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 975 | std::cerr << '"'; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 976 | Arg->dump(); |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 977 | std::cerr << "\": "; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 978 | error("Unknown leaf value for tree pattern!"); |
| 979 | } |
| 980 | } |
| 981 | |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 982 | // If the operator is an intrinsic, then this is just syntactic sugar for for |
| 983 | // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and |
| 984 | // convert the intrinsic name to a number. |
| 985 | if (Operator->isSubClassOf("Intrinsic")) { |
| 986 | const CodeGenIntrinsic &Int = getDAGISelEmitter().getIntrinsic(Operator); |
| 987 | unsigned IID = getDAGISelEmitter().getIntrinsicID(Operator)+1; |
| 988 | |
| 989 | // If this intrinsic returns void, it must have side-effects and thus a |
| 990 | // chain. |
| 991 | if (Int.ArgVTs[0] == MVT::isVoid) { |
| 992 | Operator = getDAGISelEmitter().get_intrinsic_void_sdnode(); |
| 993 | } else if (Int.ModRef != CodeGenIntrinsic::NoMem) { |
| 994 | // Has side-effects, requires chain. |
| 995 | Operator = getDAGISelEmitter().get_intrinsic_w_chain_sdnode(); |
| 996 | } else { |
| 997 | // Otherwise, no chain. |
| 998 | Operator = getDAGISelEmitter().get_intrinsic_wo_chain_sdnode(); |
| 999 | } |
| 1000 | |
| 1001 | TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID)); |
| 1002 | Children.insert(Children.begin(), IIDNode); |
| 1003 | } |
| 1004 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1005 | return new TreePatternNode(Operator, Children); |
| 1006 | } |
| 1007 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1008 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 1009 | /// patterns as possible. Return true if all types are infered, false |
| 1010 | /// otherwise. Throw an exception if a type contradiction is found. |
| 1011 | bool TreePattern::InferAllTypes() { |
| 1012 | bool MadeChange = true; |
| 1013 | while (MadeChange) { |
| 1014 | MadeChange = false; |
| 1015 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1016 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | bool HasUnresolvedTypes = false; |
| 1020 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 1021 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 1022 | return !HasUnresolvedTypes; |
| 1023 | } |
| 1024 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1025 | void TreePattern::print(std::ostream &OS) const { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1026 | OS << getRecord()->getName(); |
| 1027 | if (!Args.empty()) { |
| 1028 | OS << "(" << Args[0]; |
| 1029 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 1030 | OS << ", " << Args[i]; |
| 1031 | OS << ")"; |
| 1032 | } |
| 1033 | OS << ": "; |
| 1034 | |
| 1035 | if (Trees.size() > 1) |
| 1036 | OS << "[\n"; |
| 1037 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 1038 | OS << "\t"; |
| 1039 | Trees[i]->print(OS); |
| 1040 | OS << "\n"; |
| 1041 | } |
| 1042 | |
| 1043 | if (Trees.size() > 1) |
| 1044 | OS << "]\n"; |
| 1045 | } |
| 1046 | |
| 1047 | void TreePattern::dump() const { print(std::cerr); } |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | //===----------------------------------------------------------------------===// |
| 1052 | // DAGISelEmitter implementation |
| 1053 | // |
| 1054 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 1055 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
| 1056 | void DAGISelEmitter::ParseNodeInfo() { |
| 1057 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 1058 | while (!Nodes.empty()) { |
| 1059 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 1060 | Nodes.pop_back(); |
| 1061 | } |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Get the buildin intrinsic nodes. |
| 1064 | intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); |
| 1065 | intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); |
| 1066 | intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1069 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 1070 | /// map, and emit them to the file as functions. |
| 1071 | void DAGISelEmitter::ParseNodeTransforms(std::ostream &OS) { |
| 1072 | OS << "\n// Node transformations.\n"; |
| 1073 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 1074 | while (!Xforms.empty()) { |
| 1075 | Record *XFormNode = Xforms.back(); |
| 1076 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 1077 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
| 1078 | SDNodeXForms.insert(std::make_pair(XFormNode, |
| 1079 | std::make_pair(SDNode, Code))); |
| 1080 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 1081 | if (!Code.empty()) { |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1082 | std::string ClassName = getSDNodeInfo(SDNode).getSDClassName(); |
| 1083 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 1084 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 1085 | OS << "inline SDOperand Transform_" << XFormNode->getName() |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1086 | << "(SDNode *" << C2 << ") {\n"; |
| 1087 | if (ClassName != "SDNode") |
| 1088 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 1089 | OS << Code << "\n}\n"; |
| 1090 | } |
| 1091 | |
| 1092 | Xforms.pop_back(); |
| 1093 | } |
| 1094 | } |
| 1095 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1096 | void DAGISelEmitter::ParseComplexPatterns() { |
| 1097 | std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); |
| 1098 | while (!AMs.empty()) { |
| 1099 | ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); |
| 1100 | AMs.pop_back(); |
| 1101 | } |
| 1102 | } |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1103 | |
| 1104 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1105 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 1106 | /// file, building up the PatternFragments map. After we've collected them all, |
| 1107 | /// inline fragments together as necessary, so that there are no references left |
| 1108 | /// inside a pattern fragment to a pattern fragment. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1109 | /// |
| 1110 | /// This also emits all of the predicate functions to the output file. |
| 1111 | /// |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1112 | void DAGISelEmitter::ParsePatternFragments(std::ostream &OS) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1113 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 1114 | |
| 1115 | // First step, parse all of the fragments and emit predicate functions. |
| 1116 | OS << "\n// Predicate functions.\n"; |
| 1117 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1118 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1119 | TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1120 | PatternFragments[Fragments[i]] = P; |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 1121 | |
| 1122 | // Validate the argument list, converting it to map, to discard duplicates. |
| 1123 | std::vector<std::string> &Args = P->getArgList(); |
| 1124 | std::set<std::string> OperandsMap(Args.begin(), Args.end()); |
| 1125 | |
| 1126 | if (OperandsMap.count("")) |
| 1127 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 1128 | |
| 1129 | // Parse the operands list. |
| 1130 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
Chris Lattner | 8c06318 | 2006-03-30 22:50:40 +0000 | [diff] [blame] | 1131 | DefInit *OpsOp = dynamic_cast<DefInit*>(OpsList->getOperator()); |
| 1132 | if (!OpsOp || OpsOp->getDef()->getName() != "ops") |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 1133 | P->error("Operands list should start with '(ops ... '!"); |
| 1134 | |
| 1135 | // Copy over the arguments. |
| 1136 | Args.clear(); |
| 1137 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 1138 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 1139 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 1140 | getDef()->getName() != "node") |
| 1141 | P->error("Operands list should all be 'node' values."); |
| 1142 | if (OpsList->getArgName(j).empty()) |
| 1143 | P->error("Operands list should have names for each operand!"); |
| 1144 | if (!OperandsMap.count(OpsList->getArgName(j))) |
| 1145 | P->error("'" + OpsList->getArgName(j) + |
| 1146 | "' does not occur in pattern or was multiply specified!"); |
| 1147 | OperandsMap.erase(OpsList->getArgName(j)); |
| 1148 | Args.push_back(OpsList->getArgName(j)); |
| 1149 | } |
| 1150 | |
| 1151 | if (!OperandsMap.empty()) |
| 1152 | P->error("Operands list does not contain an entry for operand '" + |
| 1153 | *OperandsMap.begin() + "'!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1154 | |
| 1155 | // If there is a code init for this fragment, emit the predicate code and |
| 1156 | // keep track of the fact that this fragment uses it. |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1157 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
| 1158 | if (!Code.empty()) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 1159 | assert(!P->getOnlyTree()->isLeaf() && "Can't be a leaf!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1160 | std::string ClassName = |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 1161 | getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1162 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 1163 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 1164 | OS << "inline bool Predicate_" << Fragments[i]->getName() |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1165 | << "(SDNode *" << C2 << ") {\n"; |
| 1166 | if (ClassName != "SDNode") |
| 1167 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1168 | OS << Code << "\n}\n"; |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 1169 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1170 | } |
Chris Lattner | 6de8b53 | 2005-09-13 21:59:15 +0000 | [diff] [blame] | 1171 | |
| 1172 | // If there is a node transformation corresponding to this, keep track of |
| 1173 | // it. |
| 1174 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 1175 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1176 | P->getOnlyTree()->setTransformFn(Transform); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | OS << "\n\n"; |
| 1180 | |
| 1181 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 1182 | // that there are not references to PatFrags left inside of them. |
| 1183 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 1184 | E = PatternFragments.end(); I != E; ++I) { |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1185 | TreePattern *ThePat = I->second; |
| 1186 | ThePat->InlinePatternFragments(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 1187 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1188 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 1189 | // all of them, some may depend on the inputs of the pattern. |
| 1190 | try { |
| 1191 | ThePat->InferAllTypes(); |
| 1192 | } catch (...) { |
| 1193 | // If this pattern fragment is not supported by this target (no types can |
| 1194 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 1195 | // actually used by instructions, the type consistency error will be |
| 1196 | // reported there. |
| 1197 | } |
| 1198 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1199 | // If debugging, print out the pattern fragment result. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1200 | DEBUG(ThePat->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1204 | /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1205 | /// instruction input. Return true if this is a real use. |
| 1206 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1207 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1208 | std::vector<Record*> &InstImpInputs) { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1209 | // No name -> not interesting. |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1210 | if (Pat->getName().empty()) { |
| 1211 | if (Pat->isLeaf()) { |
| 1212 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1213 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 1214 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1215 | else if (DI && DI->getDef()->isSubClassOf("Register")) |
| 1216 | InstImpInputs.push_back(DI->getDef()); |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1217 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1218 | return false; |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1219 | } |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1220 | |
| 1221 | Record *Rec; |
| 1222 | if (Pat->isLeaf()) { |
| 1223 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1224 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 1225 | Rec = DI->getDef(); |
| 1226 | } else { |
| 1227 | assert(Pat->getNumChildren() == 0 && "can't be a use with children!"); |
| 1228 | Rec = Pat->getOperator(); |
| 1229 | } |
| 1230 | |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 1231 | // SRCVALUE nodes are ignored. |
| 1232 | if (Rec->getName() == "srcvalue") |
| 1233 | return false; |
| 1234 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1235 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 1236 | if (!Slot) { |
| 1237 | Slot = Pat; |
| 1238 | } else { |
| 1239 | Record *SlotRec; |
| 1240 | if (Slot->isLeaf()) { |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1241 | SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1242 | } else { |
| 1243 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 1244 | SlotRec = Slot->getOperator(); |
| 1245 | } |
| 1246 | |
| 1247 | // Ensure that the inputs agree if we've already seen this input. |
| 1248 | if (Rec != SlotRec) |
| 1249 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1250 | if (Slot->getExtTypes() != Pat->getExtTypes()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1251 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 1252 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1253 | return true; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 1257 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 1258 | /// the pattern. Report errors if we see anything naughty. |
| 1259 | void DAGISelEmitter:: |
| 1260 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 1261 | std::map<std::string, TreePatternNode*> &InstInputs, |
Chris Lattner | 947604b | 2006-03-24 21:52:20 +0000 | [diff] [blame] | 1262 | std::map<std::string, TreePatternNode*>&InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1263 | std::vector<Record*> &InstImpInputs, |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1264 | std::vector<Record*> &InstImpResults) { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1265 | if (Pat->isLeaf()) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1266 | bool isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1267 | if (!isUse && Pat->getTransformFn()) |
| 1268 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | } else if (Pat->getOperator()->getName() != "set") { |
| 1271 | // If this is not a set, verify that the children nodes are not void typed, |
| 1272 | // and recurse. |
| 1273 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1274 | if (Pat->getChild(i)->getExtTypeNum(0) == MVT::isVoid) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1275 | I->error("Cannot have void nodes inside of patterns!"); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1276 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1277 | InstImpInputs, InstImpResults); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | // If this is a non-leaf node with no children, treat it basically as if |
| 1281 | // it were a leaf. This handles nodes like (imm). |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1282 | bool isUse = false; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1283 | if (Pat->getNumChildren() == 0) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1284 | isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1286 | if (!isUse && Pat->getTransformFn()) |
| 1287 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | // Otherwise, this is a set, validate and collect instruction results. |
| 1292 | if (Pat->getNumChildren() == 0) |
| 1293 | I->error("set requires operands!"); |
| 1294 | else if (Pat->getNumChildren() & 1) |
| 1295 | I->error("set requires an even number of operands"); |
| 1296 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1297 | if (Pat->getTransformFn()) |
| 1298 | I->error("Cannot specify a transform function on a set node!"); |
| 1299 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1300 | // Check the set destinations. |
| 1301 | unsigned NumValues = Pat->getNumChildren()/2; |
| 1302 | for (unsigned i = 0; i != NumValues; ++i) { |
| 1303 | TreePatternNode *Dest = Pat->getChild(i); |
| 1304 | if (!Dest->isLeaf()) |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 1305 | I->error("set destination should be a register!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1306 | |
| 1307 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1308 | if (!Val) |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 1309 | I->error("set destination should be a register!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1310 | |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1311 | if (Val->getDef()->isSubClassOf("RegisterClass")) { |
| 1312 | if (Dest->getName().empty()) |
| 1313 | I->error("set destination must have a name!"); |
| 1314 | if (InstResults.count(Dest->getName())) |
| 1315 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1316 | InstResults[Dest->getName()] = Dest; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1317 | } else if (Val->getDef()->isSubClassOf("Register")) { |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1318 | InstImpResults.push_back(Val->getDef()); |
| 1319 | } else { |
| 1320 | I->error("set destination should be a register!"); |
| 1321 | } |
| 1322 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1323 | // Verify and collect info from the computation. |
| 1324 | FindPatternInputsAndOutputs(I, Pat->getChild(i+NumValues), |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1325 | InstInputs, InstResults, |
| 1326 | InstImpInputs, InstImpResults); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1327 | } |
| 1328 | } |
| 1329 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1330 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 1331 | /// any fragments involved. This populates the Instructions list with fully |
| 1332 | /// resolved instructions. |
| 1333 | void DAGISelEmitter::ParseInstructions() { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1334 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 1335 | |
| 1336 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1337 | ListInit *LI = 0; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1338 | |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1339 | if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 1340 | LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 1341 | |
| 1342 | // If there is no pattern, only collect minimal information about the |
| 1343 | // instruction for its operand list. We have to assume that there is one |
| 1344 | // result, as we have no detailed info. |
| 1345 | if (!LI || LI->getSize() == 0) { |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1346 | std::vector<Record*> Results; |
| 1347 | std::vector<Record*> Operands; |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1348 | |
| 1349 | CodeGenInstruction &InstInfo =Target.getInstruction(Instrs[i]->getName()); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1350 | |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1351 | if (InstInfo.OperandList.size() != 0) { |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1352 | // FIXME: temporary hack... |
Evan Cheng | 2b4ea79 | 2005-12-26 09:11:45 +0000 | [diff] [blame] | 1353 | if (InstInfo.noResults) { |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1354 | // These produce no results |
| 1355 | for (unsigned j = 0, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1356 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1357 | } else { |
| 1358 | // Assume the first operand is the result. |
| 1359 | Results.push_back(InstInfo.OperandList[0].Rec); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1360 | |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1361 | // The rest are inputs. |
| 1362 | for (unsigned j = 1, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1363 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1364 | } |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1365 | } |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Create and insert the instruction. |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1368 | std::vector<Record*> ImpResults; |
| 1369 | std::vector<Record*> ImpOperands; |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1370 | Instructions.insert(std::make_pair(Instrs[i], |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1371 | DAGInstruction(0, Results, Operands, ImpResults, |
| 1372 | ImpOperands))); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1373 | continue; // no pattern. |
| 1374 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1376 | // Parse the instruction. |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1377 | TreePattern *I = new TreePattern(Instrs[i], LI, true, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1378 | // Inline pattern fragments into it. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1379 | I->InlinePatternFragments(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1380 | |
Chris Lattner | 95f6b76 | 2005-09-08 23:26:30 +0000 | [diff] [blame] | 1381 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1382 | // never do anything with this instruction pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1383 | if (!I->InferAllTypes()) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1384 | I->error("Could not infer all types in pattern!"); |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 1385 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1386 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 1387 | // with the record they are declared as. |
| 1388 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1389 | |
| 1390 | // InstResults - Keep track of all the virtual registers that are 'set' |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1391 | // in the instruction, including what reg class they are. |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1392 | std::map<std::string, TreePatternNode*> InstResults; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1393 | |
| 1394 | std::vector<Record*> InstImpInputs; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1395 | std::vector<Record*> InstImpResults; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1396 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1397 | // Verify that the top-level forms in the instruction are of void type, and |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1398 | // fill in the InstResults map. |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1399 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 1400 | TreePatternNode *Pat = I->getTree(j); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1401 | if (Pat->getExtTypeNum(0) != MVT::isVoid) |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 1402 | I->error("Top-level forms in instruction pattern should have" |
| 1403 | " void types"); |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1405 | // Find inputs and outputs, and verify the structure of the uses/defs. |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1406 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1407 | InstImpInputs, InstImpResults); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1408 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1409 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1410 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 1411 | // list for the instruction. This determines the order that operands are |
| 1412 | // added to the machine instruction the node corresponds to. |
| 1413 | unsigned NumResults = InstResults.size(); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1414 | |
| 1415 | // Parse the operands list from the (ops) list, validating it. |
| 1416 | std::vector<std::string> &Args = I->getArgList(); |
| 1417 | assert(Args.empty() && "Args list should still be empty here!"); |
| 1418 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 1419 | |
| 1420 | // Check that all of the results occur first in the list. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1421 | std::vector<Record*> Results; |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1422 | TreePatternNode *Res0Node = NULL; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1423 | for (unsigned i = 0; i != NumResults; ++i) { |
Chris Lattner | 3a7319d | 2005-09-14 21:04:12 +0000 | [diff] [blame] | 1424 | if (i == CGI.OperandList.size()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1425 | I->error("'" + InstResults.begin()->first + |
| 1426 | "' set but does not appear in operand list!"); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1427 | const std::string &OpName = CGI.OperandList[i].Name; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1429 | // Check that it exists in InstResults. |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1430 | TreePatternNode *RNode = InstResults[OpName]; |
Chris Lattner | 5c4c774 | 2006-03-25 22:12:44 +0000 | [diff] [blame] | 1431 | if (RNode == 0) |
| 1432 | I->error("Operand $" + OpName + " does not exist in operand list!"); |
| 1433 | |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1434 | if (i == 0) |
| 1435 | Res0Node = RNode; |
| 1436 | Record *R = dynamic_cast<DefInit*>(RNode->getLeafValue())->getDef(); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1437 | if (R == 0) |
| 1438 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 1439 | "outputs must occur before inputs in operand list!"); |
| 1440 | |
| 1441 | if (CGI.OperandList[i].Rec != R) |
| 1442 | I->error("Operand $" + OpName + " class mismatch!"); |
| 1443 | |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 1444 | // Remember the return type. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1445 | Results.push_back(CGI.OperandList[i].Rec); |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 1446 | |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1447 | // Okay, this one checks out. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1448 | InstResults.erase(OpName); |
| 1449 | } |
| 1450 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1451 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 1452 | // the copy while we're checking the inputs. |
| 1453 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1454 | |
| 1455 | std::vector<TreePatternNode*> ResultNodeOperands; |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1456 | std::vector<Record*> Operands; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1457 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 1458 | const std::string &OpName = CGI.OperandList[i].Name; |
| 1459 | if (OpName.empty()) |
| 1460 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 1461 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1462 | if (!InstInputsCheck.count(OpName)) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1463 | I->error("Operand $" + OpName + |
| 1464 | " does not appear in the instruction pattern"); |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1465 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1466 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1467 | |
| 1468 | if (InVal->isLeaf() && |
| 1469 | dynamic_cast<DefInit*>(InVal->getLeafValue())) { |
| 1470 | Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1471 | if (CGI.OperandList[i].Rec != InRec && |
| 1472 | !InRec->isSubClassOf("ComplexPattern")) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1473 | I->error("Operand $" + OpName + "'s register class disagrees" |
| 1474 | " between the operand and pattern"); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1475 | } |
| 1476 | Operands.push_back(CGI.OperandList[i].Rec); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1477 | |
Chris Lattner | 2175c18 | 2005-09-14 23:01:59 +0000 | [diff] [blame] | 1478 | // Construct the result for the dest-pattern operand list. |
| 1479 | TreePatternNode *OpNode = InVal->clone(); |
| 1480 | |
| 1481 | // No predicate is useful on the result. |
| 1482 | OpNode->setPredicateFn(""); |
| 1483 | |
| 1484 | // Promote the xform function to be an explicit node if set. |
| 1485 | if (Record *Xform = OpNode->getTransformFn()) { |
| 1486 | OpNode->setTransformFn(0); |
| 1487 | std::vector<TreePatternNode*> Children; |
| 1488 | Children.push_back(OpNode); |
| 1489 | OpNode = new TreePatternNode(Xform, Children); |
| 1490 | } |
| 1491 | |
| 1492 | ResultNodeOperands.push_back(OpNode); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1495 | if (!InstInputsCheck.empty()) |
| 1496 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 1497 | " occurs in pattern but not in operands list!"); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1498 | |
| 1499 | TreePatternNode *ResultPattern = |
| 1500 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1501 | // Copy fully inferred output node type to instruction result pattern. |
| 1502 | if (NumResults > 0) |
| 1503 | ResultPattern->setTypes(Res0Node->getExtTypes()); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1504 | |
| 1505 | // Create and insert the instruction. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1506 | DAGInstruction TheInst(I, Results, Operands, InstImpResults, InstImpInputs); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1507 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
| 1508 | |
| 1509 | // Use a temporary tree pattern to infer all types and make sure that the |
| 1510 | // constructed result is correct. This depends on the instruction already |
| 1511 | // being inserted into the Instructions map. |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1512 | TreePattern Temp(I->getRecord(), ResultPattern, false, *this); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1513 | Temp.InferAllTypes(); |
| 1514 | |
| 1515 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
| 1516 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1517 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1518 | DEBUG(I->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1519 | } |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1520 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1521 | // If we can, convert the instructions to be patterns that are matched! |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 1522 | for (std::map<Record*, DAGInstruction>::iterator II = Instructions.begin(), |
| 1523 | E = Instructions.end(); II != E; ++II) { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1524 | DAGInstruction &TheInst = II->second; |
| 1525 | TreePattern *I = TheInst.getPattern(); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1526 | if (I == 0) continue; // No pattern. |
Evan Cheng | dd304dd | 2005-12-05 23:08:55 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1528 | if (I->getNumTrees() != 1) { |
| 1529 | std::cerr << "CANNOT HANDLE: " << I->getRecord()->getName() << " yet!"; |
| 1530 | continue; |
| 1531 | } |
| 1532 | TreePatternNode *Pattern = I->getTree(0); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1533 | TreePatternNode *SrcPattern; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1534 | if (Pattern->getOperator()->getName() == "set") { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1535 | if (Pattern->getNumChildren() != 2) |
| 1536 | continue; // Not a set of a single value (not handled so far) |
| 1537 | |
| 1538 | SrcPattern = Pattern->getChild(1)->clone(); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1539 | } else{ |
| 1540 | // Not a set (store or something?) |
| 1541 | SrcPattern = Pattern; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1542 | } |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1543 | |
| 1544 | std::string Reason; |
| 1545 | if (!SrcPattern->canPatternMatch(Reason, *this)) |
| 1546 | I->error("Instruction can never match: " + Reason); |
| 1547 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1548 | Record *Instr = II->first; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1549 | TreePatternNode *DstPattern = TheInst.getResultPattern(); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1550 | PatternsToMatch. |
| 1551 | push_back(PatternToMatch(Instr->getValueAsListInit("Predicates"), |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1552 | SrcPattern, DstPattern, |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1553 | Instr->getValueAsInt("AddedComplexity"))); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1554 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1557 | void DAGISelEmitter::ParsePatterns() { |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1558 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1559 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1560 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1561 | DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch"); |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1562 | TreePattern *Pattern = new TreePattern(Patterns[i], Tree, true, *this); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1563 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1564 | // Inline pattern fragments into it. |
| 1565 | Pattern->InlinePatternFragments(); |
| 1566 | |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1567 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 1568 | if (LI->getSize() == 0) continue; // no pattern. |
| 1569 | |
| 1570 | // Parse the instruction. |
| 1571 | TreePattern *Result = new TreePattern(Patterns[i], LI, false, *this); |
| 1572 | |
| 1573 | // Inline pattern fragments into it. |
| 1574 | Result->InlinePatternFragments(); |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1575 | |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1576 | if (Result->getNumTrees() != 1) |
| 1577 | Result->error("Cannot handle instructions producing instructions " |
| 1578 | "with temporaries yet!"); |
| 1579 | |
| 1580 | bool IterateInference; |
Chris Lattner | 186fb7d | 2006-06-20 00:31:27 +0000 | [diff] [blame] | 1581 | bool InferredAllPatternTypes, InferredAllResultTypes; |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1582 | do { |
| 1583 | // Infer as many types as possible. If we cannot infer all of them, we |
| 1584 | // can never do anything with this pattern: report it to the user. |
Chris Lattner | 186fb7d | 2006-06-20 00:31:27 +0000 | [diff] [blame] | 1585 | InferredAllPatternTypes = Pattern->InferAllTypes(); |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1588 | // never do anything with this pattern: report it to the user. |
Chris Lattner | 186fb7d | 2006-06-20 00:31:27 +0000 | [diff] [blame] | 1589 | InferredAllResultTypes = Result->InferAllTypes(); |
| 1590 | |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1591 | // Apply the type of the result to the source pattern. This helps us |
| 1592 | // resolve cases where the input type is known to be a pointer type (which |
| 1593 | // is considered resolved), but the result knows it needs to be 32- or |
| 1594 | // 64-bits. Infer the other way for good measure. |
| 1595 | IterateInference = Pattern->getOnlyTree()-> |
| 1596 | UpdateNodeType(Result->getOnlyTree()->getExtTypes(), *Result); |
| 1597 | IterateInference |= Result->getOnlyTree()-> |
| 1598 | UpdateNodeType(Pattern->getOnlyTree()->getExtTypes(), *Result); |
| 1599 | } while (IterateInference); |
Chris Lattner | 186fb7d | 2006-06-20 00:31:27 +0000 | [diff] [blame] | 1600 | |
| 1601 | // Verify that we inferred enough types that we can do something with the |
| 1602 | // pattern and result. If these fire the user has to add type casts. |
| 1603 | if (!InferredAllPatternTypes) |
| 1604 | Pattern->error("Could not infer all types in pattern!"); |
| 1605 | if (!InferredAllResultTypes) |
| 1606 | Result->error("Could not infer all types in pattern result!"); |
Chris Lattner | a354849 | 2006-06-20 00:18:02 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1608 | // Validate that the input pattern is correct. |
| 1609 | { |
| 1610 | std::map<std::string, TreePatternNode*> InstInputs; |
Evan Cheng | 420132e | 2006-03-20 06:04:09 +0000 | [diff] [blame] | 1611 | std::map<std::string, TreePatternNode*> InstResults; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1612 | std::vector<Record*> InstImpInputs; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1613 | std::vector<Record*> InstImpResults; |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1614 | FindPatternInputsAndOutputs(Pattern, Pattern->getOnlyTree(), |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1615 | InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1616 | InstImpInputs, InstImpResults); |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1617 | } |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1618 | |
Evan Cheng | 3a7a14b | 2006-03-21 20:44:17 +0000 | [diff] [blame] | 1619 | // Promote the xform function to be an explicit node if set. |
| 1620 | std::vector<TreePatternNode*> ResultNodeOperands; |
| 1621 | TreePatternNode *DstPattern = Result->getOnlyTree(); |
| 1622 | for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { |
| 1623 | TreePatternNode *OpNode = DstPattern->getChild(ii); |
| 1624 | if (Record *Xform = OpNode->getTransformFn()) { |
| 1625 | OpNode->setTransformFn(0); |
| 1626 | std::vector<TreePatternNode*> Children; |
| 1627 | Children.push_back(OpNode); |
| 1628 | OpNode = new TreePatternNode(Xform, Children); |
| 1629 | } |
| 1630 | ResultNodeOperands.push_back(OpNode); |
| 1631 | } |
Evan Cheng | 83e1a6a | 2006-03-23 02:35:32 +0000 | [diff] [blame] | 1632 | DstPattern = Result->getOnlyTree(); |
| 1633 | if (!DstPattern->isLeaf()) |
| 1634 | DstPattern = new TreePatternNode(DstPattern->getOperator(), |
| 1635 | ResultNodeOperands); |
Evan Cheng | 3a7a14b | 2006-03-21 20:44:17 +0000 | [diff] [blame] | 1636 | DstPattern->setTypes(Result->getOnlyTree()->getExtTypes()); |
| 1637 | TreePattern Temp(Result->getRecord(), DstPattern, false, *this); |
| 1638 | Temp.InferAllTypes(); |
| 1639 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1640 | std::string Reason; |
| 1641 | if (!Pattern->getOnlyTree()->canPatternMatch(Reason, *this)) |
| 1642 | Pattern->error("Pattern can never match: " + Reason); |
| 1643 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1644 | PatternsToMatch. |
| 1645 | push_back(PatternToMatch(Patterns[i]->getValueAsListInit("Predicates"), |
| 1646 | Pattern->getOnlyTree(), |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1647 | Temp.getOnlyTree(), |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1648 | Patterns[i]->getValueAsInt("AddedComplexity"))); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1649 | } |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1652 | /// CombineChildVariants - Given a bunch of permutations of each child of the |
| 1653 | /// 'operator' node, put them together in all possible ways. |
| 1654 | static void CombineChildVariants(TreePatternNode *Orig, |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1655 | const std::vector<std::vector<TreePatternNode*> > &ChildVariants, |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1656 | std::vector<TreePatternNode*> &OutVariants, |
| 1657 | DAGISelEmitter &ISE) { |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1658 | // Make sure that each operand has at least one variant to choose from. |
| 1659 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1660 | if (ChildVariants[i].empty()) |
| 1661 | return; |
| 1662 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1663 | // The end result is an all-pairs construction of the resultant pattern. |
| 1664 | std::vector<unsigned> Idxs; |
| 1665 | Idxs.resize(ChildVariants.size()); |
| 1666 | bool NotDone = true; |
| 1667 | while (NotDone) { |
| 1668 | // Create the variant and add it to the output list. |
| 1669 | std::vector<TreePatternNode*> NewChildren; |
| 1670 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1671 | NewChildren.push_back(ChildVariants[i][Idxs[i]]); |
| 1672 | TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren); |
| 1673 | |
| 1674 | // Copy over properties. |
| 1675 | R->setName(Orig->getName()); |
| 1676 | R->setPredicateFn(Orig->getPredicateFn()); |
| 1677 | R->setTransformFn(Orig->getTransformFn()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1678 | R->setTypes(Orig->getExtTypes()); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1679 | |
| 1680 | // If this pattern cannot every match, do not include it as a variant. |
| 1681 | std::string ErrString; |
| 1682 | if (!R->canPatternMatch(ErrString, ISE)) { |
| 1683 | delete R; |
| 1684 | } else { |
| 1685 | bool AlreadyExists = false; |
| 1686 | |
| 1687 | // Scan to see if this pattern has already been emitted. We can get |
| 1688 | // duplication due to things like commuting: |
| 1689 | // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) |
| 1690 | // which are the same pattern. Ignore the dups. |
| 1691 | for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) |
| 1692 | if (R->isIsomorphicTo(OutVariants[i])) { |
| 1693 | AlreadyExists = true; |
| 1694 | break; |
| 1695 | } |
| 1696 | |
| 1697 | if (AlreadyExists) |
| 1698 | delete R; |
| 1699 | else |
| 1700 | OutVariants.push_back(R); |
| 1701 | } |
| 1702 | |
| 1703 | // Increment indices to the next permutation. |
| 1704 | NotDone = false; |
| 1705 | // Look for something we can increment without causing a wrap-around. |
| 1706 | for (unsigned IdxsIdx = 0; IdxsIdx != Idxs.size(); ++IdxsIdx) { |
| 1707 | if (++Idxs[IdxsIdx] < ChildVariants[IdxsIdx].size()) { |
| 1708 | NotDone = true; // Found something to increment. |
| 1709 | break; |
| 1710 | } |
| 1711 | Idxs[IdxsIdx] = 0; |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1716 | /// CombineChildVariants - A helper function for binary operators. |
| 1717 | /// |
| 1718 | static void CombineChildVariants(TreePatternNode *Orig, |
| 1719 | const std::vector<TreePatternNode*> &LHS, |
| 1720 | const std::vector<TreePatternNode*> &RHS, |
| 1721 | std::vector<TreePatternNode*> &OutVariants, |
| 1722 | DAGISelEmitter &ISE) { |
| 1723 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 1724 | ChildVariants.push_back(LHS); |
| 1725 | ChildVariants.push_back(RHS); |
| 1726 | CombineChildVariants(Orig, ChildVariants, OutVariants, ISE); |
| 1727 | } |
| 1728 | |
| 1729 | |
| 1730 | static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, |
| 1731 | std::vector<TreePatternNode *> &Children) { |
| 1732 | assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); |
| 1733 | Record *Operator = N->getOperator(); |
| 1734 | |
| 1735 | // Only permit raw nodes. |
| 1736 | if (!N->getName().empty() || !N->getPredicateFn().empty() || |
| 1737 | N->getTransformFn()) { |
| 1738 | Children.push_back(N); |
| 1739 | return; |
| 1740 | } |
| 1741 | |
| 1742 | if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) |
| 1743 | Children.push_back(N->getChild(0)); |
| 1744 | else |
| 1745 | GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); |
| 1746 | |
| 1747 | if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) |
| 1748 | Children.push_back(N->getChild(1)); |
| 1749 | else |
| 1750 | GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); |
| 1751 | } |
| 1752 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1753 | /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of |
| 1754 | /// the (potentially recursive) pattern by using algebraic laws. |
| 1755 | /// |
| 1756 | static void GenerateVariantsOf(TreePatternNode *N, |
| 1757 | std::vector<TreePatternNode*> &OutVariants, |
| 1758 | DAGISelEmitter &ISE) { |
| 1759 | // We cannot permute leaves. |
| 1760 | if (N->isLeaf()) { |
| 1761 | OutVariants.push_back(N); |
| 1762 | return; |
| 1763 | } |
| 1764 | |
| 1765 | // Look up interesting info about the node. |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 1766 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(N->getOperator()); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1767 | |
| 1768 | // If this node is associative, reassociate. |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 1769 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPAssociative)) { |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1770 | // Reassociate by pulling together all of the linked operators |
| 1771 | std::vector<TreePatternNode*> MaximalChildren; |
| 1772 | GatherChildrenOfAssociativeOpcode(N, MaximalChildren); |
| 1773 | |
| 1774 | // Only handle child sizes of 3. Otherwise we'll end up trying too many |
| 1775 | // permutations. |
| 1776 | if (MaximalChildren.size() == 3) { |
| 1777 | // Find the variants of all of our maximal children. |
| 1778 | std::vector<TreePatternNode*> AVariants, BVariants, CVariants; |
| 1779 | GenerateVariantsOf(MaximalChildren[0], AVariants, ISE); |
| 1780 | GenerateVariantsOf(MaximalChildren[1], BVariants, ISE); |
| 1781 | GenerateVariantsOf(MaximalChildren[2], CVariants, ISE); |
| 1782 | |
| 1783 | // There are only two ways we can permute the tree: |
| 1784 | // (A op B) op C and A op (B op C) |
| 1785 | // Within these forms, we can also permute A/B/C. |
| 1786 | |
| 1787 | // Generate legal pair permutations of A/B/C. |
| 1788 | std::vector<TreePatternNode*> ABVariants; |
| 1789 | std::vector<TreePatternNode*> BAVariants; |
| 1790 | std::vector<TreePatternNode*> ACVariants; |
| 1791 | std::vector<TreePatternNode*> CAVariants; |
| 1792 | std::vector<TreePatternNode*> BCVariants; |
| 1793 | std::vector<TreePatternNode*> CBVariants; |
| 1794 | CombineChildVariants(N, AVariants, BVariants, ABVariants, ISE); |
| 1795 | CombineChildVariants(N, BVariants, AVariants, BAVariants, ISE); |
| 1796 | CombineChildVariants(N, AVariants, CVariants, ACVariants, ISE); |
| 1797 | CombineChildVariants(N, CVariants, AVariants, CAVariants, ISE); |
| 1798 | CombineChildVariants(N, BVariants, CVariants, BCVariants, ISE); |
| 1799 | CombineChildVariants(N, CVariants, BVariants, CBVariants, ISE); |
| 1800 | |
| 1801 | // Combine those into the result: (x op x) op x |
| 1802 | CombineChildVariants(N, ABVariants, CVariants, OutVariants, ISE); |
| 1803 | CombineChildVariants(N, BAVariants, CVariants, OutVariants, ISE); |
| 1804 | CombineChildVariants(N, ACVariants, BVariants, OutVariants, ISE); |
| 1805 | CombineChildVariants(N, CAVariants, BVariants, OutVariants, ISE); |
| 1806 | CombineChildVariants(N, BCVariants, AVariants, OutVariants, ISE); |
| 1807 | CombineChildVariants(N, CBVariants, AVariants, OutVariants, ISE); |
| 1808 | |
| 1809 | // Combine those into the result: x op (x op x) |
| 1810 | CombineChildVariants(N, CVariants, ABVariants, OutVariants, ISE); |
| 1811 | CombineChildVariants(N, CVariants, BAVariants, OutVariants, ISE); |
| 1812 | CombineChildVariants(N, BVariants, ACVariants, OutVariants, ISE); |
| 1813 | CombineChildVariants(N, BVariants, CAVariants, OutVariants, ISE); |
| 1814 | CombineChildVariants(N, AVariants, BCVariants, OutVariants, ISE); |
| 1815 | CombineChildVariants(N, AVariants, CBVariants, OutVariants, ISE); |
| 1816 | return; |
| 1817 | } |
| 1818 | } |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1819 | |
| 1820 | // Compute permutations of all children. |
| 1821 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 1822 | ChildVariants.resize(N->getNumChildren()); |
| 1823 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 1824 | GenerateVariantsOf(N->getChild(i), ChildVariants[i], ISE); |
| 1825 | |
| 1826 | // Build all permutations based on how the children were formed. |
| 1827 | CombineChildVariants(N, ChildVariants, OutVariants, ISE); |
| 1828 | |
| 1829 | // If this node is commutative, consider the commuted order. |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 1830 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) { |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1831 | assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!"); |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1832 | // Consider the commuted order. |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 1833 | CombineChildVariants(N, ChildVariants[1], ChildVariants[0], |
| 1834 | OutVariants, ISE); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1835 | } |
| 1836 | } |
| 1837 | |
| 1838 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1839 | // GenerateVariants - Generate variants. For example, commutative patterns can |
| 1840 | // match multiple ways. Add them to PatternsToMatch as well. |
| 1841 | void DAGISelEmitter::GenerateVariants() { |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1842 | |
| 1843 | DEBUG(std::cerr << "Generating instruction variants.\n"); |
| 1844 | |
| 1845 | // Loop over all of the patterns we've collected, checking to see if we can |
| 1846 | // generate variants of the instruction, through the exploitation of |
| 1847 | // identities. This permits the target to provide agressive matching without |
| 1848 | // the .td file having to contain tons of variants of instructions. |
| 1849 | // |
| 1850 | // Note that this loop adds new patterns to the PatternsToMatch list, but we |
| 1851 | // intentionally do not reconsider these. Any variants of added patterns have |
| 1852 | // already been added. |
| 1853 | // |
| 1854 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 1855 | std::vector<TreePatternNode*> Variants; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1856 | GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1857 | |
| 1858 | assert(!Variants.empty() && "Must create at least original variant!"); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1859 | Variants.erase(Variants.begin()); // Remove the original pattern. |
| 1860 | |
| 1861 | if (Variants.empty()) // No variants for this pattern. |
| 1862 | continue; |
| 1863 | |
| 1864 | DEBUG(std::cerr << "FOUND VARIANTS OF: "; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1865 | PatternsToMatch[i].getSrcPattern()->dump(); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1866 | std::cerr << "\n"); |
| 1867 | |
| 1868 | for (unsigned v = 0, e = Variants.size(); v != e; ++v) { |
| 1869 | TreePatternNode *Variant = Variants[v]; |
| 1870 | |
| 1871 | DEBUG(std::cerr << " VAR#" << v << ": "; |
| 1872 | Variant->dump(); |
| 1873 | std::cerr << "\n"); |
| 1874 | |
| 1875 | // Scan to see if an instruction or explicit pattern already matches this. |
| 1876 | bool AlreadyExists = false; |
| 1877 | for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { |
| 1878 | // Check to see if this variant already exists. |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1879 | if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern())) { |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1880 | DEBUG(std::cerr << " *** ALREADY EXISTS, ignoring variant.\n"); |
| 1881 | AlreadyExists = true; |
| 1882 | break; |
| 1883 | } |
| 1884 | } |
| 1885 | // If we already have it, ignore the variant. |
| 1886 | if (AlreadyExists) continue; |
| 1887 | |
| 1888 | // Otherwise, add it to the list of patterns we have. |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1889 | PatternsToMatch. |
| 1890 | push_back(PatternToMatch(PatternsToMatch[i].getPredicates(), |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 1891 | Variant, PatternsToMatch[i].getDstPattern(), |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 1892 | PatternsToMatch[i].getAddedComplexity())); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | DEBUG(std::cerr << "\n"); |
| 1896 | } |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1897 | } |
| 1898 | |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 1899 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1900 | // NodeIsComplexPattern - return true if N is a leaf node and a subclass of |
| 1901 | // ComplexPattern. |
| 1902 | static bool NodeIsComplexPattern(TreePatternNode *N) |
| 1903 | { |
| 1904 | return (N->isLeaf() && |
| 1905 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 1906 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 1907 | isSubClassOf("ComplexPattern")); |
| 1908 | } |
| 1909 | |
| 1910 | // NodeGetComplexPattern - return the pointer to the ComplexPattern if N |
| 1911 | // is a leaf node and a subclass of ComplexPattern, else it returns NULL. |
| 1912 | static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N, |
| 1913 | DAGISelEmitter &ISE) |
| 1914 | { |
| 1915 | if (N->isLeaf() && |
| 1916 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 1917 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 1918 | isSubClassOf("ComplexPattern")) { |
| 1919 | return &ISE.getComplexPattern(static_cast<DefInit*>(N->getLeafValue()) |
| 1920 | ->getDef()); |
| 1921 | } |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1925 | /// getPatternSize - Return the 'size' of this pattern. We want to match large |
| 1926 | /// patterns before small ones. This is used to determine the size of a |
| 1927 | /// pattern. |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1928 | static unsigned getPatternSize(TreePatternNode *P, DAGISelEmitter &ISE) { |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 1929 | assert((isExtIntegerInVTs(P->getExtTypes()) || |
| 1930 | isExtFloatingPointInVTs(P->getExtTypes()) || |
| 1931 | P->getExtTypeNum(0) == MVT::isVoid || |
| 1932 | P->getExtTypeNum(0) == MVT::Flag || |
| 1933 | P->getExtTypeNum(0) == MVT::iPTR) && |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1934 | "Not a valid pattern node to size!"); |
Evan Cheng | e1050d6 | 2006-01-06 02:30:23 +0000 | [diff] [blame] | 1935 | unsigned Size = 2; // The node itself. |
Evan Cheng | 657416c | 2006-02-01 06:06:31 +0000 | [diff] [blame] | 1936 | // If the root node is a ConstantSDNode, increases its size. |
| 1937 | // e.g. (set R32:$dst, 0). |
| 1938 | if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue())) |
| 1939 | Size++; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1940 | |
| 1941 | // FIXME: This is a hack to statically increase the priority of patterns |
| 1942 | // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. |
| 1943 | // Later we can allow complexity / cost for each pattern to be (optionally) |
| 1944 | // specified. To get best possible pattern match we'll need to dynamically |
| 1945 | // calculate the complexity of all patterns a dag can potentially map to. |
| 1946 | const ComplexPattern *AM = NodeGetComplexPattern(P, ISE); |
| 1947 | if (AM) |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1948 | Size += AM->getNumOperands() * 2; |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1949 | |
| 1950 | // If this node has some predicate function that must match, it adds to the |
| 1951 | // complexity of this node. |
| 1952 | if (!P->getPredicateFn().empty()) |
| 1953 | ++Size; |
| 1954 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1955 | // Count children in the count if they are also nodes. |
| 1956 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
| 1957 | TreePatternNode *Child = P->getChild(i); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1958 | if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other) |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1959 | Size += getPatternSize(Child, ISE); |
| 1960 | else if (Child->isLeaf()) { |
| 1961 | if (dynamic_cast<IntInit*>(Child->getLeafValue())) |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1962 | Size += 3; // Matches a ConstantSDNode (+2) and a specific value (+1). |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1963 | else if (NodeIsComplexPattern(Child)) |
| 1964 | Size += getPatternSize(Child, ISE); |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1965 | else if (!Child->getPredicateFn().empty()) |
| 1966 | ++Size; |
Chris Lattner | 2f041d4 | 2005-10-19 04:41:05 +0000 | [diff] [blame] | 1967 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | return Size; |
| 1971 | } |
| 1972 | |
| 1973 | /// getResultPatternCost - Compute the number of instructions for this pattern. |
| 1974 | /// This is a temporary hack. We should really include the instruction |
| 1975 | /// latencies in this calculation. |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 1976 | static unsigned getResultPatternCost(TreePatternNode *P, DAGISelEmitter &ISE) { |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1977 | if (P->isLeaf()) return 0; |
| 1978 | |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 1979 | unsigned Cost = 0; |
| 1980 | Record *Op = P->getOperator(); |
| 1981 | if (Op->isSubClassOf("Instruction")) { |
| 1982 | Cost++; |
| 1983 | CodeGenInstruction &II = ISE.getTargetInfo().getInstruction(Op->getName()); |
| 1984 | if (II.usesCustomDAGSchedInserter) |
| 1985 | Cost += 10; |
| 1986 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1987 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Evan Cheng | fbad708 | 2006-02-18 02:33:09 +0000 | [diff] [blame] | 1988 | Cost += getResultPatternCost(P->getChild(i), ISE); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1989 | return Cost; |
| 1990 | } |
| 1991 | |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 1992 | /// getResultPatternCodeSize - Compute the code size of instructions for this |
| 1993 | /// pattern. |
| 1994 | static unsigned getResultPatternSize(TreePatternNode *P, DAGISelEmitter &ISE) { |
| 1995 | if (P->isLeaf()) return 0; |
| 1996 | |
| 1997 | unsigned Cost = 0; |
| 1998 | Record *Op = P->getOperator(); |
| 1999 | if (Op->isSubClassOf("Instruction")) { |
| 2000 | Cost += Op->getValueAsInt("CodeSize"); |
| 2001 | } |
| 2002 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
| 2003 | Cost += getResultPatternSize(P->getChild(i), ISE); |
| 2004 | return Cost; |
| 2005 | } |
| 2006 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 2007 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 2008 | // In particular, we want to match maximal patterns first and lowest cost within |
| 2009 | // a particular complexity first. |
| 2010 | struct PatternSortingPredicate { |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 2011 | PatternSortingPredicate(DAGISelEmitter &ise) : ISE(ise) {}; |
| 2012 | DAGISelEmitter &ISE; |
| 2013 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2014 | bool operator()(PatternToMatch *LHS, |
| 2015 | PatternToMatch *RHS) { |
| 2016 | unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), ISE); |
| 2017 | unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), ISE); |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 2018 | LHSSize += LHS->getAddedComplexity(); |
| 2019 | RHSSize += RHS->getAddedComplexity(); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 2020 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 2021 | if (LHSSize < RHSSize) return false; |
| 2022 | |
| 2023 | // If the patterns have equal complexity, compare generated instruction cost |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 2024 | unsigned LHSCost = getResultPatternCost(LHS->getDstPattern(), ISE); |
| 2025 | unsigned RHSCost = getResultPatternCost(RHS->getDstPattern(), ISE); |
| 2026 | if (LHSCost < RHSCost) return true; |
| 2027 | if (LHSCost > RHSCost) return false; |
| 2028 | |
| 2029 | return getResultPatternSize(LHS->getDstPattern(), ISE) < |
| 2030 | getResultPatternSize(RHS->getDstPattern(), ISE); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 2031 | } |
| 2032 | }; |
| 2033 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 2034 | /// getRegisterValueType - Look up and return the first ValueType of specified |
| 2035 | /// RegisterClass record |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 2036 | static MVT::ValueType getRegisterValueType(Record *R, const CodeGenTarget &T) { |
Chris Lattner | 22faeab | 2005-12-05 02:36:37 +0000 | [diff] [blame] | 2037 | if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R)) |
| 2038 | return RC->getValueTypeNum(0); |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 2039 | return MVT::Other; |
| 2040 | } |
| 2041 | |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2043 | /// RemoveAllTypes - A quick recursive walk over a pattern which removes all |
| 2044 | /// type information from it. |
| 2045 | static void RemoveAllTypes(TreePatternNode *N) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2046 | N->removeTypes(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2047 | if (!N->isLeaf()) |
| 2048 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 2049 | RemoveAllTypes(N->getChild(i)); |
| 2050 | } |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 2051 | |
Chris Lattner | 0614b62 | 2005-11-02 06:49:14 +0000 | [diff] [blame] | 2052 | Record *DAGISelEmitter::getSDNodeNamed(const std::string &Name) const { |
| 2053 | Record *N = Records.getDef(Name); |
Chris Lattner | 5a1df38 | 2006-03-24 23:10:39 +0000 | [diff] [blame] | 2054 | if (!N || !N->isSubClassOf("SDNode")) { |
| 2055 | std::cerr << "Error getting SDNode '" << Name << "'!\n"; |
| 2056 | exit(1); |
| 2057 | } |
Chris Lattner | 0614b62 | 2005-11-02 06:49:14 +0000 | [diff] [blame] | 2058 | return N; |
| 2059 | } |
| 2060 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2061 | /// NodeHasProperty - return true if TreePatternNode has the specified |
| 2062 | /// property. |
| 2063 | static bool NodeHasProperty(TreePatternNode *N, SDNodeInfo::SDNP Property, |
| 2064 | DAGISelEmitter &ISE) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2065 | { |
| 2066 | if (N->isLeaf()) return false; |
| 2067 | Record *Operator = N->getOperator(); |
| 2068 | if (!Operator->isSubClassOf("SDNode")) return false; |
| 2069 | |
| 2070 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(Operator); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2071 | return NodeInfo.hasProperty(Property); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2074 | static bool PatternHasProperty(TreePatternNode *N, SDNodeInfo::SDNP Property, |
| 2075 | DAGISelEmitter &ISE) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2076 | { |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2077 | if (NodeHasProperty(N, Property, ISE)) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2078 | return true; |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2079 | |
| 2080 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 2081 | TreePatternNode *Child = N->getChild(i); |
| 2082 | if (PatternHasProperty(Child, Property, ISE)) |
| 2083 | return true; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | return false; |
| 2087 | } |
| 2088 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2089 | class PatternCodeEmitter { |
| 2090 | private: |
| 2091 | DAGISelEmitter &ISE; |
| 2092 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2093 | // Predicates. |
| 2094 | ListInit *Predicates; |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 2095 | // Pattern cost. |
| 2096 | unsigned Cost; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2097 | // Instruction selector pattern. |
| 2098 | TreePatternNode *Pattern; |
| 2099 | // Matched instruction. |
| 2100 | TreePatternNode *Instruction; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2101 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2102 | // Node to name mapping |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2103 | std::map<std::string, std::string> VariableMap; |
| 2104 | // Node to operator mapping |
| 2105 | std::map<std::string, Record*> OperatorMap; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2106 | // Names of all the folded nodes which produce chains. |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2107 | std::vector<std::pair<std::string, unsigned> > FoldedChains; |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 2108 | std::set<std::string> Duplicates; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2109 | /// These nodes are being marked "in-flight" so they cannot be folded. |
| 2110 | std::vector<std::string> InflightNodes; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2111 | |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2112 | /// GeneratedCode - This is the buffer that we emit code to. The first bool |
| 2113 | /// indicates whether this is an exit predicate (something that should be |
| 2114 | /// tested, and if true, the match fails) [when true] or normal code to emit |
| 2115 | /// [when false]. |
| 2116 | std::vector<std::pair<bool, std::string> > &GeneratedCode; |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2117 | /// GeneratedDecl - This is the set of all SDOperand declarations needed for |
| 2118 | /// the set of patterns for each top-level opcode. |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2119 | std::set<std::pair<unsigned, std::string> > &GeneratedDecl; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2120 | /// TargetOpcodes - The target specific opcodes used by the resulting |
| 2121 | /// instructions. |
| 2122 | std::vector<std::string> &TargetOpcodes; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2123 | std::vector<std::string> &TargetVTs; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2124 | |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2125 | std::string ChainName; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2126 | bool DoReplace; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2127 | unsigned TmpNo; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2128 | unsigned OpcNo; |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2129 | unsigned VTNo; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2130 | |
| 2131 | void emitCheck(const std::string &S) { |
| 2132 | if (!S.empty()) |
| 2133 | GeneratedCode.push_back(std::make_pair(true, S)); |
| 2134 | } |
| 2135 | void emitCode(const std::string &S) { |
| 2136 | if (!S.empty()) |
| 2137 | GeneratedCode.push_back(std::make_pair(false, S)); |
| 2138 | } |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2139 | void emitDecl(const std::string &S, unsigned T=0) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2140 | assert(!S.empty() && "Invalid declaration"); |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2141 | GeneratedDecl.insert(std::make_pair(T, S)); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2142 | } |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2143 | void emitOpcode(const std::string &Opc) { |
| 2144 | TargetOpcodes.push_back(Opc); |
| 2145 | OpcNo++; |
| 2146 | } |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2147 | void emitVT(const std::string &VT) { |
| 2148 | TargetVTs.push_back(VT); |
| 2149 | VTNo++; |
| 2150 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2151 | public: |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2152 | PatternCodeEmitter(DAGISelEmitter &ise, ListInit *preds, |
| 2153 | TreePatternNode *pattern, TreePatternNode *instr, |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2154 | std::vector<std::pair<bool, std::string> > &gc, |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2155 | std::set<std::pair<unsigned, std::string> > &gd, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2156 | std::vector<std::string> &to, |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2157 | std::vector<std::string> &tv, |
| 2158 | bool dorep) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2159 | : ISE(ise), Predicates(preds), Pattern(pattern), Instruction(instr), |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2160 | GeneratedCode(gc), GeneratedDecl(gd), TargetOpcodes(to), TargetVTs(tv), |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2161 | DoReplace(dorep), TmpNo(0), OpcNo(0), VTNo(0) {} |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2162 | |
| 2163 | /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo |
| 2164 | /// if the match fails. At this point, we already know that the opcode for N |
| 2165 | /// matches, and the SDNode for the result has the RootName specified name. |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2166 | void EmitMatchCode(TreePatternNode *N, TreePatternNode *P, |
| 2167 | const std::string &RootName, const std::string &ParentName, |
| 2168 | const std::string &ChainSuffix, bool &FoundChain) { |
| 2169 | bool isRoot = (P == NULL); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2170 | // Emit instruction predicates. Each predicate is just a string for now. |
| 2171 | if (isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2172 | std::string PredicateCheck; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2173 | for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { |
| 2174 | if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) { |
| 2175 | Record *Def = Pred->getDef(); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2176 | if (!Def->isSubClassOf("Predicate")) { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 2177 | #ifndef NDEBUG |
| 2178 | Def->dump(); |
| 2179 | #endif |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2180 | assert(0 && "Unknown predicate type!"); |
| 2181 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2182 | if (!PredicateCheck.empty()) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2183 | PredicateCheck += " || "; |
| 2184 | PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2185 | } |
| 2186 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2187 | |
| 2188 | emitCheck(PredicateCheck); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2191 | if (N->isLeaf()) { |
| 2192 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2193 | emitCheck("cast<ConstantSDNode>(" + RootName + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2194 | ")->getSignExtended() == " + itostr(II->getValue())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2195 | return; |
| 2196 | } else if (!NodeIsComplexPattern(N)) { |
| 2197 | assert(0 && "Cannot match this as a leaf value!"); |
| 2198 | abort(); |
| 2199 | } |
| 2200 | } |
| 2201 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2202 | // 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] | 2203 | // we already saw this in the pattern, emit code to verify dagness. |
| 2204 | if (!N->getName().empty()) { |
| 2205 | std::string &VarMapEntry = VariableMap[N->getName()]; |
| 2206 | if (VarMapEntry.empty()) { |
| 2207 | VarMapEntry = RootName; |
| 2208 | } else { |
| 2209 | // If we get here, this is a second reference to a specific name. Since |
| 2210 | // we already have checked that the first reference is valid, we don't |
| 2211 | // have to recursively match it, just check that it's the same as the |
| 2212 | // previously named thing. |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2213 | emitCheck(VarMapEntry + " == " + RootName); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2214 | return; |
| 2215 | } |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2216 | |
| 2217 | if (!N->isLeaf()) |
| 2218 | OperatorMap[N->getName()] = N->getOperator(); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
| 2221 | |
| 2222 | // Emit code to load the child nodes and match their contents recursively. |
| 2223 | unsigned OpNo = 0; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2224 | bool NodeHasChain = NodeHasProperty (N, SDNodeInfo::SDNPHasChain, ISE); |
| 2225 | bool HasChain = PatternHasProperty(N, SDNodeInfo::SDNPHasChain, ISE); |
| 2226 | bool HasOutFlag = PatternHasProperty(N, SDNodeInfo::SDNPOutFlag, ISE); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 2227 | bool EmittedUseCheck = false; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2228 | bool EmittedSlctedCheck = false; |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 2229 | if (HasChain) { |
Evan Cheng | 76356d9 | 2006-01-20 01:11:03 +0000 | [diff] [blame] | 2230 | if (NodeHasChain) |
| 2231 | OpNo = 1; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2232 | if (!isRoot) { |
Evan Cheng | 1129e87 | 2005-12-10 00:09:17 +0000 | [diff] [blame] | 2233 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(N->getOperator()); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2234 | // Not in flight? |
| 2235 | emitCheck("InFlightSet.count(" + RootName + ".Val) == 0"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2236 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2237 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 2238 | EmittedUseCheck = true; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2239 | // hasOneUse() check is not strong enough. If the original node has |
| 2240 | // already been selected, it may have been replaced with another. |
| 2241 | for (unsigned j = 0; j != CInfo.getNumResults(); j++) |
| 2242 | emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) + |
| 2243 | "))"); |
| 2244 | |
| 2245 | EmittedSlctedCheck = true; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2246 | if (NodeHasChain) { |
| 2247 | // FIXME: Don't fold if 1) the parent node writes a flag, 2) the node |
| 2248 | // has a chain use. |
| 2249 | // This a workaround for this problem: |
| 2250 | // |
| 2251 | // [ch, r : ld] |
| 2252 | // ^ ^ |
| 2253 | // | | |
| 2254 | // [XX]--/ \- [flag : cmp] |
| 2255 | // ^ ^ |
| 2256 | // | | |
| 2257 | // \---[br flag]- |
| 2258 | // |
| 2259 | // cmp + br should be considered as a single node as they are flagged |
| 2260 | // together. So, if the ld is folded into the cmp, the XX node in the |
| 2261 | // graph is now both an operand and a use of the ld/cmp/br node. |
| 2262 | if (NodeHasProperty(P, SDNodeInfo::SDNPOutFlag, ISE)) |
| 2263 | emitCheck(ParentName + ".Val->isOnlyUse(" + RootName + ".Val)"); |
| 2264 | |
| 2265 | // If the immediate use can somehow reach this node through another |
| 2266 | // path, then can't fold it either or it will create a cycle. |
| 2267 | // e.g. In the following diagram, XX can reach ld through YY. If |
| 2268 | // ld is folded into XX, then YY is both a predecessor and a successor |
| 2269 | // of XX. |
| 2270 | // |
| 2271 | // [ld] |
| 2272 | // ^ ^ |
| 2273 | // | | |
| 2274 | // / \--- |
| 2275 | // / [YY] |
| 2276 | // | ^ |
| 2277 | // [XX]-------| |
| 2278 | const SDNodeInfo &PInfo = ISE.getSDNodeInfo(P->getOperator()); |
| 2279 | if (PInfo.getNumOperands() > 1 || |
| 2280 | PInfo.hasProperty(SDNodeInfo::SDNPHasChain) || |
| 2281 | PInfo.hasProperty(SDNodeInfo::SDNPInFlag) || |
| 2282 | PInfo.hasProperty(SDNodeInfo::SDNPOptInFlag)) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2283 | if (PInfo.getNumOperands() > 1) { |
| 2284 | emitCheck("!isNonImmUse(" + ParentName + ".Val, " + RootName + |
| 2285 | ".Val)"); |
| 2286 | } else { |
| 2287 | emitCheck("(" + ParentName + ".getNumOperands() == 1 || !" + |
| 2288 | "isNonImmUse(" + ParentName + ".Val, " + RootName + |
| 2289 | ".Val))"); |
| 2290 | } |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2291 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2292 | } |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2293 | |
Evan Cheng | c15d18c | 2006-01-27 22:13:45 +0000 | [diff] [blame] | 2294 | if (NodeHasChain) { |
Evan Cheng | e638993 | 2006-07-21 22:19:51 +0000 | [diff] [blame] | 2295 | if (FoundChain) |
| 2296 | emitCheck("Chain.Val == " + RootName + ".Val"); |
| 2297 | else |
| 2298 | FoundChain = true; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2299 | ChainName = "Chain" + ChainSuffix; |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2300 | emitDecl(ChainName); |
Evan Cheng | e638993 | 2006-07-21 22:19:51 +0000 | [diff] [blame] | 2301 | emitCode(ChainName + " = " + RootName + |
| 2302 | ".getOperand(0);"); |
Evan Cheng | 1cf6db2 | 2006-01-06 00:41:12 +0000 | [diff] [blame] | 2303 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2306 | // 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] | 2307 | // 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] | 2308 | // real flag results, e.g. X86CMP output, can have multiple uses. |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2309 | // FIXME: If the optional incoming flag does not exist. Then it is ok to |
| 2310 | // fold it. |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 2311 | if (!isRoot && |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2312 | (PatternHasProperty(N, SDNodeInfo::SDNPInFlag, ISE) || |
| 2313 | PatternHasProperty(N, SDNodeInfo::SDNPOptInFlag, ISE) || |
| 2314 | PatternHasProperty(N, SDNodeInfo::SDNPOutFlag, ISE))) { |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 2315 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(N->getOperator()); |
| 2316 | if (!EmittedUseCheck) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2317 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2318 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2319 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2320 | if (!EmittedSlctedCheck) |
| 2321 | // hasOneUse() check is not strong enough. If the original node has |
| 2322 | // already been selected, it may have been replaced with another. |
| 2323 | for (unsigned j = 0; j < CInfo.getNumResults(); j++) |
| 2324 | emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) + |
| 2325 | "))"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2328 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2329 | emitDecl(RootName + utostr(OpNo)); |
| 2330 | emitCode(RootName + utostr(OpNo) + " = " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2331 | RootName + ".getOperand(" +utostr(OpNo) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2332 | TreePatternNode *Child = N->getChild(i); |
| 2333 | |
| 2334 | if (!Child->isLeaf()) { |
| 2335 | // If it's not a leaf, recursively match. |
| 2336 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(Child->getOperator()); |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2337 | emitCheck(RootName + utostr(OpNo) + ".getOpcode() == " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2338 | CInfo.getEnumName()); |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2339 | EmitMatchCode(Child, N, RootName + utostr(OpNo), RootName, |
| 2340 | ChainSuffix + utostr(OpNo), FoundChain); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2341 | if (NodeHasProperty(Child, SDNodeInfo::SDNPHasChain, ISE)) |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2342 | FoldedChains.push_back(std::make_pair(RootName + utostr(OpNo), |
| 2343 | CInfo.getNumResults())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2344 | } else { |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2345 | // If this child has a name associated with it, capture it in VarMap. If |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2346 | // we already saw this in the pattern, emit code to verify dagness. |
| 2347 | if (!Child->getName().empty()) { |
| 2348 | std::string &VarMapEntry = VariableMap[Child->getName()]; |
| 2349 | if (VarMapEntry.empty()) { |
| 2350 | VarMapEntry = RootName + utostr(OpNo); |
| 2351 | } else { |
Jeff Cohen | 9b0ffca | 2006-01-27 22:22:28 +0000 | [diff] [blame] | 2352 | // If we get here, this is a second reference to a specific name. |
| 2353 | // Since we already have checked that the first reference is valid, |
| 2354 | // we don't have to recursively match it, just check that it's the |
| 2355 | // same as the previously named thing. |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2356 | emitCheck(VarMapEntry + " == " + RootName + utostr(OpNo)); |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 2357 | Duplicates.insert(RootName + utostr(OpNo)); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2358 | continue; |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | // Handle leaves of various types. |
| 2363 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 2364 | Record *LeafRec = DI->getDef(); |
| 2365 | if (LeafRec->isSubClassOf("RegisterClass")) { |
| 2366 | // Handle register references. Nothing to do here. |
| 2367 | } else if (LeafRec->isSubClassOf("Register")) { |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2368 | // Handle register references. |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2369 | } else if (LeafRec->isSubClassOf("ComplexPattern")) { |
| 2370 | // Handle complex pattern. Nothing to do here. |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 2371 | } else if (LeafRec->getName() == "srcvalue") { |
| 2372 | // Place holder for SRCVALUE nodes. Nothing to do here. |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2373 | } else if (LeafRec->isSubClassOf("ValueType")) { |
| 2374 | // Make sure this is the specified value type. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2375 | emitCheck("cast<VTSDNode>(" + RootName + utostr(OpNo) + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2376 | ")->getVT() == MVT::" + LeafRec->getName()); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2377 | } else if (LeafRec->isSubClassOf("CondCode")) { |
| 2378 | // Make sure this is the specified cond code. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2379 | emitCheck("cast<CondCodeSDNode>(" + RootName + utostr(OpNo) + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2380 | ")->get() == ISD::" + LeafRec->getName()); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2381 | } else { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 2382 | #ifndef NDEBUG |
| 2383 | Child->dump(); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2384 | std::cerr << " "; |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 2385 | #endif |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2386 | assert(0 && "Unknown leaf type!"); |
| 2387 | } |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2388 | } else if (IntInit *II = |
| 2389 | dynamic_cast<IntInit*>(Child->getLeafValue())) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2390 | emitCheck("isa<ConstantSDNode>(" + RootName + utostr(OpNo) + ")"); |
| 2391 | unsigned CTmp = TmpNo++; |
Andrew Lenharth | 8e51773 | 2006-01-29 05:22:37 +0000 | [diff] [blame] | 2392 | emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+ |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2393 | RootName + utostr(OpNo) + ")->getSignExtended();"); |
| 2394 | |
| 2395 | emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2396 | } else { |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 2397 | #ifndef NDEBUG |
| 2398 | Child->dump(); |
| 2399 | #endif |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2400 | assert(0 && "Unknown leaf type!"); |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
| 2405 | // If there is a node predicate for this, emit the call. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2406 | if (!N->getPredicateFn().empty()) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2407 | emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /// EmitResultCode - Emit the action for a pattern. Now that it has matched |
| 2411 | /// we actually have to build a DAG! |
| 2412 | std::pair<unsigned, unsigned> |
Chris Lattner | 947604b | 2006-03-24 21:52:20 +0000 | [diff] [blame] | 2413 | EmitResultCode(TreePatternNode *N, bool LikeLeaf = false, |
| 2414 | bool isRoot = false) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2415 | // This is something selected from the pattern we matched. |
| 2416 | if (!N->getName().empty()) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2417 | std::string &Val = VariableMap[N->getName()]; |
| 2418 | assert(!Val.empty() && |
| 2419 | "Variable referenced but not defined and not caught earlier!"); |
| 2420 | if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { |
| 2421 | // Already selected this operand, just return the tmpval. |
| 2422 | return std::make_pair(1, atoi(Val.c_str()+3)); |
| 2423 | } |
| 2424 | |
| 2425 | const ComplexPattern *CP; |
| 2426 | unsigned ResNo = TmpNo++; |
| 2427 | unsigned NumRes = 1; |
| 2428 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2429 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 2430 | std::string CastType; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2431 | switch (N->getTypeNum(0)) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2432 | default: assert(0 && "Unknown type for constant node!"); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 2433 | case MVT::i1: CastType = "bool"; break; |
| 2434 | case MVT::i8: CastType = "unsigned char"; break; |
| 2435 | case MVT::i16: CastType = "unsigned short"; break; |
| 2436 | case MVT::i32: CastType = "unsigned"; break; |
| 2437 | case MVT::i64: CastType = "uint64_t"; break; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2438 | } |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2439 | emitDecl("Tmp" + utostr(ResNo)); |
| 2440 | emitCode("Tmp" + utostr(ResNo) + |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2441 | " = CurDAG->getTargetConstant(((" + CastType + |
| 2442 | ") cast<ConstantSDNode>(" + Val + ")->getValue()), " + |
| 2443 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 2444 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2445 | Record *Op = OperatorMap[N->getName()]; |
| 2446 | // Transform ExternalSymbol to TargetExternalSymbol |
| 2447 | if (Op && Op->getName() == "externalsym") { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2448 | emitDecl("Tmp" + utostr(ResNo)); |
| 2449 | emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2450 | "ExternalSymbol(cast<ExternalSymbolSDNode>(" + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 2451 | Val + ")->getSymbol(), " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2452 | getEnumName(N->getTypeNum(0)) + ");"); |
| 2453 | } else { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2454 | emitDecl("Tmp" + utostr(ResNo)); |
| 2455 | emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2456 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2457 | } else if (!N->isLeaf() && N->getOperator()->getName() == "tglobaladdr") { |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2458 | Record *Op = OperatorMap[N->getName()]; |
| 2459 | // Transform GlobalAddress to TargetGlobalAddress |
| 2460 | if (Op && Op->getName() == "globaladdr") { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2461 | emitDecl("Tmp" + utostr(ResNo)); |
| 2462 | emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2463 | "GlobalAddress(cast<GlobalAddressSDNode>(" + Val + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 2464 | ")->getGlobal(), " + getEnumName(N->getTypeNum(0)) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2465 | ");"); |
| 2466 | } else { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2467 | emitDecl("Tmp" + utostr(ResNo)); |
| 2468 | emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2469 | } |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 2470 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2471 | emitDecl("Tmp" + utostr(ResNo)); |
| 2472 | emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 2473 | } else if (!N->isLeaf() && N->getOperator()->getName() == "tconstpool") { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2474 | emitDecl("Tmp" + utostr(ResNo)); |
| 2475 | emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2476 | } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, ISE))) { |
| 2477 | std::string Fn = CP->getSelectFunc(); |
| 2478 | NumRes = CP->getNumOperands(); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2479 | for (unsigned i = 0; i < NumRes; ++i) |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 2480 | emitDecl("CPTmp" + utostr(i+ResNo)); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2481 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2482 | std::string Code = "bool Match = " + Fn + "(" + Val; |
Jeff Cohen | 60e9187 | 2006-01-04 03:23:30 +0000 | [diff] [blame] | 2483 | for (unsigned i = 0; i < NumRes; i++) |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 2484 | Code += ", CPTmp" + utostr(i + ResNo); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2485 | emitCode(Code + ");"); |
| 2486 | if (InflightNodes.size()) { |
| 2487 | // Remove the in-flight nodes if the ComplexPattern does not match! |
| 2488 | emitCode("if (!Match) {"); |
| 2489 | for (std::vector<std::string>::iterator AI = InflightNodes.begin(), |
| 2490 | AE = InflightNodes.end(); AI != AE; ++AI) |
| 2491 | emitCode(" SelectionDAG::RemoveInFlightSetEntry(InFlightSet, " + |
| 2492 | *AI + ".Val);"); |
| 2493 | emitCode("}"); |
| 2494 | } |
| 2495 | |
| 2496 | emitCheck("Match"); |
Evan Cheng | 9c4815a | 2006-02-04 08:50:49 +0000 | [diff] [blame] | 2497 | |
Evan Cheng | 61a0209 | 2006-04-28 02:08:10 +0000 | [diff] [blame] | 2498 | for (unsigned i = 0; i < NumRes; ++i) { |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2499 | emitCode("SelectionDAG::InsertInFlightSetEntry(InFlightSet, CPTmp" + |
| 2500 | utostr(i+ResNo) + ".Val);"); |
| 2501 | InflightNodes.push_back("CPTmp" + utostr(i+ResNo)); |
| 2502 | } |
| 2503 | for (unsigned i = 0; i < NumRes; ++i) { |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 2504 | emitDecl("Tmp" + utostr(i+ResNo)); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2505 | emitCode("Select(Tmp" + utostr(i+ResNo) + ", CPTmp" + |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame] | 2506 | utostr(i+ResNo) + ");"); |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 2507 | } |
Evan Cheng | 9c4815a | 2006-02-04 08:50:49 +0000 | [diff] [blame] | 2508 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2509 | TmpNo = ResNo + NumRes; |
| 2510 | } else { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2511 | emitDecl("Tmp" + utostr(ResNo)); |
Evan Cheng | 863bf5a | 2006-03-20 22:53:06 +0000 | [diff] [blame] | 2512 | // This node, probably wrapped in a SDNodeXForms, behaves like a leaf |
| 2513 | // node even if it isn't one. Don't select it. |
| 2514 | if (LikeLeaf) |
| 2515 | emitCode("Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Evan Cheng | 61a0209 | 2006-04-28 02:08:10 +0000 | [diff] [blame] | 2516 | else { |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2517 | emitCode("Select(Tmp" + utostr(ResNo) + ", " + Val + ");"); |
| 2518 | } |
| 2519 | |
| 2520 | if (isRoot && N->isLeaf()) { |
| 2521 | emitCode("Result = Tmp" + utostr(ResNo) + ";"); |
| 2522 | emitCode("return;"); |
Evan Cheng | 83e1a6a | 2006-03-23 02:35:32 +0000 | [diff] [blame] | 2523 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2524 | } |
| 2525 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 2526 | // value if used multiple times by this pattern result. |
| 2527 | Val = "Tmp"+utostr(ResNo); |
| 2528 | return std::make_pair(NumRes, ResNo); |
| 2529 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2530 | if (N->isLeaf()) { |
| 2531 | // If this is an explicit register reference, handle it. |
| 2532 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 2533 | unsigned ResNo = TmpNo++; |
| 2534 | if (DI->getDef()->isSubClassOf("Register")) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2535 | emitDecl("Tmp" + utostr(ResNo)); |
| 2536 | emitCode("Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 2537 | ISE.getQualifiedName(DI->getDef()) + ", " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2538 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2539 | return std::make_pair(1, ResNo); |
| 2540 | } |
| 2541 | } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 2542 | unsigned ResNo = TmpNo++; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2543 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2544 | emitDecl("Tmp" + utostr(ResNo)); |
| 2545 | emitCode("Tmp" + utostr(ResNo) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2546 | " = CurDAG->getTargetConstant(" + itostr(II->getValue()) + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 2547 | ", " + getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2548 | return std::make_pair(1, ResNo); |
| 2549 | } |
| 2550 | |
Jim Laskey | 16d42c6 | 2006-07-11 18:25:13 +0000 | [diff] [blame] | 2551 | #ifndef NDEBUG |
| 2552 | N->dump(); |
| 2553 | #endif |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2554 | assert(0 && "Unknown leaf type!"); |
| 2555 | return std::make_pair(1, ~0U); |
| 2556 | } |
| 2557 | |
| 2558 | Record *Op = N->getOperator(); |
| 2559 | if (Op->isSubClassOf("Instruction")) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2560 | const CodeGenTarget &CGT = ISE.getTargetInfo(); |
| 2561 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2562 | const DAGInstruction &Inst = ISE.getInstruction(Op); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2563 | TreePattern *InstPat = Inst.getPattern(); |
| 2564 | TreePatternNode *InstPatNode = |
| 2565 | isRoot ? (InstPat ? InstPat->getOnlyTree() : Pattern) |
| 2566 | : (InstPat ? InstPat->getOnlyTree() : NULL); |
| 2567 | if (InstPatNode && InstPatNode->getOperator()->getName() == "set") { |
| 2568 | InstPatNode = InstPatNode->getChild(1); |
| 2569 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2570 | bool HasVarOps = isRoot && II.hasVariableNumberOfOperands; |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2571 | bool HasImpInputs = isRoot && Inst.getNumImpOperands() > 0; |
| 2572 | bool HasImpResults = isRoot && Inst.getNumImpResults() > 0; |
| 2573 | bool NodeHasOptInFlag = isRoot && |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2574 | PatternHasProperty(Pattern, SDNodeInfo::SDNPOptInFlag, ISE); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2575 | bool NodeHasInFlag = isRoot && |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2576 | PatternHasProperty(Pattern, SDNodeInfo::SDNPInFlag, ISE); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2577 | bool NodeHasOutFlag = HasImpResults || (isRoot && |
| 2578 | PatternHasProperty(Pattern, SDNodeInfo::SDNPOutFlag, ISE)); |
| 2579 | bool NodeHasChain = InstPatNode && |
| 2580 | PatternHasProperty(InstPatNode, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 2581 | bool InputHasChain = isRoot && |
| 2582 | NodeHasProperty(Pattern, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2583 | |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2584 | if (NodeHasInFlag || NodeHasOutFlag || NodeHasOptInFlag || HasImpInputs) |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2585 | emitDecl("InFlag"); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2586 | if (NodeHasOptInFlag) { |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2587 | emitDecl("HasInFlag", 2); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2588 | emitCode("HasInFlag = " |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2589 | "(N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag);"); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2590 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2591 | if (HasVarOps) |
| 2592 | emitCode("std::vector<SDOperand> Ops;"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2593 | |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2594 | // How many results is this pattern expected to produce? |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2595 | unsigned PatResults = 0; |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2596 | for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) { |
| 2597 | MVT::ValueType VT = Pattern->getTypeNum(i); |
| 2598 | if (VT != MVT::isVoid && VT != MVT::Flag) |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2599 | PatResults++; |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2602 | // Determine operand emission order. Complex pattern first. |
| 2603 | std::vector<std::pair<unsigned, TreePatternNode*> > EmitOrder; |
| 2604 | std::vector<std::pair<unsigned, TreePatternNode*> >::iterator OI; |
| 2605 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 2606 | TreePatternNode *Child = N->getChild(i); |
| 2607 | if (i == 0) { |
| 2608 | EmitOrder.push_back(std::make_pair(i, Child)); |
| 2609 | OI = EmitOrder.begin(); |
| 2610 | } else if (NodeIsComplexPattern(Child)) { |
| 2611 | OI = EmitOrder.insert(OI, std::make_pair(i, Child)); |
| 2612 | } else { |
| 2613 | EmitOrder.push_back(std::make_pair(i, Child)); |
| 2614 | } |
| 2615 | } |
| 2616 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2617 | // Make sure these operands which would be selected won't be folded while |
| 2618 | // the isel traverses the DAG upward. |
| 2619 | for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) { |
| 2620 | TreePatternNode *Child = EmitOrder[i].second; |
| 2621 | if (!Child->getName().empty()) { |
| 2622 | std::string &Val = VariableMap[Child->getName()]; |
| 2623 | assert(!Val.empty() && |
| 2624 | "Variable referenced but not defined and not caught earlier!"); |
| 2625 | if (Child->isLeaf() && !NodeGetComplexPattern(Child, ISE)) { |
| 2626 | emitCode("SelectionDAG::InsertInFlightSetEntry(InFlightSet, " + |
| 2627 | Val + ".Val);"); |
| 2628 | InflightNodes.push_back(Val); |
| 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | |
Evan Cheng | 61a0209 | 2006-04-28 02:08:10 +0000 | [diff] [blame] | 2633 | // Emit all of the operands. |
Evan Cheng | b0793f9 | 2006-05-25 00:21:44 +0000 | [diff] [blame] | 2634 | std::vector<std::pair<unsigned, unsigned> > NumTemps(EmitOrder.size()); |
Evan Cheng | 61a0209 | 2006-04-28 02:08:10 +0000 | [diff] [blame] | 2635 | for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2636 | unsigned OpOrder = EmitOrder[i].first; |
| 2637 | TreePatternNode *Child = EmitOrder[i].second; |
| 2638 | std::pair<unsigned, unsigned> NumTemp = EmitResultCode(Child); |
| 2639 | NumTemps[OpOrder] = NumTemp; |
| 2640 | } |
| 2641 | |
| 2642 | // List all the operands in the right order. |
| 2643 | std::vector<unsigned> Ops; |
| 2644 | for (unsigned i = 0, e = NumTemps.size(); i != e; i++) { |
| 2645 | for (unsigned j = 0; j < NumTemps[i].first; j++) |
| 2646 | Ops.push_back(NumTemps[i].second + j); |
| 2647 | } |
| 2648 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2649 | // Emit all the chain and CopyToReg stuff. |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2650 | bool ChainEmitted = NodeHasChain; |
| 2651 | if (NodeHasChain) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2652 | emitCode("Select(" + ChainName + ", " + ChainName + ");"); |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 2653 | if (NodeHasInFlag || HasImpInputs) |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2654 | EmitInFlagSelectCode(Pattern, "N", ChainEmitted, true); |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 2655 | if (NodeHasOptInFlag) { |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2656 | emitCode("if (HasInFlag)"); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2657 | emitCode(" Select(InFlag, N.getOperand(N.getNumOperands()-1));"); |
| 2658 | } |
| 2659 | |
| 2660 | if (isRoot) { |
| 2661 | // The operands have been selected. Remove them from InFlightSet. |
| 2662 | for (std::vector<std::string>::iterator AI = InflightNodes.begin(), |
| 2663 | AE = InflightNodes.end(); AI != AE; ++AI) |
| 2664 | emitCode("SelectionDAG::RemoveInFlightSetEntry(InFlightSet, " + |
| 2665 | *AI + ".Val);"); |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 2666 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2667 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2668 | unsigned NumResults = Inst.getNumResults(); |
| 2669 | unsigned ResNo = TmpNo++; |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 2670 | if (!isRoot || InputHasChain || NodeHasChain || NodeHasOutFlag || |
| 2671 | NodeHasOptInFlag) { |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2672 | std::string Code; |
| 2673 | std::string Code2; |
| 2674 | std::string NodeName; |
| 2675 | if (!isRoot) { |
| 2676 | NodeName = "Tmp" + utostr(ResNo); |
| 2677 | emitDecl(NodeName); |
| 2678 | Code2 = NodeName + " = SDOperand("; |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2679 | } else { |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2680 | NodeName = "ResNode"; |
| 2681 | emitDecl(NodeName, true); |
| 2682 | Code2 = NodeName + " = "; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 2683 | } |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2684 | Code = "CurDAG->getTargetNode(Opc" + utostr(OpcNo); |
| 2685 | emitOpcode(II.Namespace + "::" + II.TheDef->getName()); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2686 | |
| 2687 | // Output order: results, chain, flags |
| 2688 | // Result types. |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2689 | if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) { |
| 2690 | Code += ", VT" + utostr(VTNo); |
| 2691 | emitVT(getEnumName(N->getTypeNum(0))); |
| 2692 | } |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2693 | if (NodeHasChain) |
| 2694 | Code += ", MVT::Other"; |
| 2695 | if (NodeHasOutFlag) |
| 2696 | Code += ", MVT::Flag"; |
| 2697 | |
| 2698 | // Inputs. |
| 2699 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 2700 | if (HasVarOps) |
| 2701 | emitCode("Ops.push_back(Tmp" + utostr(Ops[i]) + ");"); |
| 2702 | else |
| 2703 | Code += ", Tmp" + utostr(Ops[i]); |
| 2704 | } |
| 2705 | |
| 2706 | if (HasVarOps) { |
| 2707 | if (NodeHasInFlag || HasImpInputs) |
| 2708 | emitCode("for (unsigned i = 2, e = N.getNumOperands()-1; " |
| 2709 | "i != e; ++i) {"); |
| 2710 | else if (NodeHasOptInFlag) |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2711 | emitCode("for (unsigned i = 2, e = N.getNumOperands()-" |
| 2712 | "(HasInFlag?1:0); i != e; ++i) {"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2713 | else |
| 2714 | emitCode("for (unsigned i = 2, e = N.getNumOperands(); " |
| 2715 | "i != e; ++i) {"); |
| 2716 | emitCode(" SDOperand VarOp(0, 0);"); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2717 | emitCode(" Select(VarOp, N.getOperand(i));"); |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2718 | emitCode(" Ops.push_back(VarOp);"); |
| 2719 | emitCode("}"); |
| 2720 | } |
| 2721 | |
| 2722 | if (NodeHasChain) { |
| 2723 | if (HasVarOps) |
| 2724 | emitCode("Ops.push_back(" + ChainName + ");"); |
| 2725 | else |
| 2726 | Code += ", " + ChainName; |
| 2727 | } |
| 2728 | if (NodeHasInFlag || HasImpInputs) { |
| 2729 | if (HasVarOps) |
| 2730 | emitCode("Ops.push_back(InFlag);"); |
| 2731 | else |
| 2732 | Code += ", InFlag"; |
| 2733 | } else if (NodeHasOptInFlag && HasVarOps) { |
| 2734 | emitCode("if (HasInFlag)"); |
| 2735 | emitCode(" Ops.push_back(InFlag);"); |
| 2736 | } |
| 2737 | |
| 2738 | if (HasVarOps) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2739 | Code += ", Ops"; |
Evan Cheng | e945f4d | 2006-06-14 22:22:20 +0000 | [diff] [blame] | 2740 | else if (NodeHasOptInFlag) |
| 2741 | Code = "HasInFlag ? " + Code + ", InFlag) : " + Code; |
| 2742 | |
| 2743 | if (!isRoot) |
| 2744 | Code += "), 0"; |
| 2745 | emitCode(Code2 + Code + ");"); |
| 2746 | |
| 2747 | if (NodeHasChain) |
| 2748 | // Remember which op produces the chain. |
| 2749 | if (!isRoot) |
| 2750 | emitCode(ChainName + " = SDOperand(" + NodeName + |
| 2751 | ".Val, " + utostr(PatResults) + ");"); |
| 2752 | else |
| 2753 | emitCode(ChainName + " = SDOperand(" + NodeName + |
| 2754 | ", " + utostr(PatResults) + ");"); |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2755 | |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2756 | if (!isRoot) |
| 2757 | return std::make_pair(1, ResNo); |
| 2758 | |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2759 | for (unsigned i = 0; i < NumResults; i++) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2760 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " + |
| 2761 | utostr(i) + ", ResNode, " + utostr(i) + ");"); |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 2762 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2763 | if (NodeHasOutFlag) |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2764 | emitCode("InFlag = SDOperand(ResNode, " + |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2765 | utostr(NumResults + (unsigned)NodeHasChain) + ");"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2766 | |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2767 | if (HasImpResults && EmitCopyFromRegs(N, ChainEmitted)) { |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2768 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " |
| 2769 | "0, ResNode, 0);"); |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2770 | NumResults = 1; |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2771 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2772 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2773 | if (InputHasChain) { |
| 2774 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " + |
| 2775 | utostr(PatResults) + ", " + ChainName + ".Val, " + |
| 2776 | ChainName + ".ResNo" + ");"); |
| 2777 | if (DoReplace) |
| 2778 | emitCode("if (N.ResNo == 0) AddHandleReplacement(N.Val, " + |
| 2779 | utostr(PatResults) + ", " + ChainName + ".Val, " + |
| 2780 | ChainName + ".ResNo" + ");"); |
| 2781 | } |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 2782 | |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2783 | if (FoldedChains.size() > 0) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2784 | std::string Code; |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2785 | for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2786 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, " + |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 2787 | FoldedChains[j].first + ".Val, " + |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2788 | utostr(FoldedChains[j].second) + ", ResNode, " + |
| 2789 | utostr(NumResults) + ");"); |
| 2790 | |
| 2791 | for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) { |
| 2792 | std::string Code = |
| 2793 | FoldedChains[j].first + ".Val, " + |
| 2794 | utostr(FoldedChains[j].second) + ", "; |
| 2795 | emitCode("AddHandleReplacement(" + Code + "ResNode, " + |
| 2796 | utostr(NumResults) + ");"); |
| 2797 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2798 | } |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 2799 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2800 | if (NodeHasOutFlag) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2801 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " + |
| 2802 | utostr(PatResults + (unsigned)InputHasChain) + |
| 2803 | ", InFlag.Val, InFlag.ResNo);"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2804 | |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2805 | // User does not expect the instruction would produce a chain! |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2806 | bool AddedChain = NodeHasChain && !InputHasChain; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2807 | if (AddedChain && NodeHasOutFlag) { |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2808 | if (PatResults == 0) { |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2809 | emitCode("Result = SDOperand(ResNode, N.ResNo+1);"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2810 | } else { |
Evan Cheng | ed66e85 | 2006-03-09 08:19:11 +0000 | [diff] [blame] | 2811 | emitCode("if (N.ResNo < " + utostr(PatResults) + ")"); |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2812 | emitCode(" Result = SDOperand(ResNode, N.ResNo);"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2813 | emitCode("else"); |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2814 | emitCode(" Result = SDOperand(ResNode, N.ResNo+1);"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2815 | } |
Evan Cheng | 3eff89b | 2006-05-10 02:47:57 +0000 | [diff] [blame] | 2816 | } else if (InputHasChain && !NodeHasChain) { |
| 2817 | // One of the inner node produces a chain. |
| 2818 | emitCode("if (N.ResNo < " + utostr(PatResults) + ")"); |
| 2819 | emitCode(" Result = SDOperand(ResNode, N.ResNo);"); |
| 2820 | if (NodeHasOutFlag) { |
| 2821 | emitCode("else if (N.ResNo > " + utostr(PatResults) + ")"); |
| 2822 | emitCode(" Result = SDOperand(ResNode, N.ResNo-1);"); |
| 2823 | } |
| 2824 | emitCode("else"); |
Chris Lattner | 11bcd28 | 2006-06-09 23:59:44 +0000 | [diff] [blame] | 2825 | emitCode(" Result = SDOperand(" + ChainName + ".Val, " + |
| 2826 | ChainName + ".ResNo);"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2827 | } else { |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2828 | emitCode("Result = SDOperand(ResNode, N.ResNo);"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2829 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2830 | } else { |
| 2831 | // If this instruction is the root, and if there is only one use of it, |
| 2832 | // use SelectNodeTo instead of getTargetNode to avoid an allocation. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2833 | emitCode("if (N.Val->hasOneUse()) {"); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2834 | std::string Code = " Result = CurDAG->SelectNodeTo(N.Val, Opc" + |
| 2835 | utostr(OpcNo); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2836 | if (N->getTypeNum(0) != MVT::isVoid) |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2837 | Code += ", VT" + utostr(VTNo); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2838 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2839 | Code += ", MVT::Flag"; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2840 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2841 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2842 | if (NodeHasInFlag || HasImpInputs) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2843 | Code += ", InFlag"; |
| 2844 | emitCode(Code + ");"); |
| 2845 | emitCode("} else {"); |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2846 | emitDecl("ResNode", 1); |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2847 | Code = " ResNode = CurDAG->getTargetNode(Opc" + utostr(OpcNo); |
| 2848 | emitOpcode(II.Namespace + "::" + II.TheDef->getName()); |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 2849 | if (N->getTypeNum(0) != MVT::isVoid) { |
| 2850 | Code += ", VT" + utostr(VTNo); |
| 2851 | emitVT(getEnumName(N->getTypeNum(0))); |
| 2852 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2853 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2854 | Code += ", MVT::Flag"; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2855 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2856 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | 045953c | 2006-05-10 00:05:46 +0000 | [diff] [blame] | 2857 | if (NodeHasInFlag || HasImpInputs) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2858 | Code += ", InFlag"; |
| 2859 | emitCode(Code + ");"); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2860 | emitCode(" SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo" |
| 2861 | ", ResNode, 0);"); |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 2862 | emitCode(" Result = SDOperand(ResNode, 0);"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2863 | emitCode("}"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2864 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2865 | |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 2866 | if (isRoot) |
| 2867 | emitCode("return;"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2868 | return std::make_pair(1, ResNo); |
| 2869 | } else if (Op->isSubClassOf("SDNodeXForm")) { |
| 2870 | assert(N->getNumChildren() == 1 && "node xform should have one child!"); |
Evan Cheng | 863bf5a | 2006-03-20 22:53:06 +0000 | [diff] [blame] | 2871 | // PatLeaf node - the operand may or may not be a leaf node. But it should |
| 2872 | // behave like one. |
| 2873 | unsigned OpVal = EmitResultCode(N->getChild(0), true).second; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2874 | unsigned ResNo = TmpNo++; |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2875 | emitDecl("Tmp" + utostr(ResNo)); |
| 2876 | emitCode("Tmp" + utostr(ResNo) + " = Transform_" + Op->getName() |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2877 | + "(Tmp" + utostr(OpVal) + ".Val);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2878 | if (isRoot) { |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2879 | emitCode("SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val," |
| 2880 | "N.ResNo, Tmp" + utostr(ResNo) + ".Val, Tmp" + |
| 2881 | utostr(ResNo) + ".ResNo);"); |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 2882 | emitCode("Result = Tmp" + utostr(ResNo) + ";"); |
| 2883 | emitCode("return;"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2884 | } |
| 2885 | return std::make_pair(1, ResNo); |
| 2886 | } else { |
| 2887 | N->dump(); |
Chris Lattner | 7893f13 | 2006-01-11 01:33:49 +0000 | [diff] [blame] | 2888 | std::cerr << "\n"; |
| 2889 | throw std::string("Unknown node in result pattern!"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2890 | } |
| 2891 | } |
| 2892 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2893 | /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' |
| 2894 | /// 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] | 2895 | /// 'Pat' may be missing types. If we find an unresolved type to add a check |
| 2896 | /// for, this returns true otherwise false if Pat has all types. |
| 2897 | bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2898 | const std::string &Prefix) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2899 | // Did we find one? |
Evan Cheng | d15531b | 2006-05-19 07:24:32 +0000 | [diff] [blame] | 2900 | if (Pat->getExtTypes() != Other->getExtTypes()) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2901 | // Move a type over from 'other' to 'pat'. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2902 | Pat->setTypes(Other->getExtTypes()); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2903 | emitCheck(Prefix + ".Val->getValueType(0) == " + |
| 2904 | getName(Pat->getTypeNum(0))); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2905 | return true; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2908 | unsigned OpNo = |
| 2909 | (unsigned) NodeHasProperty(Pat, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2910 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo) |
| 2911 | if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i), |
| 2912 | Prefix + utostr(OpNo))) |
| 2913 | return true; |
| 2914 | return false; |
| 2915 | } |
| 2916 | |
| 2917 | private: |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2918 | /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2919 | /// being built. |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2920 | void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName, |
| 2921 | bool &ChainEmitted, bool isRoot = false) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2922 | const CodeGenTarget &T = ISE.getTargetInfo(); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2923 | unsigned OpNo = |
| 2924 | (unsigned) NodeHasProperty(N, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2925 | bool HasInFlag = NodeHasProperty(N, SDNodeInfo::SDNPInFlag, ISE); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2926 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 2927 | TreePatternNode *Child = N->getChild(i); |
| 2928 | if (!Child->isLeaf()) { |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2929 | EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2930 | } else { |
| 2931 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 2932 | if (!Child->getName().empty()) { |
| 2933 | std::string Name = RootName + utostr(OpNo); |
| 2934 | if (Duplicates.find(Name) != Duplicates.end()) |
| 2935 | // A duplicate! Do not emit a copy for this node. |
| 2936 | continue; |
| 2937 | } |
| 2938 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2939 | Record *RR = DI->getDef(); |
| 2940 | if (RR->isSubClassOf("Register")) { |
| 2941 | MVT::ValueType RVT = getRegisterValueType(RR, T); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 2942 | if (RVT == MVT::Flag) { |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2943 | emitCode("Select(InFlag, " + RootName + utostr(OpNo) + ");"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2944 | } else { |
| 2945 | if (!ChainEmitted) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2946 | emitDecl("Chain"); |
| 2947 | emitCode("Chain = CurDAG->getEntryNode();"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2948 | ChainName = "Chain"; |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2949 | ChainEmitted = true; |
| 2950 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2951 | emitCode("Select(" + RootName + utostr(OpNo) + ", " + |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 2952 | RootName + utostr(OpNo) + ");"); |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 2953 | emitCode("ResNode = CurDAG->getCopyToReg(" + ChainName + |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2954 | ", CurDAG->getRegister(" + ISE.getQualifiedName(RR) + |
Evan Cheng | 2618d07 | 2006-05-17 20:37:59 +0000 | [diff] [blame] | 2955 | ", " + getEnumName(RVT) + "), " + |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2956 | RootName + utostr(OpNo) + ", InFlag).Val;"); |
Evan Cheng | 67212a0 | 2006-02-09 22:12:27 +0000 | [diff] [blame] | 2957 | emitCode(ChainName + " = SDOperand(ResNode, 0);"); |
| 2958 | emitCode("InFlag = SDOperand(ResNode, 1);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | } |
| 2962 | } |
| 2963 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2964 | |
Evan Cheng | bc6b86a | 2006-06-14 19:27:50 +0000 | [diff] [blame] | 2965 | if (HasInFlag) |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 2966 | emitCode("Select(InFlag, " + RootName + |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 2967 | ".getOperand(" + utostr(OpNo) + "));"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2968 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2969 | |
| 2970 | /// EmitCopyFromRegs - Emit code to copy result to physical registers |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2971 | /// as specified by the instruction. It returns true if any copy is |
| 2972 | /// emitted. |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2973 | bool EmitCopyFromRegs(TreePatternNode *N, bool &ChainEmitted) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2974 | bool RetVal = false; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2975 | Record *Op = N->getOperator(); |
| 2976 | if (Op->isSubClassOf("Instruction")) { |
| 2977 | const DAGInstruction &Inst = ISE.getInstruction(Op); |
| 2978 | const CodeGenTarget &CGT = ISE.getTargetInfo(); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2979 | unsigned NumImpResults = Inst.getNumImpResults(); |
| 2980 | for (unsigned i = 0; i < NumImpResults; i++) { |
| 2981 | Record *RR = Inst.getImpResult(i); |
| 2982 | if (RR->isSubClassOf("Register")) { |
| 2983 | MVT::ValueType RVT = getRegisterValueType(RR, CGT); |
| 2984 | if (RVT != MVT::Flag) { |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2985 | if (!ChainEmitted) { |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 2986 | emitDecl("Chain"); |
| 2987 | emitCode("Chain = CurDAG->getEntryNode();"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2988 | ChainEmitted = true; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2989 | ChainName = "Chain"; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2990 | } |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 2991 | emitCode("ResNode = CurDAG->getCopyFromReg(" + ChainName + |
| 2992 | ", " + ISE.getQualifiedName(RR) + ", " + getEnumName(RVT) + |
Evan Cheng | d7805a7 | 2006-02-09 07:16:09 +0000 | [diff] [blame] | 2993 | ", InFlag).Val;"); |
| 2994 | emitCode(ChainName + " = SDOperand(ResNode, 1);"); |
| 2995 | emitCode("InFlag = SDOperand(ResNode, 2);"); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2996 | RetVal = true; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
| 2999 | } |
| 3000 | } |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 3001 | return RetVal; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 3002 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 3003 | }; |
| 3004 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 3005 | /// EmitCodeForPattern - Given a pattern to match, emit code to the specified |
| 3006 | /// 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] | 3007 | /// succeeds. Returns true if the pattern is not guaranteed to match. |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3008 | void DAGISelEmitter::GenerateCodeForPattern(PatternToMatch &Pattern, |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3009 | std::vector<std::pair<bool, std::string> > &GeneratedCode, |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 3010 | std::set<std::pair<unsigned, std::string> > &GeneratedDecl, |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 3011 | std::vector<std::string> &TargetOpcodes, |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3012 | std::vector<std::string> &TargetVTs, |
| 3013 | bool DoReplace) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 3014 | PatternCodeEmitter Emitter(*this, Pattern.getPredicates(), |
| 3015 | Pattern.getSrcPattern(), Pattern.getDstPattern(), |
Evan Cheng | f872940 | 2006-07-16 06:12:52 +0000 | [diff] [blame] | 3016 | GeneratedCode, GeneratedDecl, |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3017 | TargetOpcodes, TargetVTs, |
| 3018 | DoReplace); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 3019 | |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 3020 | // Emit the matcher, capturing named arguments in VariableMap. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 3021 | bool FoundChain = false; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3022 | Emitter.EmitMatchCode(Pattern.getSrcPattern(), NULL, "N", "", "", FoundChain); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 3024 | // TP - Get *SOME* tree pattern, we don't care which. |
| 3025 | TreePattern &TP = *PatternFragments.begin()->second; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 3027 | // At this point, we know that we structurally match the pattern, but the |
| 3028 | // types of the nodes may not match. Figure out the fewest number of type |
| 3029 | // comparisons we need to emit. For example, if there is only one integer |
| 3030 | // type supported by a target, there should be no type comparisons at all for |
| 3031 | // integer patterns! |
| 3032 | // |
| 3033 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 3034 | // remove the types, then perform type inference on the pattern as a whole. |
| 3035 | // If there are unresolved types, emit an explicit check for those types, |
| 3036 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 3037 | // types are resolved. |
| 3038 | // |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 3039 | TreePatternNode *Pat = Pattern.getSrcPattern()->clone(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 3040 | RemoveAllTypes(Pat); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 3041 | |
| 3042 | do { |
| 3043 | // Resolve/propagate as many types as possible. |
| 3044 | try { |
| 3045 | bool MadeChange = true; |
| 3046 | while (MadeChange) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 3047 | MadeChange = Pat->ApplyTypeConstraints(TP, |
| 3048 | true/*Ignore reg constraints*/); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 3049 | } catch (...) { |
| 3050 | assert(0 && "Error: could not find consistent types for something we" |
| 3051 | " already decided was ok!"); |
| 3052 | abort(); |
| 3053 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 3055 | // Insert a check for an unresolved type and add it to the tree. If we find |
| 3056 | // an unresolved type to add a check for, this returns true and we iterate, |
| 3057 | // otherwise we are done. |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3058 | } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N")); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 3059 | |
Evan Cheng | 863bf5a | 2006-03-20 22:53:06 +0000 | [diff] [blame] | 3060 | Emitter.EmitResultCode(Pattern.getDstPattern(), false, true /*the root*/); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 3061 | delete Pat; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 3064 | /// EraseCodeLine - Erase one code line from all of the patterns. If removing |
| 3065 | /// a line causes any of them to be empty, remove them and return true when |
| 3066 | /// done. |
| 3067 | static bool EraseCodeLine(std::vector<std::pair<PatternToMatch*, |
| 3068 | std::vector<std::pair<bool, std::string> > > > |
| 3069 | &Patterns) { |
| 3070 | bool ErasedPatterns = false; |
| 3071 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 3072 | Patterns[i].second.pop_back(); |
| 3073 | if (Patterns[i].second.empty()) { |
| 3074 | Patterns.erase(Patterns.begin()+i); |
| 3075 | --i; --e; |
| 3076 | ErasedPatterns = true; |
| 3077 | } |
| 3078 | } |
| 3079 | return ErasedPatterns; |
| 3080 | } |
| 3081 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3082 | /// EmitPatterns - Emit code for at least one pattern, but try to group common |
| 3083 | /// code together between the patterns. |
| 3084 | void DAGISelEmitter::EmitPatterns(std::vector<std::pair<PatternToMatch*, |
| 3085 | std::vector<std::pair<bool, std::string> > > > |
| 3086 | &Patterns, unsigned Indent, |
| 3087 | std::ostream &OS) { |
| 3088 | typedef std::pair<bool, std::string> CodeLine; |
| 3089 | typedef std::vector<CodeLine> CodeList; |
| 3090 | typedef std::vector<std::pair<PatternToMatch*, CodeList> > PatternList; |
| 3091 | |
| 3092 | if (Patterns.empty()) return; |
| 3093 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 3094 | // Figure out how many patterns share the next code line. Explicitly copy |
| 3095 | // FirstCodeLine so that we don't invalidate a reference when changing |
| 3096 | // Patterns. |
| 3097 | const CodeLine FirstCodeLine = Patterns.back().second.back(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3098 | unsigned LastMatch = Patterns.size()-1; |
| 3099 | while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine) |
| 3100 | --LastMatch; |
| 3101 | |
| 3102 | // If not all patterns share this line, split the list into two pieces. The |
| 3103 | // first chunk will use this line, the second chunk won't. |
| 3104 | if (LastMatch != 0) { |
| 3105 | PatternList Shared(Patterns.begin()+LastMatch, Patterns.end()); |
| 3106 | PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch); |
| 3107 | |
| 3108 | // FIXME: Emit braces? |
| 3109 | if (Shared.size() == 1) { |
| 3110 | PatternToMatch &Pattern = *Shared.back().first; |
| 3111 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 3112 | Pattern.getSrcPattern()->print(OS); |
| 3113 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 3114 | Pattern.getDstPattern()->print(OS); |
| 3115 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 3116 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3117 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 3118 | << getPatternSize(Pattern.getSrcPattern(), *this) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 3119 | << " cost = " |
Evan Cheng | e6f3203 | 2006-07-19 00:24:41 +0000 | [diff] [blame] | 3120 | << getResultPatternCost(Pattern.getDstPattern(), *this) |
| 3121 | << " size = " |
| 3122 | << getResultPatternSize(Pattern.getDstPattern(), *this) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3123 | } |
| 3124 | if (!FirstCodeLine.first) { |
| 3125 | OS << std::string(Indent, ' ') << "{\n"; |
| 3126 | Indent += 2; |
| 3127 | } |
| 3128 | EmitPatterns(Shared, Indent, OS); |
| 3129 | if (!FirstCodeLine.first) { |
| 3130 | Indent -= 2; |
| 3131 | OS << std::string(Indent, ' ') << "}\n"; |
| 3132 | } |
| 3133 | |
| 3134 | if (Other.size() == 1) { |
| 3135 | PatternToMatch &Pattern = *Other.back().first; |
| 3136 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 3137 | Pattern.getSrcPattern()->print(OS); |
| 3138 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 3139 | Pattern.getDstPattern()->print(OS); |
| 3140 | OS << "\n"; |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 3141 | unsigned AddedComplexity = Pattern.getAddedComplexity(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3142 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
Evan Cheng | c81d2a0 | 2006-04-19 20:36:09 +0000 | [diff] [blame] | 3143 | << getPatternSize(Pattern.getSrcPattern(), *this) + AddedComplexity |
Evan Cheng | 5941320 | 2006-04-19 18:07:24 +0000 | [diff] [blame] | 3144 | << " cost = " |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3145 | << getResultPatternCost(Pattern.getDstPattern(), *this) << "\n"; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3146 | } |
| 3147 | EmitPatterns(Other, Indent, OS); |
| 3148 | return; |
| 3149 | } |
| 3150 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 3151 | // Remove this code from all of the patterns that share it. |
| 3152 | bool ErasedPatterns = EraseCodeLine(Patterns); |
| 3153 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3154 | bool isPredicate = FirstCodeLine.first; |
| 3155 | |
| 3156 | // Otherwise, every pattern in the list has this line. Emit it. |
| 3157 | if (!isPredicate) { |
| 3158 | // Normal code. |
| 3159 | OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n"; |
| 3160 | } else { |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 3161 | OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second; |
| 3162 | |
| 3163 | // If the next code line is another predicate, and if all of the pattern |
| 3164 | // in this group share the same next line, emit it inline now. Do this |
| 3165 | // until we run out of common predicates. |
| 3166 | while (!ErasedPatterns && Patterns.back().second.back().first) { |
| 3167 | // Check that all of fhe patterns in Patterns end with the same predicate. |
| 3168 | bool AllEndWithSamePredicate = true; |
| 3169 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) |
| 3170 | if (Patterns[i].second.back() != Patterns.back().second.back()) { |
| 3171 | AllEndWithSamePredicate = false; |
| 3172 | break; |
| 3173 | } |
| 3174 | // If all of the predicates aren't the same, we can't share them. |
| 3175 | if (!AllEndWithSamePredicate) break; |
| 3176 | |
| 3177 | // Otherwise we can. Emit it shared now. |
| 3178 | OS << " &&\n" << std::string(Indent+4, ' ') |
| 3179 | << Patterns.back().second.back().second; |
| 3180 | ErasedPatterns = EraseCodeLine(Patterns); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3181 | } |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 3182 | |
| 3183 | OS << ") {\n"; |
| 3184 | Indent += 2; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | EmitPatterns(Patterns, Indent, OS); |
| 3188 | |
| 3189 | if (isPredicate) |
| 3190 | OS << std::string(Indent-2, ' ') << "}\n"; |
| 3191 | } |
| 3192 | |
| 3193 | |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 3194 | |
| 3195 | namespace { |
| 3196 | /// CompareByRecordName - An ordering predicate that implements less-than by |
| 3197 | /// comparing the names records. |
| 3198 | struct CompareByRecordName { |
| 3199 | bool operator()(const Record *LHS, const Record *RHS) const { |
| 3200 | // Sort by name first. |
| 3201 | if (LHS->getName() < RHS->getName()) return true; |
| 3202 | // If both names are equal, sort by pointer. |
| 3203 | return LHS->getName() == RHS->getName() && LHS < RHS; |
| 3204 | } |
| 3205 | }; |
| 3206 | } |
| 3207 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3208 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 3209 | std::string InstNS = Target.inst_begin()->second.Namespace; |
| 3210 | if (!InstNS.empty()) InstNS += "::"; |
| 3211 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3212 | // Group the patterns by their top-level opcodes. |
| 3213 | std::map<Record*, std::vector<PatternToMatch*>, |
| 3214 | CompareByRecordName> PatternsByOpcode; |
Evan Cheng | fceb57a | 2006-07-15 08:45:20 +0000 | [diff] [blame] | 3215 | // All unique target node emission functions. |
| 3216 | std::map<std::string, unsigned> EmitFunctions; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3217 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 3218 | TreePatternNode *Node = PatternsToMatch[i].getSrcPattern(); |
| 3219 | if (!Node->isLeaf()) { |
| 3220 | PatternsByOpcode[Node->getOperator()].push_back(&PatternsToMatch[i]); |
| 3221 | } else { |
| 3222 | const ComplexPattern *CP; |
| 3223 | if (IntInit *II = |
| 3224 | dynamic_cast<IntInit*>(Node->getLeafValue())) { |
| 3225 | PatternsByOpcode[getSDNodeNamed("imm")].push_back(&PatternsToMatch[i]); |
| 3226 | } else if ((CP = NodeGetComplexPattern(Node, *this))) { |
| 3227 | std::vector<Record*> OpNodes = CP->getRootNodes(); |
| 3228 | for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 3229 | PatternsByOpcode[OpNodes[j]] |
| 3230 | .insert(PatternsByOpcode[OpNodes[j]].begin(), &PatternsToMatch[i]); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3231 | } |
| 3232 | } else { |
| 3233 | std::cerr << "Unrecognized opcode '"; |
| 3234 | Node->dump(); |
| 3235 | std::cerr << "' on tree pattern '"; |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 3236 | std::cerr << |
| 3237 | PatternsToMatch[i].getDstPattern()->getOperator()->getName(); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3238 | std::cerr << "'!\n"; |
| 3239 | exit(1); |
| 3240 | } |
| 3241 | } |
| 3242 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3243 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3244 | // Emit one Select_* method for each top-level opcode. We do this instead of |
| 3245 | // emitting one giant switch statement to support compilers where this will |
| 3246 | // result in the recursive functions taking less stack space. |
| 3247 | for (std::map<Record*, std::vector<PatternToMatch*>, |
| 3248 | CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(), |
| 3249 | E = PatternsByOpcode.end(); PBOI != E; ++PBOI) { |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3250 | const std::string &OpName = PBOI->first->getName(); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3251 | const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3252 | bool OptSlctOrder = |
| 3253 | (OpcodeInfo.hasProperty(SDNodeInfo::SDNPHasChain) && |
| 3254 | OpcodeInfo.getNumResults() > 0); |
| 3255 | std::vector<PatternToMatch*> &Patterns = PBOI->second; |
| 3256 | assert(!Patterns.empty() && "No patterns but map has entry?"); |
| 3257 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3258 | // We want to emit all of the matching code now. However, we want to emit |
| 3259 | // the matches in order of minimal cost. Sort the patterns so the least |
| 3260 | // cost one is at the start. |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3261 | std::stable_sort(Patterns.begin(), Patterns.end(), |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3262 | PatternSortingPredicate(*this)); |
Evan Cheng | 21ad392 | 2006-02-07 00:37:41 +0000 | [diff] [blame] | 3263 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3264 | typedef std::vector<std::pair<bool, std::string> > CodeList; |
| 3265 | typedef std::vector<std::pair<bool, std::string> >::iterator CodeListI; |
Evan Cheng | b6ee7f7 | 2006-07-28 22:51:01 +0000 | [diff] [blame] | 3266 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3267 | std::vector<std::pair<PatternToMatch*, CodeList> > CodeForPatterns; |
| 3268 | std::vector<std::vector<std::string> > PatternOpcodes; |
| 3269 | std::vector<std::vector<std::string> > PatternVTs; |
| 3270 | std::vector<std::set<std::pair<unsigned, std::string> > > PatternDecls; |
| 3271 | std::set<std::pair<unsigned, std::string> > AllGenDecls; |
| 3272 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 3273 | CodeList GeneratedCode; |
| 3274 | std::set<std::pair<unsigned, std::string> > GeneratedDecl; |
| 3275 | std::vector<std::string> TargetOpcodes; |
| 3276 | std::vector<std::string> TargetVTs; |
| 3277 | GenerateCodeForPattern(*Patterns[i], GeneratedCode, GeneratedDecl, |
| 3278 | TargetOpcodes, TargetVTs, OptSlctOrder); |
Evan Cheng | b6ee7f7 | 2006-07-28 22:51:01 +0000 | [diff] [blame] | 3279 | for (std::set<std::pair<unsigned, std::string> >::iterator |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3280 | si = GeneratedDecl.begin(), se = GeneratedDecl.end(); si!=se; ++si) |
| 3281 | AllGenDecls.insert(*si); |
| 3282 | CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode)); |
| 3283 | PatternDecls.push_back(GeneratedDecl); |
| 3284 | PatternOpcodes.push_back(TargetOpcodes); |
| 3285 | PatternVTs.push_back(TargetVTs); |
Chris Lattner | b026e70 | 2006-03-28 00:41:33 +0000 | [diff] [blame] | 3286 | } |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3287 | |
| 3288 | // Scan the code to see if all of the patterns are reachable and if it is |
| 3289 | // possible that the last one might not match. |
| 3290 | bool mightNotMatch = true; |
| 3291 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 3292 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 3293 | mightNotMatch = false; |
| 3294 | |
| 3295 | for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) { |
| 3296 | if (GeneratedCode[j].first) { // predicate. |
| 3297 | mightNotMatch = true; |
| 3298 | break; |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | // If this pattern definitely matches, and if it isn't the last one, the |
| 3303 | // patterns after it CANNOT ever match. Error out. |
| 3304 | if (mightNotMatch == false && i != CodeForPatterns.size()-1) { |
| 3305 | std::cerr << "Pattern '"; |
| 3306 | CodeForPatterns[i+1].first->getSrcPattern()->print(OS); |
| 3307 | std::cerr << "' is impossible to select!\n"; |
| 3308 | exit(1); |
| 3309 | } |
| 3310 | } |
| 3311 | |
| 3312 | // Factor target node emission code (emitted by EmitResultCode) into |
| 3313 | // separate functions. Uniquing and share them among all instruction |
| 3314 | // selection routines. |
| 3315 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 3316 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 3317 | std::vector<std::string> &TargetOpcodes = PatternOpcodes[i]; |
| 3318 | std::vector<std::string> &TargetVTs = PatternVTs[i]; |
| 3319 | std::set<std::pair<unsigned, std::string> > Decls = PatternDecls[i]; |
| 3320 | int CodeSize = (int)GeneratedCode.size(); |
| 3321 | int LastPred = -1; |
| 3322 | for (int j = CodeSize-1; j >= 0; --j) { |
| 3323 | if (GeneratedCode[j].first) { |
| 3324 | LastPred = j; |
| 3325 | break; |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | std::string CalleeDecls; |
| 3330 | std::string CalleeCode = "(SDOperand &Result, SDOperand &N"; |
| 3331 | std::string CallerCode = "(Result, N"; |
| 3332 | for (unsigned j = 0, e = TargetOpcodes.size(); j != e; ++j) { |
| 3333 | CalleeCode += ", unsigned Opc" + utostr(j); |
| 3334 | CallerCode += ", " + TargetOpcodes[j]; |
| 3335 | } |
| 3336 | for (unsigned j = 0, e = TargetVTs.size(); j != e; ++j) { |
| 3337 | CalleeCode += ", MVT::ValueType VT" + utostr(j); |
| 3338 | CallerCode += ", " + TargetVTs[j]; |
| 3339 | } |
| 3340 | for (std::set<std::pair<unsigned, std::string> >::iterator |
| 3341 | I = Decls.begin(), E = Decls.end(); I != E; ++I) { |
| 3342 | std::string Name = I->second; |
| 3343 | if (I->first == 0) { |
| 3344 | if (Name == "InFlag" || |
| 3345 | (Name.size() > 3 && |
| 3346 | Name[0] == 'T' && Name[1] == 'm' && Name[2] == 'p')) { |
| 3347 | CalleeDecls += " SDOperand " + Name + "(0, 0);\n"; |
| 3348 | continue; |
| 3349 | } |
| 3350 | CalleeCode += ", SDOperand &" + Name; |
| 3351 | CallerCode += ", " + Name; |
| 3352 | } else if (I->first == 1) { |
| 3353 | if (Name == "ResNode") { |
| 3354 | CalleeDecls += " SDNode *" + Name + " = NULL;\n"; |
| 3355 | continue; |
| 3356 | } |
| 3357 | CalleeCode += ", SDNode *" + Name; |
| 3358 | CallerCode += ", " + Name; |
| 3359 | } else { |
| 3360 | CalleeCode += ", bool " + Name; |
| 3361 | CallerCode += ", " + Name; |
| 3362 | } |
| 3363 | } |
| 3364 | CallerCode += ");"; |
| 3365 | CalleeCode += ") "; |
| 3366 | // Prevent emission routines from being inlined to reduce selection |
| 3367 | // routines stack frame sizes. |
| 3368 | CalleeCode += "NOINLINE "; |
| 3369 | CalleeCode += "{\n" + CalleeDecls; |
| 3370 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 3371 | CalleeCode += " " + GeneratedCode[j].second + '\n'; |
| 3372 | for (int j = LastPred+1; j < CodeSize; ++j) |
| 3373 | GeneratedCode.pop_back(); |
| 3374 | CalleeCode += "}\n"; |
| 3375 | |
| 3376 | // Uniquing the emission routines. |
| 3377 | unsigned EmitFuncNum; |
| 3378 | std::map<std::string, unsigned>::iterator EFI = |
| 3379 | EmitFunctions.find(CalleeCode); |
| 3380 | if (EFI != EmitFunctions.end()) { |
| 3381 | EmitFuncNum = EFI->second; |
| 3382 | } else { |
| 3383 | EmitFuncNum = EmitFunctions.size(); |
| 3384 | EmitFunctions.insert(std::make_pair(CalleeCode, EmitFuncNum)); |
| 3385 | OS << "void " << "Emit_" << utostr(EmitFuncNum) << CalleeCode; |
| 3386 | } |
| 3387 | |
| 3388 | // Replace the emission code within selection routines with calls to the |
| 3389 | // emission functions. |
| 3390 | CallerCode = "Emit_" + utostr(EmitFuncNum) + CallerCode; |
| 3391 | GeneratedCode.push_back(std::make_pair(false, CallerCode)); |
| 3392 | GeneratedCode.push_back(std::make_pair(false, "return;")); |
| 3393 | } |
| 3394 | |
| 3395 | // Print function. |
| 3396 | OS << "void Select_" << OpName << "(SDOperand &Result, SDOperand N) {\n"; |
| 3397 | if (OptSlctOrder) { |
| 3398 | OS << " if (N.ResNo == " << OpcodeInfo.getNumResults() |
| 3399 | << " && N.getValue(0).hasOneUse()) {\n" |
| 3400 | << " SDOperand Dummy = " |
| 3401 | << "CurDAG->getNode(ISD::HANDLENODE, MVT::Other, N);\n" |
| 3402 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, " |
| 3403 | << OpcodeInfo.getNumResults() << ", Dummy.Val, 0);\n" |
| 3404 | << " SelectionDAG::InsertISelMapEntry(HandleMap, N.Val, " |
| 3405 | << OpcodeInfo.getNumResults() << ", Dummy.Val, 0);\n" |
| 3406 | << " Result = Dummy;\n" |
| 3407 | << " return;\n" |
| 3408 | << " }\n"; |
| 3409 | } |
| 3410 | |
| 3411 | // Print all declarations. |
| 3412 | for (std::set<std::pair<unsigned, std::string> >::iterator |
| 3413 | I = AllGenDecls.begin(), E = AllGenDecls.end(); I != E; ++I) |
| 3414 | if (I->first == 0) |
| 3415 | OS << " SDOperand " << I->second << "(0, 0);\n"; |
| 3416 | else if (I->first == 1) |
| 3417 | OS << " SDNode *" << I->second << " = NULL;\n"; |
| 3418 | else |
| 3419 | OS << " bool " << I->second << " = false;\n"; |
| 3420 | |
| 3421 | // Loop through and reverse all of the CodeList vectors, as we will be |
| 3422 | // accessing them from their logical front, but accessing the end of a |
| 3423 | // vector is more efficient. |
| 3424 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 3425 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 3426 | std::reverse(GeneratedCode.begin(), GeneratedCode.end()); |
| 3427 | } |
| 3428 | |
| 3429 | // Next, reverse the list of patterns itself for the same reason. |
| 3430 | std::reverse(CodeForPatterns.begin(), CodeForPatterns.end()); |
| 3431 | |
| 3432 | // Emit all of the patterns now, grouped together to share code. |
| 3433 | EmitPatterns(CodeForPatterns, 2, OS); |
| 3434 | |
| 3435 | // If the last pattern has predicates (which could fail) emit code to catch |
| 3436 | // the case where nothing handles a pattern. |
| 3437 | if (mightNotMatch) { |
| 3438 | OS << " std::cerr << \"Cannot yet select: \";\n"; |
| 3439 | if (OpcodeInfo.getEnumName() != "ISD::INTRINSIC_W_CHAIN" && |
| 3440 | OpcodeInfo.getEnumName() != "ISD::INTRINSIC_WO_CHAIN" && |
| 3441 | OpcodeInfo.getEnumName() != "ISD::INTRINSIC_VOID") { |
| 3442 | OS << " N.Val->dump(CurDAG);\n"; |
| 3443 | } else { |
| 3444 | OS << " unsigned iid = cast<ConstantSDNode>(N.getOperand(" |
| 3445 | "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n" |
| 3446 | << " std::cerr << \"intrinsic %\"<< " |
| 3447 | "Intrinsic::getName((Intrinsic::ID)iid);\n"; |
| 3448 | } |
| 3449 | OS << " std::cerr << '\\n';\n" |
| 3450 | << " abort();\n"; |
| 3451 | } |
| 3452 | OS << "}\n\n"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3455 | // Emit boilerplate. |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3456 | OS << "void Select_INLINEASM(SDOperand& Result, SDOperand N) {\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3457 | << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3458 | << " Select(Ops[0], N.getOperand(0)); // Select the chain.\n\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3459 | << " // Select the flag operand.\n" |
| 3460 | << " if (Ops.back().getValueType() == MVT::Flag)\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3461 | << " Select(Ops.back(), Ops.back());\n" |
Chris Lattner | fd105d4 | 2006-02-24 02:13:31 +0000 | [diff] [blame] | 3462 | << " SelectInlineAsmMemoryOperands(Ops, *CurDAG);\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3463 | << " std::vector<MVT::ValueType> VTs;\n" |
| 3464 | << " VTs.push_back(MVT::Other);\n" |
| 3465 | << " VTs.push_back(MVT::Flag);\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3466 | << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, Ops);\n" |
| 3467 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, New.Val, 0);\n" |
| 3468 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, New.Val, 1);\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3469 | << " Result = New.getValue(N.ResNo);\n" |
| 3470 | << " return;\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3471 | << "}\n\n"; |
| 3472 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3473 | OS << "// The main instruction selector code.\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3474 | << "void SelectCode(SDOperand &Result, SDOperand N) {\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 3475 | << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 3476 | << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3477 | << "INSTRUCTION_LIST_END)) {\n" |
| 3478 | << " Result = N;\n" |
| 3479 | << " return; // Already selected.\n" |
| 3480 | << " }\n\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3481 | << " std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);\n" |
| 3482 | << " if (CGMI != CodeGenMap.end()) {\n" |
| 3483 | << " Result = CGMI->second;\n" |
| 3484 | << " return;\n" |
| 3485 | << " }\n\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 3486 | << " switch (N.getOpcode()) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3487 | << " default: break;\n" |
| 3488 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
Chris Lattner | 5216c69 | 2005-12-18 21:05:44 +0000 | [diff] [blame] | 3489 | << " case ISD::BasicBlock:\n" |
Chris Lattner | 8020a52 | 2006-01-11 19:52:27 +0000 | [diff] [blame] | 3490 | << " case ISD::Register:\n" |
Evan Cheng | 0a83ed5 | 2006-02-05 08:46:14 +0000 | [diff] [blame] | 3491 | << " case ISD::HANDLENODE:\n" |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame] | 3492 | << " case ISD::TargetConstant:\n" |
| 3493 | << " case ISD::TargetConstantPool:\n" |
| 3494 | << " case ISD::TargetFrameIndex:\n" |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 3495 | << " case ISD::TargetJumpTable:\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3496 | << " case ISD::TargetGlobalAddress: {\n" |
| 3497 | << " Result = N;\n" |
| 3498 | << " return;\n" |
| 3499 | << " }\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3500 | << " case ISD::AssertSext:\n" |
Chris Lattner | fab3728 | 2005-09-26 22:10:24 +0000 | [diff] [blame] | 3501 | << " case ISD::AssertZext: {\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3502 | << " SDOperand Tmp0;\n" |
| 3503 | << " Select(Tmp0, N.getOperand(0));\n" |
| 3504 | << " if (!N.Val->hasOneUse())\n" |
| 3505 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, " |
| 3506 | << "Tmp0.Val, Tmp0.ResNo);\n" |
| 3507 | << " Result = Tmp0;\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3508 | << " return;\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 3509 | << " }\n" |
| 3510 | << " case ISD::TokenFactor:\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3511 | << " if (N.getNumOperands() == 2) {\n" |
| 3512 | << " SDOperand Op0, Op1;\n" |
| 3513 | << " Select(Op0, N.getOperand(0));\n" |
| 3514 | << " Select(Op1, N.getOperand(1));\n" |
| 3515 | << " Result = \n" |
| 3516 | << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);\n" |
| 3517 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, " |
| 3518 | << "Result.Val, Result.ResNo);\n" |
| 3519 | << " } else {\n" |
| 3520 | << " std::vector<SDOperand> Ops;\n" |
| 3521 | << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i) {\n" |
| 3522 | << " SDOperand Val;\n" |
| 3523 | << " Select(Val, N.getOperand(i));\n" |
| 3524 | << " Ops.push_back(Val);\n" |
| 3525 | << " }\n" |
| 3526 | << " Result = \n" |
| 3527 | << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);\n" |
| 3528 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, N.ResNo, " |
| 3529 | << "Result.Val, Result.ResNo);\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 3530 | << " }\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3531 | << " return;\n" |
| 3532 | << " case ISD::CopyFromReg: {\n" |
| 3533 | << " SDOperand Chain;\n" |
| 3534 | << " Select(Chain, N.getOperand(0));\n" |
| 3535 | << " unsigned Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();\n" |
| 3536 | << " MVT::ValueType VT = N.Val->getValueType(0);\n" |
| 3537 | << " if (N.Val->getNumValues() == 2) {\n" |
| 3538 | << " if (Chain == N.getOperand(0)) {\n" |
| 3539 | << " Result = N; // No change\n" |
| 3540 | << " return;\n" |
| 3541 | << " }\n" |
| 3542 | << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT);\n" |
| 3543 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, " |
| 3544 | << "New.Val, 0);\n" |
| 3545 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, " |
| 3546 | << "New.Val, 1);\n" |
| 3547 | << " Result = New.getValue(N.ResNo);\n" |
| 3548 | << " return;\n" |
| 3549 | << " } else {\n" |
| 3550 | << " SDOperand Flag;\n" |
| 3551 | << " if (N.getNumOperands() == 3) Select(Flag, N.getOperand(2));\n" |
| 3552 | << " if (Chain == N.getOperand(0) &&\n" |
| 3553 | << " (N.getNumOperands() == 2 || Flag == N.getOperand(2))) {\n" |
| 3554 | << " Result = N; // No change\n" |
| 3555 | << " return;\n" |
| 3556 | << " }\n" |
| 3557 | << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT, Flag);\n" |
| 3558 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, " |
| 3559 | << "New.Val, 0);\n" |
| 3560 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, " |
| 3561 | << "New.Val, 1);\n" |
| 3562 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 2, " |
| 3563 | << "New.Val, 2);\n" |
| 3564 | << " Result = New.getValue(N.ResNo);\n" |
| 3565 | << " return;\n" |
| 3566 | << " }\n" |
| 3567 | << " }\n" |
| 3568 | << " case ISD::CopyToReg: {\n" |
| 3569 | << " SDOperand Chain;\n" |
| 3570 | << " Select(Chain, N.getOperand(0));\n" |
| 3571 | << " unsigned Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();\n" |
| 3572 | << " SDOperand Val;\n" |
| 3573 | << " Select(Val, N.getOperand(2));\n" |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3574 | << " Result = N;\n" |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3575 | << " if (N.Val->getNumValues() == 1) {\n" |
| 3576 | << " if (Chain != N.getOperand(0) || Val != N.getOperand(2))\n" |
| 3577 | << " Result = CurDAG->getCopyToReg(Chain, Reg, Val);\n" |
| 3578 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, " |
| 3579 | << "Result.Val, 0);\n" |
| 3580 | << " } else {\n" |
| 3581 | << " SDOperand Flag(0, 0);\n" |
| 3582 | << " if (N.getNumOperands() == 4) Select(Flag, N.getOperand(3));\n" |
| 3583 | << " if (Chain != N.getOperand(0) || Val != N.getOperand(2) ||\n" |
| 3584 | << " (N.getNumOperands() == 4 && Flag != N.getOperand(3)))\n" |
| 3585 | << " Result = CurDAG->getCopyToReg(Chain, Reg, Val, Flag);\n" |
| 3586 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, " |
| 3587 | << "Result.Val, 0);\n" |
| 3588 | << " SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, " |
| 3589 | << "Result.Val, 1);\n" |
| 3590 | << " Result = Result.getValue(N.ResNo);\n" |
| 3591 | << " }\n" |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3592 | << " return;\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3593 | << " }\n" |
Chris Lattner | 947604b | 2006-03-24 21:52:20 +0000 | [diff] [blame] | 3594 | << " case ISD::INLINEASM: Select_INLINEASM(Result, N); return;\n"; |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3596 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 3597 | // Loop over all of the case statements, emiting a call to each method we |
| 3598 | // emitted above. |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 3599 | for (std::map<Record*, std::vector<PatternToMatch*>, |
| 3600 | CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(), |
| 3601 | E = PatternsByOpcode.end(); PBOI != E; ++PBOI) { |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 3602 | const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first); |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3603 | OS << " case " << OpcodeInfo.getEnumName() << ": " |
| 3604 | << std::string(std::max(0, int(24-OpcodeInfo.getEnumName().size())), ' ') |
| 3605 | << "Select_" << PBOI->first->getName() << "(Result, N); return;\n"; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 3606 | } |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 3607 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3608 | OS << " } // end of big switch.\n\n" |
| 3609 | << " std::cerr << \"Cannot yet select: \";\n" |
Chris Lattner | b026e70 | 2006-03-28 00:41:33 +0000 | [diff] [blame] | 3610 | << " if (N.getOpcode() != ISD::INTRINSIC_W_CHAIN &&\n" |
| 3611 | << " N.getOpcode() != ISD::INTRINSIC_WO_CHAIN &&\n" |
| 3612 | << " N.getOpcode() != ISD::INTRINSIC_VOID) {\n" |
Chris Lattner | 9bf2d3e | 2006-03-25 06:47:53 +0000 | [diff] [blame] | 3613 | << " N.Val->dump(CurDAG);\n" |
| 3614 | << " } else {\n" |
| 3615 | << " unsigned iid = cast<ConstantSDNode>(N.getOperand(" |
| 3616 | "N.getOperand(0).getValueType() == MVT::Other))->getValue();\n" |
| 3617 | << " std::cerr << \"intrinsic %\"<< " |
| 3618 | "Intrinsic::getName((Intrinsic::ID)iid);\n" |
| 3619 | << " }\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3620 | << " std::cerr << '\\n';\n" |
| 3621 | << " abort();\n" |
| 3622 | << "}\n"; |
| 3623 | } |
| 3624 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3625 | void DAGISelEmitter::run(std::ostream &OS) { |
| 3626 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 3627 | " target", OS); |
| 3628 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 3629 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 3630 | << "// *** instruction selector class. These functions are really " |
| 3631 | << "methods.\n\n"; |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 3632 | |
Evan Cheng | 233baf1 | 2006-07-26 23:06:27 +0000 | [diff] [blame] | 3633 | OS << "#if defined(__GNUC__) && \\\n"; |
| 3634 | OS << " ((__GNUC__ > 3) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 4)))\n"; |
| 3635 | OS << "#define NOINLINE __attribute__((noinline))\n"; |
| 3636 | OS << "#endif\n\n"; |
| 3637 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3638 | OS << "// Instance var to keep track of multiply used nodes that have \n" |
| 3639 | << "// already been selected.\n" |
| 3640 | << "std::map<SDOperand, SDOperand> CodeGenMap;\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3641 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3642 | OS << "// Instance var to keep track of mapping of chain generating nodes\n" |
| 3643 | << "// and their place handle nodes.\n"; |
| 3644 | OS << "std::map<SDOperand, SDOperand> HandleMap;\n"; |
| 3645 | OS << "// Instance var to keep track of mapping of place handle nodes\n" |
| 3646 | << "// and their replacement nodes.\n"; |
| 3647 | OS << "std::map<SDOperand, SDOperand> ReplaceMap;\n"; |
| 3648 | OS << "// Keep track of nodes that are currently being selecte and therefore\n" |
| 3649 | << "// should not be folded.\n"; |
| 3650 | OS << "std::set<SDNode*> InFlightSet;\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3651 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3652 | OS << "\n"; |
| 3653 | OS << "static void findNonImmUse(SDNode* Use, SDNode* Def, bool &found, " |
| 3654 | << "std::set<SDNode *> &Visited) {\n"; |
| 3655 | OS << " if (found || !Visited.insert(Use).second) return;\n"; |
| 3656 | OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {\n"; |
| 3657 | OS << " SDNode *N = Use->getOperand(i).Val;\n"; |
| 3658 | OS << " if (N != Def) {\n"; |
| 3659 | OS << " findNonImmUse(N, Def, found, Visited);\n"; |
| 3660 | OS << " } else {\n"; |
| 3661 | OS << " found = true;\n"; |
| 3662 | OS << " break;\n"; |
| 3663 | OS << " }\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3664 | OS << " }\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3665 | OS << "}\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3666 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3667 | OS << "\n"; |
| 3668 | OS << "static bool isNonImmUse(SDNode* Use, SDNode* Def) {\n"; |
| 3669 | OS << " std::set<SDNode *> Visited;\n"; |
| 3670 | OS << " bool found = false;\n"; |
| 3671 | OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {\n"; |
| 3672 | OS << " SDNode *N = Use->getOperand(i).Val;\n"; |
| 3673 | OS << " if (N != Def) {\n"; |
| 3674 | OS << " findNonImmUse(N, Def, found, Visited);\n"; |
| 3675 | OS << " if (found) break;\n"; |
| 3676 | OS << " }\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3677 | OS << " }\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3678 | OS << " return found;\n"; |
| 3679 | OS << "}\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3680 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3681 | OS << "\n"; |
| 3682 | OS << "// AddHandleReplacement - Note the pending replacement node for a\n" |
| 3683 | << "// handle node in ReplaceMap.\n"; |
| 3684 | OS << "void AddHandleReplacement(SDNode *H, unsigned HNum, SDNode *R, " |
| 3685 | << "unsigned RNum) {\n"; |
| 3686 | OS << " SDOperand N(H, HNum);\n"; |
| 3687 | OS << " std::map<SDOperand, SDOperand>::iterator HMI = HandleMap.find(N);\n"; |
| 3688 | OS << " if (HMI != HandleMap.end()) {\n"; |
| 3689 | OS << " ReplaceMap[HMI->second] = SDOperand(R, RNum);\n"; |
| 3690 | OS << " HandleMap.erase(N);\n"; |
| 3691 | OS << " }\n"; |
| 3692 | OS << "}\n"; |
Evan Cheng | 9bdca03 | 2006-08-07 22:17:58 +0000 | [diff] [blame] | 3693 | |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3694 | OS << "\n"; |
| 3695 | OS << "// SelectDanglingHandles - Select replacements for all `dangling`\n"; |
| 3696 | OS << "// handles.Some handles do not yet have replacements because the\n"; |
| 3697 | OS << "// nodes they replacements have only dead readers.\n"; |
| 3698 | OS << "void SelectDanglingHandles() {\n"; |
| 3699 | OS << " for (std::map<SDOperand, SDOperand>::iterator I = " |
| 3700 | << "HandleMap.begin(),\n" |
| 3701 | << " E = HandleMap.end(); I != E; ++I) {\n"; |
| 3702 | OS << " SDOperand N = I->first;\n"; |
| 3703 | OS << " SDOperand R;\n"; |
| 3704 | OS << " Select(R, N.getValue(0));\n"; |
| 3705 | OS << " AddHandleReplacement(N.Val, N.ResNo, R.Val, R.ResNo);\n"; |
| 3706 | OS << " }\n"; |
| 3707 | OS << "}\n"; |
| 3708 | OS << "\n"; |
| 3709 | OS << "// ReplaceHandles - Replace all the handles with the real target\n"; |
| 3710 | OS << "// specific nodes.\n"; |
| 3711 | OS << "void ReplaceHandles() {\n"; |
| 3712 | OS << " for (std::map<SDOperand, SDOperand>::iterator I = " |
| 3713 | << "ReplaceMap.begin(),\n" |
| 3714 | << " E = ReplaceMap.end(); I != E; ++I) {\n"; |
| 3715 | OS << " SDOperand From = I->first;\n"; |
| 3716 | OS << " SDOperand To = I->second;\n"; |
| 3717 | OS << " for (SDNode::use_iterator UI = From.Val->use_begin(), " |
| 3718 | << "E = From.Val->use_end(); UI != E; ++UI) {\n"; |
| 3719 | OS << " SDNode *Use = *UI;\n"; |
| 3720 | OS << " std::vector<SDOperand> Ops;\n"; |
| 3721 | OS << " for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i){\n"; |
| 3722 | OS << " SDOperand O = Use->getOperand(i);\n"; |
| 3723 | OS << " if (O.Val == From.Val)\n"; |
| 3724 | OS << " Ops.push_back(To);\n"; |
| 3725 | OS << " else\n"; |
| 3726 | OS << " Ops.push_back(O);\n"; |
| 3727 | OS << " }\n"; |
| 3728 | OS << " SDOperand U = SDOperand(Use, 0);\n"; |
| 3729 | OS << " CurDAG->UpdateNodeOperands(U, Ops);\n"; |
| 3730 | OS << " }\n"; |
| 3731 | OS << " }\n"; |
| 3732 | OS << "}\n"; |
| 3733 | |
| 3734 | OS << "\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3735 | OS << "// SelectRoot - Top level entry to DAG isel.\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3736 | OS << "SDOperand SelectRoot(SDOperand N) {\n"; |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3737 | OS << " SDOperand ResNode;\n"; |
Tanya Lattner | 8d4ccf0 | 2006-08-09 16:41:21 +0000 | [diff] [blame^] | 3738 | OS << " Select(ResNode, N);\n"; |
| 3739 | OS << " SelectDanglingHandles();\n"; |
| 3740 | OS << " ReplaceHandles();\n"; |
| 3741 | OS << " ReplaceMap.clear();\n"; |
Evan Cheng | 3416721 | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 3742 | OS << " return ResNode;\n"; |
Evan Cheng | e41bf82 | 2006-02-05 06:43:12 +0000 | [diff] [blame] | 3743 | OS << "}\n"; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 3744 | |
Chris Lattner | 550525e | 2006-03-24 21:48:51 +0000 | [diff] [blame] | 3745 | Intrinsics = LoadIntrinsics(Records); |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 3746 | ParseNodeInfo(); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 3747 | ParseNodeTransforms(OS); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 3748 | ParseComplexPatterns(); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 3749 | ParsePatternFragments(OS); |
| 3750 | ParseInstructions(); |
| 3751 | ParsePatterns(); |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 3752 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 3753 | // Generate variants. For example, commutative patterns can match |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 3754 | // multiple ways. Add them to PatternsToMatch as well. |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 3755 | GenerateVariants(); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 3756 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 3757 | |
| 3758 | DEBUG(std::cerr << "\n\nALL PATTERNS TO MATCH:\n\n"; |
| 3759 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 3760 | std::cerr << "PATTERN: "; PatternsToMatch[i].getSrcPattern()->dump(); |
| 3761 | std::cerr << "\nRESULT: ";PatternsToMatch[i].getDstPattern()->dump(); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 3762 | std::cerr << "\n"; |
| 3763 | }); |
| 3764 | |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 3765 | // At this point, we have full information about the 'Patterns' we need to |
| 3766 | // parse, both implicitly from instructions as well as from explicit pattern |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 3767 | // definitions. Emit the resultant instruction selector. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3768 | EmitInstructionSelector(OS); |
| 3769 | |
| 3770 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 3771 | E = PatternFragments.end(); I != E; ++I) |
| 3772 | delete I->second; |
| 3773 | PatternFragments.clear(); |
| 3774 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3775 | Instructions.clear(); |
| 3776 | } |