1 //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file declares the CodeGenDAGPatterns class, which is used to read and 11 // represent the patterns present in a .td file for instructions. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H 16 #define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H 17 18 #include "CodeGenIntrinsics.h" 19 #include "CodeGenTarget.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include <algorithm> 24 #include <map> 25 #include <set> 26 #include <vector> 27 28 namespace llvm { 29 class Record; 30 class Init; 31 class ListInit; 32 class DagInit; 33 class SDNodeInfo; 34 class TreePattern; 35 class TreePatternNode; 36 class CodeGenDAGPatterns; 37 class ComplexPattern; 38 39 /// EEVT::DAGISelGenValueType - These are some extended forms of 40 /// MVT::SimpleValueType that we use as lattice values during type inference. 41 /// The existing MVT iAny, fAny and vAny types suffice to represent 42 /// arbitrary integer, floating-point, and vector types, so only an unknown 43 /// value is needed. 44 namespace EEVT { 45 /// TypeSet - This is either empty if it's completely unknown, or holds a set 46 /// of types. It is used during type inference because register classes can 47 /// have multiple possible types and we don't know which one they get until 48 /// type inference is complete. 49 /// 50 /// TypeSet can have three states: 51 /// Vector is empty: The type is completely unknown, it can be any valid 52 /// target type. 53 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one 54 /// of those types only. 55 /// Vector has one concrete type: The type is completely known. 56 /// 57 class TypeSet { 58 SmallVector<MVT::SimpleValueType, 4> TypeVec; 59 public: 60 TypeSet() {} 61 TypeSet(MVT::SimpleValueType VT, TreePattern &TP); 62 TypeSet(ArrayRef<MVT::SimpleValueType> VTList); 63 64 bool isCompletelyUnknown() const { return TypeVec.empty(); } 65 66 bool isConcrete() const { 67 if (TypeVec.size() != 1) return false; 68 unsigned char T = TypeVec[0]; (void)T; 69 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny); 70 return true; 71 } 72 73 MVT::SimpleValueType getConcrete() const { 74 assert(isConcrete() && "Type isn't concrete yet"); 75 return (MVT::SimpleValueType)TypeVec[0]; 76 } 77 78 bool isDynamicallyResolved() const { 79 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny; 80 } 81 82 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const { 83 assert(!TypeVec.empty() && "Not a type list!"); 84 return TypeVec; 85 } 86 87 bool isVoid() const { 88 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid; 89 } 90 91 /// hasIntegerTypes - Return true if this TypeSet contains any integer value 92 /// types. 93 bool hasIntegerTypes() const; 94 95 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or 96 /// a floating point value type. 97 bool hasFloatingPointTypes() const; 98 99 /// hasScalarTypes - Return true if this TypeSet contains a scalar value 100 /// type. 101 bool hasScalarTypes() const; 102 103 /// hasVectorTypes - Return true if this TypeSet contains a vector value 104 /// type. 105 bool hasVectorTypes() const; 106 107 /// getName() - Return this TypeSet as a string. 108 std::string getName() const; 109 110 /// MergeInTypeInfo - This merges in type information from the specified 111 /// argument. If 'this' changes, it returns true. If the two types are 112 /// contradictory (e.g. merge f32 into i32) then this flags an error. 113 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP); 114 115 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) { 116 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP); 117 } 118 119 /// Force this type list to only contain integer types. 120 bool EnforceInteger(TreePattern &TP); 121 122 /// Force this type list to only contain floating point types. 123 bool EnforceFloatingPoint(TreePattern &TP); 124 125 /// EnforceScalar - Remove all vector types from this type list. 126 bool EnforceScalar(TreePattern &TP); 127 128 /// EnforceVector - Remove all non-vector types from this type list. 129 bool EnforceVector(TreePattern &TP); 130 131 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update 132 /// this an other based on this information. 133 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP); 134 135 /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type 136 /// whose element is VT. 137 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP); 138 139 /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type 140 /// whose element is VT. 141 bool EnforceVectorEltTypeIs(MVT::SimpleValueType VT, TreePattern &TP); 142 143 /// EnforceVectorSubVectorTypeIs - 'this' is now constrained to 144 /// be a vector type VT. 145 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP); 146 147 /// EnforceSameNumElts - If VTOperand is a scalar, then 'this' is a scalar. 148 /// If VTOperand is a vector, then 'this' must have the same number of 149 /// elements. 150 bool EnforceSameNumElts(EEVT::TypeSet &VT, TreePattern &TP); 151 152 /// EnforceSameSize - 'this' is now constrained to be the same size as VT. 153 bool EnforceSameSize(EEVT::TypeSet &VT, TreePattern &TP); 154 155 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; } 156 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; } 157 158 private: 159 /// FillWithPossibleTypes - Set to all legal types and return true, only 160 /// valid on completely unknown type sets. If Pred is non-null, only MVTs 161 /// that pass the predicate are added. 162 bool FillWithPossibleTypes(TreePattern &TP, 163 bool (*Pred)(MVT::SimpleValueType) = nullptr, 164 const char *PredicateName = nullptr); 165 }; 166 } 167 168 /// Set type used to track multiply used variables in patterns 169 typedef std::set<std::string> MultipleUseVarSet; 170 171 /// SDTypeConstraint - This is a discriminated union of constraints, 172 /// corresponding to the SDTypeConstraint tablegen class in Target.td. 173 struct SDTypeConstraint { 174 SDTypeConstraint(Record *R); 175 176 unsigned OperandNo; // The operand # this constraint applies to. 177 enum { 178 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs, 179 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec, 180 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs 181 } ConstraintType; 182 183 union { // The discriminated union. 184 struct { 185 MVT::SimpleValueType VT; 186 } SDTCisVT_Info; 187 struct { 188 unsigned OtherOperandNum; 189 } SDTCisSameAs_Info; 190 struct { 191 unsigned OtherOperandNum; 192 } SDTCisVTSmallerThanOp_Info; 193 struct { 194 unsigned BigOperandNum; 195 } SDTCisOpSmallerThanOp_Info; 196 struct { 197 unsigned OtherOperandNum; 198 } SDTCisEltOfVec_Info; 199 struct { 200 unsigned OtherOperandNum; 201 } SDTCisSubVecOfVec_Info; 202 struct { 203 MVT::SimpleValueType VT; 204 } SDTCVecEltisVT_Info; 205 struct { 206 unsigned OtherOperandNum; 207 } SDTCisSameNumEltsAs_Info; 208 struct { 209 unsigned OtherOperandNum; 210 } SDTCisSameSizeAs_Info; 211 } x; 212 213 /// ApplyTypeConstraint - Given a node in a pattern, apply this type 214 /// constraint to the nodes operands. This returns true if it makes a 215 /// change, false otherwise. If a type contradiction is found, an error 216 /// is flagged. 217 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo, 218 TreePattern &TP) const; 219 }; 220 221 /// SDNodeInfo - One of these records is created for each SDNode instance in 222 /// the target .td file. This represents the various dag nodes we will be 223 /// processing. 224 class SDNodeInfo { 225 Record *Def; 226 StringRef EnumName; 227 StringRef SDClassName; 228 unsigned Properties; 229 unsigned NumResults; 230 int NumOperands; 231 std::vector<SDTypeConstraint> TypeConstraints; 232 public: 233 SDNodeInfo(Record *R); // Parse the specified record. 234 235 unsigned getNumResults() const { return NumResults; } 236 237 /// getNumOperands - This is the number of operands required or -1 if 238 /// variadic. 239 int getNumOperands() const { return NumOperands; } 240 Record *getRecord() const { return Def; } 241 StringRef getEnumName() const { return EnumName; } 242 StringRef getSDClassName() const { return SDClassName; } 243 244 const std::vector<SDTypeConstraint> &getTypeConstraints() const { 245 return TypeConstraints; 246 } 247 248 /// getKnownType - If the type constraints on this node imply a fixed type 249 /// (e.g. all stores return void, etc), then return it as an 250 /// MVT::SimpleValueType. Otherwise, return MVT::Other. 251 MVT::SimpleValueType getKnownType(unsigned ResNo) const; 252 253 /// hasProperty - Return true if this node has the specified property. 254 /// 255 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); } 256 257 /// ApplyTypeConstraints - Given a node in a pattern, apply the type 258 /// constraints for this node to the operands of the node. This returns 259 /// true if it makes a change, false otherwise. If a type contradiction is 260 /// found, an error is flagged. 261 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const { 262 bool MadeChange = false; 263 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i) 264 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP); 265 return MadeChange; 266 } 267 }; 268 269 /// TreePredicateFn - This is an abstraction that represents the predicates on 270 /// a PatFrag node. This is a simple one-word wrapper around a pointer to 271 /// provide nice accessors. 272 class TreePredicateFn { 273 /// PatFragRec - This is the TreePattern for the PatFrag that we 274 /// originally came from. 275 TreePattern *PatFragRec; 276 public: 277 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag. 278 TreePredicateFn(TreePattern *N); 279 280 281 TreePattern *getOrigPatFragRecord() const { return PatFragRec; } 282 283 /// isAlwaysTrue - Return true if this is a noop predicate. 284 bool isAlwaysTrue() const; 285 286 bool isImmediatePattern() const { return !getImmCode().empty(); } 287 288 /// getImmediatePredicateCode - Return the code that evaluates this pattern if 289 /// this is an immediate predicate. It is an error to call this on a 290 /// non-immediate pattern. 291 std::string getImmediatePredicateCode() const { 292 std::string Result = getImmCode(); 293 assert(!Result.empty() && "Isn't an immediate pattern!"); 294 return Result; 295 } 296 297 298 bool operator==(const TreePredicateFn &RHS) const { 299 return PatFragRec == RHS.PatFragRec; 300 } 301 302 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); } 303 304 /// Return the name to use in the generated code to reference this, this is 305 /// "Predicate_foo" if from a pattern fragment "foo". 306 std::string getFnName() const; 307 308 /// getCodeToRunOnSDNode - Return the code for the function body that 309 /// evaluates this predicate. The argument is expected to be in "Node", 310 /// not N. This handles casting and conversion to a concrete node type as 311 /// appropriate. 312 std::string getCodeToRunOnSDNode() const; 313 314 private: 315 std::string getPredCode() const; 316 std::string getImmCode() const; 317 }; 318 319 320 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped 321 /// patterns), and as such should be ref counted. We currently just leak all 322 /// TreePatternNode objects! 323 class TreePatternNode { 324 /// The type of each node result. Before and during type inference, each 325 /// result may be a set of possible types. After (successful) type inference, 326 /// each is a single concrete type. 327 SmallVector<EEVT::TypeSet, 1> Types; 328 329 /// Operator - The Record for the operator if this is an interior node (not 330 /// a leaf). 331 Record *Operator; 332 333 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf. 334 /// 335 Init *Val; 336 337 /// Name - The name given to this node with the :$foo notation. 338 /// 339 std::string Name; 340 341 /// PredicateFns - The predicate functions to execute on this node to check 342 /// for a match. If this list is empty, no predicate is involved. 343 std::vector<TreePredicateFn> PredicateFns; 344 345 /// TransformFn - The transformation function to execute on this node before 346 /// it can be substituted into the resulting instruction on a pattern match. 347 Record *TransformFn; 348 349 std::vector<TreePatternNode*> Children; 350 public: 351 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch, 352 unsigned NumResults) 353 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) { 354 Types.resize(NumResults); 355 } 356 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor 357 : Operator(nullptr), Val(val), TransformFn(nullptr) { 358 Types.resize(NumResults); 359 } 360 ~TreePatternNode(); 361 362 bool hasName() const { return !Name.empty(); } 363 const std::string &getName() const { return Name; } 364 void setName(StringRef N) { Name.assign(N.begin(), N.end()); } 365 366 bool isLeaf() const { return Val != nullptr; } 367 368 // Type accessors. 369 unsigned getNumTypes() const { return Types.size(); } 370 MVT::SimpleValueType getType(unsigned ResNo) const { 371 return Types[ResNo].getConcrete(); 372 } 373 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; } 374 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; } 375 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; } 376 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; } 377 378 bool hasTypeSet(unsigned ResNo) const { 379 return Types[ResNo].isConcrete(); 380 } 381 bool isTypeCompletelyUnknown(unsigned ResNo) const { 382 return Types[ResNo].isCompletelyUnknown(); 383 } 384 bool isTypeDynamicallyResolved(unsigned ResNo) const { 385 return Types[ResNo].isDynamicallyResolved(); 386 } 387 388 Init *getLeafValue() const { assert(isLeaf()); return Val; } 389 Record *getOperator() const { assert(!isLeaf()); return Operator; } 390 391 unsigned getNumChildren() const { return Children.size(); } 392 TreePatternNode *getChild(unsigned N) const { return Children[N]; } 393 void setChild(unsigned i, TreePatternNode *N) { 394 Children[i] = N; 395 } 396 397 /// hasChild - Return true if N is any of our children. 398 bool hasChild(const TreePatternNode *N) const { 399 for (unsigned i = 0, e = Children.size(); i != e; ++i) 400 if (Children[i] == N) return true; 401 return false; 402 } 403 404 bool hasAnyPredicate() const { return !PredicateFns.empty(); } 405 406 const std::vector<TreePredicateFn> &getPredicateFns() const { 407 return PredicateFns; 408 } 409 void clearPredicateFns() { PredicateFns.clear(); } 410 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) { 411 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!"); 412 PredicateFns = Fns; 413 } 414 void addPredicateFn(const TreePredicateFn &Fn) { 415 assert(!Fn.isAlwaysTrue() && "Empty predicate string!"); 416 if (!is_contained(PredicateFns, Fn)) 417 PredicateFns.push_back(Fn); 418 } 419 420 Record *getTransformFn() const { return TransformFn; } 421 void setTransformFn(Record *Fn) { TransformFn = Fn; } 422 423 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the 424 /// CodeGenIntrinsic information for it, otherwise return a null pointer. 425 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const; 426 427 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern, 428 /// return the ComplexPattern information, otherwise return null. 429 const ComplexPattern * 430 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const; 431 432 /// Returns the number of MachineInstr operands that would be produced by this 433 /// node if it mapped directly to an output Instruction's 434 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it 435 /// for Operands; otherwise 1. 436 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const; 437 438 /// NodeHasProperty - Return true if this node has the specified property. 439 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const; 440 441 /// TreeHasProperty - Return true if any node in this tree has the specified 442 /// property. 443 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const; 444 445 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is 446 /// marked isCommutative. 447 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const; 448 449 void print(raw_ostream &OS) const; 450 void dump() const; 451 452 public: // Higher level manipulation routines. 453 454 /// clone - Return a new copy of this tree. 455 /// 456 TreePatternNode *clone() const; 457 458 /// RemoveAllTypes - Recursively strip all the types of this tree. 459 void RemoveAllTypes(); 460 461 /// isIsomorphicTo - Return true if this node is recursively isomorphic to 462 /// the specified node. For this comparison, all of the state of the node 463 /// is considered, except for the assigned name. Nodes with differing names 464 /// that are otherwise identical are considered isomorphic. 465 bool isIsomorphicTo(const TreePatternNode *N, 466 const MultipleUseVarSet &DepVars) const; 467 468 /// SubstituteFormalArguments - Replace the formal arguments in this tree 469 /// with actual values specified by ArgMap. 470 void SubstituteFormalArguments(std::map<std::string, 471 TreePatternNode*> &ArgMap); 472 473 /// InlinePatternFragments - If this pattern refers to any pattern 474 /// fragments, inline them into place, giving us a pattern without any 475 /// PatFrag references. 476 TreePatternNode *InlinePatternFragments(TreePattern &TP); 477 478 /// ApplyTypeConstraints - Apply all of the type constraints relevant to 479 /// this node and its children in the tree. This returns true if it makes a 480 /// change, false otherwise. If a type contradiction is found, flag an error. 481 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters); 482 483 /// UpdateNodeType - Set the node type of N to VT if VT contains 484 /// information. If N already contains a conflicting type, then flag an 485 /// error. This returns true if any information was updated. 486 /// 487 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy, 488 TreePattern &TP) { 489 return Types[ResNo].MergeInTypeInfo(InTy, TP); 490 } 491 492 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy, 493 TreePattern &TP) { 494 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP); 495 } 496 497 // Update node type with types inferred from an instruction operand or result 498 // def from the ins/outs lists. 499 // Return true if the type changed. 500 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP); 501 502 /// ContainsUnresolvedType - Return true if this tree contains any 503 /// unresolved types. 504 bool ContainsUnresolvedType() const { 505 for (unsigned i = 0, e = Types.size(); i != e; ++i) 506 if (!Types[i].isConcrete()) return true; 507 508 for (unsigned i = 0, e = getNumChildren(); i != e; ++i) 509 if (getChild(i)->ContainsUnresolvedType()) return true; 510 return false; 511 } 512 513 /// canPatternMatch - If it is impossible for this pattern to match on this 514 /// target, fill in Reason and return false. Otherwise, return true. 515 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP); 516 }; 517 518 inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) { 519 TPN.print(OS); 520 return OS; 521 } 522 523 524 /// TreePattern - Represent a pattern, used for instructions, pattern 525 /// fragments, etc. 526 /// 527 class TreePattern { 528 /// Trees - The list of pattern trees which corresponds to this pattern. 529 /// Note that PatFrag's only have a single tree. 530 /// 531 std::vector<TreePatternNode*> Trees; 532 533 /// NamedNodes - This is all of the nodes that have names in the trees in this 534 /// pattern. 535 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes; 536 537 /// TheRecord - The actual TableGen record corresponding to this pattern. 538 /// 539 Record *TheRecord; 540 541 /// Args - This is a list of all of the arguments to this pattern (for 542 /// PatFrag patterns), which are the 'node' markers in this pattern. 543 std::vector<std::string> Args; 544 545 /// CDP - the top-level object coordinating this madness. 546 /// 547 CodeGenDAGPatterns &CDP; 548 549 /// isInputPattern - True if this is an input pattern, something to match. 550 /// False if this is an output pattern, something to emit. 551 bool isInputPattern; 552 553 /// hasError - True if the currently processed nodes have unresolvable types 554 /// or other non-fatal errors 555 bool HasError; 556 557 /// It's important that the usage of operands in ComplexPatterns is 558 /// consistent: each named operand can be defined by at most one 559 /// ComplexPattern. This records the ComplexPattern instance and the operand 560 /// number for each operand encountered in a ComplexPattern to aid in that 561 /// check. 562 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands; 563 public: 564 565 /// TreePattern constructor - Parse the specified DagInits into the 566 /// current record. 567 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput, 568 CodeGenDAGPatterns &ise); 569 TreePattern(Record *TheRec, DagInit *Pat, bool isInput, 570 CodeGenDAGPatterns &ise); 571 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput, 572 CodeGenDAGPatterns &ise); 573 574 /// getTrees - Return the tree patterns which corresponds to this pattern. 575 /// 576 const std::vector<TreePatternNode*> &getTrees() const { return Trees; } 577 unsigned getNumTrees() const { return Trees.size(); } 578 TreePatternNode *getTree(unsigned i) const { return Trees[i]; } 579 TreePatternNode *getOnlyTree() const { 580 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!"); 581 return Trees[0]; 582 } 583 584 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() { 585 if (NamedNodes.empty()) 586 ComputeNamedNodes(); 587 return NamedNodes; 588 } 589 590 /// getRecord - Return the actual TableGen record corresponding to this 591 /// pattern. 592 /// 593 Record *getRecord() const { return TheRecord; } 594 595 unsigned getNumArgs() const { return Args.size(); } 596 const std::string &getArgName(unsigned i) const { 597 assert(i < Args.size() && "Argument reference out of range!"); 598 return Args[i]; 599 } 600 std::vector<std::string> &getArgList() { return Args; } 601 602 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; } 603 604 /// InlinePatternFragments - If this pattern refers to any pattern 605 /// fragments, inline them into place, giving us a pattern without any 606 /// PatFrag references. 607 void InlinePatternFragments() { 608 for (unsigned i = 0, e = Trees.size(); i != e; ++i) 609 Trees[i] = Trees[i]->InlinePatternFragments(*this); 610 } 611 612 /// InferAllTypes - Infer/propagate as many types throughout the expression 613 /// patterns as possible. Return true if all types are inferred, false 614 /// otherwise. Bail out if a type contradiction is found. 615 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> > 616 *NamedTypes=nullptr); 617 618 /// error - If this is the first error in the current resolution step, 619 /// print it and set the error flag. Otherwise, continue silently. 620 void error(const Twine &Msg); 621 bool hasError() const { 622 return HasError; 623 } 624 void resetError() { 625 HasError = false; 626 } 627 628 void print(raw_ostream &OS) const; 629 void dump() const; 630 631 private: 632 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName); 633 void ComputeNamedNodes(); 634 void ComputeNamedNodes(TreePatternNode *N); 635 }; 636 637 /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps 638 /// that has a set ExecuteAlways / DefaultOps field. 639 struct DAGDefaultOperand { 640 std::vector<TreePatternNode*> DefaultOps; 641 }; 642 643 class DAGInstruction { 644 TreePattern *Pattern; 645 std::vector<Record*> Results; 646 std::vector<Record*> Operands; 647 std::vector<Record*> ImpResults; 648 TreePatternNode *ResultPattern; 649 public: 650 DAGInstruction(TreePattern *TP, 651 const std::vector<Record*> &results, 652 const std::vector<Record*> &operands, 653 const std::vector<Record*> &impresults) 654 : Pattern(TP), Results(results), Operands(operands), 655 ImpResults(impresults), ResultPattern(nullptr) {} 656 657 TreePattern *getPattern() const { return Pattern; } 658 unsigned getNumResults() const { return Results.size(); } 659 unsigned getNumOperands() const { return Operands.size(); } 660 unsigned getNumImpResults() const { return ImpResults.size(); } 661 const std::vector<Record*>& getImpResults() const { return ImpResults; } 662 663 void setResultPattern(TreePatternNode *R) { ResultPattern = R; } 664 665 Record *getResult(unsigned RN) const { 666 assert(RN < Results.size()); 667 return Results[RN]; 668 } 669 670 Record *getOperand(unsigned ON) const { 671 assert(ON < Operands.size()); 672 return Operands[ON]; 673 } 674 675 Record *getImpResult(unsigned RN) const { 676 assert(RN < ImpResults.size()); 677 return ImpResults[RN]; 678 } 679 680 TreePatternNode *getResultPattern() const { return ResultPattern; } 681 }; 682 683 /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns 684 /// processed to produce isel. 685 class PatternToMatch { 686 public: 687 PatternToMatch(Record *srcrecord, ListInit *preds, 688 TreePatternNode *src, TreePatternNode *dst, 689 const std::vector<Record*> &dstregs, 690 int complexity, unsigned uid) 691 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst), 692 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {} 693 694 Record *SrcRecord; // Originating Record for the pattern. 695 ListInit *Predicates; // Top level predicate conditions to match. 696 TreePatternNode *SrcPattern; // Source pattern to match. 697 TreePatternNode *DstPattern; // Resulting pattern. 698 std::vector<Record*> Dstregs; // Physical register defs being matched. 699 int AddedComplexity; // Add to matching pattern complexity. 700 unsigned ID; // Unique ID for the record. 701 702 Record *getSrcRecord() const { return SrcRecord; } 703 ListInit *getPredicates() const { return Predicates; } 704 TreePatternNode *getSrcPattern() const { return SrcPattern; } 705 TreePatternNode *getDstPattern() const { return DstPattern; } 706 const std::vector<Record*> &getDstRegs() const { return Dstregs; } 707 int getAddedComplexity() const { return AddedComplexity; } 708 709 std::string getPredicateCheck() const; 710 711 /// Compute the complexity metric for the input pattern. This roughly 712 /// corresponds to the number of nodes that are covered. 713 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const; 714 }; 715 716 class CodeGenDAGPatterns { 717 RecordKeeper &Records; 718 CodeGenTarget Target; 719 CodeGenIntrinsicTable Intrinsics; 720 CodeGenIntrinsicTable TgtIntrinsics; 721 722 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes; 723 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms; 724 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns; 725 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID> 726 PatternFragments; 727 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands; 728 std::map<Record*, DAGInstruction, LessRecordByID> Instructions; 729 730 // Specific SDNode definitions: 731 Record *intrinsic_void_sdnode; 732 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode; 733 734 /// PatternsToMatch - All of the things we are matching on the DAG. The first 735 /// value is the pattern to match, the second pattern is the result to 736 /// emit. 737 std::vector<PatternToMatch> PatternsToMatch; 738 public: 739 CodeGenDAGPatterns(RecordKeeper &R); 740 741 CodeGenTarget &getTargetInfo() { return Target; } 742 const CodeGenTarget &getTargetInfo() const { return Target; } 743 744 Record *getSDNodeNamed(const std::string &Name) const; 745 746 const SDNodeInfo &getSDNodeInfo(Record *R) const { 747 assert(SDNodes.count(R) && "Unknown node!"); 748 return SDNodes.find(R)->second; 749 } 750 751 // Node transformation lookups. 752 typedef std::pair<Record*, std::string> NodeXForm; 753 const NodeXForm &getSDNodeTransform(Record *R) const { 754 assert(SDNodeXForms.count(R) && "Invalid transform!"); 755 return SDNodeXForms.find(R)->second; 756 } 757 758 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator 759 nx_iterator; 760 nx_iterator nx_begin() const { return SDNodeXForms.begin(); } 761 nx_iterator nx_end() const { return SDNodeXForms.end(); } 762 763 764 const ComplexPattern &getComplexPattern(Record *R) const { 765 assert(ComplexPatterns.count(R) && "Unknown addressing mode!"); 766 return ComplexPatterns.find(R)->second; 767 } 768 769 const CodeGenIntrinsic &getIntrinsic(Record *R) const { 770 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i) 771 if (Intrinsics[i].TheDef == R) return Intrinsics[i]; 772 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i) 773 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i]; 774 llvm_unreachable("Unknown intrinsic!"); 775 } 776 777 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const { 778 if (IID-1 < Intrinsics.size()) 779 return Intrinsics[IID-1]; 780 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size()) 781 return TgtIntrinsics[IID-Intrinsics.size()-1]; 782 llvm_unreachable("Bad intrinsic ID!"); 783 } 784 785 unsigned getIntrinsicID(Record *R) const { 786 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i) 787 if (Intrinsics[i].TheDef == R) return i; 788 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i) 789 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size(); 790 llvm_unreachable("Unknown intrinsic!"); 791 } 792 793 const DAGDefaultOperand &getDefaultOperand(Record *R) const { 794 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!"); 795 return DefaultOperands.find(R)->second; 796 } 797 798 // Pattern Fragment information. 799 TreePattern *getPatternFragment(Record *R) const { 800 assert(PatternFragments.count(R) && "Invalid pattern fragment request!"); 801 return PatternFragments.find(R)->second.get(); 802 } 803 TreePattern *getPatternFragmentIfRead(Record *R) const { 804 if (!PatternFragments.count(R)) 805 return nullptr; 806 return PatternFragments.find(R)->second.get(); 807 } 808 809 typedef std::map<Record *, std::unique_ptr<TreePattern>, 810 LessRecordByID>::const_iterator pf_iterator; 811 pf_iterator pf_begin() const { return PatternFragments.begin(); } 812 pf_iterator pf_end() const { return PatternFragments.end(); } 813 iterator_range<pf_iterator> ptfs() const { return PatternFragments; } 814 815 // Patterns to match information. 816 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator; 817 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); } 818 ptm_iterator ptm_end() const { return PatternsToMatch.end(); } 819 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; } 820 821 /// Parse the Pattern for an instruction, and insert the result in DAGInsts. 822 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap; 823 const DAGInstruction &parseInstructionPattern( 824 CodeGenInstruction &CGI, ListInit *Pattern, 825 DAGInstMap &DAGInsts); 826 827 const DAGInstruction &getInstruction(Record *R) const { 828 assert(Instructions.count(R) && "Unknown instruction!"); 829 return Instructions.find(R)->second; 830 } 831 832 Record *get_intrinsic_void_sdnode() const { 833 return intrinsic_void_sdnode; 834 } 835 Record *get_intrinsic_w_chain_sdnode() const { 836 return intrinsic_w_chain_sdnode; 837 } 838 Record *get_intrinsic_wo_chain_sdnode() const { 839 return intrinsic_wo_chain_sdnode; 840 } 841 842 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); } 843 844 private: 845 void ParseNodeInfo(); 846 void ParseNodeTransforms(); 847 void ParseComplexPatterns(); 848 void ParsePatternFragments(bool OutFrags = false); 849 void ParseDefaultOperands(); 850 void ParseInstructions(); 851 void ParsePatterns(); 852 void InferInstructionFlags(); 853 void GenerateVariants(); 854 void VerifyInstructionFlags(); 855 856 void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM); 857 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat, 858 std::map<std::string, 859 TreePatternNode*> &InstInputs, 860 std::map<std::string, 861 TreePatternNode*> &InstResults, 862 std::vector<Record*> &InstImpResults); 863 }; 864 } // end namespace llvm 865 866 #endif 867