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