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