blob: fce31487033cd2ffd9ac2f4473fdbe1abbdec972 [file] [log] [blame]
Chris Lattnerfaca5ab2003-08-06 05:42:05 +00001//===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
2//
3// This tablegen backend is responsible for emitting a description of the target
4// instruction set for the code generator.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef INSTRSELECTOR_EMITTER_H
9#define INSTRSELECTOR_EMITTER_H
10
11#include "TableGenBackend.h"
Chris Lattnerbc659dd2003-08-07 06:02:15 +000012#include "CodeGenWrappers.h"
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000013#include <vector>
14#include <map>
Chris Lattner018c9e42003-08-07 05:40:14 +000015class DagInit;
16class Init;
Chris Lattnerf8e96832003-08-07 19:12:24 +000017class InstrSelectorEmitter;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000018
Chris Lattnerf8e96832003-08-07 19:12:24 +000019/// NodeType - Represents Information parsed from the DagNode entries.
20///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000021struct NodeType {
22 enum ArgResultTypes {
23 // Both argument and return types...
24 Val, // A non-void type
25 Arg0, // Value matches the type of Arg0
Chris Lattner2787d1a2003-08-06 06:16:35 +000026 Ptr, // Tree node is the type of the target pointer
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000027
28 // Return types
29 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000030 };
31
32 ArgResultTypes ResultType;
33 std::vector<ArgResultTypes> ArgTypes;
34
35 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
36 AT.swap(ArgTypes);
37 }
38
39 NodeType() : ResultType(Val) {}
40 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
41
42 static ArgResultTypes Translate(Record *R);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000043};
44
Chris Lattnerf8e96832003-08-07 19:12:24 +000045
46
47/// TreePatternNode - Represent a node of the tree patterns.
48///
Chris Lattner018c9e42003-08-07 05:40:14 +000049class TreePatternNode {
50 /// Operator - The operation that this node represents... this is null if this
51 /// is a leaf.
52 Record *Operator;
53
54 /// Type - The inferred value type...
Chris Lattnerf8e96832003-08-07 19:12:24 +000055 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000056 MVT::ValueType Type;
57
58 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
59 /// that we contain.
60 std::vector<TreePatternNode*> Children;
61
62 /// Value - If this node is a leaf, this indicates what the thing is.
Chris Lattnerf8e96832003-08-07 19:12:24 +000063 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000064 Init *Value;
65public:
66 TreePatternNode(Record *o, const std::vector<TreePatternNode*> &c)
67 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
68 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
69
Chris Lattner6dafd392003-08-08 16:30:10 +000070 Record *getOperator() const {
71 assert(Operator && "This is a leaf node!");
72 return Operator;
73 }
Chris Lattner018c9e42003-08-07 05:40:14 +000074 MVT::ValueType getType() const { return Type; }
75 void setType(MVT::ValueType T) { Type = T; }
76
77 bool isLeaf() const { return Operator == 0; }
78
79 const std::vector<TreePatternNode*> &getChildren() const {
80 assert(Operator != 0 && "This is a leaf node!");
81 return Children;
82 }
Chris Lattnerf8e96832003-08-07 19:12:24 +000083 TreePatternNode *getChild(unsigned c) const {
84 assert(c < Children.size() && "Child access out of range!");
85 return getChildren()[c];
86 }
87
Chris Lattner018c9e42003-08-07 05:40:14 +000088 Init *getValue() const {
89 assert(Operator == 0 && "This is not a leaf node!");
90 return Value;
91 }
92
Chris Lattneref0ce6a2003-08-07 23:16:20 +000093 /// clone - Make a copy of this tree and all of its children.
94 ///
95 TreePatternNode *clone() const;
96
Chris Lattner018c9e42003-08-07 05:40:14 +000097 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +000098
Chris Lattneref0ce6a2003-08-07 23:16:20 +000099 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
100 /// are not themselves completely resolved, clone the nonterminal and resolve
101 /// it with the using context we provide.
102 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000103
104 // UpdateNodeType - Set the node type of N to VT if VT contains information.
105 // If N already contains a conflicting type, then throw an exception. This
106 // returns true if any information was updated.
107 //
108 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000109};
110
111std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
112
113
114
Chris Lattnerf8e96832003-08-07 19:12:24 +0000115/// Pattern - Represent a pattern of one form or another. Currently, three
116/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
117///
118struct Pattern {
119 enum PatternType {
120 Nonterminal, Instruction, Expander
121 };
122private:
123 /// PTy - The type of pattern this is.
124 ///
125 PatternType PTy;
126
127 /// Tree - The tree pattern which corresponds to this pattern. Note that if
128 /// there was a (set) node on the outside level that it has been stripped off.
129 ///
130 TreePatternNode *Tree;
131
132 /// Result - If this is an instruction or expander pattern, this is the
133 /// register result, specified with a (set) in the pattern.
134 ///
135 Record *Result;
136
137 /// TheRecord - The actual TableGen record corresponding to this pattern.
138 ///
139 Record *TheRecord;
140
141 /// Resolved - This is true of the pattern is useful in practice. In
142 /// particular, some non-terminals will have non-resolvable types. When a
143 /// user of the non-terminal is later found, they will have inferred a type
144 /// for the result of the non-terminal, which cause a clone of an unresolved
145 /// nonterminal to be made which is "resolved".
146 ///
147 bool Resolved;
148
149 /// ISE - the instruction selector emitter coordinating this madness.
150 ///
151 InstrSelectorEmitter &ISE;
152public:
153
154 /// Pattern constructor - Parse the specified DagInitializer into the current
155 /// record.
156 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
157 InstrSelectorEmitter &ise);
158
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000159 /// Pattern - Constructor used for cloning nonterminal patterns
160 Pattern(TreePatternNode *tree, Record *rec, bool res,
161 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
162 TheRecord(rec), Resolved(res), ISE(ise){}
163
Chris Lattnerf8e96832003-08-07 19:12:24 +0000164 /// getPatternType - Return what flavor of Record this pattern originated from
165 ///
166 PatternType getPatternType() const { return PTy; }
167
168 /// getTree - Return the tree pattern which corresponds to this pattern.
169 ///
170 TreePatternNode *getTree() const { return Tree; }
171
172 Record *getResult() const { return Result; }
173
174 /// getRecord - Return the actual TableGen record corresponding to this
175 /// pattern.
176 ///
177 Record *getRecord() const { return TheRecord; }
178
179 bool isResolved() const { return Resolved; }
180
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000181 /// InferAllTypes - Runs the type inference engine on the current pattern,
182 /// stopping when nothing can be inferred, then updating the Resolved field.
183 void InferAllTypes();
184
185 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
186 /// are not themselves completely resolved, clone the nonterminal and resolve
187 /// it with the using context we provide.
188 void InstantiateNonterminals() {
189 Tree->InstantiateNonterminals(ISE);
190 }
191
192 /// clone - This method is used to make an exact copy of the current pattern,
193 /// then change the "TheRecord" instance variable to the specified record.
194 ///
195 Pattern *clone(Record *R) const;
196
197 /// error - Throw an exception, prefixing it with information about this
198 /// pattern.
199 void error(const std::string &Msg) const;
200
Chris Lattnerf8e96832003-08-07 19:12:24 +0000201private:
Chris Lattner5709e512003-08-07 21:02:56 +0000202 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000203 TreePatternNode *ParseTreePattern(DagInit *DI);
204 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000205};
206
207std::ostream &operator<<(std::ostream &OS, const Pattern &P);
208
209
Chris Lattner6dafd392003-08-08 16:30:10 +0000210/// PatternOrganizer - This class represents all of the patterns which are
211/// useful for the instruction selector, neatly catagorized in a hierarchical
212/// structure.
213struct PatternOrganizer {
214 /// PatternsForNode - The list of patterns which can produce a value of a
215 /// particular slot type, given a particular root node in the tree. All of
216 /// the patterns in this vector produce the same value type and have the same
217 /// root DAG node.
218 typedef std::vector<Pattern*> PatternsForNode;
219
220 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
221 /// lead to the production of a value for this slot. All of the patterns in
222 /// this data structure produces values of the same slot.
223 typedef std::map<Record*, PatternsForNode> NodesForSlot;
224
225 /// AllPatterns - This data structure contains all patterns in the instruction
226 /// selector.
227 std::map<std::string, NodesForSlot> AllPatterns;
228
229 // Forwarding functions...
230 typedef std::map<std::string, NodesForSlot>::iterator iterator;
231 iterator begin() { return AllPatterns.begin(); }
232 iterator end() { return AllPatterns.end(); }
233
234
235 /// addPattern - Add the specified pattern to the appropriate location in the
236 /// collection.
237 void addPattern(Pattern *P);
238};
239
Chris Lattnerf8e96832003-08-07 19:12:24 +0000240
241/// InstrSelectorEmitter - The top-level class which coordinates construction
242/// and emission of the instruction selector.
243///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000244class InstrSelectorEmitter : public TableGenBackend {
245 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000246 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000247
248 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000249
250 /// Patterns - a list of all of the patterns defined by the target description
251 ///
252 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000253
254 /// InstantiatedNTs - A data structure to keep track of which nonterminals
255 /// have been instantiated already...
256 ///
257 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000258
259 /// ComputableValues - This map indicates which patterns can be used to
260 /// generate a value that is used by the selector. The keys of this map
261 /// implicitly define the values that are used by the selector.
262 ///
263 PatternOrganizer ComputableValues;
264
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000265public:
266 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
267
268 // run - Output the instruction set description, returning true on failure.
269 void run(std::ostream &OS);
270
Chris Lattnerf8e96832003-08-07 19:12:24 +0000271 const CodeGenTarget &getTarget() const { return Target; }
272 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
273
Chris Lattner5709e512003-08-07 21:02:56 +0000274 /// getPattern - return the pattern corresponding to the specified record, or
275 /// null if there is none.
276 Pattern *getPattern(Record *R) const {
277 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
278 return I != Patterns.end() ? I->second : 0;
279 }
280
281 /// ReadNonterminal - This method parses the specified record as a
282 /// nonterminal, but only if it hasn't been read in already.
283 Pattern *ReadNonterminal(Record *R);
284
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000285 /// InstantiateNonterminal - This method takes the nonterminal specified by
286 /// NT, which should not be completely resolved, clones it, applies ResultTy
287 /// to its root, then runs the type inference stuff on it. This should
288 /// produce a newly resolved nonterminal, which we make a record for and
289 /// return. To be extra fancy and efficient, this only makes one clone for
290 /// each type it is instantiated with.
291 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
292
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000293private:
Chris Lattneree858d22003-08-07 20:42:23 +0000294 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
295 // turning them into the more accessible NodeTypes data structure.
296 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000297
Chris Lattneree858d22003-08-07 20:42:23 +0000298 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
299 // pattern database.
300 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000301
Chris Lattneree858d22003-08-07 20:42:23 +0000302 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000303 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000304 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000305
Chris Lattneree858d22003-08-07 20:42:23 +0000306 // ReadExpanderPatterns - Read in all of the expanded patterns.
307 void ReadExpanderPatterns();
308
309 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
310 // information from the context that they are used in.
311 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000312
313 // CalculateComputableValues - Fill in the ComputableValues map through
314 // analysis of the patterns we are playing with.
315 void CalculateComputableValues();
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000316};
317
318#endif