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