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