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