blob: f81ad72828a98a459aa89510843b00a17ba5c675 [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 Lattnerc12a6142003-08-12 04:28:21 +000028 I8, // Always bool
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000029 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.
Chris Lattner053a2052003-08-10 23:51:52 +000060 std::vector<std::pair<TreePatternNode*, std::string> > Children;
Chris Lattner018c9e42003-08-07 05:40:14 +000061
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:
Chris Lattner053a2052003-08-10 23:51:52 +000066 TreePatternNode(Record *o, const std::vector<std::pair<TreePatternNode*,
67 std::string> > &c)
Chris Lattner018c9e42003-08-07 05:40:14 +000068 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
69 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
70
Chris Lattner6dafd392003-08-08 16:30:10 +000071 Record *getOperator() const {
72 assert(Operator && "This is a leaf node!");
73 return Operator;
74 }
Chris Lattner018c9e42003-08-07 05:40:14 +000075 MVT::ValueType getType() const { return Type; }
76 void setType(MVT::ValueType T) { Type = T; }
77
78 bool isLeaf() const { return Operator == 0; }
79
Chris Lattner955c1be2003-08-08 22:29:23 +000080 unsigned getNumChildren() const { return Children.size(); }
Chris Lattnerf8e96832003-08-07 19:12:24 +000081 TreePatternNode *getChild(unsigned c) const {
Chris Lattner053a2052003-08-10 23:51:52 +000082 assert(Operator != 0 && "This is a leaf node!");
Chris Lattnerf8e96832003-08-07 19:12:24 +000083 assert(c < Children.size() && "Child access out of range!");
Chris Lattner053a2052003-08-10 23:51:52 +000084 return Children[c].first;
85 }
86 const std::string &getChildName(unsigned c) const {
87 assert(Operator != 0 && "This is a leaf node!");
88 assert(c < Children.size() && "Child access out of range!");
89 return Children[c].second;
Chris Lattnerf8e96832003-08-07 19:12:24 +000090 }
91
Chris Lattner018c9e42003-08-07 05:40:14 +000092 Init *getValue() const {
93 assert(Operator == 0 && "This is not a leaf node!");
94 return Value;
95 }
96
Chris Lattner955c1be2003-08-08 22:29:23 +000097 /// getValueRecord - Returns the value of this tree node as a record. For now
98 /// we only allow DefInit's as our leaf values, so this is used.
99 Record *getValueRecord() const;
100
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000101 /// clone - Make a copy of this tree and all of its children.
102 ///
103 TreePatternNode *clone() const;
104
Chris Lattner018c9e42003-08-07 05:40:14 +0000105 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000106
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000107 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
108 /// are not themselves completely resolved, clone the nonterminal and resolve
109 /// it with the using context we provide.
110 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000111
Chris Lattner955c1be2003-08-08 22:29:23 +0000112 /// UpdateNodeType - Set the node type of N to VT if VT contains information.
113 /// If N already contains a conflicting type, then throw an exception. This
114 /// returns true if any information was updated.
115 ///
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000116 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000117};
118
119std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
120
121
122
Chris Lattnerf8e96832003-08-07 19:12:24 +0000123/// Pattern - Represent a pattern of one form or another. Currently, three
124/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
125///
126struct Pattern {
127 enum PatternType {
128 Nonterminal, Instruction, Expander
129 };
130private:
131 /// PTy - The type of pattern this is.
132 ///
133 PatternType PTy;
134
135 /// Tree - The tree pattern which corresponds to this pattern. Note that if
136 /// there was a (set) node on the outside level that it has been stripped off.
137 ///
138 TreePatternNode *Tree;
139
140 /// Result - If this is an instruction or expander pattern, this is the
141 /// register result, specified with a (set) in the pattern.
142 ///
Chris Lattnerabb215e2003-08-11 21:28:59 +0000143 std::string ResultName; // The name of the result value...
144 TreePatternNode *ResultNode; // The leaf node for the result register...
Chris Lattnerf8e96832003-08-07 19:12:24 +0000145
146 /// TheRecord - The actual TableGen record corresponding to this pattern.
147 ///
148 Record *TheRecord;
149
150 /// Resolved - This is true of the pattern is useful in practice. In
151 /// particular, some non-terminals will have non-resolvable types. When a
152 /// user of the non-terminal is later found, they will have inferred a type
153 /// for the result of the non-terminal, which cause a clone of an unresolved
154 /// nonterminal to be made which is "resolved".
155 ///
156 bool Resolved;
157
Chris Lattner053a2052003-08-10 23:51:52 +0000158 /// Args - This is a list of all of the arguments to this pattern, which are
159 /// the non-void leaf nodes in this pattern.
160 std::vector<std::pair<TreePatternNode*, std::string> > Args;
161
Chris Lattnerf8e96832003-08-07 19:12:24 +0000162 /// ISE - the instruction selector emitter coordinating this madness.
163 ///
164 InstrSelectorEmitter &ISE;
165public:
166
167 /// Pattern constructor - Parse the specified DagInitializer into the current
168 /// record.
169 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
170 InstrSelectorEmitter &ise);
171
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000172 /// Pattern - Constructor used for cloning nonterminal patterns
173 Pattern(TreePatternNode *tree, Record *rec, bool res,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000174 InstrSelectorEmitter &ise)
175 : PTy(Nonterminal), Tree(tree), ResultNode(0), TheRecord(rec),
176 Resolved(res), ISE(ise) {
Chris Lattner053a2052003-08-10 23:51:52 +0000177 calculateArgs(Tree, "");
178 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000179
Chris Lattnerf8e96832003-08-07 19:12:24 +0000180 /// getPatternType - Return what flavor of Record this pattern originated from
181 ///
182 PatternType getPatternType() const { return PTy; }
183
184 /// getTree - Return the tree pattern which corresponds to this pattern.
185 ///
186 TreePatternNode *getTree() const { return Tree; }
187
Chris Lattnerabb215e2003-08-11 21:28:59 +0000188 Record *getResult() const {
189 return ResultNode ? ResultNode->getValueRecord() : 0;
190 }
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000191 const std::string &getResultName() const { return ResultName; }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000192 TreePatternNode *getResultNode() const { return ResultNode; }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000193
194 /// getRecord - Return the actual TableGen record corresponding to this
195 /// pattern.
196 ///
197 Record *getRecord() const { return TheRecord; }
198
Chris Lattner053a2052003-08-10 23:51:52 +0000199 unsigned getNumArgs() const { return Args.size(); }
200 TreePatternNode *getArg(unsigned i) const {
201 assert(i < Args.size() && "Argument reference out of range!");
202 return Args[i].first;
203 }
204 Record *getArgRec(unsigned i) const {
205 return getArg(i)->getValueRecord();
206 }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000207 Init *getArgVal(unsigned i) const {
208 return getArg(i)->getValue();
209 }
Chris Lattner053a2052003-08-10 23:51:52 +0000210 const std::string &getArgName(unsigned i) const {
211 assert(i < Args.size() && "Argument reference out of range!");
212 return Args[i].second;
213 }
214
Chris Lattnerf8e96832003-08-07 19:12:24 +0000215 bool isResolved() const { return Resolved; }
216
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000217 /// InferAllTypes - Runs the type inference engine on the current pattern,
218 /// stopping when nothing can be inferred, then updating the Resolved field.
219 void InferAllTypes();
220
221 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
222 /// are not themselves completely resolved, clone the nonterminal and resolve
223 /// it with the using context we provide.
224 void InstantiateNonterminals() {
225 Tree->InstantiateNonterminals(ISE);
226 }
227
228 /// clone - This method is used to make an exact copy of the current pattern,
229 /// then change the "TheRecord" instance variable to the specified record.
230 ///
231 Pattern *clone(Record *R) const;
232
233 /// error - Throw an exception, prefixing it with information about this
234 /// pattern.
235 void error(const std::string &Msg) const;
236
Chris Lattner955c1be2003-08-08 22:29:23 +0000237 /// getSlotName - If this is a leaf node, return the slot name that the
238 /// operand will update.
239 std::string getSlotName() const;
240 static std::string getSlotName(Record *R);
241
Chris Lattner9552b8c2003-08-10 19:50:51 +0000242 void dump() const;
243
Chris Lattnerf8e96832003-08-07 19:12:24 +0000244private:
Chris Lattner053a2052003-08-10 23:51:52 +0000245 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000246 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000247 TreePatternNode *ParseTreePattern(DagInit *DI);
248 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000249};
250
251std::ostream &operator<<(std::ostream &OS, const Pattern &P);
252
253
Chris Lattner6dafd392003-08-08 16:30:10 +0000254/// PatternOrganizer - This class represents all of the patterns which are
255/// useful for the instruction selector, neatly catagorized in a hierarchical
256/// structure.
257struct PatternOrganizer {
258 /// PatternsForNode - The list of patterns which can produce a value of a
259 /// particular slot type, given a particular root node in the tree. All of
260 /// the patterns in this vector produce the same value type and have the same
261 /// root DAG node.
262 typedef std::vector<Pattern*> PatternsForNode;
263
264 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
265 /// lead to the production of a value for this slot. All of the patterns in
266 /// this data structure produces values of the same slot.
267 typedef std::map<Record*, PatternsForNode> NodesForSlot;
268
269 /// AllPatterns - This data structure contains all patterns in the instruction
270 /// selector.
271 std::map<std::string, NodesForSlot> AllPatterns;
272
273 // Forwarding functions...
274 typedef std::map<std::string, NodesForSlot>::iterator iterator;
275 iterator begin() { return AllPatterns.begin(); }
276 iterator end() { return AllPatterns.end(); }
277
278
279 /// addPattern - Add the specified pattern to the appropriate location in the
280 /// collection.
281 void addPattern(Pattern *P);
282};
283
Chris Lattnerf8e96832003-08-07 19:12:24 +0000284
285/// InstrSelectorEmitter - The top-level class which coordinates construction
286/// and emission of the instruction selector.
287///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000288class InstrSelectorEmitter : public TableGenBackend {
289 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000290 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000291
292 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000293
294 /// Patterns - a list of all of the patterns defined by the target description
295 ///
296 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000297
298 /// InstantiatedNTs - A data structure to keep track of which nonterminals
299 /// have been instantiated already...
300 ///
301 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000302
303 /// ComputableValues - This map indicates which patterns can be used to
304 /// generate a value that is used by the selector. The keys of this map
305 /// implicitly define the values that are used by the selector.
306 ///
307 PatternOrganizer ComputableValues;
308
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000309public:
310 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
311
312 // run - Output the instruction set description, returning true on failure.
313 void run(std::ostream &OS);
314
Chris Lattnerf8e96832003-08-07 19:12:24 +0000315 const CodeGenTarget &getTarget() const { return Target; }
316 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000317 const NodeType &getNodeType(Record *R) const {
318 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
319 assert(I != NodeTypes.end() && "Unknown node type!");
320 return I->second;
321 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000322
Chris Lattner5709e512003-08-07 21:02:56 +0000323 /// getPattern - return the pattern corresponding to the specified record, or
324 /// null if there is none.
325 Pattern *getPattern(Record *R) const {
326 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
327 return I != Patterns.end() ? I->second : 0;
328 }
329
330 /// ReadNonterminal - This method parses the specified record as a
331 /// nonterminal, but only if it hasn't been read in already.
332 Pattern *ReadNonterminal(Record *R);
333
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000334 /// InstantiateNonterminal - This method takes the nonterminal specified by
335 /// NT, which should not be completely resolved, clones it, applies ResultTy
336 /// to its root, then runs the type inference stuff on it. This should
337 /// produce a newly resolved nonterminal, which we make a record for and
338 /// return. To be extra fancy and efficient, this only makes one clone for
339 /// each type it is instantiated with.
340 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
341
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000342private:
Chris Lattneree858d22003-08-07 20:42:23 +0000343 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
344 // turning them into the more accessible NodeTypes data structure.
345 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000346
Chris Lattneree858d22003-08-07 20:42:23 +0000347 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
348 // pattern database.
349 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000350
Chris Lattneree858d22003-08-07 20:42:23 +0000351 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000352 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000353 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000354
Chris Lattneree858d22003-08-07 20:42:23 +0000355 // ReadExpanderPatterns - Read in all of the expanded patterns.
356 void ReadExpanderPatterns();
357
358 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
359 // information from the context that they are used in.
360 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000361
362 // CalculateComputableValues - Fill in the ComputableValues map through
363 // analysis of the patterns we are playing with.
364 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000365
366 // EmitMatchCosters - Given a list of patterns, which all have the same root
367 // pattern operator, emit an efficient decision tree to decide which one to
368 // pick. This is structured this way to avoid reevaluations of non-obvious
369 // subexpressions.
370 void EmitMatchCosters(std::ostream &OS,
371 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
372 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000373
374 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
375 /// process for the expander pattern P. This argument may be referencing some
376 /// values defined in P, or may just be physical register references or
377 /// something like that. If PrintArg is true, we are printing out arguments
378 /// to the BuildMI call. If it is false, we are printing the result register
379 /// name.
380 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000381 TreePatternNode *ArgDecl, Pattern *P,
Chris Lattner053a2052003-08-10 23:51:52 +0000382 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000383};
384
385#endif