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 | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 23 | // SDTypeConstraint implementation |
| 24 | // |
| 25 | |
| 26 | SDTypeConstraint::SDTypeConstraint(Record *R) { |
| 27 | OperandNo = R->getValueAsInt("OperandNum"); |
| 28 | |
| 29 | if (R->isSubClassOf("SDTCisVT")) { |
| 30 | ConstraintType = SDTCisVT; |
| 31 | x.SDTCisVT_Info.VT = getValueType(R->getValueAsDef("VT")); |
| 32 | } else if (R->isSubClassOf("SDTCisInt")) { |
| 33 | ConstraintType = SDTCisInt; |
| 34 | } else if (R->isSubClassOf("SDTCisFP")) { |
| 35 | ConstraintType = SDTCisFP; |
| 36 | } else if (R->isSubClassOf("SDTCisSameAs")) { |
| 37 | ConstraintType = SDTCisSameAs; |
| 38 | x.SDTCisSameAs_Info.OtherOperandNum = R->getValueAsInt("OtherOperandNum"); |
| 39 | } else if (R->isSubClassOf("SDTCisVTSmallerThanOp")) { |
| 40 | ConstraintType = SDTCisVTSmallerThanOp; |
| 41 | x.SDTCisVTSmallerThanOp_Info.OtherOperandNum = |
| 42 | R->getValueAsInt("OtherOperandNum"); |
| 43 | } else { |
| 44 | std::cerr << "Unrecognized SDTypeConstraint '" << R->getName() << "'!\n"; |
| 45 | exit(1); |
| 46 | } |
| 47 | } |
| 48 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 49 | /// getOperandNum - Return the node corresponding to operand #OpNo in tree |
| 50 | /// N, which has NumResults results. |
| 51 | TreePatternNode *SDTypeConstraint::getOperandNum(unsigned OpNo, |
| 52 | TreePatternNode *N, |
| 53 | unsigned NumResults) const { |
| 54 | assert(NumResults == 1 && "We only work with single result nodes so far!"); |
| 55 | |
| 56 | if (OpNo < NumResults) |
| 57 | return N; // FIXME: need value # |
| 58 | else |
| 59 | return N->getChild(OpNo-NumResults); |
| 60 | } |
| 61 | |
| 62 | /// ApplyTypeConstraint - Given a node in a pattern, apply this type |
| 63 | /// constraint to the nodes operands. This returns true if it makes a |
| 64 | /// change, false otherwise. If a type contradiction is found, throw an |
| 65 | /// exception. |
| 66 | bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N, |
| 67 | const SDNodeInfo &NodeInfo, |
| 68 | TreePattern &TP) const { |
| 69 | unsigned NumResults = NodeInfo.getNumResults(); |
| 70 | assert(NumResults == 1 && "We only work with single result nodes so far!"); |
| 71 | |
| 72 | // Check that the number of operands is sane. |
| 73 | if (NodeInfo.getNumOperands() >= 0) { |
| 74 | if (N->getNumChildren() != (unsigned)NodeInfo.getNumOperands()) |
| 75 | TP.error(N->getOperator()->getName() + " node requires exactly " + |
| 76 | itostr(NodeInfo.getNumOperands()) + " operands!"); |
| 77 | } |
| 78 | |
| 79 | TreePatternNode *NodeToApply = getOperandNum(OperandNo, N, NumResults); |
| 80 | |
| 81 | switch (ConstraintType) { |
| 82 | default: assert(0 && "Unknown constraint type!"); |
| 83 | case SDTCisVT: |
| 84 | // Operand must be a particular type. |
| 85 | return NodeToApply->UpdateNodeType(x.SDTCisVT_Info.VT, TP); |
| 86 | case SDTCisInt: |
| 87 | if (NodeToApply->hasTypeSet() && !MVT::isInteger(NodeToApply->getType())) |
| 88 | NodeToApply->UpdateNodeType(MVT::i1, TP); // throw an error. |
| 89 | |
| 90 | // FIXME: can tell from the target if there is only one Int type supported. |
| 91 | return false; |
| 92 | case SDTCisFP: |
| 93 | if (NodeToApply->hasTypeSet() && |
| 94 | !MVT::isFloatingPoint(NodeToApply->getType())) |
| 95 | NodeToApply->UpdateNodeType(MVT::f32, TP); // throw an error. |
| 96 | // FIXME: can tell from the target if there is only one FP type supported. |
| 97 | return false; |
| 98 | case SDTCisSameAs: { |
| 99 | TreePatternNode *OtherNode = |
| 100 | getOperandNum(x.SDTCisSameAs_Info.OtherOperandNum, N, NumResults); |
| 101 | return NodeToApply->UpdateNodeType(OtherNode->getType(), TP) | |
| 102 | OtherNode->UpdateNodeType(NodeToApply->getType(), TP); |
| 103 | } |
| 104 | case SDTCisVTSmallerThanOp: { |
| 105 | // The NodeToApply must be a leaf node that is a VT. OtherOperandNum must |
| 106 | // have an integer type that is smaller than the VT. |
| 107 | if (!NodeToApply->isLeaf() || |
| 108 | !dynamic_cast<DefInit*>(NodeToApply->getLeafValue()) || |
| 109 | !static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef() |
| 110 | ->isSubClassOf("ValueType")) |
| 111 | TP.error(N->getOperator()->getName() + " expects a VT operand!"); |
| 112 | MVT::ValueType VT = |
| 113 | getValueType(static_cast<DefInit*>(NodeToApply->getLeafValue())->getDef()); |
| 114 | if (!MVT::isInteger(VT)) |
| 115 | TP.error(N->getOperator()->getName() + " VT operand must be integer!"); |
| 116 | |
| 117 | TreePatternNode *OtherNode = |
| 118 | getOperandNum(x.SDTCisVTSmallerThanOp_Info.OtherOperandNum, N,NumResults); |
| 119 | if (OtherNode->hasTypeSet() && |
| 120 | (!MVT::isInteger(OtherNode->getType()) || |
| 121 | OtherNode->getType() <= VT)) |
| 122 | OtherNode->UpdateNodeType(MVT::Other, TP); // Throw an error. |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 131 | // SDNodeInfo implementation |
| 132 | // |
| 133 | SDNodeInfo::SDNodeInfo(Record *R) : Def(R) { |
| 134 | EnumName = R->getValueAsString("Opcode"); |
| 135 | SDClassName = R->getValueAsString("SDClass"); |
Chris Lattner | 33c92e9 | 2005-09-08 21:27:15 +0000 | [diff] [blame] | 136 | Record *TypeProfile = R->getValueAsDef("TypeProfile"); |
| 137 | NumResults = TypeProfile->getValueAsInt("NumResults"); |
| 138 | NumOperands = TypeProfile->getValueAsInt("NumOperands"); |
| 139 | |
| 140 | // Parse the type constraints. |
| 141 | ListInit *Constraints = TypeProfile->getValueAsListInit("Constraints"); |
| 142 | for (unsigned i = 0, e = Constraints->getSize(); i != e; ++i) { |
| 143 | assert(dynamic_cast<DefInit*>(Constraints->getElement(i)) && |
| 144 | "Constraints list should contain constraint definitions!"); |
| 145 | Record *Constraint = |
| 146 | static_cast<DefInit*>(Constraints->getElement(i))->getDef(); |
| 147 | TypeConstraints.push_back(Constraint); |
| 148 | } |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 149 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 150 | |
| 151 | //===----------------------------------------------------------------------===// |
| 152 | // TreePatternNode implementation |
| 153 | // |
| 154 | |
| 155 | TreePatternNode::~TreePatternNode() { |
| 156 | #if 0 // FIXME: implement refcounted tree nodes! |
| 157 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 158 | delete getChild(i); |
| 159 | #endif |
| 160 | } |
| 161 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 162 | /// UpdateNodeType - Set the node type of N to VT if VT contains |
| 163 | /// information. If N already contains a conflicting type, then throw an |
| 164 | /// exception. This returns true if any information was updated. |
| 165 | /// |
| 166 | bool TreePatternNode::UpdateNodeType(MVT::ValueType VT, TreePattern &TP) { |
| 167 | if (VT == MVT::LAST_VALUETYPE || getType() == VT) return false; |
| 168 | if (getType() == MVT::LAST_VALUETYPE) { |
| 169 | setType(VT); |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | TP.error("Type inference contradiction found in node " + |
| 174 | getOperator()->getName() + "!"); |
| 175 | return true; // unreachable |
| 176 | } |
| 177 | |
| 178 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 179 | void TreePatternNode::print(std::ostream &OS) const { |
| 180 | if (isLeaf()) { |
| 181 | OS << *getLeafValue(); |
| 182 | } else { |
| 183 | OS << "(" << getOperator()->getName(); |
| 184 | } |
| 185 | |
| 186 | if (getType() == MVT::Other) |
| 187 | OS << ":Other"; |
| 188 | else if (getType() == MVT::LAST_VALUETYPE) |
| 189 | ;//OS << ":?"; |
| 190 | else |
| 191 | OS << ":" << getType(); |
| 192 | |
| 193 | if (!isLeaf()) { |
| 194 | if (getNumChildren() != 0) { |
| 195 | OS << " "; |
| 196 | getChild(0)->print(OS); |
| 197 | for (unsigned i = 1, e = getNumChildren(); i != e; ++i) { |
| 198 | OS << ", "; |
| 199 | getChild(i)->print(OS); |
| 200 | } |
| 201 | } |
| 202 | OS << ")"; |
| 203 | } |
| 204 | |
| 205 | if (!PredicateFn.empty()) |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 206 | OS << "<<P:" << PredicateFn << ">>"; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 207 | if (TransformFn) |
| 208 | OS << "<<X:" << TransformFn->getName() << ">>"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 209 | if (!getName().empty()) |
| 210 | OS << ":$" << getName(); |
| 211 | |
| 212 | } |
| 213 | void TreePatternNode::dump() const { |
| 214 | print(std::cerr); |
| 215 | } |
| 216 | |
| 217 | /// clone - Make a copy of this tree and all of its children. |
| 218 | /// |
| 219 | TreePatternNode *TreePatternNode::clone() const { |
| 220 | TreePatternNode *New; |
| 221 | if (isLeaf()) { |
| 222 | New = new TreePatternNode(getLeafValue()); |
| 223 | } else { |
| 224 | std::vector<TreePatternNode*> CChildren; |
| 225 | CChildren.reserve(Children.size()); |
| 226 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 227 | CChildren.push_back(getChild(i)->clone()); |
| 228 | New = new TreePatternNode(getOperator(), CChildren); |
| 229 | } |
| 230 | New->setName(getName()); |
| 231 | New->setType(getType()); |
| 232 | New->setPredicateFn(getPredicateFn()); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 233 | New->setTransformFn(getTransformFn()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 234 | return New; |
| 235 | } |
| 236 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 237 | /// SubstituteFormalArguments - Replace the formal arguments in this tree |
| 238 | /// with actual values specified by ArgMap. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 239 | void TreePatternNode:: |
| 240 | SubstituteFormalArguments(std::map<std::string, TreePatternNode*> &ArgMap) { |
| 241 | if (isLeaf()) return; |
| 242 | |
| 243 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 244 | TreePatternNode *Child = getChild(i); |
| 245 | if (Child->isLeaf()) { |
| 246 | Init *Val = Child->getLeafValue(); |
| 247 | if (dynamic_cast<DefInit*>(Val) && |
| 248 | static_cast<DefInit*>(Val)->getDef()->getName() == "node") { |
| 249 | // We found a use of a formal argument, replace it with its value. |
| 250 | Child = ArgMap[Child->getName()]; |
| 251 | assert(Child && "Couldn't find formal argument!"); |
| 252 | setChild(i, Child); |
| 253 | } |
| 254 | } else { |
| 255 | getChild(i)->SubstituteFormalArguments(ArgMap); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | /// InlinePatternFragments - If this pattern refers to any pattern |
| 262 | /// fragments, inline them into place, giving us a pattern without any |
| 263 | /// PatFrag references. |
| 264 | TreePatternNode *TreePatternNode::InlinePatternFragments(TreePattern &TP) { |
| 265 | if (isLeaf()) return this; // nothing to do. |
| 266 | Record *Op = getOperator(); |
| 267 | |
| 268 | if (!Op->isSubClassOf("PatFrag")) { |
| 269 | // Just recursively inline children nodes. |
| 270 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) |
| 271 | setChild(i, getChild(i)->InlinePatternFragments(TP)); |
| 272 | return this; |
| 273 | } |
| 274 | |
| 275 | // Otherwise, we found a reference to a fragment. First, look up its |
| 276 | // TreePattern record. |
| 277 | TreePattern *Frag = TP.getDAGISelEmitter().getPatternFragment(Op); |
| 278 | |
| 279 | // Verify that we are passing the right number of operands. |
| 280 | if (Frag->getNumArgs() != Children.size()) |
| 281 | TP.error("'" + Op->getName() + "' fragment requires " + |
| 282 | utostr(Frag->getNumArgs()) + " operands!"); |
| 283 | |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 284 | TreePatternNode *FragTree = Frag->getOnlyTree()->clone(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 285 | |
| 286 | // Resolve formal arguments to their actual value. |
| 287 | if (Frag->getNumArgs()) { |
| 288 | // Compute the map of formal to actual arguments. |
| 289 | std::map<std::string, TreePatternNode*> ArgMap; |
| 290 | for (unsigned i = 0, e = Frag->getNumArgs(); i != e; ++i) |
| 291 | ArgMap[Frag->getArgName(i)] = getChild(i)->InlinePatternFragments(TP); |
| 292 | |
| 293 | FragTree->SubstituteFormalArguments(ArgMap); |
| 294 | } |
| 295 | |
Chris Lattner | fbf8e57 | 2005-09-08 17:45:12 +0000 | [diff] [blame] | 296 | FragTree->setName(getName()); |
| 297 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 298 | // Get a new copy of this fragment to stitch into here. |
| 299 | //delete this; // FIXME: implement refcounting! |
| 300 | return FragTree; |
| 301 | } |
| 302 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 303 | /// ApplyTypeConstraints - Apply all of the type constraints relevent to |
| 304 | /// this node and its children in the tree. This returns true if it makes a |
| 305 | /// change, false otherwise. If a type contradiction is found, throw an |
| 306 | /// exception. |
| 307 | bool TreePatternNode::ApplyTypeConstraints(TreePattern &TP) { |
| 308 | if (isLeaf()) return false; |
| 309 | |
| 310 | // special handling for set, which isn't really an SDNode. |
| 311 | if (getOperator()->getName() == "set") { |
| 312 | assert (getNumChildren() == 2 && "Only handle 2 operand set's for now!"); |
| 313 | bool MadeChange = getChild(0)->ApplyTypeConstraints(TP); |
| 314 | MadeChange |= getChild(1)->ApplyTypeConstraints(TP); |
| 315 | |
| 316 | // Types of operands must match. |
| 317 | MadeChange |= getChild(0)->UpdateNodeType(getChild(1)->getType(), TP); |
| 318 | MadeChange |= getChild(1)->UpdateNodeType(getChild(0)->getType(), TP); |
| 319 | MadeChange |= UpdateNodeType(MVT::isVoid, TP); |
| 320 | return MadeChange; |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 321 | } else if (getOperator()->isSubClassOf("SDNode")) { |
| 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; |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 328 | } else if (getOperator()->isSubClassOf("Instruction")) { |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 329 | const DAGInstruction &Inst = |
| 330 | TP.getDAGISelEmitter().getInstruction(getOperator()); |
| 331 | |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 332 | assert(Inst.getNumResults() == 1 && "Only supports one result instrs!"); |
| 333 | // Apply the result type to the node |
| 334 | bool MadeChange = UpdateNodeType(Inst.getResultType(0), TP); |
| 335 | |
| 336 | if (getNumChildren() != Inst.getNumOperands()) |
| 337 | TP.error("Instruction '" + getOperator()->getName() + " expects " + |
| 338 | utostr(Inst.getNumOperands()) + " operands, not " + |
| 339 | utostr(getNumChildren()) + " operands!"); |
| 340 | for (unsigned i = 0, e = getNumChildren(); i != e; ++i) { |
| 341 | MadeChange |= getChild(i)->UpdateNodeType(Inst.getOperandType(i), TP); |
| 342 | MadeChange |= getChild(i)->ApplyTypeConstraints(TP); |
| 343 | } |
| 344 | return MadeChange; |
| 345 | } else { |
| 346 | assert(getOperator()->isSubClassOf("SDNodeXForm") && "Unknown node type!"); |
| 347 | |
| 348 | // Node transforms always take one operand, and take and return the same |
| 349 | // type. |
| 350 | if (getNumChildren() != 1) |
| 351 | TP.error("Node transform '" + getOperator()->getName() + |
| 352 | "' requires one operand!"); |
| 353 | bool MadeChange = UpdateNodeType(getChild(0)->getType(), TP); |
| 354 | MadeChange |= getChild(0)->UpdateNodeType(getType(), TP); |
| 355 | return MadeChange; |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 356 | } |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 360 | //===----------------------------------------------------------------------===// |
| 361 | // TreePattern implementation |
| 362 | // |
| 363 | |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 364 | TreePattern::TreePattern(Record *TheRec, ListInit *RawPat, |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 365 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 366 | for (unsigned i = 0, e = RawPat->getSize(); i != e; ++i) |
| 367 | Trees.push_back(ParseTreePattern((DagInit*)RawPat->getElement(i))); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 370 | TreePattern::TreePattern(Record *TheRec, DagInit *Pat, |
| 371 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
| 372 | Trees.push_back(ParseTreePattern(Pat)); |
| 373 | } |
| 374 | |
| 375 | TreePattern::TreePattern(Record *TheRec, TreePatternNode *Pat, |
| 376 | DAGISelEmitter &ise) : TheRecord(TheRec), ISE(ise) { |
| 377 | Trees.push_back(Pat); |
| 378 | } |
| 379 | |
| 380 | |
| 381 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 382 | void TreePattern::error(const std::string &Msg) const { |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 383 | dump(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 384 | throw "In " + TheRecord->getName() + ": " + Msg; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /// getIntrinsicType - Check to see if the specified record has an intrinsic |
| 388 | /// type which should be applied to it. This infer the type of register |
| 389 | /// references from the register file information, for example. |
| 390 | /// |
| 391 | MVT::ValueType TreePattern::getIntrinsicType(Record *R) const { |
| 392 | // Check to see if this is a register or a register class... |
| 393 | if (R->isSubClassOf("RegisterClass")) |
| 394 | return getValueType(R->getValueAsDef("RegType")); |
| 395 | else if (R->isSubClassOf("PatFrag")) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 396 | // Pattern fragment types will be resolved when they are inlined. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 397 | return MVT::LAST_VALUETYPE; |
| 398 | } else if (R->isSubClassOf("Register")) { |
| 399 | assert(0 && "Explicit registers not handled here yet!\n"); |
| 400 | return MVT::LAST_VALUETYPE; |
| 401 | } else if (R->isSubClassOf("ValueType")) { |
| 402 | // Using a VTSDNode. |
| 403 | return MVT::Other; |
| 404 | } else if (R->getName() == "node") { |
| 405 | // Placeholder. |
| 406 | return MVT::LAST_VALUETYPE; |
| 407 | } |
| 408 | |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 409 | error("Unknown node flavor used in pattern: " + R->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 410 | return MVT::Other; |
| 411 | } |
| 412 | |
| 413 | TreePatternNode *TreePattern::ParseTreePattern(DagInit *Dag) { |
| 414 | Record *Operator = Dag->getNodeType(); |
| 415 | |
| 416 | if (Operator->isSubClassOf("ValueType")) { |
| 417 | // If the operator is a ValueType, then this must be "type cast" of a leaf |
| 418 | // node. |
| 419 | if (Dag->getNumArgs() != 1) |
| 420 | error("Type cast only valid for a leaf node!"); |
| 421 | |
| 422 | Init *Arg = Dag->getArg(0); |
| 423 | TreePatternNode *New; |
| 424 | if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) { |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 425 | Record *R = DI->getDef(); |
| 426 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 427 | Dag->setArg(0, new DagInit(R, |
| 428 | std::vector<std::pair<Init*, std::string> >())); |
| 429 | TreePatternNode *TPN = ParseTreePattern(Dag); |
| 430 | TPN->setName(Dag->getArgName(0)); |
| 431 | return TPN; |
| 432 | } |
| 433 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 434 | New = new TreePatternNode(DI); |
| 435 | // If it's a regclass or something else known, set the type. |
| 436 | New->setType(getIntrinsicType(DI->getDef())); |
| 437 | } else if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 438 | New = ParseTreePattern(DI); |
| 439 | } else { |
| 440 | Arg->dump(); |
| 441 | error("Unknown leaf value for tree pattern!"); |
| 442 | return 0; |
| 443 | } |
| 444 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 445 | // Apply the type cast. |
| 446 | New->UpdateNodeType(getValueType(Operator), *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 447 | return New; |
| 448 | } |
| 449 | |
| 450 | // Verify that this is something that makes sense for an operator. |
| 451 | if (!Operator->isSubClassOf("PatFrag") && !Operator->isSubClassOf("SDNode") && |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 452 | !Operator->isSubClassOf("Instruction") && |
| 453 | !Operator->isSubClassOf("SDNodeXForm") && |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 454 | Operator->getName() != "set") |
| 455 | error("Unrecognized node '" + Operator->getName() + "'!"); |
| 456 | |
| 457 | std::vector<TreePatternNode*> Children; |
| 458 | |
| 459 | for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) { |
| 460 | Init *Arg = Dag->getArg(i); |
| 461 | if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) { |
| 462 | Children.push_back(ParseTreePattern(DI)); |
| 463 | Children.back()->setName(Dag->getArgName(i)); |
| 464 | } else if (DefInit *DefI = dynamic_cast<DefInit*>(Arg)) { |
| 465 | Record *R = DefI->getDef(); |
| 466 | // Direct reference to a leaf DagNode or PatFrag? Turn it into a |
| 467 | // TreePatternNode if its own. |
| 468 | if (R->isSubClassOf("SDNode") || R->isSubClassOf("PatFrag")) { |
| 469 | Dag->setArg(i, new DagInit(R, |
| 470 | std::vector<std::pair<Init*, std::string> >())); |
| 471 | --i; // Revisit this node... |
| 472 | } else { |
| 473 | TreePatternNode *Node = new TreePatternNode(DefI); |
| 474 | Node->setName(Dag->getArgName(i)); |
| 475 | Children.push_back(Node); |
| 476 | |
| 477 | // If it's a regclass or something else known, set the type. |
| 478 | Node->setType(getIntrinsicType(R)); |
| 479 | |
| 480 | // Input argument? |
| 481 | if (R->getName() == "node") { |
| 482 | if (Dag->getArgName(i).empty()) |
| 483 | error("'node' argument requires a name to match with operand list"); |
| 484 | Args.push_back(Dag->getArgName(i)); |
| 485 | } |
| 486 | } |
| 487 | } else { |
| 488 | Arg->dump(); |
| 489 | error("Unknown leaf value for tree pattern!"); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return new TreePatternNode(Operator, Children); |
| 494 | } |
| 495 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 496 | /// InferAllTypes - Infer/propagate as many types throughout the expression |
| 497 | /// patterns as possible. Return true if all types are infered, false |
| 498 | /// otherwise. Throw an exception if a type contradiction is found. |
| 499 | bool TreePattern::InferAllTypes() { |
| 500 | bool MadeChange = true; |
| 501 | while (MadeChange) { |
| 502 | MadeChange = false; |
| 503 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 504 | MadeChange |= Trees[i]->ApplyTypeConstraints(*this); |
| 505 | } |
| 506 | |
| 507 | bool HasUnresolvedTypes = false; |
| 508 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) |
| 509 | HasUnresolvedTypes |= Trees[i]->ContainsUnresolvedType(); |
| 510 | return !HasUnresolvedTypes; |
| 511 | } |
| 512 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 513 | void TreePattern::print(std::ostream &OS) const { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 514 | OS << getRecord()->getName(); |
| 515 | if (!Args.empty()) { |
| 516 | OS << "(" << Args[0]; |
| 517 | for (unsigned i = 1, e = Args.size(); i != e; ++i) |
| 518 | OS << ", " << Args[i]; |
| 519 | OS << ")"; |
| 520 | } |
| 521 | OS << ": "; |
| 522 | |
| 523 | if (Trees.size() > 1) |
| 524 | OS << "[\n"; |
| 525 | for (unsigned i = 0, e = Trees.size(); i != e; ++i) { |
| 526 | OS << "\t"; |
| 527 | Trees[i]->print(OS); |
| 528 | OS << "\n"; |
| 529 | } |
| 530 | |
| 531 | if (Trees.size() > 1) |
| 532 | OS << "]\n"; |
| 533 | } |
| 534 | |
| 535 | void TreePattern::dump() const { print(std::cerr); } |
| 536 | |
| 537 | |
| 538 | |
| 539 | //===----------------------------------------------------------------------===// |
| 540 | // DAGISelEmitter implementation |
| 541 | // |
| 542 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 543 | // Parse all of the SDNode definitions for the target, populating SDNodes. |
| 544 | void DAGISelEmitter::ParseNodeInfo() { |
| 545 | std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("SDNode"); |
| 546 | while (!Nodes.empty()) { |
| 547 | SDNodes.insert(std::make_pair(Nodes.back(), Nodes.back())); |
| 548 | Nodes.pop_back(); |
| 549 | } |
| 550 | } |
| 551 | |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 552 | /// ParseNodeTransforms - Parse all SDNodeXForm instances into the SDNodeXForms |
| 553 | /// map, and emit them to the file as functions. |
| 554 | void DAGISelEmitter::ParseNodeTransforms(std::ostream &OS) { |
| 555 | OS << "\n// Node transformations.\n"; |
| 556 | std::vector<Record*> Xforms = Records.getAllDerivedDefinitions("SDNodeXForm"); |
| 557 | while (!Xforms.empty()) { |
| 558 | Record *XFormNode = Xforms.back(); |
| 559 | Record *SDNode = XFormNode->getValueAsDef("Opcode"); |
| 560 | std::string Code = XFormNode->getValueAsCode("XFormFunction"); |
| 561 | SDNodeXForms.insert(std::make_pair(XFormNode, |
| 562 | std::make_pair(SDNode, Code))); |
| 563 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 564 | if (!Code.empty()) { |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 565 | std::string ClassName = getSDNodeInfo(SDNode).getSDClassName(); |
| 566 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 567 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 568 | OS << "inline SDOperand Transform_" << XFormNode->getName() |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 569 | << "(SDNode *" << C2 << ") {\n"; |
| 570 | if (ClassName != "SDNode") |
| 571 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
| 572 | OS << Code << "\n}\n"; |
| 573 | } |
| 574 | |
| 575 | Xforms.pop_back(); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | |
| 580 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 581 | /// ParsePatternFragments - Parse all of the PatFrag definitions in the .td |
| 582 | /// file, building up the PatternFragments map. After we've collected them all, |
| 583 | /// inline fragments together as necessary, so that there are no references left |
| 584 | /// inside a pattern fragment to a pattern fragment. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 585 | /// |
| 586 | /// This also emits all of the predicate functions to the output file. |
| 587 | /// |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 588 | void DAGISelEmitter::ParsePatternFragments(std::ostream &OS) { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 589 | std::vector<Record*> Fragments = Records.getAllDerivedDefinitions("PatFrag"); |
| 590 | |
| 591 | // First step, parse all of the fragments and emit predicate functions. |
| 592 | OS << "\n// Predicate functions.\n"; |
| 593 | for (unsigned i = 0, e = Fragments.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 594 | DagInit *Tree = Fragments[i]->getValueAsDag("Fragment"); |
| 595 | TreePattern *P = new TreePattern(Fragments[i], Tree, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 596 | PatternFragments[Fragments[i]] = P; |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 597 | |
| 598 | // Validate the argument list, converting it to map, to discard duplicates. |
| 599 | std::vector<std::string> &Args = P->getArgList(); |
| 600 | std::set<std::string> OperandsMap(Args.begin(), Args.end()); |
| 601 | |
| 602 | if (OperandsMap.count("")) |
| 603 | P->error("Cannot have unnamed 'node' values in pattern fragment!"); |
| 604 | |
| 605 | // Parse the operands list. |
| 606 | DagInit *OpsList = Fragments[i]->getValueAsDag("Operands"); |
| 607 | if (OpsList->getNodeType()->getName() != "ops") |
| 608 | P->error("Operands list should start with '(ops ... '!"); |
| 609 | |
| 610 | // Copy over the arguments. |
| 611 | Args.clear(); |
| 612 | for (unsigned j = 0, e = OpsList->getNumArgs(); j != e; ++j) { |
| 613 | if (!dynamic_cast<DefInit*>(OpsList->getArg(j)) || |
| 614 | static_cast<DefInit*>(OpsList->getArg(j))-> |
| 615 | getDef()->getName() != "node") |
| 616 | P->error("Operands list should all be 'node' values."); |
| 617 | if (OpsList->getArgName(j).empty()) |
| 618 | P->error("Operands list should have names for each operand!"); |
| 619 | if (!OperandsMap.count(OpsList->getArgName(j))) |
| 620 | P->error("'" + OpsList->getArgName(j) + |
| 621 | "' does not occur in pattern or was multiply specified!"); |
| 622 | OperandsMap.erase(OpsList->getArgName(j)); |
| 623 | Args.push_back(OpsList->getArgName(j)); |
| 624 | } |
| 625 | |
| 626 | if (!OperandsMap.empty()) |
| 627 | P->error("Operands list does not contain an entry for operand '" + |
| 628 | *OperandsMap.begin() + "'!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 629 | |
| 630 | // If there is a code init for this fragment, emit the predicate code and |
| 631 | // keep track of the fact that this fragment uses it. |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 632 | std::string Code = Fragments[i]->getValueAsCode("Predicate"); |
| 633 | if (!Code.empty()) { |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 634 | assert(!P->getOnlyTree()->isLeaf() && "Can't be a leaf!"); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 635 | std::string ClassName = |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 636 | getSDNodeInfo(P->getOnlyTree()->getOperator()).getSDClassName(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 637 | const char *C2 = ClassName == "SDNode" ? "N" : "inN"; |
| 638 | |
Chris Lattner | 1048b7a | 2005-09-13 22:03:37 +0000 | [diff] [blame] | 639 | OS << "inline bool Predicate_" << Fragments[i]->getName() |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 640 | << "(SDNode *" << C2 << ") {\n"; |
| 641 | if (ClassName != "SDNode") |
| 642 | OS << " " << ClassName << " *N = cast<" << ClassName << ">(inN);\n"; |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 643 | OS << Code << "\n}\n"; |
Chris Lattner | 3793709 | 2005-09-09 01:15:01 +0000 | [diff] [blame] | 644 | P->getOnlyTree()->setPredicateFn("Predicate_"+Fragments[i]->getName()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | 6de8b53 | 2005-09-13 21:59:15 +0000 | [diff] [blame] | 646 | |
| 647 | // If there is a node transformation corresponding to this, keep track of |
| 648 | // it. |
| 649 | Record *Transform = Fragments[i]->getValueAsDef("OperandTransform"); |
| 650 | if (!getSDNodeTransform(Transform).second.empty()) // not noop xform? |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 651 | P->getOnlyTree()->setTransformFn(Transform); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | OS << "\n\n"; |
| 655 | |
| 656 | // Now that we've parsed all of the tree fragments, do a closure on them so |
| 657 | // that there are not references to PatFrags left inside of them. |
| 658 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 659 | E = PatternFragments.end(); I != E; ++I) { |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 660 | TreePattern *ThePat = I->second; |
| 661 | ThePat->InlinePatternFragments(); |
Chris Lattner | ee9f0c3 | 2005-09-13 21:20:49 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 663 | // Infer as many types as possible. Don't worry about it if we don't infer |
| 664 | // all of them, some may depend on the inputs of the pattern. |
| 665 | try { |
| 666 | ThePat->InferAllTypes(); |
| 667 | } catch (...) { |
| 668 | // If this pattern fragment is not supported by this target (no types can |
| 669 | // satisfy its constraints), just ignore it. If the bogus pattern is |
| 670 | // actually used by instructions, the type consistency error will be |
| 671 | // reported there. |
| 672 | } |
| 673 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 674 | // If debugging, print out the pattern fragment result. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 675 | DEBUG(ThePat->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 679 | /// 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] | 680 | /// instruction input. Return true if this is a real use. |
| 681 | static bool HandleUse(TreePattern *I, TreePatternNode *Pat, |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 682 | std::map<std::string, TreePatternNode*> &InstInputs) { |
| 683 | // No name -> not interesting. |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 684 | if (Pat->getName().empty()) { |
| 685 | if (Pat->isLeaf()) { |
| 686 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 687 | if (DI && DI->getDef()->isSubClassOf("RegisterClass")) |
| 688 | I->error("Input " + DI->getDef()->getName() + " must be named!"); |
| 689 | |
| 690 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 691 | return false; |
Chris Lattner | 7da852f | 2005-09-14 22:06:36 +0000 | [diff] [blame] | 692 | } |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 693 | |
| 694 | Record *Rec; |
| 695 | if (Pat->isLeaf()) { |
| 696 | DefInit *DI = dynamic_cast<DefInit*>(Pat->getLeafValue()); |
| 697 | if (!DI) I->error("Input $" + Pat->getName() + " must be an identifier!"); |
| 698 | Rec = DI->getDef(); |
| 699 | } else { |
| 700 | assert(Pat->getNumChildren() == 0 && "can't be a use with children!"); |
| 701 | Rec = Pat->getOperator(); |
| 702 | } |
| 703 | |
| 704 | TreePatternNode *&Slot = InstInputs[Pat->getName()]; |
| 705 | if (!Slot) { |
| 706 | Slot = Pat; |
| 707 | } else { |
| 708 | Record *SlotRec; |
| 709 | if (Slot->isLeaf()) { |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 710 | SlotRec = dynamic_cast<DefInit*>(Slot->getLeafValue())->getDef(); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 711 | } else { |
| 712 | assert(Slot->getNumChildren() == 0 && "can't be a use with children!"); |
| 713 | SlotRec = Slot->getOperator(); |
| 714 | } |
| 715 | |
| 716 | // Ensure that the inputs agree if we've already seen this input. |
| 717 | if (Rec != SlotRec) |
| 718 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 719 | if (Slot->getType() != Pat->getType()) |
| 720 | I->error("All $" + Pat->getName() + " inputs must agree with each other"); |
| 721 | } |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 722 | return true; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /// FindPatternInputsAndOutputs - Scan the specified TreePatternNode (which is |
| 726 | /// part of "I", the instruction), computing the set of inputs and outputs of |
| 727 | /// the pattern. Report errors if we see anything naughty. |
| 728 | void DAGISelEmitter:: |
| 729 | FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, |
| 730 | std::map<std::string, TreePatternNode*> &InstInputs, |
| 731 | std::map<std::string, Record*> &InstResults) { |
| 732 | if (Pat->isLeaf()) { |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 733 | bool isUse = HandleUse(I, Pat, InstInputs); |
| 734 | if (!isUse && Pat->getTransformFn()) |
| 735 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 736 | return; |
| 737 | } else if (Pat->getOperator()->getName() != "set") { |
| 738 | // If this is not a set, verify that the children nodes are not void typed, |
| 739 | // and recurse. |
| 740 | for (unsigned i = 0, e = Pat->getNumChildren(); i != e; ++i) { |
| 741 | if (Pat->getChild(i)->getType() == MVT::isVoid) |
| 742 | I->error("Cannot have void nodes inside of patterns!"); |
| 743 | FindPatternInputsAndOutputs(I, Pat->getChild(i), InstInputs, InstResults); |
| 744 | } |
| 745 | |
| 746 | // If this is a non-leaf node with no children, treat it basically as if |
| 747 | // it were a leaf. This handles nodes like (imm). |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 748 | bool isUse = false; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 749 | if (Pat->getNumChildren() == 0) |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 750 | isUse = HandleUse(I, Pat, InstInputs); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 751 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 752 | if (!isUse && Pat->getTransformFn()) |
| 753 | I->error("Cannot specify a transform function for a non-input value!"); |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 754 | return; |
| 755 | } |
| 756 | |
| 757 | // Otherwise, this is a set, validate and collect instruction results. |
| 758 | if (Pat->getNumChildren() == 0) |
| 759 | I->error("set requires operands!"); |
| 760 | else if (Pat->getNumChildren() & 1) |
| 761 | I->error("set requires an even number of operands"); |
| 762 | |
Chris Lattner | f131184 | 2005-09-14 23:05:13 +0000 | [diff] [blame] | 763 | if (Pat->getTransformFn()) |
| 764 | I->error("Cannot specify a transform function on a set node!"); |
| 765 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 766 | // Check the set destinations. |
| 767 | unsigned NumValues = Pat->getNumChildren()/2; |
| 768 | for (unsigned i = 0; i != NumValues; ++i) { |
| 769 | TreePatternNode *Dest = Pat->getChild(i); |
| 770 | if (!Dest->isLeaf()) |
| 771 | I->error("set destination should be a virtual register!"); |
| 772 | |
| 773 | DefInit *Val = dynamic_cast<DefInit*>(Dest->getLeafValue()); |
| 774 | if (!Val) |
| 775 | I->error("set destination should be a virtual register!"); |
| 776 | |
| 777 | if (!Val->getDef()->isSubClassOf("RegisterClass")) |
| 778 | I->error("set destination should be a virtual register!"); |
| 779 | if (Dest->getName().empty()) |
| 780 | I->error("set destination must have a name!"); |
| 781 | if (InstResults.count(Dest->getName())) |
| 782 | I->error("cannot set '" + Dest->getName() +"' multiple times"); |
| 783 | InstResults[Dest->getName()] = Val->getDef(); |
| 784 | |
| 785 | // Verify and collect info from the computation. |
| 786 | FindPatternInputsAndOutputs(I, Pat->getChild(i+NumValues), |
| 787 | InstInputs, InstResults); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 792 | /// ParseInstructions - Parse all of the instructions, inlining and resolving |
| 793 | /// any fragments involved. This populates the Instructions list with fully |
| 794 | /// resolved instructions. |
| 795 | void DAGISelEmitter::ParseInstructions() { |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 796 | std::vector<Record*> Instrs = Records.getAllDerivedDefinitions("Instruction"); |
| 797 | |
| 798 | for (unsigned i = 0, e = Instrs.size(); i != e; ++i) { |
| 799 | if (!dynamic_cast<ListInit*>(Instrs[i]->getValueInit("Pattern"))) |
| 800 | continue; // no pattern yet, ignore it. |
| 801 | |
| 802 | ListInit *LI = Instrs[i]->getValueAsListInit("Pattern"); |
| 803 | if (LI->getSize() == 0) continue; // no pattern. |
| 804 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 805 | // Parse the instruction. |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 806 | TreePattern *I = new TreePattern(Instrs[i], LI, *this); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 807 | // Inline pattern fragments into it. |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 808 | I->InlinePatternFragments(); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 809 | |
Chris Lattner | 95f6b76 | 2005-09-08 23:26:30 +0000 | [diff] [blame] | 810 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 811 | // never do anything with this instruction pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 812 | if (!I->InferAllTypes()) |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 813 | I->error("Could not infer all types in pattern!"); |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 814 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 815 | // InstInputs - Keep track of all of the inputs of the instruction, along |
| 816 | // with the record they are declared as. |
| 817 | std::map<std::string, TreePatternNode*> InstInputs; |
| 818 | |
| 819 | // InstResults - Keep track of all the virtual registers that are 'set' |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 820 | // in the instruction, including what reg class they are. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 821 | std::map<std::string, Record*> InstResults; |
| 822 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 823 | // 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] | 824 | // fill in the InstResults map. |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 825 | for (unsigned j = 0, e = I->getNumTrees(); j != e; ++j) { |
| 826 | TreePatternNode *Pat = I->getTree(j); |
| 827 | if (Pat->getType() != MVT::isVoid) { |
Chris Lattner | f2a17a7 | 2005-09-09 01:11:44 +0000 | [diff] [blame] | 828 | I->dump(); |
| 829 | I->error("Top-level forms in instruction pattern should have" |
| 830 | " void types"); |
| 831 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 832 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 833 | // Find inputs and outputs, and verify the structure of the uses/defs. |
| 834 | FindPatternInputsAndOutputs(I, Pat, InstInputs, InstResults); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 835 | } |
Chris Lattner | 5f8cb2a | 2005-09-14 02:11:12 +0000 | [diff] [blame] | 836 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 837 | // Now that we have inputs and outputs of the pattern, inspect the operands |
| 838 | // list for the instruction. This determines the order that operands are |
| 839 | // added to the machine instruction the node corresponds to. |
| 840 | unsigned NumResults = InstResults.size(); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 841 | |
| 842 | // Parse the operands list from the (ops) list, validating it. |
| 843 | std::vector<std::string> &Args = I->getArgList(); |
| 844 | assert(Args.empty() && "Args list should still be empty here!"); |
| 845 | CodeGenInstruction &CGI = Target.getInstruction(Instrs[i]->getName()); |
| 846 | |
| 847 | // Check that all of the results occur first in the list. |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 848 | std::vector<MVT::ValueType> ResultTypes; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 849 | for (unsigned i = 0; i != NumResults; ++i) { |
Chris Lattner | 3a7319d | 2005-09-14 21:04:12 +0000 | [diff] [blame] | 850 | if (i == CGI.OperandList.size()) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 851 | I->error("'" + InstResults.begin()->first + |
| 852 | "' set but does not appear in operand list!"); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 853 | const std::string &OpName = CGI.OperandList[i].Name; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 854 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 855 | // Check that it exists in InstResults. |
| 856 | Record *R = InstResults[OpName]; |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 857 | if (R == 0) |
| 858 | I->error("Operand $" + OpName + " should be a set destination: all " |
| 859 | "outputs must occur before inputs in operand list!"); |
| 860 | |
| 861 | if (CGI.OperandList[i].Rec != R) |
| 862 | I->error("Operand $" + OpName + " class mismatch!"); |
| 863 | |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 864 | // Remember the return type. |
| 865 | ResultTypes.push_back(CGI.OperandList[i].Ty); |
| 866 | |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 867 | // Okay, this one checks out. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 868 | InstResults.erase(OpName); |
| 869 | } |
| 870 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 871 | // Loop over the inputs next. Make a copy of InstInputs so we can destroy |
| 872 | // the copy while we're checking the inputs. |
| 873 | std::map<std::string, TreePatternNode*> InstInputsCheck(InstInputs); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 874 | |
| 875 | std::vector<TreePatternNode*> ResultNodeOperands; |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 876 | std::vector<MVT::ValueType> OperandTypes; |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 877 | for (unsigned i = NumResults, e = CGI.OperandList.size(); i != e; ++i) { |
| 878 | const std::string &OpName = CGI.OperandList[i].Name; |
| 879 | if (OpName.empty()) |
| 880 | I->error("Operand #" + utostr(i) + " in operands list has no name!"); |
| 881 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 882 | if (!InstInputsCheck.count(OpName)) |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 883 | I->error("Operand $" + OpName + |
| 884 | " does not appear in the instruction pattern"); |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 885 | TreePatternNode *InVal = InstInputsCheck[OpName]; |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 886 | InstInputsCheck.erase(OpName); // It occurred, remove from map. |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 887 | if (CGI.OperandList[i].Ty != InVal->getType()) |
| 888 | I->error("Operand $" + OpName + |
| 889 | "'s type disagrees between the operand and pattern"); |
Chris Lattner | ae6d828 | 2005-09-15 21:51:12 +0000 | [diff] [blame] | 890 | OperandTypes.push_back(InVal->getType()); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 2175c18 | 2005-09-14 23:01:59 +0000 | [diff] [blame] | 892 | // Construct the result for the dest-pattern operand list. |
| 893 | TreePatternNode *OpNode = InVal->clone(); |
| 894 | |
| 895 | // No predicate is useful on the result. |
| 896 | OpNode->setPredicateFn(""); |
| 897 | |
| 898 | // Promote the xform function to be an explicit node if set. |
| 899 | if (Record *Xform = OpNode->getTransformFn()) { |
| 900 | OpNode->setTransformFn(0); |
| 901 | std::vector<TreePatternNode*> Children; |
| 902 | Children.push_back(OpNode); |
| 903 | OpNode = new TreePatternNode(Xform, Children); |
| 904 | } |
| 905 | |
| 906 | ResultNodeOperands.push_back(OpNode); |
Chris Lattner | 39e8af9 | 2005-09-14 18:19:25 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Chris Lattner | 0b59225 | 2005-09-14 21:59:34 +0000 | [diff] [blame] | 909 | if (!InstInputsCheck.empty()) |
| 910 | I->error("Input operand $" + InstInputsCheck.begin()->first + |
| 911 | " occurs in pattern but not in operands list!"); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 912 | |
| 913 | TreePatternNode *ResultPattern = |
| 914 | new TreePatternNode(I->getRecord(), ResultNodeOperands); |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 915 | |
| 916 | // Create and insert the instruction. |
| 917 | DAGInstruction TheInst(I, ResultTypes, OperandTypes); |
| 918 | Instructions.insert(std::make_pair(I->getRecord(), TheInst)); |
| 919 | |
| 920 | // Use a temporary tree pattern to infer all types and make sure that the |
| 921 | // constructed result is correct. This depends on the instruction already |
| 922 | // being inserted into the Instructions map. |
| 923 | TreePattern Temp(I->getRecord(), ResultPattern, *this); |
| 924 | Temp.InferAllTypes(); |
| 925 | |
| 926 | DAGInstruction &TheInsertedInst = Instructions.find(I->getRecord())->second; |
| 927 | TheInsertedInst.setResultPattern(Temp.getOnlyTree()); |
Chris Lattner | b027620 | 2005-09-14 22:55:26 +0000 | [diff] [blame] | 928 | |
Chris Lattner | 3270760 | 2005-09-08 23:22:48 +0000 | [diff] [blame] | 929 | DEBUG(I->dump()); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 930 | } |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 931 | |
Chris Lattner | d8a3bde | 2005-09-14 20:53:42 +0000 | [diff] [blame] | 932 | // If we can, convert the instructions to be patterns that are matched! |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 933 | for (std::map<Record*, DAGInstruction>::iterator II = Instructions.begin(), |
| 934 | E = Instructions.end(); II != E; ++II) { |
| 935 | TreePattern *I = II->second.getPattern(); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 936 | |
| 937 | if (I->getNumTrees() != 1) { |
| 938 | std::cerr << "CANNOT HANDLE: " << I->getRecord()->getName() << " yet!"; |
| 939 | continue; |
| 940 | } |
| 941 | TreePatternNode *Pattern = I->getTree(0); |
| 942 | if (Pattern->getOperator()->getName() != "set") |
| 943 | continue; // Not a set (store or something?) |
| 944 | |
| 945 | if (Pattern->getNumChildren() != 2) |
| 946 | continue; // Not a set of a single value (not handled so far) |
| 947 | |
| 948 | TreePatternNode *SrcPattern = Pattern->getChild(1)->clone(); |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 949 | TreePatternNode *DstPattern = II->second.getResultPattern(); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 950 | PatternsToMatch.push_back(std::make_pair(SrcPattern, DstPattern)); |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 951 | } |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 954 | void DAGISelEmitter::ParsePatterns() { |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 955 | std::vector<Record*> Patterns = Records.getAllDerivedDefinitions("Pattern"); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 956 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 957 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) { |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 958 | DagInit *Tree = Patterns[i]->getValueAsDag("PatternToMatch"); |
| 959 | TreePattern *Pattern = new TreePattern(Patterns[i], Tree, *this); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 960 | |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 961 | // Inline pattern fragments into it. |
| 962 | Pattern->InlinePatternFragments(); |
| 963 | |
| 964 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 965 | // never do anything with this pattern: report it to the user. |
| 966 | if (!Pattern->InferAllTypes()) |
| 967 | Pattern->error("Could not infer all types in pattern!"); |
| 968 | |
| 969 | ListInit *LI = Patterns[i]->getValueAsListInit("ResultInstrs"); |
| 970 | if (LI->getSize() == 0) continue; // no pattern. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 971 | |
| 972 | // Parse the instruction. |
Chris Lattner | a28aec1 | 2005-09-15 22:23:50 +0000 | [diff] [blame] | 973 | TreePattern *Result = new TreePattern(Patterns[i], LI, *this); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 974 | |
| 975 | // Inline pattern fragments into it. |
| 976 | Result->InlinePatternFragments(); |
| 977 | |
| 978 | // Infer as many types as possible. If we cannot infer all of them, we can |
| 979 | // never do anything with this pattern: report it to the user. |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 980 | if (!Result->InferAllTypes()) |
Chris Lattner | ae5b350 | 2005-09-15 21:57:35 +0000 | [diff] [blame] | 981 | Result->error("Could not infer all types in pattern result!"); |
Chris Lattner | abbb605 | 2005-09-15 21:42:00 +0000 | [diff] [blame] | 982 | |
| 983 | if (Result->getNumTrees() != 1) |
| 984 | Result->error("Cannot handle instructions producing instructions " |
| 985 | "with temporaries yet!"); |
| 986 | PatternsToMatch.push_back(std::make_pair(Pattern->getOnlyTree(), |
| 987 | Result->getOnlyTree())); |
| 988 | } |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 989 | |
| 990 | DEBUG(std::cerr << "\n\nPARSED PATTERNS TO MATCH:\n\n"; |
| 991 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) { |
| 992 | std::cerr << "PATTERN: "; PatternsToMatch[i].first->dump(); |
| 993 | std::cerr << "\nRESULT: ";PatternsToMatch[i].second->dump(); |
| 994 | std::cerr << "\n"; |
| 995 | }); |
| 996 | } |
| 997 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 998 | /// EmitMatchForPattern - Emit a matcher for N, going to the label for PatternNo |
| 999 | /// if the match fails. At this point, we already know that the opcode for N |
| 1000 | /// matches, and the SDNode for the result has the RootName specified name. |
| 1001 | void DAGISelEmitter::EmitMatchForPattern(TreePatternNode *N, |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1002 | const std::string &RootName, |
| 1003 | std::map<std::string,std::string> &VarMap, |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1004 | unsigned PatternNo, std::ostream &OS) { |
| 1005 | assert(!N->isLeaf() && "Cannot match against a leaf!"); |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1006 | |
| 1007 | // If this node has a name associated with it, capture it in VarMap. If |
| 1008 | // we already saw this in the pattern, emit code to verify dagness. |
| 1009 | if (!N->getName().empty()) { |
| 1010 | std::string &VarMapEntry = VarMap[N->getName()]; |
| 1011 | if (VarMapEntry.empty()) { |
| 1012 | VarMapEntry = RootName; |
| 1013 | } else { |
| 1014 | // If we get here, this is a second reference to a specific name. Since |
| 1015 | // we already have checked that the first reference is valid, we don't |
| 1016 | // have to recursively match it, just check that it's the same as the |
| 1017 | // previously named thing. |
| 1018 | OS << " if (" << VarMapEntry << " != " << RootName |
| 1019 | << ") goto P" << PatternNo << "Fail;\n"; |
| 1020 | return; |
| 1021 | } |
| 1022 | } |
| 1023 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1024 | // Emit code to load the child nodes and match their contents recursively. |
| 1025 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) { |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1026 | OS << " SDOperand " << RootName << i <<" = " << RootName |
| 1027 | << ".getOperand(" << i << ");\n"; |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1028 | TreePatternNode *Child = N->getChild(i); |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1030 | if (!Child->isLeaf()) { |
| 1031 | // If it's not a leaf, recursively match. |
| 1032 | const SDNodeInfo &CInfo = getSDNodeInfo(Child->getOperator()); |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1033 | OS << " if (" << RootName << i << ".getOpcode() != " |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1034 | << CInfo.getEnumName() << ") goto P" << PatternNo << "Fail;\n"; |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1035 | EmitMatchForPattern(Child, RootName + utostr(i), VarMap, PatternNo, OS); |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1036 | } else { |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1037 | // If this child has a name associated with it, capture it in VarMap. If |
| 1038 | // we already saw this in the pattern, emit code to verify dagness. |
| 1039 | if (!Child->getName().empty()) { |
| 1040 | std::string &VarMapEntry = VarMap[Child->getName()]; |
| 1041 | if (VarMapEntry.empty()) { |
| 1042 | VarMapEntry = RootName + utostr(i); |
| 1043 | } else { |
| 1044 | // If we get here, this is a second reference to a specific name. Since |
| 1045 | // we already have checked that the first reference is valid, we don't |
| 1046 | // have to recursively match it, just check that it's the same as the |
| 1047 | // previously named thing. |
| 1048 | OS << " if (" << VarMapEntry << " != " << RootName << i |
| 1049 | << ") goto P" << PatternNo << "Fail;\n"; |
| 1050 | continue; |
| 1051 | } |
| 1052 | } |
| 1053 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1054 | // Handle leaves of various types. |
| 1055 | Init *LeafVal = Child->getLeafValue(); |
| 1056 | Record *LeafRec = dynamic_cast<DefInit*>(LeafVal)->getDef(); |
| 1057 | if (LeafRec->isSubClassOf("RegisterClass")) { |
| 1058 | // Handle register references. Nothing to do here. |
| 1059 | } else if (LeafRec->isSubClassOf("ValueType")) { |
| 1060 | // Make sure this is the specified value type. |
| 1061 | OS << " if (cast<VTSDNode>(" << RootName << i << ")->getVT() != " |
| 1062 | << "MVT::" << LeafRec->getName() << ") goto P" << PatternNo |
| 1063 | << "Fail;\n"; |
| 1064 | } else { |
| 1065 | Child->dump(); |
| 1066 | assert(0 && "Unknown leaf type!"); |
| 1067 | } |
| 1068 | } |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | // If there is a node predicate for this, emit the call. |
| 1072 | if (!N->getPredicateFn().empty()) |
| 1073 | OS << " if (!" << N->getPredicateFn() << "(" << RootName |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1074 | << ".Val)) goto P" << PatternNo << "Fail;\n"; |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Chris Lattner | 6bc7e51 | 2005-09-26 21:53:26 +0000 | [diff] [blame] | 1077 | /// CodeGenPatternResult - Emit the action for a pattern. Now that it has |
| 1078 | /// matched, we actually have to build a DAG! |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1079 | unsigned DAGISelEmitter:: |
| 1080 | CodeGenPatternResult(TreePatternNode *N, unsigned &Ctr, |
| 1081 | std::map<std::string,std::string> &VariableMap, |
Chris Lattner | 6bc7e51 | 2005-09-26 21:53:26 +0000 | [diff] [blame] | 1082 | std::ostream &OS) { |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1083 | // This is something selected from the pattern we matched. |
| 1084 | if (!N->getName().empty()) { |
Chris Lattner | 6bc7e51 | 2005-09-26 21:53:26 +0000 | [diff] [blame] | 1085 | std::string &Val = VariableMap[N->getName()]; |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1086 | assert(!Val.empty() && |
| 1087 | "Variable referenced but not defined and not caught earlier!"); |
| 1088 | if (Val[0] == 'T' && Val[1] == 'm' && Val[2] == 'p') { |
| 1089 | // Already selected this operand, just return the tmpval. |
Chris Lattner | 6bc7e51 | 2005-09-26 21:53:26 +0000 | [diff] [blame] | 1090 | return atoi(Val.c_str()+3); |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1091 | } |
Chris Lattner | f6f9416 | 2005-09-28 16:58:06 +0000 | [diff] [blame^] | 1092 | |
| 1093 | unsigned ResNo = Ctr++; |
| 1094 | if (!N->isLeaf() && N->getOperator()->getName() == "imm") { |
| 1095 | switch (N->getType()) { |
| 1096 | default: assert(0 && "Unknown type for constant node!"); |
| 1097 | case MVT::i1: OS << " bool Tmp"; break; |
| 1098 | case MVT::i8: OS << " unsigned char Tmp"; break; |
| 1099 | case MVT::i16: OS << " unsigned short Tmp"; break; |
| 1100 | case MVT::i32: OS << " unsigned Tmp"; break; |
| 1101 | case MVT::i64: OS << " uint64_t Tmp"; break; |
| 1102 | } |
| 1103 | OS << ResNo << "C = cast<ConstantSDNode>(" << Val << ")->getValue();\n"; |
| 1104 | OS << " SDOperand Tmp" << ResNo << " = CurDAG->getTargetConstant(Tmp" |
| 1105 | << ResNo << "C, MVT::" << getEnumName(N->getType()) << ");\n"; |
| 1106 | } else { |
| 1107 | OS << " SDOperand Tmp" << ResNo << " = Select(" << Val << ");\n"; |
| 1108 | } |
| 1109 | // Add Tmp<ResNo> to VariableMap, so that we don't multiply select this |
| 1110 | // value if used multiple times by this pattern result. |
| 1111 | Val = "Tmp"+utostr(ResNo); |
| 1112 | return ResNo; |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | if (N->isLeaf()) { |
| 1116 | N->dump(); |
| 1117 | assert(0 && "Unknown leaf type!"); |
| 1118 | return ~0U; |
| 1119 | } |
| 1120 | |
| 1121 | Record *Op = N->getOperator(); |
| 1122 | if (Op->isSubClassOf("Instruction")) { |
| 1123 | // Emit all of the operands. |
| 1124 | std::vector<unsigned> Ops; |
| 1125 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 1126 | Ops.push_back(CodeGenPatternResult(N->getChild(i), Ctr, VariableMap, OS)); |
| 1127 | |
| 1128 | CodeGenInstruction &II = Target.getInstruction(Op->getName()); |
| 1129 | unsigned ResNo = Ctr++; |
| 1130 | |
| 1131 | OS << " SDOperand Tmp" << ResNo << " = CurDAG->getTargetNode(" |
| 1132 | << II.Namespace << "::" << II.TheDef->getName() << ", MVT::" |
| 1133 | << getEnumName(N->getType()); |
| 1134 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 1135 | OS << ", Tmp" << Ops[i]; |
| 1136 | OS << ");\n"; |
| 1137 | return ResNo; |
| 1138 | } else if (Op->isSubClassOf("SDNodeXForm")) { |
| 1139 | assert(N->getNumChildren() == 1 && "node xform should have one child!"); |
| 1140 | unsigned OpVal = CodeGenPatternResult(N->getChild(0), Ctr, VariableMap, OS); |
| 1141 | |
| 1142 | unsigned ResNo = Ctr++; |
| 1143 | OS << " SDOperand Tmp" << ResNo << " = Transform_" << Op->getName() |
| 1144 | << "(Tmp" << OpVal << ".Val);\n"; |
| 1145 | return ResNo; |
| 1146 | } else { |
| 1147 | N->dump(); |
| 1148 | assert(0 && "Unknown node in result pattern!"); |
Jeff Cohen | a48283b | 2005-09-25 19:04:43 +0000 | [diff] [blame] | 1149 | return ~0U; |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1154 | /// EmitCodeForPattern - Given a pattern to match, emit code to the specified |
| 1155 | /// stream to match the pattern, and generate the code for the match if it |
| 1156 | /// succeeds. |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1157 | void DAGISelEmitter::EmitCodeForPattern(PatternToMatch &Pattern, |
| 1158 | std::ostream &OS) { |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1159 | static unsigned PatternCount = 0; |
| 1160 | unsigned PatternNo = PatternCount++; |
| 1161 | OS << " { // Pattern #" << PatternNo << ": "; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1162 | Pattern.first->print(OS); |
| 1163 | OS << "\n"; |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 8fc3568 | 2005-09-23 23:16:51 +0000 | [diff] [blame] | 1165 | // Emit the matcher, capturing named arguments in VariableMap. |
| 1166 | std::map<std::string,std::string> VariableMap; |
| 1167 | EmitMatchForPattern(Pattern.first, "N", VariableMap, PatternNo, OS); |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1169 | OS << " // Emit: "; |
| 1170 | Pattern.second->print(OS); |
| 1171 | OS << "\n"; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1172 | |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1173 | unsigned TmpNo = 0; |
| 1174 | unsigned Res = CodeGenPatternResult(Pattern.second, TmpNo, VariableMap, OS); |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Add the result to the map if it has multiple uses. |
| 1177 | OS << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp" << Res << ";\n"; |
Chris Lattner | 72fe91c | 2005-09-24 00:40:24 +0000 | [diff] [blame] | 1178 | OS << " return Tmp" << Res << ";\n"; |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1179 | OS << " }\n P" << PatternNo << "Fail:\n"; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /// getPatternSize - Return the 'size' of this pattern. We want to match large |
| 1183 | /// patterns before small ones. This is used to determine the size of a |
| 1184 | /// pattern. |
| 1185 | static unsigned getPatternSize(TreePatternNode *P) { |
| 1186 | assert(MVT::isInteger(P->getType()) || MVT::isFloatingPoint(P->getType()) && |
| 1187 | "Not a valid pattern node to size!"); |
| 1188 | unsigned Size = 1; // The node itself. |
| 1189 | |
| 1190 | // Count children in the count if they are also nodes. |
| 1191 | for (unsigned i = 0, e = P->getNumChildren(); i != e; ++i) { |
| 1192 | TreePatternNode *Child = P->getChild(i); |
| 1193 | if (!Child->isLeaf() && Child->getType() != MVT::Other) |
| 1194 | Size += getPatternSize(Child); |
| 1195 | } |
| 1196 | |
| 1197 | return Size; |
| 1198 | } |
| 1199 | |
| 1200 | // PatternSortingPredicate - return true if we prefer to match LHS before RHS. |
| 1201 | // In particular, we want to match maximal patterns first and lowest cost within |
| 1202 | // a particular complexity first. |
| 1203 | struct PatternSortingPredicate { |
| 1204 | bool operator()(DAGISelEmitter::PatternToMatch *LHS, |
| 1205 | DAGISelEmitter::PatternToMatch *RHS) { |
| 1206 | unsigned LHSSize = getPatternSize(LHS->first); |
| 1207 | unsigned RHSSize = getPatternSize(RHS->first); |
| 1208 | if (LHSSize > RHSSize) return true; // LHS -> bigger -> less cost |
| 1209 | if (LHSSize < RHSSize) return false; |
| 1210 | |
| 1211 | // If they are equal, compare cost. |
| 1212 | // FIXME: Compute cost! |
| 1213 | return false; |
| 1214 | } |
| 1215 | }; |
| 1216 | |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 1217 | |
| 1218 | namespace { |
| 1219 | /// CompareByRecordName - An ordering predicate that implements less-than by |
| 1220 | /// comparing the names records. |
| 1221 | struct CompareByRecordName { |
| 1222 | bool operator()(const Record *LHS, const Record *RHS) const { |
| 1223 | // Sort by name first. |
| 1224 | if (LHS->getName() < RHS->getName()) return true; |
| 1225 | // If both names are equal, sort by pointer. |
| 1226 | return LHS->getName() == RHS->getName() && LHS < RHS; |
| 1227 | } |
| 1228 | }; |
| 1229 | } |
| 1230 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1231 | void DAGISelEmitter::EmitInstructionSelector(std::ostream &OS) { |
| 1232 | // Emit boilerplate. |
| 1233 | OS << "// The main instruction selector code.\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1234 | << "SDOperand SelectCode(SDOperand N) {\n" |
| 1235 | << " if (N.getOpcode() >= ISD::BUILTIN_OP_END &&\n" |
| 1236 | << " N.getOpcode() < PPCISD::FIRST_NUMBER)\n" |
| 1237 | << " return N; // Already selected.\n\n" |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1238 | << " if (!N.Val->hasOneUse()) {\n" |
| 1239 | << " std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(N);\n" |
| 1240 | << " if (CGMI != CodeGenMap.end()) return CGMI->second;\n" |
| 1241 | << " }\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1242 | << " switch (N.getOpcode()) {\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1243 | << " default: break;\n" |
| 1244 | << " case ISD::EntryToken: // These leaves remain the same.\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1245 | << " return N;\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1246 | << " case ISD::AssertSext:\n" |
Chris Lattner | fab3728 | 2005-09-26 22:10:24 +0000 | [diff] [blame] | 1247 | << " case ISD::AssertZext: {\n" |
| 1248 | << " SDOperand Tmp0 = Select(N.getOperand(0));\n" |
| 1249 | << " if (!N.Val->hasOneUse()) CodeGenMap[N] = Tmp0;\n" |
| 1250 | << " return Tmp0;\n" |
| 1251 | << " }\n"; |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1253 | // Group the patterns by their top-level opcodes. |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 1254 | std::map<Record*, std::vector<PatternToMatch*>, |
| 1255 | CompareByRecordName> PatternsByOpcode; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1256 | for (unsigned i = 0, e = PatternsToMatch.size(); i != e; ++i) |
| 1257 | PatternsByOpcode[PatternsToMatch[i].first->getOperator()] |
| 1258 | .push_back(&PatternsToMatch[i]); |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1260 | // Loop over all of the case statements. |
Chris Lattner | 3748147 | 2005-09-26 21:59:35 +0000 | [diff] [blame] | 1261 | for (std::map<Record*, std::vector<PatternToMatch*>, |
| 1262 | CompareByRecordName>::iterator PBOI = PatternsByOpcode.begin(), |
| 1263 | E = PatternsByOpcode.end(); PBOI != E; ++PBOI) { |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1264 | const SDNodeInfo &OpcodeInfo = getSDNodeInfo(PBOI->first); |
| 1265 | std::vector<PatternToMatch*> &Patterns = PBOI->second; |
| 1266 | |
| 1267 | OS << " case " << OpcodeInfo.getEnumName() << ":\n"; |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1268 | |
| 1269 | // We want to emit all of the matching code now. However, we want to emit |
| 1270 | // the matches in order of minimal cost. Sort the patterns so the least |
| 1271 | // cost one is at the start. |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1272 | std::stable_sort(Patterns.begin(), Patterns.end(), |
| 1273 | PatternSortingPredicate()); |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1274 | |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1275 | for (unsigned i = 0, e = Patterns.size(); i != e; ++i) |
| 1276 | EmitCodeForPattern(*Patterns[i], OS); |
Chris Lattner | d1ff35a | 2005-09-23 21:33:23 +0000 | [diff] [blame] | 1277 | OS << " break;\n\n"; |
Chris Lattner | 8130332 | 2005-09-23 19:36:15 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1281 | OS << " } // end of big switch.\n\n" |
| 1282 | << " std::cerr << \"Cannot yet select: \";\n" |
Chris Lattner | 547394c | 2005-09-23 21:53:45 +0000 | [diff] [blame] | 1283 | << " N.Val->dump();\n" |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1284 | << " std::cerr << '\\n';\n" |
| 1285 | << " abort();\n" |
| 1286 | << "}\n"; |
| 1287 | } |
| 1288 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1289 | void DAGISelEmitter::run(std::ostream &OS) { |
| 1290 | EmitSourceFileHeader("DAG Instruction Selector for the " + Target.getName() + |
| 1291 | " target", OS); |
| 1292 | |
Chris Lattner | 1f39e29 | 2005-09-14 00:09:24 +0000 | [diff] [blame] | 1293 | OS << "// *** NOTE: This file is #included into the middle of the target\n" |
| 1294 | << "// *** instruction selector class. These functions are really " |
| 1295 | << "methods.\n\n"; |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1296 | |
Chris Lattner | 296dfe3 | 2005-09-24 00:50:51 +0000 | [diff] [blame] | 1297 | OS << "// Instance var to keep track of multiply used nodes that have \n" |
| 1298 | << "// already been selected.\n" |
| 1299 | << "std::map<SDOperand, SDOperand> CodeGenMap;\n"; |
| 1300 | |
Chris Lattner | ca559d0 | 2005-09-08 21:03:01 +0000 | [diff] [blame] | 1301 | ParseNodeInfo(); |
Chris Lattner | 24eeeb8 | 2005-09-13 21:51:00 +0000 | [diff] [blame] | 1302 | ParseNodeTransforms(OS); |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1303 | ParsePatternFragments(OS); |
| 1304 | ParseInstructions(); |
| 1305 | ParsePatterns(); |
Chris Lattner | 3f7e914 | 2005-09-23 20:52:47 +0000 | [diff] [blame] | 1306 | |
| 1307 | // FIXME: Generate variants. For example, commutative patterns can match |
| 1308 | // multiple ways. Add them to PatternsToMatch as well. |
Chris Lattner | b39e4be | 2005-09-15 02:38:02 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | b9f01eb | 2005-09-16 00:29:46 +0000 | [diff] [blame] | 1310 | // At this point, we have full information about the 'Patterns' we need to |
| 1311 | // parse, both implicitly from instructions as well as from explicit pattern |
| 1312 | // definitions. |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1313 | |
| 1314 | EmitInstructionSelector(OS); |
| 1315 | |
| 1316 | for (std::map<Record*, TreePattern*>::iterator I = PatternFragments.begin(), |
| 1317 | E = PatternFragments.end(); I != E; ++I) |
| 1318 | delete I->second; |
| 1319 | PatternFragments.clear(); |
| 1320 | |
Chris Lattner | 54cb8fd | 2005-09-07 23:44:43 +0000 | [diff] [blame] | 1321 | Instructions.clear(); |
| 1322 | } |