Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1 | //===- CodeGenDAGPatterns.cpp - Read DAG patterns from .td file -----------===// |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 10 | // This file implements the CodeGenDAGPatterns class, which is used to read and |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 11 | // represent the patterns present in a .td file for instructions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | 93c7e41 | 2008-01-05 23:37:52 +0000 | [diff] [blame] | 15 | #include "CodeGenDAGPatterns.h" |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 16 | #include "Record.h" |
| 17 | #include "llvm/ADT/StringExtras.h" |
| 18 | #include "llvm/Support/Debug.h" |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 19 | #include <set> |
Chuck Rose III | 9a79de3 | 2008-01-15 21:43:17 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 21 | #include <iostream> |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // Helpers for working with extended types. |
| 26 | |
| 27 | /// FilterVTs - Filter a list of VT's according to a predicate. |
| 28 | /// |
| 29 | template<typename T> |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 30 | static std::vector<MVT::SimpleValueType> |
| 31 | FilterVTs(const std::vector<MVT::SimpleValueType> &InVTs, T Filter) { |
| 32 | std::vector<MVT::SimpleValueType> Result; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 33 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 34 | if (Filter(InVTs[i])) |
| 35 | Result.push_back(InVTs[i]); |
| 36 | return Result; |
| 37 | } |
| 38 | |
| 39 | template<typename T> |
| 40 | static std::vector<unsigned char> |
| 41 | FilterEVTs(const std::vector<unsigned char> &InVTs, T Filter) { |
| 42 | std::vector<unsigned char> Result; |
| 43 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 44 | if (Filter((MVT::SimpleValueType)InVTs[i])) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 45 | Result.push_back(InVTs[i]); |
| 46 | return Result; |
| 47 | } |
| 48 | |
| 49 | static std::vector<unsigned char> |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 50 | ConvertVTs(const std::vector<MVT::SimpleValueType> &InVTs) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 51 | std::vector<unsigned char> Result; |
| 52 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 53 | Result.push_back(InVTs[i]); |
| 54 | return Result; |
| 55 | } |
| 56 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 57 | static inline bool isInteger(MVT::SimpleValueType VT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 58 | return EVT(VT).isInteger(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 61 | static inline bool isFloatingPoint(MVT::SimpleValueType VT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 62 | return EVT(VT).isFloatingPoint(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 65 | static inline bool isVector(MVT::SimpleValueType VT) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 66 | return EVT(VT).isVector(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 69 | static bool LHSIsSubsetOfRHS(const std::vector<unsigned char> &LHS, |
| 70 | const std::vector<unsigned char> &RHS) { |
| 71 | if (LHS.size() > RHS.size()) return false; |
| 72 | for (unsigned i = 0, e = LHS.size(); i != e; ++i) |
| 73 | if (std::find(RHS.begin(), RHS.end(), LHS[i]) == RHS.end()) |
| 74 | return false; |
| 75 | return true; |
| 76 | } |
| 77 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 78 | namespace llvm { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 79 | namespace EEVT { |
Dan Gohman | 4b6fce4 | 2009-03-31 16:48:35 +0000 | [diff] [blame] | 80 | /// isExtIntegerInVTs - Return true if the specified extended value type vector |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 81 | /// contains iAny or an integer value type. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 82 | bool isExtIntegerInVTs(const std::vector<unsigned char> &EVTs) { |
| 83 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 84 | return EVTs[0] == MVT::iAny || !(FilterEVTs(EVTs, isInteger).empty()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Dan Gohman | 4b6fce4 | 2009-03-31 16:48:35 +0000 | [diff] [blame] | 87 | /// isExtFloatingPointInVTs - Return true if the specified extended value type |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 88 | /// vector contains fAny or a FP value type. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 89 | bool isExtFloatingPointInVTs(const std::vector<unsigned char> &EVTs) { |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 90 | assert(!EVTs.empty() && "Cannot check for FP in empty ExtVT list!"); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 91 | return EVTs[0] == MVT::fAny || !(FilterEVTs(EVTs, isFloatingPoint).empty()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 92 | } |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 93 | |
| 94 | /// isExtVectorInVTs - Return true if the specified extended value type |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 95 | /// vector contains vAny or a vector value type. |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 96 | bool isExtVectorInVTs(const std::vector<unsigned char> &EVTs) { |
| 97 | assert(!EVTs.empty() && "Cannot check for vector in empty ExtVT list!"); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 98 | return EVTs[0] == MVT::vAny || !(FilterEVTs(EVTs, isVector).empty()); |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 99 | } |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 100 | } // end namespace EEVT. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 101 | } // end namespace llvm. |
| 102 | |
Daniel Dunbar | 6f5cc82 | 2009-08-23 09:47:37 +0000 | [diff] [blame] | 103 | bool RecordPtrCmp::operator()(const Record *LHS, const Record *RHS) const { |
| 104 | return LHS->getID() < RHS->getID(); |
| 105 | } |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 106 | |
| 107 | /// Dependent variable map for CodeGenDAGPattern variant generation |
| 108 | typedef std::map<std::string, int> DepVarMap; |
| 109 | |
| 110 | /// Const iterator shorthand for DepVarMap |
| 111 | typedef DepVarMap::const_iterator DepVarMap_citer; |
| 112 | |
| 113 | namespace { |
| 114 | void FindDepVarsOf(TreePatternNode *N, DepVarMap &DepMap) { |
| 115 | if (N->isLeaf()) { |
| 116 | if (dynamic_cast<DefInit*>(N->getLeafValue()) != NULL) { |
| 117 | DepMap[N->getName()]++; |
| 118 | } |
| 119 | } else { |
| 120 | for (size_t i = 0, e = N->getNumChildren(); i != e; ++i) |
| 121 | FindDepVarsOf(N->getChild(i), DepMap); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | //! Find dependent variables within child patterns |
| 126 | /*! |
| 127 | */ |
| 128 | void FindDepVars(TreePatternNode *N, MultipleUseVarSet &DepVars) { |
| 129 | DepVarMap depcounts; |
| 130 | FindDepVarsOf(N, depcounts); |
| 131 | for (DepVarMap_citer i = depcounts.begin(); i != depcounts.end(); ++i) { |
| 132 | if (i->second > 1) { // std::pair<std::string, int> |
| 133 | DepVars.insert(i->first); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | //! Dump the dependent variable set: |
| 139 | void DumpDepVars(MultipleUseVarSet &DepVars) { |
| 140 | if (DepVars.empty()) { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 141 | DEBUG(errs() << "<empty set>"); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 142 | } else { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 143 | DEBUG(errs() << "[ "); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 144 | for (MultipleUseVarSet::const_iterator i = DepVars.begin(), e = DepVars.end(); |
| 145 | i != e; ++i) { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 146 | DEBUG(errs() << (*i) << " "); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 148 | DEBUG(errs() << "]"); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
Dan Gohman | 22bb311 | 2008-08-22 00:20:26 +0000 | [diff] [blame] | 154 | // PatternToMatch implementation |
| 155 | // |
| 156 | |
| 157 | /// getPredicateCheck - Return a single string containing all of this |
| 158 | /// pattern's predicates concatenated with "&&" operators. |
| 159 | /// |
| 160 | std::string PatternToMatch::getPredicateCheck() const { |
| 161 | std::string PredicateCheck; |
| 162 | for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { |
| 163 | if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) { |
| 164 | Record *Def = Pred->getDef(); |
| 165 | if (!Def->isSubClassOf("Predicate")) { |
| 166 | #ifndef NDEBUG |
| 167 | Def->dump(); |
| 168 | #endif |
| 169 | assert(0 && "Unknown predicate type!"); |
| 170 | } |
| 171 | if (!PredicateCheck.empty()) |
| 172 | PredicateCheck += " && "; |
| 173 | PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return PredicateCheck; |
| 178 | } |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 181 | // SDTypeConstraint implementation |
| 182 | // |
| 183 | |
| 184 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 185 | OperandNo = R->getValueAsInt("OperandNum"); |
| 186 | |
| 187 | if (R->isSubClassOf("SDTCisVT")) { |
| 188 | ConstraintType = SDTCisVT; |
| 189 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
| 190 | } else if (R->isSubClassOf("SDTCisPtrTy")) { |
| 191 | ConstraintType = SDTCisPtrTy; |
| 192 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 193 | ConstraintType = SDTCisInt; |
| 194 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 195 | ConstraintType = SDTCisFP; |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 196 | } else if (R->isSubClassOf("SDTCisVec")) { |
| 197 | ConstraintType = SDTCisVec; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 198 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 199 | ConstraintType = SDTCisSameAs; |
| 200 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 201 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 202 | ConstraintType = SDTCisVTSmallerThanOp; |
| 203 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 204 | R->getValueAsInt("OtherOperandNum"); |
| 205 | } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { |
| 206 | ConstraintType = SDTCisOpSmallerThanOp; |
| 207 | x.SDTCisOpSmallerThanOp_Info.BigOperandNum = |
| 208 | R->getValueAsInt("BigOperandNum"); |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 209 | } else if (R->isSubClassOf("SDTCisEltOfVec")) { |
| 210 | ConstraintType = SDTCisEltOfVec; |
| 211 | x.SDTCisEltOfVec_Info.OtherOperandNum = |
| 212 | R->getValueAsInt("OtherOpNum"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 213 | } else { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 214 | errs() << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 215 | exit(1); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 220 | /// N, which has NumResults results. |
| 221 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 222 | TreePatternNode *N, |
| 223 | unsigned NumResults) const { |
| 224 | assert(NumResults <= 1 && |
| 225 | "We only work with nodes with zero or one result so far!"); |
| 226 | |
| 227 | if (OpNo >= (NumResults + N->getNumChildren())) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 228 | errs() << "Invalid operand number " << OpNo << " "; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 229 | N->dump(); |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 230 | errs() << '\n'; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 231 | exit(1); |
| 232 | } |
| 233 | |
| 234 | if (OpNo < NumResults) |
| 235 | return N; // FIXME: need value # |
| 236 | else |
| 237 | return N->getChild(OpNo-NumResults); |
| 238 | } |
| 239 | |
| 240 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 241 | /// constraint to the nodes operands. This returns true if it makes a |
| 242 | /// change, false otherwise. If a type contradiction is found, throw an |
| 243 | /// exception. |
| 244 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 245 | const SDNodeInfo &NodeInfo, |
| 246 | TreePattern &TP) const { |
| 247 | unsigned NumResults = NodeInfo.getNumResults(); |
| 248 | assert(NumResults <= 1 && |
| 249 | "We only work with nodes with zero or one result so far!"); |
| 250 | |
| 251 | // Check that the number of operands is sane. Negative operands -> varargs. |
| 252 | if (NodeInfo.getNumOperands() >= 0) { |
| 253 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 254 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 255 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 256 | } |
| 257 | |
| 258 | const CodeGenTarget &CGT = TP.getDAGPatterns().getTargetInfo(); |
| 259 | |
| 260 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 261 | |
| 262 | switch (ConstraintType) { |
| 263 | default: assert(0 && "Unknown constraint type!"); |
| 264 | case SDTCisVT: |
| 265 | // Operand must be a particular type. |
| 266 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
| 267 | case SDTCisPtrTy: { |
| 268 | // Operand must be same as target pointer type. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 269 | return NodeToApply->UpdateNodeType(MVT::iPTR, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 270 | } |
| 271 | case SDTCisInt: { |
| 272 | // If there is only one integer type supported, this must be it. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 273 | std::vector<MVT::SimpleValueType> IntVTs = |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 274 | FilterVTs(CGT.getLegalValueTypes(), isInteger); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 275 | |
| 276 | // If we found exactly one supported integer type, apply it. |
| 277 | if (IntVTs.size() == 1) |
| 278 | return NodeToApply->UpdateNodeType(IntVTs[0], TP); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 279 | return NodeToApply->UpdateNodeType(MVT::iAny, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 280 | } |
| 281 | case SDTCisFP: { |
| 282 | // If there is only one FP type supported, this must be it. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 283 | std::vector<MVT::SimpleValueType> FPVTs = |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 284 | FilterVTs(CGT.getLegalValueTypes(), isFloatingPoint); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 285 | |
| 286 | // If we found exactly one supported FP type, apply it. |
| 287 | if (FPVTs.size() == 1) |
| 288 | return NodeToApply->UpdateNodeType(FPVTs[0], TP); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 289 | return NodeToApply->UpdateNodeType(MVT::fAny, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 290 | } |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 291 | case SDTCisVec: { |
| 292 | // If there is only one vector type supported, this must be it. |
| 293 | std::vector<MVT::SimpleValueType> VecVTs = |
| 294 | FilterVTs(CGT.getLegalValueTypes(), isVector); |
| 295 | |
| 296 | // If we found exactly one supported vector type, apply it. |
| 297 | if (VecVTs.size() == 1) |
| 298 | return NodeToApply->UpdateNodeType(VecVTs[0], TP); |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 299 | return NodeToApply->UpdateNodeType(MVT::vAny, TP); |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 301 | case SDTCisSameAs: { |
| 302 | TreePatternNode *OtherNode = |
| 303 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
| 304 | return NodeToApply->UpdateNodeType(OtherNode->getExtTypes(), TP) | |
| 305 | OtherNode->UpdateNodeType(NodeToApply->getExtTypes(), TP); |
| 306 | } |
| 307 | case SDTCisVTSmallerThanOp: { |
| 308 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 309 | // have an integer type that is smaller than the VT. |
| 310 | if (!NodeToApply->isLeaf() || |
| 311 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 312 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 313 | ->isSubClassOf("ValueType")) |
| 314 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 315 | MVT::SimpleValueType VT = |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 316 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 317 | if (!isInteger(VT)) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 318 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 319 | |
| 320 | TreePatternNode *OtherNode = |
| 321 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
| 322 | |
| 323 | // It must be integer. |
Bill Wendling | 7529ece | 2009-12-25 13:35:40 +0000 | [diff] [blame] | 324 | bool MadeChange = OtherNode->UpdateNodeType(MVT::iAny, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 325 | |
| 326 | // This code only handles nodes that have one type set. Assert here so |
| 327 | // that we can change this if we ever need to deal with multiple value |
| 328 | // types at this point. |
| 329 | assert(OtherNode->getExtTypes().size() == 1 && "Node has too many types!"); |
| 330 | if (OtherNode->hasTypeSet() && OtherNode->getTypeNum(0) <= VT) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 331 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
Bill Wendling | 7529ece | 2009-12-25 13:35:40 +0000 | [diff] [blame] | 332 | return MadeChange; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 333 | } |
| 334 | case SDTCisOpSmallerThanOp: { |
| 335 | TreePatternNode *BigOperand = |
| 336 | getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NumResults); |
| 337 | |
| 338 | // Both operands must be integer or FP, but we don't care which. |
| 339 | bool MadeChange = false; |
| 340 | |
| 341 | // This code does not currently handle nodes which have multiple types, |
| 342 | // where some types are integer, and some are fp. Assert that this is not |
| 343 | // the case. |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 344 | assert(!(EEVT::isExtIntegerInVTs(NodeToApply->getExtTypes()) && |
| 345 | EEVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) && |
| 346 | !(EEVT::isExtIntegerInVTs(BigOperand->getExtTypes()) && |
| 347 | EEVT::isExtFloatingPointInVTs(BigOperand->getExtTypes())) && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 348 | "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 349 | if (EEVT::isExtIntegerInVTs(NodeToApply->getExtTypes())) |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 350 | MadeChange |= BigOperand->UpdateNodeType(MVT::iAny, TP); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 351 | else if (EEVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 352 | MadeChange |= BigOperand->UpdateNodeType(MVT::fAny, TP); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 353 | if (EEVT::isExtIntegerInVTs(BigOperand->getExtTypes())) |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 354 | MadeChange |= NodeToApply->UpdateNodeType(MVT::iAny, TP); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 355 | else if (EEVT::isExtFloatingPointInVTs(BigOperand->getExtTypes())) |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 356 | MadeChange |= NodeToApply->UpdateNodeType(MVT::fAny, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 357 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 358 | std::vector<MVT::SimpleValueType> VTs = CGT.getLegalValueTypes(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 359 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 360 | if (EEVT::isExtIntegerInVTs(NodeToApply->getExtTypes())) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 361 | VTs = FilterVTs(VTs, isInteger); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 362 | } else if (EEVT::isExtFloatingPointInVTs(NodeToApply->getExtTypes())) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 363 | VTs = FilterVTs(VTs, isFloatingPoint); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 364 | } else { |
| 365 | VTs.clear(); |
| 366 | } |
| 367 | |
| 368 | switch (VTs.size()) { |
| 369 | default: // Too many VT's to pick from. |
| 370 | case 0: break; // No info yet. |
| 371 | case 1: |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 372 | // Only one VT of this flavor. Cannot ever satisfy the constraints. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 373 | return NodeToApply->UpdateNodeType(MVT::Other, TP); // throw |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 374 | case 2: |
| 375 | // If we have exactly two possible types, the little operand must be the |
| 376 | // small one, the big operand should be the big one. Common with |
| 377 | // float/double for example. |
| 378 | assert(VTs[0] < VTs[1] && "Should be sorted!"); |
| 379 | MadeChange |= NodeToApply->UpdateNodeType(VTs[0], TP); |
| 380 | MadeChange |= BigOperand->UpdateNodeType(VTs[1], TP); |
| 381 | break; |
| 382 | } |
| 383 | return MadeChange; |
| 384 | } |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 385 | case SDTCisEltOfVec: { |
| 386 | TreePatternNode *OtherOperand = |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 387 | getOperandNum(x.SDTCisEltOfVec_Info.OtherOperandNum, |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 388 | N, NumResults); |
| 389 | if (OtherOperand->hasTypeSet()) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 390 | if (!isVector(OtherOperand->getTypeNum(0))) |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 391 | TP.error(N->getOperator()->getName() + " VT operand must be a vector!"); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 392 | EVT IVT = OtherOperand->getTypeNum(0); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 393 | IVT = IVT.getVectorElementType(); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 394 | return NodeToApply->UpdateNodeType(IVT.getSimpleVT().SimpleTy, TP); |
Nate Begeman | b5af334 | 2008-02-09 01:37:05 +0000 | [diff] [blame] | 395 | } |
| 396 | return false; |
| 397 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 398 | } |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | //===----------------------------------------------------------------------===// |
| 403 | // SDNodeInfo implementation |
| 404 | // |
| 405 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 406 | EnumName = R->getValueAsString("Opcode"); |
| 407 | SDClassName = R->getValueAsString("SDClass"); |
| 408 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 409 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 410 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 411 | |
| 412 | // Parse the properties. |
| 413 | Properties = 0; |
| 414 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
| 415 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) { |
| 416 | if (PropList[i]->getName() == "SDNPCommutative") { |
| 417 | Properties |= 1 << SDNPCommutative; |
| 418 | } else if (PropList[i]->getName() == "SDNPAssociative") { |
| 419 | Properties |= 1 << SDNPAssociative; |
| 420 | } else if (PropList[i]->getName() == "SDNPHasChain") { |
| 421 | Properties |= 1 << SDNPHasChain; |
| 422 | } else if (PropList[i]->getName() == "SDNPOutFlag") { |
Dale Johannesen | 874ae25 | 2009-06-02 03:12:52 +0000 | [diff] [blame] | 423 | Properties |= 1 << SDNPOutFlag; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 424 | } else if (PropList[i]->getName() == "SDNPInFlag") { |
| 425 | Properties |= 1 << SDNPInFlag; |
| 426 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 427 | Properties |= 1 << SDNPOptInFlag; |
Chris Lattner | c8478d8 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 428 | } else if (PropList[i]->getName() == "SDNPMayStore") { |
| 429 | Properties |= 1 << SDNPMayStore; |
Chris Lattner | 710e995 | 2008-01-10 04:38:57 +0000 | [diff] [blame] | 430 | } else if (PropList[i]->getName() == "SDNPMayLoad") { |
| 431 | Properties |= 1 << SDNPMayLoad; |
Chris Lattner | bc0b9f7 | 2008-01-10 05:39:30 +0000 | [diff] [blame] | 432 | } else if (PropList[i]->getName() == "SDNPSideEffect") { |
| 433 | Properties |= 1 << SDNPSideEffect; |
Mon P Wang | 2887310 | 2008-06-25 08:15:39 +0000 | [diff] [blame] | 434 | } else if (PropList[i]->getName() == "SDNPMemOperand") { |
| 435 | Properties |= 1 << SDNPMemOperand; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 436 | } else { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 437 | errs() << "Unknown SD Node property '" << PropList[i]->getName() |
| 438 | << "' on node '" << R->getName() << "'!\n"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 439 | exit(1); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | |
| 444 | // Parse the type constraints. |
| 445 | std::vector<Record*> ConstraintList = |
| 446 | TypeProfile->getValueAsListOfDefs("Constraints"); |
| 447 | TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); |
| 448 | } |
| 449 | |
| 450 | //===----------------------------------------------------------------------===// |
| 451 | // TreePatternNode implementation |
| 452 | // |
| 453 | |
| 454 | TreePatternNode::~TreePatternNode() { |
| 455 | #if 0 // FIXME: implement refcounted tree nodes! |
| 456 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 457 | delete getChild(i); |
| 458 | #endif |
| 459 | } |
| 460 | |
| 461 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 462 | /// information. If N already contains a conflicting type, then throw an |
| 463 | /// exception. This returns true if any information was updated. |
| 464 | /// |
| 465 | bool TreePatternNode::UpdateNodeType(const std::vector<unsigned char> &ExtVTs, |
| 466 | TreePattern &TP) { |
| 467 | assert(!ExtVTs.empty() && "Cannot update node type with empty type vector!"); |
| 468 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 469 | if (ExtVTs[0] == EEVT::isUnknown || LHSIsSubsetOfRHS(getExtTypes(), ExtVTs)) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 470 | return false; |
| 471 | if (isTypeCompletelyUnknown() || LHSIsSubsetOfRHS(ExtVTs, getExtTypes())) { |
| 472 | setTypes(ExtVTs); |
| 473 | return true; |
| 474 | } |
| 475 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 476 | if (getExtTypeNum(0) == MVT::iPTR || getExtTypeNum(0) == MVT::iPTRAny) { |
| 477 | if (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny || |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 478 | ExtVTs[0] == MVT::iAny) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 479 | return false; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 480 | if (EEVT::isExtIntegerInVTs(ExtVTs)) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 481 | std::vector<unsigned char> FVTs = FilterEVTs(ExtVTs, isInteger); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 482 | if (FVTs.size()) { |
| 483 | setTypes(ExtVTs); |
| 484 | return true; |
| 485 | } |
| 486 | } |
| 487 | } |
Bob Wilson | e035fa5 | 2009-01-05 17:52:54 +0000 | [diff] [blame] | 488 | |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 489 | // Merge vAny with iAny/fAny. The latter include vector types so keep them |
| 490 | // as the more specific information. |
| 491 | if (ExtVTs[0] == MVT::vAny && |
| 492 | (getExtTypeNum(0) == MVT::iAny || getExtTypeNum(0) == MVT::fAny)) |
| 493 | return false; |
| 494 | if (getExtTypeNum(0) == MVT::vAny && |
| 495 | (ExtVTs[0] == MVT::iAny || ExtVTs[0] == MVT::fAny)) { |
| 496 | setTypes(ExtVTs); |
| 497 | return true; |
| 498 | } |
| 499 | |
| 500 | if (ExtVTs[0] == MVT::iAny && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 501 | EEVT::isExtIntegerInVTs(getExtTypes())) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 502 | assert(hasTypeSet() && "should be handled above!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 503 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), isInteger); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 504 | if (getExtTypes() == FVTs) |
| 505 | return false; |
| 506 | setTypes(FVTs); |
| 507 | return true; |
| 508 | } |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 509 | if ((ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny) && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 510 | EEVT::isExtIntegerInVTs(getExtTypes())) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 511 | //assert(hasTypeSet() && "should be handled above!"); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 512 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), isInteger); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 513 | if (getExtTypes() == FVTs) |
| 514 | return false; |
| 515 | if (FVTs.size()) { |
| 516 | setTypes(FVTs); |
| 517 | return true; |
| 518 | } |
| 519 | } |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 520 | if (ExtVTs[0] == MVT::fAny && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 521 | EEVT::isExtFloatingPointInVTs(getExtTypes())) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 522 | assert(hasTypeSet() && "should be handled above!"); |
| 523 | std::vector<unsigned char> FVTs = |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 524 | FilterEVTs(getExtTypes(), isFloatingPoint); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 525 | if (getExtTypes() == FVTs) |
| 526 | return false; |
| 527 | setTypes(FVTs); |
| 528 | return true; |
| 529 | } |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 530 | if (ExtVTs[0] == MVT::vAny && |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 531 | EEVT::isExtVectorInVTs(getExtTypes())) { |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 532 | assert(hasTypeSet() && "should be handled above!"); |
| 533 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), isVector); |
| 534 | if (getExtTypes() == FVTs) |
| 535 | return false; |
| 536 | setTypes(FVTs); |
| 537 | return true; |
| 538 | } |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 539 | |
Bob Wilson | 36e3e66 | 2009-08-12 22:30:59 +0000 | [diff] [blame] | 540 | // If we know this is an int, FP, or vector type, and we are told it is a |
| 541 | // specific one, take the advice. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 542 | // |
| 543 | // Similarly, we should probably set the type here to the intersection of |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 544 | // {iAny|fAny|vAny} and ExtVTs |
| 545 | if ((getExtTypeNum(0) == MVT::iAny && |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 546 | EEVT::isExtIntegerInVTs(ExtVTs)) || |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 547 | (getExtTypeNum(0) == MVT::fAny && |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 548 | EEVT::isExtFloatingPointInVTs(ExtVTs)) || |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 549 | (getExtTypeNum(0) == MVT::vAny && |
Bob Wilson | 61fc4cf | 2009-08-11 01:14:02 +0000 | [diff] [blame] | 550 | EEVT::isExtVectorInVTs(ExtVTs))) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 551 | setTypes(ExtVTs); |
| 552 | return true; |
| 553 | } |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 554 | if (getExtTypeNum(0) == MVT::iAny && |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 555 | (ExtVTs[0] == MVT::iPTR || ExtVTs[0] == MVT::iPTRAny)) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 556 | setTypes(ExtVTs); |
| 557 | return true; |
| 558 | } |
| 559 | |
| 560 | if (isLeaf()) { |
| 561 | dump(); |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 562 | errs() << " "; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 563 | TP.error("Type inference contradiction found in node!"); |
| 564 | } else { |
| 565 | TP.error("Type inference contradiction found in node " + |
| 566 | getOperator()->getName() + "!"); |
| 567 | } |
| 568 | return true; // unreachable |
| 569 | } |
| 570 | |
Chris Lattner | ba1cff4 | 2010-02-23 07:50:58 +0000 | [diff] [blame] | 571 | static std::string GetTypeName(unsigned char TypeID) { |
| 572 | switch (TypeID) { |
| 573 | case MVT::Other: return "Other"; |
| 574 | case MVT::iAny: return "iAny"; |
| 575 | case MVT::fAny: return "fAny"; |
| 576 | case MVT::vAny: return "vAny"; |
| 577 | case EEVT::isUnknown: return "isUnknown"; |
| 578 | case MVT::iPTR: return "iPTR"; |
| 579 | case MVT::iPTRAny: return "iPTRAny"; |
| 580 | default: |
| 581 | std::string VTName = llvm::getName((MVT::SimpleValueType)TypeID); |
| 582 | // Strip off EVT:: prefix if present. |
| 583 | if (VTName.substr(0,5) == "MVT::") |
| 584 | VTName = VTName.substr(5); |
| 585 | return VTName; |
| 586 | } |
| 587 | } |
| 588 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 589 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 590 | void TreePatternNode::print(raw_ostream &OS) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 591 | if (isLeaf()) { |
| 592 | OS << *getLeafValue(); |
| 593 | } else { |
Chris Lattner | ba1cff4 | 2010-02-23 07:50:58 +0000 | [diff] [blame] | 594 | OS << '(' << getOperator()->getName(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | // FIXME: At some point we should handle printing all the value types for |
| 598 | // nodes that are multiply typed. |
Chris Lattner | ba1cff4 | 2010-02-23 07:50:58 +0000 | [diff] [blame] | 599 | if (getExtTypeNum(0) != EEVT::isUnknown) |
| 600 | OS << ':' << GetTypeName(getExtTypeNum(0)); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 601 | |
| 602 | if (!isLeaf()) { |
| 603 | if (getNumChildren() != 0) { |
| 604 | OS << " "; |
| 605 | getChild(0)->print(OS); |
| 606 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 607 | OS << ", "; |
| 608 | getChild(i)->print(OS); |
| 609 | } |
| 610 | } |
| 611 | OS << ")"; |
| 612 | } |
| 613 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 614 | for (unsigned i = 0, e = PredicateFns.size(); i != e; ++i) |
| 615 | OS << "<<P:" << PredicateFns[i] << ">>"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 616 | if (TransformFn) |
| 617 | OS << "<<X:" << TransformFn->getName() << ">>"; |
| 618 | if (!getName().empty()) |
| 619 | OS << ":$" << getName(); |
| 620 | |
| 621 | } |
| 622 | void TreePatternNode::dump() const { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 623 | print(errs()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 626 | /// isIsomorphicTo - Return true if this node is recursively |
| 627 | /// isomorphic to the specified node. For this comparison, the node's |
| 628 | /// entire state is considered. The assigned name is ignored, since |
| 629 | /// nodes with differing names are considered isomorphic. However, if |
| 630 | /// the assigned name is present in the dependent variable set, then |
| 631 | /// the assigned name is considered significant and the node is |
| 632 | /// isomorphic if the names match. |
| 633 | bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N, |
| 634 | const MultipleUseVarSet &DepVars) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 635 | if (N == this) return true; |
| 636 | if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 637 | getPredicateFns() != N->getPredicateFns() || |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 638 | getTransformFn() != N->getTransformFn()) |
| 639 | return false; |
| 640 | |
| 641 | if (isLeaf()) { |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 642 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) { |
| 643 | if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
Chris Lattner | 71a2cb2 | 2008-03-20 01:22:40 +0000 | [diff] [blame] | 644 | return ((DI->getDef() == NDI->getDef()) |
| 645 | && (DepVars.find(getName()) == DepVars.end() |
| 646 | || getName() == N->getName())); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 649 | return getLeafValue() == N->getLeafValue(); |
| 650 | } |
| 651 | |
| 652 | if (N->getOperator() != getOperator() || |
| 653 | N->getNumChildren() != getNumChildren()) return false; |
| 654 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 655 | if (!getChild(i)->isIsomorphicTo(N->getChild(i), DepVars)) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 656 | return false; |
| 657 | return true; |
| 658 | } |
| 659 | |
| 660 | /// clone - Make a copy of this tree and all of its children. |
| 661 | /// |
| 662 | TreePatternNode *TreePatternNode::clone() const { |
| 663 | TreePatternNode *New; |
| 664 | if (isLeaf()) { |
| 665 | New = new TreePatternNode(getLeafValue()); |
| 666 | } else { |
| 667 | std::vector<TreePatternNode*> CChildren; |
| 668 | CChildren.reserve(Children.size()); |
| 669 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 670 | CChildren.push_back(getChild(i)->clone()); |
| 671 | New = new TreePatternNode(getOperator(), CChildren); |
| 672 | } |
| 673 | New->setName(getName()); |
| 674 | New->setTypes(getExtTypes()); |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 675 | New->setPredicateFns(getPredicateFns()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 676 | New->setTransformFn(getTransformFn()); |
| 677 | return New; |
| 678 | } |
| 679 | |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 680 | /// RemoveAllTypes - Recursively strip all the types of this tree. |
| 681 | void TreePatternNode::RemoveAllTypes() { |
| 682 | removeTypes(); |
| 683 | if (isLeaf()) return; |
| 684 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 685 | getChild(i)->RemoveAllTypes(); |
| 686 | } |
| 687 | |
| 688 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 689 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 690 | /// with actual values specified by ArgMap. |
| 691 | void TreePatternNode:: |
| 692 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 693 | if (isLeaf()) return; |
| 694 | |
| 695 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 696 | TreePatternNode *Child = getChild(i); |
| 697 | if (Child->isLeaf()) { |
| 698 | Init *Val = Child->getLeafValue(); |
| 699 | if (dynamic_cast<DefInit*>(Val) && |
| 700 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 701 | // We found a use of a formal argument, replace it with its value. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 702 | TreePatternNode *NewChild = ArgMap[Child->getName()]; |
| 703 | assert(NewChild && "Couldn't find formal argument!"); |
| 704 | assert((Child->getPredicateFns().empty() || |
| 705 | NewChild->getPredicateFns() == Child->getPredicateFns()) && |
| 706 | "Non-empty child predicate clobbered!"); |
| 707 | setChild(i, NewChild); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 708 | } |
| 709 | } else { |
| 710 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | |
| 716 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 717 | /// fragments, inline them into place, giving us a pattern without any |
| 718 | /// PatFrag references. |
| 719 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 720 | if (isLeaf()) return this; // nothing to do. |
| 721 | Record *Op = getOperator(); |
| 722 | |
| 723 | if (!Op->isSubClassOf("PatFrag")) { |
| 724 | // Just recursively inline children nodes. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 725 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 726 | TreePatternNode *Child = getChild(i); |
| 727 | TreePatternNode *NewChild = Child->InlinePatternFragments(TP); |
| 728 | |
| 729 | assert((Child->getPredicateFns().empty() || |
| 730 | NewChild->getPredicateFns() == Child->getPredicateFns()) && |
| 731 | "Non-empty child predicate clobbered!"); |
| 732 | |
| 733 | setChild(i, NewChild); |
| 734 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 735 | return this; |
| 736 | } |
| 737 | |
| 738 | // Otherwise, we found a reference to a fragment. First, look up its |
| 739 | // TreePattern record. |
| 740 | TreePattern *Frag = TP.getDAGPatterns().getPatternFragment(Op); |
| 741 | |
| 742 | // Verify that we are passing the right number of operands. |
| 743 | if (Frag->getNumArgs() != Children.size()) |
| 744 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 745 | utostr(Frag->getNumArgs()) + " operands!"); |
| 746 | |
| 747 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
| 748 | |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 749 | std::string Code = Op->getValueAsCode("Predicate"); |
| 750 | if (!Code.empty()) |
| 751 | FragTree->addPredicateFn("Predicate_"+Op->getName()); |
| 752 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 753 | // Resolve formal arguments to their actual value. |
| 754 | if (Frag->getNumArgs()) { |
| 755 | // Compute the map of formal to actual arguments. |
| 756 | std::map<std::string, TreePatternNode*> ArgMap; |
| 757 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 758 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 759 | |
| 760 | FragTree->SubstituteFormalArguments(ArgMap); |
| 761 | } |
| 762 | |
| 763 | FragTree->setName(getName()); |
| 764 | FragTree->UpdateNodeType(getExtTypes(), TP); |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 765 | |
| 766 | // Transfer in the old predicates. |
| 767 | for (unsigned i = 0, e = getPredicateFns().size(); i != e; ++i) |
| 768 | FragTree->addPredicateFn(getPredicateFns()[i]); |
| 769 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 770 | // Get a new copy of this fragment to stitch into here. |
| 771 | //delete this; // FIXME: implement refcounting! |
Chris Lattner | 2ca698d | 2008-06-30 03:02:03 +0000 | [diff] [blame] | 772 | |
| 773 | // The fragment we inlined could have recursive inlining that is needed. See |
| 774 | // if there are any pattern fragments in it and inline them as needed. |
| 775 | return FragTree->InlinePatternFragments(TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /// getImplicitType - Check to see if the specified record has an implicit |
Nick Lewycky | fc4c255 | 2009-06-17 04:23:52 +0000 | [diff] [blame] | 779 | /// type which should be applied to it. This will infer the type of register |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 780 | /// references from the register file information, for example. |
| 781 | /// |
| 782 | static std::vector<unsigned char> getImplicitType(Record *R, bool NotRegisters, |
Chris Lattner | 523f6a5 | 2010-02-14 21:10:15 +0000 | [diff] [blame] | 783 | TreePattern &TP) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 784 | // Some common return values |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 785 | std::vector<unsigned char> Unknown(1, EEVT::isUnknown); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 786 | std::vector<unsigned char> Other(1, MVT::Other); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 787 | |
| 788 | // Check to see if this is a register or a register class... |
| 789 | if (R->isSubClassOf("RegisterClass")) { |
| 790 | if (NotRegisters) |
| 791 | return Unknown; |
| 792 | const CodeGenRegisterClass &RC = |
| 793 | TP.getDAGPatterns().getTargetInfo().getRegisterClass(R); |
| 794 | return ConvertVTs(RC.getValueTypes()); |
| 795 | } else if (R->isSubClassOf("PatFrag")) { |
| 796 | // Pattern fragment types will be resolved when they are inlined. |
| 797 | return Unknown; |
| 798 | } else if (R->isSubClassOf("Register")) { |
| 799 | if (NotRegisters) |
| 800 | return Unknown; |
| 801 | const CodeGenTarget &T = TP.getDAGPatterns().getTargetInfo(); |
| 802 | return T.getRegisterVTs(R); |
| 803 | } else if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) { |
| 804 | // Using a VTSDNode or CondCodeSDNode. |
| 805 | return Other; |
| 806 | } else if (R->isSubClassOf("ComplexPattern")) { |
| 807 | if (NotRegisters) |
| 808 | return Unknown; |
| 809 | std::vector<unsigned char> |
| 810 | ComplexPat(1, TP.getDAGPatterns().getComplexPattern(R).getValueType()); |
| 811 | return ComplexPat; |
Chris Lattner | a938ac6 | 2009-07-29 20:43:05 +0000 | [diff] [blame] | 812 | } else if (R->isSubClassOf("PointerLikeRegClass")) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 813 | Other[0] = MVT::iPTR; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 814 | return Other; |
| 815 | } else if (R->getName() == "node" || R->getName() == "srcvalue" || |
| 816 | R->getName() == "zero_reg") { |
| 817 | // Placeholder. |
| 818 | return Unknown; |
| 819 | } |
| 820 | |
| 821 | TP.error("Unknown node flavor used in pattern: " + R->getName()); |
| 822 | return Other; |
| 823 | } |
| 824 | |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 825 | |
| 826 | /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the |
| 827 | /// CodeGenIntrinsic information for it, otherwise return a null pointer. |
| 828 | const CodeGenIntrinsic *TreePatternNode:: |
| 829 | getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const { |
| 830 | if (getOperator() != CDP.get_intrinsic_void_sdnode() && |
| 831 | getOperator() != CDP.get_intrinsic_w_chain_sdnode() && |
| 832 | getOperator() != CDP.get_intrinsic_wo_chain_sdnode()) |
| 833 | return 0; |
| 834 | |
| 835 | unsigned IID = |
| 836 | dynamic_cast<IntInit*>(getChild(0)->getLeafValue())->getValue(); |
| 837 | return &CDP.getIntrinsicInfo(IID); |
| 838 | } |
| 839 | |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 840 | /// getComplexPatternInfo - If this node corresponds to a ComplexPattern, |
| 841 | /// return the ComplexPattern information, otherwise return null. |
| 842 | const ComplexPattern * |
| 843 | TreePatternNode::getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const { |
| 844 | if (!isLeaf()) return 0; |
| 845 | |
| 846 | DefInit *DI = dynamic_cast<DefInit*>(getLeafValue()); |
| 847 | if (DI && DI->getDef()->isSubClassOf("ComplexPattern")) |
| 848 | return &CGP.getComplexPattern(DI->getDef()); |
| 849 | return 0; |
| 850 | } |
| 851 | |
| 852 | /// NodeHasProperty - Return true if this node has the specified property. |
| 853 | bool TreePatternNode::NodeHasProperty(SDNP Property, |
Chris Lattner | 751d5aa | 2010-02-14 22:33:49 +0000 | [diff] [blame] | 854 | const CodeGenDAGPatterns &CGP) const { |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 855 | if (isLeaf()) { |
| 856 | if (const ComplexPattern *CP = getComplexPatternInfo(CGP)) |
| 857 | return CP->hasProperty(Property); |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | Record *Operator = getOperator(); |
| 862 | if (!Operator->isSubClassOf("SDNode")) return false; |
| 863 | |
| 864 | return CGP.getSDNodeInfo(Operator).hasProperty(Property); |
| 865 | } |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | /// TreeHasProperty - Return true if any node in this tree has the specified |
| 871 | /// property. |
| 872 | bool TreePatternNode::TreeHasProperty(SDNP Property, |
Chris Lattner | 751d5aa | 2010-02-14 22:33:49 +0000 | [diff] [blame] | 873 | const CodeGenDAGPatterns &CGP) const { |
Chris Lattner | 4766132 | 2010-02-14 22:22:58 +0000 | [diff] [blame] | 874 | if (NodeHasProperty(Property, CGP)) |
| 875 | return true; |
| 876 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 877 | if (getChild(i)->TreeHasProperty(Property, CGP)) |
| 878 | return true; |
| 879 | return false; |
| 880 | } |
| 881 | |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 882 | /// isCommutativeIntrinsic - Return true if the node corresponds to a |
| 883 | /// commutative intrinsic. |
| 884 | bool |
| 885 | TreePatternNode::isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const { |
| 886 | if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) |
| 887 | return Int->isCommutative; |
| 888 | return false; |
| 889 | } |
| 890 | |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 891 | |
Bob Wilson | 6c01ca9 | 2009-01-05 17:23:09 +0000 | [diff] [blame] | 892 | /// ApplyTypeConstraints - Apply all of the type constraints relevant to |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 893 | /// this node and its children in the tree. This returns true if it makes a |
| 894 | /// change, false otherwise. If a type contradiction is found, throw an |
| 895 | /// exception. |
| 896 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 897 | CodeGenDAGPatterns &CDP = TP.getDAGPatterns(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 898 | if (isLeaf()) { |
| 899 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) { |
| 900 | // If it's a regclass or something else known, include the type. |
| 901 | return UpdateNodeType(getImplicitType(DI->getDef(), NotRegisters, TP),TP); |
Chris Lattner | 523f6a5 | 2010-02-14 21:10:15 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 905 | // Int inits are always integers. :) |
Bob Wilson | cdfa01b | 2009-08-29 05:53:25 +0000 | [diff] [blame] | 906 | bool MadeChange = UpdateNodeType(MVT::iAny, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 907 | |
| 908 | if (hasTypeSet()) { |
| 909 | // At some point, it may make sense for this tree pattern to have |
| 910 | // multiple types. Assert here that it does not, so we revisit this |
| 911 | // code when appropriate. |
| 912 | assert(getExtTypes().size() >= 1 && "TreePattern doesn't have a type!"); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 913 | MVT::SimpleValueType VT = getTypeNum(0); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 914 | for (unsigned i = 1, e = getExtTypes().size(); i != e; ++i) |
| 915 | assert(getTypeNum(i) == VT && "TreePattern has too many types!"); |
| 916 | |
| 917 | VT = getTypeNum(0); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 918 | if (VT != MVT::iPTR && VT != MVT::iPTRAny) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 919 | unsigned Size = EVT(VT).getSizeInBits(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 920 | // Make sure that the value is representable for this type. |
| 921 | if (Size < 32) { |
| 922 | int Val = (II->getValue() << (32-Size)) >> (32-Size); |
Scott Michel | 0123b7d | 2008-02-15 23:05:48 +0000 | [diff] [blame] | 923 | if (Val != II->getValue()) { |
Bill Wendling | 27926af | 2008-02-26 10:45:29 +0000 | [diff] [blame] | 924 | // If sign-extended doesn't fit, does it fit as unsigned? |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 925 | unsigned ValueMask; |
| 926 | unsigned UnsignedVal; |
Duncan Sands | b0d5cdd | 2009-02-01 18:06:53 +0000 | [diff] [blame] | 927 | ValueMask = unsigned(~uint32_t(0UL) >> (32-Size)); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 928 | UnsignedVal = unsigned(II->getValue()); |
Scott Michel | 0123b7d | 2008-02-15 23:05:48 +0000 | [diff] [blame] | 929 | |
Bill Wendling | 27926af | 2008-02-26 10:45:29 +0000 | [diff] [blame] | 930 | if ((ValueMask & UnsignedVal) != UnsignedVal) { |
| 931 | TP.error("Integer value '" + itostr(II->getValue())+ |
| 932 | "' is out of range for type '" + |
| 933 | getEnumName(getTypeNum(0)) + "'!"); |
| 934 | } |
| 935 | } |
Nick Lewycky | fc4c255 | 2009-06-17 04:23:52 +0000 | [diff] [blame] | 936 | } |
| 937 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | return MadeChange; |
| 941 | } |
| 942 | return false; |
| 943 | } |
| 944 | |
| 945 | // special handling for set, which isn't really an SDNode. |
| 946 | if (getOperator()->getName() == "set") { |
| 947 | assert (getNumChildren() >= 2 && "Missing RHS of a set?"); |
| 948 | unsigned NC = getNumChildren(); |
| 949 | bool MadeChange = false; |
| 950 | for (unsigned i = 0; i < NC-1; ++i) { |
| 951 | MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 952 | MadeChange |= getChild(NC-1)->ApplyTypeConstraints(TP, NotRegisters); |
| 953 | |
| 954 | // Types of operands must match. |
| 955 | MadeChange |= getChild(i)->UpdateNodeType(getChild(NC-1)->getExtTypes(), |
| 956 | TP); |
| 957 | MadeChange |= getChild(NC-1)->UpdateNodeType(getChild(i)->getExtTypes(), |
| 958 | TP); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 959 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 960 | } |
| 961 | return MadeChange; |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | if (getOperator()->getName() == "implicit" || |
| 965 | getOperator()->getName() == "parallel") { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 966 | bool MadeChange = false; |
| 967 | for (unsigned i = 0; i < getNumChildren(); ++i) |
| 968 | MadeChange = getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 969 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 970 | return MadeChange; |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (getOperator()->getName() == "COPY_TO_REGCLASS") { |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 974 | bool MadeChange = false; |
| 975 | MadeChange |= getChild(0)->ApplyTypeConstraints(TP, NotRegisters); |
| 976 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters); |
Dan Gohman | f8c7394 | 2009-04-13 15:38:05 +0000 | [diff] [blame] | 977 | return MadeChange; |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | if (const CodeGenIntrinsic *Int = getIntrinsicInfo(CDP)) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 981 | bool MadeChange = false; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 982 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 983 | // Apply the result type to the node. |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 984 | unsigned NumRetVTs = Int->IS.RetVTs.size(); |
| 985 | unsigned NumParamVTs = Int->IS.ParamVTs.size(); |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 986 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 987 | for (unsigned i = 0, e = NumRetVTs; i != e; ++i) |
| 988 | MadeChange |= UpdateNodeType(Int->IS.RetVTs[i], TP); |
| 989 | |
| 990 | if (getNumChildren() != NumParamVTs + NumRetVTs) |
Chris Lattner | e67bde5 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 991 | TP.error("Intrinsic '" + Int->Name + "' expects " + |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 992 | utostr(NumParamVTs + NumRetVTs - 1) + " operands, not " + |
| 993 | utostr(getNumChildren() - 1) + " operands!"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 994 | |
| 995 | // Apply type info to the intrinsic ID. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 996 | MadeChange |= getChild(0)->UpdateNodeType(MVT::iPTR, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 997 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 998 | for (unsigned i = NumRetVTs, e = getNumChildren(); i != e; ++i) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 999 | MVT::SimpleValueType OpVT = Int->IS.ParamVTs[i - NumRetVTs]; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1000 | MadeChange |= getChild(i)->UpdateNodeType(OpVT, TP); |
| 1001 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 1002 | } |
| 1003 | return MadeChange; |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | if (getOperator()->isSubClassOf("SDNode")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1007 | const SDNodeInfo &NI = CDP.getSDNodeInfo(getOperator()); |
| 1008 | |
| 1009 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 1010 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 1011 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
| 1012 | // Branch, etc. do not produce results and top-level forms in instr pattern |
| 1013 | // must have void types. |
| 1014 | if (NI.getNumResults() == 0) |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1015 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1017 | return MadeChange; |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if (getOperator()->isSubClassOf("Instruction")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1021 | const DAGInstruction &Inst = CDP.getInstruction(getOperator()); |
| 1022 | bool MadeChange = false; |
| 1023 | unsigned NumResults = Inst.getNumResults(); |
| 1024 | |
| 1025 | assert(NumResults <= 1 && |
| 1026 | "Only supports zero or one result instrs!"); |
| 1027 | |
| 1028 | CodeGenInstruction &InstInfo = |
| 1029 | CDP.getTargetInfo().getInstruction(getOperator()->getName()); |
| 1030 | // Apply the result type to the node |
| 1031 | if (NumResults == 0 || InstInfo.NumDefs == 0) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1032 | MadeChange = UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | Record *ResultNode = Inst.getResult(0); |
| 1035 | |
Chris Lattner | a938ac6 | 2009-07-29 20:43:05 +0000 | [diff] [blame] | 1036 | if (ResultNode->isSubClassOf("PointerLikeRegClass")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1037 | std::vector<unsigned char> VT; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1038 | VT.push_back(MVT::iPTR); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1039 | MadeChange = UpdateNodeType(VT, TP); |
Christopher Lamb | 5b41537 | 2008-03-11 09:33:47 +0000 | [diff] [blame] | 1040 | } else if (ResultNode->getName() == "unknown") { |
| 1041 | std::vector<unsigned char> VT; |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1042 | VT.push_back(EEVT::isUnknown); |
Christopher Lamb | 5b41537 | 2008-03-11 09:33:47 +0000 | [diff] [blame] | 1043 | MadeChange = UpdateNodeType(VT, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | assert(ResultNode->isSubClassOf("RegisterClass") && |
| 1046 | "Operands should be register classes!"); |
| 1047 | |
| 1048 | const CodeGenRegisterClass &RC = |
| 1049 | CDP.getTargetInfo().getRegisterClass(ResultNode); |
| 1050 | MadeChange = UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | unsigned ChildNo = 0; |
| 1055 | for (unsigned i = 0, e = Inst.getNumOperands(); i != e; ++i) { |
| 1056 | Record *OperandNode = Inst.getOperand(i); |
| 1057 | |
| 1058 | // If the instruction expects a predicate or optional def operand, we |
| 1059 | // codegen this by setting the operand to it's default value if it has a |
| 1060 | // non-empty DefaultOps field. |
| 1061 | if ((OperandNode->isSubClassOf("PredicateOperand") || |
| 1062 | OperandNode->isSubClassOf("OptionalDefOperand")) && |
| 1063 | !CDP.getDefaultOperand(OperandNode).DefaultOps.empty()) |
| 1064 | continue; |
| 1065 | |
| 1066 | // Verify that we didn't run out of provided operands. |
| 1067 | if (ChildNo >= getNumChildren()) |
| 1068 | TP.error("Instruction '" + getOperator()->getName() + |
| 1069 | "' expects more operands than were provided."); |
| 1070 | |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1071 | MVT::SimpleValueType VT; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1072 | TreePatternNode *Child = getChild(ChildNo++); |
| 1073 | if (OperandNode->isSubClassOf("RegisterClass")) { |
| 1074 | const CodeGenRegisterClass &RC = |
| 1075 | CDP.getTargetInfo().getRegisterClass(OperandNode); |
| 1076 | MadeChange |= Child->UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
| 1077 | } else if (OperandNode->isSubClassOf("Operand")) { |
| 1078 | VT = getValueType(OperandNode->getValueAsDef("Type")); |
| 1079 | MadeChange |= Child->UpdateNodeType(VT, TP); |
Chris Lattner | a938ac6 | 2009-07-29 20:43:05 +0000 | [diff] [blame] | 1080 | } else if (OperandNode->isSubClassOf("PointerLikeRegClass")) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1081 | MadeChange |= Child->UpdateNodeType(MVT::iPTR, TP); |
Christopher Lamb | 5b41537 | 2008-03-11 09:33:47 +0000 | [diff] [blame] | 1082 | } else if (OperandNode->getName() == "unknown") { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 1083 | MadeChange |= Child->UpdateNodeType(EEVT::isUnknown, TP); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1084 | } else { |
| 1085 | assert(0 && "Unknown operand type!"); |
| 1086 | abort(); |
| 1087 | } |
| 1088 | MadeChange |= Child->ApplyTypeConstraints(TP, NotRegisters); |
| 1089 | } |
Christopher Lamb | 5b41537 | 2008-03-11 09:33:47 +0000 | [diff] [blame] | 1090 | |
Christopher Lamb | 02f6937 | 2008-03-10 04:16:09 +0000 | [diff] [blame] | 1091 | if (ChildNo != getNumChildren()) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1092 | TP.error("Instruction '" + getOperator()->getName() + |
| 1093 | "' was provided too many operands!"); |
| 1094 | |
| 1095 | return MadeChange; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1096 | } |
Chris Lattner | 6eb3012 | 2010-02-23 05:51:07 +0000 | [diff] [blame] | 1097 | |
| 1098 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); |
| 1099 | |
| 1100 | // Node transforms always take one operand. |
| 1101 | if (getNumChildren() != 1) |
| 1102 | TP.error("Node transform '" + getOperator()->getName() + |
| 1103 | "' requires one operand!"); |
| 1104 | |
| 1105 | // If either the output or input of the xform does not have exact |
| 1106 | // type info. We assume they must be the same. Otherwise, it is perfectly |
| 1107 | // legal to transform from one type to a completely different type. |
| 1108 | if (!hasTypeSet() || !getChild(0)->hasTypeSet()) { |
| 1109 | bool MadeChange = UpdateNodeType(getChild(0)->getExtTypes(), TP); |
| 1110 | MadeChange |= getChild(0)->UpdateNodeType(getExtTypes(), TP); |
| 1111 | return MadeChange; |
| 1112 | } |
| 1113 | return false; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /// OnlyOnRHSOfCommutative - Return true if this value is only allowed on the |
| 1117 | /// RHS of a commutative operation, not the on LHS. |
| 1118 | static bool OnlyOnRHSOfCommutative(TreePatternNode *N) { |
| 1119 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") |
| 1120 | return true; |
| 1121 | if (N->isLeaf() && dynamic_cast<IntInit*>(N->getLeafValue())) |
| 1122 | return true; |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
| 1126 | |
| 1127 | /// canPatternMatch - If it is impossible for this pattern to match on this |
| 1128 | /// target, fill in Reason and return false. Otherwise, return true. This is |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1129 | /// used as a sanity check for .td files (to prevent people from writing stuff |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1130 | /// that can never possibly work), and to prevent the pattern permuter from |
| 1131 | /// generating stuff that is useless. |
| 1132 | bool TreePatternNode::canPatternMatch(std::string &Reason, |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 1133 | const CodeGenDAGPatterns &CDP) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1134 | if (isLeaf()) return true; |
| 1135 | |
| 1136 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 1137 | if (!getChild(i)->canPatternMatch(Reason, CDP)) |
| 1138 | return false; |
| 1139 | |
| 1140 | // If this is an intrinsic, handle cases that would make it not match. For |
| 1141 | // example, if an operand is required to be an immediate. |
| 1142 | if (getOperator()->isSubClassOf("Intrinsic")) { |
| 1143 | // TODO: |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | // If this node is a commutative operator, check that the LHS isn't an |
| 1148 | // immediate. |
| 1149 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(getOperator()); |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 1150 | bool isCommIntrinsic = isCommutativeIntrinsic(CDP); |
| 1151 | if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1152 | // Scan all of the operands of the node and make sure that only the last one |
| 1153 | // is a constant node, unless the RHS also is. |
| 1154 | if (!OnlyOnRHSOfCommutative(getChild(getNumChildren()-1))) { |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 1155 | bool Skip = isCommIntrinsic ? 1 : 0; // First operand is intrinsic id. |
| 1156 | for (unsigned i = Skip, e = getNumChildren()-1; i != e; ++i) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1157 | if (OnlyOnRHSOfCommutative(getChild(i))) { |
| 1158 | Reason="Immediate value must be on the RHS of commutative operators!"; |
| 1159 | return false; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | //===----------------------------------------------------------------------===// |
| 1168 | // TreePattern implementation |
| 1169 | // |
| 1170 | |
| 1171 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1172 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1173 | isInputPattern = isInput; |
| 1174 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
| 1175 | Trees.push_back(ParseTreePattern((DagInit*)RawPat->getElement(i))); |
| 1176 | } |
| 1177 | |
| 1178 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1179 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1180 | isInputPattern = isInput; |
| 1181 | Trees.push_back(ParseTreePattern(Pat)); |
| 1182 | } |
| 1183 | |
| 1184 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1185 | CodeGenDAGPatterns &cdp) : TheRecord(TheRec), CDP(cdp){ |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1186 | isInputPattern = isInput; |
| 1187 | Trees.push_back(Pat); |
| 1188 | } |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | void TreePattern::error(const std::string &Msg) const { |
| 1193 | dump(); |
Chris Lattner | a14b1de | 2009-03-13 16:25:21 +0000 | [diff] [blame] | 1194 | throw TGError(TheRecord->getLoc(), "In " + TheRecord->getName() + ": " + Msg); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 1198 | DefInit *OpDef = dynamic_cast<DefInit*>(Dag->getOperator()); |
| 1199 | if (!OpDef) error("Pattern has unexpected operator type!"); |
| 1200 | Record *Operator = OpDef->getDef(); |
| 1201 | |
| 1202 | if (Operator->isSubClassOf("ValueType")) { |
| 1203 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 1204 | // node. |
| 1205 | if (Dag->getNumArgs() != 1) |
| 1206 | error("Type cast only takes one operand!"); |
| 1207 | |
| 1208 | Init *Arg = Dag->getArg(0); |
| 1209 | TreePatternNode *New; |
| 1210 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
| 1211 | Record *R = DI->getDef(); |
| 1212 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1213 | Dag->setArg(0, new DagInit(DI, "", |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1214 | std::vector<std::pair<Init*, std::string> >())); |
| 1215 | return ParseTreePattern(Dag); |
| 1216 | } |
| 1217 | New = new TreePatternNode(DI); |
| 1218 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 1219 | New = ParseTreePattern(DI); |
| 1220 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 1221 | New = new TreePatternNode(II); |
| 1222 | if (!Dag->getArgName(0).empty()) |
| 1223 | error("Constant int argument should not have a name!"); |
| 1224 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 1225 | // Turn this into an IntInit. |
| 1226 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 1227 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 1228 | error("Bits value must be constants!"); |
| 1229 | |
| 1230 | New = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 1231 | if (!Dag->getArgName(0).empty()) |
| 1232 | error("Constant int argument should not have a name!"); |
| 1233 | } else { |
| 1234 | Arg->dump(); |
| 1235 | error("Unknown leaf value for tree pattern!"); |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | // Apply the type cast. |
| 1240 | New->UpdateNodeType(getValueType(Operator), *this); |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1241 | if (New->getNumChildren() == 0) |
| 1242 | New->setName(Dag->getArgName(0)); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1243 | return New; |
| 1244 | } |
| 1245 | |
| 1246 | // Verify that this is something that makes sense for an operator. |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1247 | if (!Operator->isSubClassOf("PatFrag") && |
| 1248 | !Operator->isSubClassOf("SDNode") && |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1249 | !Operator->isSubClassOf("Instruction") && |
| 1250 | !Operator->isSubClassOf("SDNodeXForm") && |
| 1251 | !Operator->isSubClassOf("Intrinsic") && |
| 1252 | Operator->getName() != "set" && |
| 1253 | Operator->getName() != "implicit" && |
| 1254 | Operator->getName() != "parallel") |
| 1255 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 1256 | |
| 1257 | // Check to see if this is something that is illegal in an input pattern. |
| 1258 | if (isInputPattern && (Operator->isSubClassOf("Instruction") || |
| 1259 | Operator->isSubClassOf("SDNodeXForm"))) |
| 1260 | error("Cannot use '" + Operator->getName() + "' in an input pattern!"); |
| 1261 | |
| 1262 | std::vector<TreePatternNode*> Children; |
| 1263 | |
| 1264 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 1265 | Init *Arg = Dag->getArg(i); |
| 1266 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 1267 | Children.push_back(ParseTreePattern(DI)); |
| 1268 | if (Children.back()->getName().empty()) |
| 1269 | Children.back()->setName(Dag->getArgName(i)); |
| 1270 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 1271 | Record *R = DefI->getDef(); |
| 1272 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 1273 | // TreePatternNode if its own. |
| 1274 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1275 | Dag->setArg(i, new DagInit(DefI, "", |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1276 | std::vector<std::pair<Init*, std::string> >())); |
| 1277 | --i; // Revisit this node... |
| 1278 | } else { |
| 1279 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 1280 | Node->setName(Dag->getArgName(i)); |
| 1281 | Children.push_back(Node); |
| 1282 | |
| 1283 | // Input argument? |
| 1284 | if (R->getName() == "node") { |
| 1285 | if (Dag->getArgName(i).empty()) |
| 1286 | error("'node' argument requires a name to match with operand list"); |
| 1287 | Args.push_back(Dag->getArgName(i)); |
| 1288 | } |
| 1289 | } |
| 1290 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 1291 | TreePatternNode *Node = new TreePatternNode(II); |
| 1292 | if (!Dag->getArgName(i).empty()) |
| 1293 | error("Constant int argument should not have a name!"); |
| 1294 | Children.push_back(Node); |
| 1295 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Arg)) { |
| 1296 | // Turn this into an IntInit. |
| 1297 | Init *II = BI->convertInitializerTo(new IntRecTy()); |
| 1298 | if (II == 0 || !dynamic_cast<IntInit*>(II)) |
| 1299 | error("Bits value must be constants!"); |
| 1300 | |
| 1301 | TreePatternNode *Node = new TreePatternNode(dynamic_cast<IntInit*>(II)); |
| 1302 | if (!Dag->getArgName(i).empty()) |
| 1303 | error("Constant int argument should not have a name!"); |
| 1304 | Children.push_back(Node); |
| 1305 | } else { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1306 | errs() << '"'; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1307 | Arg->dump(); |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1308 | errs() << "\": "; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1309 | error("Unknown leaf value for tree pattern!"); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | // If the operator is an intrinsic, then this is just syntactic sugar for for |
| 1314 | // (intrinsic_* <number>, ..children..). Pick the right intrinsic node, and |
| 1315 | // convert the intrinsic name to a number. |
| 1316 | if (Operator->isSubClassOf("Intrinsic")) { |
| 1317 | const CodeGenIntrinsic &Int = getDAGPatterns().getIntrinsic(Operator); |
| 1318 | unsigned IID = getDAGPatterns().getIntrinsicID(Operator)+1; |
| 1319 | |
| 1320 | // If this intrinsic returns void, it must have side-effects and thus a |
| 1321 | // chain. |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1322 | if (Int.IS.RetVTs[0] == MVT::isVoid) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1323 | Operator = getDAGPatterns().get_intrinsic_void_sdnode(); |
| 1324 | } else if (Int.ModRef != CodeGenIntrinsic::NoMem) { |
| 1325 | // Has side-effects, requires chain. |
| 1326 | Operator = getDAGPatterns().get_intrinsic_w_chain_sdnode(); |
| 1327 | } else { |
| 1328 | // Otherwise, no chain. |
| 1329 | Operator = getDAGPatterns().get_intrinsic_wo_chain_sdnode(); |
| 1330 | } |
| 1331 | |
| 1332 | TreePatternNode *IIDNode = new TreePatternNode(new IntInit(IID)); |
| 1333 | Children.insert(Children.begin(), IIDNode); |
| 1334 | } |
| 1335 | |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1336 | TreePatternNode *Result = new TreePatternNode(Operator, Children); |
| 1337 | Result->setName(Dag->getName()); |
| 1338 | return Result; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1342 | /// patterns as possible. Return true if all types are inferred, false |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1343 | /// otherwise. Throw an exception if a type contradiction is found. |
| 1344 | bool TreePattern::InferAllTypes() { |
| 1345 | bool MadeChange = true; |
| 1346 | while (MadeChange) { |
| 1347 | MadeChange = false; |
| 1348 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 1349 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); |
| 1350 | } |
| 1351 | |
| 1352 | bool HasUnresolvedTypes = false; |
| 1353 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 1354 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 1355 | return !HasUnresolvedTypes; |
| 1356 | } |
| 1357 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1358 | void TreePattern::print(raw_ostream &OS) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1359 | OS << getRecord()->getName(); |
| 1360 | if (!Args.empty()) { |
| 1361 | OS << "(" << Args[0]; |
| 1362 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 1363 | OS << ", " << Args[i]; |
| 1364 | OS << ")"; |
| 1365 | } |
| 1366 | OS << ": "; |
| 1367 | |
| 1368 | if (Trees.size() > 1) |
| 1369 | OS << "[\n"; |
| 1370 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 1371 | OS << "\t"; |
| 1372 | Trees[i]->print(OS); |
| 1373 | OS << "\n"; |
| 1374 | } |
| 1375 | |
| 1376 | if (Trees.size() > 1) |
| 1377 | OS << "]\n"; |
| 1378 | } |
| 1379 | |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1380 | void TreePattern::dump() const { print(errs()); } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1381 | |
| 1382 | //===----------------------------------------------------------------------===// |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1383 | // CodeGenDAGPatterns implementation |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1384 | // |
| 1385 | |
| 1386 | // FIXME: REMOVE OSTREAM ARGUMENT |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1387 | CodeGenDAGPatterns::CodeGenDAGPatterns(RecordKeeper &R) : Records(R) { |
Dale Johannesen | 49de982 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 1388 | Intrinsics = LoadIntrinsics(Records, false); |
| 1389 | TgtIntrinsics = LoadIntrinsics(Records, true); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1390 | ParseNodeInfo(); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 1391 | ParseNodeTransforms(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1392 | ParseComplexPatterns(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1393 | ParsePatternFragments(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1394 | ParseDefaultOperands(); |
| 1395 | ParseInstructions(); |
| 1396 | ParsePatterns(); |
| 1397 | |
| 1398 | // Generate variants. For example, commutative patterns can match |
| 1399 | // multiple ways. Add them to PatternsToMatch as well. |
| 1400 | GenerateVariants(); |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 1401 | |
| 1402 | // Infer instruction flags. For example, we can detect loads, |
| 1403 | // stores, and side effects in many cases by examining an |
| 1404 | // instruction's pattern. |
| 1405 | InferInstructionFlags(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1408 | CodeGenDAGPatterns::~CodeGenDAGPatterns() { |
Benjamin Kramer | 5b9e7ef | 2009-08-23 10:39:21 +0000 | [diff] [blame] | 1409 | for (pf_iterator I = PatternFragments.begin(), |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1410 | E = PatternFragments.end(); I != E; ++I) |
| 1411 | delete I->second; |
| 1412 | } |
| 1413 | |
| 1414 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1415 | Record *CodeGenDAGPatterns::getSDNodeNamed(const std::string &Name) const { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1416 | Record *N = Records.getDef(Name); |
| 1417 | if (!N || !N->isSubClassOf("SDNode")) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 1418 | errs() << "Error getting SDNode '" << Name << "'!\n"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1419 | exit(1); |
| 1420 | } |
| 1421 | return N; |
| 1422 | } |
| 1423 | |
| 1424 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1425 | void CodeGenDAGPatterns::ParseNodeInfo() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1426 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 1427 | while (!Nodes.empty()) { |
| 1428 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 1429 | Nodes.pop_back(); |
| 1430 | } |
| 1431 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1432 | // Get the builtin intrinsic nodes. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1433 | intrinsic_void_sdnode = getSDNodeNamed("intrinsic_void"); |
| 1434 | intrinsic_w_chain_sdnode = getSDNodeNamed("intrinsic_w_chain"); |
| 1435 | intrinsic_wo_chain_sdnode = getSDNodeNamed("intrinsic_wo_chain"); |
| 1436 | } |
| 1437 | |
| 1438 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 1439 | /// map, and emit them to the file as functions. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1440 | void CodeGenDAGPatterns::ParseNodeTransforms() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1441 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 1442 | while (!Xforms.empty()) { |
| 1443 | Record *XFormNode = Xforms.back(); |
| 1444 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 1445 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
Chris Lattner | 443e3f9 | 2008-01-05 22:54:53 +0000 | [diff] [blame] | 1446 | SDNodeXForms.insert(std::make_pair(XFormNode, NodeXForm(SDNode, Code))); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1447 | |
| 1448 | Xforms.pop_back(); |
| 1449 | } |
| 1450 | } |
| 1451 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1452 | void CodeGenDAGPatterns::ParseComplexPatterns() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1453 | std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); |
| 1454 | while (!AMs.empty()) { |
| 1455 | ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); |
| 1456 | AMs.pop_back(); |
| 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | |
| 1461 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 1462 | /// file, building up the PatternFragments map. After we've collected them all, |
| 1463 | /// inline fragments together as necessary, so that there are no references left |
| 1464 | /// inside a pattern fragment to a pattern fragment. |
| 1465 | /// |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1466 | void CodeGenDAGPatterns::ParsePatternFragments() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1467 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 1468 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1469 | // First step, parse all of the fragments. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1470 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
| 1471 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
| 1472 | TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this); |
| 1473 | PatternFragments[Fragments[i]] = P; |
| 1474 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1475 | // Validate the argument list, converting it to set, to discard duplicates. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1476 | std::vector<std::string> &Args = P->getArgList(); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1477 | std::set<std::string> OperandsSet(Args.begin(), Args.end()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1479 | if (OperandsSet.count("")) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1480 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 1481 | |
| 1482 | // Parse the operands list. |
| 1483 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 1484 | DefInit *OpsOp = dynamic_cast<DefInit*>(OpsList->getOperator()); |
| 1485 | // Special cases: ops == outs == ins. Different names are used to |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 1486 | // improve readability. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1487 | if (!OpsOp || |
| 1488 | (OpsOp->getDef()->getName() != "ops" && |
| 1489 | OpsOp->getDef()->getName() != "outs" && |
| 1490 | OpsOp->getDef()->getName() != "ins")) |
| 1491 | P->error("Operands list should start with '(ops ... '!"); |
| 1492 | |
| 1493 | // Copy over the arguments. |
| 1494 | Args.clear(); |
| 1495 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 1496 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 1497 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 1498 | getDef()->getName() != "node") |
| 1499 | P->error("Operands list should all be 'node' values."); |
| 1500 | if (OpsList->getArgName(j).empty()) |
| 1501 | P->error("Operands list should have names for each operand!"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1502 | if (!OperandsSet.count(OpsList->getArgName(j))) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1503 | P->error("'" + OpsList->getArgName(j) + |
| 1504 | "' does not occur in pattern or was multiply specified!"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1505 | OperandsSet.erase(OpsList->getArgName(j)); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1506 | Args.push_back(OpsList->getArgName(j)); |
| 1507 | } |
| 1508 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1509 | if (!OperandsSet.empty()) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1510 | P->error("Operands list does not contain an entry for operand '" + |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1511 | *OperandsSet.begin() + "'!"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1513 | // If there is a code init for this fragment, keep track of the fact that |
| 1514 | // this fragment uses it. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1515 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
Chris Lattner | dc32f98 | 2008-01-05 22:43:57 +0000 | [diff] [blame] | 1516 | if (!Code.empty()) |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 1517 | P->getOnlyTree()->addPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1518 | |
| 1519 | // If there is a node transformation corresponding to this, keep track of |
| 1520 | // it. |
| 1521 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 1522 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
| 1523 | P->getOnlyTree()->setTransformFn(Transform); |
| 1524 | } |
| 1525 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1526 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 1527 | // that there are not references to PatFrags left inside of them. |
Chris Lattner | 2ca698d | 2008-06-30 03:02:03 +0000 | [diff] [blame] | 1528 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
| 1529 | TreePattern *ThePat = PatternFragments[Fragments[i]]; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1530 | ThePat->InlinePatternFragments(); |
| 1531 | |
| 1532 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 1533 | // all of them, some may depend on the inputs of the pattern. |
| 1534 | try { |
| 1535 | ThePat->InferAllTypes(); |
| 1536 | } catch (...) { |
| 1537 | // If this pattern fragment is not supported by this target (no types can |
| 1538 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 1539 | // actually used by instructions, the type consistency error will be |
| 1540 | // reported there. |
| 1541 | } |
| 1542 | |
| 1543 | // If debugging, print out the pattern fragment result. |
| 1544 | DEBUG(ThePat->dump()); |
| 1545 | } |
| 1546 | } |
| 1547 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1548 | void CodeGenDAGPatterns::ParseDefaultOperands() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1549 | std::vector<Record*> DefaultOps[2]; |
| 1550 | DefaultOps[0] = Records.getAllDerivedDefinitions("PredicateOperand"); |
| 1551 | DefaultOps[1] = Records.getAllDerivedDefinitions("OptionalDefOperand"); |
| 1552 | |
| 1553 | // Find some SDNode. |
| 1554 | assert(!SDNodes.empty() && "No SDNodes parsed?"); |
| 1555 | Init *SomeSDNode = new DefInit(SDNodes.begin()->first); |
| 1556 | |
| 1557 | for (unsigned iter = 0; iter != 2; ++iter) { |
| 1558 | for (unsigned i = 0, e = DefaultOps[iter].size(); i != e; ++i) { |
| 1559 | DagInit *DefaultInfo = DefaultOps[iter][i]->getValueAsDag("DefaultOps"); |
| 1560 | |
| 1561 | // Clone the DefaultInfo dag node, changing the operator from 'ops' to |
| 1562 | // SomeSDnode so that we can parse this. |
| 1563 | std::vector<std::pair<Init*, std::string> > Ops; |
| 1564 | for (unsigned op = 0, e = DefaultInfo->getNumArgs(); op != e; ++op) |
| 1565 | Ops.push_back(std::make_pair(DefaultInfo->getArg(op), |
| 1566 | DefaultInfo->getArgName(op))); |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1567 | DagInit *DI = new DagInit(SomeSDNode, "", Ops); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1568 | |
| 1569 | // Create a TreePattern to parse this. |
| 1570 | TreePattern P(DefaultOps[iter][i], DI, false, *this); |
| 1571 | assert(P.getNumTrees() == 1 && "This ctor can only produce one tree!"); |
| 1572 | |
| 1573 | // Copy the operands over into a DAGDefaultOperand. |
| 1574 | DAGDefaultOperand DefaultOpInfo; |
| 1575 | |
| 1576 | TreePatternNode *T = P.getTree(0); |
| 1577 | for (unsigned op = 0, e = T->getNumChildren(); op != e; ++op) { |
| 1578 | TreePatternNode *TPN = T->getChild(op); |
| 1579 | while (TPN->ApplyTypeConstraints(P, false)) |
| 1580 | /* Resolve all types */; |
| 1581 | |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1582 | if (TPN->ContainsUnresolvedType()) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1583 | if (iter == 0) |
| 1584 | throw "Value #" + utostr(i) + " of PredicateOperand '" + |
Chris Lattner | 53d09bd | 2010-02-23 05:59:10 +0000 | [diff] [blame] | 1585 | DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!"; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1586 | else |
| 1587 | throw "Value #" + utostr(i) + " of OptionalDefOperand '" + |
Chris Lattner | 53d09bd | 2010-02-23 05:59:10 +0000 | [diff] [blame] | 1588 | DefaultOps[iter][i]->getName() +"' doesn't have a concrete type!"; |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 1589 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1590 | DefaultOpInfo.DefaultOps.push_back(TPN); |
| 1591 | } |
| 1592 | |
| 1593 | // Insert it into the DefaultOperands map so we can find it later. |
| 1594 | DefaultOperands[DefaultOps[iter][i]] = DefaultOpInfo; |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an |
| 1600 | /// instruction input. Return true if this is a real use. |
| 1601 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
| 1602 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1603 | std::vector<Record*> &InstImpInputs) { |
| 1604 | // No name -> not interesting. |
| 1605 | if (Pat->getName().empty()) { |
| 1606 | if (Pat->isLeaf()) { |
| 1607 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1608 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 1609 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
| 1610 | else if (DI && DI->getDef()->isSubClassOf("Register")) |
| 1611 | InstImpInputs.push_back(DI->getDef()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1612 | } |
| 1613 | return false; |
| 1614 | } |
| 1615 | |
| 1616 | Record *Rec; |
| 1617 | if (Pat->isLeaf()) { |
| 1618 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1619 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 1620 | Rec = DI->getDef(); |
| 1621 | } else { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1622 | Rec = Pat->getOperator(); |
| 1623 | } |
| 1624 | |
| 1625 | // SRCVALUE nodes are ignored. |
| 1626 | if (Rec->getName() == "srcvalue") |
| 1627 | return false; |
| 1628 | |
| 1629 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 1630 | if (!Slot) { |
| 1631 | Slot = Pat; |
Chris Lattner | 53d09bd | 2010-02-23 05:59:10 +0000 | [diff] [blame] | 1632 | return true; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1633 | } |
Chris Lattner | 53d09bd | 2010-02-23 05:59:10 +0000 | [diff] [blame] | 1634 | Record *SlotRec; |
| 1635 | if (Slot->isLeaf()) { |
| 1636 | SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
| 1637 | } else { |
| 1638 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 1639 | SlotRec = Slot->getOperator(); |
| 1640 | } |
| 1641 | |
| 1642 | // Ensure that the inputs agree if we've already seen this input. |
| 1643 | if (Rec != SlotRec) |
| 1644 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 1645 | if (Slot->getExtTypes() != Pat->getExtTypes()) |
| 1646 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 1651 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 1652 | /// the pattern. Report errors if we see anything naughty. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1653 | void CodeGenDAGPatterns:: |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1654 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 1655 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1656 | std::map<std::string, TreePatternNode*>&InstResults, |
| 1657 | std::vector<Record*> &InstImpInputs, |
| 1658 | std::vector<Record*> &InstImpResults) { |
| 1659 | if (Pat->isLeaf()) { |
| 1660 | bool isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
| 1661 | if (!isUse && Pat->getTransformFn()) |
| 1662 | I->error("Cannot specify a transform function for a non-input value!"); |
| 1663 | return; |
Chris Lattner | 84aa60b | 2010-02-17 06:53:36 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | if (Pat->getOperator()->getName() == "implicit") { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1667 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
| 1668 | TreePatternNode *Dest = Pat->getChild(i); |
| 1669 | if (!Dest->isLeaf()) |
| 1670 | I->error("implicitly defined value should be a register!"); |
| 1671 | |
| 1672 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1673 | if (!Val || !Val->getDef()->isSubClassOf("Register")) |
| 1674 | I->error("implicitly defined value should be a register!"); |
| 1675 | InstImpResults.push_back(Val->getDef()); |
| 1676 | } |
| 1677 | return; |
Chris Lattner | 84aa60b | 2010-02-17 06:53:36 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | if (Pat->getOperator()->getName() != "set") { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1681 | // If this is not a set, verify that the children nodes are not void typed, |
| 1682 | // and recurse. |
| 1683 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1684 | if (Pat->getChild(i)->getExtTypeNum(0) == MVT::isVoid) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1685 | I->error("Cannot have void nodes inside of patterns!"); |
| 1686 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, |
| 1687 | InstImpInputs, InstImpResults); |
| 1688 | } |
| 1689 | |
| 1690 | // If this is a non-leaf node with no children, treat it basically as if |
| 1691 | // it were a leaf. This handles nodes like (imm). |
Nate Begeman | 7cee817 | 2009-03-19 05:21:56 +0000 | [diff] [blame] | 1692 | bool isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1693 | |
| 1694 | if (!isUse && Pat->getTransformFn()) |
| 1695 | I->error("Cannot specify a transform function for a non-input value!"); |
| 1696 | return; |
Chris Lattner | 84aa60b | 2010-02-17 06:53:36 +0000 | [diff] [blame] | 1697 | } |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1698 | |
| 1699 | // Otherwise, this is a set, validate and collect instruction results. |
| 1700 | if (Pat->getNumChildren() == 0) |
| 1701 | I->error("set requires operands!"); |
| 1702 | |
| 1703 | if (Pat->getTransformFn()) |
| 1704 | I->error("Cannot specify a transform function on a set node!"); |
| 1705 | |
| 1706 | // Check the set destinations. |
| 1707 | unsigned NumDests = Pat->getNumChildren()-1; |
| 1708 | for (unsigned i = 0; i != NumDests; ++i) { |
| 1709 | TreePatternNode *Dest = Pat->getChild(i); |
| 1710 | if (!Dest->isLeaf()) |
| 1711 | I->error("set destination should be a register!"); |
| 1712 | |
| 1713 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1714 | if (!Val) |
| 1715 | I->error("set destination should be a register!"); |
| 1716 | |
| 1717 | if (Val->getDef()->isSubClassOf("RegisterClass") || |
Chris Lattner | a938ac6 | 2009-07-29 20:43:05 +0000 | [diff] [blame] | 1718 | Val->getDef()->isSubClassOf("PointerLikeRegClass")) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1719 | if (Dest->getName().empty()) |
| 1720 | I->error("set destination must have a name!"); |
| 1721 | if (InstResults.count(Dest->getName())) |
| 1722 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
| 1723 | InstResults[Dest->getName()] = Dest; |
| 1724 | } else if (Val->getDef()->isSubClassOf("Register")) { |
| 1725 | InstImpResults.push_back(Val->getDef()); |
| 1726 | } else { |
| 1727 | I->error("set destination should be a register!"); |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | // Verify and collect info from the computation. |
| 1732 | FindPatternInputsAndOutputs(I, Pat->getChild(NumDests), |
| 1733 | InstInputs, InstResults, |
| 1734 | InstImpInputs, InstImpResults); |
| 1735 | } |
| 1736 | |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 1737 | //===----------------------------------------------------------------------===// |
| 1738 | // Instruction Analysis |
| 1739 | //===----------------------------------------------------------------------===// |
| 1740 | |
| 1741 | class InstAnalyzer { |
| 1742 | const CodeGenDAGPatterns &CDP; |
| 1743 | bool &mayStore; |
| 1744 | bool &mayLoad; |
| 1745 | bool &HasSideEffects; |
| 1746 | public: |
| 1747 | InstAnalyzer(const CodeGenDAGPatterns &cdp, |
| 1748 | bool &maystore, bool &mayload, bool &hse) |
| 1749 | : CDP(cdp), mayStore(maystore), mayLoad(mayload), HasSideEffects(hse){ |
| 1750 | } |
| 1751 | |
| 1752 | /// Analyze - Analyze the specified instruction, returning true if the |
| 1753 | /// instruction had a pattern. |
| 1754 | bool Analyze(Record *InstRecord) { |
| 1755 | const TreePattern *Pattern = CDP.getInstruction(InstRecord).getPattern(); |
| 1756 | if (Pattern == 0) { |
| 1757 | HasSideEffects = 1; |
| 1758 | return false; // No pattern. |
| 1759 | } |
| 1760 | |
| 1761 | // FIXME: Assume only the first tree is the pattern. The others are clobber |
| 1762 | // nodes. |
| 1763 | AnalyzeNode(Pattern->getTree(0)); |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
| 1767 | private: |
| 1768 | void AnalyzeNode(const TreePatternNode *N) { |
| 1769 | if (N->isLeaf()) { |
| 1770 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 1771 | Record *LeafRec = DI->getDef(); |
| 1772 | // Handle ComplexPattern leaves. |
| 1773 | if (LeafRec->isSubClassOf("ComplexPattern")) { |
| 1774 | const ComplexPattern &CP = CDP.getComplexPattern(LeafRec); |
| 1775 | if (CP.hasProperty(SDNPMayStore)) mayStore = true; |
| 1776 | if (CP.hasProperty(SDNPMayLoad)) mayLoad = true; |
| 1777 | if (CP.hasProperty(SDNPSideEffect)) HasSideEffects = true; |
| 1778 | } |
| 1779 | } |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | // Analyze children. |
| 1784 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 1785 | AnalyzeNode(N->getChild(i)); |
| 1786 | |
| 1787 | // Ignore set nodes, which are not SDNodes. |
| 1788 | if (N->getOperator()->getName() == "set") |
| 1789 | return; |
| 1790 | |
| 1791 | // Get information about the SDNode for the operator. |
| 1792 | const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator()); |
| 1793 | |
| 1794 | // Notice properties of the node. |
| 1795 | if (OpInfo.hasProperty(SDNPMayStore)) mayStore = true; |
| 1796 | if (OpInfo.hasProperty(SDNPMayLoad)) mayLoad = true; |
| 1797 | if (OpInfo.hasProperty(SDNPSideEffect)) HasSideEffects = true; |
| 1798 | |
| 1799 | if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { |
| 1800 | // If this is an intrinsic, analyze it. |
| 1801 | if (IntInfo->ModRef >= CodeGenIntrinsic::ReadArgMem) |
| 1802 | mayLoad = true;// These may load memory. |
| 1803 | |
| 1804 | if (IntInfo->ModRef >= CodeGenIntrinsic::WriteArgMem) |
| 1805 | mayStore = true;// Intrinsics that can write to memory are 'mayStore'. |
| 1806 | |
| 1807 | if (IntInfo->ModRef >= CodeGenIntrinsic::WriteMem) |
| 1808 | // WriteMem intrinsics can have other strange effects. |
| 1809 | HasSideEffects = true; |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | }; |
| 1814 | |
| 1815 | static void InferFromPattern(const CodeGenInstruction &Inst, |
| 1816 | bool &MayStore, bool &MayLoad, |
| 1817 | bool &HasSideEffects, |
| 1818 | const CodeGenDAGPatterns &CDP) { |
| 1819 | MayStore = MayLoad = HasSideEffects = false; |
| 1820 | |
| 1821 | bool HadPattern = |
| 1822 | InstAnalyzer(CDP, MayStore, MayLoad, HasSideEffects).Analyze(Inst.TheDef); |
| 1823 | |
| 1824 | // InstAnalyzer only correctly analyzes mayStore/mayLoad so far. |
| 1825 | if (Inst.mayStore) { // If the .td file explicitly sets mayStore, use it. |
| 1826 | // If we decided that this is a store from the pattern, then the .td file |
| 1827 | // entry is redundant. |
| 1828 | if (MayStore) |
| 1829 | fprintf(stderr, |
| 1830 | "Warning: mayStore flag explicitly set on instruction '%s'" |
| 1831 | " but flag already inferred from pattern.\n", |
| 1832 | Inst.TheDef->getName().c_str()); |
| 1833 | MayStore = true; |
| 1834 | } |
| 1835 | |
| 1836 | if (Inst.mayLoad) { // If the .td file explicitly sets mayLoad, use it. |
| 1837 | // If we decided that this is a load from the pattern, then the .td file |
| 1838 | // entry is redundant. |
| 1839 | if (MayLoad) |
| 1840 | fprintf(stderr, |
| 1841 | "Warning: mayLoad flag explicitly set on instruction '%s'" |
| 1842 | " but flag already inferred from pattern.\n", |
| 1843 | Inst.TheDef->getName().c_str()); |
| 1844 | MayLoad = true; |
| 1845 | } |
| 1846 | |
| 1847 | if (Inst.neverHasSideEffects) { |
| 1848 | if (HadPattern) |
| 1849 | fprintf(stderr, "Warning: neverHasSideEffects set on instruction '%s' " |
| 1850 | "which already has a pattern\n", Inst.TheDef->getName().c_str()); |
| 1851 | HasSideEffects = false; |
| 1852 | } |
| 1853 | |
| 1854 | if (Inst.hasSideEffects) { |
| 1855 | if (HasSideEffects) |
| 1856 | fprintf(stderr, "Warning: hasSideEffects set on instruction '%s' " |
| 1857 | "which already inferred this.\n", Inst.TheDef->getName().c_str()); |
| 1858 | HasSideEffects = true; |
| 1859 | } |
| 1860 | } |
| 1861 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1862 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 1863 | /// any fragments involved. This populates the Instructions list with fully |
| 1864 | /// resolved instructions. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 1865 | void CodeGenDAGPatterns::ParseInstructions() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1866 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 1867 | |
| 1868 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
| 1869 | ListInit *LI = 0; |
| 1870 | |
| 1871 | if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 1872 | LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 1873 | |
| 1874 | // If there is no pattern, only collect minimal information about the |
| 1875 | // instruction for its operand list. We have to assume that there is one |
| 1876 | // result, as we have no detailed info. |
| 1877 | if (!LI || LI->getSize() == 0) { |
| 1878 | std::vector<Record*> Results; |
| 1879 | std::vector<Record*> Operands; |
| 1880 | |
| 1881 | CodeGenInstruction &InstInfo =Target.getInstruction(Instrs[i]->getName()); |
| 1882 | |
| 1883 | if (InstInfo.OperandList.size() != 0) { |
| 1884 | if (InstInfo.NumDefs == 0) { |
| 1885 | // These produce no results |
| 1886 | for (unsigned j = 0, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1887 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1888 | } else { |
| 1889 | // Assume the first operand is the result. |
| 1890 | Results.push_back(InstInfo.OperandList[0].Rec); |
| 1891 | |
| 1892 | // The rest are inputs. |
| 1893 | for (unsigned j = 1, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1894 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | // Create and insert the instruction. |
| 1899 | std::vector<Record*> ImpResults; |
| 1900 | std::vector<Record*> ImpOperands; |
| 1901 | Instructions.insert(std::make_pair(Instrs[i], |
| 1902 | DAGInstruction(0, Results, Operands, ImpResults, |
| 1903 | ImpOperands))); |
| 1904 | continue; // no pattern. |
| 1905 | } |
| 1906 | |
| 1907 | // Parse the instruction. |
| 1908 | TreePattern *I = new TreePattern(Instrs[i], LI, true, *this); |
| 1909 | // Inline pattern fragments into it. |
| 1910 | I->InlinePatternFragments(); |
| 1911 | |
| 1912 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1913 | // never do anything with this instruction pattern: report it to the user. |
| 1914 | if (!I->InferAllTypes()) |
| 1915 | I->error("Could not infer all types in pattern!"); |
| 1916 | |
| 1917 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 1918 | // with the record they are declared as. |
| 1919 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1920 | |
| 1921 | // InstResults - Keep track of all the virtual registers that are 'set' |
| 1922 | // in the instruction, including what reg class they are. |
| 1923 | std::map<std::string, TreePatternNode*> InstResults; |
| 1924 | |
| 1925 | std::vector<Record*> InstImpInputs; |
| 1926 | std::vector<Record*> InstImpResults; |
| 1927 | |
| 1928 | // Verify that the top-level forms in the instruction are of void type, and |
| 1929 | // fill in the InstResults map. |
| 1930 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 1931 | TreePatternNode *Pat = I->getTree(j); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1932 | if (Pat->getExtTypeNum(0) != MVT::isVoid) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 1933 | I->error("Top-level forms in instruction pattern should have" |
| 1934 | " void types"); |
| 1935 | |
| 1936 | // Find inputs and outputs, and verify the structure of the uses/defs. |
| 1937 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, |
| 1938 | InstImpInputs, InstImpResults); |
| 1939 | } |
| 1940 | |
| 1941 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 1942 | // list for the instruction. This determines the order that operands are |
| 1943 | // added to the machine instruction the node corresponds to. |
| 1944 | unsigned NumResults = InstResults.size(); |
| 1945 | |
| 1946 | // Parse the operands list from the (ops) list, validating it. |
| 1947 | assert(I->getArgList().empty() && "Args list should still be empty here!"); |
| 1948 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 1949 | |
| 1950 | // Check that all of the results occur first in the list. |
| 1951 | std::vector<Record*> Results; |
| 1952 | TreePatternNode *Res0Node = NULL; |
| 1953 | for (unsigned i = 0; i != NumResults; ++i) { |
| 1954 | if (i == CGI.OperandList.size()) |
| 1955 | I->error("'" + InstResults.begin()->first + |
| 1956 | "' set but does not appear in operand list!"); |
| 1957 | const std::string &OpName = CGI.OperandList[i].Name; |
| 1958 | |
| 1959 | // Check that it exists in InstResults. |
| 1960 | TreePatternNode *RNode = InstResults[OpName]; |
| 1961 | if (RNode == 0) |
| 1962 | I->error("Operand $" + OpName + " does not exist in operand list!"); |
| 1963 | |
| 1964 | if (i == 0) |
| 1965 | Res0Node = RNode; |
| 1966 | Record *R = dynamic_cast<DefInit*>(RNode->getLeafValue())->getDef(); |
| 1967 | if (R == 0) |
| 1968 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 1969 | "outputs must occur before inputs in operand list!"); |
| 1970 | |
| 1971 | if (CGI.OperandList[i].Rec != R) |
| 1972 | I->error("Operand $" + OpName + " class mismatch!"); |
| 1973 | |
| 1974 | // Remember the return type. |
| 1975 | Results.push_back(CGI.OperandList[i].Rec); |
| 1976 | |
| 1977 | // Okay, this one checks out. |
| 1978 | InstResults.erase(OpName); |
| 1979 | } |
| 1980 | |
| 1981 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 1982 | // the copy while we're checking the inputs. |
| 1983 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
| 1984 | |
| 1985 | std::vector<TreePatternNode*> ResultNodeOperands; |
| 1986 | std::vector<Record*> Operands; |
| 1987 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 1988 | CodeGenInstruction::OperandInfo &Op = CGI.OperandList[i]; |
| 1989 | const std::string &OpName = Op.Name; |
| 1990 | if (OpName.empty()) |
| 1991 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 1992 | |
| 1993 | if (!InstInputsCheck.count(OpName)) { |
| 1994 | // If this is an predicate operand or optional def operand with an |
| 1995 | // DefaultOps set filled in, we can ignore this. When we codegen it, |
| 1996 | // we will do so as always executed. |
| 1997 | if (Op.Rec->isSubClassOf("PredicateOperand") || |
| 1998 | Op.Rec->isSubClassOf("OptionalDefOperand")) { |
| 1999 | // Does it have a non-empty DefaultOps field? If so, ignore this |
| 2000 | // operand. |
| 2001 | if (!getDefaultOperand(Op.Rec).DefaultOps.empty()) |
| 2002 | continue; |
| 2003 | } |
| 2004 | I->error("Operand $" + OpName + |
| 2005 | " does not appear in the instruction pattern"); |
| 2006 | } |
| 2007 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
| 2008 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
| 2009 | |
| 2010 | if (InVal->isLeaf() && |
| 2011 | dynamic_cast<DefInit*>(InVal->getLeafValue())) { |
| 2012 | Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); |
| 2013 | if (Op.Rec != InRec && !InRec->isSubClassOf("ComplexPattern")) |
| 2014 | I->error("Operand $" + OpName + "'s register class disagrees" |
| 2015 | " between the operand and pattern"); |
| 2016 | } |
| 2017 | Operands.push_back(Op.Rec); |
| 2018 | |
| 2019 | // Construct the result for the dest-pattern operand list. |
| 2020 | TreePatternNode *OpNode = InVal->clone(); |
| 2021 | |
| 2022 | // No predicate is useful on the result. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 2023 | OpNode->clearPredicateFns(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2024 | |
| 2025 | // Promote the xform function to be an explicit node if set. |
| 2026 | if (Record *Xform = OpNode->getTransformFn()) { |
| 2027 | OpNode->setTransformFn(0); |
| 2028 | std::vector<TreePatternNode*> Children; |
| 2029 | Children.push_back(OpNode); |
| 2030 | OpNode = new TreePatternNode(Xform, Children); |
| 2031 | } |
| 2032 | |
| 2033 | ResultNodeOperands.push_back(OpNode); |
| 2034 | } |
| 2035 | |
| 2036 | if (!InstInputsCheck.empty()) |
| 2037 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 2038 | " occurs in pattern but not in operands list!"); |
| 2039 | |
| 2040 | TreePatternNode *ResultPattern = |
| 2041 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
| 2042 | // Copy fully inferred output node type to instruction result pattern. |
| 2043 | if (NumResults > 0) |
| 2044 | ResultPattern->setTypes(Res0Node->getExtTypes()); |
| 2045 | |
| 2046 | // Create and insert the instruction. |
| 2047 | // FIXME: InstImpResults and InstImpInputs should not be part of |
| 2048 | // DAGInstruction. |
| 2049 | DAGInstruction TheInst(I, Results, Operands, InstImpResults, InstImpInputs); |
| 2050 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
| 2051 | |
| 2052 | // Use a temporary tree pattern to infer all types and make sure that the |
| 2053 | // constructed result is correct. This depends on the instruction already |
| 2054 | // being inserted into the Instructions map. |
| 2055 | TreePattern Temp(I->getRecord(), ResultPattern, false, *this); |
| 2056 | Temp.InferAllTypes(); |
| 2057 | |
| 2058 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
| 2059 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
| 2060 | |
| 2061 | DEBUG(I->dump()); |
| 2062 | } |
| 2063 | |
| 2064 | // If we can, convert the instructions to be patterns that are matched! |
Benjamin Kramer | 5b9e7ef | 2009-08-23 10:39:21 +0000 | [diff] [blame] | 2065 | for (std::map<Record*, DAGInstruction, RecordPtrCmp>::iterator II = |
| 2066 | Instructions.begin(), |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2067 | E = Instructions.end(); II != E; ++II) { |
| 2068 | DAGInstruction &TheInst = II->second; |
Chris Lattner | f1ab4f1 | 2008-01-06 01:52:22 +0000 | [diff] [blame] | 2069 | const TreePattern *I = TheInst.getPattern(); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2070 | if (I == 0) continue; // No pattern. |
| 2071 | |
| 2072 | // FIXME: Assume only the first tree is the pattern. The others are clobber |
| 2073 | // nodes. |
| 2074 | TreePatternNode *Pattern = I->getTree(0); |
| 2075 | TreePatternNode *SrcPattern; |
| 2076 | if (Pattern->getOperator()->getName() == "set") { |
| 2077 | SrcPattern = Pattern->getChild(Pattern->getNumChildren()-1)->clone(); |
| 2078 | } else{ |
| 2079 | // Not a set (store or something?) |
| 2080 | SrcPattern = Pattern; |
| 2081 | } |
| 2082 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2083 | Record *Instr = II->first; |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2084 | AddPatternToMatch(I, |
| 2085 | PatternToMatch(Instr->getValueAsListInit("Predicates"), |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2086 | SrcPattern, |
| 2087 | TheInst.getResultPattern(), |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2088 | TheInst.getImpResults(), |
| 2089 | Instr->getValueAsInt("AddedComplexity"))); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2093 | |
| 2094 | typedef std::pair<const TreePatternNode*, unsigned> NameRecord; |
| 2095 | |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2096 | static void FindNames(const TreePatternNode *P, |
Chris Lattner | a27234e | 2010-02-23 07:22:28 +0000 | [diff] [blame] | 2097 | std::map<std::string, NameRecord> &Names, |
| 2098 | const TreePattern *PatternTop) { |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2099 | if (!P->getName().empty()) { |
| 2100 | NameRecord &Rec = Names[P->getName()]; |
| 2101 | // If this is the first instance of the name, remember the node. |
| 2102 | if (Rec.second++ == 0) |
| 2103 | Rec.first = P; |
Chris Lattner | a27234e | 2010-02-23 07:22:28 +0000 | [diff] [blame] | 2104 | else if (Rec.first->getExtTypes() != P->getExtTypes()) |
| 2105 | PatternTop->error("repetition of value: $" + P->getName() + |
| 2106 | " where different uses have different types!"); |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2107 | } |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2108 | |
| 2109 | if (!P->isLeaf()) { |
| 2110 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
Chris Lattner | a27234e | 2010-02-23 07:22:28 +0000 | [diff] [blame] | 2111 | FindNames(P->getChild(i), Names, PatternTop); |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2115 | void CodeGenDAGPatterns::AddPatternToMatch(const TreePattern *Pattern, |
| 2116 | const PatternToMatch &PTM) { |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2117 | // Do some sanity checking on the pattern we're about to match. |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2118 | std::string Reason; |
| 2119 | if (!PTM.getSrcPattern()->canPatternMatch(Reason, *this)) |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2120 | Pattern->error("Pattern can never match: " + Reason); |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2122 | // Find all of the named values in the input and output, ensure they have the |
| 2123 | // same type. |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2124 | std::map<std::string, NameRecord> SrcNames, DstNames; |
Chris Lattner | a27234e | 2010-02-23 07:22:28 +0000 | [diff] [blame] | 2125 | FindNames(PTM.getSrcPattern(), SrcNames, Pattern); |
| 2126 | FindNames(PTM.getDstPattern(), DstNames, Pattern); |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2127 | |
| 2128 | // Scan all of the named values in the destination pattern, rejecting them if |
| 2129 | // they don't exist in the input pattern. |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2130 | for (std::map<std::string, NameRecord>::iterator |
Chris Lattner | ba1cff4 | 2010-02-23 07:50:58 +0000 | [diff] [blame] | 2131 | I = DstNames.begin(), E = DstNames.end(); I != E; ++I) { |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2132 | if (SrcNames[I->first].first == 0) |
Chris Lattner | 967d54a | 2010-02-23 06:35:45 +0000 | [diff] [blame] | 2133 | Pattern->error("Pattern has input without matching name in output: $" + |
| 2134 | I->first); |
Chris Lattner | ba1cff4 | 2010-02-23 07:50:58 +0000 | [diff] [blame] | 2135 | |
| 2136 | #if 0 |
| 2137 | const std::vector<unsigned char> &SrcTypeVec = |
| 2138 | SrcNames[I->first].first->getExtTypes(); |
| 2139 | const std::vector<unsigned char> &DstTypeVec = |
| 2140 | I->second.first->getExtTypes(); |
| 2141 | if (SrcTypeVec == DstTypeVec) continue; |
| 2142 | |
| 2143 | std::string SrcType, DstType; |
| 2144 | for (unsigned i = 0, e = SrcTypeVec.size(); i != e; ++i) |
| 2145 | SrcType += ":" + GetTypeName(SrcTypeVec[i]); |
| 2146 | for (unsigned i = 0, e = DstTypeVec.size(); i != e; ++i) |
| 2147 | DstType += ":" + GetTypeName(DstTypeVec[i]); |
| 2148 | |
| 2149 | Pattern->error("Variable $" + I->first + |
| 2150 | " has different types in source (" + SrcType + |
| 2151 | ") and dest (" + DstType + ") pattern!"); |
| 2152 | #endif |
| 2153 | } |
Chris Lattner | 4ac7a0c | 2010-02-23 06:55:24 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Scan all of the named values in the source pattern, rejecting them if the |
| 2156 | // name isn't used in the dest, and isn't used to tie two values together. |
| 2157 | for (std::map<std::string, NameRecord>::iterator |
| 2158 | I = SrcNames.begin(), E = SrcNames.end(); I != E; ++I) |
| 2159 | if (DstNames[I->first].first == 0 && SrcNames[I->first].second == 1) |
| 2160 | Pattern->error("Pattern has dead named input: $" + I->first); |
| 2161 | |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2162 | PatternsToMatch.push_back(PTM); |
| 2163 | } |
| 2164 | |
| 2165 | |
Dan Gohman | ee4fa19 | 2008-04-03 00:02:49 +0000 | [diff] [blame] | 2166 | |
| 2167 | void CodeGenDAGPatterns::InferInstructionFlags() { |
| 2168 | std::map<std::string, CodeGenInstruction> &InstrDescs = |
| 2169 | Target.getInstructions(); |
| 2170 | for (std::map<std::string, CodeGenInstruction>::iterator |
| 2171 | II = InstrDescs.begin(), E = InstrDescs.end(); II != E; ++II) { |
| 2172 | CodeGenInstruction &InstInfo = II->second; |
| 2173 | // Determine properties of the instruction from its pattern. |
| 2174 | bool MayStore, MayLoad, HasSideEffects; |
| 2175 | InferFromPattern(InstInfo, MayStore, MayLoad, HasSideEffects, *this); |
| 2176 | InstInfo.mayStore = MayStore; |
| 2177 | InstInfo.mayLoad = MayLoad; |
| 2178 | InstInfo.hasSideEffects = HasSideEffects; |
| 2179 | } |
| 2180 | } |
| 2181 | |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 2182 | void CodeGenDAGPatterns::ParsePatterns() { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2183 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
| 2184 | |
| 2185 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 2186 | DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch"); |
| 2187 | DefInit *OpDef = dynamic_cast<DefInit*>(Tree->getOperator()); |
| 2188 | Record *Operator = OpDef->getDef(); |
| 2189 | TreePattern *Pattern; |
| 2190 | if (Operator->getName() != "parallel") |
| 2191 | Pattern = new TreePattern(Patterns[i], Tree, true, *this); |
| 2192 | else { |
| 2193 | std::vector<Init*> Values; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 2194 | RecTy *ListTy = 0; |
| 2195 | for (unsigned j = 0, ee = Tree->getNumArgs(); j != ee; ++j) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2196 | Values.push_back(Tree->getArg(j)); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 2197 | TypedInit *TArg = dynamic_cast<TypedInit*>(Tree->getArg(j)); |
| 2198 | if (TArg == 0) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2199 | errs() << "In dag: " << Tree->getAsString(); |
| 2200 | errs() << " -- Untyped argument in pattern\n"; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 2201 | assert(0 && "Untyped argument in pattern"); |
| 2202 | } |
| 2203 | if (ListTy != 0) { |
| 2204 | ListTy = resolveTypes(ListTy, TArg->getType()); |
| 2205 | if (ListTy == 0) { |
Daniel Dunbar | 1a55180 | 2009-07-03 00:10:29 +0000 | [diff] [blame] | 2206 | errs() << "In dag: " << Tree->getAsString(); |
| 2207 | errs() << " -- Incompatible types in pattern arguments\n"; |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 2208 | assert(0 && "Incompatible types in pattern arguments"); |
| 2209 | } |
| 2210 | } |
| 2211 | else { |
Bill Wendling | ee1f6b0 | 2009-06-09 18:49:42 +0000 | [diff] [blame] | 2212 | ListTy = TArg->getType(); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | ListInit *LI = new ListInit(Values, new ListRecTy(ListTy)); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2216 | Pattern = new TreePattern(Patterns[i], LI, true, *this); |
| 2217 | } |
| 2218 | |
| 2219 | // Inline pattern fragments into it. |
| 2220 | Pattern->InlinePatternFragments(); |
| 2221 | |
| 2222 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 2223 | if (LI->getSize() == 0) continue; // no pattern. |
| 2224 | |
| 2225 | // Parse the instruction. |
| 2226 | TreePattern *Result = new TreePattern(Patterns[i], LI, false, *this); |
| 2227 | |
| 2228 | // Inline pattern fragments into it. |
| 2229 | Result->InlinePatternFragments(); |
| 2230 | |
| 2231 | if (Result->getNumTrees() != 1) |
| 2232 | Result->error("Cannot handle instructions producing instructions " |
| 2233 | "with temporaries yet!"); |
| 2234 | |
| 2235 | bool IterateInference; |
| 2236 | bool InferredAllPatternTypes, InferredAllResultTypes; |
| 2237 | do { |
| 2238 | // Infer as many types as possible. If we cannot infer all of them, we |
| 2239 | // can never do anything with this pattern: report it to the user. |
| 2240 | InferredAllPatternTypes = Pattern->InferAllTypes(); |
| 2241 | |
| 2242 | // Infer as many types as possible. If we cannot infer all of them, we |
| 2243 | // can never do anything with this pattern: report it to the user. |
| 2244 | InferredAllResultTypes = Result->InferAllTypes(); |
| 2245 | |
| 2246 | // Apply the type of the result to the source pattern. This helps us |
| 2247 | // resolve cases where the input type is known to be a pointer type (which |
| 2248 | // is considered resolved), but the result knows it needs to be 32- or |
| 2249 | // 64-bits. Infer the other way for good measure. |
| 2250 | IterateInference = Pattern->getTree(0)-> |
| 2251 | UpdateNodeType(Result->getTree(0)->getExtTypes(), *Result); |
| 2252 | IterateInference |= Result->getTree(0)-> |
| 2253 | UpdateNodeType(Pattern->getTree(0)->getExtTypes(), *Result); |
| 2254 | } while (IterateInference); |
Nate Begeman | 9008ca6 | 2009-04-27 18:41:29 +0000 | [diff] [blame] | 2255 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2256 | // Verify that we inferred enough types that we can do something with the |
| 2257 | // pattern and result. If these fire the user has to add type casts. |
| 2258 | if (!InferredAllPatternTypes) |
| 2259 | Pattern->error("Could not infer all types in pattern!"); |
| 2260 | if (!InferredAllResultTypes) |
| 2261 | Result->error("Could not infer all types in pattern result!"); |
| 2262 | |
| 2263 | // Validate that the input pattern is correct. |
| 2264 | std::map<std::string, TreePatternNode*> InstInputs; |
| 2265 | std::map<std::string, TreePatternNode*> InstResults; |
| 2266 | std::vector<Record*> InstImpInputs; |
| 2267 | std::vector<Record*> InstImpResults; |
| 2268 | for (unsigned j = 0, ee = Pattern->getNumTrees(); j != ee; ++j) |
| 2269 | FindPatternInputsAndOutputs(Pattern, Pattern->getTree(j), |
| 2270 | InstInputs, InstResults, |
| 2271 | InstImpInputs, InstImpResults); |
| 2272 | |
| 2273 | // Promote the xform function to be an explicit node if set. |
| 2274 | TreePatternNode *DstPattern = Result->getOnlyTree(); |
| 2275 | std::vector<TreePatternNode*> ResultNodeOperands; |
| 2276 | for (unsigned ii = 0, ee = DstPattern->getNumChildren(); ii != ee; ++ii) { |
| 2277 | TreePatternNode *OpNode = DstPattern->getChild(ii); |
| 2278 | if (Record *Xform = OpNode->getTransformFn()) { |
| 2279 | OpNode->setTransformFn(0); |
| 2280 | std::vector<TreePatternNode*> Children; |
| 2281 | Children.push_back(OpNode); |
| 2282 | OpNode = new TreePatternNode(Xform, Children); |
| 2283 | } |
| 2284 | ResultNodeOperands.push_back(OpNode); |
| 2285 | } |
| 2286 | DstPattern = Result->getOnlyTree(); |
| 2287 | if (!DstPattern->isLeaf()) |
| 2288 | DstPattern = new TreePatternNode(DstPattern->getOperator(), |
| 2289 | ResultNodeOperands); |
| 2290 | DstPattern->setTypes(Result->getOnlyTree()->getExtTypes()); |
| 2291 | TreePattern Temp(Result->getRecord(), DstPattern, false, *this); |
| 2292 | Temp.InferAllTypes(); |
| 2293 | |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2294 | |
Chris Lattner | 25b6f91 | 2010-02-23 06:16:51 +0000 | [diff] [blame] | 2295 | AddPatternToMatch(Pattern, |
| 2296 | PatternToMatch(Patterns[i]->getValueAsListInit("Predicates"), |
| 2297 | Pattern->getTree(0), |
| 2298 | Temp.getOnlyTree(), InstImpResults, |
| 2299 | Patterns[i]->getValueAsInt("AddedComplexity"))); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | /// CombineChildVariants - Given a bunch of permutations of each child of the |
| 2304 | /// 'operator' node, put them together in all possible ways. |
| 2305 | static void CombineChildVariants(TreePatternNode *Orig, |
| 2306 | const std::vector<std::vector<TreePatternNode*> > &ChildVariants, |
| 2307 | std::vector<TreePatternNode*> &OutVariants, |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2308 | CodeGenDAGPatterns &CDP, |
| 2309 | const MultipleUseVarSet &DepVars) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2310 | // Make sure that each operand has at least one variant to choose from. |
| 2311 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 2312 | if (ChildVariants[i].empty()) |
| 2313 | return; |
| 2314 | |
| 2315 | // The end result is an all-pairs construction of the resultant pattern. |
| 2316 | std::vector<unsigned> Idxs; |
| 2317 | Idxs.resize(ChildVariants.size()); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2318 | bool NotDone; |
| 2319 | do { |
| 2320 | #ifndef NDEBUG |
Chris Lattner | aaf5486 | 2010-02-27 06:51:44 +0000 | [diff] [blame^] | 2321 | DEBUG(if (!Idxs.empty()) { |
| 2322 | errs() << Orig->getOperator()->getName() << ": Idxs = [ "; |
| 2323 | for (unsigned i = 0; i < Idxs.size(); ++i) { |
| 2324 | errs() << Idxs[i] << " "; |
| 2325 | } |
| 2326 | errs() << "]\n"; |
| 2327 | }); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2328 | #endif |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2329 | // Create the variant and add it to the output list. |
| 2330 | std::vector<TreePatternNode*> NewChildren; |
| 2331 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 2332 | NewChildren.push_back(ChildVariants[i][Idxs[i]]); |
| 2333 | TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren); |
| 2334 | |
| 2335 | // Copy over properties. |
| 2336 | R->setName(Orig->getName()); |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 2337 | R->setPredicateFns(Orig->getPredicateFns()); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2338 | R->setTransformFn(Orig->getTransformFn()); |
| 2339 | R->setTypes(Orig->getExtTypes()); |
| 2340 | |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2341 | // If this pattern cannot match, do not include it as a variant. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2342 | std::string ErrString; |
| 2343 | if (!R->canPatternMatch(ErrString, CDP)) { |
| 2344 | delete R; |
| 2345 | } else { |
| 2346 | bool AlreadyExists = false; |
| 2347 | |
| 2348 | // Scan to see if this pattern has already been emitted. We can get |
| 2349 | // duplication due to things like commuting: |
| 2350 | // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) |
| 2351 | // which are the same pattern. Ignore the dups. |
| 2352 | for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2353 | if (R->isIsomorphicTo(OutVariants[i], DepVars)) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2354 | AlreadyExists = true; |
| 2355 | break; |
| 2356 | } |
| 2357 | |
| 2358 | if (AlreadyExists) |
| 2359 | delete R; |
| 2360 | else |
| 2361 | OutVariants.push_back(R); |
| 2362 | } |
| 2363 | |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2364 | // Increment indices to the next permutation by incrementing the |
| 2365 | // indicies from last index backward, e.g., generate the sequence |
| 2366 | // [0, 0], [0, 1], [1, 0], [1, 1]. |
| 2367 | int IdxsIdx; |
| 2368 | for (IdxsIdx = Idxs.size() - 1; IdxsIdx >= 0; --IdxsIdx) { |
| 2369 | if (++Idxs[IdxsIdx] == ChildVariants[IdxsIdx].size()) |
| 2370 | Idxs[IdxsIdx] = 0; |
| 2371 | else |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2372 | break; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2373 | } |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2374 | NotDone = (IdxsIdx >= 0); |
| 2375 | } while (NotDone); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | /// CombineChildVariants - A helper function for binary operators. |
| 2379 | /// |
| 2380 | static void CombineChildVariants(TreePatternNode *Orig, |
| 2381 | const std::vector<TreePatternNode*> &LHS, |
| 2382 | const std::vector<TreePatternNode*> &RHS, |
| 2383 | std::vector<TreePatternNode*> &OutVariants, |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2384 | CodeGenDAGPatterns &CDP, |
| 2385 | const MultipleUseVarSet &DepVars) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2386 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 2387 | ChildVariants.push_back(LHS); |
| 2388 | ChildVariants.push_back(RHS); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2389 | CombineChildVariants(Orig, ChildVariants, OutVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | |
| 2393 | static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, |
| 2394 | std::vector<TreePatternNode *> &Children) { |
| 2395 | assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); |
| 2396 | Record *Operator = N->getOperator(); |
| 2397 | |
| 2398 | // Only permit raw nodes. |
Dan Gohman | 0540e17 | 2008-10-15 06:17:21 +0000 | [diff] [blame] | 2399 | if (!N->getName().empty() || !N->getPredicateFns().empty() || |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2400 | N->getTransformFn()) { |
| 2401 | Children.push_back(N); |
| 2402 | return; |
| 2403 | } |
| 2404 | |
| 2405 | if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) |
| 2406 | Children.push_back(N->getChild(0)); |
| 2407 | else |
| 2408 | GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); |
| 2409 | |
| 2410 | if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) |
| 2411 | Children.push_back(N->getChild(1)); |
| 2412 | else |
| 2413 | GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); |
| 2414 | } |
| 2415 | |
| 2416 | /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of |
| 2417 | /// the (potentially recursive) pattern by using algebraic laws. |
| 2418 | /// |
| 2419 | static void GenerateVariantsOf(TreePatternNode *N, |
| 2420 | std::vector<TreePatternNode*> &OutVariants, |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2421 | CodeGenDAGPatterns &CDP, |
| 2422 | const MultipleUseVarSet &DepVars) { |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2423 | // We cannot permute leaves. |
| 2424 | if (N->isLeaf()) { |
| 2425 | OutVariants.push_back(N); |
| 2426 | return; |
| 2427 | } |
| 2428 | |
| 2429 | // Look up interesting info about the node. |
| 2430 | const SDNodeInfo &NodeInfo = CDP.getSDNodeInfo(N->getOperator()); |
| 2431 | |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 2432 | // If this node is associative, re-associate. |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2433 | if (NodeInfo.hasProperty(SDNPAssociative)) { |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 2434 | // Re-associate by pulling together all of the linked operators |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2435 | std::vector<TreePatternNode*> MaximalChildren; |
| 2436 | GatherChildrenOfAssociativeOpcode(N, MaximalChildren); |
| 2437 | |
| 2438 | // Only handle child sizes of 3. Otherwise we'll end up trying too many |
| 2439 | // permutations. |
| 2440 | if (MaximalChildren.size() == 3) { |
| 2441 | // Find the variants of all of our maximal children. |
| 2442 | std::vector<TreePatternNode*> AVariants, BVariants, CVariants; |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2443 | GenerateVariantsOf(MaximalChildren[0], AVariants, CDP, DepVars); |
| 2444 | GenerateVariantsOf(MaximalChildren[1], BVariants, CDP, DepVars); |
| 2445 | GenerateVariantsOf(MaximalChildren[2], CVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2446 | |
| 2447 | // There are only two ways we can permute the tree: |
| 2448 | // (A op B) op C and A op (B op C) |
| 2449 | // Within these forms, we can also permute A/B/C. |
| 2450 | |
| 2451 | // Generate legal pair permutations of A/B/C. |
| 2452 | std::vector<TreePatternNode*> ABVariants; |
| 2453 | std::vector<TreePatternNode*> BAVariants; |
| 2454 | std::vector<TreePatternNode*> ACVariants; |
| 2455 | std::vector<TreePatternNode*> CAVariants; |
| 2456 | std::vector<TreePatternNode*> BCVariants; |
| 2457 | std::vector<TreePatternNode*> CBVariants; |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2458 | CombineChildVariants(N, AVariants, BVariants, ABVariants, CDP, DepVars); |
| 2459 | CombineChildVariants(N, BVariants, AVariants, BAVariants, CDP, DepVars); |
| 2460 | CombineChildVariants(N, AVariants, CVariants, ACVariants, CDP, DepVars); |
| 2461 | CombineChildVariants(N, CVariants, AVariants, CAVariants, CDP, DepVars); |
| 2462 | CombineChildVariants(N, BVariants, CVariants, BCVariants, CDP, DepVars); |
| 2463 | CombineChildVariants(N, CVariants, BVariants, CBVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Combine those into the result: (x op x) op x |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2466 | CombineChildVariants(N, ABVariants, CVariants, OutVariants, CDP, DepVars); |
| 2467 | CombineChildVariants(N, BAVariants, CVariants, OutVariants, CDP, DepVars); |
| 2468 | CombineChildVariants(N, ACVariants, BVariants, OutVariants, CDP, DepVars); |
| 2469 | CombineChildVariants(N, CAVariants, BVariants, OutVariants, CDP, DepVars); |
| 2470 | CombineChildVariants(N, BCVariants, AVariants, OutVariants, CDP, DepVars); |
| 2471 | CombineChildVariants(N, CBVariants, AVariants, OutVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2472 | |
| 2473 | // Combine those into the result: x op (x op x) |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2474 | CombineChildVariants(N, CVariants, ABVariants, OutVariants, CDP, DepVars); |
| 2475 | CombineChildVariants(N, CVariants, BAVariants, OutVariants, CDP, DepVars); |
| 2476 | CombineChildVariants(N, BVariants, ACVariants, OutVariants, CDP, DepVars); |
| 2477 | CombineChildVariants(N, BVariants, CAVariants, OutVariants, CDP, DepVars); |
| 2478 | CombineChildVariants(N, AVariants, BCVariants, OutVariants, CDP, DepVars); |
| 2479 | CombineChildVariants(N, AVariants, CBVariants, OutVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2480 | return; |
| 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | // Compute permutations of all children. |
| 2485 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 2486 | ChildVariants.resize(N->getNumChildren()); |
| 2487 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2488 | GenerateVariantsOf(N->getChild(i), ChildVariants[i], CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2489 | |
| 2490 | // Build all permutations based on how the children were formed. |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2491 | CombineChildVariants(N, ChildVariants, OutVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2492 | |
| 2493 | // If this node is commutative, consider the commuted order. |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 2494 | bool isCommIntrinsic = N->isCommutativeIntrinsic(CDP); |
| 2495 | if (NodeInfo.hasProperty(SDNPCommutative) || isCommIntrinsic) { |
| 2496 | assert((N->getNumChildren()==2 || isCommIntrinsic) && |
| 2497 | "Commutative but doesn't have 2 children!"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2498 | // Don't count children which are actually register references. |
| 2499 | unsigned NC = 0; |
| 2500 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 2501 | TreePatternNode *Child = N->getChild(i); |
| 2502 | if (Child->isLeaf()) |
| 2503 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 2504 | Record *RR = DI->getDef(); |
| 2505 | if (RR->isSubClassOf("Register")) |
| 2506 | continue; |
| 2507 | } |
| 2508 | NC++; |
| 2509 | } |
| 2510 | // Consider the commuted order. |
Evan Cheng | 6bd9567 | 2008-06-16 20:29:38 +0000 | [diff] [blame] | 2511 | if (isCommIntrinsic) { |
| 2512 | // Commutative intrinsic. First operand is the intrinsic id, 2nd and 3rd |
| 2513 | // operands are the commutative operands, and there might be more operands |
| 2514 | // after those. |
| 2515 | assert(NC >= 3 && |
| 2516 | "Commutative intrinsic should have at least 3 childrean!"); |
| 2517 | std::vector<std::vector<TreePatternNode*> > Variants; |
| 2518 | Variants.push_back(ChildVariants[0]); // Intrinsic id. |
| 2519 | Variants.push_back(ChildVariants[2]); |
| 2520 | Variants.push_back(ChildVariants[1]); |
| 2521 | for (unsigned i = 3; i != NC; ++i) |
| 2522 | Variants.push_back(ChildVariants[i]); |
| 2523 | CombineChildVariants(N, Variants, OutVariants, CDP, DepVars); |
| 2524 | } else if (NC == 2) |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2525 | CombineChildVariants(N, ChildVariants[1], ChildVariants[0], |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2526 | OutVariants, CDP, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | |
| 2531 | // GenerateVariants - Generate variants. For example, commutative patterns can |
| 2532 | // match multiple ways. Add them to PatternsToMatch as well. |
Chris Lattner | fe71893 | 2008-01-06 01:10:31 +0000 | [diff] [blame] | 2533 | void CodeGenDAGPatterns::GenerateVariants() { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2534 | DEBUG(errs() << "Generating instruction variants.\n"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2535 | |
| 2536 | // Loop over all of the patterns we've collected, checking to see if we can |
| 2537 | // generate variants of the instruction, through the exploitation of |
Jim Grosbach | da4231f | 2009-03-26 16:17:51 +0000 | [diff] [blame] | 2538 | // identities. This permits the target to provide aggressive matching without |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2539 | // the .td file having to contain tons of variants of instructions. |
| 2540 | // |
| 2541 | // Note that this loop adds new patterns to the PatternsToMatch list, but we |
| 2542 | // intentionally do not reconsider these. Any variants of added patterns have |
| 2543 | // already been added. |
| 2544 | // |
| 2545 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2546 | MultipleUseVarSet DepVars; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2547 | std::vector<TreePatternNode*> Variants; |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2548 | FindDepVars(PatternsToMatch[i].getSrcPattern(), DepVars); |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2549 | DEBUG(errs() << "Dependent/multiply used variables: "); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2550 | DEBUG(DumpDepVars(DepVars)); |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2551 | DEBUG(errs() << "\n"); |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2552 | GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this, DepVars); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2553 | |
| 2554 | assert(!Variants.empty() && "Must create at least original variant!"); |
| 2555 | Variants.erase(Variants.begin()); // Remove the original pattern. |
| 2556 | |
| 2557 | if (Variants.empty()) // No variants for this pattern. |
| 2558 | continue; |
| 2559 | |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2560 | DEBUG(errs() << "FOUND VARIANTS OF: "; |
| 2561 | PatternsToMatch[i].getSrcPattern()->dump(); |
| 2562 | errs() << "\n"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2563 | |
| 2564 | for (unsigned v = 0, e = Variants.size(); v != e; ++v) { |
| 2565 | TreePatternNode *Variant = Variants[v]; |
| 2566 | |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2567 | DEBUG(errs() << " VAR#" << v << ": "; |
| 2568 | Variant->dump(); |
| 2569 | errs() << "\n"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Scan to see if an instruction or explicit pattern already matches this. |
| 2572 | bool AlreadyExists = false; |
| 2573 | for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { |
Evan Cheng | c0ad80f | 2009-06-26 05:59:16 +0000 | [diff] [blame] | 2574 | // Skip if the top level predicates do not match. |
| 2575 | if (PatternsToMatch[i].getPredicates() != |
| 2576 | PatternsToMatch[p].getPredicates()) |
| 2577 | continue; |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2578 | // Check to see if this variant already exists. |
Scott Michel | 327d065 | 2008-03-05 17:49:05 +0000 | [diff] [blame] | 2579 | if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern(), DepVars)) { |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2580 | DEBUG(errs() << " *** ALREADY EXISTS, ignoring variant.\n"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2581 | AlreadyExists = true; |
| 2582 | break; |
| 2583 | } |
| 2584 | } |
| 2585 | // If we already have it, ignore the variant. |
| 2586 | if (AlreadyExists) continue; |
| 2587 | |
| 2588 | // Otherwise, add it to the list of patterns we have. |
| 2589 | PatternsToMatch. |
| 2590 | push_back(PatternToMatch(PatternsToMatch[i].getPredicates(), |
| 2591 | Variant, PatternsToMatch[i].getDstPattern(), |
| 2592 | PatternsToMatch[i].getDstRegs(), |
| 2593 | PatternsToMatch[i].getAddedComplexity())); |
| 2594 | } |
| 2595 | |
Chris Lattner | 569f121 | 2009-08-23 04:44:11 +0000 | [diff] [blame] | 2596 | DEBUG(errs() << "\n"); |
Chris Lattner | 6cefb77 | 2008-01-05 22:25:12 +0000 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |