blob: a8ab145c87c8de8843761d782602270a6add71d6 [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 Lattner88118bf2003-08-11 20:25:52 +000026 Arg1, // Value matches the type of Arg1
Chris Lattner2787d1a2003-08-06 06:16:35 +000027 Ptr, // Tree node is the type of the target pointer
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000028
29 // Return types
30 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000031 };
32
33 ArgResultTypes ResultType;
34 std::vector<ArgResultTypes> ArgTypes;
35
36 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
37 AT.swap(ArgTypes);
38 }
39
40 NodeType() : ResultType(Val) {}
41 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
42
43 static ArgResultTypes Translate(Record *R);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000044};
45
Chris Lattnerf8e96832003-08-07 19:12:24 +000046
47
48/// TreePatternNode - Represent a node of the tree patterns.
49///
Chris Lattner018c9e42003-08-07 05:40:14 +000050class TreePatternNode {
51 /// Operator - The operation that this node represents... this is null if this
52 /// is a leaf.
53 Record *Operator;
54
55 /// Type - The inferred value type...
Chris Lattnerf8e96832003-08-07 19:12:24 +000056 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000057 MVT::ValueType Type;
58
59 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
60 /// that we contain.
Chris Lattner053a2052003-08-10 23:51:52 +000061 std::vector<std::pair<TreePatternNode*, std::string> > Children;
Chris Lattner018c9e42003-08-07 05:40:14 +000062
63 /// Value - If this node is a leaf, this indicates what the thing is.
Chris Lattnerf8e96832003-08-07 19:12:24 +000064 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000065 Init *Value;
66public:
Chris Lattner053a2052003-08-10 23:51:52 +000067 TreePatternNode(Record *o, const std::vector<std::pair<TreePatternNode*,
68 std::string> > &c)
Chris Lattner018c9e42003-08-07 05:40:14 +000069 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
70 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
71
Chris Lattner6dafd392003-08-08 16:30:10 +000072 Record *getOperator() const {
73 assert(Operator && "This is a leaf node!");
74 return Operator;
75 }
Chris Lattner018c9e42003-08-07 05:40:14 +000076 MVT::ValueType getType() const { return Type; }
77 void setType(MVT::ValueType T) { Type = T; }
78
79 bool isLeaf() const { return Operator == 0; }
80
Chris Lattner955c1be2003-08-08 22:29:23 +000081 unsigned getNumChildren() const { return Children.size(); }
Chris Lattnerf8e96832003-08-07 19:12:24 +000082 TreePatternNode *getChild(unsigned c) const {
Chris Lattner053a2052003-08-10 23:51:52 +000083 assert(Operator != 0 && "This is a leaf node!");
Chris Lattnerf8e96832003-08-07 19:12:24 +000084 assert(c < Children.size() && "Child access out of range!");
Chris Lattner053a2052003-08-10 23:51:52 +000085 return Children[c].first;
86 }
87 const std::string &getChildName(unsigned c) const {
88 assert(Operator != 0 && "This is a leaf node!");
89 assert(c < Children.size() && "Child access out of range!");
90 return Children[c].second;
Chris Lattnerf8e96832003-08-07 19:12:24 +000091 }
92
Chris Lattner018c9e42003-08-07 05:40:14 +000093 Init *getValue() const {
94 assert(Operator == 0 && "This is not a leaf node!");
95 return Value;
96 }
97
Chris Lattner955c1be2003-08-08 22:29:23 +000098 /// getValueRecord - Returns the value of this tree node as a record. For now
99 /// we only allow DefInit's as our leaf values, so this is used.
100 Record *getValueRecord() const;
101
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000102 /// clone - Make a copy of this tree and all of its children.
103 ///
104 TreePatternNode *clone() const;
105
Chris Lattner018c9e42003-08-07 05:40:14 +0000106 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000107
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000108 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
109 /// are not themselves completely resolved, clone the nonterminal and resolve
110 /// it with the using context we provide.
111 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000112
Chris Lattner955c1be2003-08-08 22:29:23 +0000113 /// UpdateNodeType - Set the node type of N to VT if VT contains information.
114 /// If N already contains a conflicting type, then throw an exception. This
115 /// returns true if any information was updated.
116 ///
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000117 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000118};
119
120std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
121
122
123
Chris Lattnerf8e96832003-08-07 19:12:24 +0000124/// Pattern - Represent a pattern of one form or another. Currently, three
125/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
126///
127struct Pattern {
128 enum PatternType {
129 Nonterminal, Instruction, Expander
130 };
131private:
132 /// PTy - The type of pattern this is.
133 ///
134 PatternType PTy;
135
136 /// Tree - The tree pattern which corresponds to this pattern. Note that if
137 /// there was a (set) node on the outside level that it has been stripped off.
138 ///
139 TreePatternNode *Tree;
140
141 /// Result - If this is an instruction or expander pattern, this is the
142 /// register result, specified with a (set) in the pattern.
143 ///
Chris Lattnerabb215e2003-08-11 21:28:59 +0000144 std::string ResultName; // The name of the result value...
145 TreePatternNode *ResultNode; // The leaf node for the result register...
Chris Lattnerf8e96832003-08-07 19:12:24 +0000146
147 /// TheRecord - The actual TableGen record corresponding to this pattern.
148 ///
149 Record *TheRecord;
150
151 /// Resolved - This is true of the pattern is useful in practice. In
152 /// particular, some non-terminals will have non-resolvable types. When a
153 /// user of the non-terminal is later found, they will have inferred a type
154 /// for the result of the non-terminal, which cause a clone of an unresolved
155 /// nonterminal to be made which is "resolved".
156 ///
157 bool Resolved;
158
Chris Lattner053a2052003-08-10 23:51:52 +0000159 /// Args - This is a list of all of the arguments to this pattern, which are
160 /// the non-void leaf nodes in this pattern.
161 std::vector<std::pair<TreePatternNode*, std::string> > Args;
162
Chris Lattnerf8e96832003-08-07 19:12:24 +0000163 /// ISE - the instruction selector emitter coordinating this madness.
164 ///
165 InstrSelectorEmitter &ISE;
166public:
167
168 /// Pattern constructor - Parse the specified DagInitializer into the current
169 /// record.
170 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
171 InstrSelectorEmitter &ise);
172
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000173 /// Pattern - Constructor used for cloning nonterminal patterns
174 Pattern(TreePatternNode *tree, Record *rec, bool res,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000175 InstrSelectorEmitter &ise)
176 : PTy(Nonterminal), Tree(tree), ResultNode(0), TheRecord(rec),
177 Resolved(res), ISE(ise) {
Chris Lattner053a2052003-08-10 23:51:52 +0000178 calculateArgs(Tree, "");
179 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000180
Chris Lattnerf8e96832003-08-07 19:12:24 +0000181 /// getPatternType - Return what flavor of Record this pattern originated from
182 ///
183 PatternType getPatternType() const { return PTy; }
184
185 /// getTree - Return the tree pattern which corresponds to this pattern.
186 ///
187 TreePatternNode *getTree() const { return Tree; }
188
Chris Lattnerabb215e2003-08-11 21:28:59 +0000189 Record *getResult() const {
190 return ResultNode ? ResultNode->getValueRecord() : 0;
191 }
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000192 const std::string &getResultName() const { return ResultName; }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000193 TreePatternNode *getResultNode() const { return ResultNode; }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000194
195 /// getRecord - Return the actual TableGen record corresponding to this
196 /// pattern.
197 ///
198 Record *getRecord() const { return TheRecord; }
199
Chris Lattner053a2052003-08-10 23:51:52 +0000200 unsigned getNumArgs() const { return Args.size(); }
201 TreePatternNode *getArg(unsigned i) const {
202 assert(i < Args.size() && "Argument reference out of range!");
203 return Args[i].first;
204 }
205 Record *getArgRec(unsigned i) const {
206 return getArg(i)->getValueRecord();
207 }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000208 Init *getArgVal(unsigned i) const {
209 return getArg(i)->getValue();
210 }
Chris Lattner053a2052003-08-10 23:51:52 +0000211 const std::string &getArgName(unsigned i) const {
212 assert(i < Args.size() && "Argument reference out of range!");
213 return Args[i].second;
214 }
215
Chris Lattnerf8e96832003-08-07 19:12:24 +0000216 bool isResolved() const { return Resolved; }
217
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000218 /// InferAllTypes - Runs the type inference engine on the current pattern,
219 /// stopping when nothing can be inferred, then updating the Resolved field.
220 void InferAllTypes();
221
222 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
223 /// are not themselves completely resolved, clone the nonterminal and resolve
224 /// it with the using context we provide.
225 void InstantiateNonterminals() {
226 Tree->InstantiateNonterminals(ISE);
227 }
228
229 /// clone - This method is used to make an exact copy of the current pattern,
230 /// then change the "TheRecord" instance variable to the specified record.
231 ///
232 Pattern *clone(Record *R) const;
233
234 /// error - Throw an exception, prefixing it with information about this
235 /// pattern.
236 void error(const std::string &Msg) const;
237
Chris Lattner955c1be2003-08-08 22:29:23 +0000238 /// getSlotName - If this is a leaf node, return the slot name that the
239 /// operand will update.
240 std::string getSlotName() const;
241 static std::string getSlotName(Record *R);
242
Chris Lattner9552b8c2003-08-10 19:50:51 +0000243 void dump() const;
244
Chris Lattnerf8e96832003-08-07 19:12:24 +0000245private:
Chris Lattner053a2052003-08-10 23:51:52 +0000246 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000247 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000248 TreePatternNode *ParseTreePattern(DagInit *DI);
249 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000250};
251
252std::ostream &operator<<(std::ostream &OS, const Pattern &P);
253
254
Chris Lattner6dafd392003-08-08 16:30:10 +0000255/// PatternOrganizer - This class represents all of the patterns which are
256/// useful for the instruction selector, neatly catagorized in a hierarchical
257/// structure.
258struct PatternOrganizer {
259 /// PatternsForNode - The list of patterns which can produce a value of a
260 /// particular slot type, given a particular root node in the tree. All of
261 /// the patterns in this vector produce the same value type and have the same
262 /// root DAG node.
263 typedef std::vector<Pattern*> PatternsForNode;
264
265 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
266 /// lead to the production of a value for this slot. All of the patterns in
267 /// this data structure produces values of the same slot.
268 typedef std::map<Record*, PatternsForNode> NodesForSlot;
269
270 /// AllPatterns - This data structure contains all patterns in the instruction
271 /// selector.
272 std::map<std::string, NodesForSlot> AllPatterns;
273
274 // Forwarding functions...
275 typedef std::map<std::string, NodesForSlot>::iterator iterator;
276 iterator begin() { return AllPatterns.begin(); }
277 iterator end() { return AllPatterns.end(); }
278
279
280 /// addPattern - Add the specified pattern to the appropriate location in the
281 /// collection.
282 void addPattern(Pattern *P);
283};
284
Chris Lattnerf8e96832003-08-07 19:12:24 +0000285
286/// InstrSelectorEmitter - The top-level class which coordinates construction
287/// and emission of the instruction selector.
288///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000289class InstrSelectorEmitter : public TableGenBackend {
290 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000291 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000292
293 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000294
295 /// Patterns - a list of all of the patterns defined by the target description
296 ///
297 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000298
299 /// InstantiatedNTs - A data structure to keep track of which nonterminals
300 /// have been instantiated already...
301 ///
302 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000303
304 /// ComputableValues - This map indicates which patterns can be used to
305 /// generate a value that is used by the selector. The keys of this map
306 /// implicitly define the values that are used by the selector.
307 ///
308 PatternOrganizer ComputableValues;
309
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000310public:
311 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
312
313 // run - Output the instruction set description, returning true on failure.
314 void run(std::ostream &OS);
315
Chris Lattnerf8e96832003-08-07 19:12:24 +0000316 const CodeGenTarget &getTarget() const { return Target; }
317 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000318 const NodeType &getNodeType(Record *R) const {
319 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
320 assert(I != NodeTypes.end() && "Unknown node type!");
321 return I->second;
322 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000323
Chris Lattner5709e512003-08-07 21:02:56 +0000324 /// getPattern - return the pattern corresponding to the specified record, or
325 /// null if there is none.
326 Pattern *getPattern(Record *R) const {
327 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
328 return I != Patterns.end() ? I->second : 0;
329 }
330
331 /// ReadNonterminal - This method parses the specified record as a
332 /// nonterminal, but only if it hasn't been read in already.
333 Pattern *ReadNonterminal(Record *R);
334
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000335 /// InstantiateNonterminal - This method takes the nonterminal specified by
336 /// NT, which should not be completely resolved, clones it, applies ResultTy
337 /// to its root, then runs the type inference stuff on it. This should
338 /// produce a newly resolved nonterminal, which we make a record for and
339 /// return. To be extra fancy and efficient, this only makes one clone for
340 /// each type it is instantiated with.
341 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
342
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000343private:
Chris Lattneree858d22003-08-07 20:42:23 +0000344 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
345 // turning them into the more accessible NodeTypes data structure.
346 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000347
Chris Lattneree858d22003-08-07 20:42:23 +0000348 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
349 // pattern database.
350 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000351
Chris Lattneree858d22003-08-07 20:42:23 +0000352 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000353 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000354 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000355
Chris Lattneree858d22003-08-07 20:42:23 +0000356 // ReadExpanderPatterns - Read in all of the expanded patterns.
357 void ReadExpanderPatterns();
358
359 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
360 // information from the context that they are used in.
361 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000362
363 // CalculateComputableValues - Fill in the ComputableValues map through
364 // analysis of the patterns we are playing with.
365 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000366
367 // EmitMatchCosters - Given a list of patterns, which all have the same root
368 // pattern operator, emit an efficient decision tree to decide which one to
369 // pick. This is structured this way to avoid reevaluations of non-obvious
370 // subexpressions.
371 void EmitMatchCosters(std::ostream &OS,
372 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
373 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000374
375 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
376 /// process for the expander pattern P. This argument may be referencing some
377 /// values defined in P, or may just be physical register references or
378 /// something like that. If PrintArg is true, we are printing out arguments
379 /// to the BuildMI call. If it is false, we are printing the result register
380 /// name.
381 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000382 TreePatternNode *ArgDecl, Pattern *P,
Chris Lattner053a2052003-08-10 23:51:52 +0000383 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000384};
385
386#endif