CodeGenDAGPatterns.h revision 48e86dbe29e331357b0df11075b7974009c65f34
1464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
2464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//
3464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//                     The LLVM Compiler Infrastructure
4464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//
5464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// This file is distributed under the University of Illinois Open Source
6464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// License. See LICENSE.TXT for details.
7464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//
8464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//===----------------------------------------------------------------------===//
9464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//
10464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// This file declares the CodeGenDAGPatterns class, which is used to read and
11464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com// represent the patterns present in a .td file for instructions.
12464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//
13464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com//===----------------------------------------------------------------------===//
14464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
15464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#ifndef CODEGEN_DAGPATTERNS_H
16464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#define CODEGEN_DAGPATTERNS_H
1779fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com
1879fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com#include "CodeGenTarget.h"
1979fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com#include "CodeGenIntrinsics.h"
20ba91573e890c0871c6949e480a365bc85d408e77dfilimon@google.com#include "llvm/ADT/SmallVector.h"
2179fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com#include "llvm/ADT/StringMap.h"
22464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com#include <set>
23bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com#include <algorithm>
24bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com#include <vector>
25bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com#include <map>
26bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
276b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.comnamespace llvm {
28464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class Record;
29bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  struct Init;
30464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class ListInit;
31bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  class DagInit;
32bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  class SDNodeInfo;
33464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class TreePattern;
34464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class TreePatternNode;
35464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class CodeGenDAGPatterns;
36464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  class ComplexPattern;
37464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
38bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// EEVT::DAGISelGenValueType - These are some extended forms of
39bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// MVT::SimpleValueType that we use as lattice values during type inference.
40bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// The existing MVT iAny, fAny and vAny types suffice to represent
41bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// arbitrary integer, floating-point, and vector types, so only an unknown
42bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// value is needed.
43bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.comnamespace EEVT {
44bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// TypeSet - This is either empty if it's completely unknown, or holds a set
45bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// of types.  It is used during type inference because register classes can
46bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// have multiple possible types and we don't know which one they get until
47bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// type inference is complete.
48bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
49bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// TypeSet can have three states:
50bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///    Vector is empty: The type is completely unknown, it can be any valid
51464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ///       target type.
52464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ///    Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
53464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ///       of those types only.
54bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///    Vector has one concrete type: The type is completely known.
55bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
56bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  class TypeSet {
57bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    SmallVector<MVT::SimpleValueType, 4> TypeVec;
58464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  public:
59464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    TypeSet() {}
60bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
61bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    TypeSet(const std::vector<MVT::SimpleValueType> &VTList);
62bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
63bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool isCompletelyUnknown() const { return TypeVec.empty(); }
64bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
65bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool isConcrete() const {
66bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      if (TypeVec.size() != 1) return false;
67bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      unsigned char T = TypeVec[0]; (void)T;
68bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
69bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      return true;
70bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    }
71bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
72bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    MVT::SimpleValueType getConcrete() const {
73bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      assert(isConcrete() && "Type isn't concrete yet");
74bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      return (MVT::SimpleValueType)TypeVec[0];
75bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    }
76bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
77bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool isDynamicallyResolved() const {
78bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
79bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    }
80bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
8179fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com    const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
82bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      assert(!TypeVec.empty() && "Not a type list!");
83bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      return TypeVec;
84bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    }
85bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
86bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool isVoid() const {
87bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
88bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    }
89bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
90bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// hasIntegerTypes - Return true if this TypeSet contains any integer value
91abb68accf47583a130eba5578baa89b4257e5679dfilimon@google.com    /// types.
92abb68accf47583a130eba5578baa89b4257e5679dfilimon@google.com    bool hasIntegerTypes() const;
93bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
94bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
95bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// a floating point value type.
96bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool hasFloatingPointTypes() const;
97bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
98bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// hasVectorTypes - Return true if this TypeSet contains a vector value
99bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// type.
100bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool hasVectorTypes() const;
101bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
102bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// getName() - Return this TypeSet as a string.
103bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    std::string getName() const;
104bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
105bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// MergeInTypeInfo - This merges in type information from the specified
106246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// argument.  If 'this' changes, it returns true.  If the two types are
107246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// contradictory (e.g. merge f32 into i32) then this throws an exception.
108464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
109464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
110246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
111246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
112464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    }
113464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
114246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// Force this type list to only contain integer types.
115bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool EnforceInteger(TreePattern &TP);
116464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
117464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    /// Force this type list to only contain floating point types.
118246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool EnforceFloatingPoint(TreePattern &TP);
119246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
120246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// EnforceScalar - Remove all vector types from this type list.
121464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool EnforceScalar(TreePattern &TP);
122464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
123246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// EnforceVector - Remove all non-vector types from this type list.
124246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool EnforceVector(TreePattern &TP);
125246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
126464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    /// EnforceSmallerThan - 'this' must be a smaller VT than Other.  Update
127464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    /// this an other based on this information.
128246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
129246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
130246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
131464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    /// whose element is VT.
132464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
133246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
134246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
135464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
136464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
137bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  private:
138bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// FillWithPossibleTypes - Set to all legal types and return true, only
139bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// valid on completely unknown type sets.  If Pred is non-null, only MVTs
140bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    /// that pass the predicate are added.
141bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    bool FillWithPossibleTypes(TreePattern &TP,
142bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                               bool (*Pred)(MVT::SimpleValueType) = 0,
143464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                               const char *PredicateName = 0);
144464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  };
145bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com}
146bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
147bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// Set type used to track multiply used variables in patterns
148bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.comtypedef std::set<std::string> MultipleUseVarSet;
149bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
150464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/// SDTypeConstraint - This is a discriminated union of constraints,
151464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/// corresponding to the SDTypeConstraint tablegen class in Target.td.
152bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.comstruct SDTypeConstraint {
153bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  SDTypeConstraint(Record *R);
154bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
155bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  unsigned OperandNo;   // The operand # this constraint applies to.
156bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  enum {
157bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
158464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec
159464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  } ConstraintType;
160bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
161bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  union {   // The discriminated union.
162bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    struct {
163bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      MVT::SimpleValueType VT;
164bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    } SDTCisVT_Info;
165bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    struct {
166bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      unsigned OtherOperandNum;
167bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    } SDTCisSameAs_Info;
168bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    struct {
169bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      unsigned OtherOperandNum;
170bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    } SDTCisVTSmallerThanOp_Info;
171bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    struct {
172bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      unsigned BigOperandNum;
173bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    } SDTCisOpSmallerThanOp_Info;
174464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    struct {
175464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com      unsigned OtherOperandNum;
176bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    } SDTCisEltOfVec_Info;
177bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  } x;
178bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
179bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// ApplyTypeConstraint - Given a node in a pattern, apply this type
180bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// constraint to the nodes operands.  This returns true if it makes a
181bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// change, false otherwise.  If a type contradiction is found, throw an
182bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// exception.
183bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
184bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                           TreePattern &TP) const;
185bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com};
186bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
187bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// SDNodeInfo - One of these records is created for each SDNode instance in
188bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// the target .td file.  This represents the various dag nodes we will be
189bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// processing.
190bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.comclass SDNodeInfo {
191bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  Record *Def;
192bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::string EnumName;
193bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::string SDClassName;
194464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned Properties;
195464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned NumResults;
196bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  int NumOperands;
197bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::vector<SDTypeConstraint> TypeConstraints;
198bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.compublic:
199464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  SDNodeInfo(Record *R);  // Parse the specified record.
200464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
201464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned getNumResults() const { return NumResults; }
202464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
203464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// getNumOperands - This is the number of operands required or -1 if
204464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// variadic.
205bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  int getNumOperands() const { return NumOperands; }
206bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  Record *getRecord() const { return Def; }
207bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const std::string &getEnumName() const { return EnumName; }
208bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const std::string &getSDClassName() const { return SDClassName; }
209bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
210bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const std::vector<SDTypeConstraint> &getTypeConstraints() const {
211bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return TypeConstraints;
212bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
213bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
214bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// getKnownType - If the type constraints on this node imply a fixed type
215bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// (e.g. all stores return void, etc), then return it as an
216bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// MVT::SimpleValueType.  Otherwise, return MVT::Other.
217bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  MVT::SimpleValueType getKnownType(unsigned ResNo) const;
218bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
219bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// hasProperty - Return true if this node has the specified property.
220bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
221bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
222bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
223bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// ApplyTypeConstraints - Given a node in a pattern, apply the type
224bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// constraints for this node to the operands of the node.  This returns
225bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// true if it makes a change, false otherwise.  If a type contradiction is
226bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// found, throw an exception.
227bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
228464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    bool MadeChange = false;
229464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
230464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com      MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
231464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    return MadeChange;
232464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
233464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
234464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
235464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
236464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/// patterns), and as such should be ref counted.  We currently just leak all
237464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com/// TreePatternNode objects!
238246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comclass TreePatternNode {
239246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// The type of each node result.  Before and during type inference, each
240464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// result may be a set of possible types.  After (successful) type inference,
241464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// each is a single concrete type.
242464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  SmallVector<EEVT::TypeSet, 1> Types;
243464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
244246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// Operator - The Record for the operator if this is an interior node (not
245246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// a leaf).
246246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Record *Operator;
247246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
248bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
249b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  ///
250246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Init *Val;
251246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
252246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// Name - The name given to this node with the :$foo notation.
253246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ///
254246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::string Name;
255bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
256246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// PredicateFns - The predicate functions to execute on this node to check
257246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// for a match.  If this list is empty, no predicate is involved.
258bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::vector<std::string> PredicateFns;
259bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
260bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// TransformFn - The transformation function to execute on this node before
261bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// it can be substituted into the resulting instruction on a pattern match.
262bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  Record *TransformFn;
263bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
264a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::vector<TreePatternNode*> Children;
265246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.compublic:
266246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
267bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                  unsigned NumResults)
268bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    : Operator(Op), Val(0), TransformFn(0), Children(Ch) {
269bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    Types.resize(NumResults);
270bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
271bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  TreePatternNode(Init *val, unsigned NumResults)    // leaf ctor
272bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    : Operator(0), Val(val), TransformFn(0) {
273bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    Types.resize(NumResults);
274bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
275bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ~TreePatternNode();
276bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
277bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const std::string &getName() const { return Name; }
278bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
279bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
280bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool isLeaf() const { return Val != 0; }
281bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
282bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  // Type accessors.
283bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  unsigned getNumTypes() const { return Types.size(); }
284bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  MVT::SimpleValueType getType(unsigned ResNo) const {
285bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return Types[ResNo].getConcrete();
286bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
287bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
288bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
289a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
290bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
291bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
292bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool hasTypeSet(unsigned ResNo) const {
293bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return Types[ResNo].isConcrete();
294bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
295246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool isTypeCompletelyUnknown(unsigned ResNo) const {
296246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    return Types[ResNo].isCompletelyUnknown();
297246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  }
298246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool isTypeDynamicallyResolved(unsigned ResNo) const {
299246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    return Types[ResNo].isDynamicallyResolved();
300b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  }
301b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
3026b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com  Init *getLeafValue() const { assert(isLeaf()); return Val; }
303b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  Record *getOperator() const { assert(!isLeaf()); return Operator; }
304246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
305bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  unsigned getNumChildren() const { return Children.size(); }
306bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  TreePatternNode *getChild(unsigned N) const { return Children[N]; }
307464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void setChild(unsigned i, TreePatternNode *N) {
308464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    Children[i] = N;
309b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  }
310b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
3116b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com  /// hasChild - Return true if N is any of our children.
312b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  bool hasChild(const TreePatternNode *N) const {
313246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    for (unsigned i = 0, e = Children.size(); i != e; ++i)
314bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      if (Children[i] == N) return true;
315bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return false;
316464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
317464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
318246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const std::vector<std::string> &getPredicateFns() const {return PredicateFns;}
319246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void clearPredicateFns() { PredicateFns.clear(); }
320464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void setPredicateFns(const std::vector<std::string> &Fns) {
321464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
322246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    PredicateFns = Fns;
323464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
324464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void addPredicateFn(const std::string &Fn) {
325464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    assert(!Fn.empty() && "Empty predicate string!");
326246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
327bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com          PredicateFns.end())
328bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      PredicateFns.push_back(Fn);
329bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
330bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
331464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  Record *getTransformFn() const { return TransformFn; }
332464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void setTransformFn(Record *Fn) { TransformFn = Fn; }
333246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
334464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
335464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// CodeGenIntrinsic information for it, otherwise return a null pointer.
336464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
337464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
338246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
339464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// return the ComplexPattern information, otherwise return null.
340246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const ComplexPattern *
341464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
342464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
343246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// NodeHasProperty - Return true if this node has the specified property.
344246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
345246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
346464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// TreeHasProperty - Return true if any node in this tree has the specified
347246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// property.
348464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
349464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
350464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
351bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// marked isCommutative.
352464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
353464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
354246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void print(raw_ostream &OS) const;
355246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void dump() const;
356246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
357464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.compublic:   // Higher level manipulation routines.
358464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
35979fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com  /// clone - Return a new copy of this tree.
36079fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com  ///
361bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  TreePatternNode *clone() const;
362bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
363bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// RemoveAllTypes - Recursively strip all the types of this tree.
364bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  void RemoveAllTypes();
365bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
366bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// isIsomorphicTo - Return true if this node is recursively isomorphic to
367bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// the specified node.  For this comparison, all of the state of the node
368bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// is considered, except for the assigned name.  Nodes with differing names
369bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// that are otherwise identical are considered isomorphic.
370bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool isIsomorphicTo(const TreePatternNode *N,
371bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                      const MultipleUseVarSet &DepVars) const;
372bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
373bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// SubstituteFormalArguments - Replace the formal arguments in this tree
374bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// with actual values specified by ArgMap.
375bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  void SubstituteFormalArguments(std::map<std::string,
376bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                                          TreePatternNode*> &ArgMap);
377bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
378bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// InlinePatternFragments - If this pattern refers to any pattern
379bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// fragments, inline them into place, giving us a pattern without any
380bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// PatFrag references.
381bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  TreePatternNode *InlinePatternFragments(TreePattern &TP);
382bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
383bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// ApplyTypeConstraints - Apply all of the type constraints relevant to
384bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// this node and its children in the tree.  This returns true if it makes a
385bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// change, false otherwise.  If a type contradiction is found, throw an
386bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// exception.
387bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
388246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
389246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// UpdateNodeType - Set the node type of N to VT if VT contains
390246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// information.  If N already contains a conflicting type, then throw an
391bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// exception.  This returns true if any information was updated.
392bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
393bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
394bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                      TreePattern &TP) {
395bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return Types[ResNo].MergeInTypeInfo(InTy, TP);
396bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
397bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
398bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
399bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com                      TreePattern &TP) {
400bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
401bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
402bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
403bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// ContainsUnresolvedType - Return true if this tree contains any
404bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// unresolved types.
405bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool ContainsUnresolvedType() const {
406bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    for (unsigned i = 0, e = Types.size(); i != e; ++i)
407bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      if (!Types[i].isConcrete()) return true;
408bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
409bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
410bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com      if (getChild(i)->ContainsUnresolvedType()) return true;
411bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com    return false;
412bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  }
413bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
414bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// canPatternMatch - If it is impossible for this pattern to match on this
415bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// target, fill in Reason and return false.  Otherwise, return true.
416bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
417464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com};
418464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
419bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.cominline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
420bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  TPN.print(OS);
421bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  return OS;
422bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com}
423bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
424bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
425bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// TreePattern - Represent a pattern, used for instructions, pattern
426bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com/// fragments, etc.
427bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com///
428b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.comclass TreePattern {
429bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// Trees - The list of pattern trees which corresponds to this pattern.
430bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// Note that PatFrag's only have a single tree.
431bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
432bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::vector<TreePatternNode*> Trees;
433bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
434bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// NamedNodes - This is all of the nodes that have names in the trees in this
435bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// pattern.
436bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
437bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com
438bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// TheRecord - The actual TableGen record corresponding to this pattern.
439bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  ///
440bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  Record *TheRecord;
44179fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com
442bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// Args - This is a list of all of the arguments to this pattern (for
443bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  /// PatFrag patterns), which are the 'node' markers in this pattern.
444bb035e4061737ef1ac7ee3e1263c443b55c123e9dfilimon@google.com  std::vector<std::string> Args;
445464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
446464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// CDP - the top-level object coordinating this madness.
447246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ///
448246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  CodeGenDAGPatterns &CDP;
449464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
450246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// isInputPattern - True if this is an input pattern, something to match.
451246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// False if this is an output pattern, something to emit.
452246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  bool isInputPattern;
453246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.compublic:
454464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
455464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// TreePattern constructor - Parse the specified DagInits into the
456464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// current record.
457246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
458464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com              CodeGenDAGPatterns &ise);
459246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
460464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com              CodeGenDAGPatterns &ise);
461464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
462246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com              CodeGenDAGPatterns &ise);
463246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
464464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// getTrees - Return the tree patterns which corresponds to this pattern.
465464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  ///
466246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
467464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned getNumTrees() const { return Trees.size(); }
468464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
469464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  TreePatternNode *getOnlyTree() const {
470464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
471246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    return Trees[0];
472464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
473464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
474246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
475464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    if (NamedNodes.empty())
476464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com      ComputeNamedNodes();
477464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    return NamedNodes;
478464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
479464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
480464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// getRecord - Return the actual TableGen record corresponding to this
481246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// pattern.
482246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ///
483464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  Record *getRecord() const { return TheRecord; }
484464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
485464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned getNumArgs() const { return Args.size(); }
486464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  const std::string &getArgName(unsigned i) const {
487464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    assert(i < Args.size() && "Argument reference out of range!");
488246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    return Args[i];
489464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
490464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  std::vector<std::string> &getArgList() { return Args; }
491464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
492464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
4936b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com
4946b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com  /// InlinePatternFragments - If this pattern refers to any pattern
495246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// fragments, inline them into place, giving us a pattern without any
496464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// PatFrag references.
497464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void InlinePatternFragments() {
498464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    for (unsigned i = 0, e = Trees.size(); i != e; ++i)
499464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com      Trees[i] = Trees[i]->InlinePatternFragments(*this);
500464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  }
501464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
502464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// InferAllTypes - Infer/propagate as many types throughout the expression
503246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// patterns as possible.  Return true if all types are inferred, false
504464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  /// otherwise.  Throw an exception if a type contradiction is found.
505464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
506464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                          *NamedTypes=0);
507246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
508246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// error - Throw an exception, prefixing it with information about this
509246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  /// pattern.
510246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void error(const std::string &Msg) const;
511246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
512246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void print(raw_ostream &OS) const;
513246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void dump() const;
514246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
515246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comprivate:
516246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
517246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void ComputeNamedNodes();
518246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  void ComputeNamedNodes(TreePatternNode *N);
519246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com};
520246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
521246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com/// DAGDefaultOperand - One of these is created for each PredicateOperand
522246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
523246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comstruct DAGDefaultOperand {
524246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::vector<TreePatternNode*> DefaultOps;
525246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com};
526246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
527246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.comclass DAGInstruction {
528246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePattern *Pattern;
529246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::vector<Record*> Results;
530246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::vector<Record*> Operands;
531246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::vector<Record*> ImpResults;
532246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  std::vector<Record*> ImpOperands;
533246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePatternNode *ResultPattern;
534246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.compublic:
535246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  DAGInstruction(TreePattern *TP,
536246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 const std::vector<Record*> &results,
537246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 const std::vector<Record*> &operands,
538246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 const std::vector<Record*> &impresults,
539246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 const std::vector<Record*> &impoperands)
540246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    : Pattern(TP), Results(results), Operands(operands),
541246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      ImpResults(impresults), ImpOperands(impoperands),
542246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      ResultPattern(0) {}
543246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
544246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const TreePattern *getPattern() const { return Pattern; }
545246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  unsigned getNumResults() const { return Results.size(); }
546246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  unsigned getNumOperands() const { return Operands.size(); }
547246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  unsigned getNumImpResults() const { return ImpResults.size(); }
548246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  unsigned getNumImpOperands() const { return ImpOperands.size(); }
549246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  const std::vector<Record*>& getImpResults() const { return ImpResults; }
550464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
551464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
55279fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com
55379fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com  Record *getResult(unsigned RN) const {
55479fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com    assert(RN < Results.size());
55579fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com    return Results[RN];
55679fe62d80f2ab09a217c5948a9adbcd019f4455ddfilimon@google.com  }
557246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
558246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  Record *getOperand(unsigned ON) const {
559246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    assert(ON < Operands.size());
560b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com    return Operands[ON];
561b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  }
562b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
563b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  Record *getImpResult(unsigned RN) const {
564246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com    assert(RN < ImpResults.size());
5656b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com    return ImpResults[RN];
5666b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com  }
567464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com
568464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  Record *getImpOperand(unsigned ON) const {
569464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    assert(ON < ImpOperands.size());
570464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    return ImpOperands[ON];
571b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  }
572b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com
573b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com  TreePatternNode *getResultPattern() const { return ResultPattern; }
574b54cce09c1fc2b09e2adae43d7eb017b47b0cccfarthurhsu@google.com};
575246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
5766b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
5776b5ae74df6b2ff199c63923219c9a18bd9c4d931arthurhsu@google.com/// processed to produce isel.
578464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.comclass PatternToMatch {
579464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.compublic:
580464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  PatternToMatch(ListInit *preds,
581464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com                 TreePatternNode *src, TreePatternNode *dst,
582246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 const std::vector<Record*> &dstregs,
583246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com                 unsigned complexity, unsigned uid)
584464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com    : Predicates(preds), SrcPattern(src), DstPattern(dst),
585246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com      Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
586246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com
587246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  ListInit        *Predicates;  // Top level predicate conditions to match.
588246300f7fab1f2539c3207ce5ec28cc355465be8arthurhsu@google.com  TreePatternNode *SrcPattern;  // Source pattern to match.
589464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  TreePatternNode *DstPattern;  // Resulting pattern.
590464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  std::vector<Record*> Dstregs; // Physical register defs being matched.
591464987db923362e596195f9eebd34fc508c9a41arthurhsu@google.com  unsigned         AddedComplexity; // Add to matching pattern complexity.
592a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  unsigned         ID;          // Unique ID for the record.
593a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
594a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  ListInit        *getPredicates() const { return Predicates; }
595a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  TreePatternNode *getSrcPattern() const { return SrcPattern; }
596a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  TreePatternNode *getDstPattern() const { return DstPattern; }
597a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const std::vector<Record*> &getDstRegs() const { return Dstregs; }
598a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  unsigned         getAddedComplexity() const { return AddedComplexity; }
599a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
600a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::string getPredicateCheck() const;
601a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
602a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  /// Compute the complexity metric for the input pattern.  This roughly
603a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  /// corresponds to the number of nodes that are covered.
604a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
605a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com};
606a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
607a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com// Deterministic comparison of Record*.
608a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.comstruct RecordPtrCmp {
609a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  bool operator()(const Record *LHS, const Record *RHS) const;
610a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com};
611a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
612a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.comclass CodeGenDAGPatterns {
613a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  RecordKeeper &Records;
614a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  CodeGenTarget Target;
615a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::vector<CodeGenIntrinsic> Intrinsics;
616a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::vector<CodeGenIntrinsic> TgtIntrinsics;
617a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
618a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
619a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
620a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
621a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
622a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
623a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
624a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
625a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  // Specific SDNode definitions:
626a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *intrinsic_void_sdnode;
627a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
628a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
629a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  /// PatternsToMatch - All of the things we are matching on the DAG.  The first
630a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  /// value is the pattern to match, the second pattern is the result to
631a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  /// emit.
632a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  std::vector<PatternToMatch> PatternsToMatch;
633a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.compublic:
634a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  CodeGenDAGPatterns(RecordKeeper &R);
635a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  ~CodeGenDAGPatterns();
636a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
637a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  CodeGenTarget &getTargetInfo() { return Target; }
638a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const CodeGenTarget &getTargetInfo() const { return Target; }
639a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
640a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *getSDNodeNamed(const std::string &Name) const;
641a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
642a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const SDNodeInfo &getSDNodeInfo(Record *R) const {
643a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(SDNodes.count(R) && "Unknown node!");
644a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return SDNodes.find(R)->second;
645a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
646a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
647a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  // Node transformation lookups.
648a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  typedef std::pair<Record*, std::string> NodeXForm;
649a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const NodeXForm &getSDNodeTransform(Record *R) const {
650a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(SDNodeXForms.count(R) && "Invalid transform!");
651a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return SDNodeXForms.find(R)->second;
652a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
653a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
654a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
655a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com          nx_iterator;
656a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
657a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  nx_iterator nx_end() const { return SDNodeXForms.end(); }
658a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
659a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
660a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const ComplexPattern &getComplexPattern(Record *R) const {
661a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
662a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return ComplexPatterns.find(R)->second;
663a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
664a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
665a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const CodeGenIntrinsic &getIntrinsic(Record *R) const {
666a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
667a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      if (Intrinsics[i].TheDef == R) return Intrinsics[i];
668a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
669a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
670a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(0 && "Unknown intrinsic!");
671a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    abort();
672a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
673a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
674a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
675a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    if (IID-1 < Intrinsics.size())
676a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      return Intrinsics[IID-1];
677a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
678a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      return TgtIntrinsics[IID-Intrinsics.size()-1];
679a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(0 && "Bad intrinsic ID!");
680a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    abort();
681a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
682a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
683a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  unsigned getIntrinsicID(Record *R) const {
684a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
685a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      if (Intrinsics[i].TheDef == R) return i;
686a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
687a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com      if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
688a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(0 && "Unknown intrinsic!");
689a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    abort();
690a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
691a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
692a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const DAGDefaultOperand &getDefaultOperand(Record *R) const {
693a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
694a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return DefaultOperands.find(R)->second;
695a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
696a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
697a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  // Pattern Fragment information.
698a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  TreePattern *getPatternFragment(Record *R) const {
699a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
700a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return PatternFragments.find(R)->second;
701a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
702a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  TreePattern *getPatternFragmentIfRead(Record *R) const {
703a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    if (!PatternFragments.count(R)) return 0;
704a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return PatternFragments.find(R)->second;
705a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
706a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
707a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
708a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com          pf_iterator;
709a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  pf_iterator pf_begin() const { return PatternFragments.begin(); }
710a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  pf_iterator pf_end() const { return PatternFragments.end(); }
711a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
712a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  // Patterns to match information.
713a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
714a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
715a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
716a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
717a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
718a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
719a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  const DAGInstruction &getInstruction(Record *R) const {
720a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    assert(Instructions.count(R) && "Unknown instruction!");
721a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return Instructions.find(R)->second;
722a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
723a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
724a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *get_intrinsic_void_sdnode() const {
725a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return intrinsic_void_sdnode;
726a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
727a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *get_intrinsic_w_chain_sdnode() const {
728a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return intrinsic_w_chain_sdnode;
729a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
730a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  Record *get_intrinsic_wo_chain_sdnode() const {
731a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com    return intrinsic_wo_chain_sdnode;
732a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  }
733a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
734a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
735a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
736a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.comprivate:
737a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParseNodeInfo();
738a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParseNodeTransforms();
739a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParseComplexPatterns();
740a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParsePatternFragments();
741a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParseDefaultOperands();
742a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParseInstructions();
743a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void ParsePatterns();
744a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void InferInstructionFlags();
745a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void GenerateVariants();
746a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
747a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
748a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com  void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
749a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   std::map<std::string,
750a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   TreePatternNode*> &InstInputs,
751a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   std::map<std::string,
752a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   TreePatternNode*> &InstResults,
753a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   std::vector<Record*> &InstImpInputs,
754a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com                                   std::vector<Record*> &InstImpResults);
755a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com};
756a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com} // end namespace llvm
757a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com
758a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com#endif
759a8be98eb7c7b56644732b866346cf8b852592170dfilimon@google.com