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