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