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 << ">>"; |
| 206 | if (!TransformFn.empty()) |
| 207 | OS << "<<X:" << TransformFn << ">>"; |
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; |
| 320 | } |
| 321 | |
| 322 | const SDNodeInfo &NI = TP.getDAGISelEmitter().getSDNodeInfo(getOperator()); |
| 323 | |
| 324 | bool MadeChange = NI.ApplyTypeConstraints(this, TP); |
| 325 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 326 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP); |
| 327 | return MadeChange; |
| 328 | } |
| 329 | |
| 330 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 331 | //===----------------------------------------------------------------------===// |
| 332 | // TreePattern implementation |
| 333 | // |
| 334 | |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 335 | TreePattern::TreePattern(Record *TheRec, const std::vector<DagInit *> &RawPat, |
| 336 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 337 | |
| 338 | for (unsigned i = 0, e = RawPat.size(); i != e; ++i) |
| 339 | Trees.push_back(ParseTreePattern(RawPat[i])); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void TreePattern::error(const std::string &Msg) const { |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 343 | throw "In " + TheRecord->getName() + ": " + Msg; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | /// getIntrinsicType - Check to see if the specified record has an intrinsic |
| 347 | /// type which should be applied to it. This infer the type of register |
| 348 | /// references from the register file information, for example. |
| 349 | /// |
| 350 | MVT::ValueType TreePattern::getIntrinsicType(Record *R) const { |
| 351 | // Check to see if this is a register or a register class... |
| 352 | if (R->isSubClassOf("RegisterClass")) |
| 353 | return getValueType(R->getValueAsDef("RegType")); |
| 354 | else if (R->isSubClassOf("PatFrag")) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 355 | // Pattern fragment types will be resolved when they are inlined. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 356 | return MVT::LAST_VALUETYPE; |
| 357 | } else if (R->isSubClassOf("Register")) { |
| 358 | assert(0 && "Explicit registers not handled here yet!\n"); |
| 359 | return MVT::LAST_VALUETYPE; |
| 360 | } else if (R->isSubClassOf("ValueType")) { |
| 361 | // Using a VTSDNode. |
| 362 | return MVT::Other; |
| 363 | } else if (R->getName() == "node") { |
| 364 | // Placeholder. |
| 365 | return MVT::LAST_VALUETYPE; |
| 366 | } |
| 367 | |
| 368 | error("Unknown value used: " + R->getName()); |
| 369 | return MVT::Other; |
| 370 | } |
| 371 | |
| 372 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 373 | Record *Operator = Dag->getNodeType(); |
| 374 | |
| 375 | if (Operator->isSubClassOf("ValueType")) { |
| 376 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 377 | // node. |
| 378 | if (Dag->getNumArgs() != 1) |
| 379 | error("Type cast only valid for a leaf node!"); |
| 380 | |
| 381 | Init *Arg = Dag->getArg(0); |
| 382 | TreePatternNode *New; |
| 383 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
| 384 | New = new TreePatternNode(DI); |
| 385 | // If it's a regclass or something else known, set the type. |
| 386 | New->setType(getIntrinsicType(DI->getDef())); |
| 387 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 388 | New = ParseTreePattern(DI); |
| 389 | } else { |
| 390 | Arg->dump(); |
| 391 | error("Unknown leaf value for tree pattern!"); |
| 392 | return 0; |
| 393 | } |
| 394 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 395 | // Apply the type cast. |
| 396 | New->UpdateNodeType(getValueType(Operator), *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 397 | return New; |
| 398 | } |
| 399 | |
| 400 | // Verify that this is something that makes sense for an operator. |
| 401 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
| 402 | Operator->getName() != "set") |
| 403 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 404 | |
| 405 | std::vector<TreePatternNode*> Children; |
| 406 | |
| 407 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 408 | Init *Arg = Dag->getArg(i); |
| 409 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 410 | Children.push_back(ParseTreePattern(DI)); |
| 411 | Children.back()->setName(Dag->getArgName(i)); |
| 412 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 413 | Record *R = DefI->getDef(); |
| 414 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 415 | // TreePatternNode if its own. |
| 416 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 417 | Dag->setArg(i, new DagInit(R, |
| 418 | std::vector<std::pair<Init*, std::string> >())); |
| 419 | --i; // Revisit this node... |
| 420 | } else { |
| 421 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 422 | Node->setName(Dag->getArgName(i)); |
| 423 | Children.push_back(Node); |
| 424 | |
| 425 | // If it's a regclass or something else known, set the type. |
| 426 | Node->setType(getIntrinsicType(R)); |
| 427 | |
| 428 | // Input argument? |
| 429 | if (R->getName() == "node") { |
| 430 | if (Dag->getArgName(i).empty()) |
| 431 | error("'node' argument requires a name to match with operand list"); |
| 432 | Args.push_back(Dag->getArgName(i)); |
| 433 | } |
| 434 | } |
| 435 | } else { |
| 436 | Arg->dump(); |
| 437 | error("Unknown leaf value for tree pattern!"); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return new TreePatternNode(Operator, Children); |
| 442 | } |
| 443 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 444 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 445 | /// patterns as possible. Return true if all types are infered, false |
| 446 | /// otherwise. Throw an exception if a type contradiction is found. |
| 447 | bool TreePattern::InferAllTypes() { |
| 448 | bool MadeChange = true; |
| 449 | while (MadeChange) { |
| 450 | MadeChange = false; |
| 451 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 452 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this); |
| 453 | } |
| 454 | |
| 455 | bool HasUnresolvedTypes = false; |
| 456 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 457 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 458 | return !HasUnresolvedTypes; |
| 459 | } |
| 460 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 461 | void TreePattern::print(std::ostream &OS) const { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 462 | OS << getRecord()->getName(); |
| 463 | if (!Args.empty()) { |
| 464 | OS << "(" << Args[0]; |
| 465 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 466 | OS << ", " << Args[i]; |
| 467 | OS << ")"; |
| 468 | } |
| 469 | OS << ": "; |
| 470 | |
| 471 | if (Trees.size() > 1) |
| 472 | OS << "[\n"; |
| 473 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 474 | OS << "\t"; |
| 475 | Trees[i]->print(OS); |
| 476 | OS << "\n"; |
| 477 | } |
| 478 | |
| 479 | if (Trees.size() > 1) |
| 480 | OS << "]\n"; |
| 481 | } |
| 482 | |
| 483 | void TreePattern::dump() const { print(std::cerr); } |
| 484 | |
| 485 | |
| 486 | |
| 487 | //===----------------------------------------------------------------------===// |
| 488 | // DAGISelEmitter implementation |
| 489 | // |
| 490 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 491 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
| 492 | void DAGISelEmitter::ParseNodeInfo() { |
| 493 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 494 | while (!Nodes.empty()) { |
| 495 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 496 | Nodes.pop_back(); |
| 497 | } |
| 498 | } |
| 499 | |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 500 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 501 | /// map, and emit them to the file as functions. |
| 502 | void DAGISelEmitter::ParseNodeTransforms(std::ostream &OS) { |
| 503 | OS << "\n// Node transformations.\n"; |
| 504 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 505 | while (!Xforms.empty()) { |
| 506 | Record *XFormNode = Xforms.back(); |
| 507 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 508 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
| 509 | SDNodeXForms.insert(std::make_pair(XFormNode, |
| 510 | std::make_pair(SDNode, Code))); |
| 511 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 512 | if (!Code.empty()) { |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 513 | std::string ClassName = getSDNodeInfo(SDNode).getSDClassName(); |
| 514 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 515 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 516 | OS << "inline SDOperand Transform_" << XFormNode->getName() |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 517 | << "(SDNode *" << C2 << ") {\n"; |
| 518 | if (ClassName != "SDNode") |
| 519 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 520 | OS << Code << "\n}\n"; |
| 521 | } |
| 522 | |
| 523 | Xforms.pop_back(); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | |
| 528 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 529 | /// ParseAndResolvePatternFragments - Parse all of the PatFrag definitions in |
| 530 | /// the .td file, building up the PatternFragments map. After we've collected |
| 531 | /// them all, inline fragments together as necessary, so that there are no |
| 532 | /// references left inside a pattern fragment to a pattern fragment. |
| 533 | /// |
| 534 | /// This also emits all of the predicate functions to the output file. |
| 535 | /// |
| 536 | void DAGISelEmitter::ParseAndResolvePatternFragments(std::ostream &OS) { |
| 537 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 538 | |
| 539 | // First step, parse all of the fragments and emit predicate functions. |
| 540 | OS << "\n// Predicate functions.\n"; |
| 541 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
| 542 | std::vector<DagInit*> Trees; |
| 543 | Trees.push_back(Fragments[i]->getValueAsDag("Fragment")); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 544 | TreePattern *P = new TreePattern(Fragments[i], Trees, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 545 | PatternFragments[Fragments[i]] = P; |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 546 | |
| 547 | // Validate the argument list, converting it to map, to discard duplicates. |
| 548 | std::vector<std::string> &Args = P->getArgList(); |
| 549 | std::set<std::string> OperandsMap(Args.begin(), Args.end()); |
| 550 | |
| 551 | if (OperandsMap.count("")) |
| 552 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 553 | |
| 554 | // Parse the operands list. |
| 555 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 556 | if (OpsList->getNodeType()->getName() != "ops") |
| 557 | P->error("Operands list should start with '(ops ... '!"); |
| 558 | |
| 559 | // Copy over the arguments. |
| 560 | Args.clear(); |
| 561 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 562 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 563 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 564 | getDef()->getName() != "node") |
| 565 | P->error("Operands list should all be 'node' values."); |
| 566 | if (OpsList->getArgName(j).empty()) |
| 567 | P->error("Operands list should have names for each operand!"); |
| 568 | if (!OperandsMap.count(OpsList->getArgName(j))) |
| 569 | P->error("'" + OpsList->getArgName(j) + |
| 570 | "' does not occur in pattern or was multiply specified!"); |
| 571 | OperandsMap.erase(OpsList->getArgName(j)); |
| 572 | Args.push_back(OpsList->getArgName(j)); |
| 573 | } |
| 574 | |
| 575 | if (!OperandsMap.empty()) |
| 576 | P->error("Operands list does not contain an entry for operand '" + |
| 577 | *OperandsMap.begin() + "'!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 578 | |
| 579 | // If there is a code init for this fragment, emit the predicate code and |
| 580 | // keep track of the fact that this fragment uses it. |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 581 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
| 582 | if (!Code.empty()) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 583 | assert(!P->getOnlyTree()->isLeaf() && "Can't be a leaf!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 584 | std::string ClassName = |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 585 | getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 586 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 587 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 588 | OS << "inline bool Predicate_" << Fragments[i]->getName() |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 589 | << "(SDNode *" << C2 << ") {\n"; |
| 590 | if (ClassName != "SDNode") |
| 591 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 592 | OS << Code << "\n}\n"; |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 593 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | 6de8b53 | 2005-09-13 21:59:15 +0000 | [diff] [blame] | 595 | |
| 596 | // If there is a node transformation corresponding to this, keep track of |
| 597 | // it. |
| 598 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 599 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
| 600 | P->getOnlyTree()->setTransformFn("Transform_"+Transform->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | OS << "\n\n"; |
| 604 | |
| 605 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 606 | // that there are not references to PatFrags left inside of them. |
| 607 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 608 | E = PatternFragments.end(); I != E; ++I) { |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 609 | TreePattern *ThePat = I->second; |
| 610 | ThePat->InlinePatternFragments(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 611 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 612 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 613 | // all of them, some may depend on the inputs of the pattern. |
| 614 | try { |
| 615 | ThePat->InferAllTypes(); |
| 616 | } catch (...) { |
| 617 | // If this pattern fragment is not supported by this target (no types can |
| 618 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 619 | // actually used by instructions, the type consistency error will be |
| 620 | // reported there. |
| 621 | } |
| 622 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 623 | // If debugging, print out the pattern fragment result. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 624 | DEBUG(ThePat->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
| 628 | /// ParseAndResolveInstructions - Parse all of the instructions, inlining and |
| 629 | /// resolving any fragments involved. This populates the Instructions list with |
| 630 | /// fully resolved instructions. |
| 631 | void DAGISelEmitter::ParseAndResolveInstructions() { |
| 632 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 633 | |
| 634 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
| 635 | if (!dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 636 | continue; // no pattern yet, ignore it. |
| 637 | |
| 638 | ListInit *LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 639 | if (LI->getSize() == 0) continue; // no pattern. |
| 640 | |
| 641 | std::vector<DagInit*> Trees; |
| 642 | for (unsigned j = 0, e = LI->getSize(); j != e; ++j) |
| 643 | Trees.push_back((DagInit*)LI->getElement(j)); |
| 644 | |
| 645 | // Parse the instruction. |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 646 | TreePattern *I = new TreePattern(Instrs[i], Trees, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 647 | // Inline pattern fragments into it. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 648 | I->InlinePatternFragments(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 95f6b76 | 2005-09-08 23:26:30 +0000 | [diff] [blame] | 650 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 651 | // never do anything with this instruction pattern: report it to the user. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 652 | if (!I->InferAllTypes()) { |
| 653 | I->dump(); |
| 654 | I->error("Could not infer all types in pattern!"); |
| 655 | } |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 656 | |
| 657 | // Verify that the top-level forms in the instruction are of void type. |
| 658 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) |
| 659 | if (I->getTree(j)->getType() != MVT::isVoid) { |
| 660 | I->dump(); |
| 661 | I->error("Top-level forms in instruction pattern should have" |
| 662 | " void types"); |
| 663 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 664 | |
| 665 | DEBUG(I->dump()); |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 666 | Instructions.push_back(I); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
| 670 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
| 671 | // Emit boilerplate. |
| 672 | OS << "// The main instruction selector code.\n" |
Chris Lattner | f4f33ba | 2005-09-13 22:05:02 +0000 | [diff] [blame^] | 673 | << "SDOperand SelectCode(SDOperand Op) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 674 | << " SDNode *N = Op.Val;\n" |
| 675 | << " if (N->getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
| 676 | << " N->getOpcode() < PPCISD::FIRST_NUMBER)\n" |
| 677 | << " return Op; // Already selected.\n\n" |
| 678 | << " switch (N->getOpcode()) {\n" |
| 679 | << " default: break;\n" |
| 680 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
| 681 | << " return Op;\n" |
| 682 | << " case ISD::AssertSext:\n" |
| 683 | << " case ISD::AssertZext:\n" |
| 684 | << " return Select(N->getOperand(0));\n"; |
| 685 | |
| 686 | |
| 687 | |
| 688 | OS << " } // end of big switch.\n\n" |
| 689 | << " std::cerr << \"Cannot yet select: \";\n" |
| 690 | << " N->dump();\n" |
| 691 | << " std::cerr << '\\n';\n" |
| 692 | << " abort();\n" |
| 693 | << "}\n"; |
| 694 | } |
| 695 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 696 | void DAGISelEmitter::run(std::ostream &OS) { |
| 697 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 698 | " target", OS); |
| 699 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 700 | ParseNodeInfo(); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 701 | ParseNodeTransforms(OS); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 702 | ParseAndResolvePatternFragments(OS); |
| 703 | ParseAndResolveInstructions(); |
| 704 | |
| 705 | // TODO: convert some instructions to expanders if needed or something. |
| 706 | |
| 707 | EmitInstructionSelector(OS); |
| 708 | |
| 709 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 710 | E = PatternFragments.end(); I != E; ++I) |
| 711 | delete I->second; |
| 712 | PatternFragments.clear(); |
| 713 | |
| 714 | for (unsigned i = 0, e = Instructions.size(); i != e; ++i) |
| 715 | delete Instructions[i]; |
| 716 | Instructions.clear(); |
| 717 | } |