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