e2fade9917130d8aaf9213f077a91140c6cd1f76
[oota-llvm.git] / utils / TableGen / DAGISelEmitter.h
1 //===- DAGISelEmitter.h - Generate an instruction selector ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This tablegen backend emits a DAG instruction selector.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef DAGISEL_EMITTER_H
15 #define DAGISEL_EMITTER_H
16
17 #include "TableGenBackend.h"
18 #include "CodeGenTarget.h"
19
20 namespace llvm {
21   class Record;
22   class Init;
23   class DagInit;
24   class TreePattern;
25   class DAGISelEmitter;
26   
27   /// SDTypeConstraint - This is a discriminated union of constraints,
28   /// corresponding to the SDTypeConstraint tablegen class in Target.td.
29   struct SDTypeConstraint {
30     SDTypeConstraint(Record *R);
31     
32     unsigned OperandNo;   // The operand # this constraint applies to.
33     enum { 
34       SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
35     } ConstraintType;
36     
37     union {   // The discriminated union.
38       struct {
39         MVT::ValueType VT;
40       } SDTCisVT_Info;
41       struct {
42         unsigned OtherOperandNum;
43       } SDTCisSameAs_Info;
44       struct {
45         unsigned OtherOperandNum;
46       } SDTCisVTSmallerThanOp_Info;
47     } x;
48   };
49   
50   /// SDNodeInfo - One of these records is created for each SDNode instance in
51   /// the target .td file.  This represents the various dag nodes we will be
52   /// processing.
53   class SDNodeInfo {
54     Record *Def;
55     std::string EnumName;
56     std::string SDClassName;
57     int NumResults, NumOperands;
58     std::vector<SDTypeConstraint> TypeConstraints;
59   public:
60     SDNodeInfo(Record *R);  // Parse the specified record.
61     
62     int getNumResults() const { return NumResults; }
63     int getNumOperands() const { return NumOperands; }
64     Record *getRecord() const { return Def; }
65     const std::string &getEnumName() const { return EnumName; }
66     const std::string &getSDClassName() const { return SDClassName; }
67     
68     const std::vector<SDTypeConstraint> &getTypeConstraints() {
69       return TypeConstraints;
70     }
71   };
72
73   /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
74   /// patterns), and as such should be ref counted.  We currently just leak all
75   /// TreePatternNode objects!
76   class TreePatternNode {
77     /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
78     /// been determined yet.
79     MVT::ValueType Ty;
80
81     /// Operator - The Record for the operator if this is an interior node (not
82     /// a leaf).
83     Record *Operator;
84     
85     /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
86     ///
87     Init *Val;
88     
89     /// Name - The name given to this node with the :$foo notation.
90     ///
91     std::string Name;
92     
93     /// PredicateFn - The predicate function to execute on this node to check
94     /// for a match.  If this string is empty, no predicate is involved.
95     std::string PredicateFn;
96     
97     std::vector<TreePatternNode*> Children;
98   public:
99     TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch) 
100       : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
101     TreePatternNode(Init *val)    // leaf ctor
102       : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
103     ~TreePatternNode();
104     
105     const std::string &getName() const { return Name; }
106     void setName(const std::string &N) { Name = N; }
107     
108     bool isLeaf() const { return Val != 0; }
109     MVT::ValueType getType() const { return Ty; }
110     void setType(MVT::ValueType VT) { Ty = VT; }
111     
112     Init *getLeafValue() const { assert(isLeaf()); return Val; }
113     Record *getOperator() const { assert(!isLeaf()); return Operator; }
114     
115     unsigned getNumChildren() const { return Children.size(); }
116     TreePatternNode *getChild(unsigned N) const { return Children[N]; }
117     void setChild(unsigned i, TreePatternNode *N) {
118       Children[i] = N;
119     }
120     
121     const std::string &getPredicateFn() const { return PredicateFn; }
122     void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
123     
124     void print(std::ostream &OS) const;
125     void dump() const;
126     
127   public:   // Higher level manipulation routines.
128
129     /// clone - Return a new copy of this tree.
130     ///
131     TreePatternNode *clone() const;
132     
133     void SubstituteFormalArguments(std::map<std::string,
134                                             TreePatternNode*> &ArgMap);
135
136     /// InlinePatternFragments - If this pattern refers to any pattern
137     /// fragments, inline them into place, giving us a pattern without any
138     /// PatFrag references.
139     TreePatternNode *InlinePatternFragments(TreePattern &TP);
140         
141   };
142   
143   
144   /// TreePattern - Represent a pattern of one form or another.  Currently, two
145   /// types of patterns are possible: Instructions and PatFrags.
146   ///
147   class TreePattern {
148   public:
149     enum PatternType {
150       PatFrag, Instruction
151     };
152   private:
153     /// PTy - The type of pattern this is.
154     ///
155     PatternType PTy;
156     
157     /// Trees - The list of pattern trees which corresponds to this pattern.
158     /// Note that PatFrag's only have a single tree.
159     ///
160     std::vector<TreePatternNode*> Trees;
161     
162     /// TheRecord - The actual TableGen record corresponding to this pattern.
163     ///
164     Record *TheRecord;
165       
166     /// Args - This is a list of all of the arguments to this pattern (for
167     /// PatFrag patterns), which are the 'node' markers in this pattern.
168     std::vector<std::string> Args;
169     
170     /// ISE - the DAG isel emitter coordinating this madness.
171     ///
172     DAGISelEmitter &ISE;
173   public:
174       
175     /// TreePattern constructor - Parse the specified DagInits into the
176     /// current record.
177     TreePattern(PatternType pty, Record *TheRec,
178                 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
179         
180     /// getPatternType - Return what flavor of Record this pattern originated from
181     ///
182     PatternType getPatternType() const { return PTy; }
183     
184     /// getTrees - Return the tree patterns which corresponds to this pattern.
185     ///
186     const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
187         
188     /// getRecord - Return the actual TableGen record corresponding to this
189     /// pattern.
190     ///
191     Record *getRecord() const { return TheRecord; }
192     
193     unsigned getNumArgs() const { return Args.size(); }
194     const std::string &getArgName(unsigned i) const {
195       assert(i < Args.size() && "Argument reference out of range!");
196       return Args[i];
197     }
198     
199     DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
200
201     /// InlinePatternFragments - If this pattern refers to any pattern
202     /// fragments, inline them into place, giving us a pattern without any
203     /// PatFrag references.
204     void InlinePatternFragments() {
205       for (unsigned i = 0, e = Trees.size(); i != e; ++i)
206         Trees[i] = Trees[i]->InlinePatternFragments(*this);
207     }
208     
209     /// error - Throw an exception, prefixing it with information about this
210     /// pattern.
211     void error(const std::string &Msg) const;
212     
213     void print(std::ostream &OS) const;
214     void dump() const;
215     
216   private:
217     MVT::ValueType getIntrinsicType(Record *R) const;
218     TreePatternNode *ParseTreePattern(DagInit *DI);
219   };
220   
221   
222   
223 /// InstrSelectorEmitter - The top-level class which coordinates construction
224 /// and emission of the instruction selector.
225 ///
226 class DAGISelEmitter : public TableGenBackend {
227   RecordKeeper &Records;
228   CodeGenTarget Target;
229
230   std::map<Record*, SDNodeInfo> SDNodes;
231   std::map<Record*, TreePattern*> PatternFragments;
232   std::vector<TreePattern*> Instructions;
233 public:
234   DAGISelEmitter(RecordKeeper &R) : Records(R) {}
235
236   // run - Output the isel, returning true on failure.
237   void run(std::ostream &OS);
238   
239   const SDNodeInfo &getSDNodeInfo(Record *R) const {
240     assert(SDNodes.count(R) && "Unknown node!");
241     return SDNodes.find(R)->second;
242   }
243
244   TreePattern *getPatternFragment(Record *R) const {
245     assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
246     return PatternFragments.find(R)->second;
247   }
248   
249 private:
250   void ParseNodeInfo();
251   void ParseAndResolvePatternFragments(std::ostream &OS);
252   void ParseAndResolveInstructions();
253   void EmitInstructionSelector(std::ostream &OS);
254 };
255
256 } // End llvm namespace
257
258 #endif