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