Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1 | //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===// |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 10 | // This file implements the CodeGenDAGPatterns class, which is used to read and |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 11 | // represent the patterns present in a .td file for instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 93c7e41 | 2008-01-05 23:37:52 +0000 | [diff] [blame] | 15 | #include "CodeGenDAGPatterns.h" |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 16 | #include "Record.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Streams.h" |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 20 | #include <set> |
Chuck Rose III | 9a79de3 | 2008-01-15 21:43:17 +0000 | [diff] [blame] | 21 | #include <algorithm> |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Helpers for working with extended types. |
| 26 | |
| 27 | /// FilterVTs - Filter a list of VT's according to a predicate. |
| 28 | /// |
| 29 | template<typename T> |
| 30 | static std::vector<MVT::ValueType> |
| 31 | FilterVTs(const std::vector<MVT::ValueType> &InVTs, T Filter) { |
| 32 | std::vector<MVT::ValueType> Result; |
| 33 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 34 | if (Filter(InVTs[i])) |
| 35 | Result.push_back(InVTs[i]); |
| 36 | return Result; |
| 37 | } |
| 38 | |
| 39 | template<typename T> |
| 40 | static std::vector<unsigned char> |
| 41 | FilterEVTs(const std::vector<unsigned char> &InVTs, T Filter) { |
| 42 | std::vector<unsigned char> Result; |
| 43 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 44 | if (Filter((MVT::ValueType)InVTs[i])) |
| 45 | Result.push_back(InVTs[i]); |
| 46 | return Result; |
| 47 | } |
| 48 | |
| 49 | static std::vector<unsigned char> |
| 50 | ConvertVTs(const std::vector<MVT::ValueType> &InVTs) { |
| 51 | std::vector<unsigned char> Result; |
| 52 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 53 | Result.push_back(InVTs[i]); |
| 54 | return Result; |
| 55 | } |
| 56 | |
| 57 | static bool LHSIsSubsetOfRHS(const std::vector<unsigned char> &LHS, |
| 58 | const std::vector<unsigned char> &RHS) { |
| 59 | if (LHS.size() > RHS.size()) return false; |
| 60 | for (unsigned i = 0, e = LHS.size(); i != e; ++i) |
| 61 | if (std::find(RHS.begin(), RHS.end(), LHS[i]) == RHS.end()) |
| 62 | return false; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | /// isExtIntegerVT - Return true if the specified extended value type vector |
| 67 | /// contains isInt or an integer value type. |
| 68 | namespace llvm { |
| 69 | namespace MVT { |
| 70 | bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs) { |
| 71 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
| 72 | return EVTs[0] == isInt || !(FilterEVTs(EVTs, isInteger).empty()); |
| 73 | } |
| 74 | |
| 75 | /// isExtFloatingPointVT - Return true if the specified extended value type |
| 76 | /// vector contains isFP or a FP value type. |
| 77 | bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs) { |
| 78 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
| 79 | return EVTs[0] == isFP || !(FilterEVTs(EVTs, isFloatingPoint).empty()); |
| 80 | } |
| 81 | } // end namespace MVT. |
| 82 | } // end namespace llvm. |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // SDTypeConstraint implementation |
| 86 | // |
| 87 | |
| 88 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 89 | OperandNo = R->getValueAsInt("OperandNum"); |
| 90 | |
| 91 | if (R->isSubClassOf("SDTCisVT")) { |
| 92 | ConstraintType = SDTCisVT; |
| 93 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
| 94 | } else if (R->isSubClassOf("SDTCisPtrTy")) { |
| 95 | ConstraintType = SDTCisPtrTy; |
| 96 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 97 | ConstraintType = SDTCisInt; |
| 98 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 99 | ConstraintType = SDTCisFP; |
| 100 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 101 | ConstraintType = SDTCisSameAs; |
| 102 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 103 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 104 | ConstraintType = SDTCisVTSmallerThanOp; |
| 105 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 106 | R->getValueAsInt("OtherOperandNum"); |
| 107 | } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { |
| 108 | ConstraintType = SDTCisOpSmallerThanOp; |
| 109 | x.SDTCisOpSmallerThanOp_Info.BigOperandNum = |
| 110 | R->getValueAsInt("BigOperandNum"); |
| 111 | } else if (R->isSubClassOf("SDTCisIntVectorOfSameSize")) { |
| 112 | ConstraintType = SDTCisIntVectorOfSameSize; |
| 113 | x.SDTCisIntVectorOfSameSize_Info.OtherOperandNum = |
| 114 | R->getValueAsInt("OtherOpNum"); |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame^] | 115 | } else if (R->isSubClassOf("SDTCisEltOfVec")) { |
| 116 | ConstraintType = SDTCisEltOfVec; |
| 117 | x.SDTCisEltOfVec_Info.OtherOperandNum = |
| 118 | R->getValueAsInt("OtherOpNum"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 119 | } else { |
| 120 | cerr << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
| 121 | exit(1); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 126 | /// N, which has NumResults results. |
| 127 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 128 | TreePatternNode *N, |
| 129 | unsigned NumResults) const { |
| 130 | assert(NumResults <= 1 && |
| 131 | "We only work with nodes with zero or one result so far!"); |
| 132 | |
| 133 | if (OpNo >= (NumResults + N->getNumChildren())) { |
| 134 | cerr << "Invalid operand number " << OpNo << " "; |
| 135 | N->dump(); |
| 136 | cerr << '\n'; |
| 137 | exit(1); |
| 138 | } |
| 139 | |
| 140 | if (OpNo < NumResults) |
| 141 | return N; // FIXME: need value # |
| 142 | else |
| 143 | return N->getChild(OpNo-NumResults); |
| 144 | } |
| 145 | |
| 146 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 147 | /// constraint to the nodes operands. This returns true if it makes a |
| 148 | /// change, false otherwise. If a type contradiction is found, throw an |
| 149 | /// exception. |
| 150 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 151 | const SDNodeInfo &NodeInfo, |
| 152 | TreePattern &TP) const { |
| 153 | unsigned NumResults = NodeInfo.getNumResults(); |
| 154 | assert(NumResults <= 1 && |
| 155 | "We only work with nodes with zero or one result so far!"); |
| 156 | |
| 157 | // Check that the number of operands is sane. Negative operands -> varargs. |
| 158 | if (NodeInfo.getNumOperands() >= 0) { |
| 159 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 160 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 161 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 162 | } |
| 163 | |
| 164 | const CodeGenTarget &CGT = TP.getDAGPatterns().getTargetInfo(); |
| 165 | |
| 166 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 167 | |
| 168 | switch (ConstraintType) { |
| 169 | default: assert(0 && "Unknown constraint type!"); |
| 170 | case SDTCisVT: |
| 171 | // Operand must be a particular type. |
| 172 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
| 173 | case SDTCisPtrTy: { |
| 174 | // Operand must be same as target pointer type. |
| 175 | return NodeToApply->UpdateNodeType(MVT::iPTR, TP); |
| 176 | } |
| 177 | case SDTCisInt: { |
| 178 | // If there is only one integer type supported, this must be it. |
| 179 | std::vector<MVT::ValueType> IntVTs = |
| 180 | FilterVTs(CGT.getLegalValueTypes(), MVT::isInteger); |
| 181 | |
| 182 | // If we found exactly one supported integer type, apply it. |
| 183 | if (IntVTs.size() == 1) |
| 184 | return NodeToApply->UpdateNodeType(IntVTs[0], TP); |
| 185 | return NodeToApply->UpdateNodeType(MVT::isInt, TP); |
| 186 | } |
| 187 | case SDTCisFP: { |
| 188 | // If there is only one FP type supported, this must be it. |
| 189 | std::vector<MVT::ValueType> FPVTs = |
| 190 | FilterVTs(CGT.getLegalValueTypes(), MVT::isFloatingPoint); |
| 191 | |
| 192 | // If we found exactly one supported FP type, apply it. |
| 193 | if (FPVTs.size() == 1) |
| 194 | return NodeToApply->UpdateNodeType(FPVTs[0], TP); |
| 195 | return NodeToApply->UpdateNodeType(MVT::isFP, TP); |
| 196 | } |
| 197 | case SDTCisSameAs: { |
| 198 | TreePatternNode *OtherNode = |
| 199 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
| 200 | return NodeToApply->UpdateNodeType(OtherNode->getExtTypes(), TP) | |
| 201 | OtherNode->UpdateNodeType(NodeToApply->getExtTypes(), TP); |
| 202 | } |
| 203 | case SDTCisVTSmallerThanOp: { |
| 204 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 205 | // have an integer type that is smaller than the VT. |
| 206 | if (!NodeToApply->isLeaf() || |
| 207 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 208 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 209 | ->isSubClassOf("ValueType")) |
| 210 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
| 211 | MVT::ValueType VT = |
| 212 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
| 213 | if (!MVT::isInteger(VT)) |
| 214 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 215 | |
| 216 | TreePatternNode *OtherNode = |
| 217 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
| 218 | |
| 219 | // It must be integer. |
| 220 | bool MadeChange = false; |
| 221 | MadeChange |= OtherNode->UpdateNodeType(MVT::isInt, TP); |
| 222 | |
| 223 | // This code only handles nodes that have one type set. Assert here so |
| 224 | // that we can change this if we ever need to deal with multiple value |
| 225 | // types at this point. |
| 226 | assert(OtherNode->getExtTypes().size() == 1 && "Node has too many types!"); |
| 227 | if (OtherNode->hasTypeSet() && OtherNode->getTypeNum(0) <= VT) |
| 228 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
| 229 | return false; |
| 230 | } |
| 231 | case SDTCisOpSmallerThanOp: { |
| 232 | TreePatternNode *BigOperand = |
| 233 | getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NumResults); |
| 234 | |
| 235 | // Both operands must be integer or FP, but we don't care which. |
| 236 | bool MadeChange = false; |
| 237 | |
| 238 | // This code does not currently handle nodes which have multiple types, |
| 239 | // where some types are integer, and some are fp. Assert that this is not |
| 240 | // the case. |
| 241 | assert(!(MVT::isExtIntegerInVTs(NodeToApply->getExtTypes()) && |
| 242 | MVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) && |
| 243 | !(MVT::isExtIntegerInVTs(BigOperand->getExtTypes()) && |
| 244 | MVT::isExtFloatingPointInVTs(BigOperand->getExtTypes())) && |
| 245 | "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); |
| 246 | if (MVT::isExtIntegerInVTs(NodeToApply->getExtTypes())) |
| 247 | MadeChange |= BigOperand->UpdateNodeType(MVT::isInt, TP); |
| 248 | else if (MVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) |
| 249 | MadeChange |= BigOperand->UpdateNodeType(MVT::isFP, TP); |
| 250 | if (MVT::isExtIntegerInVTs(BigOperand->getExtTypes())) |
| 251 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isInt, TP); |
| 252 | else if (MVT::isExtFloatingPointInVTs(BigOperand->getExtTypes())) |
| 253 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isFP, TP); |
| 254 | |
| 255 | std::vector<MVT::ValueType> VTs = CGT.getLegalValueTypes(); |
| 256 | |
| 257 | if (MVT::isExtIntegerInVTs(NodeToApply->getExtTypes())) { |
| 258 | VTs = FilterVTs(VTs, MVT::isInteger); |
| 259 | } else if (MVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) { |
| 260 | VTs = FilterVTs(VTs, MVT::isFloatingPoint); |
| 261 | } else { |
| 262 | VTs.clear(); |
| 263 | } |
| 264 | |
| 265 | switch (VTs.size()) { |
| 266 | default: // Too many VT's to pick from. |
| 267 | case 0: break; // No info yet. |
| 268 | case 1: |
| 269 | // Only one VT of this flavor. Cannot ever satisify the constraints. |
| 270 | return NodeToApply->UpdateNodeType(MVT::Other, TP); // throw |
| 271 | case 2: |
| 272 | // If we have exactly two possible types, the little operand must be the |
| 273 | // small one, the big operand should be the big one. Common with |
| 274 | // float/double for example. |
| 275 | assert(VTs[0] < VTs[1] && "Should be sorted!"); |
| 276 | MadeChange |= NodeToApply->UpdateNodeType(VTs[0], TP); |
| 277 | MadeChange |= BigOperand->UpdateNodeType(VTs[1], TP); |
| 278 | break; |
| 279 | } |
| 280 | return MadeChange; |
| 281 | } |
| 282 | case SDTCisIntVectorOfSameSize: { |
| 283 | TreePatternNode *OtherOperand = |
| 284 | getOperandNum(x.SDTCisIntVectorOfSameSize_Info.OtherOperandNum, |
| 285 | N, NumResults); |
| 286 | if (OtherOperand->hasTypeSet()) { |
| 287 | if (!MVT::isVector(OtherOperand->getTypeNum(0))) |
| 288 | TP.error(N->getOperator()->getName() + " VT operand must be a vector!"); |
| 289 | MVT::ValueType IVT = OtherOperand->getTypeNum(0); |
| 290 | IVT = MVT::getIntVectorWithNumElements(MVT::getVectorNumElements(IVT)); |
| 291 | return NodeToApply->UpdateNodeType(IVT, TP); |
| 292 | } |
| 293 | return false; |
| 294 | } |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame^] | 295 | case SDTCisEltOfVec: { |
| 296 | TreePatternNode *OtherOperand = |
| 297 | getOperandNum(x.SDTCisIntVectorOfSameSize_Info.OtherOperandNum, |
| 298 | N, NumResults); |
| 299 | if (OtherOperand->hasTypeSet()) { |
| 300 | if (!MVT::isVector(OtherOperand->getTypeNum(0))) |
| 301 | TP.error(N->getOperator()->getName() + " VT operand must be a vector!"); |
| 302 | MVT::ValueType IVT = OtherOperand->getTypeNum(0); |
| 303 | IVT = MVT::getVectorElementType(IVT); |
| 304 | return NodeToApply->UpdateNodeType(IVT, TP); |
| 305 | } |
| 306 | return false; |
| 307 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 308 | } |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | //===----------------------------------------------------------------------===// |
| 313 | // SDNodeInfo implementation |
| 314 | // |
| 315 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 316 | EnumName = R->getValueAsString("Opcode"); |
| 317 | SDClassName = R->getValueAsString("SDClass"); |
| 318 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 319 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 320 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 321 | |
| 322 | // Parse the properties. |
| 323 | Properties = 0; |
| 324 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
| 325 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) { |
| 326 | if (PropList[i]->getName() == "SDNPCommutative") { |
| 327 | Properties |= 1 << SDNPCommutative; |
| 328 | } else if (PropList[i]->getName() == "SDNPAssociative") { |
| 329 | Properties |= 1 << SDNPAssociative; |
| 330 | } else if (PropList[i]->getName() == "SDNPHasChain") { |
| 331 | Properties |= 1 << SDNPHasChain; |
| 332 | } else if (PropList[i]->getName() == "SDNPOutFlag") { |
| 333 | Properties |= 1 << SDNPOutFlag; |
| 334 | } else if (PropList[i]->getName() == "SDNPInFlag") { |
| 335 | Properties |= 1 << SDNPInFlag; |
| 336 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 337 | Properties |= 1 << SDNPOptInFlag; |
Chris Lattner | c8478d8 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 338 | } else if (PropList[i]->getName() == "SDNPMayStore") { |
| 339 | Properties |= 1 << SDNPMayStore; |
Chris Lattner | 710e995 | 2008-01-10 04:38:57 +0000 | [diff] [blame] | 340 | } else if (PropList[i]->getName() == "SDNPMayLoad") { |
| 341 | Properties |= 1 << SDNPMayLoad; |
Chris Lattner | bc0b9f7 | 2008-01-10 05:39:30 +0000 | [diff] [blame] | 342 | } else if (PropList[i]->getName() == "SDNPSideEffect") { |
| 343 | Properties |= 1 << SDNPSideEffect; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 344 | } else { |
| 345 | cerr << "Unknown SD Node property '" << PropList[i]->getName() |
| 346 | << "' on node '" << R->getName() << "'!\n"; |
| 347 | exit(1); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | |
| 352 | // Parse the type constraints. |
| 353 | std::vector<Record*> ConstraintList = |
| 354 | TypeProfile->getValueAsListOfDefs("Constraints"); |
| 355 | TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); |
| 356 | } |
| 357 | |
| 358 | //===----------------------------------------------------------------------===// |
| 359 | // TreePatternNode implementation |
| 360 | // |
| 361 | |
| 362 | TreePatternNode::~TreePatternNode() { |
| 363 | #if 0 // FIXME: implement refcounted tree nodes! |
| 364 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 365 | delete getChild(i); |
| 366 | #endif |
| 367 | } |
| 368 | |
| 369 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 370 | /// information. If N already contains a conflicting type, then throw an |
| 371 | /// exception. This returns true if any information was updated. |
| 372 | /// |
| 373 | bool TreePatternNode::UpdateNodeType(const std::vector<unsigned char> &ExtVTs, |
| 374 | TreePattern &TP) { |
| 375 | assert(!ExtVTs.empty() && "Cannot update node type with empty type vector!"); |
| 376 | |
| 377 | if (ExtVTs[0] == MVT::isUnknown || LHSIsSubsetOfRHS(getExtTypes(), ExtVTs)) |
| 378 | return false; |
| 379 | if (isTypeCompletelyUnknown() || LHSIsSubsetOfRHS(ExtVTs, getExtTypes())) { |
| 380 | setTypes(ExtVTs); |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | if (getExtTypeNum(0) == MVT::iPTR) { |
| 385 | if (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::isInt) |
| 386 | return false; |
| 387 | if (MVT::isExtIntegerInVTs(ExtVTs)) { |
| 388 | std::vector<unsigned char> FVTs = FilterEVTs(ExtVTs, MVT::isInteger); |
| 389 | if (FVTs.size()) { |
| 390 | setTypes(ExtVTs); |
| 391 | return true; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (ExtVTs[0] == MVT::isInt && MVT::isExtIntegerInVTs(getExtTypes())) { |
| 397 | assert(hasTypeSet() && "should be handled above!"); |
| 398 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), MVT::isInteger); |
| 399 | if (getExtTypes() == FVTs) |
| 400 | return false; |
| 401 | setTypes(FVTs); |
| 402 | return true; |
| 403 | } |
| 404 | if (ExtVTs[0] == MVT::iPTR && MVT::isExtIntegerInVTs(getExtTypes())) { |
| 405 | //assert(hasTypeSet() && "should be handled above!"); |
| 406 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), MVT::isInteger); |
| 407 | if (getExtTypes() == FVTs) |
| 408 | return false; |
| 409 | if (FVTs.size()) { |
| 410 | setTypes(FVTs); |
| 411 | return true; |
| 412 | } |
| 413 | } |
| 414 | if (ExtVTs[0] == MVT::isFP && MVT::isExtFloatingPointInVTs(getExtTypes())) { |
| 415 | assert(hasTypeSet() && "should be handled above!"); |
| 416 | std::vector<unsigned char> FVTs = |
| 417 | FilterEVTs(getExtTypes(), MVT::isFloatingPoint); |
| 418 | if (getExtTypes() == FVTs) |
| 419 | return false; |
| 420 | setTypes(FVTs); |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | // If we know this is an int or fp type, and we are told it is a specific one, |
| 425 | // take the advice. |
| 426 | // |
| 427 | // Similarly, we should probably set the type here to the intersection of |
| 428 | // {isInt|isFP} and ExtVTs |
| 429 | if ((getExtTypeNum(0) == MVT::isInt && MVT::isExtIntegerInVTs(ExtVTs)) || |
| 430 | (getExtTypeNum(0) == MVT::isFP && MVT::isExtFloatingPointInVTs(ExtVTs))){ |
| 431 | setTypes(ExtVTs); |
| 432 | return true; |
| 433 | } |
| 434 | if (getExtTypeNum(0) == MVT::isInt && ExtVTs[0] == MVT::iPTR) { |
| 435 | setTypes(ExtVTs); |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | if (isLeaf()) { |
| 440 | dump(); |
| 441 | cerr << " "; |
| 442 | TP.error("Type inference contradiction found in node!"); |
| 443 | } else { |
| 444 | TP.error("Type inference contradiction found in node " + |
| 445 | getOperator()->getName() + "!"); |
| 446 | } |
| 447 | return true; // unreachable |
| 448 | } |
| 449 | |
| 450 | |
| 451 | void TreePatternNode::print(std::ostream &OS) const { |
| 452 | if (isLeaf()) { |
| 453 | OS << *getLeafValue(); |
| 454 | } else { |
| 455 | OS << "(" << getOperator()->getName(); |
| 456 | } |
| 457 | |
| 458 | // FIXME: At some point we should handle printing all the value types for |
| 459 | // nodes that are multiply typed. |
| 460 | switch (getExtTypeNum(0)) { |
| 461 | case MVT::Other: OS << ":Other"; break; |
| 462 | case MVT::isInt: OS << ":isInt"; break; |
| 463 | case MVT::isFP : OS << ":isFP"; break; |
| 464 | case MVT::isUnknown: ; /*OS << ":?";*/ break; |
| 465 | case MVT::iPTR: OS << ":iPTR"; break; |
| 466 | default: { |
| 467 | std::string VTName = llvm::getName(getTypeNum(0)); |
| 468 | // Strip off MVT:: prefix if present. |
| 469 | if (VTName.substr(0,5) == "MVT::") |
| 470 | VTName = VTName.substr(5); |
| 471 | OS << ":" << VTName; |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (!isLeaf()) { |
| 477 | if (getNumChildren() != 0) { |
| 478 | OS << " "; |
| 479 | getChild(0)->print(OS); |
| 480 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 481 | OS << ", "; |
| 482 | getChild(i)->print(OS); |
| 483 | } |
| 484 | } |
| 485 | OS << ")"; |
| 486 | } |
| 487 | |
| 488 | if (!PredicateFn.empty()) |
| 489 | OS << "<<P:" << PredicateFn << ">>"; |
| 490 | if (TransformFn) |
| 491 | OS << "<<X:" << TransformFn->getName() << ">>"; |
| 492 | if (!getName().empty()) |
| 493 | OS << ":$" << getName(); |
| 494 | |
| 495 | } |
| 496 | void TreePatternNode::dump() const { |
| 497 | print(*cerr.stream()); |
| 498 | } |
| 499 | |
| 500 | /// isIsomorphicTo - Return true if this node is recursively isomorphic to |
| 501 | /// the specified node. For this comparison, all of the state of the node |
| 502 | /// is considered, except for the assigned name. Nodes with differing names |
| 503 | /// that are otherwise identical are considered isomorphic. |
| 504 | bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N) const { |
| 505 | if (N == this) return true; |
| 506 | if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || |
| 507 | getPredicateFn() != N->getPredicateFn() || |
| 508 | getTransformFn() != N->getTransformFn()) |
| 509 | return false; |
| 510 | |
| 511 | if (isLeaf()) { |
| 512 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) |
| 513 | if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) |
| 514 | return DI->getDef() == NDI->getDef(); |
| 515 | return getLeafValue() == N->getLeafValue(); |
| 516 | } |
| 517 | |
| 518 | if (N->getOperator() != getOperator() || |
| 519 | N->getNumChildren() != getNumChildren()) return false; |
| 520 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 521 | if (!getChild(i)->isIsomorphicTo(N->getChild(i))) |
| 522 | return false; |
| 523 | return true; |
| 524 | } |
| 525 | |
| 526 | /// clone - Make a copy of this tree and all of its children. |
| 527 | /// |
| 528 | TreePatternNode *TreePatternNode::clone() const { |
| 529 | TreePatternNode *New; |
| 530 | if (isLeaf()) { |
| 531 | New = new TreePatternNode(getLeafValue()); |
| 532 | } else { |
| 533 | std::vector<TreePatternNode*> CChildren; |
| 534 | CChildren.reserve(Children.size()); |
| 535 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 536 | CChildren.push_back(getChild(i)->clone()); |
| 537 | New = new TreePatternNode(getOperator(), CChildren); |
| 538 | } |
| 539 | New->setName(getName()); |
| 540 | New->setTypes(getExtTypes()); |
| 541 | New->setPredicateFn(getPredicateFn()); |
| 542 | New->setTransformFn(getTransformFn()); |
| 543 | return New; |
| 544 | } |
| 545 | |
| 546 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 547 | /// with actual values specified by ArgMap. |
| 548 | void TreePatternNode:: |
| 549 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 550 | if (isLeaf()) return; |
| 551 | |
| 552 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 553 | TreePatternNode *Child = getChild(i); |
| 554 | if (Child->isLeaf()) { |
| 555 | Init *Val = Child->getLeafValue(); |
| 556 | if (dynamic_cast<DefInit*>(Val) && |
| 557 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 558 | // We found a use of a formal argument, replace it with its value. |
| 559 | Child = ArgMap[Child->getName()]; |
| 560 | assert(Child && "Couldn't find formal argument!"); |
| 561 | setChild(i, Child); |
| 562 | } |
| 563 | } else { |
| 564 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | |
| 570 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 571 | /// fragments, inline them into place, giving us a pattern without any |
| 572 | /// PatFrag references. |
| 573 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 574 | if (isLeaf()) return this; // nothing to do. |
| 575 | Record *Op = getOperator(); |
| 576 | |
| 577 | if (!Op->isSubClassOf("PatFrag")) { |
| 578 | // Just recursively inline children nodes. |
| 579 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 580 | setChild(i, getChild(i)->InlinePatternFragments(TP)); |
| 581 | return this; |
| 582 | } |
| 583 | |
| 584 | // Otherwise, we found a reference to a fragment. First, look up its |
| 585 | // TreePattern record. |
| 586 | TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); |
| 587 | |
| 588 | // Verify that we are passing the right number of operands. |
| 589 | if (Frag->getNumArgs() != Children.size()) |
| 590 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 591 | utostr(Frag->getNumArgs()) + " operands!"); |
| 592 | |
| 593 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
| 594 | |
| 595 | // Resolve formal arguments to their actual value. |
| 596 | if (Frag->getNumArgs()) { |
| 597 | // Compute the map of formal to actual arguments. |
| 598 | std::map<std::string, TreePatternNode*> ArgMap; |
| 599 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 600 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 601 | |
| 602 | FragTree->SubstituteFormalArguments(ArgMap); |
| 603 | } |
| 604 | |
| 605 | FragTree->setName(getName()); |
| 606 | FragTree->UpdateNodeType(getExtTypes(), TP); |
| 607 | |
| 608 | // Get a new copy of this fragment to stitch into here. |
| 609 | //delete this; // FIXME: implement refcounting! |
| 610 | return FragTree; |
| 611 | } |
| 612 | |
| 613 | /// getImplicitType - Check to see if the specified record has an implicit |
| 614 | /// type which should be applied to it. This infer the type of register |
| 615 | /// references from the register file information, for example. |
| 616 | /// |
| 617 | static std::vector<unsigned char> getImplicitType(Record *R, bool NotRegisters, |
| 618 | TreePattern &TP) { |
| 619 | // Some common return values |
| 620 | std::vector<unsigned char> Unknown(1, MVT::isUnknown); |
| 621 | std::vector<unsigned char> Other(1, MVT::Other); |
| 622 | |
| 623 | // Check to see if this is a register or a register class... |
| 624 | if (R->isSubClassOf("RegisterClass")) { |
| 625 | if (NotRegisters) |
| 626 | return Unknown; |
| 627 | const CodeGenRegisterClass &RC = |
| 628 | TP.getDAGPatterns().getTargetInfo().getRegisterClass(R); |
| 629 | return ConvertVTs(RC.getValueTypes()); |
| 630 | } else if (R->isSubClassOf("PatFrag")) { |
| 631 | // Pattern fragment types will be resolved when they are inlined. |
| 632 | return Unknown; |
| 633 | } else if (R->isSubClassOf("Register")) { |
| 634 | if (NotRegisters) |
| 635 | return Unknown; |
| 636 | const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); |
| 637 | return T.getRegisterVTs(R); |
| 638 | } else if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) { |
| 639 | // Using a VTSDNode or CondCodeSDNode. |
| 640 | return Other; |
| 641 | } else if (R->isSubClassOf("ComplexPattern")) { |
| 642 | if (NotRegisters) |
| 643 | return Unknown; |
| 644 | std::vector<unsigned char> |
| 645 | ComplexPat(1, TP.getDAGPatterns().getComplexPattern(R).getValueType()); |
| 646 | return ComplexPat; |
| 647 | } else if (R->getName() == "ptr_rc") { |
| 648 | Other[0] = MVT::iPTR; |
| 649 | return Other; |
| 650 | } else if (R->getName() == "node" || R->getName() == "srcvalue" || |
| 651 | R->getName() == "zero_reg") { |
| 652 | // Placeholder. |
| 653 | return Unknown; |
| 654 | } |
| 655 | |
| 656 | TP.error("Unknown node flavor used in pattern: " + R->getName()); |
| 657 | return Other; |
| 658 | } |
| 659 | |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 660 | |
| 661 | /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the |
| 662 | /// CodeGenIntrinsic information for it, otherwise return a null pointer. |
| 663 | const CodeGenIntrinsic *TreePatternNode:: |
| 664 | getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { |
| 665 | if (getOperator() != CDP.get_intrinsic_void_sdnode() && |
| 666 | getOperator() != CDP.get_intrinsic_w_chain_sdnode() && |
| 667 | getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) |
| 668 | return 0; |
| 669 | |
| 670 | unsigned IID = |
| 671 | dynamic_cast<IntInit*>(getChild(0)->getLeafValue())->getValue(); |
| 672 | return &CDP.getIntrinsicInfo(IID); |
| 673 | } |
| 674 | |
| 675 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 676 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 677 | /// this node and its children in the tree. This returns true if it makes a |
| 678 | /// change, false otherwise. If a type contradiction is found, throw an |
| 679 | /// exception. |
| 680 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 681 | CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 682 | if (isLeaf()) { |
| 683 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) { |
| 684 | // If it's a regclass or something else known, include the type. |
| 685 | return UpdateNodeType(getImplicitType(DI->getDef(), NotRegisters, TP),TP); |
| 686 | } else if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) { |
| 687 | // Int inits are always integers. :) |
| 688 | bool MadeChange = UpdateNodeType(MVT::isInt, TP); |
| 689 | |
| 690 | if (hasTypeSet()) { |
| 691 | // At some point, it may make sense for this tree pattern to have |
| 692 | // multiple types. Assert here that it does not, so we revisit this |
| 693 | // code when appropriate. |
| 694 | assert(getExtTypes().size() >= 1 && "TreePattern doesn't have a type!"); |
| 695 | MVT::ValueType VT = getTypeNum(0); |
| 696 | for (unsigned i = 1, e = getExtTypes().size(); i != e; ++i) |
| 697 | assert(getTypeNum(i) == VT && "TreePattern has too many types!"); |
| 698 | |
| 699 | VT = getTypeNum(0); |
| 700 | if (VT != MVT::iPTR) { |
| 701 | unsigned Size = MVT::getSizeInBits(VT); |
| 702 | // Make sure that the value is representable for this type. |
| 703 | if (Size < 32) { |
| 704 | int Val = (II->getValue() << (32-Size)) >> (32-Size); |
| 705 | if (Val != II->getValue()) |
| 706 | TP.error("Sign-extended integer value '" + itostr(II->getValue())+ |
| 707 | "' is out of range for type '" + |
| 708 | getEnumName(getTypeNum(0)) + "'!"); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | return MadeChange; |
| 714 | } |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | // special handling for set, which isn't really an SDNode. |
| 719 | if (getOperator()->getName() == "set") { |
| 720 | assert (getNumChildren() >= 2 && "Missing RHS of a set?"); |
| 721 | unsigned NC = getNumChildren(); |
| 722 | bool MadeChange = false; |
| 723 | for (unsigned i = 0; i < NC-1; ++i) { |
| 724 | MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 725 | MadeChange |= getChild(NC-1)->ApplyTypeConstraints(TP, NotRegisters); |
| 726 | |
| 727 | // Types of operands must match. |
| 728 | MadeChange |= getChild(i)->UpdateNodeType(getChild(NC-1)->getExtTypes(), |
| 729 | TP); |
| 730 | MadeChange |= getChild(NC-1)->UpdateNodeType(getChild(i)->getExtTypes(), |
| 731 | TP); |
| 732 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 733 | } |
| 734 | return MadeChange; |
| 735 | } else if (getOperator()->getName() == "implicit" || |
| 736 | getOperator()->getName() == "parallel") { |
| 737 | bool MadeChange = false; |
| 738 | for (unsigned i = 0; i < getNumChildren(); ++i) |
| 739 | MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 740 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 741 | return MadeChange; |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 742 | } else if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 743 | bool MadeChange = false; |
| 744 | |
| 745 | // Apply the result type to the node. |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 746 | MadeChange = UpdateNodeType(Int->ArgVTs[0], TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 747 | |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 748 | if (getNumChildren() != Int->ArgVTs.size()) |
| 749 | TP.error("Intrinsic '" + Int->Name + "' expects " + |
| 750 | utostr(Int->ArgVTs.size()-1) + " operands, not " + |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 751 | utostr(getNumChildren()-1) + " operands!"); |
| 752 | |
| 753 | // Apply type info to the intrinsic ID. |
| 754 | MadeChange |= getChild(0)->UpdateNodeType(MVT::iPTR, TP); |
| 755 | |
| 756 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 757 | MVT::ValueType OpVT = Int->ArgVTs[i]; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 758 | MadeChange |= getChild(i)->UpdateNodeType(OpVT, TP); |
| 759 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 760 | } |
| 761 | return MadeChange; |
| 762 | } else if (getOperator()->isSubClassOf("SDNode")) { |
| 763 | const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); |
| 764 | |
| 765 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 766 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 767 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 768 | // Branch, etc. do not produce results and top-level forms in instr pattern |
| 769 | // must have void types. |
| 770 | if (NI.getNumResults() == 0) |
| 771 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 772 | |
| 773 | // If this is a vector_shuffle operation, apply types to the build_vector |
| 774 | // operation. The types of the integers don't matter, but this ensures they |
| 775 | // won't get checked. |
| 776 | if (getOperator()->getName() == "vector_shuffle" && |
| 777 | getChild(2)->getOperator()->getName() == "build_vector") { |
| 778 | TreePatternNode *BV = getChild(2); |
| 779 | const std::vector<MVT::ValueType> &LegalVTs |
| 780 | = CDP.getTargetInfo().getLegalValueTypes(); |
| 781 | MVT::ValueType LegalIntVT = MVT::Other; |
| 782 | for (unsigned i = 0, e = LegalVTs.size(); i != e; ++i) |
| 783 | if (MVT::isInteger(LegalVTs[i]) && !MVT::isVector(LegalVTs[i])) { |
| 784 | LegalIntVT = LegalVTs[i]; |
| 785 | break; |
| 786 | } |
| 787 | assert(LegalIntVT != MVT::Other && "No legal integer VT?"); |
| 788 | |
| 789 | for (unsigned i = 0, e = BV->getNumChildren(); i != e; ++i) |
| 790 | MadeChange |= BV->getChild(i)->UpdateNodeType(LegalIntVT, TP); |
| 791 | } |
| 792 | return MadeChange; |
| 793 | } else if (getOperator()->isSubClassOf("Instruction")) { |
| 794 | const DAGInstruction &Inst = CDP.getInstruction(getOperator()); |
| 795 | bool MadeChange = false; |
| 796 | unsigned NumResults = Inst.getNumResults(); |
| 797 | |
| 798 | assert(NumResults <= 1 && |
| 799 | "Only supports zero or one result instrs!"); |
| 800 | |
| 801 | CodeGenInstruction &InstInfo = |
| 802 | CDP.getTargetInfo().getInstruction(getOperator()->getName()); |
| 803 | // Apply the result type to the node |
| 804 | if (NumResults == 0 || InstInfo.NumDefs == 0) { |
| 805 | MadeChange = UpdateNodeType(MVT::isVoid, TP); |
| 806 | } else { |
| 807 | Record *ResultNode = Inst.getResult(0); |
| 808 | |
| 809 | if (ResultNode->getName() == "ptr_rc") { |
| 810 | std::vector<unsigned char> VT; |
| 811 | VT.push_back(MVT::iPTR); |
| 812 | MadeChange = UpdateNodeType(VT, TP); |
| 813 | } else { |
| 814 | assert(ResultNode->isSubClassOf("RegisterClass") && |
| 815 | "Operands should be register classes!"); |
| 816 | |
| 817 | const CodeGenRegisterClass &RC = |
| 818 | CDP.getTargetInfo().getRegisterClass(ResultNode); |
| 819 | MadeChange = UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | unsigned ChildNo = 0; |
| 824 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
| 825 | Record *OperandNode = Inst.getOperand(i); |
| 826 | |
| 827 | // If the instruction expects a predicate or optional def operand, we |
| 828 | // codegen this by setting the operand to it's default value if it has a |
| 829 | // non-empty DefaultOps field. |
| 830 | if ((OperandNode->isSubClassOf("PredicateOperand") || |
| 831 | OperandNode->isSubClassOf("OptionalDefOperand")) && |
| 832 | !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) |
| 833 | continue; |
| 834 | |
| 835 | // Verify that we didn't run out of provided operands. |
| 836 | if (ChildNo >= getNumChildren()) |
| 837 | TP.error("Instruction '" + getOperator()->getName() + |
| 838 | "' expects more operands than were provided."); |
| 839 | |
| 840 | MVT::ValueType VT; |
| 841 | TreePatternNode *Child = getChild(ChildNo++); |
| 842 | if (OperandNode->isSubClassOf("RegisterClass")) { |
| 843 | const CodeGenRegisterClass &RC = |
| 844 | CDP.getTargetInfo().getRegisterClass(OperandNode); |
| 845 | MadeChange |= Child->UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
| 846 | } else if (OperandNode->isSubClassOf("Operand")) { |
| 847 | VT = getValueType(OperandNode->getValueAsDef("Type")); |
| 848 | MadeChange |= Child->UpdateNodeType(VT, TP); |
| 849 | } else if (OperandNode->getName() == "ptr_rc") { |
| 850 | MadeChange |= Child->UpdateNodeType(MVT::iPTR, TP); |
| 851 | } else { |
| 852 | assert(0 && "Unknown operand type!"); |
| 853 | abort(); |
| 854 | } |
| 855 | MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters); |
| 856 | } |
| 857 | |
| 858 | if (ChildNo != getNumChildren()) |
| 859 | TP.error("Instruction '" + getOperator()->getName() + |
| 860 | "' was provided too many operands!"); |
| 861 | |
| 862 | return MadeChange; |
| 863 | } else { |
| 864 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); |
| 865 | |
| 866 | // Node transforms always take one operand. |
| 867 | if (getNumChildren() != 1) |
| 868 | TP.error("Node transform '" + getOperator()->getName() + |
| 869 | "' requires one operand!"); |
| 870 | |
| 871 | // If either the output or input of the xform does not have exact |
| 872 | // type info. We assume they must be the same. Otherwise, it is perfectly |
| 873 | // legal to transform from one type to a completely different type. |
| 874 | if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { |
| 875 | bool MadeChange = UpdateNodeType(getChild(0)->getExtTypes(), TP); |
| 876 | MadeChange |= getChild(0)->UpdateNodeType(getExtTypes(), TP); |
| 877 | return MadeChange; |
| 878 | } |
| 879 | return false; |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the |
| 884 | /// RHS of a commutative operation, not the on LHS. |
| 885 | static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { |
| 886 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") |
| 887 | return true; |
| 888 | if (N->isLeaf() && dynamic_cast<IntInit*>(N->getLeafValue())) |
| 889 | return true; |
| 890 | return false; |
| 891 | } |
| 892 | |
| 893 | |
| 894 | /// canPatternMatch - If it is impossible for this pattern to match on this |
| 895 | /// target, fill in Reason and return false. Otherwise, return true. This is |
| 896 | /// used as a santity check for .td files (to prevent people from writing stuff |
| 897 | /// that can never possibly work), and to prevent the pattern permuter from |
| 898 | /// generating stuff that is useless. |
| 899 | bool TreePatternNode::canPatternMatch(std::string &Reason, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 900 | CodeGenDAGPatterns &CDP){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 901 | if (isLeaf()) return true; |
| 902 | |
| 903 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 904 | if (!getChild(i)->canPatternMatch(Reason, CDP)) |
| 905 | return false; |
| 906 | |
| 907 | // If this is an intrinsic, handle cases that would make it not match. For |
| 908 | // example, if an operand is required to be an immediate. |
| 909 | if (getOperator()->isSubClassOf("Intrinsic")) { |
| 910 | // TODO: |
| 911 | return true; |
| 912 | } |
| 913 | |
| 914 | // If this node is a commutative operator, check that the LHS isn't an |
| 915 | // immediate. |
| 916 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); |
| 917 | if (NodeInfo.hasProperty(SDNPCommutative)) { |
| 918 | // Scan all of the operands of the node and make sure that only the last one |
| 919 | // is a constant node, unless the RHS also is. |
| 920 | if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { |
| 921 | for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) |
| 922 | if (OnlyOnRHSOfCommutative(getChild(i))) { |
| 923 | Reason="Immediate value must be on the RHS of commutative operators!"; |
| 924 | return false; |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | return true; |
| 930 | } |
| 931 | |
| 932 | //===----------------------------------------------------------------------===// |
| 933 | // TreePattern implementation |
| 934 | // |
| 935 | |
| 936 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 937 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 938 | isInputPattern = isInput; |
| 939 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
| 940 | Trees.push_back(ParseTreePattern((DagInit*)RawPat->getElement(i))); |
| 941 | } |
| 942 | |
| 943 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 944 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 945 | isInputPattern = isInput; |
| 946 | Trees.push_back(ParseTreePattern(Pat)); |
| 947 | } |
| 948 | |
| 949 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 950 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 951 | isInputPattern = isInput; |
| 952 | Trees.push_back(Pat); |
| 953 | } |
| 954 | |
| 955 | |
| 956 | |
| 957 | void TreePattern::error(const std::string &Msg) const { |
| 958 | dump(); |
| 959 | throw "In " + TheRecord->getName() + ": " + Msg; |
| 960 | } |
| 961 | |
| 962 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 963 | DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator()); |
| 964 | if (!OpDef) error("Pattern has unexpected operator type!"); |
| 965 | Record *Operator = OpDef->getDef(); |
| 966 | |
| 967 | if (Operator->isSubClassOf("ValueType")) { |
| 968 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 969 | // node. |
| 970 | if (Dag->getNumArgs() != 1) |
| 971 | error("Type cast only takes one operand!"); |
| 972 | |
| 973 | Init *Arg = Dag->getArg(0); |
| 974 | TreePatternNode *New; |
| 975 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
| 976 | Record *R = DI->getDef(); |
| 977 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 978 | Dag->setArg(0, new DagInit(DI, |
| 979 | std::vector<std::pair<Init*, std::string> >())); |
| 980 | return ParseTreePattern(Dag); |
| 981 | } |
| 982 | New = new TreePatternNode(DI); |
| 983 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 984 | New = ParseTreePattern(DI); |
| 985 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 986 | New = new TreePatternNode(II); |
| 987 | if (!Dag->getArgName(0).empty()) |
| 988 | error("Constant int argument should not have a name!"); |
| 989 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 990 | // Turn this into an IntInit. |
| 991 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 992 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 993 | error("Bits value must be constants!"); |
| 994 | |
| 995 | New = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 996 | if (!Dag->getArgName(0).empty()) |
| 997 | error("Constant int argument should not have a name!"); |
| 998 | } else { |
| 999 | Arg->dump(); |
| 1000 | error("Unknown leaf value for tree pattern!"); |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
| 1004 | // Apply the type cast. |
| 1005 | New->UpdateNodeType(getValueType(Operator), *this); |
| 1006 | New->setName(Dag->getArgName(0)); |
| 1007 | return New; |
| 1008 | } |
| 1009 | |
| 1010 | // Verify that this is something that makes sense for an operator. |
| 1011 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
| 1012 | !Operator->isSubClassOf("Instruction") && |
| 1013 | !Operator->isSubClassOf("SDNodeXForm") && |
| 1014 | !Operator->isSubClassOf("Intrinsic") && |
| 1015 | Operator->getName() != "set" && |
| 1016 | Operator->getName() != "implicit" && |
| 1017 | Operator->getName() != "parallel") |
| 1018 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 1019 | |
| 1020 | // Check to see if this is something that is illegal in an input pattern. |
| 1021 | if (isInputPattern && (Operator->isSubClassOf("Instruction") || |
| 1022 | Operator->isSubClassOf("SDNodeXForm"))) |
| 1023 | error("Cannot use '" + Operator->getName() + "' in an input pattern!"); |
| 1024 | |
| 1025 | std::vector<TreePatternNode*> Children; |
| 1026 | |
| 1027 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 1028 | Init *Arg = Dag->getArg(i); |
| 1029 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 1030 | Children.push_back(ParseTreePattern(DI)); |
| 1031 | if (Children.back()->getName().empty()) |
| 1032 | Children.back()->setName(Dag->getArgName(i)); |
| 1033 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 1034 | Record *R = DefI->getDef(); |
| 1035 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 1036 | // TreePatternNode if its own. |
| 1037 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 1038 | Dag->setArg(i, new DagInit(DefI, |
| 1039 | std::vector<std::pair<Init*, std::string> >())); |
| 1040 | --i; // Revisit this node... |
| 1041 | } else { |
| 1042 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 1043 | Node->setName(Dag->getArgName(i)); |
| 1044 | Children.push_back(Node); |
| 1045 | |
| 1046 | // Input argument? |
| 1047 | if (R->getName() == "node") { |
| 1048 | if (Dag->getArgName(i).empty()) |
| 1049 | error("'node' argument requires a name to match with operand list"); |
| 1050 | Args.push_back(Dag->getArgName(i)); |
| 1051 | } |
| 1052 | } |
| 1053 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 1054 | TreePatternNode *Node = new TreePatternNode(II); |
| 1055 | if (!Dag->getArgName(i).empty()) |
| 1056 | error("Constant int argument should not have a name!"); |
| 1057 | Children.push_back(Node); |
| 1058 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 1059 | // Turn this into an IntInit. |
| 1060 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 1061 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 1062 | error("Bits value must be constants!"); |
| 1063 | |
| 1064 | TreePatternNode *Node = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 1065 | if (!Dag->getArgName(i).empty()) |
| 1066 | error("Constant int argument should not have a name!"); |
| 1067 | Children.push_back(Node); |
| 1068 | } else { |
| 1069 | cerr << '"'; |
| 1070 | Arg->dump(); |
| 1071 | cerr << "\": "; |
| 1072 | error("Unknown leaf value for tree pattern!"); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | // If the operator is an intrinsic, then this is just syntactic sugar for for |
| 1077 | // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and |
| 1078 | // convert the intrinsic name to a number. |
| 1079 | if (Operator->isSubClassOf("Intrinsic")) { |
| 1080 | const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); |
| 1081 | unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; |
| 1082 | |
| 1083 | // If this intrinsic returns void, it must have side-effects and thus a |
| 1084 | // chain. |
| 1085 | if (Int.ArgVTs[0] == MVT::isVoid) { |
| 1086 | Operator = getDAGPatterns().get_intrinsic_void_sdnode(); |
| 1087 | } else if (Int.ModRef != CodeGenIntrinsic::NoMem) { |
| 1088 | // Has side-effects, requires chain. |
| 1089 | Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); |
| 1090 | } else { |
| 1091 | // Otherwise, no chain. |
| 1092 | Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); |
| 1093 | } |
| 1094 | |
| 1095 | TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID)); |
| 1096 | Children.insert(Children.begin(), IIDNode); |
| 1097 | } |
| 1098 | |
| 1099 | return new TreePatternNode(Operator, Children); |
| 1100 | } |
| 1101 | |
| 1102 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 1103 | /// patterns as possible. Return true if all types are infered, false |
| 1104 | /// otherwise. Throw an exception if a type contradiction is found. |
| 1105 | bool TreePattern::InferAllTypes() { |
| 1106 | bool MadeChange = true; |
| 1107 | while (MadeChange) { |
| 1108 | MadeChange = false; |
| 1109 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 1110 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); |
| 1111 | } |
| 1112 | |
| 1113 | bool HasUnresolvedTypes = false; |
| 1114 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 1115 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 1116 | return !HasUnresolvedTypes; |
| 1117 | } |
| 1118 | |
| 1119 | void TreePattern::print(std::ostream &OS) const { |
| 1120 | OS << getRecord()->getName(); |
| 1121 | if (!Args.empty()) { |
| 1122 | OS << "(" << Args[0]; |
| 1123 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 1124 | OS << ", " << Args[i]; |
| 1125 | OS << ")"; |
| 1126 | } |
| 1127 | OS << ": "; |
| 1128 | |
| 1129 | if (Trees.size() > 1) |
| 1130 | OS << "[\n"; |
| 1131 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 1132 | OS << "\t"; |
| 1133 | Trees[i]->print(OS); |
| 1134 | OS << "\n"; |
| 1135 | } |
| 1136 | |
| 1137 | if (Trees.size() > 1) |
| 1138 | OS << "]\n"; |
| 1139 | } |
| 1140 | |
| 1141 | void TreePattern::dump() const { print(*cerr.stream()); } |
| 1142 | |
| 1143 | //===----------------------------------------------------------------------===// |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1144 | // CodeGenDAGPatterns implementation |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1145 | // |
| 1146 | |
| 1147 | // FIXME: REMOVE OSTREAM ARGUMENT |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1148 | CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : Records(R) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1149 | Intrinsics = LoadIntrinsics(Records); |
| 1150 | ParseNodeInfo(); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 1151 | ParseNodeTransforms(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1152 | ParseComplexPatterns(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1153 | ParsePatternFragments(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1154 | ParseDefaultOperands(); |
| 1155 | ParseInstructions(); |
| 1156 | ParsePatterns(); |
| 1157 | |
| 1158 | // Generate variants. For example, commutative patterns can match |
| 1159 | // multiple ways. Add them to PatternsToMatch as well. |
| 1160 | GenerateVariants(); |
| 1161 | } |
| 1162 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1163 | CodeGenDAGPatterns::~CodeGenDAGPatterns() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1164 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 1165 | E = PatternFragments.end(); I != E; ++I) |
| 1166 | delete I->second; |
| 1167 | } |
| 1168 | |
| 1169 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1170 | Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1171 | Record *N = Records.getDef(Name); |
| 1172 | if (!N || !N->isSubClassOf("SDNode")) { |
| 1173 | cerr << "Error getting SDNode '" << Name << "'!\n"; |
| 1174 | exit(1); |
| 1175 | } |
| 1176 | return N; |
| 1177 | } |
| 1178 | |
| 1179 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1180 | void CodeGenDAGPatterns::ParseNodeInfo() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1181 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 1182 | while (!Nodes.empty()) { |
| 1183 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 1184 | Nodes.pop_back(); |
| 1185 | } |
| 1186 | |
| 1187 | // Get the buildin intrinsic nodes. |
| 1188 | intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); |
| 1189 | intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); |
| 1190 | intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); |
| 1191 | } |
| 1192 | |
| 1193 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 1194 | /// map, and emit them to the file as functions. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1195 | void CodeGenDAGPatterns::ParseNodeTransforms() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1196 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 1197 | while (!Xforms.empty()) { |
| 1198 | Record *XFormNode = Xforms.back(); |
| 1199 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 1200 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 1201 | SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code))); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1202 | |
| 1203 | Xforms.pop_back(); |
| 1204 | } |
| 1205 | } |
| 1206 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1207 | void CodeGenDAGPatterns::ParseComplexPatterns() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1208 | std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); |
| 1209 | while (!AMs.empty()) { |
| 1210 | ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); |
| 1211 | AMs.pop_back(); |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 1217 | /// file, building up the PatternFragments map. After we've collected them all, |
| 1218 | /// inline fragments together as necessary, so that there are no references left |
| 1219 | /// inside a pattern fragment to a pattern fragment. |
| 1220 | /// |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1221 | void CodeGenDAGPatterns::ParsePatternFragments() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1222 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 1223 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1224 | // First step, parse all of the fragments. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1225 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
| 1226 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
| 1227 | TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this); |
| 1228 | PatternFragments[Fragments[i]] = P; |
| 1229 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1230 | // Validate the argument list, converting it to set, to discard duplicates. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1231 | std::vector<std::string> &Args = P->getArgList(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1232 | std::set<std::string> OperandsSet(Args.begin(), Args.end()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1234 | if (OperandsSet.count("")) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1235 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 1236 | |
| 1237 | // Parse the operands list. |
| 1238 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 1239 | DefInit *OpsOp = dynamic_cast<DefInit*>(OpsList->getOperator()); |
| 1240 | // Special cases: ops == outs == ins. Different names are used to |
| 1241 | // improve readibility. |
| 1242 | if (!OpsOp || |
| 1243 | (OpsOp->getDef()->getName() != "ops" && |
| 1244 | OpsOp->getDef()->getName() != "outs" && |
| 1245 | OpsOp->getDef()->getName() != "ins")) |
| 1246 | P->error("Operands list should start with '(ops ... '!"); |
| 1247 | |
| 1248 | // Copy over the arguments. |
| 1249 | Args.clear(); |
| 1250 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 1251 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 1252 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 1253 | getDef()->getName() != "node") |
| 1254 | P->error("Operands list should all be 'node' values."); |
| 1255 | if (OpsList->getArgName(j).empty()) |
| 1256 | P->error("Operands list should have names for each operand!"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1257 | if (!OperandsSet.count(OpsList->getArgName(j))) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1258 | P->error("'" + OpsList->getArgName(j) + |
| 1259 | "' does not occur in pattern or was multiply specified!"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1260 | OperandsSet.erase(OpsList->getArgName(j)); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1261 | Args.push_back(OpsList->getArgName(j)); |
| 1262 | } |
| 1263 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1264 | if (!OperandsSet.empty()) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1265 | P->error("Operands list does not contain an entry for operand '" + |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1266 | *OperandsSet.begin() + "'!"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1268 | // If there is a code init for this fragment, keep track of the fact that |
| 1269 | // this fragment uses it. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1270 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1271 | if (!Code.empty()) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1272 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1273 | |
| 1274 | // If there is a node transformation corresponding to this, keep track of |
| 1275 | // it. |
| 1276 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 1277 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
| 1278 | P->getOnlyTree()->setTransformFn(Transform); |
| 1279 | } |
| 1280 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1281 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 1282 | // that there are not references to PatFrags left inside of them. |
| 1283 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 1284 | E = PatternFragments.end(); I != E; ++I) { |
| 1285 | TreePattern *ThePat = I->second; |
| 1286 | ThePat->InlinePatternFragments(); |
| 1287 | |
| 1288 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 1289 | // all of them, some may depend on the inputs of the pattern. |
| 1290 | try { |
| 1291 | ThePat->InferAllTypes(); |
| 1292 | } catch (...) { |
| 1293 | // If this pattern fragment is not supported by this target (no types can |
| 1294 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 1295 | // actually used by instructions, the type consistency error will be |
| 1296 | // reported there. |
| 1297 | } |
| 1298 | |
| 1299 | // If debugging, print out the pattern fragment result. |
| 1300 | DEBUG(ThePat->dump()); |
| 1301 | } |
| 1302 | } |
| 1303 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1304 | void CodeGenDAGPatterns::ParseDefaultOperands() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1305 | std::vector<Record*> DefaultOps[2]; |
| 1306 | DefaultOps[0] = Records.getAllDerivedDefinitions("PredicateOperand"); |
| 1307 | DefaultOps[1] = Records.getAllDerivedDefinitions("OptionalDefOperand"); |
| 1308 | |
| 1309 | // Find some SDNode. |
| 1310 | assert(!SDNodes.empty() && "No SDNodes parsed?"); |
| 1311 | Init *SomeSDNode = new DefInit(SDNodes.begin()->first); |
| 1312 | |
| 1313 | for (unsigned iter = 0; iter != 2; ++iter) { |
| 1314 | for (unsigned i = 0, e = DefaultOps[iter].size(); i != e; ++i) { |
| 1315 | DagInit *DefaultInfo = DefaultOps[iter][i]->getValueAsDag("DefaultOps"); |
| 1316 | |
| 1317 | // Clone the DefaultInfo dag node, changing the operator from 'ops' to |
| 1318 | // SomeSDnode so that we can parse this. |
| 1319 | std::vector<std::pair<Init*, std::string> > Ops; |
| 1320 | for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) |
| 1321 | Ops.push_back(std::make_pair(DefaultInfo->getArg(op), |
| 1322 | DefaultInfo->getArgName(op))); |
| 1323 | DagInit *DI = new DagInit(SomeSDNode, Ops); |
| 1324 | |
| 1325 | // Create a TreePattern to parse this. |
| 1326 | TreePattern P(DefaultOps[iter][i], DI, false, *this); |
| 1327 | assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!"); |
| 1328 | |
| 1329 | // Copy the operands over into a DAGDefaultOperand. |
| 1330 | DAGDefaultOperand DefaultOpInfo; |
| 1331 | |
| 1332 | TreePatternNode *T = P.getTree(0); |
| 1333 | for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { |
| 1334 | TreePatternNode *TPN = T->getChild(op); |
| 1335 | while (TPN->ApplyTypeConstraints(P, false)) |
| 1336 | /* Resolve all types */; |
| 1337 | |
| 1338 | if (TPN->ContainsUnresolvedType()) |
| 1339 | if (iter == 0) |
| 1340 | throw "Value #" + utostr(i) + " of PredicateOperand '" + |
| 1341 | DefaultOps[iter][i]->getName() + "' doesn't have a concrete type!"; |
| 1342 | else |
| 1343 | throw "Value #" + utostr(i) + " of OptionalDefOperand '" + |
| 1344 | DefaultOps[iter][i]->getName() + "' doesn't have a concrete type!"; |
| 1345 | |
| 1346 | DefaultOpInfo.DefaultOps.push_back(TPN); |
| 1347 | } |
| 1348 | |
| 1349 | // Insert it into the DefaultOperands map so we can find it later. |
| 1350 | DefaultOperands[DefaultOps[iter][i]] = DefaultOpInfo; |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an |
| 1356 | /// instruction input. Return true if this is a real use. |
| 1357 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
| 1358 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1359 | std::vector<Record*> &InstImpInputs) { |
| 1360 | // No name -> not interesting. |
| 1361 | if (Pat->getName().empty()) { |
| 1362 | if (Pat->isLeaf()) { |
| 1363 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1364 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 1365 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
| 1366 | else if (DI && DI->getDef()->isSubClassOf("Register")) |
| 1367 | InstImpInputs.push_back(DI->getDef()); |
| 1368 | ; |
| 1369 | } |
| 1370 | return false; |
| 1371 | } |
| 1372 | |
| 1373 | Record *Rec; |
| 1374 | if (Pat->isLeaf()) { |
| 1375 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1376 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 1377 | Rec = DI->getDef(); |
| 1378 | } else { |
| 1379 | assert(Pat->getNumChildren() == 0 && "can't be a use with children!"); |
| 1380 | Rec = Pat->getOperator(); |
| 1381 | } |
| 1382 | |
| 1383 | // SRCVALUE nodes are ignored. |
| 1384 | if (Rec->getName() == "srcvalue") |
| 1385 | return false; |
| 1386 | |
| 1387 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 1388 | if (!Slot) { |
| 1389 | Slot = Pat; |
| 1390 | } else { |
| 1391 | Record *SlotRec; |
| 1392 | if (Slot->isLeaf()) { |
| 1393 | SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
| 1394 | } else { |
| 1395 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 1396 | SlotRec = Slot->getOperator(); |
| 1397 | } |
| 1398 | |
| 1399 | // Ensure that the inputs agree if we've already seen this input. |
| 1400 | if (Rec != SlotRec) |
| 1401 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 1402 | if (Slot->getExtTypes() != Pat->getExtTypes()) |
| 1403 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 1404 | } |
| 1405 | return true; |
| 1406 | } |
| 1407 | |
| 1408 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 1409 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 1410 | /// the pattern. Report errors if we see anything naughty. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1411 | void CodeGenDAGPatterns:: |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1412 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 1413 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1414 | std::map<std::string, TreePatternNode*>&InstResults, |
| 1415 | std::vector<Record*> &InstImpInputs, |
| 1416 | std::vector<Record*> &InstImpResults) { |
| 1417 | if (Pat->isLeaf()) { |
| 1418 | bool isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
| 1419 | if (!isUse && Pat->getTransformFn()) |
| 1420 | I->error("Cannot specify a transform function for a non-input value!"); |
| 1421 | return; |
| 1422 | } else if (Pat->getOperator()->getName() == "implicit") { |
| 1423 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
| 1424 | TreePatternNode *Dest = Pat->getChild(i); |
| 1425 | if (!Dest->isLeaf()) |
| 1426 | I->error("implicitly defined value should be a register!"); |
| 1427 | |
| 1428 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1429 | if (!Val || !Val->getDef()->isSubClassOf("Register")) |
| 1430 | I->error("implicitly defined value should be a register!"); |
| 1431 | InstImpResults.push_back(Val->getDef()); |
| 1432 | } |
| 1433 | return; |
| 1434 | } else if (Pat->getOperator()->getName() != "set") { |
| 1435 | // If this is not a set, verify that the children nodes are not void typed, |
| 1436 | // and recurse. |
| 1437 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
| 1438 | if (Pat->getChild(i)->getExtTypeNum(0) == MVT::isVoid) |
| 1439 | I->error("Cannot have void nodes inside of patterns!"); |
| 1440 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, |
| 1441 | InstImpInputs, InstImpResults); |
| 1442 | } |
| 1443 | |
| 1444 | // If this is a non-leaf node with no children, treat it basically as if |
| 1445 | // it were a leaf. This handles nodes like (imm). |
| 1446 | bool isUse = false; |
| 1447 | if (Pat->getNumChildren() == 0) |
| 1448 | isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
| 1449 | |
| 1450 | if (!isUse && Pat->getTransformFn()) |
| 1451 | I->error("Cannot specify a transform function for a non-input value!"); |
| 1452 | return; |
| 1453 | } |
| 1454 | |
| 1455 | // Otherwise, this is a set, validate and collect instruction results. |
| 1456 | if (Pat->getNumChildren() == 0) |
| 1457 | I->error("set requires operands!"); |
| 1458 | |
| 1459 | if (Pat->getTransformFn()) |
| 1460 | I->error("Cannot specify a transform function on a set node!"); |
| 1461 | |
| 1462 | // Check the set destinations. |
| 1463 | unsigned NumDests = Pat->getNumChildren()-1; |
| 1464 | for (unsigned i = 0; i != NumDests; ++i) { |
| 1465 | TreePatternNode *Dest = Pat->getChild(i); |
| 1466 | if (!Dest->isLeaf()) |
| 1467 | I->error("set destination should be a register!"); |
| 1468 | |
| 1469 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1470 | if (!Val) |
| 1471 | I->error("set destination should be a register!"); |
| 1472 | |
| 1473 | if (Val->getDef()->isSubClassOf("RegisterClass") || |
| 1474 | Val->getDef()->getName() == "ptr_rc") { |
| 1475 | if (Dest->getName().empty()) |
| 1476 | I->error("set destination must have a name!"); |
| 1477 | if (InstResults.count(Dest->getName())) |
| 1478 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
| 1479 | InstResults[Dest->getName()] = Dest; |
| 1480 | } else if (Val->getDef()->isSubClassOf("Register")) { |
| 1481 | InstImpResults.push_back(Val->getDef()); |
| 1482 | } else { |
| 1483 | I->error("set destination should be a register!"); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | // Verify and collect info from the computation. |
| 1488 | FindPatternInputsAndOutputs(I, Pat->getChild(NumDests), |
| 1489 | InstInputs, InstResults, |
| 1490 | InstImpInputs, InstImpResults); |
| 1491 | } |
| 1492 | |
| 1493 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 1494 | /// any fragments involved. This populates the Instructions list with fully |
| 1495 | /// resolved instructions. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1496 | void CodeGenDAGPatterns::ParseInstructions() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1497 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 1498 | |
| 1499 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
| 1500 | ListInit *LI = 0; |
| 1501 | |
| 1502 | if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 1503 | LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 1504 | |
| 1505 | // If there is no pattern, only collect minimal information about the |
| 1506 | // instruction for its operand list. We have to assume that there is one |
| 1507 | // result, as we have no detailed info. |
| 1508 | if (!LI || LI->getSize() == 0) { |
| 1509 | std::vector<Record*> Results; |
| 1510 | std::vector<Record*> Operands; |
| 1511 | |
| 1512 | CodeGenInstruction &InstInfo =Target.getInstruction(Instrs[i]->getName()); |
| 1513 | |
| 1514 | if (InstInfo.OperandList.size() != 0) { |
| 1515 | if (InstInfo.NumDefs == 0) { |
| 1516 | // These produce no results |
| 1517 | for (unsigned j = 0, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1518 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1519 | } else { |
| 1520 | // Assume the first operand is the result. |
| 1521 | Results.push_back(InstInfo.OperandList[0].Rec); |
| 1522 | |
| 1523 | // The rest are inputs. |
| 1524 | for (unsigned j = 1, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1525 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | // Create and insert the instruction. |
| 1530 | std::vector<Record*> ImpResults; |
| 1531 | std::vector<Record*> ImpOperands; |
| 1532 | Instructions.insert(std::make_pair(Instrs[i], |
| 1533 | DAGInstruction(0, Results, Operands, ImpResults, |
| 1534 | ImpOperands))); |
| 1535 | continue; // no pattern. |
| 1536 | } |
| 1537 | |
| 1538 | // Parse the instruction. |
| 1539 | TreePattern *I = new TreePattern(Instrs[i], LI, true, *this); |
| 1540 | // Inline pattern fragments into it. |
| 1541 | I->InlinePatternFragments(); |
| 1542 | |
| 1543 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1544 | // never do anything with this instruction pattern: report it to the user. |
| 1545 | if (!I->InferAllTypes()) |
| 1546 | I->error("Could not infer all types in pattern!"); |
| 1547 | |
| 1548 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 1549 | // with the record they are declared as. |
| 1550 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1551 | |
| 1552 | // InstResults - Keep track of all the virtual registers that are 'set' |
| 1553 | // in the instruction, including what reg class they are. |
| 1554 | std::map<std::string, TreePatternNode*> InstResults; |
| 1555 | |
| 1556 | std::vector<Record*> InstImpInputs; |
| 1557 | std::vector<Record*> InstImpResults; |
| 1558 | |
| 1559 | // Verify that the top-level forms in the instruction are of void type, and |
| 1560 | // fill in the InstResults map. |
| 1561 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 1562 | TreePatternNode *Pat = I->getTree(j); |
| 1563 | if (Pat->getExtTypeNum(0) != MVT::isVoid) |
| 1564 | I->error("Top-level forms in instruction pattern should have" |
| 1565 | " void types"); |
| 1566 | |
| 1567 | // Find inputs and outputs, and verify the structure of the uses/defs. |
| 1568 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, |
| 1569 | InstImpInputs, InstImpResults); |
| 1570 | } |
| 1571 | |
| 1572 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 1573 | // list for the instruction. This determines the order that operands are |
| 1574 | // added to the machine instruction the node corresponds to. |
| 1575 | unsigned NumResults = InstResults.size(); |
| 1576 | |
| 1577 | // Parse the operands list from the (ops) list, validating it. |
| 1578 | assert(I->getArgList().empty() && "Args list should still be empty here!"); |
| 1579 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 1580 | |
| 1581 | // Check that all of the results occur first in the list. |
| 1582 | std::vector<Record*> Results; |
| 1583 | TreePatternNode *Res0Node = NULL; |
| 1584 | for (unsigned i = 0; i != NumResults; ++i) { |
| 1585 | if (i == CGI.OperandList.size()) |
| 1586 | I->error("'" + InstResults.begin()->first + |
| 1587 | "' set but does not appear in operand list!"); |
| 1588 | const std::string &OpName = CGI.OperandList[i].Name; |
| 1589 | |
| 1590 | // Check that it exists in InstResults. |
| 1591 | TreePatternNode *RNode = InstResults[OpName]; |
| 1592 | if (RNode == 0) |
| 1593 | I->error("Operand $" + OpName + " does not exist in operand list!"); |
| 1594 | |
| 1595 | if (i == 0) |
| 1596 | Res0Node = RNode; |
| 1597 | Record *R = dynamic_cast<DefInit*>(RNode->getLeafValue())->getDef(); |
| 1598 | if (R == 0) |
| 1599 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 1600 | "outputs must occur before inputs in operand list!"); |
| 1601 | |
| 1602 | if (CGI.OperandList[i].Rec != R) |
| 1603 | I->error("Operand $" + OpName + " class mismatch!"); |
| 1604 | |
| 1605 | // Remember the return type. |
| 1606 | Results.push_back(CGI.OperandList[i].Rec); |
| 1607 | |
| 1608 | // Okay, this one checks out. |
| 1609 | InstResults.erase(OpName); |
| 1610 | } |
| 1611 | |
| 1612 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 1613 | // the copy while we're checking the inputs. |
| 1614 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
| 1615 | |
| 1616 | std::vector<TreePatternNode*> ResultNodeOperands; |
| 1617 | std::vector<Record*> Operands; |
| 1618 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 1619 | CodeGenInstruction::OperandInfo &Op = CGI.OperandList[i]; |
| 1620 | const std::string &OpName = Op.Name; |
| 1621 | if (OpName.empty()) |
| 1622 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 1623 | |
| 1624 | if (!InstInputsCheck.count(OpName)) { |
| 1625 | // If this is an predicate operand or optional def operand with an |
| 1626 | // DefaultOps set filled in, we can ignore this. When we codegen it, |
| 1627 | // we will do so as always executed. |
| 1628 | if (Op.Rec->isSubClassOf("PredicateOperand") || |
| 1629 | Op.Rec->isSubClassOf("OptionalDefOperand")) { |
| 1630 | // Does it have a non-empty DefaultOps field? If so, ignore this |
| 1631 | // operand. |
| 1632 | if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) |
| 1633 | continue; |
| 1634 | } |
| 1635 | I->error("Operand $" + OpName + |
| 1636 | " does not appear in the instruction pattern"); |
| 1637 | } |
| 1638 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
| 1639 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
| 1640 | |
| 1641 | if (InVal->isLeaf() && |
| 1642 | dynamic_cast<DefInit*>(InVal->getLeafValue())) { |
| 1643 | Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); |
| 1644 | if (Op.Rec != InRec && !InRec->isSubClassOf("ComplexPattern")) |
| 1645 | I->error("Operand $" + OpName + "'s register class disagrees" |
| 1646 | " between the operand and pattern"); |
| 1647 | } |
| 1648 | Operands.push_back(Op.Rec); |
| 1649 | |
| 1650 | // Construct the result for the dest-pattern operand list. |
| 1651 | TreePatternNode *OpNode = InVal->clone(); |
| 1652 | |
| 1653 | // No predicate is useful on the result. |
| 1654 | OpNode->setPredicateFn(""); |
| 1655 | |
| 1656 | // Promote the xform function to be an explicit node if set. |
| 1657 | if (Record *Xform = OpNode->getTransformFn()) { |
| 1658 | OpNode->setTransformFn(0); |
| 1659 | std::vector<TreePatternNode*> Children; |
| 1660 | Children.push_back(OpNode); |
| 1661 | OpNode = new TreePatternNode(Xform, Children); |
| 1662 | } |
| 1663 | |
| 1664 | ResultNodeOperands.push_back(OpNode); |
| 1665 | } |
| 1666 | |
| 1667 | if (!InstInputsCheck.empty()) |
| 1668 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 1669 | " occurs in pattern but not in operands list!"); |
| 1670 | |
| 1671 | TreePatternNode *ResultPattern = |
| 1672 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
| 1673 | // Copy fully inferred output node type to instruction result pattern. |
| 1674 | if (NumResults > 0) |
| 1675 | ResultPattern->setTypes(Res0Node->getExtTypes()); |
| 1676 | |
| 1677 | // Create and insert the instruction. |
| 1678 | // FIXME: InstImpResults and InstImpInputs should not be part of |
| 1679 | // DAGInstruction. |
| 1680 | DAGInstruction TheInst(I, Results, Operands, InstImpResults, InstImpInputs); |
| 1681 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
| 1682 | |
| 1683 | // Use a temporary tree pattern to infer all types and make sure that the |
| 1684 | // constructed result is correct. This depends on the instruction already |
| 1685 | // being inserted into the Instructions map. |
| 1686 | TreePattern Temp(I->getRecord(), ResultPattern, false, *this); |
| 1687 | Temp.InferAllTypes(); |
| 1688 | |
| 1689 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
| 1690 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
| 1691 | |
| 1692 | DEBUG(I->dump()); |
| 1693 | } |
| 1694 | |
| 1695 | // If we can, convert the instructions to be patterns that are matched! |
| 1696 | for (std::map<Record*, DAGInstruction>::iterator II = Instructions.begin(), |
| 1697 | E = Instructions.end(); II != E; ++II) { |
| 1698 | DAGInstruction &TheInst = II->second; |
Chris Lattner | f1ab4f1 | 2008-01-06 01:52:22 +0000 | [diff] [blame] | 1699 | const TreePattern *I = TheInst.getPattern(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1700 | if (I == 0) continue; // No pattern. |
| 1701 | |
| 1702 | // FIXME: Assume only the first tree is the pattern. The others are clobber |
| 1703 | // nodes. |
| 1704 | TreePatternNode *Pattern = I->getTree(0); |
| 1705 | TreePatternNode *SrcPattern; |
| 1706 | if (Pattern->getOperator()->getName() == "set") { |
| 1707 | SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); |
| 1708 | } else{ |
| 1709 | // Not a set (store or something?) |
| 1710 | SrcPattern = Pattern; |
| 1711 | } |
| 1712 | |
| 1713 | std::string Reason; |
| 1714 | if (!SrcPattern->canPatternMatch(Reason, *this)) |
| 1715 | I->error("Instruction can never match: " + Reason); |
| 1716 | |
| 1717 | Record *Instr = II->first; |
| 1718 | TreePatternNode *DstPattern = TheInst.getResultPattern(); |
| 1719 | PatternsToMatch. |
| 1720 | push_back(PatternToMatch(Instr->getValueAsListInit("Predicates"), |
| 1721 | SrcPattern, DstPattern, TheInst.getImpResults(), |
| 1722 | Instr->getValueAsInt("AddedComplexity"))); |
| 1723 | } |
| 1724 | } |
| 1725 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1726 | void CodeGenDAGPatterns::ParsePatterns() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1727 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
| 1728 | |
| 1729 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 1730 | DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch"); |
| 1731 | DefInit *OpDef = dynamic_cast<DefInit*>(Tree->getOperator()); |
| 1732 | Record *Operator = OpDef->getDef(); |
| 1733 | TreePattern *Pattern; |
| 1734 | if (Operator->getName() != "parallel") |
| 1735 | Pattern = new TreePattern(Patterns[i], Tree, true, *this); |
| 1736 | else { |
| 1737 | std::vector<Init*> Values; |
| 1738 | for (unsigned j = 0, ee = Tree->getNumArgs(); j != ee; ++j) |
| 1739 | Values.push_back(Tree->getArg(j)); |
| 1740 | ListInit *LI = new ListInit(Values); |
| 1741 | Pattern = new TreePattern(Patterns[i], LI, true, *this); |
| 1742 | } |
| 1743 | |
| 1744 | // Inline pattern fragments into it. |
| 1745 | Pattern->InlinePatternFragments(); |
| 1746 | |
| 1747 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 1748 | if (LI->getSize() == 0) continue; // no pattern. |
| 1749 | |
| 1750 | // Parse the instruction. |
| 1751 | TreePattern *Result = new TreePattern(Patterns[i], LI, false, *this); |
| 1752 | |
| 1753 | // Inline pattern fragments into it. |
| 1754 | Result->InlinePatternFragments(); |
| 1755 | |
| 1756 | if (Result->getNumTrees() != 1) |
| 1757 | Result->error("Cannot handle instructions producing instructions " |
| 1758 | "with temporaries yet!"); |
| 1759 | |
| 1760 | bool IterateInference; |
| 1761 | bool InferredAllPatternTypes, InferredAllResultTypes; |
| 1762 | do { |
| 1763 | // Infer as many types as possible. If we cannot infer all of them, we |
| 1764 | // can never do anything with this pattern: report it to the user. |
| 1765 | InferredAllPatternTypes = Pattern->InferAllTypes(); |
| 1766 | |
| 1767 | // Infer as many types as possible. If we cannot infer all of them, we |
| 1768 | // can never do anything with this pattern: report it to the user. |
| 1769 | InferredAllResultTypes = Result->InferAllTypes(); |
| 1770 | |
| 1771 | // Apply the type of the result to the source pattern. This helps us |
| 1772 | // resolve cases where the input type is known to be a pointer type (which |
| 1773 | // is considered resolved), but the result knows it needs to be 32- or |
| 1774 | // 64-bits. Infer the other way for good measure. |
| 1775 | IterateInference = Pattern->getTree(0)-> |
| 1776 | UpdateNodeType(Result->getTree(0)->getExtTypes(), *Result); |
| 1777 | IterateInference |= Result->getTree(0)-> |
| 1778 | UpdateNodeType(Pattern->getTree(0)->getExtTypes(), *Result); |
| 1779 | } while (IterateInference); |
| 1780 | |
| 1781 | // Verify that we inferred enough types that we can do something with the |
| 1782 | // pattern and result. If these fire the user has to add type casts. |
| 1783 | if (!InferredAllPatternTypes) |
| 1784 | Pattern->error("Could not infer all types in pattern!"); |
| 1785 | if (!InferredAllResultTypes) |
| 1786 | Result->error("Could not infer all types in pattern result!"); |
| 1787 | |
| 1788 | // Validate that the input pattern is correct. |
| 1789 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1790 | std::map<std::string, TreePatternNode*> InstResults; |
| 1791 | std::vector<Record*> InstImpInputs; |
| 1792 | std::vector<Record*> InstImpResults; |
| 1793 | for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j) |
| 1794 | FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j), |
| 1795 | InstInputs, InstResults, |
| 1796 | InstImpInputs, InstImpResults); |
| 1797 | |
| 1798 | // Promote the xform function to be an explicit node if set. |
| 1799 | TreePatternNode *DstPattern = Result->getOnlyTree(); |
| 1800 | std::vector<TreePatternNode*> ResultNodeOperands; |
| 1801 | for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { |
| 1802 | TreePatternNode *OpNode = DstPattern->getChild(ii); |
| 1803 | if (Record *Xform = OpNode->getTransformFn()) { |
| 1804 | OpNode->setTransformFn(0); |
| 1805 | std::vector<TreePatternNode*> Children; |
| 1806 | Children.push_back(OpNode); |
| 1807 | OpNode = new TreePatternNode(Xform, Children); |
| 1808 | } |
| 1809 | ResultNodeOperands.push_back(OpNode); |
| 1810 | } |
| 1811 | DstPattern = Result->getOnlyTree(); |
| 1812 | if (!DstPattern->isLeaf()) |
| 1813 | DstPattern = new TreePatternNode(DstPattern->getOperator(), |
| 1814 | ResultNodeOperands); |
| 1815 | DstPattern->setTypes(Result->getOnlyTree()->getExtTypes()); |
| 1816 | TreePattern Temp(Result->getRecord(), DstPattern, false, *this); |
| 1817 | Temp.InferAllTypes(); |
| 1818 | |
| 1819 | std::string Reason; |
| 1820 | if (!Pattern->getTree(0)->canPatternMatch(Reason, *this)) |
| 1821 | Pattern->error("Pattern can never match: " + Reason); |
| 1822 | |
| 1823 | PatternsToMatch. |
| 1824 | push_back(PatternToMatch(Patterns[i]->getValueAsListInit("Predicates"), |
| 1825 | Pattern->getTree(0), |
| 1826 | Temp.getOnlyTree(), InstImpResults, |
| 1827 | Patterns[i]->getValueAsInt("AddedComplexity"))); |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | /// CombineChildVariants - Given a bunch of permutations of each child of the |
| 1832 | /// 'operator' node, put them together in all possible ways. |
| 1833 | static void CombineChildVariants(TreePatternNode *Orig, |
| 1834 | const std::vector<std::vector<TreePatternNode*> > &ChildVariants, |
| 1835 | std::vector<TreePatternNode*> &OutVariants, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1836 | CodeGenDAGPatterns &CDP) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1837 | // Make sure that each operand has at least one variant to choose from. |
| 1838 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1839 | if (ChildVariants[i].empty()) |
| 1840 | return; |
| 1841 | |
| 1842 | // The end result is an all-pairs construction of the resultant pattern. |
| 1843 | std::vector<unsigned> Idxs; |
| 1844 | Idxs.resize(ChildVariants.size()); |
| 1845 | bool NotDone = true; |
| 1846 | while (NotDone) { |
| 1847 | // Create the variant and add it to the output list. |
| 1848 | std::vector<TreePatternNode*> NewChildren; |
| 1849 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1850 | NewChildren.push_back(ChildVariants[i][Idxs[i]]); |
| 1851 | TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren); |
| 1852 | |
| 1853 | // Copy over properties. |
| 1854 | R->setName(Orig->getName()); |
| 1855 | R->setPredicateFn(Orig->getPredicateFn()); |
| 1856 | R->setTransformFn(Orig->getTransformFn()); |
| 1857 | R->setTypes(Orig->getExtTypes()); |
| 1858 | |
| 1859 | // If this pattern cannot every match, do not include it as a variant. |
| 1860 | std::string ErrString; |
| 1861 | if (!R->canPatternMatch(ErrString, CDP)) { |
| 1862 | delete R; |
| 1863 | } else { |
| 1864 | bool AlreadyExists = false; |
| 1865 | |
| 1866 | // Scan to see if this pattern has already been emitted. We can get |
| 1867 | // duplication due to things like commuting: |
| 1868 | // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) |
| 1869 | // which are the same pattern. Ignore the dups. |
| 1870 | for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) |
| 1871 | if (R->isIsomorphicTo(OutVariants[i])) { |
| 1872 | AlreadyExists = true; |
| 1873 | break; |
| 1874 | } |
| 1875 | |
| 1876 | if (AlreadyExists) |
| 1877 | delete R; |
| 1878 | else |
| 1879 | OutVariants.push_back(R); |
| 1880 | } |
| 1881 | |
| 1882 | // Increment indices to the next permutation. |
| 1883 | NotDone = false; |
| 1884 | // Look for something we can increment without causing a wrap-around. |
| 1885 | for (unsigned IdxsIdx = 0; IdxsIdx != Idxs.size(); ++IdxsIdx) { |
| 1886 | if (++Idxs[IdxsIdx] < ChildVariants[IdxsIdx].size()) { |
| 1887 | NotDone = true; // Found something to increment. |
| 1888 | break; |
| 1889 | } |
| 1890 | Idxs[IdxsIdx] = 0; |
| 1891 | } |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | /// CombineChildVariants - A helper function for binary operators. |
| 1896 | /// |
| 1897 | static void CombineChildVariants(TreePatternNode *Orig, |
| 1898 | const std::vector<TreePatternNode*> &LHS, |
| 1899 | const std::vector<TreePatternNode*> &RHS, |
| 1900 | std::vector<TreePatternNode*> &OutVariants, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1901 | CodeGenDAGPatterns &CDP) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1902 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 1903 | ChildVariants.push_back(LHS); |
| 1904 | ChildVariants.push_back(RHS); |
| 1905 | CombineChildVariants(Orig, ChildVariants, OutVariants, CDP); |
| 1906 | } |
| 1907 | |
| 1908 | |
| 1909 | static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, |
| 1910 | std::vector<TreePatternNode *> &Children) { |
| 1911 | assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); |
| 1912 | Record *Operator = N->getOperator(); |
| 1913 | |
| 1914 | // Only permit raw nodes. |
| 1915 | if (!N->getName().empty() || !N->getPredicateFn().empty() || |
| 1916 | N->getTransformFn()) { |
| 1917 | Children.push_back(N); |
| 1918 | return; |
| 1919 | } |
| 1920 | |
| 1921 | if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) |
| 1922 | Children.push_back(N->getChild(0)); |
| 1923 | else |
| 1924 | GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); |
| 1925 | |
| 1926 | if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) |
| 1927 | Children.push_back(N->getChild(1)); |
| 1928 | else |
| 1929 | GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); |
| 1930 | } |
| 1931 | |
| 1932 | /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of |
| 1933 | /// the (potentially recursive) pattern by using algebraic laws. |
| 1934 | /// |
| 1935 | static void GenerateVariantsOf(TreePatternNode *N, |
| 1936 | std::vector<TreePatternNode*> &OutVariants, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1937 | CodeGenDAGPatterns &CDP) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1938 | // We cannot permute leaves. |
| 1939 | if (N->isLeaf()) { |
| 1940 | OutVariants.push_back(N); |
| 1941 | return; |
| 1942 | } |
| 1943 | |
| 1944 | // Look up interesting info about the node. |
| 1945 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); |
| 1946 | |
| 1947 | // If this node is associative, reassociate. |
| 1948 | if (NodeInfo.hasProperty(SDNPAssociative)) { |
| 1949 | // Reassociate by pulling together all of the linked operators |
| 1950 | std::vector<TreePatternNode*> MaximalChildren; |
| 1951 | GatherChildrenOfAssociativeOpcode(N, MaximalChildren); |
| 1952 | |
| 1953 | // Only handle child sizes of 3. Otherwise we'll end up trying too many |
| 1954 | // permutations. |
| 1955 | if (MaximalChildren.size() == 3) { |
| 1956 | // Find the variants of all of our maximal children. |
| 1957 | std::vector<TreePatternNode*> AVariants, BVariants, CVariants; |
| 1958 | GenerateVariantsOf(MaximalChildren[0], AVariants, CDP); |
| 1959 | GenerateVariantsOf(MaximalChildren[1], BVariants, CDP); |
| 1960 | GenerateVariantsOf(MaximalChildren[2], CVariants, CDP); |
| 1961 | |
| 1962 | // There are only two ways we can permute the tree: |
| 1963 | // (A op B) op C and A op (B op C) |
| 1964 | // Within these forms, we can also permute A/B/C. |
| 1965 | |
| 1966 | // Generate legal pair permutations of A/B/C. |
| 1967 | std::vector<TreePatternNode*> ABVariants; |
| 1968 | std::vector<TreePatternNode*> BAVariants; |
| 1969 | std::vector<TreePatternNode*> ACVariants; |
| 1970 | std::vector<TreePatternNode*> CAVariants; |
| 1971 | std::vector<TreePatternNode*> BCVariants; |
| 1972 | std::vector<TreePatternNode*> CBVariants; |
| 1973 | CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP); |
| 1974 | CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP); |
| 1975 | CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP); |
| 1976 | CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP); |
| 1977 | CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP); |
| 1978 | CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP); |
| 1979 | |
| 1980 | // Combine those into the result: (x op x) op x |
| 1981 | CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP); |
| 1982 | CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP); |
| 1983 | CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP); |
| 1984 | CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP); |
| 1985 | CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP); |
| 1986 | CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP); |
| 1987 | |
| 1988 | // Combine those into the result: x op (x op x) |
| 1989 | CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP); |
| 1990 | CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP); |
| 1991 | CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP); |
| 1992 | CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP); |
| 1993 | CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP); |
| 1994 | CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP); |
| 1995 | return; |
| 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | // Compute permutations of all children. |
| 2000 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 2001 | ChildVariants.resize(N->getNumChildren()); |
| 2002 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 2003 | GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP); |
| 2004 | |
| 2005 | // Build all permutations based on how the children were formed. |
| 2006 | CombineChildVariants(N, ChildVariants, OutVariants, CDP); |
| 2007 | |
| 2008 | // If this node is commutative, consider the commuted order. |
| 2009 | if (NodeInfo.hasProperty(SDNPCommutative)) { |
| 2010 | assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!"); |
| 2011 | // Don't count children which are actually register references. |
| 2012 | unsigned NC = 0; |
| 2013 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 2014 | TreePatternNode *Child = N->getChild(i); |
| 2015 | if (Child->isLeaf()) |
| 2016 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 2017 | Record *RR = DI->getDef(); |
| 2018 | if (RR->isSubClassOf("Register")) |
| 2019 | continue; |
| 2020 | } |
| 2021 | NC++; |
| 2022 | } |
| 2023 | // Consider the commuted order. |
| 2024 | if (NC == 2) |
| 2025 | CombineChildVariants(N, ChildVariants[1], ChildVariants[0], |
| 2026 | OutVariants, CDP); |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | // GenerateVariants - Generate variants. For example, commutative patterns can |
| 2032 | // match multiple ways. Add them to PatternsToMatch as well. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 2033 | void CodeGenDAGPatterns::GenerateVariants() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2034 | DOUT << "Generating instruction variants.\n"; |
| 2035 | |
| 2036 | // Loop over all of the patterns we've collected, checking to see if we can |
| 2037 | // generate variants of the instruction, through the exploitation of |
| 2038 | // identities. This permits the target to provide agressive matching without |
| 2039 | // the .td file having to contain tons of variants of instructions. |
| 2040 | // |
| 2041 | // Note that this loop adds new patterns to the PatternsToMatch list, but we |
| 2042 | // intentionally do not reconsider these. Any variants of added patterns have |
| 2043 | // already been added. |
| 2044 | // |
| 2045 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 2046 | std::vector<TreePatternNode*> Variants; |
| 2047 | GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this); |
| 2048 | |
| 2049 | assert(!Variants.empty() && "Must create at least original variant!"); |
| 2050 | Variants.erase(Variants.begin()); // Remove the original pattern. |
| 2051 | |
| 2052 | if (Variants.empty()) // No variants for this pattern. |
| 2053 | continue; |
| 2054 | |
| 2055 | DOUT << "FOUND VARIANTS OF: "; |
| 2056 | DEBUG(PatternsToMatch[i].getSrcPattern()->dump()); |
| 2057 | DOUT << "\n"; |
| 2058 | |
| 2059 | for (unsigned v = 0, e = Variants.size(); v != e; ++v) { |
| 2060 | TreePatternNode *Variant = Variants[v]; |
| 2061 | |
| 2062 | DOUT << " VAR#" << v << ": "; |
| 2063 | DEBUG(Variant->dump()); |
| 2064 | DOUT << "\n"; |
| 2065 | |
| 2066 | // Scan to see if an instruction or explicit pattern already matches this. |
| 2067 | bool AlreadyExists = false; |
| 2068 | for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { |
| 2069 | // Check to see if this variant already exists. |
| 2070 | if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern())) { |
| 2071 | DOUT << " *** ALREADY EXISTS, ignoring variant.\n"; |
| 2072 | AlreadyExists = true; |
| 2073 | break; |
| 2074 | } |
| 2075 | } |
| 2076 | // If we already have it, ignore the variant. |
| 2077 | if (AlreadyExists) continue; |
| 2078 | |
| 2079 | // Otherwise, add it to the list of patterns we have. |
| 2080 | PatternsToMatch. |
| 2081 | push_back(PatternToMatch(PatternsToMatch[i].getPredicates(), |
| 2082 | Variant, PatternsToMatch[i].getDstPattern(), |
| 2083 | PatternsToMatch[i].getDstRegs(), |
| 2084 | PatternsToMatch[i].getAddedComplexity())); |
| 2085 | } |
| 2086 | |
| 2087 | DOUT << "\n"; |
| 2088 | } |
| 2089 | } |
| 2090 | |