blob: 9588130b612880732c2d18c4feb302f2abeca0a5 [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;
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000145 std::string ResultName; // The name of the result value...
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,
175 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
Chris Lattner053a2052003-08-10 23:51:52 +0000176 TheRecord(rec), Resolved(res), ISE(ise) {
177 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
188 Record *getResult() const { return Result; }
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000189 const std::string &getResultName() const { return ResultName; }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000190
191 /// getRecord - Return the actual TableGen record corresponding to this
192 /// pattern.
193 ///
194 Record *getRecord() const { return TheRecord; }
195
Chris Lattner053a2052003-08-10 23:51:52 +0000196 unsigned getNumArgs() const { return Args.size(); }
197 TreePatternNode *getArg(unsigned i) const {
198 assert(i < Args.size() && "Argument reference out of range!");
199 return Args[i].first;
200 }
201 Record *getArgRec(unsigned i) const {
202 return getArg(i)->getValueRecord();
203 }
204 const std::string &getArgName(unsigned i) const {
205 assert(i < Args.size() && "Argument reference out of range!");
206 return Args[i].second;
207 }
208
Chris Lattnerf8e96832003-08-07 19:12:24 +0000209 bool isResolved() const { return Resolved; }
210
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000211 /// InferAllTypes - Runs the type inference engine on the current pattern,
212 /// stopping when nothing can be inferred, then updating the Resolved field.
213 void InferAllTypes();
214
215 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
216 /// are not themselves completely resolved, clone the nonterminal and resolve
217 /// it with the using context we provide.
218 void InstantiateNonterminals() {
219 Tree->InstantiateNonterminals(ISE);
220 }
221
222 /// clone - This method is used to make an exact copy of the current pattern,
223 /// then change the "TheRecord" instance variable to the specified record.
224 ///
225 Pattern *clone(Record *R) const;
226
227 /// error - Throw an exception, prefixing it with information about this
228 /// pattern.
229 void error(const std::string &Msg) const;
230
Chris Lattner955c1be2003-08-08 22:29:23 +0000231 /// getSlotName - If this is a leaf node, return the slot name that the
232 /// operand will update.
233 std::string getSlotName() const;
234 static std::string getSlotName(Record *R);
235
Chris Lattner9552b8c2003-08-10 19:50:51 +0000236 void dump() const;
237
Chris Lattnerf8e96832003-08-07 19:12:24 +0000238private:
Chris Lattner053a2052003-08-10 23:51:52 +0000239 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000240 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000241 TreePatternNode *ParseTreePattern(DagInit *DI);
242 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000243};
244
245std::ostream &operator<<(std::ostream &OS, const Pattern &P);
246
247
Chris Lattner6dafd392003-08-08 16:30:10 +0000248/// PatternOrganizer - This class represents all of the patterns which are
249/// useful for the instruction selector, neatly catagorized in a hierarchical
250/// structure.
251struct PatternOrganizer {
252 /// PatternsForNode - The list of patterns which can produce a value of a
253 /// particular slot type, given a particular root node in the tree. All of
254 /// the patterns in this vector produce the same value type and have the same
255 /// root DAG node.
256 typedef std::vector<Pattern*> PatternsForNode;
257
258 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
259 /// lead to the production of a value for this slot. All of the patterns in
260 /// this data structure produces values of the same slot.
261 typedef std::map<Record*, PatternsForNode> NodesForSlot;
262
263 /// AllPatterns - This data structure contains all patterns in the instruction
264 /// selector.
265 std::map<std::string, NodesForSlot> AllPatterns;
266
267 // Forwarding functions...
268 typedef std::map<std::string, NodesForSlot>::iterator iterator;
269 iterator begin() { return AllPatterns.begin(); }
270 iterator end() { return AllPatterns.end(); }
271
272
273 /// addPattern - Add the specified pattern to the appropriate location in the
274 /// collection.
275 void addPattern(Pattern *P);
276};
277
Chris Lattnerf8e96832003-08-07 19:12:24 +0000278
279/// InstrSelectorEmitter - The top-level class which coordinates construction
280/// and emission of the instruction selector.
281///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000282class InstrSelectorEmitter : public TableGenBackend {
283 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000284 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000285
286 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000287
288 /// Patterns - a list of all of the patterns defined by the target description
289 ///
290 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000291
292 /// InstantiatedNTs - A data structure to keep track of which nonterminals
293 /// have been instantiated already...
294 ///
295 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000296
297 /// ComputableValues - This map indicates which patterns can be used to
298 /// generate a value that is used by the selector. The keys of this map
299 /// implicitly define the values that are used by the selector.
300 ///
301 PatternOrganizer ComputableValues;
302
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000303public:
304 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
305
306 // run - Output the instruction set description, returning true on failure.
307 void run(std::ostream &OS);
308
Chris Lattnerf8e96832003-08-07 19:12:24 +0000309 const CodeGenTarget &getTarget() const { return Target; }
310 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000311 const NodeType &getNodeType(Record *R) const {
312 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
313 assert(I != NodeTypes.end() && "Unknown node type!");
314 return I->second;
315 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000316
Chris Lattner5709e512003-08-07 21:02:56 +0000317 /// getPattern - return the pattern corresponding to the specified record, or
318 /// null if there is none.
319 Pattern *getPattern(Record *R) const {
320 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
321 return I != Patterns.end() ? I->second : 0;
322 }
323
324 /// ReadNonterminal - This method parses the specified record as a
325 /// nonterminal, but only if it hasn't been read in already.
326 Pattern *ReadNonterminal(Record *R);
327
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000328 /// InstantiateNonterminal - This method takes the nonterminal specified by
329 /// NT, which should not be completely resolved, clones it, applies ResultTy
330 /// to its root, then runs the type inference stuff on it. This should
331 /// produce a newly resolved nonterminal, which we make a record for and
332 /// return. To be extra fancy and efficient, this only makes one clone for
333 /// each type it is instantiated with.
334 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
335
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000336private:
Chris Lattneree858d22003-08-07 20:42:23 +0000337 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
338 // turning them into the more accessible NodeTypes data structure.
339 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000340
Chris Lattneree858d22003-08-07 20:42:23 +0000341 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
342 // pattern database.
343 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000344
Chris Lattneree858d22003-08-07 20:42:23 +0000345 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000346 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000347 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000348
Chris Lattneree858d22003-08-07 20:42:23 +0000349 // ReadExpanderPatterns - Read in all of the expanded patterns.
350 void ReadExpanderPatterns();
351
352 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
353 // information from the context that they are used in.
354 void InstantiateNonterminals();
Chris Lattner6dafd392003-08-08 16:30:10 +0000355
356 // CalculateComputableValues - Fill in the ComputableValues map through
357 // analysis of the patterns we are playing with.
358 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000359
360 // EmitMatchCosters - Given a list of patterns, which all have the same root
361 // pattern operator, emit an efficient decision tree to decide which one to
362 // pick. This is structured this way to avoid reevaluations of non-obvious
363 // subexpressions.
364 void EmitMatchCosters(std::ostream &OS,
365 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
366 const std::string &VarPrefix, unsigned Indent);
Chris Lattner053a2052003-08-10 23:51:52 +0000367
368 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
369 /// process for the expander pattern P. This argument may be referencing some
370 /// values defined in P, or may just be physical register references or
371 /// something like that. If PrintArg is true, we are printing out arguments
372 /// to the BuildMI call. If it is false, we are printing the result register
373 /// name.
374 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
375 Record *ArgDecl, Pattern *P,
376 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000377};
378
379#endif