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