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