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