blob: 20e800bf73959d9fc1f78d8c43c03f2e4e21db59 [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.
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 ///
143 Record *Result;
144
145 /// TheRecord - The actual TableGen record corresponding to this pattern.
146 ///
147 Record *TheRecord;
148
149 /// Resolved - This is true of the pattern is useful in practice. In
150 /// particular, some non-terminals will have non-resolvable types. When a
151 /// user of the non-terminal is later found, they will have inferred a type
152 /// for the result of the non-terminal, which cause a clone of an unresolved
153 /// nonterminal to be made which is "resolved".
154 ///
155 bool Resolved;
156
Chris Lattner053a2052003-08-10 23:51:52 +0000157 /// Args - This is a list of all of the arguments to this pattern, which are
158 /// the non-void leaf nodes in this pattern.
159 std::vector<std::pair<TreePatternNode*, std::string> > Args;
160
Chris Lattnerf8e96832003-08-07 19:12:24 +0000161 /// ISE - the instruction selector emitter coordinating this madness.
162 ///
163 InstrSelectorEmitter &ISE;
164public:
165
166 /// Pattern constructor - Parse the specified DagInitializer into the current
167 /// record.
168 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
169 InstrSelectorEmitter &ise);
170
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000171 /// Pattern - Constructor used for cloning nonterminal patterns
172 Pattern(TreePatternNode *tree, Record *rec, bool res,
173 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
Chris Lattner053a2052003-08-10 23:51:52 +0000174 TheRecord(rec), Resolved(res), ISE(ise) {
175 calculateArgs(Tree, "");
176 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000177
Chris Lattnerf8e96832003-08-07 19:12:24 +0000178 /// getPatternType - Return what flavor of Record this pattern originated from
179 ///
180 PatternType getPatternType() const { return PTy; }
181
182 /// getTree - Return the tree pattern which corresponds to this pattern.
183 ///
184 TreePatternNode *getTree() const { return Tree; }
185
186 Record *getResult() const { return Result; }
187
188 /// getRecord - Return the actual TableGen record corresponding to this
189 /// pattern.
190 ///
191 Record *getRecord() const { return TheRecord; }
192
Chris Lattner053a2052003-08-10 23:51:52 +0000193 unsigned getNumArgs() const { return Args.size(); }
194 TreePatternNode *getArg(unsigned i) const {
195 assert(i < Args.size() && "Argument reference out of range!");
196 return Args[i].first;
197 }
198 Record *getArgRec(unsigned i) const {
199 return getArg(i)->getValueRecord();
200 }
201 const std::string &getArgName(unsigned i) const {
202 assert(i < Args.size() && "Argument reference out of range!");
203 return Args[i].second;
204 }
205
Chris Lattnerf8e96832003-08-07 19:12:24 +0000206 bool isResolved() const { return Resolved; }
207
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000208 /// InferAllTypes - Runs the type inference engine on the current pattern,
209 /// stopping when nothing can be inferred, then updating the Resolved field.
210 void InferAllTypes();
211
212 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
213 /// are not themselves completely resolved, clone the nonterminal and resolve
214 /// it with the using context we provide.
215 void InstantiateNonterminals() {
216 Tree->InstantiateNonterminals(ISE);
217 }
218
219 /// clone - This method is used to make an exact copy of the current pattern,
220 /// then change the "TheRecord" instance variable to the specified record.
221 ///
222 Pattern *clone(Record *R) const;
223
224 /// error - Throw an exception, prefixing it with information about this
225 /// pattern.
226 void error(const std::string &Msg) const;
227
Chris Lattner955c1be2003-08-08 22:29:23 +0000228 /// getSlotName - If this is a leaf node, return the slot name that the
229 /// operand will update.
230 std::string getSlotName() const;
231 static std::string getSlotName(Record *R);
232
Chris Lattner9552b8c2003-08-10 19:50:51 +0000233 void dump() const;
234
Chris Lattnerf8e96832003-08-07 19:12:24 +0000235private:
Chris Lattner053a2052003-08-10 23:51:52 +0000236 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000237 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000238 TreePatternNode *ParseTreePattern(DagInit *DI);
239 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000240};
241
242std::ostream &operator<<(std::ostream &OS, const Pattern &P);
243
244
Chris Lattner6dafd392003-08-08 16:30:10 +0000245/// PatternOrganizer - This class represents all of the patterns which are
246/// useful for the instruction selector, neatly catagorized in a hierarchical
247/// structure.
248struct PatternOrganizer {
249 /// PatternsForNode - The list of patterns which can produce a value of a
250 /// particular slot type, given a particular root node in the tree. All of
251 /// the patterns in this vector produce the same value type and have the same
252 /// root DAG node.
253 typedef std::vector<Pattern*> PatternsForNode;
254
255 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
256 /// lead to the production of a value for this slot. All of the patterns in
257 /// this data structure produces values of the same slot.
258 typedef std::map<Record*, PatternsForNode> NodesForSlot;
259
260 /// AllPatterns - This data structure contains all patterns in the instruction
261 /// selector.
262 std::map<std::string, NodesForSlot> AllPatterns;
263
264 // Forwarding functions...
265 typedef std::map<std::string, NodesForSlot>::iterator iterator;
266 iterator begin() { return AllPatterns.begin(); }
267 iterator end() { return AllPatterns.end(); }
268
269
270 /// addPattern - Add the specified pattern to the appropriate location in the
271 /// collection.
272 void addPattern(Pattern *P);
273};
274
Chris Lattnerf8e96832003-08-07 19:12:24 +0000275
276/// InstrSelectorEmitter - The top-level class which coordinates construction
277/// and emission of the instruction selector.
278///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000279class InstrSelectorEmitter : public TableGenBackend {
280 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000281 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000282
283 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000284
285 /// Patterns - a list of all of the patterns defined by the target description
286 ///
287 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000288
289 /// InstantiatedNTs - A data structure to keep track of which nonterminals
290 /// have been instantiated already...
291 ///
292 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000293
294 /// ComputableValues - This map indicates which patterns can be used to
295 /// generate a value that is used by the selector. The keys of this map
296 /// implicitly define the values that are used by the selector.
297 ///
298 PatternOrganizer ComputableValues;
299
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000300public:
301 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
302
303 // run - Output the instruction set description, returning true on failure.
304 void run(std::ostream &OS);
305
Chris Lattnerf8e96832003-08-07 19:12:24 +0000306 const CodeGenTarget &getTarget() const { return Target; }
307 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000308 const NodeType &getNodeType(Record *R) const {
309 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
310 assert(I != NodeTypes.end() && "Unknown node type!");
311 return I->second;
312 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000313
Chris Lattner5709e512003-08-07 21:02:56 +0000314 /// getPattern - return the pattern corresponding to the specified record, or
315 /// null if there is none.
316 Pattern *getPattern(Record *R) const {
317 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
318 return I != Patterns.end() ? I->second : 0;
319 }
320
321 /// ReadNonterminal - This method parses the specified record as a
322 /// nonterminal, but only if it hasn't been read in already.
323 Pattern *ReadNonterminal(Record *R);
324
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000325 /// InstantiateNonterminal - This method takes the nonterminal specified by
326 /// NT, which should not be completely resolved, clones it, applies ResultTy
327 /// to its root, then runs the type inference stuff on it. This should
328 /// produce a newly resolved nonterminal, which we make a record for and
329 /// return. To be extra fancy and efficient, this only makes one clone for
330 /// each type it is instantiated with.
331 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
332
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000333private:
Chris Lattneree858d22003-08-07 20:42:23 +0000334 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
335 // turning them into the more accessible NodeTypes data structure.
336 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000337
Chris Lattneree858d22003-08-07 20:42:23 +0000338 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
339 // pattern database.
340 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000341
Chris Lattneree858d22003-08-07 20:42:23 +0000342 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000343 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000344 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000345
Chris Lattneree858d22003-08-07 20:42:23 +0000346 // ReadExpanderPatterns - Read in all of the expanded patterns.
347 void ReadExpanderPatterns();
348
349 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
350 // information from the context that they are used in.
351 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000352
353 // CalculateComputableValues - Fill in the ComputableValues map through
354 // analysis of the patterns we are playing with.
355 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000356
357 // EmitMatchCosters - Given a list of patterns, which all have the same root
358 // pattern operator, emit an efficient decision tree to decide which one to
359 // pick. This is structured this way to avoid reevaluations of non-obvious
360 // subexpressions.
361 void EmitMatchCosters(std::ostream &OS,
362 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
363 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000364
365 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
366 /// process for the expander pattern P. This argument may be referencing some
367 /// values defined in P, or may just be physical register references or
368 /// something like that. If PrintArg is true, we are printing out arguments
369 /// to the BuildMI call. If it is false, we are printing the result register
370 /// name.
371 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
372 Record *ArgDecl, Pattern *P,
373 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000374};
375
376#endif