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