blob: 922aa79dfa1b6a7e2488c358206a23c38bb7afcb [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>
John Criswell43bf32e2003-08-20 22:07:45 +000015#include <cassert>
16
Chris Lattner018c9e42003-08-07 05:40:14 +000017class DagInit;
18class Init;
Chris Lattnerf8e96832003-08-07 19:12:24 +000019class InstrSelectorEmitter;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000020
Chris Lattnerf8e96832003-08-07 19:12:24 +000021/// NodeType - Represents Information parsed from the DagNode entries.
22///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000023struct NodeType {
24 enum ArgResultTypes {
Chris Lattner6b666e82003-08-12 04:56:42 +000025 Any, // No constraint on type
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000026 Val, // A non-void type
27 Arg0, // Value matches the type of Arg0
Chris Lattner88118bf2003-08-11 20:25:52 +000028 Arg1, // Value matches the type of Arg1
Chris Lattner2787d1a2003-08-06 06:16:35 +000029 Ptr, // Tree node is the type of the target pointer
Chris Lattnerc12a6142003-08-12 04:28:21 +000030 I8, // Always bool
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000031 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000032 };
33
34 ArgResultTypes ResultType;
35 std::vector<ArgResultTypes> ArgTypes;
36
37 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
38 AT.swap(ArgTypes);
39 }
40
41 NodeType() : ResultType(Val) {}
42 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
43
44 static ArgResultTypes Translate(Record *R);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000045};
46
Chris Lattnerf8e96832003-08-07 19:12:24 +000047
48
49/// TreePatternNode - Represent a node of the tree patterns.
50///
Chris Lattner018c9e42003-08-07 05:40:14 +000051class TreePatternNode {
52 /// Operator - The operation that this node represents... this is null if this
53 /// is a leaf.
54 Record *Operator;
55
56 /// Type - The inferred value type...
Chris Lattnerf8e96832003-08-07 19:12:24 +000057 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000058 MVT::ValueType Type;
59
60 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
61 /// that we contain.
Chris Lattner053a2052003-08-10 23:51:52 +000062 std::vector<std::pair<TreePatternNode*, std::string> > Children;
Chris Lattner018c9e42003-08-07 05:40:14 +000063
64 /// Value - If this node is a leaf, this indicates what the thing is.
Chris Lattnerf8e96832003-08-07 19:12:24 +000065 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000066 Init *Value;
67public:
Chris Lattner053a2052003-08-10 23:51:52 +000068 TreePatternNode(Record *o, const std::vector<std::pair<TreePatternNode*,
69 std::string> > &c)
Chris Lattner018c9e42003-08-07 05:40:14 +000070 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
71 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
72
Chris Lattner6dafd392003-08-08 16:30:10 +000073 Record *getOperator() const {
74 assert(Operator && "This is a leaf node!");
75 return Operator;
76 }
Chris Lattner018c9e42003-08-07 05:40:14 +000077 MVT::ValueType getType() const { return Type; }
78 void setType(MVT::ValueType T) { Type = T; }
79
80 bool isLeaf() const { return Operator == 0; }
81
Chris Lattner955c1be2003-08-08 22:29:23 +000082 unsigned getNumChildren() const { return Children.size(); }
Chris Lattnerf8e96832003-08-07 19:12:24 +000083 TreePatternNode *getChild(unsigned c) const {
Chris Lattner053a2052003-08-10 23:51:52 +000084 assert(Operator != 0 && "This is a leaf node!");
Chris Lattnerf8e96832003-08-07 19:12:24 +000085 assert(c < Children.size() && "Child access out of range!");
Chris Lattner053a2052003-08-10 23:51:52 +000086 return Children[c].first;
87 }
88 const std::string &getChildName(unsigned c) const {
89 assert(Operator != 0 && "This is a leaf node!");
90 assert(c < Children.size() && "Child access out of range!");
91 return Children[c].second;
Chris Lattnerf8e96832003-08-07 19:12:24 +000092 }
93
Chris Lattner018c9e42003-08-07 05:40:14 +000094 Init *getValue() const {
95 assert(Operator == 0 && "This is not a leaf node!");
96 return Value;
97 }
98
Chris Lattner955c1be2003-08-08 22:29:23 +000099 /// getValueRecord - Returns the value of this tree node as a record. For now
100 /// we only allow DefInit's as our leaf values, so this is used.
101 Record *getValueRecord() const;
102
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000103 /// clone - Make a copy of this tree and all of its children.
104 ///
105 TreePatternNode *clone() const;
106
Chris Lattner018c9e42003-08-07 05:40:14 +0000107 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000108
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000109 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
110 /// are not themselves completely resolved, clone the nonterminal and resolve
111 /// it with the using context we provide.
112 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000113
Chris Lattner955c1be2003-08-08 22:29:23 +0000114 /// UpdateNodeType - Set the node type of N to VT if VT contains information.
115 /// If N already contains a conflicting type, then throw an exception. This
116 /// returns true if any information was updated.
117 ///
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000118 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000119};
120
121std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
122
123
124
Chris Lattnerf8e96832003-08-07 19:12:24 +0000125/// Pattern - Represent a pattern of one form or another. Currently, three
126/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
127///
128struct Pattern {
129 enum PatternType {
130 Nonterminal, Instruction, Expander
131 };
132private:
133 /// PTy - The type of pattern this is.
134 ///
135 PatternType PTy;
136
137 /// Tree - The tree pattern which corresponds to this pattern. Note that if
138 /// there was a (set) node on the outside level that it has been stripped off.
139 ///
140 TreePatternNode *Tree;
141
142 /// Result - If this is an instruction or expander pattern, this is the
143 /// register result, specified with a (set) in the pattern.
144 ///
Chris Lattnerabb215e2003-08-11 21:28:59 +0000145 std::string ResultName; // The name of the result value...
146 TreePatternNode *ResultNode; // The leaf node for the result register...
Chris Lattnerf8e96832003-08-07 19:12:24 +0000147
148 /// TheRecord - The actual TableGen record corresponding to this pattern.
149 ///
150 Record *TheRecord;
151
152 /// Resolved - This is true of the pattern is useful in practice. In
153 /// particular, some non-terminals will have non-resolvable types. When a
154 /// user of the non-terminal is later found, they will have inferred a type
155 /// for the result of the non-terminal, which cause a clone of an unresolved
156 /// nonterminal to be made which is "resolved".
157 ///
158 bool Resolved;
159
Chris Lattner053a2052003-08-10 23:51:52 +0000160 /// Args - This is a list of all of the arguments to this pattern, which are
161 /// the non-void leaf nodes in this pattern.
162 std::vector<std::pair<TreePatternNode*, std::string> > Args;
163
Chris Lattnerf8e96832003-08-07 19:12:24 +0000164 /// ISE - the instruction selector emitter coordinating this madness.
165 ///
166 InstrSelectorEmitter &ISE;
167public:
168
169 /// Pattern constructor - Parse the specified DagInitializer into the current
170 /// record.
171 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
172 InstrSelectorEmitter &ise);
173
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000174 /// Pattern - Constructor used for cloning nonterminal patterns
175 Pattern(TreePatternNode *tree, Record *rec, bool res,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000176 InstrSelectorEmitter &ise)
177 : PTy(Nonterminal), Tree(tree), ResultNode(0), TheRecord(rec),
178 Resolved(res), ISE(ise) {
Chris Lattner053a2052003-08-10 23:51:52 +0000179 calculateArgs(Tree, "");
180 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000181
Chris Lattnerf8e96832003-08-07 19:12:24 +0000182 /// getPatternType - Return what flavor of Record this pattern originated from
183 ///
184 PatternType getPatternType() const { return PTy; }
185
186 /// getTree - Return the tree pattern which corresponds to this pattern.
187 ///
188 TreePatternNode *getTree() const { return Tree; }
189
Chris Lattnerabb215e2003-08-11 21:28:59 +0000190 Record *getResult() const {
191 return ResultNode ? ResultNode->getValueRecord() : 0;
192 }
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000193 const std::string &getResultName() const { return ResultName; }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000194 TreePatternNode *getResultNode() const { return ResultNode; }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000195
196 /// getRecord - Return the actual TableGen record corresponding to this
197 /// pattern.
198 ///
199 Record *getRecord() const { return TheRecord; }
200
Chris Lattner053a2052003-08-10 23:51:52 +0000201 unsigned getNumArgs() const { return Args.size(); }
202 TreePatternNode *getArg(unsigned i) const {
203 assert(i < Args.size() && "Argument reference out of range!");
204 return Args[i].first;
205 }
206 Record *getArgRec(unsigned i) const {
207 return getArg(i)->getValueRecord();
208 }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000209 Init *getArgVal(unsigned i) const {
210 return getArg(i)->getValue();
211 }
Chris Lattner053a2052003-08-10 23:51:52 +0000212 const std::string &getArgName(unsigned i) const {
213 assert(i < Args.size() && "Argument reference out of range!");
214 return Args[i].second;
215 }
216
Chris Lattnerf8e96832003-08-07 19:12:24 +0000217 bool isResolved() const { return Resolved; }
218
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000219 /// InferAllTypes - Runs the type inference engine on the current pattern,
220 /// stopping when nothing can be inferred, then updating the Resolved field.
221 void InferAllTypes();
222
223 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
224 /// are not themselves completely resolved, clone the nonterminal and resolve
225 /// it with the using context we provide.
226 void InstantiateNonterminals() {
227 Tree->InstantiateNonterminals(ISE);
228 }
229
230 /// clone - This method is used to make an exact copy of the current pattern,
231 /// then change the "TheRecord" instance variable to the specified record.
232 ///
233 Pattern *clone(Record *R) const;
234
235 /// error - Throw an exception, prefixing it with information about this
236 /// pattern.
237 void error(const std::string &Msg) const;
238
Chris Lattner955c1be2003-08-08 22:29:23 +0000239 /// getSlotName - If this is a leaf node, return the slot name that the
240 /// operand will update.
241 std::string getSlotName() const;
242 static std::string getSlotName(Record *R);
243
Chris Lattner9552b8c2003-08-10 19:50:51 +0000244 void dump() const;
245
Chris Lattnerf8e96832003-08-07 19:12:24 +0000246private:
Chris Lattner053a2052003-08-10 23:51:52 +0000247 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000248 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000249 TreePatternNode *ParseTreePattern(DagInit *DI);
250 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000251};
252
253std::ostream &operator<<(std::ostream &OS, const Pattern &P);
254
255
Chris Lattner6dafd392003-08-08 16:30:10 +0000256/// PatternOrganizer - This class represents all of the patterns which are
257/// useful for the instruction selector, neatly catagorized in a hierarchical
258/// structure.
259struct PatternOrganizer {
260 /// PatternsForNode - The list of patterns which can produce a value of a
261 /// particular slot type, given a particular root node in the tree. All of
262 /// the patterns in this vector produce the same value type and have the same
263 /// root DAG node.
264 typedef std::vector<Pattern*> PatternsForNode;
265
266 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
267 /// lead to the production of a value for this slot. All of the patterns in
268 /// this data structure produces values of the same slot.
269 typedef std::map<Record*, PatternsForNode> NodesForSlot;
270
271 /// AllPatterns - This data structure contains all patterns in the instruction
272 /// selector.
273 std::map<std::string, NodesForSlot> AllPatterns;
274
275 // Forwarding functions...
276 typedef std::map<std::string, NodesForSlot>::iterator iterator;
277 iterator begin() { return AllPatterns.begin(); }
278 iterator end() { return AllPatterns.end(); }
279
280
281 /// addPattern - Add the specified pattern to the appropriate location in the
282 /// collection.
283 void addPattern(Pattern *P);
284};
285
Chris Lattnerf8e96832003-08-07 19:12:24 +0000286
287/// InstrSelectorEmitter - The top-level class which coordinates construction
288/// and emission of the instruction selector.
289///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000290class InstrSelectorEmitter : public TableGenBackend {
291 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000292 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000293
294 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000295
296 /// Patterns - a list of all of the patterns defined by the target description
297 ///
298 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000299
300 /// InstantiatedNTs - A data structure to keep track of which nonterminals
301 /// have been instantiated already...
302 ///
303 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000304
305 /// ComputableValues - This map indicates which patterns can be used to
306 /// generate a value that is used by the selector. The keys of this map
307 /// implicitly define the values that are used by the selector.
308 ///
309 PatternOrganizer ComputableValues;
310
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000311public:
312 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
313
314 // run - Output the instruction set description, returning true on failure.
315 void run(std::ostream &OS);
316
Chris Lattnerf8e96832003-08-07 19:12:24 +0000317 const CodeGenTarget &getTarget() const { return Target; }
318 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000319 const NodeType &getNodeType(Record *R) const {
320 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
321 assert(I != NodeTypes.end() && "Unknown node type!");
322 return I->second;
323 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000324
Chris Lattner5709e512003-08-07 21:02:56 +0000325 /// getPattern - return the pattern corresponding to the specified record, or
326 /// null if there is none.
327 Pattern *getPattern(Record *R) const {
328 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
329 return I != Patterns.end() ? I->second : 0;
330 }
331
332 /// ReadNonterminal - This method parses the specified record as a
333 /// nonterminal, but only if it hasn't been read in already.
334 Pattern *ReadNonterminal(Record *R);
335
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000336 /// InstantiateNonterminal - This method takes the nonterminal specified by
337 /// NT, which should not be completely resolved, clones it, applies ResultTy
338 /// to its root, then runs the type inference stuff on it. This should
339 /// produce a newly resolved nonterminal, which we make a record for and
340 /// return. To be extra fancy and efficient, this only makes one clone for
341 /// each type it is instantiated with.
342 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
343
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000344private:
Chris Lattneree858d22003-08-07 20:42:23 +0000345 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
346 // turning them into the more accessible NodeTypes data structure.
347 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000348
Chris Lattneree858d22003-08-07 20:42:23 +0000349 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
350 // pattern database.
351 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000352
Chris Lattneree858d22003-08-07 20:42:23 +0000353 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000354 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000355 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000356
Chris Lattneree858d22003-08-07 20:42:23 +0000357 // ReadExpanderPatterns - Read in all of the expanded patterns.
358 void ReadExpanderPatterns();
359
360 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
361 // information from the context that they are used in.
362 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000363
364 // CalculateComputableValues - Fill in the ComputableValues map through
365 // analysis of the patterns we are playing with.
366 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000367
368 // EmitMatchCosters - Given a list of patterns, which all have the same root
369 // pattern operator, emit an efficient decision tree to decide which one to
370 // pick. This is structured this way to avoid reevaluations of non-obvious
371 // subexpressions.
372 void EmitMatchCosters(std::ostream &OS,
373 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
374 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000375
376 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
377 /// process for the expander pattern P. This argument may be referencing some
378 /// values defined in P, or may just be physical register references or
379 /// something like that. If PrintArg is true, we are printing out arguments
380 /// to the BuildMI call. If it is false, we are printing the result register
381 /// name.
382 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000383 TreePatternNode *ArgDecl, Pattern *P,
Chris Lattner053a2052003-08-10 23:51:52 +0000384 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000385};
386
387#endif