Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1 | //===- DAGISelEmitter.cpp - Generate an instruction selector --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits a DAG instruction selector. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "DAGISelEmitter.h" |
| 15 | #include "Record.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/Debug.h" |
Jeff Cohen | a48283b | 2005-09-25 19:04:43 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 19 | #include <set> |
| 20 | using namespace llvm; |
| 21 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 22 | //===----------------------------------------------------------------------===// |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 23 | // Helpers for working with extended types. |
| 24 | |
| 25 | /// FilterVTs - Filter a list of VT's according to a predicate. |
| 26 | /// |
| 27 | template<typename T> |
| 28 | static std::vector<MVT::ValueType> |
| 29 | FilterVTs(const std::vector<MVT::ValueType> &InVTs, T Filter) { |
| 30 | std::vector<MVT::ValueType> Result; |
| 31 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 32 | if (Filter(InVTs[i])) |
| 33 | Result.push_back(InVTs[i]); |
| 34 | return Result; |
| 35 | } |
| 36 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 37 | template<typename T> |
| 38 | static std::vector<unsigned char> |
| 39 | FilterEVTs(const std::vector<unsigned char> &InVTs, T Filter) { |
| 40 | std::vector<unsigned char> Result; |
| 41 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 42 | if (Filter((MVT::ValueType)InVTs[i])) |
| 43 | Result.push_back(InVTs[i]); |
| 44 | return Result; |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 47 | static std::vector<unsigned char> |
| 48 | ConvertVTs(const std::vector<MVT::ValueType> &InVTs) { |
| 49 | std::vector<unsigned char> Result; |
| 50 | for (unsigned i = 0, e = InVTs.size(); i != e; ++i) |
| 51 | Result.push_back(InVTs[i]); |
| 52 | return Result; |
| 53 | } |
| 54 | |
| 55 | static bool LHSIsSubsetOfRHS(const std::vector<unsigned char> &LHS, |
| 56 | const std::vector<unsigned char> &RHS) { |
| 57 | if (LHS.size() > RHS.size()) return false; |
| 58 | for (unsigned i = 0, e = LHS.size(); i != e; ++i) |
Duraid Madina | d47ae09 | 2005-12-30 16:41:48 +0000 | [diff] [blame] | 59 | if (std::find(RHS.begin(), RHS.end(), LHS[i]) == RHS.end()) |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 60 | return false; |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /// isExtIntegerVT - Return true if the specified extended value type vector |
| 65 | /// contains isInt or an integer value type. |
| 66 | static bool isExtIntegerInVTs(std::vector<unsigned char> EVTs) { |
| 67 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
| 68 | return EVTs[0] == MVT::isInt || !(FilterEVTs(EVTs, MVT::isInteger).empty()); |
| 69 | } |
| 70 | |
| 71 | /// isExtFloatingPointVT - Return true if the specified extended value type |
| 72 | /// vector contains isFP or a FP value type. |
| 73 | static bool isExtFloatingPointInVTs(std::vector<unsigned char> EVTs) { |
| 74 | assert(!EVTs.empty() && "Cannot check for integer in empty ExtVT list!"); |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 75 | return EVTs[0] == MVT::isFP || |
| 76 | !(FilterEVTs(EVTs, MVT::isFloatingPoint).empty()); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | //===----------------------------------------------------------------------===// |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 80 | // SDTypeConstraint implementation |
| 81 | // |
| 82 | |
| 83 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 84 | OperandNo = R->getValueAsInt("OperandNum"); |
| 85 | |
| 86 | if (R->isSubClassOf("SDTCisVT")) { |
| 87 | ConstraintType = SDTCisVT; |
| 88 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
Chris Lattner | 5b21be7 | 2005-12-09 22:57:42 +0000 | [diff] [blame] | 89 | } else if (R->isSubClassOf("SDTCisPtrTy")) { |
| 90 | ConstraintType = SDTCisPtrTy; |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 91 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 92 | ConstraintType = SDTCisInt; |
| 93 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 94 | ConstraintType = SDTCisFP; |
| 95 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 96 | ConstraintType = SDTCisSameAs; |
| 97 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 98 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 99 | ConstraintType = SDTCisVTSmallerThanOp; |
| 100 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 101 | R->getValueAsInt("OtherOperandNum"); |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 102 | } else if (R->isSubClassOf("SDTCisOpSmallerThanOp")) { |
| 103 | ConstraintType = SDTCisOpSmallerThanOp; |
| 104 | x.SDTCisOpSmallerThanOp_Info.BigOperandNum = |
| 105 | R->getValueAsInt("BigOperandNum"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 106 | } else { |
| 107 | std::cerr << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
| 108 | exit(1); |
| 109 | } |
| 110 | } |
| 111 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 112 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 113 | /// N, which has NumResults results. |
| 114 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 115 | TreePatternNode *N, |
| 116 | unsigned NumResults) const { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 117 | assert(NumResults <= 1 && |
| 118 | "We only work with nodes with zero or one result so far!"); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 119 | |
| 120 | if (OpNo < NumResults) |
| 121 | return N; // FIXME: need value # |
| 122 | else |
| 123 | return N->getChild(OpNo-NumResults); |
| 124 | } |
| 125 | |
| 126 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 127 | /// constraint to the nodes operands. This returns true if it makes a |
| 128 | /// change, false otherwise. If a type contradiction is found, throw an |
| 129 | /// exception. |
| 130 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 131 | const SDNodeInfo &NodeInfo, |
| 132 | TreePattern &TP) const { |
| 133 | unsigned NumResults = NodeInfo.getNumResults(); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 134 | assert(NumResults <= 1 && |
| 135 | "We only work with nodes with zero or one result so far!"); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 136 | |
| 137 | // Check that the number of operands is sane. |
| 138 | if (NodeInfo.getNumOperands() >= 0) { |
| 139 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 140 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 141 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 142 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 143 | |
| 144 | const CodeGenTarget &CGT = TP.getDAGISelEmitter().getTargetInfo(); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 145 | |
| 146 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 147 | |
| 148 | switch (ConstraintType) { |
| 149 | default: assert(0 && "Unknown constraint type!"); |
| 150 | case SDTCisVT: |
| 151 | // Operand must be a particular type. |
| 152 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
Chris Lattner | 5b21be7 | 2005-12-09 22:57:42 +0000 | [diff] [blame] | 153 | case SDTCisPtrTy: { |
| 154 | // Operand must be same as target pointer type. |
| 155 | return NodeToApply->UpdateNodeType(CGT.getPointerType(), TP); |
| 156 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 157 | case SDTCisInt: { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 158 | // If there is only one integer type supported, this must be it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 159 | std::vector<MVT::ValueType> IntVTs = |
| 160 | FilterVTs(CGT.getLegalValueTypes(), MVT::isInteger); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 161 | |
| 162 | // If we found exactly one supported integer type, apply it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 163 | if (IntVTs.size() == 1) |
| 164 | return NodeToApply->UpdateNodeType(IntVTs[0], TP); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 165 | return NodeToApply->UpdateNodeType(MVT::isInt, TP); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 166 | } |
| 167 | case SDTCisFP: { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 168 | // If there is only one FP type supported, this must be it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 169 | std::vector<MVT::ValueType> FPVTs = |
| 170 | FilterVTs(CGT.getLegalValueTypes(), MVT::isFloatingPoint); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 171 | |
| 172 | // If we found exactly one supported FP type, apply it. |
Chris Lattner | e0583b1 | 2005-10-14 05:08:37 +0000 | [diff] [blame] | 173 | if (FPVTs.size() == 1) |
| 174 | return NodeToApply->UpdateNodeType(FPVTs[0], TP); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 175 | return NodeToApply->UpdateNodeType(MVT::isFP, TP); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 176 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 177 | case SDTCisSameAs: { |
| 178 | TreePatternNode *OtherNode = |
| 179 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 180 | return NodeToApply->UpdateNodeType(OtherNode->getExtTypes(), TP) | |
| 181 | OtherNode->UpdateNodeType(NodeToApply->getExtTypes(), TP); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 182 | } |
| 183 | case SDTCisVTSmallerThanOp: { |
| 184 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 185 | // have an integer type that is smaller than the VT. |
| 186 | if (!NodeToApply->isLeaf() || |
| 187 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 188 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 189 | ->isSubClassOf("ValueType")) |
| 190 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
| 191 | MVT::ValueType VT = |
| 192 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
| 193 | if (!MVT::isInteger(VT)) |
| 194 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 195 | |
| 196 | TreePatternNode *OtherNode = |
| 197 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 198 | |
| 199 | // It must be integer. |
| 200 | bool MadeChange = false; |
| 201 | MadeChange |= OtherNode->UpdateNodeType(MVT::isInt, TP); |
| 202 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 203 | // This code only handles nodes that have one type set. Assert here so |
| 204 | // that we can change this if we ever need to deal with multiple value |
| 205 | // types at this point. |
| 206 | assert(OtherNode->getExtTypes().size() == 1 && "Node has too many types!"); |
| 207 | if (OtherNode->hasTypeSet() && OtherNode->getTypeNum(0) <= VT) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 208 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
| 209 | return false; |
| 210 | } |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 211 | case SDTCisOpSmallerThanOp: { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 212 | TreePatternNode *BigOperand = |
| 213 | getOperandNum(x.SDTCisOpSmallerThanOp_Info.BigOperandNum, N, NumResults); |
| 214 | |
| 215 | // Both operands must be integer or FP, but we don't care which. |
| 216 | bool MadeChange = false; |
| 217 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 218 | // This code does not currently handle nodes which have multiple types, |
| 219 | // where some types are integer, and some are fp. Assert that this is not |
| 220 | // the case. |
| 221 | assert(!(isExtIntegerInVTs(NodeToApply->getExtTypes()) && |
| 222 | isExtFloatingPointInVTs(NodeToApply->getExtTypes())) && |
| 223 | !(isExtIntegerInVTs(BigOperand->getExtTypes()) && |
| 224 | isExtFloatingPointInVTs(BigOperand->getExtTypes())) && |
| 225 | "SDTCisOpSmallerThanOp does not handle mixed int/fp types!"); |
| 226 | if (isExtIntegerInVTs(NodeToApply->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 227 | MadeChange |= BigOperand->UpdateNodeType(MVT::isInt, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 228 | else if (isExtFloatingPointInVTs(NodeToApply->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 229 | MadeChange |= BigOperand->UpdateNodeType(MVT::isFP, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 230 | if (isExtIntegerInVTs(BigOperand->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 231 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isInt, TP); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 232 | else if (isExtFloatingPointInVTs(BigOperand->getExtTypes())) |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 233 | MadeChange |= NodeToApply->UpdateNodeType(MVT::isFP, TP); |
| 234 | |
| 235 | std::vector<MVT::ValueType> VTs = CGT.getLegalValueTypes(); |
| 236 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 237 | if (isExtIntegerInVTs(NodeToApply->getExtTypes())) { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 238 | VTs = FilterVTs(VTs, MVT::isInteger); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 239 | } else if (isExtFloatingPointInVTs(NodeToApply->getExtTypes())) { |
Chris Lattner | 603d78c | 2005-10-14 06:25:00 +0000 | [diff] [blame] | 240 | VTs = FilterVTs(VTs, MVT::isFloatingPoint); |
| 241 | } else { |
| 242 | VTs.clear(); |
| 243 | } |
| 244 | |
| 245 | switch (VTs.size()) { |
| 246 | default: // Too many VT's to pick from. |
| 247 | case 0: break; // No info yet. |
| 248 | case 1: |
| 249 | // Only one VT of this flavor. Cannot ever satisify the constraints. |
| 250 | return NodeToApply->UpdateNodeType(MVT::Other, TP); // throw |
| 251 | case 2: |
| 252 | // If we have exactly two possible types, the little operand must be the |
| 253 | // small one, the big operand should be the big one. Common with |
| 254 | // float/double for example. |
| 255 | assert(VTs[0] < VTs[1] && "Should be sorted!"); |
| 256 | MadeChange |= NodeToApply->UpdateNodeType(VTs[0], TP); |
| 257 | MadeChange |= BigOperand->UpdateNodeType(VTs[1], TP); |
| 258 | break; |
| 259 | } |
| 260 | return MadeChange; |
Chris Lattner | 03ebd80 | 2005-10-14 04:53:53 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 262 | } |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 268 | // SDNodeInfo implementation |
| 269 | // |
| 270 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 271 | EnumName = R->getValueAsString("Opcode"); |
| 272 | SDClassName = R->getValueAsString("SDClass"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 273 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 274 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 275 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 276 | |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 277 | // Parse the properties. |
| 278 | Properties = 0; |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 279 | std::vector<Record*> PropList = R->getValueAsListOfDefs("Properties"); |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 280 | for (unsigned i = 0, e = PropList.size(); i != e; ++i) { |
| 281 | if (PropList[i]->getName() == "SDNPCommutative") { |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 282 | Properties |= 1 << SDNPCommutative; |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 283 | } else if (PropList[i]->getName() == "SDNPAssociative") { |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 284 | Properties |= 1 << SDNPAssociative; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 285 | } else if (PropList[i]->getName() == "SDNPHasChain") { |
| 286 | Properties |= 1 << SDNPHasChain; |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 287 | } else if (PropList[i]->getName() == "SDNPOutFlag") { |
| 288 | Properties |= 1 << SDNPOutFlag; |
| 289 | } else if (PropList[i]->getName() == "SDNPInFlag") { |
| 290 | Properties |= 1 << SDNPInFlag; |
| 291 | } else if (PropList[i]->getName() == "SDNPOptInFlag") { |
| 292 | Properties |= 1 << SDNPOptInFlag; |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 293 | } else { |
Chris Lattner | 6bc0d74 | 2005-10-28 22:43:25 +0000 | [diff] [blame] | 294 | std::cerr << "Unknown SD Node property '" << PropList[i]->getName() |
Chris Lattner | a1a68ae | 2005-09-28 18:28:29 +0000 | [diff] [blame] | 295 | << "' on node '" << R->getName() << "'!\n"; |
| 296 | exit(1); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 301 | // Parse the type constraints. |
Chris Lattner | b0e103d | 2005-10-28 22:49:02 +0000 | [diff] [blame] | 302 | std::vector<Record*> ConstraintList = |
| 303 | TypeProfile->getValueAsListOfDefs("Constraints"); |
| 304 | TypeConstraints.assign(ConstraintList.begin(), ConstraintList.end()); |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 305 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 306 | |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | // TreePatternNode implementation |
| 309 | // |
| 310 | |
| 311 | TreePatternNode::~TreePatternNode() { |
| 312 | #if 0 // FIXME: implement refcounted tree nodes! |
| 313 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 314 | delete getChild(i); |
| 315 | #endif |
| 316 | } |
| 317 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 318 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 319 | /// information. If N already contains a conflicting type, then throw an |
| 320 | /// exception. This returns true if any information was updated. |
| 321 | /// |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 322 | bool TreePatternNode::UpdateNodeType(const std::vector<unsigned char> &ExtVTs, |
| 323 | TreePattern &TP) { |
| 324 | assert(!ExtVTs.empty() && "Cannot update node type with empty type vector!"); |
| 325 | |
| 326 | if (ExtVTs[0] == MVT::isUnknown || LHSIsSubsetOfRHS(getExtTypes(), ExtVTs)) |
| 327 | return false; |
| 328 | if (isTypeCompletelyUnknown() || LHSIsSubsetOfRHS(ExtVTs, getExtTypes())) { |
| 329 | setTypes(ExtVTs); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 333 | if (ExtVTs[0] == MVT::isInt && isExtIntegerInVTs(getExtTypes())) { |
| 334 | assert(hasTypeSet() && "should be handled above!"); |
| 335 | std::vector<unsigned char> FVTs = FilterEVTs(getExtTypes(), MVT::isInteger); |
| 336 | if (getExtTypes() == FVTs) |
| 337 | return false; |
| 338 | setTypes(FVTs); |
| 339 | return true; |
| 340 | } |
| 341 | if (ExtVTs[0] == MVT::isFP && isExtFloatingPointInVTs(getExtTypes())) { |
| 342 | assert(hasTypeSet() && "should be handled above!"); |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 343 | std::vector<unsigned char> FVTs = |
| 344 | FilterEVTs(getExtTypes(), MVT::isFloatingPoint); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 345 | if (getExtTypes() == FVTs) |
| 346 | return false; |
| 347 | setTypes(FVTs); |
| 348 | return true; |
| 349 | } |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 350 | |
| 351 | // If we know this is an int or fp type, and we are told it is a specific one, |
| 352 | // take the advice. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 353 | // |
| 354 | // Similarly, we should probably set the type here to the intersection of |
| 355 | // {isInt|isFP} and ExtVTs |
| 356 | if ((getExtTypeNum(0) == MVT::isInt && isExtIntegerInVTs(ExtVTs)) || |
| 357 | (getExtTypeNum(0) == MVT::isFP && isExtFloatingPointInVTs(ExtVTs))) { |
| 358 | setTypes(ExtVTs); |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 359 | return true; |
| 360 | } |
| 361 | |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 362 | if (isLeaf()) { |
| 363 | dump(); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 364 | std::cerr << " "; |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 365 | TP.error("Type inference contradiction found in node!"); |
| 366 | } else { |
| 367 | TP.error("Type inference contradiction found in node " + |
| 368 | getOperator()->getName() + "!"); |
| 369 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 370 | return true; // unreachable |
| 371 | } |
| 372 | |
| 373 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 374 | void TreePatternNode::print(std::ostream &OS) const { |
| 375 | if (isLeaf()) { |
| 376 | OS << *getLeafValue(); |
| 377 | } else { |
| 378 | OS << "(" << getOperator()->getName(); |
| 379 | } |
| 380 | |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 381 | // FIXME: At some point we should handle printing all the value types for |
| 382 | // nodes that are multiply typed. |
| 383 | switch (getExtTypeNum(0)) { |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 384 | case MVT::Other: OS << ":Other"; break; |
| 385 | case MVT::isInt: OS << ":isInt"; break; |
| 386 | case MVT::isFP : OS << ":isFP"; break; |
| 387 | case MVT::isUnknown: ; /*OS << ":?";*/ break; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 388 | default: OS << ":" << getTypeNum(0); break; |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 389 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 390 | |
| 391 | if (!isLeaf()) { |
| 392 | if (getNumChildren() != 0) { |
| 393 | OS << " "; |
| 394 | getChild(0)->print(OS); |
| 395 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 396 | OS << ", "; |
| 397 | getChild(i)->print(OS); |
| 398 | } |
| 399 | } |
| 400 | OS << ")"; |
| 401 | } |
| 402 | |
| 403 | if (!PredicateFn.empty()) |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 404 | OS << "<<P:" << PredicateFn << ">>"; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 405 | if (TransformFn) |
| 406 | OS << "<<X:" << TransformFn->getName() << ">>"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 407 | if (!getName().empty()) |
| 408 | OS << ":$" << getName(); |
| 409 | |
| 410 | } |
| 411 | void TreePatternNode::dump() const { |
| 412 | print(std::cerr); |
| 413 | } |
| 414 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 415 | /// isIsomorphicTo - Return true if this node is recursively isomorphic to |
| 416 | /// the specified node. For this comparison, all of the state of the node |
| 417 | /// is considered, except for the assigned name. Nodes with differing names |
| 418 | /// that are otherwise identical are considered isomorphic. |
| 419 | bool TreePatternNode::isIsomorphicTo(const TreePatternNode *N) const { |
| 420 | if (N == this) return true; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 421 | if (N->isLeaf() != isLeaf() || getExtTypes() != N->getExtTypes() || |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 422 | getPredicateFn() != N->getPredicateFn() || |
| 423 | getTransformFn() != N->getTransformFn()) |
| 424 | return false; |
| 425 | |
| 426 | if (isLeaf()) { |
| 427 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) |
| 428 | if (DefInit *NDI = dynamic_cast<DefInit*>(N->getLeafValue())) |
| 429 | return DI->getDef() == NDI->getDef(); |
| 430 | return getLeafValue() == N->getLeafValue(); |
| 431 | } |
| 432 | |
| 433 | if (N->getOperator() != getOperator() || |
| 434 | N->getNumChildren() != getNumChildren()) return false; |
| 435 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 436 | if (!getChild(i)->isIsomorphicTo(N->getChild(i))) |
| 437 | return false; |
| 438 | return true; |
| 439 | } |
| 440 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 441 | /// clone - Make a copy of this tree and all of its children. |
| 442 | /// |
| 443 | TreePatternNode *TreePatternNode::clone() const { |
| 444 | TreePatternNode *New; |
| 445 | if (isLeaf()) { |
| 446 | New = new TreePatternNode(getLeafValue()); |
| 447 | } else { |
| 448 | std::vector<TreePatternNode*> CChildren; |
| 449 | CChildren.reserve(Children.size()); |
| 450 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 451 | CChildren.push_back(getChild(i)->clone()); |
| 452 | New = new TreePatternNode(getOperator(), CChildren); |
| 453 | } |
| 454 | New->setName(getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 455 | New->setTypes(getExtTypes()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 456 | New->setPredicateFn(getPredicateFn()); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 457 | New->setTransformFn(getTransformFn()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 458 | return New; |
| 459 | } |
| 460 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 461 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 462 | /// with actual values specified by ArgMap. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 463 | void TreePatternNode:: |
| 464 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 465 | if (isLeaf()) return; |
| 466 | |
| 467 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 468 | TreePatternNode *Child = getChild(i); |
| 469 | if (Child->isLeaf()) { |
| 470 | Init *Val = Child->getLeafValue(); |
| 471 | if (dynamic_cast<DefInit*>(Val) && |
| 472 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 473 | // We found a use of a formal argument, replace it with its value. |
| 474 | Child = ArgMap[Child->getName()]; |
| 475 | assert(Child && "Couldn't find formal argument!"); |
| 476 | setChild(i, Child); |
| 477 | } |
| 478 | } else { |
| 479 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 486 | /// fragments, inline them into place, giving us a pattern without any |
| 487 | /// PatFrag references. |
| 488 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 489 | if (isLeaf()) return this; // nothing to do. |
| 490 | Record *Op = getOperator(); |
| 491 | |
| 492 | if (!Op->isSubClassOf("PatFrag")) { |
| 493 | // Just recursively inline children nodes. |
| 494 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 495 | setChild(i, getChild(i)->InlinePatternFragments(TP)); |
| 496 | return this; |
| 497 | } |
| 498 | |
| 499 | // Otherwise, we found a reference to a fragment. First, look up its |
| 500 | // TreePattern record. |
| 501 | TreePattern *Frag = TP.getDAGISelEmitter().getPatternFragment(Op); |
| 502 | |
| 503 | // Verify that we are passing the right number of operands. |
| 504 | if (Frag->getNumArgs() != Children.size()) |
| 505 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 506 | utostr(Frag->getNumArgs()) + " operands!"); |
| 507 | |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 508 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 509 | |
| 510 | // Resolve formal arguments to their actual value. |
| 511 | if (Frag->getNumArgs()) { |
| 512 | // Compute the map of formal to actual arguments. |
| 513 | std::map<std::string, TreePatternNode*> ArgMap; |
| 514 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 515 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 516 | |
| 517 | FragTree->SubstituteFormalArguments(ArgMap); |
| 518 | } |
| 519 | |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 520 | FragTree->setName(getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 521 | FragTree->UpdateNodeType(getExtTypes(), TP); |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 522 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 523 | // Get a new copy of this fragment to stitch into here. |
| 524 | //delete this; // FIXME: implement refcounting! |
| 525 | return FragTree; |
| 526 | } |
| 527 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 528 | /// getIntrinsicType - Check to see if the specified record has an intrinsic |
| 529 | /// type which should be applied to it. This infer the type of register |
| 530 | /// references from the register file information, for example. |
| 531 | /// |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 532 | static std::vector<unsigned char> getIntrinsicType(Record *R, bool NotRegisters, |
Chris Lattner | 3c7e18d | 2005-10-14 06:12:03 +0000 | [diff] [blame] | 533 | TreePattern &TP) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 534 | // Some common return values |
| 535 | std::vector<unsigned char> Unknown(1, MVT::isUnknown); |
| 536 | std::vector<unsigned char> Other(1, MVT::Other); |
| 537 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 538 | // Check to see if this is a register or a register class... |
| 539 | if (R->isSubClassOf("RegisterClass")) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 540 | if (NotRegisters) |
| 541 | return Unknown; |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 542 | const CodeGenRegisterClass &RC = |
| 543 | TP.getDAGISelEmitter().getTargetInfo().getRegisterClass(R); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 544 | return ConvertVTs(RC.getValueTypes()); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 545 | } else if (R->isSubClassOf("PatFrag")) { |
| 546 | // Pattern fragment types will be resolved when they are inlined. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 547 | return Unknown; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 548 | } else if (R->isSubClassOf("Register")) { |
Evan Cheng | 37e9005 | 2006-01-15 10:04:45 +0000 | [diff] [blame] | 549 | if (NotRegisters) |
| 550 | return Unknown; |
Chris Lattner | 22faeab | 2005-12-05 02:36:37 +0000 | [diff] [blame] | 551 | // If the register appears in exactly one regclass, and the regclass has one |
| 552 | // value type, use it as the known type. |
| 553 | const CodeGenTarget &T = TP.getDAGISelEmitter().getTargetInfo(); |
| 554 | if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R)) |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 555 | return ConvertVTs(RC->getValueTypes()); |
| 556 | return Unknown; |
Chris Lattner | 1531f20 | 2005-10-26 16:59:37 +0000 | [diff] [blame] | 557 | } else if (R->isSubClassOf("ValueType") || R->isSubClassOf("CondCode")) { |
| 558 | // Using a VTSDNode or CondCodeSDNode. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 559 | return Other; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 560 | } else if (R->isSubClassOf("ComplexPattern")) { |
Evan Cheng | 57c517d | 2006-01-17 07:36:41 +0000 | [diff] [blame] | 561 | if (NotRegisters) |
| 562 | return Unknown; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 563 | std::vector<unsigned char> |
| 564 | ComplexPat(1, TP.getDAGISelEmitter().getComplexPattern(R).getValueType()); |
| 565 | return ComplexPat; |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 566 | } else if (R->getName() == "node" || R->getName() == "srcvalue") { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 567 | // Placeholder. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 568 | return Unknown; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | TP.error("Unknown node flavor used in pattern: " + R->getName()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 572 | return Other; |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 575 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 576 | /// this node and its children in the tree. This returns true if it makes a |
| 577 | /// change, false otherwise. If a type contradiction is found, throw an |
| 578 | /// exception. |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 579 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP, bool NotRegisters) { |
| 580 | if (isLeaf()) { |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 581 | if (DefInit *DI = dynamic_cast<DefInit*>(getLeafValue())) { |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 582 | // If it's a regclass or something else known, include the type. |
| 583 | return UpdateNodeType(getIntrinsicType(DI->getDef(), NotRegisters, TP), |
| 584 | TP); |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 585 | } else if (IntInit *II = dynamic_cast<IntInit*>(getLeafValue())) { |
| 586 | // Int inits are always integers. :) |
| 587 | bool MadeChange = UpdateNodeType(MVT::isInt, TP); |
| 588 | |
| 589 | if (hasTypeSet()) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 590 | // At some point, it may make sense for this tree pattern to have |
| 591 | // multiple types. Assert here that it does not, so we revisit this |
| 592 | // code when appropriate. |
| 593 | assert(getExtTypes().size() == 1 && "TreePattern has too many types!"); |
| 594 | |
| 595 | unsigned Size = MVT::getSizeInBits(getTypeNum(0)); |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 596 | // Make sure that the value is representable for this type. |
| 597 | if (Size < 32) { |
| 598 | int Val = (II->getValue() << (32-Size)) >> (32-Size); |
| 599 | if (Val != II->getValue()) |
| 600 | TP.error("Sign-extended integer value '" + itostr(II->getValue()) + |
| 601 | "' is out of range for type 'MVT::" + |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 602 | getEnumName(getTypeNum(0)) + "'!"); |
Chris Lattner | 465c737 | 2005-11-03 05:46:11 +0000 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
| 606 | return MadeChange; |
| 607 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 608 | return false; |
| 609 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 610 | |
| 611 | // special handling for set, which isn't really an SDNode. |
| 612 | if (getOperator()->getName() == "set") { |
| 613 | assert (getNumChildren() == 2 && "Only handle 2 operand set's for now!"); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 614 | bool MadeChange = getChild(0)->ApplyTypeConstraints(TP, NotRegisters); |
| 615 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP, NotRegisters); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 616 | |
| 617 | // Types of operands must match. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 618 | MadeChange |= getChild(0)->UpdateNodeType(getChild(1)->getExtTypes(), TP); |
| 619 | MadeChange |= getChild(1)->UpdateNodeType(getChild(0)->getExtTypes(), TP); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 620 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 621 | return MadeChange; |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 622 | } else if (getOperator()->isSubClassOf("SDNode")) { |
| 623 | const SDNodeInfo &NI = TP.getDAGISelEmitter().getSDNodeInfo(getOperator()); |
| 624 | |
| 625 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 626 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 627 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 628 | // Branch, etc. do not produce results and top-level forms in instr pattern |
| 629 | // must have void types. |
| 630 | if (NI.getNumResults() == 0) |
| 631 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 632 | return MadeChange; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 633 | } else if (getOperator()->isSubClassOf("Instruction")) { |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 634 | const DAGInstruction &Inst = |
| 635 | TP.getDAGISelEmitter().getInstruction(getOperator()); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 636 | bool MadeChange = false; |
| 637 | unsigned NumResults = Inst.getNumResults(); |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 638 | |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 639 | assert(NumResults <= 1 && |
| 640 | "Only supports zero or one result instrs!"); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 641 | // Apply the result type to the node |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 642 | if (NumResults == 0) { |
| 643 | MadeChange = UpdateNodeType(MVT::isVoid, TP); |
| 644 | } else { |
| 645 | Record *ResultNode = Inst.getResult(0); |
| 646 | assert(ResultNode->isSubClassOf("RegisterClass") && |
| 647 | "Operands should be register classes!"); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 648 | |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 649 | const CodeGenRegisterClass &RC = |
| 650 | TP.getDAGISelEmitter().getTargetInfo().getRegisterClass(ResultNode); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 651 | MadeChange = UpdateNodeType(ConvertVTs(RC.getValueTypes()), TP); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 653 | |
| 654 | if (getNumChildren() != Inst.getNumOperands()) |
| 655 | TP.error("Instruction '" + getOperator()->getName() + " expects " + |
| 656 | utostr(Inst.getNumOperands()) + " operands, not " + |
| 657 | utostr(getNumChildren()) + " operands!"); |
| 658 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 659 | Record *OperandNode = Inst.getOperand(i); |
| 660 | MVT::ValueType VT; |
| 661 | if (OperandNode->isSubClassOf("RegisterClass")) { |
| 662 | const CodeGenRegisterClass &RC = |
| 663 | TP.getDAGISelEmitter().getTargetInfo().getRegisterClass(OperandNode); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 664 | //VT = RC.getValueTypeNum(0); |
| 665 | MadeChange |=getChild(i)->UpdateNodeType(ConvertVTs(RC.getValueTypes()), |
| 666 | TP); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 667 | } else if (OperandNode->isSubClassOf("Operand")) { |
| 668 | VT = getValueType(OperandNode->getValueAsDef("Type")); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 669 | MadeChange |= getChild(i)->UpdateNodeType(VT, TP); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 670 | } else { |
| 671 | assert(0 && "Unknown operand type!"); |
| 672 | abort(); |
| 673 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 674 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP, NotRegisters); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 675 | } |
| 676 | return MadeChange; |
| 677 | } else { |
| 678 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); |
| 679 | |
| 680 | // Node transforms always take one operand, and take and return the same |
| 681 | // type. |
| 682 | if (getNumChildren() != 1) |
| 683 | TP.error("Node transform '" + getOperator()->getName() + |
| 684 | "' requires one operand!"); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 685 | bool MadeChange = UpdateNodeType(getChild(0)->getExtTypes(), TP); |
| 686 | MadeChange |= getChild(0)->UpdateNodeType(getExtTypes(), TP); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 687 | return MadeChange; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 688 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 691 | /// canPatternMatch - If it is impossible for this pattern to match on this |
| 692 | /// target, fill in Reason and return false. Otherwise, return true. This is |
| 693 | /// used as a santity check for .td files (to prevent people from writing stuff |
| 694 | /// that can never possibly work), and to prevent the pattern permuter from |
| 695 | /// generating stuff that is useless. |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 696 | bool TreePatternNode::canPatternMatch(std::string &Reason, DAGISelEmitter &ISE){ |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 697 | if (isLeaf()) return true; |
| 698 | |
| 699 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 700 | if (!getChild(i)->canPatternMatch(Reason, ISE)) |
| 701 | return false; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 702 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 703 | // If this node is a commutative operator, check that the LHS isn't an |
| 704 | // immediate. |
| 705 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(getOperator()); |
| 706 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) { |
| 707 | // Scan all of the operands of the node and make sure that only the last one |
| 708 | // is a constant node. |
| 709 | for (unsigned i = 0, e = getNumChildren()-1; i != e; ++i) |
| 710 | if (!getChild(i)->isLeaf() && |
| 711 | getChild(i)->getOperator()->getName() == "imm") { |
| 712 | Reason = "Immediate value must be on the RHS of commutative operators!"; |
| 713 | return false; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | return true; |
| 718 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 720 | //===----------------------------------------------------------------------===// |
| 721 | // TreePattern implementation |
| 722 | // |
| 723 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 724 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 725 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 726 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 727 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
| 728 | Trees.push_back(ParseTreePattern((DagInit*)RawPat->getElement(i))); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 731 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, bool isInput, |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 732 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 733 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 734 | Trees.push_back(ParseTreePattern(Pat)); |
| 735 | } |
| 736 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 737 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 738 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 739 | isInputPattern = isInput; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 740 | Trees.push_back(Pat); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 745 | void TreePattern::error(const std::string &Msg) const { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 746 | dump(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 747 | throw "In " + TheRecord->getName() + ": " + Msg; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 750 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 751 | Record *Operator = Dag->getNodeType(); |
| 752 | |
| 753 | if (Operator->isSubClassOf("ValueType")) { |
| 754 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 755 | // node. |
| 756 | if (Dag->getNumArgs() != 1) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 757 | error("Type cast only takes one operand!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 758 | |
| 759 | Init *Arg = Dag->getArg(0); |
| 760 | TreePatternNode *New; |
| 761 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 762 | Record *R = DI->getDef(); |
| 763 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 764 | Dag->setArg(0, new DagInit(R, |
| 765 | std::vector<std::pair<Init*, std::string> >())); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 766 | return ParseTreePattern(Dag); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 767 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 768 | New = new TreePatternNode(DI); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 769 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 770 | New = ParseTreePattern(DI); |
Chris Lattner | 0614b62 | 2005-11-02 06:49:14 +0000 | [diff] [blame] | 771 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 772 | New = new TreePatternNode(II); |
| 773 | if (!Dag->getArgName(0).empty()) |
| 774 | error("Constant int argument should not have a name!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 775 | } else { |
| 776 | Arg->dump(); |
| 777 | error("Unknown leaf value for tree pattern!"); |
| 778 | return 0; |
| 779 | } |
| 780 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 781 | // Apply the type cast. |
| 782 | New->UpdateNodeType(getValueType(Operator), *this); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 783 | New->setName(Dag->getArgName(0)); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 784 | return New; |
| 785 | } |
| 786 | |
| 787 | // Verify that this is something that makes sense for an operator. |
| 788 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 789 | !Operator->isSubClassOf("Instruction") && |
| 790 | !Operator->isSubClassOf("SDNodeXForm") && |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 791 | Operator->getName() != "set") |
| 792 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 793 | |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 794 | // Check to see if this is something that is illegal in an input pattern. |
| 795 | if (isInputPattern && (Operator->isSubClassOf("Instruction") || |
| 796 | Operator->isSubClassOf("SDNodeXForm"))) |
| 797 | error("Cannot use '" + Operator->getName() + "' in an input pattern!"); |
| 798 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 799 | std::vector<TreePatternNode*> Children; |
| 800 | |
| 801 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 802 | Init *Arg = Dag->getArg(i); |
| 803 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 804 | Children.push_back(ParseTreePattern(DI)); |
Chris Lattner | 12cf909 | 2005-11-16 23:14:54 +0000 | [diff] [blame] | 805 | if (Children.back()->getName().empty()) |
| 806 | Children.back()->setName(Dag->getArgName(i)); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 807 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 808 | Record *R = DefI->getDef(); |
| 809 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 810 | // TreePatternNode if its own. |
| 811 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 812 | Dag->setArg(i, new DagInit(R, |
| 813 | std::vector<std::pair<Init*, std::string> >())); |
| 814 | --i; // Revisit this node... |
| 815 | } else { |
| 816 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 817 | Node->setName(Dag->getArgName(i)); |
| 818 | Children.push_back(Node); |
| 819 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 820 | // Input argument? |
| 821 | if (R->getName() == "node") { |
| 822 | if (Dag->getArgName(i).empty()) |
| 823 | error("'node' argument requires a name to match with operand list"); |
| 824 | Args.push_back(Dag->getArgName(i)); |
| 825 | } |
| 826 | } |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 827 | } else if (IntInit *II = dynamic_cast<IntInit*>(Arg)) { |
| 828 | TreePatternNode *Node = new TreePatternNode(II); |
| 829 | if (!Dag->getArgName(i).empty()) |
| 830 | error("Constant int argument should not have a name!"); |
| 831 | Children.push_back(Node); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 832 | } else { |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 833 | std::cerr << '"'; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 834 | Arg->dump(); |
Chris Lattner | 5d5a056 | 2005-10-19 04:30:56 +0000 | [diff] [blame] | 835 | std::cerr << "\": "; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 836 | error("Unknown leaf value for tree pattern!"); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | return new TreePatternNode(Operator, Children); |
| 841 | } |
| 842 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 843 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 844 | /// patterns as possible. Return true if all types are infered, false |
| 845 | /// otherwise. Throw an exception if a type contradiction is found. |
| 846 | bool TreePattern::InferAllTypes() { |
| 847 | bool MadeChange = true; |
| 848 | while (MadeChange) { |
| 849 | MadeChange = false; |
| 850 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 851 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this, false); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | bool HasUnresolvedTypes = false; |
| 855 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 856 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 857 | return !HasUnresolvedTypes; |
| 858 | } |
| 859 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 860 | void TreePattern::print(std::ostream &OS) const { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 861 | OS << getRecord()->getName(); |
| 862 | if (!Args.empty()) { |
| 863 | OS << "(" << Args[0]; |
| 864 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 865 | OS << ", " << Args[i]; |
| 866 | OS << ")"; |
| 867 | } |
| 868 | OS << ": "; |
| 869 | |
| 870 | if (Trees.size() > 1) |
| 871 | OS << "[\n"; |
| 872 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 873 | OS << "\t"; |
| 874 | Trees[i]->print(OS); |
| 875 | OS << "\n"; |
| 876 | } |
| 877 | |
| 878 | if (Trees.size() > 1) |
| 879 | OS << "]\n"; |
| 880 | } |
| 881 | |
| 882 | void TreePattern::dump() const { print(std::cerr); } |
| 883 | |
| 884 | |
| 885 | |
| 886 | //===----------------------------------------------------------------------===// |
| 887 | // DAGISelEmitter implementation |
| 888 | // |
| 889 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 890 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
| 891 | void DAGISelEmitter::ParseNodeInfo() { |
| 892 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 893 | while (!Nodes.empty()) { |
| 894 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 895 | Nodes.pop_back(); |
| 896 | } |
| 897 | } |
| 898 | |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 899 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 900 | /// map, and emit them to the file as functions. |
| 901 | void DAGISelEmitter::ParseNodeTransforms(std::ostream &OS) { |
| 902 | OS << "\n// Node transformations.\n"; |
| 903 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 904 | while (!Xforms.empty()) { |
| 905 | Record *XFormNode = Xforms.back(); |
| 906 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 907 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
| 908 | SDNodeXForms.insert(std::make_pair(XFormNode, |
| 909 | std::make_pair(SDNode, Code))); |
| 910 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 911 | if (!Code.empty()) { |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 912 | std::string ClassName = getSDNodeInfo(SDNode).getSDClassName(); |
| 913 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 914 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 915 | OS << "inline SDOperand Transform_" << XFormNode->getName() |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 916 | << "(SDNode *" << C2 << ") {\n"; |
| 917 | if (ClassName != "SDNode") |
| 918 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 919 | OS << Code << "\n}\n"; |
| 920 | } |
| 921 | |
| 922 | Xforms.pop_back(); |
| 923 | } |
| 924 | } |
| 925 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 926 | void DAGISelEmitter::ParseComplexPatterns() { |
| 927 | std::vector<Record*> AMs = Records.getAllDerivedDefinitions("ComplexPattern"); |
| 928 | while (!AMs.empty()) { |
| 929 | ComplexPatterns.insert(std::make_pair(AMs.back(), AMs.back())); |
| 930 | AMs.pop_back(); |
| 931 | } |
| 932 | } |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 933 | |
| 934 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 935 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 936 | /// file, building up the PatternFragments map. After we've collected them all, |
| 937 | /// inline fragments together as necessary, so that there are no references left |
| 938 | /// inside a pattern fragment to a pattern fragment. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 939 | /// |
| 940 | /// This also emits all of the predicate functions to the output file. |
| 941 | /// |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 942 | void DAGISelEmitter::ParsePatternFragments(std::ostream &OS) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 943 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 944 | |
| 945 | // First step, parse all of the fragments and emit predicate functions. |
| 946 | OS << "\n// Predicate functions.\n"; |
| 947 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 948 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 949 | TreePattern *P = new TreePattern(Fragments[i], Tree, true, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 950 | PatternFragments[Fragments[i]] = P; |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 951 | |
| 952 | // Validate the argument list, converting it to map, to discard duplicates. |
| 953 | std::vector<std::string> &Args = P->getArgList(); |
| 954 | std::set<std::string> OperandsMap(Args.begin(), Args.end()); |
| 955 | |
| 956 | if (OperandsMap.count("")) |
| 957 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 958 | |
| 959 | // Parse the operands list. |
| 960 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 961 | if (OpsList->getNodeType()->getName() != "ops") |
| 962 | P->error("Operands list should start with '(ops ... '!"); |
| 963 | |
| 964 | // Copy over the arguments. |
| 965 | Args.clear(); |
| 966 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 967 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 968 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 969 | getDef()->getName() != "node") |
| 970 | P->error("Operands list should all be 'node' values."); |
| 971 | if (OpsList->getArgName(j).empty()) |
| 972 | P->error("Operands list should have names for each operand!"); |
| 973 | if (!OperandsMap.count(OpsList->getArgName(j))) |
| 974 | P->error("'" + OpsList->getArgName(j) + |
| 975 | "' does not occur in pattern or was multiply specified!"); |
| 976 | OperandsMap.erase(OpsList->getArgName(j)); |
| 977 | Args.push_back(OpsList->getArgName(j)); |
| 978 | } |
| 979 | |
| 980 | if (!OperandsMap.empty()) |
| 981 | P->error("Operands list does not contain an entry for operand '" + |
| 982 | *OperandsMap.begin() + "'!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 983 | |
| 984 | // If there is a code init for this fragment, emit the predicate code and |
| 985 | // keep track of the fact that this fragment uses it. |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 986 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
| 987 | if (!Code.empty()) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 988 | assert(!P->getOnlyTree()->isLeaf() && "Can't be a leaf!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 989 | std::string ClassName = |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 990 | getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 991 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 992 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 993 | OS << "inline bool Predicate_" << Fragments[i]->getName() |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 994 | << "(SDNode *" << C2 << ") {\n"; |
| 995 | if (ClassName != "SDNode") |
| 996 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 997 | OS << Code << "\n}\n"; |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 998 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 999 | } |
Chris Lattner | 6de8b53 | 2005-09-13 21:59:15 +0000 | [diff] [blame] | 1000 | |
| 1001 | // If there is a node transformation corresponding to this, keep track of |
| 1002 | // it. |
| 1003 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 1004 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1005 | P->getOnlyTree()->setTransformFn(Transform); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | OS << "\n\n"; |
| 1009 | |
| 1010 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 1011 | // that there are not references to PatFrags left inside of them. |
| 1012 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 1013 | E = PatternFragments.end(); I != E; ++I) { |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1014 | TreePattern *ThePat = I->second; |
| 1015 | ThePat->InlinePatternFragments(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1017 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 1018 | // all of them, some may depend on the inputs of the pattern. |
| 1019 | try { |
| 1020 | ThePat->InferAllTypes(); |
| 1021 | } catch (...) { |
| 1022 | // If this pattern fragment is not supported by this target (no types can |
| 1023 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 1024 | // actually used by instructions, the type consistency error will be |
| 1025 | // reported there. |
| 1026 | } |
| 1027 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1028 | // If debugging, print out the pattern fragment result. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1029 | DEBUG(ThePat->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1033 | /// HandleUse - Given "Pat" a leaf in the pattern, check to see if it is an |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1034 | /// instruction input. Return true if this is a real use. |
| 1035 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1036 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 1037 | std::vector<Record*> &InstImpInputs) { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1038 | // No name -> not interesting. |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1039 | if (Pat->getName().empty()) { |
| 1040 | if (Pat->isLeaf()) { |
| 1041 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1042 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 1043 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1044 | else if (DI && DI->getDef()->isSubClassOf("Register")) |
| 1045 | InstImpInputs.push_back(DI->getDef()); |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1046 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1047 | return false; |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 1048 | } |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1049 | |
| 1050 | Record *Rec; |
| 1051 | if (Pat->isLeaf()) { |
| 1052 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 1053 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 1054 | Rec = DI->getDef(); |
| 1055 | } else { |
| 1056 | assert(Pat->getNumChildren() == 0 && "can't be a use with children!"); |
| 1057 | Rec = Pat->getOperator(); |
| 1058 | } |
| 1059 | |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 1060 | // SRCVALUE nodes are ignored. |
| 1061 | if (Rec->getName() == "srcvalue") |
| 1062 | return false; |
| 1063 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1064 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 1065 | if (!Slot) { |
| 1066 | Slot = Pat; |
| 1067 | } else { |
| 1068 | Record *SlotRec; |
| 1069 | if (Slot->isLeaf()) { |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1070 | SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1071 | } else { |
| 1072 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 1073 | SlotRec = Slot->getOperator(); |
| 1074 | } |
| 1075 | |
| 1076 | // Ensure that the inputs agree if we've already seen this input. |
| 1077 | if (Rec != SlotRec) |
| 1078 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1079 | if (Slot->getExtTypes() != Pat->getExtTypes()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1080 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 1081 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1082 | return true; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 1086 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 1087 | /// the pattern. Report errors if we see anything naughty. |
| 1088 | void DAGISelEmitter:: |
| 1089 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 1090 | std::map<std::string, TreePatternNode*> &InstInputs, |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1091 | std::map<std::string, Record*> &InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1092 | std::vector<Record*> &InstImpInputs, |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1093 | std::vector<Record*> &InstImpResults) { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1094 | if (Pat->isLeaf()) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1095 | bool isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1096 | if (!isUse && Pat->getTransformFn()) |
| 1097 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1098 | return; |
| 1099 | } else if (Pat->getOperator()->getName() != "set") { |
| 1100 | // If this is not a set, verify that the children nodes are not void typed, |
| 1101 | // and recurse. |
| 1102 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1103 | if (Pat->getChild(i)->getExtTypeNum(0) == MVT::isVoid) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1104 | I->error("Cannot have void nodes inside of patterns!"); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1105 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1106 | InstImpInputs, InstImpResults); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | // If this is a non-leaf node with no children, treat it basically as if |
| 1110 | // it were a leaf. This handles nodes like (imm). |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1111 | bool isUse = false; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1112 | if (Pat->getNumChildren() == 0) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1113 | isUse = HandleUse(I, Pat, InstInputs, InstImpInputs); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1114 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1115 | if (!isUse && Pat->getTransformFn()) |
| 1116 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | // Otherwise, this is a set, validate and collect instruction results. |
| 1121 | if (Pat->getNumChildren() == 0) |
| 1122 | I->error("set requires operands!"); |
| 1123 | else if (Pat->getNumChildren() & 1) |
| 1124 | I->error("set requires an even number of operands"); |
| 1125 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 1126 | if (Pat->getTransformFn()) |
| 1127 | I->error("Cannot specify a transform function on a set node!"); |
| 1128 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1129 | // Check the set destinations. |
| 1130 | unsigned NumValues = Pat->getNumChildren()/2; |
| 1131 | for (unsigned i = 0; i != NumValues; ++i) { |
| 1132 | TreePatternNode *Dest = Pat->getChild(i); |
| 1133 | if (!Dest->isLeaf()) |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 1134 | I->error("set destination should be a register!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1135 | |
| 1136 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 1137 | if (!Val) |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 1138 | I->error("set destination should be a register!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1139 | |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1140 | if (Val->getDef()->isSubClassOf("RegisterClass")) { |
| 1141 | if (Dest->getName().empty()) |
| 1142 | I->error("set destination must have a name!"); |
| 1143 | if (InstResults.count(Dest->getName())) |
| 1144 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
| 1145 | InstResults[Dest->getName()] = Val->getDef(); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1146 | } else if (Val->getDef()->isSubClassOf("Register")) { |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1147 | InstImpResults.push_back(Val->getDef()); |
| 1148 | } else { |
| 1149 | I->error("set destination should be a register!"); |
| 1150 | } |
| 1151 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1152 | // Verify and collect info from the computation. |
| 1153 | FindPatternInputsAndOutputs(I, Pat->getChild(i+NumValues), |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1154 | InstInputs, InstResults, |
| 1155 | InstImpInputs, InstImpResults); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1159 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 1160 | /// any fragments involved. This populates the Instructions list with fully |
| 1161 | /// resolved instructions. |
| 1162 | void DAGISelEmitter::ParseInstructions() { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1163 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 1164 | |
| 1165 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1166 | ListInit *LI = 0; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1167 | |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1168 | if (dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 1169 | LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 1170 | |
| 1171 | // If there is no pattern, only collect minimal information about the |
| 1172 | // instruction for its operand list. We have to assume that there is one |
| 1173 | // result, as we have no detailed info. |
| 1174 | if (!LI || LI->getSize() == 0) { |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1175 | std::vector<Record*> Results; |
| 1176 | std::vector<Record*> Operands; |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1177 | |
| 1178 | CodeGenInstruction &InstInfo =Target.getInstruction(Instrs[i]->getName()); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1179 | |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1180 | if (InstInfo.OperandList.size() != 0) { |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1181 | // FIXME: temporary hack... |
Evan Cheng | 2b4ea79 | 2005-12-26 09:11:45 +0000 | [diff] [blame] | 1182 | if (InstInfo.noResults) { |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1183 | // These produce no results |
| 1184 | for (unsigned j = 0, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1185 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1186 | } else { |
| 1187 | // Assume the first operand is the result. |
| 1188 | Results.push_back(InstInfo.OperandList[0].Rec); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1189 | |
Evan Cheng | 3a217f3 | 2005-12-22 02:35:21 +0000 | [diff] [blame] | 1190 | // The rest are inputs. |
| 1191 | for (unsigned j = 1, e = InstInfo.OperandList.size(); j < e; ++j) |
| 1192 | Operands.push_back(InstInfo.OperandList[j].Rec); |
| 1193 | } |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1194 | } |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1195 | |
| 1196 | // Create and insert the instruction. |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1197 | std::vector<Record*> ImpResults; |
| 1198 | std::vector<Record*> ImpOperands; |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1199 | Instructions.insert(std::make_pair(Instrs[i], |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1200 | DAGInstruction(0, Results, Operands, ImpResults, |
| 1201 | ImpOperands))); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1202 | continue; // no pattern. |
| 1203 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1204 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1205 | // Parse the instruction. |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1206 | TreePattern *I = new TreePattern(Instrs[i], LI, true, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1207 | // Inline pattern fragments into it. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1208 | I->InlinePatternFragments(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1209 | |
Chris Lattner | 95f6b76 | 2005-09-08 23:26:30 +0000 | [diff] [blame] | 1210 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1211 | // never do anything with this instruction pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1212 | if (!I->InferAllTypes()) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1213 | I->error("Could not infer all types in pattern!"); |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1215 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 1216 | // with the record they are declared as. |
| 1217 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1218 | |
| 1219 | // InstResults - Keep track of all the virtual registers that are 'set' |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1220 | // in the instruction, including what reg class they are. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1221 | std::map<std::string, Record*> InstResults; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1222 | |
| 1223 | std::vector<Record*> InstImpInputs; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1224 | std::vector<Record*> InstImpResults; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1225 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1226 | // Verify that the top-level forms in the instruction are of void type, and |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1227 | // fill in the InstResults map. |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1228 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 1229 | TreePatternNode *Pat = I->getTree(j); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1230 | if (Pat->getExtTypeNum(0) != MVT::isVoid) |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 1231 | I->error("Top-level forms in instruction pattern should have" |
| 1232 | " void types"); |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1234 | // Find inputs and outputs, and verify the structure of the uses/defs. |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1235 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1236 | InstImpInputs, InstImpResults); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1237 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 1238 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1239 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 1240 | // list for the instruction. This determines the order that operands are |
| 1241 | // added to the machine instruction the node corresponds to. |
| 1242 | unsigned NumResults = InstResults.size(); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1243 | |
| 1244 | // Parse the operands list from the (ops) list, validating it. |
| 1245 | std::vector<std::string> &Args = I->getArgList(); |
| 1246 | assert(Args.empty() && "Args list should still be empty here!"); |
| 1247 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 1248 | |
| 1249 | // Check that all of the results occur first in the list. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1250 | std::vector<Record*> Results; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1251 | for (unsigned i = 0; i != NumResults; ++i) { |
Chris Lattner | 3a7319d | 2005-09-14 21:04:12 +0000 | [diff] [blame] | 1252 | if (i == CGI.OperandList.size()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1253 | I->error("'" + InstResults.begin()->first + |
| 1254 | "' set but does not appear in operand list!"); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1255 | const std::string &OpName = CGI.OperandList[i].Name; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1257 | // Check that it exists in InstResults. |
| 1258 | Record *R = InstResults[OpName]; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1259 | if (R == 0) |
| 1260 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 1261 | "outputs must occur before inputs in operand list!"); |
| 1262 | |
| 1263 | if (CGI.OperandList[i].Rec != R) |
| 1264 | I->error("Operand $" + OpName + " class mismatch!"); |
| 1265 | |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 1266 | // Remember the return type. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1267 | Results.push_back(CGI.OperandList[i].Rec); |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 1268 | |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1269 | // Okay, this one checks out. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1270 | InstResults.erase(OpName); |
| 1271 | } |
| 1272 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1273 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 1274 | // the copy while we're checking the inputs. |
| 1275 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1276 | |
| 1277 | std::vector<TreePatternNode*> ResultNodeOperands; |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1278 | std::vector<Record*> Operands; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1279 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 1280 | const std::string &OpName = CGI.OperandList[i].Name; |
| 1281 | if (OpName.empty()) |
| 1282 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 1283 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1284 | if (!InstInputsCheck.count(OpName)) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1285 | I->error("Operand $" + OpName + |
| 1286 | " does not appear in the instruction pattern"); |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1287 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1288 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (InVal->isLeaf() && |
| 1291 | dynamic_cast<DefInit*>(InVal->getLeafValue())) { |
| 1292 | Record *InRec = static_cast<DefInit*>(InVal->getLeafValue())->getDef(); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1293 | if (CGI.OperandList[i].Rec != InRec && |
| 1294 | !InRec->isSubClassOf("ComplexPattern")) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1295 | I->error("Operand $" + OpName + "'s register class disagrees" |
| 1296 | " between the operand and pattern"); |
Nate Begeman | ddb3954 | 2005-12-01 00:06:14 +0000 | [diff] [blame] | 1297 | } |
| 1298 | Operands.push_back(CGI.OperandList[i].Rec); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1299 | |
Chris Lattner | 2175c18 | 2005-09-14 23:01:59 +0000 | [diff] [blame] | 1300 | // Construct the result for the dest-pattern operand list. |
| 1301 | TreePatternNode *OpNode = InVal->clone(); |
| 1302 | |
| 1303 | // No predicate is useful on the result. |
| 1304 | OpNode->setPredicateFn(""); |
| 1305 | |
| 1306 | // Promote the xform function to be an explicit node if set. |
| 1307 | if (Record *Xform = OpNode->getTransformFn()) { |
| 1308 | OpNode->setTransformFn(0); |
| 1309 | std::vector<TreePatternNode*> Children; |
| 1310 | Children.push_back(OpNode); |
| 1311 | OpNode = new TreePatternNode(Xform, Children); |
| 1312 | } |
| 1313 | |
| 1314 | ResultNodeOperands.push_back(OpNode); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 1317 | if (!InstInputsCheck.empty()) |
| 1318 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 1319 | " occurs in pattern but not in operands list!"); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1320 | |
| 1321 | TreePatternNode *ResultPattern = |
| 1322 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Create and insert the instruction. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1325 | DAGInstruction TheInst(I, Results, Operands, InstImpResults, InstImpInputs); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1326 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
| 1327 | |
| 1328 | // Use a temporary tree pattern to infer all types and make sure that the |
| 1329 | // constructed result is correct. This depends on the instruction already |
| 1330 | // being inserted into the Instructions map. |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1331 | TreePattern Temp(I->getRecord(), ResultPattern, false, *this); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1332 | Temp.InferAllTypes(); |
| 1333 | |
| 1334 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
| 1335 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 1336 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 1337 | DEBUG(I->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1338 | } |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 1340 | // If we can, convert the instructions to be patterns that are matched! |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 1341 | for (std::map<Record*, DAGInstruction>::iterator II = Instructions.begin(), |
| 1342 | E = Instructions.end(); II != E; ++II) { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1343 | DAGInstruction &TheInst = II->second; |
| 1344 | TreePattern *I = TheInst.getPattern(); |
Chris Lattner | 0c0cfa7 | 2005-10-19 01:27:22 +0000 | [diff] [blame] | 1345 | if (I == 0) continue; // No pattern. |
Evan Cheng | dd304dd | 2005-12-05 23:08:55 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1347 | if (I->getNumTrees() != 1) { |
| 1348 | std::cerr << "CANNOT HANDLE: " << I->getRecord()->getName() << " yet!"; |
| 1349 | continue; |
| 1350 | } |
| 1351 | TreePatternNode *Pattern = I->getTree(0); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1352 | TreePatternNode *SrcPattern; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1353 | if (Pattern->getOperator()->getName() == "set") { |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1354 | if (Pattern->getNumChildren() != 2) |
| 1355 | continue; // Not a set of a single value (not handled so far) |
| 1356 | |
| 1357 | SrcPattern = Pattern->getChild(1)->clone(); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1358 | } else{ |
| 1359 | // Not a set (store or something?) |
| 1360 | SrcPattern = Pattern; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1361 | } |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1362 | |
| 1363 | std::string Reason; |
| 1364 | if (!SrcPattern->canPatternMatch(Reason, *this)) |
| 1365 | I->error("Instruction can never match: " + Reason); |
| 1366 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1367 | Record *Instr = II->first; |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 1368 | TreePatternNode *DstPattern = TheInst.getResultPattern(); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1369 | PatternsToMatch. |
| 1370 | push_back(PatternToMatch(Instr->getValueAsListInit("Predicates"), |
| 1371 | SrcPattern, DstPattern)); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1372 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1375 | void DAGISelEmitter::ParsePatterns() { |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1376 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1377 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1378 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 1379 | DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch"); |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1380 | TreePattern *Pattern = new TreePattern(Patterns[i], Tree, true, *this); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1381 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1382 | // Inline pattern fragments into it. |
| 1383 | Pattern->InlinePatternFragments(); |
| 1384 | |
| 1385 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1386 | // never do anything with this pattern: report it to the user. |
| 1387 | if (!Pattern->InferAllTypes()) |
| 1388 | Pattern->error("Could not infer all types in pattern!"); |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1389 | |
| 1390 | // Validate that the input pattern is correct. |
| 1391 | { |
| 1392 | std::map<std::string, TreePatternNode*> InstInputs; |
| 1393 | std::map<std::string, Record*> InstResults; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1394 | std::vector<Record*> InstImpInputs; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1395 | std::vector<Record*> InstImpResults; |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1396 | FindPatternInputsAndOutputs(Pattern, Pattern->getOnlyTree(), |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 1397 | InstInputs, InstResults, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1398 | InstImpInputs, InstImpResults); |
Chris Lattner | 09c0339 | 2005-11-17 17:43:52 +0000 | [diff] [blame] | 1399 | } |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1400 | |
| 1401 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 1402 | if (LI->getSize() == 0) continue; // no pattern. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Parse the instruction. |
Chris Lattner | edbd871 | 2005-10-21 01:19:59 +0000 | [diff] [blame] | 1405 | TreePattern *Result = new TreePattern(Patterns[i], LI, false, *this); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Inline pattern fragments into it. |
| 1408 | Result->InlinePatternFragments(); |
| 1409 | |
| 1410 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 1411 | // never do anything with this pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1412 | if (!Result->InferAllTypes()) |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 1413 | Result->error("Could not infer all types in pattern result!"); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (Result->getNumTrees() != 1) |
| 1416 | Result->error("Cannot handle instructions producing instructions " |
| 1417 | "with temporaries yet!"); |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1418 | |
| 1419 | std::string Reason; |
| 1420 | if (!Pattern->getOnlyTree()->canPatternMatch(Reason, *this)) |
| 1421 | Pattern->error("Pattern can never match: " + Reason); |
| 1422 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1423 | PatternsToMatch. |
| 1424 | push_back(PatternToMatch(Patterns[i]->getValueAsListInit("Predicates"), |
| 1425 | Pattern->getOnlyTree(), |
| 1426 | Result->getOnlyTree())); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 1427 | } |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1430 | /// CombineChildVariants - Given a bunch of permutations of each child of the |
| 1431 | /// 'operator' node, put them together in all possible ways. |
| 1432 | static void CombineChildVariants(TreePatternNode *Orig, |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1433 | const std::vector<std::vector<TreePatternNode*> > &ChildVariants, |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1434 | std::vector<TreePatternNode*> &OutVariants, |
| 1435 | DAGISelEmitter &ISE) { |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1436 | // Make sure that each operand has at least one variant to choose from. |
| 1437 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1438 | if (ChildVariants[i].empty()) |
| 1439 | return; |
| 1440 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1441 | // The end result is an all-pairs construction of the resultant pattern. |
| 1442 | std::vector<unsigned> Idxs; |
| 1443 | Idxs.resize(ChildVariants.size()); |
| 1444 | bool NotDone = true; |
| 1445 | while (NotDone) { |
| 1446 | // Create the variant and add it to the output list. |
| 1447 | std::vector<TreePatternNode*> NewChildren; |
| 1448 | for (unsigned i = 0, e = ChildVariants.size(); i != e; ++i) |
| 1449 | NewChildren.push_back(ChildVariants[i][Idxs[i]]); |
| 1450 | TreePatternNode *R = new TreePatternNode(Orig->getOperator(), NewChildren); |
| 1451 | |
| 1452 | // Copy over properties. |
| 1453 | R->setName(Orig->getName()); |
| 1454 | R->setPredicateFn(Orig->getPredicateFn()); |
| 1455 | R->setTransformFn(Orig->getTransformFn()); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1456 | R->setTypes(Orig->getExtTypes()); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If this pattern cannot every match, do not include it as a variant. |
| 1459 | std::string ErrString; |
| 1460 | if (!R->canPatternMatch(ErrString, ISE)) { |
| 1461 | delete R; |
| 1462 | } else { |
| 1463 | bool AlreadyExists = false; |
| 1464 | |
| 1465 | // Scan to see if this pattern has already been emitted. We can get |
| 1466 | // duplication due to things like commuting: |
| 1467 | // (and GPRC:$a, GPRC:$b) -> (and GPRC:$b, GPRC:$a) |
| 1468 | // which are the same pattern. Ignore the dups. |
| 1469 | for (unsigned i = 0, e = OutVariants.size(); i != e; ++i) |
| 1470 | if (R->isIsomorphicTo(OutVariants[i])) { |
| 1471 | AlreadyExists = true; |
| 1472 | break; |
| 1473 | } |
| 1474 | |
| 1475 | if (AlreadyExists) |
| 1476 | delete R; |
| 1477 | else |
| 1478 | OutVariants.push_back(R); |
| 1479 | } |
| 1480 | |
| 1481 | // Increment indices to the next permutation. |
| 1482 | NotDone = false; |
| 1483 | // Look for something we can increment without causing a wrap-around. |
| 1484 | for (unsigned IdxsIdx = 0; IdxsIdx != Idxs.size(); ++IdxsIdx) { |
| 1485 | if (++Idxs[IdxsIdx] < ChildVariants[IdxsIdx].size()) { |
| 1486 | NotDone = true; // Found something to increment. |
| 1487 | break; |
| 1488 | } |
| 1489 | Idxs[IdxsIdx] = 0; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1494 | /// CombineChildVariants - A helper function for binary operators. |
| 1495 | /// |
| 1496 | static void CombineChildVariants(TreePatternNode *Orig, |
| 1497 | const std::vector<TreePatternNode*> &LHS, |
| 1498 | const std::vector<TreePatternNode*> &RHS, |
| 1499 | std::vector<TreePatternNode*> &OutVariants, |
| 1500 | DAGISelEmitter &ISE) { |
| 1501 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 1502 | ChildVariants.push_back(LHS); |
| 1503 | ChildVariants.push_back(RHS); |
| 1504 | CombineChildVariants(Orig, ChildVariants, OutVariants, ISE); |
| 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | static void GatherChildrenOfAssociativeOpcode(TreePatternNode *N, |
| 1509 | std::vector<TreePatternNode *> &Children) { |
| 1510 | assert(N->getNumChildren()==2 &&"Associative but doesn't have 2 children!"); |
| 1511 | Record *Operator = N->getOperator(); |
| 1512 | |
| 1513 | // Only permit raw nodes. |
| 1514 | if (!N->getName().empty() || !N->getPredicateFn().empty() || |
| 1515 | N->getTransformFn()) { |
| 1516 | Children.push_back(N); |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | if (N->getChild(0)->isLeaf() || N->getChild(0)->getOperator() != Operator) |
| 1521 | Children.push_back(N->getChild(0)); |
| 1522 | else |
| 1523 | GatherChildrenOfAssociativeOpcode(N->getChild(0), Children); |
| 1524 | |
| 1525 | if (N->getChild(1)->isLeaf() || N->getChild(1)->getOperator() != Operator) |
| 1526 | Children.push_back(N->getChild(1)); |
| 1527 | else |
| 1528 | GatherChildrenOfAssociativeOpcode(N->getChild(1), Children); |
| 1529 | } |
| 1530 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1531 | /// GenerateVariantsOf - Given a pattern N, generate all permutations we can of |
| 1532 | /// the (potentially recursive) pattern by using algebraic laws. |
| 1533 | /// |
| 1534 | static void GenerateVariantsOf(TreePatternNode *N, |
| 1535 | std::vector<TreePatternNode*> &OutVariants, |
| 1536 | DAGISelEmitter &ISE) { |
| 1537 | // We cannot permute leaves. |
| 1538 | if (N->isLeaf()) { |
| 1539 | OutVariants.push_back(N); |
| 1540 | return; |
| 1541 | } |
| 1542 | |
| 1543 | // Look up interesting info about the node. |
| 1544 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(N->getOperator()); |
| 1545 | |
| 1546 | // If this node is associative, reassociate. |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1547 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPAssociative)) { |
| 1548 | // Reassociate by pulling together all of the linked operators |
| 1549 | std::vector<TreePatternNode*> MaximalChildren; |
| 1550 | GatherChildrenOfAssociativeOpcode(N, MaximalChildren); |
| 1551 | |
| 1552 | // Only handle child sizes of 3. Otherwise we'll end up trying too many |
| 1553 | // permutations. |
| 1554 | if (MaximalChildren.size() == 3) { |
| 1555 | // Find the variants of all of our maximal children. |
| 1556 | std::vector<TreePatternNode*> AVariants, BVariants, CVariants; |
| 1557 | GenerateVariantsOf(MaximalChildren[0], AVariants, ISE); |
| 1558 | GenerateVariantsOf(MaximalChildren[1], BVariants, ISE); |
| 1559 | GenerateVariantsOf(MaximalChildren[2], CVariants, ISE); |
| 1560 | |
| 1561 | // There are only two ways we can permute the tree: |
| 1562 | // (A op B) op C and A op (B op C) |
| 1563 | // Within these forms, we can also permute A/B/C. |
| 1564 | |
| 1565 | // Generate legal pair permutations of A/B/C. |
| 1566 | std::vector<TreePatternNode*> ABVariants; |
| 1567 | std::vector<TreePatternNode*> BAVariants; |
| 1568 | std::vector<TreePatternNode*> ACVariants; |
| 1569 | std::vector<TreePatternNode*> CAVariants; |
| 1570 | std::vector<TreePatternNode*> BCVariants; |
| 1571 | std::vector<TreePatternNode*> CBVariants; |
| 1572 | CombineChildVariants(N, AVariants, BVariants, ABVariants, ISE); |
| 1573 | CombineChildVariants(N, BVariants, AVariants, BAVariants, ISE); |
| 1574 | CombineChildVariants(N, AVariants, CVariants, ACVariants, ISE); |
| 1575 | CombineChildVariants(N, CVariants, AVariants, CAVariants, ISE); |
| 1576 | CombineChildVariants(N, BVariants, CVariants, BCVariants, ISE); |
| 1577 | CombineChildVariants(N, CVariants, BVariants, CBVariants, ISE); |
| 1578 | |
| 1579 | // Combine those into the result: (x op x) op x |
| 1580 | CombineChildVariants(N, ABVariants, CVariants, OutVariants, ISE); |
| 1581 | CombineChildVariants(N, BAVariants, CVariants, OutVariants, ISE); |
| 1582 | CombineChildVariants(N, ACVariants, BVariants, OutVariants, ISE); |
| 1583 | CombineChildVariants(N, CAVariants, BVariants, OutVariants, ISE); |
| 1584 | CombineChildVariants(N, BCVariants, AVariants, OutVariants, ISE); |
| 1585 | CombineChildVariants(N, CBVariants, AVariants, OutVariants, ISE); |
| 1586 | |
| 1587 | // Combine those into the result: x op (x op x) |
| 1588 | CombineChildVariants(N, CVariants, ABVariants, OutVariants, ISE); |
| 1589 | CombineChildVariants(N, CVariants, BAVariants, OutVariants, ISE); |
| 1590 | CombineChildVariants(N, BVariants, ACVariants, OutVariants, ISE); |
| 1591 | CombineChildVariants(N, BVariants, CAVariants, OutVariants, ISE); |
| 1592 | CombineChildVariants(N, AVariants, BCVariants, OutVariants, ISE); |
| 1593 | CombineChildVariants(N, AVariants, CBVariants, OutVariants, ISE); |
| 1594 | return; |
| 1595 | } |
| 1596 | } |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1597 | |
| 1598 | // Compute permutations of all children. |
| 1599 | std::vector<std::vector<TreePatternNode*> > ChildVariants; |
| 1600 | ChildVariants.resize(N->getNumChildren()); |
| 1601 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 1602 | GenerateVariantsOf(N->getChild(i), ChildVariants[i], ISE); |
| 1603 | |
| 1604 | // Build all permutations based on how the children were formed. |
| 1605 | CombineChildVariants(N, ChildVariants, OutVariants, ISE); |
| 1606 | |
| 1607 | // If this node is commutative, consider the commuted order. |
| 1608 | if (NodeInfo.hasProperty(SDNodeInfo::SDNPCommutative)) { |
| 1609 | assert(N->getNumChildren()==2 &&"Commutative but doesn't have 2 children!"); |
Chris Lattner | af30291 | 2005-09-29 22:36:54 +0000 | [diff] [blame] | 1610 | // Consider the commuted order. |
| 1611 | CombineChildVariants(N, ChildVariants[1], ChildVariants[0], |
| 1612 | OutVariants, ISE); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1617 | // GenerateVariants - Generate variants. For example, commutative patterns can |
| 1618 | // match multiple ways. Add them to PatternsToMatch as well. |
| 1619 | void DAGISelEmitter::GenerateVariants() { |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1620 | |
| 1621 | DEBUG(std::cerr << "Generating instruction variants.\n"); |
| 1622 | |
| 1623 | // Loop over all of the patterns we've collected, checking to see if we can |
| 1624 | // generate variants of the instruction, through the exploitation of |
| 1625 | // identities. This permits the target to provide agressive matching without |
| 1626 | // the .td file having to contain tons of variants of instructions. |
| 1627 | // |
| 1628 | // Note that this loop adds new patterns to the PatternsToMatch list, but we |
| 1629 | // intentionally do not reconsider these. Any variants of added patterns have |
| 1630 | // already been added. |
| 1631 | // |
| 1632 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 1633 | std::vector<TreePatternNode*> Variants; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1634 | GenerateVariantsOf(PatternsToMatch[i].getSrcPattern(), Variants, *this); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1635 | |
| 1636 | assert(!Variants.empty() && "Must create at least original variant!"); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1637 | Variants.erase(Variants.begin()); // Remove the original pattern. |
| 1638 | |
| 1639 | if (Variants.empty()) // No variants for this pattern. |
| 1640 | continue; |
| 1641 | |
| 1642 | DEBUG(std::cerr << "FOUND VARIANTS OF: "; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1643 | PatternsToMatch[i].getSrcPattern()->dump(); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1644 | std::cerr << "\n"); |
| 1645 | |
| 1646 | for (unsigned v = 0, e = Variants.size(); v != e; ++v) { |
| 1647 | TreePatternNode *Variant = Variants[v]; |
| 1648 | |
| 1649 | DEBUG(std::cerr << " VAR#" << v << ": "; |
| 1650 | Variant->dump(); |
| 1651 | std::cerr << "\n"); |
| 1652 | |
| 1653 | // Scan to see if an instruction or explicit pattern already matches this. |
| 1654 | bool AlreadyExists = false; |
| 1655 | for (unsigned p = 0, e = PatternsToMatch.size(); p != e; ++p) { |
| 1656 | // Check to see if this variant already exists. |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1657 | if (Variant->isIsomorphicTo(PatternsToMatch[p].getSrcPattern())) { |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1658 | DEBUG(std::cerr << " *** ALREADY EXISTS, ignoring variant.\n"); |
| 1659 | AlreadyExists = true; |
| 1660 | break; |
| 1661 | } |
| 1662 | } |
| 1663 | // If we already have it, ignore the variant. |
| 1664 | if (AlreadyExists) continue; |
| 1665 | |
| 1666 | // Otherwise, add it to the list of patterns we have. |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1667 | PatternsToMatch. |
| 1668 | push_back(PatternToMatch(PatternsToMatch[i].getPredicates(), |
| 1669 | Variant, PatternsToMatch[i].getDstPattern())); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | DEBUG(std::cerr << "\n"); |
| 1673 | } |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Chris Lattner | 7cf2fe6 | 2005-09-28 20:58:06 +0000 | [diff] [blame] | 1676 | |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1677 | // NodeIsComplexPattern - return true if N is a leaf node and a subclass of |
| 1678 | // ComplexPattern. |
| 1679 | static bool NodeIsComplexPattern(TreePatternNode *N) |
| 1680 | { |
| 1681 | return (N->isLeaf() && |
| 1682 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 1683 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 1684 | isSubClassOf("ComplexPattern")); |
| 1685 | } |
| 1686 | |
| 1687 | // NodeGetComplexPattern - return the pointer to the ComplexPattern if N |
| 1688 | // is a leaf node and a subclass of ComplexPattern, else it returns NULL. |
| 1689 | static const ComplexPattern *NodeGetComplexPattern(TreePatternNode *N, |
| 1690 | DAGISelEmitter &ISE) |
| 1691 | { |
| 1692 | if (N->isLeaf() && |
| 1693 | dynamic_cast<DefInit*>(N->getLeafValue()) && |
| 1694 | static_cast<DefInit*>(N->getLeafValue())->getDef()-> |
| 1695 | isSubClassOf("ComplexPattern")) { |
| 1696 | return &ISE.getComplexPattern(static_cast<DefInit*>(N->getLeafValue()) |
| 1697 | ->getDef()); |
| 1698 | } |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1702 | /// getPatternSize - Return the 'size' of this pattern. We want to match large |
| 1703 | /// patterns before small ones. This is used to determine the size of a |
| 1704 | /// pattern. |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1705 | static unsigned getPatternSize(TreePatternNode *P, DAGISelEmitter &ISE) { |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1706 | assert(isExtIntegerInVTs(P->getExtTypes()) || |
| 1707 | isExtFloatingPointInVTs(P->getExtTypes()) || |
| 1708 | P->getExtTypeNum(0) == MVT::isVoid || |
| 1709 | P->getExtTypeNum(0) == MVT::Flag && |
| 1710 | "Not a valid pattern node to size!"); |
Evan Cheng | e1050d6 | 2006-01-06 02:30:23 +0000 | [diff] [blame] | 1711 | unsigned Size = 2; // The node itself. |
Evan Cheng | 657416c | 2006-02-01 06:06:31 +0000 | [diff] [blame] | 1712 | // If the root node is a ConstantSDNode, increases its size. |
| 1713 | // e.g. (set R32:$dst, 0). |
| 1714 | if (P->isLeaf() && dynamic_cast<IntInit*>(P->getLeafValue())) |
| 1715 | Size++; |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1716 | |
| 1717 | // FIXME: This is a hack to statically increase the priority of patterns |
| 1718 | // which maps a sub-dag to a complex pattern. e.g. favors LEA over ADD. |
| 1719 | // Later we can allow complexity / cost for each pattern to be (optionally) |
| 1720 | // specified. To get best possible pattern match we'll need to dynamically |
| 1721 | // calculate the complexity of all patterns a dag can potentially map to. |
| 1722 | const ComplexPattern *AM = NodeGetComplexPattern(P, ISE); |
| 1723 | if (AM) |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1724 | Size += AM->getNumOperands() * 2; |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1725 | |
| 1726 | // If this node has some predicate function that must match, it adds to the |
| 1727 | // complexity of this node. |
| 1728 | if (!P->getPredicateFn().empty()) |
| 1729 | ++Size; |
| 1730 | |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1731 | // Count children in the count if they are also nodes. |
| 1732 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
| 1733 | TreePatternNode *Child = P->getChild(i); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1734 | if (!Child->isLeaf() && Child->getExtTypeNum(0) != MVT::Other) |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1735 | Size += getPatternSize(Child, ISE); |
| 1736 | else if (Child->isLeaf()) { |
| 1737 | if (dynamic_cast<IntInit*>(Child->getLeafValue())) |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1738 | Size += 3; // Matches a ConstantSDNode (+2) and a specific value (+1). |
Evan Cheng | 4a7c284 | 2006-01-06 22:19:44 +0000 | [diff] [blame] | 1739 | else if (NodeIsComplexPattern(Child)) |
| 1740 | Size += getPatternSize(Child, ISE); |
Chris Lattner | 3e17980 | 2006-02-03 18:06:02 +0000 | [diff] [blame] | 1741 | else if (!Child->getPredicateFn().empty()) |
| 1742 | ++Size; |
Chris Lattner | 2f041d4 | 2005-10-19 04:41:05 +0000 | [diff] [blame] | 1743 | } |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | return Size; |
| 1747 | } |
| 1748 | |
| 1749 | /// getResultPatternCost - Compute the number of instructions for this pattern. |
| 1750 | /// This is a temporary hack. We should really include the instruction |
| 1751 | /// latencies in this calculation. |
| 1752 | static unsigned getResultPatternCost(TreePatternNode *P) { |
| 1753 | if (P->isLeaf()) return 0; |
| 1754 | |
| 1755 | unsigned Cost = P->getOperator()->isSubClassOf("Instruction"); |
| 1756 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) |
| 1757 | Cost += getResultPatternCost(P->getChild(i)); |
| 1758 | return Cost; |
| 1759 | } |
| 1760 | |
| 1761 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 1762 | // In particular, we want to match maximal patterns first and lowest cost within |
| 1763 | // a particular complexity first. |
| 1764 | struct PatternSortingPredicate { |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 1765 | PatternSortingPredicate(DAGISelEmitter &ise) : ISE(ise) {}; |
| 1766 | DAGISelEmitter &ISE; |
| 1767 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1768 | bool operator()(PatternToMatch *LHS, |
| 1769 | PatternToMatch *RHS) { |
| 1770 | unsigned LHSSize = getPatternSize(LHS->getSrcPattern(), ISE); |
| 1771 | unsigned RHSSize = getPatternSize(RHS->getSrcPattern(), ISE); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1772 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 1773 | if (LHSSize < RHSSize) return false; |
| 1774 | |
| 1775 | // If the patterns have equal complexity, compare generated instruction cost |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1776 | return getResultPatternCost(LHS->getDstPattern()) < |
| 1777 | getResultPatternCost(RHS->getDstPattern()); |
Chris Lattner | 05814af | 2005-09-28 17:57:56 +0000 | [diff] [blame] | 1778 | } |
| 1779 | }; |
| 1780 | |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 1781 | /// getRegisterValueType - Look up and return the first ValueType of specified |
| 1782 | /// RegisterClass record |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 1783 | static MVT::ValueType getRegisterValueType(Record *R, const CodeGenTarget &T) { |
Chris Lattner | 22faeab | 2005-12-05 02:36:37 +0000 | [diff] [blame] | 1784 | if (const CodeGenRegisterClass *RC = T.getRegisterClassForRegister(R)) |
| 1785 | return RC->getValueTypeNum(0); |
Evan Cheng | 66a48bb | 2005-12-01 00:18:45 +0000 | [diff] [blame] | 1786 | return MVT::Other; |
| 1787 | } |
| 1788 | |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1790 | /// RemoveAllTypes - A quick recursive walk over a pattern which removes all |
| 1791 | /// type information from it. |
| 1792 | static void RemoveAllTypes(TreePatternNode *N) { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 1793 | N->removeTypes(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 1794 | if (!N->isLeaf()) |
| 1795 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 1796 | RemoveAllTypes(N->getChild(i)); |
| 1797 | } |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1798 | |
Chris Lattner | 0614b62 | 2005-11-02 06:49:14 +0000 | [diff] [blame] | 1799 | Record *DAGISelEmitter::getSDNodeNamed(const std::string &Name) const { |
| 1800 | Record *N = Records.getDef(Name); |
| 1801 | assert(N && N->isSubClassOf("SDNode") && "Bad argument"); |
| 1802 | return N; |
| 1803 | } |
| 1804 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1805 | /// NodeHasProperty - return true if TreePatternNode has the specified |
| 1806 | /// property. |
| 1807 | static bool NodeHasProperty(TreePatternNode *N, SDNodeInfo::SDNP Property, |
| 1808 | DAGISelEmitter &ISE) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1809 | { |
| 1810 | if (N->isLeaf()) return false; |
| 1811 | Record *Operator = N->getOperator(); |
| 1812 | if (!Operator->isSubClassOf("SDNode")) return false; |
| 1813 | |
| 1814 | const SDNodeInfo &NodeInfo = ISE.getSDNodeInfo(Operator); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1815 | return NodeInfo.hasProperty(Property); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1818 | static bool PatternHasProperty(TreePatternNode *N, SDNodeInfo::SDNP Property, |
| 1819 | DAGISelEmitter &ISE) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1820 | { |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1821 | if (NodeHasProperty(N, Property, ISE)) |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1822 | return true; |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 1823 | |
| 1824 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 1825 | TreePatternNode *Child = N->getChild(i); |
| 1826 | if (PatternHasProperty(Child, Property, ISE)) |
| 1827 | return true; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | return false; |
| 1831 | } |
| 1832 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1833 | class PatternCodeEmitter { |
| 1834 | private: |
| 1835 | DAGISelEmitter &ISE; |
| 1836 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1837 | // Predicates. |
| 1838 | ListInit *Predicates; |
| 1839 | // Instruction selector pattern. |
| 1840 | TreePatternNode *Pattern; |
| 1841 | // Matched instruction. |
| 1842 | TreePatternNode *Instruction; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1843 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1844 | // Node to name mapping |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 1845 | std::map<std::string, std::string> VariableMap; |
| 1846 | // Node to operator mapping |
| 1847 | std::map<std::string, Record*> OperatorMap; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1848 | // Names of all the folded nodes which produce chains. |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 1849 | std::vector<std::pair<std::string, unsigned> > FoldedChains; |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 1850 | std::set<std::string> Duplicates; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1851 | |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1852 | /// GeneratedCode - This is the buffer that we emit code to. The first bool |
| 1853 | /// indicates whether this is an exit predicate (something that should be |
| 1854 | /// tested, and if true, the match fails) [when true] or normal code to emit |
| 1855 | /// [when false]. |
| 1856 | std::vector<std::pair<bool, std::string> > &GeneratedCode; |
| 1857 | |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 1858 | std::string ChainName; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1859 | unsigned TmpNo; |
| 1860 | |
| 1861 | void emitCheck(const std::string &S) { |
| 1862 | if (!S.empty()) |
| 1863 | GeneratedCode.push_back(std::make_pair(true, S)); |
| 1864 | } |
| 1865 | void emitCode(const std::string &S) { |
| 1866 | if (!S.empty()) |
| 1867 | GeneratedCode.push_back(std::make_pair(false, S)); |
| 1868 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1869 | public: |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1870 | PatternCodeEmitter(DAGISelEmitter &ise, ListInit *preds, |
| 1871 | TreePatternNode *pattern, TreePatternNode *instr, |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1872 | std::vector<std::pair<bool, std::string> > &gc) |
| 1873 | : ISE(ise), Predicates(preds), Pattern(pattern), Instruction(instr), |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 1874 | GeneratedCode(gc), TmpNo(0) {} |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1875 | |
| 1876 | /// EmitMatchCode - Emit a matcher for N, going to the label for PatternNo |
| 1877 | /// if the match fails. At this point, we already know that the opcode for N |
| 1878 | /// matches, and the SDNode for the result has the RootName specified name. |
| 1879 | void EmitMatchCode(TreePatternNode *N, const std::string &RootName, |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 1880 | const std::string &ChainSuffix, bool &FoundChain, |
| 1881 | bool isRoot = false) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Emit instruction predicates. Each predicate is just a string for now. |
| 1884 | if (isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1885 | std::string PredicateCheck; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1886 | for (unsigned i = 0, e = Predicates->getSize(); i != e; ++i) { |
| 1887 | if (DefInit *Pred = dynamic_cast<DefInit*>(Predicates->getElement(i))) { |
| 1888 | Record *Def = Pred->getDef(); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1889 | if (!Def->isSubClassOf("Predicate")) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1890 | Def->dump(); |
| 1891 | assert(0 && "Unknown predicate type!"); |
| 1892 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1893 | if (!PredicateCheck.empty()) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1894 | PredicateCheck += " || "; |
| 1895 | PredicateCheck += "(" + Def->getValueAsString("CondString") + ")"; |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1896 | } |
| 1897 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1898 | |
| 1899 | emitCheck(PredicateCheck); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1902 | if (N->isLeaf()) { |
| 1903 | if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1904 | emitCheck("cast<ConstantSDNode>(" + RootName + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1905 | ")->getSignExtended() == " + itostr(II->getValue())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1906 | return; |
| 1907 | } else if (!NodeIsComplexPattern(N)) { |
| 1908 | assert(0 && "Cannot match this as a leaf value!"); |
| 1909 | abort(); |
| 1910 | } |
| 1911 | } |
| 1912 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 1913 | // If this node has a name associated with it, capture it in VariableMap. If |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1914 | // we already saw this in the pattern, emit code to verify dagness. |
| 1915 | if (!N->getName().empty()) { |
| 1916 | std::string &VarMapEntry = VariableMap[N->getName()]; |
| 1917 | if (VarMapEntry.empty()) { |
| 1918 | VarMapEntry = RootName; |
| 1919 | } else { |
| 1920 | // If we get here, this is a second reference to a specific name. Since |
| 1921 | // we already have checked that the first reference is valid, we don't |
| 1922 | // have to recursively match it, just check that it's the same as the |
| 1923 | // previously named thing. |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1924 | emitCheck(VarMapEntry + " == " + RootName); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1925 | return; |
| 1926 | } |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 1927 | |
| 1928 | if (!N->isLeaf()) |
| 1929 | OperatorMap[N->getName()] = N->getOperator(); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | |
| 1933 | // Emit code to load the child nodes and match their contents recursively. |
| 1934 | unsigned OpNo = 0; |
Evan Cheng | 76356d9 | 2006-01-20 01:11:03 +0000 | [diff] [blame] | 1935 | bool NodeHasChain = NodeHasProperty(N, SDNodeInfo::SDNPHasChain, ISE); |
| 1936 | bool HasChain = PatternHasProperty(N, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1937 | bool EmittedUseCheck = false; |
| 1938 | bool EmittedSlctedCheck = false; |
Evan Cheng | 8621789 | 2005-12-12 19:37:43 +0000 | [diff] [blame] | 1939 | if (HasChain) { |
Evan Cheng | 76356d9 | 2006-01-20 01:11:03 +0000 | [diff] [blame] | 1940 | if (NodeHasChain) |
| 1941 | OpNo = 1; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1942 | if (!isRoot) { |
Evan Cheng | 1129e87 | 2005-12-10 00:09:17 +0000 | [diff] [blame] | 1943 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(N->getOperator()); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1944 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1945 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1946 | EmittedUseCheck = true; |
| 1947 | // hasOneUse() check is not strong enough. If the original node has |
| 1948 | // already been selected, it may have been replaced with another. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1949 | for (unsigned j = 0; j != CInfo.getNumResults(); j++) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1950 | emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1951 | "))"); |
| 1952 | |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1953 | EmittedSlctedCheck = true; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1954 | if (NodeHasChain) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1955 | emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1956 | utostr(CInfo.getNumResults()) + "))"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1957 | } |
Evan Cheng | c15d18c | 2006-01-27 22:13:45 +0000 | [diff] [blame] | 1958 | if (NodeHasChain) { |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 1959 | if (FoundChain) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1960 | emitCheck("Chain.Val == " + RootName + ".Val"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 1961 | else |
| 1962 | FoundChain = true; |
| 1963 | ChainName = "Chain" + ChainSuffix; |
| 1964 | emitCode("SDOperand " + ChainName + " = " + RootName + |
| 1965 | ".getOperand(0);"); |
Evan Cheng | 1cf6db2 | 2006-01-06 00:41:12 +0000 | [diff] [blame] | 1966 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1969 | // Don't fold any node which reads or writes a flag and has multiple uses. |
| 1970 | // FIXME: we really need to separate the concepts of flag and "glue". Those |
| 1971 | // real flag results, e.g. X86CMP output, can have multiple uses. |
Evan Cheng | 8eab360 | 2006-01-26 02:13:31 +0000 | [diff] [blame] | 1972 | // FIXME: If the incoming flag is optional. Then it is ok to fold it. |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1973 | if (!isRoot && |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1974 | (PatternHasProperty(N, SDNodeInfo::SDNPInFlag, ISE) || |
| 1975 | PatternHasProperty(N, SDNodeInfo::SDNPOptInFlag, ISE) || |
| 1976 | PatternHasProperty(N, SDNodeInfo::SDNPOutFlag, ISE))) { |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1977 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(N->getOperator()); |
| 1978 | if (!EmittedUseCheck) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1979 | // Multiple uses of actual result? |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1980 | emitCheck(RootName + ".hasOneUse()"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1981 | } |
Evan Cheng | 1feeeec | 2006-01-26 19:13:45 +0000 | [diff] [blame] | 1982 | if (!EmittedSlctedCheck) |
| 1983 | // hasOneUse() check is not strong enough. If the original node has |
| 1984 | // already been selected, it may have been replaced with another. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1985 | for (unsigned j = 0; j < CInfo.getNumResults(); j++) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1986 | emitCheck("!CodeGenMap.count(" + RootName + ".getValue(" + utostr(j) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1987 | "))"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1990 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1991 | emitCode("SDOperand " + RootName + utostr(OpNo) + " = " + |
| 1992 | RootName + ".getOperand(" +utostr(OpNo) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 1993 | TreePatternNode *Child = N->getChild(i); |
| 1994 | |
| 1995 | if (!Child->isLeaf()) { |
| 1996 | // If it's not a leaf, recursively match. |
| 1997 | const SDNodeInfo &CInfo = ISE.getSDNodeInfo(Child->getOperator()); |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 1998 | emitCheck(RootName + utostr(OpNo) + ".getOpcode() == " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 1999 | CInfo.getEnumName()); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2000 | EmitMatchCode(Child, RootName + utostr(OpNo), ChainSuffix + utostr(OpNo), |
| 2001 | FoundChain); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2002 | if (NodeHasProperty(Child, SDNodeInfo::SDNPHasChain, ISE)) |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2003 | FoldedChains.push_back(std::make_pair(RootName + utostr(OpNo), |
| 2004 | CInfo.getNumResults())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2005 | } else { |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2006 | // If this child has a name associated with it, capture it in VarMap. If |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2007 | // we already saw this in the pattern, emit code to verify dagness. |
| 2008 | if (!Child->getName().empty()) { |
| 2009 | std::string &VarMapEntry = VariableMap[Child->getName()]; |
| 2010 | if (VarMapEntry.empty()) { |
| 2011 | VarMapEntry = RootName + utostr(OpNo); |
| 2012 | } else { |
Jeff Cohen | 9b0ffca | 2006-01-27 22:22:28 +0000 | [diff] [blame] | 2013 | // If we get here, this is a second reference to a specific name. |
| 2014 | // Since we already have checked that the first reference is valid, |
| 2015 | // we don't have to recursively match it, just check that it's the |
| 2016 | // same as the previously named thing. |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2017 | emitCheck(VarMapEntry + " == " + RootName + utostr(OpNo)); |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 2018 | Duplicates.insert(RootName + utostr(OpNo)); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2019 | continue; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | // Handle leaves of various types. |
| 2024 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
| 2025 | Record *LeafRec = DI->getDef(); |
| 2026 | if (LeafRec->isSubClassOf("RegisterClass")) { |
| 2027 | // Handle register references. Nothing to do here. |
| 2028 | } else if (LeafRec->isSubClassOf("Register")) { |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2029 | // Handle register references. |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2030 | } else if (LeafRec->isSubClassOf("ComplexPattern")) { |
| 2031 | // Handle complex pattern. Nothing to do here. |
Evan Cheng | 01f318b | 2005-12-14 02:21:57 +0000 | [diff] [blame] | 2032 | } else if (LeafRec->getName() == "srcvalue") { |
| 2033 | // Place holder for SRCVALUE nodes. Nothing to do here. |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2034 | } else if (LeafRec->isSubClassOf("ValueType")) { |
| 2035 | // Make sure this is the specified value type. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2036 | emitCheck("cast<VTSDNode>(" + RootName + utostr(OpNo) + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2037 | ")->getVT() == MVT::" + LeafRec->getName()); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2038 | } else if (LeafRec->isSubClassOf("CondCode")) { |
| 2039 | // Make sure this is the specified cond code. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2040 | emitCheck("cast<CondCodeSDNode>(" + RootName + utostr(OpNo) + |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2041 | ")->get() == ISD::" + LeafRec->getName()); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2042 | } else { |
| 2043 | Child->dump(); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2044 | std::cerr << " "; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2045 | assert(0 && "Unknown leaf type!"); |
| 2046 | } |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2047 | } else if (IntInit *II = |
| 2048 | dynamic_cast<IntInit*>(Child->getLeafValue())) { |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2049 | emitCheck("isa<ConstantSDNode>(" + RootName + utostr(OpNo) + ")"); |
| 2050 | unsigned CTmp = TmpNo++; |
Andrew Lenharth | 8e51773 | 2006-01-29 05:22:37 +0000 | [diff] [blame] | 2051 | emitCode("int64_t CN"+utostr(CTmp)+" = cast<ConstantSDNode>("+ |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2052 | RootName + utostr(OpNo) + ")->getSignExtended();"); |
| 2053 | |
| 2054 | emitCheck("CN" + utostr(CTmp) + " == " +itostr(II->getValue())); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2055 | } else { |
| 2056 | Child->dump(); |
| 2057 | assert(0 && "Unknown leaf type!"); |
| 2058 | } |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | // If there is a node predicate for this, emit the call. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2063 | if (!N->getPredicateFn().empty()) |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2064 | emitCheck(N->getPredicateFn() + "(" + RootName + ".Val)"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /// EmitResultCode - Emit the action for a pattern. Now that it has matched |
| 2068 | /// we actually have to build a DAG! |
| 2069 | std::pair<unsigned, unsigned> |
| 2070 | EmitResultCode(TreePatternNode *N, bool isRoot = false) { |
| 2071 | // This is something selected from the pattern we matched. |
| 2072 | if (!N->getName().empty()) { |
| 2073 | assert(!isRoot && "Root of pattern cannot be a leaf!"); |
| 2074 | std::string &Val = VariableMap[N->getName()]; |
| 2075 | assert(!Val.empty() && |
| 2076 | "Variable referenced but not defined and not caught earlier!"); |
| 2077 | if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { |
| 2078 | // Already selected this operand, just return the tmpval. |
| 2079 | return std::make_pair(1, atoi(Val.c_str()+3)); |
| 2080 | } |
| 2081 | |
| 2082 | const ComplexPattern *CP; |
| 2083 | unsigned ResNo = TmpNo++; |
| 2084 | unsigned NumRes = 1; |
| 2085 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") { |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2086 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 2087 | std::string CastType; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2088 | switch (N->getTypeNum(0)) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2089 | default: assert(0 && "Unknown type for constant node!"); |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 2090 | case MVT::i1: CastType = "bool"; break; |
| 2091 | case MVT::i8: CastType = "unsigned char"; break; |
| 2092 | case MVT::i16: CastType = "unsigned short"; break; |
| 2093 | case MVT::i32: CastType = "unsigned"; break; |
| 2094 | case MVT::i64: CastType = "uint64_t"; break; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2095 | } |
Chris Lattner | 7859313 | 2006-01-29 20:01:35 +0000 | [diff] [blame] | 2096 | emitCode(CastType + " Tmp" + utostr(ResNo) + "C = (" + CastType + |
Andrew Lenharth | 2cba57c | 2006-01-29 05:17:22 +0000 | [diff] [blame] | 2097 | ")cast<ConstantSDNode>(" + Val + ")->getValue();"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2098 | emitCode("SDOperand Tmp" + utostr(ResNo) + |
| 2099 | " = CurDAG->getTargetConstant(Tmp" + utostr(ResNo) + |
| 2100 | "C, MVT::" + getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 2101 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2102 | Record *Op = OperatorMap[N->getName()]; |
| 2103 | // Transform ExternalSymbol to TargetExternalSymbol |
| 2104 | if (Op && Op->getName() == "externalsym") { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2105 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
| 2106 | "ExternalSymbol(cast<ExternalSymbolSDNode>(" + |
| 2107 | Val + ")->getSymbol(), MVT::" + |
| 2108 | getEnumName(N->getTypeNum(0)) + ");"); |
| 2109 | } else { |
| 2110 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = " + Val + ";"); |
| 2111 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2112 | } else if (!N->isLeaf() && N->getOperator()->getName() == "tglobaladdr") { |
Evan Cheng | f805c2e | 2006-01-12 19:35:54 +0000 | [diff] [blame] | 2113 | Record *Op = OperatorMap[N->getName()]; |
| 2114 | // Transform GlobalAddress to TargetGlobalAddress |
| 2115 | if (Op && Op->getName() == "globaladdr") { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2116 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTarget" |
| 2117 | "GlobalAddress(cast<GlobalAddressSDNode>(" + Val + |
| 2118 | ")->getGlobal(), MVT::" + getEnumName(N->getTypeNum(0)) + |
| 2119 | ");"); |
| 2120 | } else { |
| 2121 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = " + Val + ";"); |
| 2122 | } |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 2123 | } else if (!N->isLeaf() && N->getOperator()->getName() == "texternalsym"){ |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2124 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Evan Cheng | bb48e33 | 2006-01-12 07:54:57 +0000 | [diff] [blame] | 2125 | } else if (!N->isLeaf() && N->getOperator()->getName() == "tconstpool") { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2126 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = " + Val + ";"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2127 | } else if (N->isLeaf() && (CP = NodeGetComplexPattern(N, ISE))) { |
| 2128 | std::string Fn = CP->getSelectFunc(); |
| 2129 | NumRes = CP->getNumOperands(); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2130 | std::string Code = "SDOperand "; |
Jeff Cohen | 60e9187 | 2006-01-04 03:23:30 +0000 | [diff] [blame] | 2131 | for (unsigned i = 0; i < NumRes - 1; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2132 | Code += "Tmp" + utostr(i+ResNo) + ", "; |
| 2133 | emitCode(Code + "Tmp" + utostr(NumRes - 1 + ResNo) + ";"); |
| 2134 | |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2135 | Code = Fn + "(" + Val; |
Jeff Cohen | 60e9187 | 2006-01-04 03:23:30 +0000 | [diff] [blame] | 2136 | for (unsigned i = 0; i < NumRes; i++) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2137 | Code += ", Tmp" + utostr(i + ResNo); |
| 2138 | emitCheck(Code + ")"); |
Evan Cheng | 9c4815a | 2006-02-04 08:50:49 +0000 | [diff] [blame] | 2139 | |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame^] | 2140 | for (unsigned i = 0; i < NumRes; ++i) |
| 2141 | emitCode("Tmp" + utostr(i+ResNo) + " = Select(Tmp" + |
| 2142 | utostr(i+ResNo) + ");"); |
Evan Cheng | 9c4815a | 2006-02-04 08:50:49 +0000 | [diff] [blame] | 2143 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2144 | TmpNo = ResNo + NumRes; |
| 2145 | } else { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2146 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = Select(" + Val + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2147 | } |
| 2148 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 2149 | // value if used multiple times by this pattern result. |
| 2150 | Val = "Tmp"+utostr(ResNo); |
| 2151 | return std::make_pair(NumRes, ResNo); |
| 2152 | } |
| 2153 | |
| 2154 | if (N->isLeaf()) { |
| 2155 | // If this is an explicit register reference, handle it. |
| 2156 | if (DefInit *DI = dynamic_cast<DefInit*>(N->getLeafValue())) { |
| 2157 | unsigned ResNo = TmpNo++; |
| 2158 | if (DI->getDef()->isSubClassOf("Register")) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2159 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getRegister(" + |
| 2160 | ISE.getQualifiedName(DI->getDef()) + ", MVT::" + |
| 2161 | getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2162 | return std::make_pair(1, ResNo); |
| 2163 | } |
| 2164 | } else if (IntInit *II = dynamic_cast<IntInit*>(N->getLeafValue())) { |
| 2165 | unsigned ResNo = TmpNo++; |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2166 | assert(N->getExtTypes().size() == 1 && "Multiple types not handled!"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2167 | emitCode("SDOperand Tmp" + utostr(ResNo) + |
| 2168 | " = CurDAG->getTargetConstant(" + itostr(II->getValue()) + |
| 2169 | ", MVT::" + getEnumName(N->getTypeNum(0)) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2170 | return std::make_pair(1, ResNo); |
| 2171 | } |
| 2172 | |
| 2173 | N->dump(); |
| 2174 | assert(0 && "Unknown leaf type!"); |
| 2175 | return std::make_pair(1, ~0U); |
| 2176 | } |
| 2177 | |
| 2178 | Record *Op = N->getOperator(); |
| 2179 | if (Op->isSubClassOf("Instruction")) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2180 | const CodeGenTarget &CGT = ISE.getTargetInfo(); |
| 2181 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2182 | const DAGInstruction &Inst = ISE.getInstruction(Op); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2183 | bool HasImpInputs = Inst.getNumImpOperands() > 0; |
| 2184 | bool HasImpResults = Inst.getNumImpResults() > 0; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2185 | bool HasOptInFlag = isRoot && |
| 2186 | PatternHasProperty(Pattern, SDNodeInfo::SDNPOptInFlag, ISE); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2187 | bool HasInFlag = isRoot && |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2188 | PatternHasProperty(Pattern, SDNodeInfo::SDNPInFlag, ISE); |
| 2189 | bool NodeHasOutFlag = HasImpResults || |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2190 | (isRoot && PatternHasProperty(Pattern, SDNodeInfo::SDNPOutFlag, ISE)); |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2191 | bool NodeHasChain = |
| 2192 | NodeHasProperty(Pattern, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2193 | bool HasChain = II.hasCtrlDep || |
| 2194 | (isRoot && PatternHasProperty(Pattern, SDNodeInfo::SDNPHasChain, ISE)); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2195 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2196 | if (HasInFlag || NodeHasOutFlag || HasOptInFlag || HasImpInputs) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2197 | emitCode("SDOperand InFlag = SDOperand(0, 0);"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2198 | if (HasOptInFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2199 | emitCode("bool HasOptInFlag = false;"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2200 | |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2201 | // How many results is this pattern expected to produce? |
| 2202 | unsigned NumExpectedResults = 0; |
| 2203 | for (unsigned i = 0, e = Pattern->getExtTypes().size(); i != e; i++) { |
| 2204 | MVT::ValueType VT = Pattern->getTypeNum(i); |
| 2205 | if (VT != MVT::isVoid && VT != MVT::Flag) |
| 2206 | NumExpectedResults++; |
| 2207 | } |
| 2208 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2209 | // Determine operand emission order. Complex pattern first. |
| 2210 | std::vector<std::pair<unsigned, TreePatternNode*> > EmitOrder; |
| 2211 | std::vector<std::pair<unsigned, TreePatternNode*> >::iterator OI; |
| 2212 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
| 2213 | TreePatternNode *Child = N->getChild(i); |
| 2214 | if (i == 0) { |
| 2215 | EmitOrder.push_back(std::make_pair(i, Child)); |
| 2216 | OI = EmitOrder.begin(); |
| 2217 | } else if (NodeIsComplexPattern(Child)) { |
| 2218 | OI = EmitOrder.insert(OI, std::make_pair(i, Child)); |
| 2219 | } else { |
| 2220 | EmitOrder.push_back(std::make_pair(i, Child)); |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | // Emit all of the operands. |
| 2225 | std::vector<std::pair<unsigned, unsigned> > NumTemps(EmitOrder.size()); |
| 2226 | for (unsigned i = 0, e = EmitOrder.size(); i != e; ++i) { |
| 2227 | unsigned OpOrder = EmitOrder[i].first; |
| 2228 | TreePatternNode *Child = EmitOrder[i].second; |
| 2229 | std::pair<unsigned, unsigned> NumTemp = EmitResultCode(Child); |
| 2230 | NumTemps[OpOrder] = NumTemp; |
| 2231 | } |
| 2232 | |
| 2233 | // List all the operands in the right order. |
| 2234 | std::vector<unsigned> Ops; |
| 2235 | for (unsigned i = 0, e = NumTemps.size(); i != e; i++) { |
| 2236 | for (unsigned j = 0; j < NumTemps[i].first; j++) |
| 2237 | Ops.push_back(NumTemps[i].second + j); |
| 2238 | } |
| 2239 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2240 | // Emit all the chain and CopyToReg stuff. |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2241 | bool ChainEmitted = HasChain; |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2242 | if (HasChain) |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2243 | emitCode(ChainName + " = Select(" + ChainName + ");"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2244 | if (HasInFlag || HasOptInFlag || HasImpInputs) |
| 2245 | EmitInFlagSelectCode(Pattern, "N", ChainEmitted, true); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2246 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2247 | unsigned NumResults = Inst.getNumResults(); |
| 2248 | unsigned ResNo = TmpNo++; |
| 2249 | if (!isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2250 | std::string Code = |
| 2251 | "SDOperand Tmp" + utostr(ResNo) + " = CurDAG->getTargetNode(" + |
| 2252 | II.Namespace + "::" + II.TheDef->getName(); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2253 | if (N->getTypeNum(0) != MVT::isVoid) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2254 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2255 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2256 | Code += ", MVT::Flag"; |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 2257 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2258 | unsigned LastOp = 0; |
| 2259 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 2260 | LastOp = Ops[i]; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2261 | Code += ", Tmp" + utostr(LastOp); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2262 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2263 | emitCode(Code + ");"); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2264 | if (HasChain) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2265 | // Must have at least one result |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2266 | emitCode(ChainName + " = Tmp" + utostr(LastOp) + ".getValue(" + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2267 | utostr(NumResults) + ");"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2268 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2269 | } else if (HasChain || NodeHasOutFlag) { |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2270 | if (HasOptInFlag) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2271 | emitCode("SDOperand Result = SDOperand(0, 0);"); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2272 | unsigned FlagNo = (unsigned) NodeHasChain + Pattern->getNumChildren(); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2273 | emitCode("if (HasOptInFlag)"); |
| 2274 | std::string Code = " Result = CurDAG->getTargetNode(" + |
| 2275 | II.Namespace + "::" + II.TheDef->getName(); |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2276 | |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2277 | // Output order: results, chain, flags |
| 2278 | // Result types. |
| 2279 | if (NumResults > 0) { |
| 2280 | if (N->getTypeNum(0) != MVT::isVoid) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2281 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2282 | } |
| 2283 | if (HasChain) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2284 | Code += ", MVT::Other"; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2285 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2286 | Code += ", MVT::Flag"; |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2287 | |
| 2288 | // Inputs. |
| 2289 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2290 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2291 | if (HasChain) Code += ", " + ChainName; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2292 | emitCode(Code + ", InFlag);"); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2293 | |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2294 | emitCode("else"); |
| 2295 | Code = " Result = CurDAG->getTargetNode(" + II.Namespace + "::" + |
| 2296 | II.TheDef->getName(); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2297 | |
| 2298 | // Output order: results, chain, flags |
| 2299 | // Result types. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2300 | if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) |
| 2301 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2302 | if (HasChain) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2303 | Code += ", MVT::Other"; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2304 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2305 | Code += ", MVT::Flag"; |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2306 | |
| 2307 | // Inputs. |
| 2308 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2309 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2310 | if (HasChain) Code += ", " + ChainName + ");"; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2311 | emitCode(Code); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2312 | } else { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2313 | std::string Code = "SDOperand Result = CurDAG->getTargetNode(" + |
| 2314 | II.Namespace + "::" + II.TheDef->getName(); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2315 | |
| 2316 | // Output order: results, chain, flags |
| 2317 | // Result types. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2318 | if (NumResults > 0 && N->getTypeNum(0) != MVT::isVoid) |
| 2319 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2320 | if (HasChain) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2321 | Code += ", MVT::Other"; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2322 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2323 | Code += ", MVT::Flag"; |
Evan Cheng | 9789aaa | 2006-01-24 20:46:50 +0000 | [diff] [blame] | 2324 | |
| 2325 | // Inputs. |
| 2326 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2327 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2328 | if (HasChain) Code += ", " + ChainName; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2329 | if (HasInFlag || HasImpInputs) Code += ", InFlag"; |
| 2330 | emitCode(Code + ");"); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 2331 | } |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2332 | |
| 2333 | unsigned ValNo = 0; |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 2334 | for (unsigned i = 0; i < NumResults; i++) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2335 | emitCode("CodeGenMap[N.getValue(" + utostr(ValNo) + ")] = Result" |
| 2336 | ".getValue(" + utostr(ValNo) + ");"); |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 2337 | ValNo++; |
| 2338 | } |
| 2339 | |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2340 | if (HasChain) |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2341 | emitCode(ChainName + " = Result.getValue(" + utostr(ValNo) + ");"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2342 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2343 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2344 | emitCode("InFlag = Result.getValue(" + |
| 2345 | utostr(ValNo + (unsigned)HasChain) + ");"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2346 | |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2347 | if (HasImpResults && EmitCopyFromRegs(N, ChainEmitted)) { |
| 2348 | emitCode("CodeGenMap[N.getValue(" + utostr(ValNo) + ")] = " |
| 2349 | "Result.getValue(" + utostr(ValNo) + ");"); |
| 2350 | ValNo++; |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2351 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2352 | |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2353 | // User does not expect that the instruction produces a chain! |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2354 | bool AddedChain = HasChain && !NodeHasChain; |
| 2355 | if (NodeHasChain) |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2356 | emitCode("CodeGenMap[N.getValue(" + utostr(ValNo++) + ")] = " + |
| 2357 | ChainName + ";"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2358 | |
| 2359 | if (FoldedChains.size() > 0) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2360 | std::string Code; |
Evan Cheng | 1b80f4d | 2005-12-19 07:18:51 +0000 | [diff] [blame] | 2361 | for (unsigned j = 0, e = FoldedChains.size(); j < e; j++) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2362 | Code += "CodeGenMap[" + FoldedChains[j].first + ".getValue(" + |
| 2363 | utostr(FoldedChains[j].second) + ")] = "; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2364 | emitCode(Code + ChainName + ";"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2365 | } |
Evan Cheng | f9fc25d | 2005-12-19 22:40:04 +0000 | [diff] [blame] | 2366 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2367 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2368 | emitCode("CodeGenMap[N.getValue(" + utostr(ValNo) + ")] = InFlag;"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2369 | |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2370 | if (AddedChain && NodeHasOutFlag) { |
Evan Cheng | 823b752 | 2006-01-19 21:57:10 +0000 | [diff] [blame] | 2371 | if (NumExpectedResults == 0) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2372 | emitCode("return Result.getValue(N.ResNo+1);"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2373 | } else { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2374 | emitCode("if (N.ResNo < " + utostr(NumExpectedResults) + ")"); |
| 2375 | emitCode(" return Result.getValue(N.ResNo);"); |
| 2376 | emitCode("else"); |
| 2377 | emitCode(" return Result.getValue(N.ResNo+1);"); |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2378 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2379 | } else { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2380 | emitCode("return Result.getValue(N.ResNo);"); |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2381 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2382 | } else { |
| 2383 | // If this instruction is the root, and if there is only one use of it, |
| 2384 | // use SelectNodeTo instead of getTargetNode to avoid an allocation. |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2385 | emitCode("if (N.Val->hasOneUse()) {"); |
| 2386 | std::string Code = " return CurDAG->SelectNodeTo(N.Val, " + |
| 2387 | II.Namespace + "::" + II.TheDef->getName(); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2388 | if (N->getTypeNum(0) != MVT::isVoid) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2389 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2390 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2391 | Code += ", MVT::Flag"; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2392 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2393 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2394 | if (HasInFlag || HasImpInputs) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2395 | Code += ", InFlag"; |
| 2396 | emitCode(Code + ");"); |
| 2397 | emitCode("} else {"); |
| 2398 | Code = " return CodeGenMap[N] = CurDAG->getTargetNode(" + |
| 2399 | II.Namespace + "::" + II.TheDef->getName(); |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2400 | if (N->getTypeNum(0) != MVT::isVoid) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2401 | Code += ", MVT::" + getEnumName(N->getTypeNum(0)); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2402 | if (NodeHasOutFlag) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2403 | Code += ", MVT::Flag"; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2404 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2405 | Code += ", Tmp" + utostr(Ops[i]); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2406 | if (HasInFlag || HasImpInputs) |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2407 | Code += ", InFlag"; |
| 2408 | emitCode(Code + ");"); |
| 2409 | emitCode("}"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2410 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2411 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2412 | return std::make_pair(1, ResNo); |
| 2413 | } else if (Op->isSubClassOf("SDNodeXForm")) { |
| 2414 | assert(N->getNumChildren() == 1 && "node xform should have one child!"); |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2415 | unsigned OpVal = EmitResultCode(N->getChild(0)).second; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2416 | unsigned ResNo = TmpNo++; |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2417 | emitCode("SDOperand Tmp" + utostr(ResNo) + " = Transform_" + Op->getName() |
| 2418 | + "(Tmp" + utostr(OpVal) + ".Val);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2419 | if (isRoot) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2420 | emitCode("CodeGenMap[N] = Tmp" +utostr(ResNo) + ";"); |
| 2421 | emitCode("return Tmp" + utostr(ResNo) + ";"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2422 | } |
| 2423 | return std::make_pair(1, ResNo); |
| 2424 | } else { |
| 2425 | N->dump(); |
Chris Lattner | 7893f13 | 2006-01-11 01:33:49 +0000 | [diff] [blame] | 2426 | std::cerr << "\n"; |
| 2427 | throw std::string("Unknown node in result pattern!"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2431 | /// InsertOneTypeCheck - Insert a type-check for an unresolved type in 'Pat' |
| 2432 | /// and add it to the tree. 'Pat' and 'Other' are isomorphic trees except that |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2433 | /// 'Pat' may be missing types. If we find an unresolved type to add a check |
| 2434 | /// for, this returns true otherwise false if Pat has all types. |
| 2435 | bool InsertOneTypeCheck(TreePatternNode *Pat, TreePatternNode *Other, |
| 2436 | const std::string &Prefix) { |
| 2437 | // Did we find one? |
| 2438 | if (!Pat->hasTypeSet()) { |
| 2439 | // Move a type over from 'other' to 'pat'. |
Nate Begeman | b73628b | 2005-12-30 00:12:56 +0000 | [diff] [blame] | 2440 | Pat->setTypes(Other->getExtTypes()); |
Chris Lattner | 67a202b | 2006-01-28 20:43:52 +0000 | [diff] [blame] | 2441 | emitCheck(Prefix + ".Val->getValueType(0) == MVT::" + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2442 | getName(Pat->getTypeNum(0))); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2443 | return true; |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2444 | } |
| 2445 | |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2446 | unsigned OpNo = |
| 2447 | (unsigned) NodeHasProperty(Pat, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2448 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i, ++OpNo) |
| 2449 | if (InsertOneTypeCheck(Pat->getChild(i), Other->getChild(i), |
| 2450 | Prefix + utostr(OpNo))) |
| 2451 | return true; |
| 2452 | return false; |
| 2453 | } |
| 2454 | |
| 2455 | private: |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2456 | /// EmitInFlagSelectCode - Emit the flag operands for the DAG that is |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2457 | /// being built. |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2458 | void EmitInFlagSelectCode(TreePatternNode *N, const std::string &RootName, |
| 2459 | bool &ChainEmitted, bool isRoot = false) { |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2460 | const CodeGenTarget &T = ISE.getTargetInfo(); |
Evan Cheng | 51fecc8 | 2006-01-09 18:27:06 +0000 | [diff] [blame] | 2461 | unsigned OpNo = |
| 2462 | (unsigned) NodeHasProperty(N, SDNodeInfo::SDNPHasChain, ISE); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2463 | bool HasInFlag = NodeHasProperty(N, SDNodeInfo::SDNPInFlag, ISE); |
| 2464 | bool HasOptInFlag = NodeHasProperty(N, SDNodeInfo::SDNPOptInFlag, ISE); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2465 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i, ++OpNo) { |
| 2466 | TreePatternNode *Child = N->getChild(i); |
| 2467 | if (!Child->isLeaf()) { |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2468 | EmitInFlagSelectCode(Child, RootName + utostr(OpNo), ChainEmitted); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2469 | } else { |
| 2470 | if (DefInit *DI = dynamic_cast<DefInit*>(Child->getLeafValue())) { |
Evan Cheng | b4ad33c | 2006-01-19 01:55:45 +0000 | [diff] [blame] | 2471 | if (!Child->getName().empty()) { |
| 2472 | std::string Name = RootName + utostr(OpNo); |
| 2473 | if (Duplicates.find(Name) != Duplicates.end()) |
| 2474 | // A duplicate! Do not emit a copy for this node. |
| 2475 | continue; |
| 2476 | } |
| 2477 | |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2478 | Record *RR = DI->getDef(); |
| 2479 | if (RR->isSubClassOf("Register")) { |
| 2480 | MVT::ValueType RVT = getRegisterValueType(RR, T); |
Evan Cheng | bcecf33 | 2005-12-17 01:19:28 +0000 | [diff] [blame] | 2481 | if (RVT == MVT::Flag) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2482 | emitCode("InFlag = Select(" + RootName + utostr(OpNo) + ");"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2483 | } else { |
| 2484 | if (!ChainEmitted) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2485 | emitCode("SDOperand Chain = CurDAG->getEntryNode();"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2486 | ChainName = "Chain"; |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2487 | ChainEmitted = true; |
| 2488 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2489 | emitCode("SDOperand " + RootName + "CR" + utostr(i) + ";"); |
| 2490 | emitCode(RootName + "CR" + utostr(i) + |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2491 | " = CurDAG->getCopyToReg(" + ChainName + |
| 2492 | ", CurDAG->getRegister(" + ISE.getQualifiedName(RR) + |
| 2493 | ", MVT::" + getEnumName(RVT) + "), Select(" + RootName + |
| 2494 | utostr(OpNo) + "), InFlag);"); |
| 2495 | emitCode(ChainName + " = " + RootName + "CR" + utostr(i) + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2496 | ".getValue(0);"); |
| 2497 | emitCode("InFlag = " + RootName + "CR" + utostr(i) + |
| 2498 | ".getValue(1);"); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2499 | } |
| 2500 | } |
| 2501 | } |
| 2502 | } |
| 2503 | } |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2504 | |
| 2505 | if (HasInFlag || HasOptInFlag) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2506 | std::string Code; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2507 | if (HasOptInFlag) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2508 | emitCode("if (" + RootName + ".getNumOperands() == " + utostr(OpNo+1) + |
| 2509 | ") {"); |
| 2510 | Code = " "; |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2511 | } |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2512 | emitCode(Code + "InFlag = Select(" + RootName + ".getOperand(" + |
| 2513 | utostr(OpNo) + "));"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2514 | if (HasOptInFlag) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2515 | emitCode(" HasOptInFlag = true;"); |
| 2516 | emitCode("}"); |
Evan Cheng | 5459773 | 2006-01-26 00:22:25 +0000 | [diff] [blame] | 2517 | } |
| 2518 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2519 | } |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2520 | |
| 2521 | /// EmitCopyFromRegs - Emit code to copy result to physical registers |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2522 | /// as specified by the instruction. It returns true if any copy is |
| 2523 | /// emitted. |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2524 | bool EmitCopyFromRegs(TreePatternNode *N, bool &ChainEmitted) { |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2525 | bool RetVal = false; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2526 | Record *Op = N->getOperator(); |
| 2527 | if (Op->isSubClassOf("Instruction")) { |
| 2528 | const DAGInstruction &Inst = ISE.getInstruction(Op); |
| 2529 | const CodeGenTarget &CGT = ISE.getTargetInfo(); |
| 2530 | CodeGenInstruction &II = CGT.getInstruction(Op->getName()); |
| 2531 | unsigned NumImpResults = Inst.getNumImpResults(); |
| 2532 | for (unsigned i = 0; i < NumImpResults; i++) { |
| 2533 | Record *RR = Inst.getImpResult(i); |
| 2534 | if (RR->isSubClassOf("Register")) { |
| 2535 | MVT::ValueType RVT = getRegisterValueType(RR, CGT); |
| 2536 | if (RVT != MVT::Flag) { |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2537 | if (!ChainEmitted) { |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2538 | emitCode("SDOperand Chain = CurDAG->getEntryNode();"); |
Evan Cheng | b2c6d49 | 2006-01-11 22:16:13 +0000 | [diff] [blame] | 2539 | ChainEmitted = true; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2540 | ChainName = "Chain"; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2541 | } |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2542 | emitCode("Result = CurDAG->getCopyFromReg(" + ChainName + ", " + |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2543 | ISE.getQualifiedName(RR) + ", MVT::" + getEnumName(RVT) + |
| 2544 | ", InFlag);"); |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2545 | emitCode(ChainName + " = Result.getValue(1);"); |
Chris Lattner | 8a0604b | 2006-01-28 20:31:24 +0000 | [diff] [blame] | 2546 | emitCode("InFlag = Result.getValue(2);"); |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2547 | RetVal = true; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } |
| 2550 | } |
| 2551 | } |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2552 | return RetVal; |
Evan Cheng | 4fba281 | 2005-12-20 07:37:41 +0000 | [diff] [blame] | 2553 | } |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2554 | }; |
| 2555 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 2556 | /// EmitCodeForPattern - Given a pattern to match, emit code to the specified |
| 2557 | /// stream to match the pattern, and generate the code for the match if it |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2558 | /// succeeds. Returns true if the pattern is not guaranteed to match. |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2559 | void DAGISelEmitter::GenerateCodeForPattern(PatternToMatch &Pattern, |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2560 | std::vector<std::pair<bool, std::string> > &GeneratedCode) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2561 | PatternCodeEmitter Emitter(*this, Pattern.getPredicates(), |
| 2562 | Pattern.getSrcPattern(), Pattern.getDstPattern(), |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2563 | GeneratedCode); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 2565 | // Emit the matcher, capturing named arguments in VariableMap. |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2566 | bool FoundChain = false; |
Evan Cheng | e4a8a6e | 2006-02-03 06:22:41 +0000 | [diff] [blame] | 2567 | Emitter.EmitMatchCode(Pattern.getSrcPattern(), "N", "", FoundChain, |
Evan Cheng | 7b05bd5 | 2005-12-23 22:11:47 +0000 | [diff] [blame] | 2568 | true /*the root*/); |
Evan Cheng | b915f31 | 2005-12-09 22:45:35 +0000 | [diff] [blame] | 2569 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2570 | // TP - Get *SOME* tree pattern, we don't care which. |
| 2571 | TreePattern &TP = *PatternFragments.begin()->second; |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 2572 | |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2573 | // At this point, we know that we structurally match the pattern, but the |
| 2574 | // types of the nodes may not match. Figure out the fewest number of type |
| 2575 | // comparisons we need to emit. For example, if there is only one integer |
| 2576 | // type supported by a target, there should be no type comparisons at all for |
| 2577 | // integer patterns! |
| 2578 | // |
| 2579 | // To figure out the fewest number of type checks needed, clone the pattern, |
| 2580 | // remove the types, then perform type inference on the pattern as a whole. |
| 2581 | // If there are unresolved types, emit an explicit check for those types, |
| 2582 | // apply the type to the tree, then rerun type inference. Iterate until all |
| 2583 | // types are resolved. |
| 2584 | // |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2585 | TreePatternNode *Pat = Pattern.getSrcPattern()->clone(); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2586 | RemoveAllTypes(Pat); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 2587 | |
| 2588 | do { |
| 2589 | // Resolve/propagate as many types as possible. |
| 2590 | try { |
| 2591 | bool MadeChange = true; |
| 2592 | while (MadeChange) |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2593 | MadeChange = Pat->ApplyTypeConstraints(TP, |
| 2594 | true/*Ignore reg constraints*/); |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 2595 | } catch (...) { |
| 2596 | assert(0 && "Error: could not find consistent types for something we" |
| 2597 | " already decided was ok!"); |
| 2598 | abort(); |
| 2599 | } |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | 7e82f13 | 2005-10-15 21:34:21 +0000 | [diff] [blame] | 2601 | // Insert a check for an unresolved type and add it to the tree. If we find |
| 2602 | // an unresolved type to add a check for, this returns true and we iterate, |
| 2603 | // otherwise we are done. |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2604 | } while (Emitter.InsertOneTypeCheck(Pat, Pattern.getSrcPattern(), "N")); |
Evan Cheng | 1c3d19e | 2005-12-04 08:18:16 +0000 | [diff] [blame] | 2605 | |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 2606 | Emitter.EmitResultCode(Pattern.getDstPattern(), true /*the root*/); |
Chris Lattner | 0ee7cff | 2005-10-14 04:11:13 +0000 | [diff] [blame] | 2607 | delete Pat; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 2610 | /// EraseCodeLine - Erase one code line from all of the patterns. If removing |
| 2611 | /// a line causes any of them to be empty, remove them and return true when |
| 2612 | /// done. |
| 2613 | static bool EraseCodeLine(std::vector<std::pair<PatternToMatch*, |
| 2614 | std::vector<std::pair<bool, std::string> > > > |
| 2615 | &Patterns) { |
| 2616 | bool ErasedPatterns = false; |
| 2617 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 2618 | Patterns[i].second.pop_back(); |
| 2619 | if (Patterns[i].second.empty()) { |
| 2620 | Patterns.erase(Patterns.begin()+i); |
| 2621 | --i; --e; |
| 2622 | ErasedPatterns = true; |
| 2623 | } |
| 2624 | } |
| 2625 | return ErasedPatterns; |
| 2626 | } |
| 2627 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2628 | /// EmitPatterns - Emit code for at least one pattern, but try to group common |
| 2629 | /// code together between the patterns. |
| 2630 | void DAGISelEmitter::EmitPatterns(std::vector<std::pair<PatternToMatch*, |
| 2631 | std::vector<std::pair<bool, std::string> > > > |
| 2632 | &Patterns, unsigned Indent, |
| 2633 | std::ostream &OS) { |
| 2634 | typedef std::pair<bool, std::string> CodeLine; |
| 2635 | typedef std::vector<CodeLine> CodeList; |
| 2636 | typedef std::vector<std::pair<PatternToMatch*, CodeList> > PatternList; |
| 2637 | |
| 2638 | if (Patterns.empty()) return; |
| 2639 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 2640 | // Figure out how many patterns share the next code line. Explicitly copy |
| 2641 | // FirstCodeLine so that we don't invalidate a reference when changing |
| 2642 | // Patterns. |
| 2643 | const CodeLine FirstCodeLine = Patterns.back().second.back(); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2644 | unsigned LastMatch = Patterns.size()-1; |
| 2645 | while (LastMatch != 0 && Patterns[LastMatch-1].second.back() == FirstCodeLine) |
| 2646 | --LastMatch; |
| 2647 | |
| 2648 | // If not all patterns share this line, split the list into two pieces. The |
| 2649 | // first chunk will use this line, the second chunk won't. |
| 2650 | if (LastMatch != 0) { |
| 2651 | PatternList Shared(Patterns.begin()+LastMatch, Patterns.end()); |
| 2652 | PatternList Other(Patterns.begin(), Patterns.begin()+LastMatch); |
| 2653 | |
| 2654 | // FIXME: Emit braces? |
| 2655 | if (Shared.size() == 1) { |
| 2656 | PatternToMatch &Pattern = *Shared.back().first; |
| 2657 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 2658 | Pattern.getSrcPattern()->print(OS); |
| 2659 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 2660 | Pattern.getDstPattern()->print(OS); |
| 2661 | OS << "\n"; |
| 2662 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
| 2663 | << getPatternSize(Pattern.getSrcPattern(), *this) << " cost = " |
| 2664 | << getResultPatternCost(Pattern.getDstPattern()) << "\n"; |
| 2665 | } |
| 2666 | if (!FirstCodeLine.first) { |
| 2667 | OS << std::string(Indent, ' ') << "{\n"; |
| 2668 | Indent += 2; |
| 2669 | } |
| 2670 | EmitPatterns(Shared, Indent, OS); |
| 2671 | if (!FirstCodeLine.first) { |
| 2672 | Indent -= 2; |
| 2673 | OS << std::string(Indent, ' ') << "}\n"; |
| 2674 | } |
| 2675 | |
| 2676 | if (Other.size() == 1) { |
| 2677 | PatternToMatch &Pattern = *Other.back().first; |
| 2678 | OS << "\n" << std::string(Indent, ' ') << "// Pattern: "; |
| 2679 | Pattern.getSrcPattern()->print(OS); |
| 2680 | OS << "\n" << std::string(Indent, ' ') << "// Emits: "; |
| 2681 | Pattern.getDstPattern()->print(OS); |
| 2682 | OS << "\n"; |
| 2683 | OS << std::string(Indent, ' ') << "// Pattern complexity = " |
| 2684 | << getPatternSize(Pattern.getSrcPattern(), *this) << " cost = " |
| 2685 | << getResultPatternCost(Pattern.getDstPattern()) << "\n"; |
| 2686 | } |
| 2687 | EmitPatterns(Other, Indent, OS); |
| 2688 | return; |
| 2689 | } |
| 2690 | |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 2691 | // Remove this code from all of the patterns that share it. |
| 2692 | bool ErasedPatterns = EraseCodeLine(Patterns); |
| 2693 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2694 | bool isPredicate = FirstCodeLine.first; |
| 2695 | |
| 2696 | // Otherwise, every pattern in the list has this line. Emit it. |
| 2697 | if (!isPredicate) { |
| 2698 | // Normal code. |
| 2699 | OS << std::string(Indent, ' ') << FirstCodeLine.second << "\n"; |
| 2700 | } else { |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 2701 | OS << std::string(Indent, ' ') << "if (" << FirstCodeLine.second; |
| 2702 | |
| 2703 | // If the next code line is another predicate, and if all of the pattern |
| 2704 | // in this group share the same next line, emit it inline now. Do this |
| 2705 | // until we run out of common predicates. |
| 2706 | while (!ErasedPatterns && Patterns.back().second.back().first) { |
| 2707 | // Check that all of fhe patterns in Patterns end with the same predicate. |
| 2708 | bool AllEndWithSamePredicate = true; |
| 2709 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) |
| 2710 | if (Patterns[i].second.back() != Patterns.back().second.back()) { |
| 2711 | AllEndWithSamePredicate = false; |
| 2712 | break; |
| 2713 | } |
| 2714 | // If all of the predicates aren't the same, we can't share them. |
| 2715 | if (!AllEndWithSamePredicate) break; |
| 2716 | |
| 2717 | // Otherwise we can. Emit it shared now. |
| 2718 | OS << " &&\n" << std::string(Indent+4, ' ') |
| 2719 | << Patterns.back().second.back().second; |
| 2720 | ErasedPatterns = EraseCodeLine(Patterns); |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2721 | } |
Chris Lattner | 24e00a4 | 2006-01-29 04:41:05 +0000 | [diff] [blame] | 2722 | |
| 2723 | OS << ") {\n"; |
| 2724 | Indent += 2; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | EmitPatterns(Patterns, Indent, OS); |
| 2728 | |
| 2729 | if (isPredicate) |
| 2730 | OS << std::string(Indent-2, ' ') << "}\n"; |
| 2731 | } |
| 2732 | |
| 2733 | |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 2734 | |
| 2735 | namespace { |
| 2736 | /// CompareByRecordName - An ordering predicate that implements less-than by |
| 2737 | /// comparing the names records. |
| 2738 | struct CompareByRecordName { |
| 2739 | bool operator()(const Record *LHS, const Record *RHS) const { |
| 2740 | // Sort by name first. |
| 2741 | if (LHS->getName() < RHS->getName()) return true; |
| 2742 | // If both names are equal, sort by pointer. |
| 2743 | return LHS->getName() == RHS->getName() && LHS < RHS; |
| 2744 | } |
| 2745 | }; |
| 2746 | } |
| 2747 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2748 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 2749 | std::string InstNS = Target.inst_begin()->second.Namespace; |
| 2750 | if (!InstNS.empty()) InstNS += "::"; |
| 2751 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2752 | // Group the patterns by their top-level opcodes. |
| 2753 | std::map<Record*, std::vector<PatternToMatch*>, |
| 2754 | CompareByRecordName> PatternsByOpcode; |
| 2755 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 2756 | TreePatternNode *Node = PatternsToMatch[i].getSrcPattern(); |
| 2757 | if (!Node->isLeaf()) { |
| 2758 | PatternsByOpcode[Node->getOperator()].push_back(&PatternsToMatch[i]); |
| 2759 | } else { |
| 2760 | const ComplexPattern *CP; |
| 2761 | if (IntInit *II = |
| 2762 | dynamic_cast<IntInit*>(Node->getLeafValue())) { |
| 2763 | PatternsByOpcode[getSDNodeNamed("imm")].push_back(&PatternsToMatch[i]); |
| 2764 | } else if ((CP = NodeGetComplexPattern(Node, *this))) { |
| 2765 | std::vector<Record*> OpNodes = CP->getRootNodes(); |
| 2766 | for (unsigned j = 0, e = OpNodes.size(); j != e; j++) { |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2767 | PatternsByOpcode[OpNodes[j]] |
| 2768 | .insert(PatternsByOpcode[OpNodes[j]].begin(), &PatternsToMatch[i]); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } else { |
| 2771 | std::cerr << "Unrecognized opcode '"; |
| 2772 | Node->dump(); |
| 2773 | std::cerr << "' on tree pattern '"; |
Chris Lattner | 488580c | 2006-01-28 19:06:51 +0000 | [diff] [blame] | 2774 | std::cerr << |
| 2775 | PatternsToMatch[i].getDstPattern()->getOperator()->getName(); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2776 | std::cerr << "'!\n"; |
| 2777 | exit(1); |
| 2778 | } |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | // Emit one Select_* method for each top-level opcode. We do this instead of |
| 2783 | // emitting one giant switch statement to support compilers where this will |
| 2784 | // result in the recursive functions taking less stack space. |
| 2785 | for (std::map<Record*, std::vector<PatternToMatch*>, |
| 2786 | CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(), |
| 2787 | E = PatternsByOpcode.end(); PBOI != E; ++PBOI) { |
| 2788 | OS << "SDOperand Select_" << PBOI->first->getName() << "(SDOperand N) {\n"; |
| 2789 | |
| 2790 | const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first); |
| 2791 | std::vector<PatternToMatch*> &Patterns = PBOI->second; |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2792 | assert(!Patterns.empty() && "No patterns but map has entry?"); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2793 | |
| 2794 | // We want to emit all of the matching code now. However, we want to emit |
| 2795 | // the matches in order of minimal cost. Sort the patterns so the least |
| 2796 | // cost one is at the start. |
| 2797 | std::stable_sort(Patterns.begin(), Patterns.end(), |
| 2798 | PatternSortingPredicate(*this)); |
| 2799 | |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2800 | typedef std::vector<std::pair<bool, std::string> > CodeList; |
| 2801 | |
| 2802 | std::vector<std::pair<PatternToMatch*, CodeList> > CodeForPatterns; |
Jeff Cohen | 9b0ffca | 2006-01-27 22:22:28 +0000 | [diff] [blame] | 2803 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2804 | CodeList GeneratedCode; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2805 | GenerateCodeForPattern(*Patterns[i], GeneratedCode); |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2806 | CodeForPatterns.push_back(std::make_pair(Patterns[i], GeneratedCode)); |
| 2807 | } |
| 2808 | |
| 2809 | // Scan the code to see if all of the patterns are reachable and if it is |
| 2810 | // possible that the last one might not match. |
| 2811 | bool mightNotMatch = true; |
| 2812 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 2813 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
| 2814 | mightNotMatch = false; |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2815 | |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2816 | for (unsigned j = 0, e = GeneratedCode.size(); j != e; ++j) { |
| 2817 | if (GeneratedCode[j].first) { // predicate. |
| 2818 | mightNotMatch = true; |
| 2819 | break; |
| 2820 | } |
| 2821 | } |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2822 | |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2823 | // If this pattern definitely matches, and if it isn't the last one, the |
| 2824 | // patterns after it CANNOT ever match. Error out. |
| 2825 | if (mightNotMatch == false && i != CodeForPatterns.size()-1) { |
| 2826 | std::cerr << "Pattern '"; |
| 2827 | CodeForPatterns[i+1].first->getSrcPattern()->print(OS); |
| 2828 | std::cerr << "' is impossible to select!\n"; |
| 2829 | exit(1); |
| 2830 | } |
| 2831 | } |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2832 | |
| 2833 | // Loop through and reverse all of the CodeList vectors, as we will be |
| 2834 | // accessing them from their logical front, but accessing the end of a |
| 2835 | // vector is more efficient. |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2836 | for (unsigned i = 0, e = CodeForPatterns.size(); i != e; ++i) { |
| 2837 | CodeList &GeneratedCode = CodeForPatterns[i].second; |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2838 | std::reverse(GeneratedCode.begin(), GeneratedCode.end()); |
Jeff Cohen | 9b0ffca | 2006-01-27 22:22:28 +0000 | [diff] [blame] | 2839 | } |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | 8bc7472 | 2006-01-29 04:25:26 +0000 | [diff] [blame] | 2841 | // Next, reverse the list of patterns itself for the same reason. |
| 2842 | std::reverse(CodeForPatterns.begin(), CodeForPatterns.end()); |
| 2843 | |
| 2844 | // Emit all of the patterns now, grouped together to share code. |
| 2845 | EmitPatterns(CodeForPatterns, 2, OS); |
| 2846 | |
Chris Lattner | 2bd4dd7 | 2006-01-29 02:57:39 +0000 | [diff] [blame] | 2847 | // If the last pattern has predicates (which could fail) emit code to catch |
| 2848 | // the case where nothing handles a pattern. |
Chris Lattner | 355408b | 2006-01-29 02:43:35 +0000 | [diff] [blame] | 2849 | if (mightNotMatch) |
Jeff Cohen | 9b0ffca | 2006-01-27 22:22:28 +0000 | [diff] [blame] | 2850 | OS << " std::cerr << \"Cannot yet select: \";\n" |
| 2851 | << " N.Val->dump(CurDAG);\n" |
| 2852 | << " std::cerr << '\\n';\n" |
| 2853 | << " abort();\n"; |
| 2854 | |
| 2855 | OS << "}\n\n"; |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2858 | // Emit boilerplate. |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 2859 | OS << "SDOperand Select_INLINEASM(SDOperand N) {\n" |
| 2860 | << " std::vector<SDOperand> Ops(N.Val->op_begin(), N.Val->op_end());\n" |
| 2861 | << " Ops[0] = Select(N.getOperand(0)); // Select the chain.\n\n" |
| 2862 | << " // Select the flag operand.\n" |
| 2863 | << " if (Ops.back().getValueType() == MVT::Flag)\n" |
| 2864 | << " Ops.back() = Select(Ops.back());\n" |
| 2865 | << " std::vector<MVT::ValueType> VTs;\n" |
| 2866 | << " VTs.push_back(MVT::Other);\n" |
| 2867 | << " VTs.push_back(MVT::Flag);\n" |
| 2868 | << " SDOperand New = CurDAG->getNode(ISD::INLINEASM, VTs, Ops);\n" |
| 2869 | << " CodeGenMap[N.getValue(0)] = New;\n" |
| 2870 | << " CodeGenMap[N.getValue(1)] = New.getValue(1);\n" |
| 2871 | << " return New.getValue(N.ResNo);\n" |
| 2872 | << "}\n\n"; |
| 2873 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2874 | OS << "// The main instruction selector code.\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 2875 | << "SDOperand SelectCode(SDOperand N) {\n" |
| 2876 | << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
Chris Lattner | b277cbc | 2005-10-18 04:41:01 +0000 | [diff] [blame] | 2877 | << " N.getOpcode() < (ISD::BUILTIN_OP_END+" << InstNS |
| 2878 | << "INSTRUCTION_LIST_END))\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 2879 | << " return N; // Already selected.\n\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2880 | << " std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);\n" |
Evan Cheng | 481c8e0 | 2005-12-12 23:22:48 +0000 | [diff] [blame] | 2881 | << " if (CGMI != CodeGenMap.end()) return CGMI->second;\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 2882 | << " switch (N.getOpcode()) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2883 | << " default: break;\n" |
| 2884 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
Chris Lattner | 5216c69 | 2005-12-18 21:05:44 +0000 | [diff] [blame] | 2885 | << " case ISD::BasicBlock:\n" |
Chris Lattner | 8020a52 | 2006-01-11 19:52:27 +0000 | [diff] [blame] | 2886 | << " case ISD::Register:\n" |
Evan Cheng | 2216d8a | 2006-02-05 05:22:18 +0000 | [diff] [blame^] | 2887 | << " case ISD::TargetConstant:\n" |
| 2888 | << " case ISD::TargetConstantPool:\n" |
| 2889 | << " case ISD::TargetFrameIndex:\n" |
| 2890 | << " case ISD::TargetGlobalAddress:\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 2891 | << " return N;\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2892 | << " case ISD::AssertSext:\n" |
Chris Lattner | fab3728 | 2005-09-26 22:10:24 +0000 | [diff] [blame] | 2893 | << " case ISD::AssertZext: {\n" |
| 2894 | << " SDOperand Tmp0 = Select(N.getOperand(0));\n" |
| 2895 | << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp0;\n" |
| 2896 | << " return Tmp0;\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 2897 | << " }\n" |
| 2898 | << " case ISD::TokenFactor:\n" |
| 2899 | << " if (N.getNumOperands() == 2) {\n" |
| 2900 | << " SDOperand Op0 = Select(N.getOperand(0));\n" |
| 2901 | << " SDOperand Op1 = Select(N.getOperand(1));\n" |
| 2902 | << " return CodeGenMap[N] =\n" |
| 2903 | << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);\n" |
| 2904 | << " } else {\n" |
| 2905 | << " std::vector<SDOperand> Ops;\n" |
| 2906 | << " for (unsigned i = 0, e = N.getNumOperands(); i != e; ++i)\n" |
| 2907 | << " Ops.push_back(Select(N.getOperand(i)));\n" |
| 2908 | << " return CodeGenMap[N] = \n" |
| 2909 | << " CurDAG->getNode(ISD::TokenFactor, MVT::Other, Ops);\n" |
| 2910 | << " }\n" |
| 2911 | << " case ISD::CopyFromReg: {\n" |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 2912 | << " SDOperand Chain = Select(N.getOperand(0));\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2913 | << " unsigned Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();\n" |
| 2914 | << " MVT::ValueType VT = N.Val->getValueType(0);\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2915 | << " if (N.Val->getNumValues() == 2) {\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2916 | << " if (Chain == N.getOperand(0)) return N; // No change\n" |
| 2917 | << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT);\n" |
| 2918 | << " CodeGenMap[N.getValue(0)] = New;\n" |
| 2919 | << " CodeGenMap[N.getValue(1)] = New.getValue(1);\n" |
| 2920 | << " return New.getValue(N.ResNo);\n" |
| 2921 | << " } else {\n" |
Chris Lattner | 7a8054f | 2005-12-22 20:37:36 +0000 | [diff] [blame] | 2922 | << " SDOperand Flag(0, 0);\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2923 | << " if (N.getNumOperands() == 3) Flag = Select(N.getOperand(2));\n" |
| 2924 | << " if (Chain == N.getOperand(0) &&\n" |
| 2925 | << " (N.getNumOperands() == 2 || Flag == N.getOperand(2)))\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2926 | << " return N; // No change\n" |
| 2927 | << " SDOperand New = CurDAG->getCopyFromReg(Chain, Reg, VT, Flag);\n" |
| 2928 | << " CodeGenMap[N.getValue(0)] = New;\n" |
| 2929 | << " CodeGenMap[N.getValue(1)] = New.getValue(1);\n" |
| 2930 | << " CodeGenMap[N.getValue(2)] = New.getValue(2);\n" |
| 2931 | << " return New.getValue(N.ResNo);\n" |
| 2932 | << " }\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 2933 | << " }\n" |
| 2934 | << " case ISD::CopyToReg: {\n" |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 2935 | << " SDOperand Chain = Select(N.getOperand(0));\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2936 | << " unsigned Reg = cast<RegisterSDNode>(N.getOperand(1))->getReg();\n" |
Chris Lattner | f071bb5 | 2005-10-25 20:35:14 +0000 | [diff] [blame] | 2937 | << " SDOperand Val = Select(N.getOperand(2));\n" |
Chris Lattner | 4e3c8e51 | 2006-01-03 22:55:16 +0000 | [diff] [blame] | 2938 | << " SDOperand Result = N;\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2939 | << " if (N.Val->getNumValues() == 1) {\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2940 | << " if (Chain != N.getOperand(0) || Val != N.getOperand(2))\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2941 | << " Result = CurDAG->getCopyToReg(Chain, Reg, Val);\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2942 | << " return CodeGenMap[N] = Result;\n" |
| 2943 | << " } else {\n" |
Chris Lattner | 7a8054f | 2005-12-22 20:37:36 +0000 | [diff] [blame] | 2944 | << " SDOperand Flag(0, 0);\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2945 | << " if (N.getNumOperands() == 4) Flag = Select(N.getOperand(3));\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2946 | << " if (Chain != N.getOperand(0) || Val != N.getOperand(2) ||\n" |
Chris Lattner | dc464de | 2005-12-18 15:45:51 +0000 | [diff] [blame] | 2947 | << " (N.getNumOperands() == 4 && Flag != N.getOperand(3)))\n" |
| 2948 | << " Result = CurDAG->getCopyToReg(Chain, Reg, Val, Flag);\n" |
Chris Lattner | 755dd09 | 2005-12-18 15:28:25 +0000 | [diff] [blame] | 2949 | << " CodeGenMap[N.getValue(0)] = Result;\n" |
| 2950 | << " CodeGenMap[N.getValue(1)] = Result.getValue(1);\n" |
| 2951 | << " return Result.getValue(N.ResNo);\n" |
| 2952 | << " }\n" |
Chris Lattner | fabcb7a | 2006-01-26 23:08:55 +0000 | [diff] [blame] | 2953 | << " }\n" |
| 2954 | << " case ISD::INLINEASM: return Select_INLINEASM(N);\n"; |
| 2955 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2956 | |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2957 | // Loop over all of the case statements, emiting a call to each method we |
| 2958 | // emitted above. |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 2959 | for (std::map<Record*, std::vector<PatternToMatch*>, |
| 2960 | CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(), |
| 2961 | E = PatternsByOpcode.end(); PBOI != E; ++PBOI) { |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 2962 | const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first); |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2963 | OS << " case " << OpcodeInfo.getEnumName() << ": " |
Chris Lattner | 11966a0 | 2006-01-04 00:32:01 +0000 | [diff] [blame] | 2964 | << std::string(std::max(0, int(24-OpcodeInfo.getEnumName().size())), ' ') |
Chris Lattner | 602f692 | 2006-01-04 00:25:00 +0000 | [diff] [blame] | 2965 | << "return Select_" << PBOI->first->getName() << "(N);\n"; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 2966 | } |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 2967 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2968 | OS << " } // end of big switch.\n\n" |
| 2969 | << " std::cerr << \"Cannot yet select: \";\n" |
Evan Cheng | 9793888 | 2005-12-22 02:24:50 +0000 | [diff] [blame] | 2970 | << " N.Val->dump(CurDAG);\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2971 | << " std::cerr << '\\n';\n" |
| 2972 | << " abort();\n" |
| 2973 | << "}\n"; |
| 2974 | } |
| 2975 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 2976 | void DAGISelEmitter::run(std::ostream &OS) { |
| 2977 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 2978 | " target", OS); |
| 2979 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 2980 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 2981 | << "// *** instruction selector class. These functions are really " |
| 2982 | << "methods.\n\n"; |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 2983 | |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 2984 | OS << "// Instance var to keep track of multiply used nodes that have \n" |
| 2985 | << "// already been selected.\n" |
| 2986 | << "std::map<SDOperand, SDOperand> CodeGenMap;\n"; |
| 2987 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 2988 | ParseNodeInfo(); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 2989 | ParseNodeTransforms(OS); |
Evan Cheng | 0fc7198 | 2005-12-08 02:00:36 +0000 | [diff] [blame] | 2990 | ParseComplexPatterns(); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 2991 | ParsePatternFragments(OS); |
| 2992 | ParseInstructions(); |
| 2993 | ParsePatterns(); |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 2994 | |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 2995 | // Generate variants. For example, commutative patterns can match |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 2996 | // multiple ways. Add them to PatternsToMatch as well. |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 2997 | GenerateVariants(); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 2999 | |
| 3000 | DEBUG(std::cerr << "\n\nALL PATTERNS TO MATCH:\n\n"; |
| 3001 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
Evan Cheng | 58e84a6 | 2005-12-14 22:02:59 +0000 | [diff] [blame] | 3002 | std::cerr << "PATTERN: "; PatternsToMatch[i].getSrcPattern()->dump(); |
| 3003 | std::cerr << "\nRESULT: ";PatternsToMatch[i].getDstPattern()->dump(); |
Chris Lattner | e46e17b | 2005-09-29 19:28:10 +0000 | [diff] [blame] | 3004 | std::cerr << "\n"; |
| 3005 | }); |
| 3006 | |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 3007 | // At this point, we have full information about the 'Patterns' we need to |
| 3008 | // parse, both implicitly from instructions as well as from explicit pattern |
Chris Lattner | e97603f | 2005-09-28 19:27:25 +0000 | [diff] [blame] | 3009 | // definitions. Emit the resultant instruction selector. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3010 | EmitInstructionSelector(OS); |
| 3011 | |
| 3012 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 3013 | E = PatternFragments.end(); I != E; ++I) |
| 3014 | delete I->second; |
| 3015 | PatternFragments.clear(); |
| 3016 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 3017 | Instructions.clear(); |
| 3018 | } |