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