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