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