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" |
| 18 | #include <set> |
| 19 | using namespace llvm; |
| 20 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 21 | //===----------------------------------------------------------------------===// |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 22 | // SDTypeConstraint implementation |
| 23 | // |
| 24 | |
| 25 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 26 | OperandNo = R->getValueAsInt("OperandNum"); |
| 27 | |
| 28 | if (R->isSubClassOf("SDTCisVT")) { |
| 29 | ConstraintType = SDTCisVT; |
| 30 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
| 31 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 32 | ConstraintType = SDTCisInt; |
| 33 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 34 | ConstraintType = SDTCisFP; |
| 35 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 36 | ConstraintType = SDTCisSameAs; |
| 37 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 38 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 39 | ConstraintType = SDTCisVTSmallerThanOp; |
| 40 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 41 | R->getValueAsInt("OtherOperandNum"); |
| 42 | } else { |
| 43 | std::cerr << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
| 44 | exit(1); |
| 45 | } |
| 46 | } |
| 47 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 48 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 49 | /// N, which has NumResults results. |
| 50 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 51 | TreePatternNode *N, |
| 52 | unsigned NumResults) const { |
| 53 | assert(NumResults == 1 && "We only work with single result nodes so far!"); |
| 54 | |
| 55 | if (OpNo < NumResults) |
| 56 | return N; // FIXME: need value # |
| 57 | else |
| 58 | return N->getChild(OpNo-NumResults); |
| 59 | } |
| 60 | |
| 61 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 62 | /// constraint to the nodes operands. This returns true if it makes a |
| 63 | /// change, false otherwise. If a type contradiction is found, throw an |
| 64 | /// exception. |
| 65 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 66 | const SDNodeInfo &NodeInfo, |
| 67 | TreePattern &TP) const { |
| 68 | unsigned NumResults = NodeInfo.getNumResults(); |
| 69 | assert(NumResults == 1 && "We only work with single result nodes so far!"); |
| 70 | |
| 71 | // Check that the number of operands is sane. |
| 72 | if (NodeInfo.getNumOperands() >= 0) { |
| 73 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 74 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 75 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 76 | } |
| 77 | |
| 78 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 79 | |
| 80 | switch (ConstraintType) { |
| 81 | default: assert(0 && "Unknown constraint type!"); |
| 82 | case SDTCisVT: |
| 83 | // Operand must be a particular type. |
| 84 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
| 85 | case SDTCisInt: |
| 86 | if (NodeToApply->hasTypeSet() && !MVT::isInteger(NodeToApply->getType())) |
| 87 | NodeToApply->UpdateNodeType(MVT::i1, TP); // throw an error. |
| 88 | |
| 89 | // FIXME: can tell from the target if there is only one Int type supported. |
| 90 | return false; |
| 91 | case SDTCisFP: |
| 92 | if (NodeToApply->hasTypeSet() && |
| 93 | !MVT::isFloatingPoint(NodeToApply->getType())) |
| 94 | NodeToApply->UpdateNodeType(MVT::f32, TP); // throw an error. |
| 95 | // FIXME: can tell from the target if there is only one FP type supported. |
| 96 | return false; |
| 97 | case SDTCisSameAs: { |
| 98 | TreePatternNode *OtherNode = |
| 99 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
| 100 | return NodeToApply->UpdateNodeType(OtherNode->getType(), TP) | |
| 101 | OtherNode->UpdateNodeType(NodeToApply->getType(), TP); |
| 102 | } |
| 103 | case SDTCisVTSmallerThanOp: { |
| 104 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 105 | // have an integer type that is smaller than the VT. |
| 106 | if (!NodeToApply->isLeaf() || |
| 107 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 108 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 109 | ->isSubClassOf("ValueType")) |
| 110 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
| 111 | MVT::ValueType VT = |
| 112 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
| 113 | if (!MVT::isInteger(VT)) |
| 114 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 115 | |
| 116 | TreePatternNode *OtherNode = |
| 117 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
| 118 | if (OtherNode->hasTypeSet() && |
| 119 | (!MVT::isInteger(OtherNode->getType()) || |
| 120 | OtherNode->getType() <= VT)) |
| 121 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
| 122 | return false; |
| 123 | } |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 129 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 130 | // SDNodeInfo implementation |
| 131 | // |
| 132 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 133 | EnumName = R->getValueAsString("Opcode"); |
| 134 | SDClassName = R->getValueAsString("SDClass"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 135 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 136 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 137 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 138 | |
| 139 | // Parse the type constraints. |
| 140 | ListInit *Constraints = TypeProfile->getValueAsListInit("Constraints"); |
| 141 | for (unsigned i = 0, e = Constraints->getSize(); i != e; ++i) { |
| 142 | assert(dynamic_cast<DefInit*>(Constraints->getElement(i)) && |
| 143 | "Constraints list should contain constraint definitions!"); |
| 144 | Record *Constraint = |
| 145 | static_cast<DefInit*>(Constraints->getElement(i))->getDef(); |
| 146 | TypeConstraints.push_back(Constraint); |
| 147 | } |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 148 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 149 | |
| 150 | //===----------------------------------------------------------------------===// |
| 151 | // TreePatternNode implementation |
| 152 | // |
| 153 | |
| 154 | TreePatternNode::~TreePatternNode() { |
| 155 | #if 0 // FIXME: implement refcounted tree nodes! |
| 156 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 157 | delete getChild(i); |
| 158 | #endif |
| 159 | } |
| 160 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 161 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 162 | /// information. If N already contains a conflicting type, then throw an |
| 163 | /// exception. This returns true if any information was updated. |
| 164 | /// |
| 165 | bool TreePatternNode::UpdateNodeType(MVT::ValueType VT, TreePattern &TP) { |
| 166 | if (VT == MVT::LAST_VALUETYPE || getType() == VT) return false; |
| 167 | if (getType() == MVT::LAST_VALUETYPE) { |
| 168 | setType(VT); |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | TP.error("Type inference contradiction found in node " + |
| 173 | getOperator()->getName() + "!"); |
| 174 | return true; // unreachable |
| 175 | } |
| 176 | |
| 177 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 178 | void TreePatternNode::print(std::ostream &OS) const { |
| 179 | if (isLeaf()) { |
| 180 | OS << *getLeafValue(); |
| 181 | } else { |
| 182 | OS << "(" << getOperator()->getName(); |
| 183 | } |
| 184 | |
| 185 | if (getType() == MVT::Other) |
| 186 | OS << ":Other"; |
| 187 | else if (getType() == MVT::LAST_VALUETYPE) |
| 188 | ;//OS << ":?"; |
| 189 | else |
| 190 | OS << ":" << getType(); |
| 191 | |
| 192 | if (!isLeaf()) { |
| 193 | if (getNumChildren() != 0) { |
| 194 | OS << " "; |
| 195 | getChild(0)->print(OS); |
| 196 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 197 | OS << ", "; |
| 198 | getChild(i)->print(OS); |
| 199 | } |
| 200 | } |
| 201 | OS << ")"; |
| 202 | } |
| 203 | |
| 204 | if (!PredicateFn.empty()) |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 205 | OS << "<<P:" << PredicateFn << ">>"; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 206 | if (TransformFn) |
| 207 | OS << "<<X:" << TransformFn->getName() << ">>"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 208 | if (!getName().empty()) |
| 209 | OS << ":$" << getName(); |
| 210 | |
| 211 | } |
| 212 | void TreePatternNode::dump() const { |
| 213 | print(std::cerr); |
| 214 | } |
| 215 | |
| 216 | /// clone - Make a copy of this tree and all of its children. |
| 217 | /// |
| 218 | TreePatternNode *TreePatternNode::clone() const { |
| 219 | TreePatternNode *New; |
| 220 | if (isLeaf()) { |
| 221 | New = new TreePatternNode(getLeafValue()); |
| 222 | } else { |
| 223 | std::vector<TreePatternNode*> CChildren; |
| 224 | CChildren.reserve(Children.size()); |
| 225 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 226 | CChildren.push_back(getChild(i)->clone()); |
| 227 | New = new TreePatternNode(getOperator(), CChildren); |
| 228 | } |
| 229 | New->setName(getName()); |
| 230 | New->setType(getType()); |
| 231 | New->setPredicateFn(getPredicateFn()); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 232 | New->setTransformFn(getTransformFn()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 233 | return New; |
| 234 | } |
| 235 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 236 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 237 | /// with actual values specified by ArgMap. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 238 | void TreePatternNode:: |
| 239 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 240 | if (isLeaf()) return; |
| 241 | |
| 242 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 243 | TreePatternNode *Child = getChild(i); |
| 244 | if (Child->isLeaf()) { |
| 245 | Init *Val = Child->getLeafValue(); |
| 246 | if (dynamic_cast<DefInit*>(Val) && |
| 247 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 248 | // We found a use of a formal argument, replace it with its value. |
| 249 | Child = ArgMap[Child->getName()]; |
| 250 | assert(Child && "Couldn't find formal argument!"); |
| 251 | setChild(i, Child); |
| 252 | } |
| 253 | } else { |
| 254 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 261 | /// fragments, inline them into place, giving us a pattern without any |
| 262 | /// PatFrag references. |
| 263 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 264 | if (isLeaf()) return this; // nothing to do. |
| 265 | Record *Op = getOperator(); |
| 266 | |
| 267 | if (!Op->isSubClassOf("PatFrag")) { |
| 268 | // Just recursively inline children nodes. |
| 269 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 270 | setChild(i, getChild(i)->InlinePatternFragments(TP)); |
| 271 | return this; |
| 272 | } |
| 273 | |
| 274 | // Otherwise, we found a reference to a fragment. First, look up its |
| 275 | // TreePattern record. |
| 276 | TreePattern *Frag = TP.getDAGISelEmitter().getPatternFragment(Op); |
| 277 | |
| 278 | // Verify that we are passing the right number of operands. |
| 279 | if (Frag->getNumArgs() != Children.size()) |
| 280 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 281 | utostr(Frag->getNumArgs()) + " operands!"); |
| 282 | |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 283 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 284 | |
| 285 | // Resolve formal arguments to their actual value. |
| 286 | if (Frag->getNumArgs()) { |
| 287 | // Compute the map of formal to actual arguments. |
| 288 | std::map<std::string, TreePatternNode*> ArgMap; |
| 289 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 290 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 291 | |
| 292 | FragTree->SubstituteFormalArguments(ArgMap); |
| 293 | } |
| 294 | |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 295 | FragTree->setName(getName()); |
| 296 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 297 | // Get a new copy of this fragment to stitch into here. |
| 298 | //delete this; // FIXME: implement refcounting! |
| 299 | return FragTree; |
| 300 | } |
| 301 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 302 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 303 | /// this node and its children in the tree. This returns true if it makes a |
| 304 | /// change, false otherwise. If a type contradiction is found, throw an |
| 305 | /// exception. |
| 306 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP) { |
| 307 | if (isLeaf()) return false; |
| 308 | |
| 309 | // special handling for set, which isn't really an SDNode. |
| 310 | if (getOperator()->getName() == "set") { |
| 311 | assert (getNumChildren() == 2 && "Only handle 2 operand set's for now!"); |
| 312 | bool MadeChange = getChild(0)->ApplyTypeConstraints(TP); |
| 313 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP); |
| 314 | |
| 315 | // Types of operands must match. |
| 316 | MadeChange |= getChild(0)->UpdateNodeType(getChild(1)->getType(), TP); |
| 317 | MadeChange |= getChild(1)->UpdateNodeType(getChild(0)->getType(), TP); |
| 318 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 319 | return MadeChange; |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 320 | } else if (getOperator()->isSubClassOf("SDNode")) { |
| 321 | const SDNodeInfo &NI = TP.getDAGISelEmitter().getSDNodeInfo(getOperator()); |
| 322 | |
| 323 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 324 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 325 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP); |
| 326 | return MadeChange; |
| 327 | } else { |
| 328 | assert(getOperator()->isSubClassOf("Instruction") && "Unknown node type!"); |
| 329 | // TODO: type inference for instructions. |
| 330 | return false; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 331 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 335 | //===----------------------------------------------------------------------===// |
| 336 | // TreePattern implementation |
| 337 | // |
| 338 | |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 339 | TreePattern::TreePattern(Record *TheRec, const std::vector<DagInit *> &RawPat, |
| 340 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 341 | |
| 342 | for (unsigned i = 0, e = RawPat.size(); i != e; ++i) |
| 343 | Trees.push_back(ParseTreePattern(RawPat[i])); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | void TreePattern::error(const std::string &Msg) const { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 347 | dump(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 348 | throw "In " + TheRecord->getName() + ": " + Msg; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /// getIntrinsicType - Check to see if the specified record has an intrinsic |
| 352 | /// type which should be applied to it. This infer the type of register |
| 353 | /// references from the register file information, for example. |
| 354 | /// |
| 355 | MVT::ValueType TreePattern::getIntrinsicType(Record *R) const { |
| 356 | // Check to see if this is a register or a register class... |
| 357 | if (R->isSubClassOf("RegisterClass")) |
| 358 | return getValueType(R->getValueAsDef("RegType")); |
| 359 | else if (R->isSubClassOf("PatFrag")) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 360 | // Pattern fragment types will be resolved when they are inlined. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 361 | return MVT::LAST_VALUETYPE; |
| 362 | } else if (R->isSubClassOf("Register")) { |
| 363 | assert(0 && "Explicit registers not handled here yet!\n"); |
| 364 | return MVT::LAST_VALUETYPE; |
| 365 | } else if (R->isSubClassOf("ValueType")) { |
| 366 | // Using a VTSDNode. |
| 367 | return MVT::Other; |
| 368 | } else if (R->getName() == "node") { |
| 369 | // Placeholder. |
| 370 | return MVT::LAST_VALUETYPE; |
| 371 | } |
| 372 | |
| 373 | error("Unknown value used: " + R->getName()); |
| 374 | return MVT::Other; |
| 375 | } |
| 376 | |
| 377 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 378 | Record *Operator = Dag->getNodeType(); |
| 379 | |
| 380 | if (Operator->isSubClassOf("ValueType")) { |
| 381 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 382 | // node. |
| 383 | if (Dag->getNumArgs() != 1) |
| 384 | error("Type cast only valid for a leaf node!"); |
| 385 | |
| 386 | Init *Arg = Dag->getArg(0); |
| 387 | TreePatternNode *New; |
| 388 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
| 389 | New = new TreePatternNode(DI); |
| 390 | // If it's a regclass or something else known, set the type. |
| 391 | New->setType(getIntrinsicType(DI->getDef())); |
| 392 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 393 | New = ParseTreePattern(DI); |
| 394 | } else { |
| 395 | Arg->dump(); |
| 396 | error("Unknown leaf value for tree pattern!"); |
| 397 | return 0; |
| 398 | } |
| 399 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 400 | // Apply the type cast. |
| 401 | New->UpdateNodeType(getValueType(Operator), *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 402 | return New; |
| 403 | } |
| 404 | |
| 405 | // Verify that this is something that makes sense for an operator. |
| 406 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 407 | !Operator->isSubClassOf("Instruction") && |
| 408 | !Operator->isSubClassOf("SDNodeXForm") && |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 409 | Operator->getName() != "set") |
| 410 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 411 | |
| 412 | std::vector<TreePatternNode*> Children; |
| 413 | |
| 414 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 415 | Init *Arg = Dag->getArg(i); |
| 416 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 417 | Children.push_back(ParseTreePattern(DI)); |
| 418 | Children.back()->setName(Dag->getArgName(i)); |
| 419 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 420 | Record *R = DefI->getDef(); |
| 421 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 422 | // TreePatternNode if its own. |
| 423 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 424 | Dag->setArg(i, new DagInit(R, |
| 425 | std::vector<std::pair<Init*, std::string> >())); |
| 426 | --i; // Revisit this node... |
| 427 | } else { |
| 428 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 429 | Node->setName(Dag->getArgName(i)); |
| 430 | Children.push_back(Node); |
| 431 | |
| 432 | // If it's a regclass or something else known, set the type. |
| 433 | Node->setType(getIntrinsicType(R)); |
| 434 | |
| 435 | // Input argument? |
| 436 | if (R->getName() == "node") { |
| 437 | if (Dag->getArgName(i).empty()) |
| 438 | error("'node' argument requires a name to match with operand list"); |
| 439 | Args.push_back(Dag->getArgName(i)); |
| 440 | } |
| 441 | } |
| 442 | } else { |
| 443 | Arg->dump(); |
| 444 | error("Unknown leaf value for tree pattern!"); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return new TreePatternNode(Operator, Children); |
| 449 | } |
| 450 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 451 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 452 | /// patterns as possible. Return true if all types are infered, false |
| 453 | /// otherwise. Throw an exception if a type contradiction is found. |
| 454 | bool TreePattern::InferAllTypes() { |
| 455 | bool MadeChange = true; |
| 456 | while (MadeChange) { |
| 457 | MadeChange = false; |
| 458 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 459 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this); |
| 460 | } |
| 461 | |
| 462 | bool HasUnresolvedTypes = false; |
| 463 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 464 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 465 | return !HasUnresolvedTypes; |
| 466 | } |
| 467 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 468 | void TreePattern::print(std::ostream &OS) const { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 469 | OS << getRecord()->getName(); |
| 470 | if (!Args.empty()) { |
| 471 | OS << "(" << Args[0]; |
| 472 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 473 | OS << ", " << Args[i]; |
| 474 | OS << ")"; |
| 475 | } |
| 476 | OS << ": "; |
| 477 | |
| 478 | if (Trees.size() > 1) |
| 479 | OS << "[\n"; |
| 480 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 481 | OS << "\t"; |
| 482 | Trees[i]->print(OS); |
| 483 | OS << "\n"; |
| 484 | } |
| 485 | |
| 486 | if (Trees.size() > 1) |
| 487 | OS << "]\n"; |
| 488 | } |
| 489 | |
| 490 | void TreePattern::dump() const { print(std::cerr); } |
| 491 | |
| 492 | |
| 493 | |
| 494 | //===----------------------------------------------------------------------===// |
| 495 | // DAGISelEmitter implementation |
| 496 | // |
| 497 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 498 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
| 499 | void DAGISelEmitter::ParseNodeInfo() { |
| 500 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 501 | while (!Nodes.empty()) { |
| 502 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 503 | Nodes.pop_back(); |
| 504 | } |
| 505 | } |
| 506 | |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 507 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 508 | /// map, and emit them to the file as functions. |
| 509 | void DAGISelEmitter::ParseNodeTransforms(std::ostream &OS) { |
| 510 | OS << "\n// Node transformations.\n"; |
| 511 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 512 | while (!Xforms.empty()) { |
| 513 | Record *XFormNode = Xforms.back(); |
| 514 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 515 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
| 516 | SDNodeXForms.insert(std::make_pair(XFormNode, |
| 517 | std::make_pair(SDNode, Code))); |
| 518 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 519 | if (!Code.empty()) { |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 520 | std::string ClassName = getSDNodeInfo(SDNode).getSDClassName(); |
| 521 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 522 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 523 | OS << "inline SDOperand Transform_" << XFormNode->getName() |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 524 | << "(SDNode *" << C2 << ") {\n"; |
| 525 | if (ClassName != "SDNode") |
| 526 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 527 | OS << Code << "\n}\n"; |
| 528 | } |
| 529 | |
| 530 | Xforms.pop_back(); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | |
| 535 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 536 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 537 | /// file, building up the PatternFragments map. After we've collected them all, |
| 538 | /// inline fragments together as necessary, so that there are no references left |
| 539 | /// inside a pattern fragment to a pattern fragment. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 540 | /// |
| 541 | /// This also emits all of the predicate functions to the output file. |
| 542 | /// |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 543 | void DAGISelEmitter::ParsePatternFragments(std::ostream &OS) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 544 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 545 | |
| 546 | // First step, parse all of the fragments and emit predicate functions. |
| 547 | OS << "\n// Predicate functions.\n"; |
| 548 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
| 549 | std::vector<DagInit*> Trees; |
| 550 | Trees.push_back(Fragments[i]->getValueAsDag("Fragment")); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 551 | TreePattern *P = new TreePattern(Fragments[i], Trees, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 552 | PatternFragments[Fragments[i]] = P; |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 553 | |
| 554 | // Validate the argument list, converting it to map, to discard duplicates. |
| 555 | std::vector<std::string> &Args = P->getArgList(); |
| 556 | std::set<std::string> OperandsMap(Args.begin(), Args.end()); |
| 557 | |
| 558 | if (OperandsMap.count("")) |
| 559 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 560 | |
| 561 | // Parse the operands list. |
| 562 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 563 | if (OpsList->getNodeType()->getName() != "ops") |
| 564 | P->error("Operands list should start with '(ops ... '!"); |
| 565 | |
| 566 | // Copy over the arguments. |
| 567 | Args.clear(); |
| 568 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 569 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 570 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 571 | getDef()->getName() != "node") |
| 572 | P->error("Operands list should all be 'node' values."); |
| 573 | if (OpsList->getArgName(j).empty()) |
| 574 | P->error("Operands list should have names for each operand!"); |
| 575 | if (!OperandsMap.count(OpsList->getArgName(j))) |
| 576 | P->error("'" + OpsList->getArgName(j) + |
| 577 | "' does not occur in pattern or was multiply specified!"); |
| 578 | OperandsMap.erase(OpsList->getArgName(j)); |
| 579 | Args.push_back(OpsList->getArgName(j)); |
| 580 | } |
| 581 | |
| 582 | if (!OperandsMap.empty()) |
| 583 | P->error("Operands list does not contain an entry for operand '" + |
| 584 | *OperandsMap.begin() + "'!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 585 | |
| 586 | // If there is a code init for this fragment, emit the predicate code and |
| 587 | // keep track of the fact that this fragment uses it. |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 588 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
| 589 | if (!Code.empty()) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 590 | assert(!P->getOnlyTree()->isLeaf() && "Can't be a leaf!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 591 | std::string ClassName = |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 592 | getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 593 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 594 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 595 | OS << "inline bool Predicate_" << Fragments[i]->getName() |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 596 | << "(SDNode *" << C2 << ") {\n"; |
| 597 | if (ClassName != "SDNode") |
| 598 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 599 | OS << Code << "\n}\n"; |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 600 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | 6de8b53 | 2005-09-13 21:59:15 +0000 | [diff] [blame] | 602 | |
| 603 | // If there is a node transformation corresponding to this, keep track of |
| 604 | // it. |
| 605 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 606 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 607 | P->getOnlyTree()->setTransformFn(Transform); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | OS << "\n\n"; |
| 611 | |
| 612 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 613 | // that there are not references to PatFrags left inside of them. |
| 614 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 615 | E = PatternFragments.end(); I != E; ++I) { |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 616 | TreePattern *ThePat = I->second; |
| 617 | ThePat->InlinePatternFragments(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 619 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 620 | // all of them, some may depend on the inputs of the pattern. |
| 621 | try { |
| 622 | ThePat->InferAllTypes(); |
| 623 | } catch (...) { |
| 624 | // If this pattern fragment is not supported by this target (no types can |
| 625 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 626 | // actually used by instructions, the type consistency error will be |
| 627 | // reported there. |
| 628 | } |
| 629 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 630 | // If debugging, print out the pattern fragment result. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 631 | DEBUG(ThePat->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 632 | } |
| 633 | } |
| 634 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 635 | /// 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] | 636 | /// instruction input. Return true if this is a real use. |
| 637 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 638 | std::map<std::string, TreePatternNode*> &InstInputs) { |
| 639 | // No name -> not interesting. |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 640 | if (Pat->getName().empty()) { |
| 641 | if (Pat->isLeaf()) { |
| 642 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 643 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 644 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
| 645 | |
| 646 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 647 | return false; |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 648 | } |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 649 | |
| 650 | Record *Rec; |
| 651 | if (Pat->isLeaf()) { |
| 652 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 653 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 654 | Rec = DI->getDef(); |
| 655 | } else { |
| 656 | assert(Pat->getNumChildren() == 0 && "can't be a use with children!"); |
| 657 | Rec = Pat->getOperator(); |
| 658 | } |
| 659 | |
| 660 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 661 | if (!Slot) { |
| 662 | Slot = Pat; |
| 663 | } else { |
| 664 | Record *SlotRec; |
| 665 | if (Slot->isLeaf()) { |
| 666 | Rec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
| 667 | } else { |
| 668 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 669 | SlotRec = Slot->getOperator(); |
| 670 | } |
| 671 | |
| 672 | // Ensure that the inputs agree if we've already seen this input. |
| 673 | if (Rec != SlotRec) |
| 674 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 675 | if (Slot->getType() != Pat->getType()) |
| 676 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 677 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 678 | return true; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 682 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 683 | /// the pattern. Report errors if we see anything naughty. |
| 684 | void DAGISelEmitter:: |
| 685 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 686 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 687 | std::map<std::string, Record*> &InstResults) { |
| 688 | if (Pat->isLeaf()) { |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 689 | bool isUse = HandleUse(I, Pat, InstInputs); |
| 690 | if (!isUse && Pat->getTransformFn()) |
| 691 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 692 | return; |
| 693 | } else if (Pat->getOperator()->getName() != "set") { |
| 694 | // If this is not a set, verify that the children nodes are not void typed, |
| 695 | // and recurse. |
| 696 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
| 697 | if (Pat->getChild(i)->getType() == MVT::isVoid) |
| 698 | I->error("Cannot have void nodes inside of patterns!"); |
| 699 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults); |
| 700 | } |
| 701 | |
| 702 | // If this is a non-leaf node with no children, treat it basically as if |
| 703 | // it were a leaf. This handles nodes like (imm). |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 704 | bool isUse = false; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 705 | if (Pat->getNumChildren() == 0) |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 706 | isUse = HandleUse(I, Pat, InstInputs); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 707 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 708 | if (!isUse && Pat->getTransformFn()) |
| 709 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | |
| 713 | // Otherwise, this is a set, validate and collect instruction results. |
| 714 | if (Pat->getNumChildren() == 0) |
| 715 | I->error("set requires operands!"); |
| 716 | else if (Pat->getNumChildren() & 1) |
| 717 | I->error("set requires an even number of operands"); |
| 718 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 719 | if (Pat->getTransformFn()) |
| 720 | I->error("Cannot specify a transform function on a set node!"); |
| 721 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 722 | // Check the set destinations. |
| 723 | unsigned NumValues = Pat->getNumChildren()/2; |
| 724 | for (unsigned i = 0; i != NumValues; ++i) { |
| 725 | TreePatternNode *Dest = Pat->getChild(i); |
| 726 | if (!Dest->isLeaf()) |
| 727 | I->error("set destination should be a virtual register!"); |
| 728 | |
| 729 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 730 | if (!Val) |
| 731 | I->error("set destination should be a virtual register!"); |
| 732 | |
| 733 | if (!Val->getDef()->isSubClassOf("RegisterClass")) |
| 734 | I->error("set destination should be a virtual register!"); |
| 735 | if (Dest->getName().empty()) |
| 736 | I->error("set destination must have a name!"); |
| 737 | if (InstResults.count(Dest->getName())) |
| 738 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
| 739 | InstResults[Dest->getName()] = Val->getDef(); |
| 740 | |
| 741 | // Verify and collect info from the computation. |
| 742 | FindPatternInputsAndOutputs(I, Pat->getChild(i+NumValues), |
| 743 | InstInputs, InstResults); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 748 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 749 | /// any fragments involved. This populates the Instructions list with fully |
| 750 | /// resolved instructions. |
| 751 | void DAGISelEmitter::ParseInstructions() { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 752 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 753 | |
| 754 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
| 755 | if (!dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 756 | continue; // no pattern yet, ignore it. |
| 757 | |
| 758 | ListInit *LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 759 | if (LI->getSize() == 0) continue; // no pattern. |
| 760 | |
| 761 | std::vector<DagInit*> Trees; |
| 762 | for (unsigned j = 0, e = LI->getSize(); j != e; ++j) |
| 763 | Trees.push_back((DagInit*)LI->getElement(j)); |
| 764 | |
| 765 | // Parse the instruction. |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 766 | TreePattern *I = new TreePattern(Instrs[i], Trees, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 767 | // Inline pattern fragments into it. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 768 | I->InlinePatternFragments(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 95f6b76 | 2005-09-08 23:26:30 +0000 | [diff] [blame] | 770 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 771 | // never do anything with this instruction pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 772 | if (!I->InferAllTypes()) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 773 | I->error("Could not infer all types in pattern!"); |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 774 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 775 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 776 | // with the record they are declared as. |
| 777 | std::map<std::string, TreePatternNode*> InstInputs; |
| 778 | |
| 779 | // InstResults - Keep track of all the virtual registers that are 'set' |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 780 | // in the instruction, including what reg class they are. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 781 | std::map<std::string, Record*> InstResults; |
| 782 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 783 | // 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] | 784 | // fill in the InstResults map. |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 785 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 786 | TreePatternNode *Pat = I->getTree(j); |
| 787 | if (Pat->getType() != MVT::isVoid) { |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 788 | I->dump(); |
| 789 | I->error("Top-level forms in instruction pattern should have" |
| 790 | " void types"); |
| 791 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 792 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 793 | // Find inputs and outputs, and verify the structure of the uses/defs. |
| 794 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 796 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 797 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 798 | // list for the instruction. This determines the order that operands are |
| 799 | // added to the machine instruction the node corresponds to. |
| 800 | unsigned NumResults = InstResults.size(); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 801 | |
| 802 | // Parse the operands list from the (ops) list, validating it. |
| 803 | std::vector<std::string> &Args = I->getArgList(); |
| 804 | assert(Args.empty() && "Args list should still be empty here!"); |
| 805 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 806 | |
| 807 | // Check that all of the results occur first in the list. |
| 808 | for (unsigned i = 0; i != NumResults; ++i) { |
Chris Lattner | 3a7319d | 2005-09-14 21:04:12 +0000 | [diff] [blame] | 809 | if (i == CGI.OperandList.size()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 810 | I->error("'" + InstResults.begin()->first + |
| 811 | "' set but does not appear in operand list!"); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 812 | const std::string &OpName = CGI.OperandList[i].Name; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 813 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 814 | // Check that it exists in InstResults. |
| 815 | Record *R = InstResults[OpName]; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 816 | if (R == 0) |
| 817 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 818 | "outputs must occur before inputs in operand list!"); |
| 819 | |
| 820 | if (CGI.OperandList[i].Rec != R) |
| 821 | I->error("Operand $" + OpName + " class mismatch!"); |
| 822 | |
| 823 | // Okay, this one checks out. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 824 | InstResults.erase(OpName); |
| 825 | } |
| 826 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 827 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 828 | // the copy while we're checking the inputs. |
| 829 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 830 | |
| 831 | std::vector<TreePatternNode*> ResultNodeOperands; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 832 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 833 | const std::string &OpName = CGI.OperandList[i].Name; |
| 834 | if (OpName.empty()) |
| 835 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 836 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 837 | if (!InstInputsCheck.count(OpName)) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 838 | I->error("Operand $" + OpName + |
| 839 | " does not appear in the instruction pattern"); |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 840 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 841 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 842 | if (CGI.OperandList[i].Ty != InVal->getType()) |
| 843 | I->error("Operand $" + OpName + |
| 844 | "'s type disagrees between the operand and pattern"); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 845 | |
Chris Lattner | 2175c18 | 2005-09-14 23:01:59 +0000 | [diff] [blame] | 846 | // Construct the result for the dest-pattern operand list. |
| 847 | TreePatternNode *OpNode = InVal->clone(); |
| 848 | |
| 849 | // No predicate is useful on the result. |
| 850 | OpNode->setPredicateFn(""); |
| 851 | |
| 852 | // Promote the xform function to be an explicit node if set. |
| 853 | if (Record *Xform = OpNode->getTransformFn()) { |
| 854 | OpNode->setTransformFn(0); |
| 855 | std::vector<TreePatternNode*> Children; |
| 856 | Children.push_back(OpNode); |
| 857 | OpNode = new TreePatternNode(Xform, Children); |
| 858 | } |
| 859 | |
| 860 | ResultNodeOperands.push_back(OpNode); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 863 | if (!InstInputsCheck.empty()) |
| 864 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 865 | " occurs in pattern but not in operands list!"); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 866 | |
| 867 | TreePatternNode *ResultPattern = |
| 868 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
| 869 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 870 | unsigned NumOperands = CGI.OperandList.size()-NumResults; |
| 871 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 872 | DEBUG(I->dump()); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 873 | Instructions.push_back(DAGInstruction(I, NumResults, NumOperands, |
| 874 | ResultPattern)); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 875 | } |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 876 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 877 | // If we can, convert the instructions to be patterns that are matched! |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 878 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) { |
Chris Lattner | ec67643 | 2005-09-14 04:03:16 +0000 | [diff] [blame] | 879 | TreePattern *I = Instructions[i].getPattern(); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 880 | |
| 881 | if (I->getNumTrees() != 1) { |
| 882 | std::cerr << "CANNOT HANDLE: " << I->getRecord()->getName() << " yet!"; |
| 883 | continue; |
| 884 | } |
| 885 | TreePatternNode *Pattern = I->getTree(0); |
| 886 | if (Pattern->getOperator()->getName() != "set") |
| 887 | continue; // Not a set (store or something?) |
| 888 | |
| 889 | if (Pattern->getNumChildren() != 2) |
| 890 | continue; // Not a set of a single value (not handled so far) |
| 891 | |
| 892 | TreePatternNode *SrcPattern = Pattern->getChild(1)->clone(); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 893 | TreePatternNode *DstPattern = Instructions[i].getResultPattern(); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 894 | PatternsToMatch.push_back(std::make_pair(SrcPattern, DstPattern)); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 895 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 898 | void DAGISelEmitter::ParsePatterns() { |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 899 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 900 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 901 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
| 902 | std::vector<DagInit*> Trees; |
| 903 | Trees.push_back(Patterns[i]->getValueAsDag("PatternToMatch")); |
| 904 | TreePattern *Pattern = new TreePattern(Patterns[i], Trees, *this); |
| 905 | Trees.clear(); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 906 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 907 | // Inline pattern fragments into it. |
| 908 | Pattern->InlinePatternFragments(); |
| 909 | |
| 910 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 911 | // never do anything with this pattern: report it to the user. |
| 912 | if (!Pattern->InferAllTypes()) |
| 913 | Pattern->error("Could not infer all types in pattern!"); |
| 914 | |
| 915 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 916 | if (LI->getSize() == 0) continue; // no pattern. |
| 917 | for (unsigned j = 0, e = LI->getSize(); j != e; ++j) |
| 918 | Trees.push_back((DagInit*)LI->getElement(j)); |
| 919 | |
| 920 | // Parse the instruction. |
| 921 | TreePattern *Result = new TreePattern(Patterns[i], Trees, *this); |
| 922 | |
| 923 | // Inline pattern fragments into it. |
| 924 | Result->InlinePatternFragments(); |
| 925 | |
| 926 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 927 | // never do anything with this pattern: report it to the user. |
| 928 | #if 0 // FIXME: ENABLE when we can infer though instructions! |
| 929 | if (!Result->InferAllTypes()) |
| 930 | Result->error("Could not infer all types in pattern!"); |
| 931 | #endif |
| 932 | |
| 933 | if (Result->getNumTrees() != 1) |
| 934 | Result->error("Cannot handle instructions producing instructions " |
| 935 | "with temporaries yet!"); |
| 936 | PatternsToMatch.push_back(std::make_pair(Pattern->getOnlyTree(), |
| 937 | Result->getOnlyTree())); |
| 938 | } |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 939 | |
| 940 | DEBUG(std::cerr << "\n\nPARSED PATTERNS TO MATCH:\n\n"; |
| 941 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 942 | std::cerr << "PATTERN: "; PatternsToMatch[i].first->dump(); |
| 943 | std::cerr << "\nRESULT: ";PatternsToMatch[i].second->dump(); |
| 944 | std::cerr << "\n"; |
| 945 | }); |
| 946 | } |
| 947 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 948 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
| 949 | // Emit boilerplate. |
| 950 | OS << "// The main instruction selector code.\n" |
Chris Lattner | f4f33ba | 2005-09-13 22:05:02 +0000 | [diff] [blame] | 951 | << "SDOperand SelectCode(SDOperand Op) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 952 | << " SDNode *N = Op.Val;\n" |
| 953 | << " if (N->getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
| 954 | << " N->getOpcode() < PPCISD::FIRST_NUMBER)\n" |
| 955 | << " return Op; // Already selected.\n\n" |
| 956 | << " switch (N->getOpcode()) {\n" |
| 957 | << " default: break;\n" |
| 958 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
| 959 | << " return Op;\n" |
| 960 | << " case ISD::AssertSext:\n" |
| 961 | << " case ISD::AssertZext:\n" |
| 962 | << " return Select(N->getOperand(0));\n"; |
| 963 | |
| 964 | |
| 965 | |
| 966 | OS << " } // end of big switch.\n\n" |
| 967 | << " std::cerr << \"Cannot yet select: \";\n" |
| 968 | << " N->dump();\n" |
| 969 | << " std::cerr << '\\n';\n" |
| 970 | << " abort();\n" |
| 971 | << "}\n"; |
| 972 | } |
| 973 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 974 | void DAGISelEmitter::run(std::ostream &OS) { |
| 975 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 976 | " target", OS); |
| 977 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 978 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 979 | << "// *** instruction selector class. These functions are really " |
| 980 | << "methods.\n\n"; |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 981 | ParseNodeInfo(); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 982 | ParseNodeTransforms(OS); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 983 | ParsePatternFragments(OS); |
| 984 | ParseInstructions(); |
| 985 | ParsePatterns(); |
| 986 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 987 | // TODO: convert some instructions to expanders if needed or something. |
| 988 | |
| 989 | EmitInstructionSelector(OS); |
| 990 | |
| 991 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 992 | E = PatternFragments.end(); I != E; ++I) |
| 993 | delete I->second; |
| 994 | PatternFragments.clear(); |
| 995 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 996 | Instructions.clear(); |
| 997 | } |