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