blob: a1b1b6e7816adffbdfc158b8a33c33a4e4487cf8 [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 ///
144 Record *Result;
145
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,
174 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
Chris Lattner053a2052003-08-10 23:51:52 +0000175 TheRecord(rec), Resolved(res), ISE(ise) {
176 calculateArgs(Tree, "");
177 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000178
Chris Lattnerf8e96832003-08-07 19:12:24 +0000179 /// getPatternType - Return what flavor of Record this pattern originated from
180 ///
181 PatternType getPatternType() const { return PTy; }
182
183 /// getTree - Return the tree pattern which corresponds to this pattern.
184 ///
185 TreePatternNode *getTree() const { return Tree; }
186
187 Record *getResult() const { return Result; }
188
189 /// getRecord - Return the actual TableGen record corresponding to this
190 /// pattern.
191 ///
192 Record *getRecord() const { return TheRecord; }
193
Chris Lattner053a2052003-08-10 23:51:52 +0000194 unsigned getNumArgs() const { return Args.size(); }
195 TreePatternNode *getArg(unsigned i) const {
196 assert(i < Args.size() && "Argument reference out of range!");
197 return Args[i].first;
198 }
199 Record *getArgRec(unsigned i) const {
200 return getArg(i)->getValueRecord();
201 }
202 const std::string &getArgName(unsigned i) const {
203 assert(i < Args.size() && "Argument reference out of range!");
204 return Args[i].second;
205 }
206
Chris Lattnerf8e96832003-08-07 19:12:24 +0000207 bool isResolved() const { return Resolved; }
208
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000209 /// InferAllTypes - Runs the type inference engine on the current pattern,
210 /// stopping when nothing can be inferred, then updating the Resolved field.
211 void InferAllTypes();
212
213 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
214 /// are not themselves completely resolved, clone the nonterminal and resolve
215 /// it with the using context we provide.
216 void InstantiateNonterminals() {
217 Tree->InstantiateNonterminals(ISE);
218 }
219
220 /// clone - This method is used to make an exact copy of the current pattern,
221 /// then change the "TheRecord" instance variable to the specified record.
222 ///
223 Pattern *clone(Record *R) const;
224
225 /// error - Throw an exception, prefixing it with information about this
226 /// pattern.
227 void error(const std::string &Msg) const;
228
Chris Lattner955c1be2003-08-08 22:29:23 +0000229 /// getSlotName - If this is a leaf node, return the slot name that the
230 /// operand will update.
231 std::string getSlotName() const;
232 static std::string getSlotName(Record *R);
233
Chris Lattner9552b8c2003-08-10 19:50:51 +0000234 void dump() const;
235
Chris Lattnerf8e96832003-08-07 19:12:24 +0000236private:
Chris Lattner053a2052003-08-10 23:51:52 +0000237 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000238 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000239 TreePatternNode *ParseTreePattern(DagInit *DI);
240 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000241};
242
243std::ostream &operator<<(std::ostream &OS, const Pattern &P);
244
245
Chris Lattner6dafd392003-08-08 16:30:10 +0000246/// PatternOrganizer - This class represents all of the patterns which are
247/// useful for the instruction selector, neatly catagorized in a hierarchical
248/// structure.
249struct PatternOrganizer {
250 /// PatternsForNode - The list of patterns which can produce a value of a
251 /// particular slot type, given a particular root node in the tree. All of
252 /// the patterns in this vector produce the same value type and have the same
253 /// root DAG node.
254 typedef std::vector<Pattern*> PatternsForNode;
255
256 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
257 /// lead to the production of a value for this slot. All of the patterns in
258 /// this data structure produces values of the same slot.
259 typedef std::map<Record*, PatternsForNode> NodesForSlot;
260
261 /// AllPatterns - This data structure contains all patterns in the instruction
262 /// selector.
263 std::map<std::string, NodesForSlot> AllPatterns;
264
265 // Forwarding functions...
266 typedef std::map<std::string, NodesForSlot>::iterator iterator;
267 iterator begin() { return AllPatterns.begin(); }
268 iterator end() { return AllPatterns.end(); }
269
270
271 /// addPattern - Add the specified pattern to the appropriate location in the
272 /// collection.
273 void addPattern(Pattern *P);
274};
275
Chris Lattnerf8e96832003-08-07 19:12:24 +0000276
277/// InstrSelectorEmitter - The top-level class which coordinates construction
278/// and emission of the instruction selector.
279///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000280class InstrSelectorEmitter : public TableGenBackend {
281 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000282 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000283
284 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000285
286 /// Patterns - a list of all of the patterns defined by the target description
287 ///
288 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000289
290 /// InstantiatedNTs - A data structure to keep track of which nonterminals
291 /// have been instantiated already...
292 ///
293 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000294
295 /// ComputableValues - This map indicates which patterns can be used to
296 /// generate a value that is used by the selector. The keys of this map
297 /// implicitly define the values that are used by the selector.
298 ///
299 PatternOrganizer ComputableValues;
300
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000301public:
302 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
303
304 // run - Output the instruction set description, returning true on failure.
305 void run(std::ostream &OS);
306
Chris Lattnerf8e96832003-08-07 19:12:24 +0000307 const CodeGenTarget &getTarget() const { return Target; }
308 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000309 const NodeType &getNodeType(Record *R) const {
310 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
311 assert(I != NodeTypes.end() && "Unknown node type!");
312 return I->second;
313 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000314
Chris Lattner5709e512003-08-07 21:02:56 +0000315 /// getPattern - return the pattern corresponding to the specified record, or
316 /// null if there is none.
317 Pattern *getPattern(Record *R) const {
318 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
319 return I != Patterns.end() ? I->second : 0;
320 }
321
322 /// ReadNonterminal - This method parses the specified record as a
323 /// nonterminal, but only if it hasn't been read in already.
324 Pattern *ReadNonterminal(Record *R);
325
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000326 /// InstantiateNonterminal - This method takes the nonterminal specified by
327 /// NT, which should not be completely resolved, clones it, applies ResultTy
328 /// to its root, then runs the type inference stuff on it. This should
329 /// produce a newly resolved nonterminal, which we make a record for and
330 /// return. To be extra fancy and efficient, this only makes one clone for
331 /// each type it is instantiated with.
332 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
333
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000334private:
Chris Lattneree858d22003-08-07 20:42:23 +0000335 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
336 // turning them into the more accessible NodeTypes data structure.
337 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000338
Chris Lattneree858d22003-08-07 20:42:23 +0000339 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
340 // pattern database.
341 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000342
Chris Lattneree858d22003-08-07 20:42:23 +0000343 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000344 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000345 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000346
Chris Lattneree858d22003-08-07 20:42:23 +0000347 // ReadExpanderPatterns - Read in all of the expanded patterns.
348 void ReadExpanderPatterns();
349
350 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
351 // information from the context that they are used in.
352 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000353
354 // CalculateComputableValues - Fill in the ComputableValues map through
355 // analysis of the patterns we are playing with.
356 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000357
358 // EmitMatchCosters - Given a list of patterns, which all have the same root
359 // pattern operator, emit an efficient decision tree to decide which one to
360 // pick. This is structured this way to avoid reevaluations of non-obvious
361 // subexpressions.
362 void EmitMatchCosters(std::ostream &OS,
363 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
364 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000365
366 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
367 /// process for the expander pattern P. This argument may be referencing some
368 /// values defined in P, or may just be physical register references or
369 /// something like that. If PrintArg is true, we are printing out arguments
370 /// to the BuildMI call. If it is false, we are printing the result register
371 /// name.
372 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
373 Record *ArgDecl, Pattern *P,
374 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000375};
376
377#endif