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