blob: fbf31f26299cc2ba4c0032020375b714b7ad82b1 [file] [log] [blame]
Chris Lattnerfaca5ab2003-08-06 05:42:05 +00001//===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattnerfaca5ab2003-08-06 05:42:05 +00009//
10// This tablegen backend is responsible for emitting a description of the target
11// instruction set for the code generator.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef INSTRSELECTOR_EMITTER_H
16#define INSTRSELECTOR_EMITTER_H
17
18#include "TableGenBackend.h"
Chris Lattner803a5f62004-08-01 04:04:35 +000019#include "CodeGenTarget.h"
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000020#include <vector>
21#include <map>
John Criswell43bf32e2003-08-20 22:07:45 +000022#include <cassert>
23
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner018c9e42003-08-07 05:40:14 +000026class DagInit;
Chris Lattner1fca5ff2004-10-27 16:14:51 +000027struct Init;
Chris Lattnerf8e96832003-08-07 19:12:24 +000028class InstrSelectorEmitter;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000029
Chris Lattnerf8e96832003-08-07 19:12:24 +000030/// NodeType - Represents Information parsed from the DagNode entries.
31///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000032struct NodeType {
33 enum ArgResultTypes {
Chris Lattner6b666e82003-08-12 04:56:42 +000034 Any, // No constraint on type
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000035 Val, // A non-void type
36 Arg0, // Value matches the type of Arg0
Chris Lattner88118bf2003-08-11 20:25:52 +000037 Arg1, // Value matches the type of Arg1
Chris Lattner2787d1a2003-08-06 06:16:35 +000038 Ptr, // Tree node is the type of the target pointer
Chris Lattnerc12a6142003-08-12 04:28:21 +000039 I8, // Always bool
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000040 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000041 };
42
43 ArgResultTypes ResultType;
44 std::vector<ArgResultTypes> ArgTypes;
45
46 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
47 AT.swap(ArgTypes);
48 }
49
50 NodeType() : ResultType(Val) {}
51 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
52
53 static ArgResultTypes Translate(Record *R);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000054};
55
Chris Lattnerf8e96832003-08-07 19:12:24 +000056
57
58/// TreePatternNode - Represent a node of the tree patterns.
59///
Chris Lattner018c9e42003-08-07 05:40:14 +000060class TreePatternNode {
61 /// Operator - The operation that this node represents... this is null if this
62 /// is a leaf.
63 Record *Operator;
64
65 /// Type - The inferred value type...
Chris Lattnerf8e96832003-08-07 19:12:24 +000066 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000067 MVT::ValueType Type;
68
69 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
70 /// that we contain.
Chris Lattner053a2052003-08-10 23:51:52 +000071 std::vector<std::pair<TreePatternNode*, std::string> > Children;
Chris Lattner018c9e42003-08-07 05:40:14 +000072
73 /// Value - If this node is a leaf, this indicates what the thing is.
Chris Lattnerf8e96832003-08-07 19:12:24 +000074 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000075 Init *Value;
76public:
Chris Lattner053a2052003-08-10 23:51:52 +000077 TreePatternNode(Record *o, const std::vector<std::pair<TreePatternNode*,
78 std::string> > &c)
Chris Lattner018c9e42003-08-07 05:40:14 +000079 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
80 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
81
Chris Lattner6dafd392003-08-08 16:30:10 +000082 Record *getOperator() const {
83 assert(Operator && "This is a leaf node!");
84 return Operator;
85 }
Chris Lattner018c9e42003-08-07 05:40:14 +000086 MVT::ValueType getType() const { return Type; }
87 void setType(MVT::ValueType T) { Type = T; }
88
89 bool isLeaf() const { return Operator == 0; }
90
Chris Lattner955c1be2003-08-08 22:29:23 +000091 unsigned getNumChildren() const { return Children.size(); }
Chris Lattnerf8e96832003-08-07 19:12:24 +000092 TreePatternNode *getChild(unsigned c) const {
Chris Lattner053a2052003-08-10 23:51:52 +000093 assert(Operator != 0 && "This is a leaf node!");
Chris Lattnerf8e96832003-08-07 19:12:24 +000094 assert(c < Children.size() && "Child access out of range!");
Chris Lattner053a2052003-08-10 23:51:52 +000095 return Children[c].first;
96 }
97 const std::string &getChildName(unsigned c) const {
98 assert(Operator != 0 && "This is a leaf node!");
99 assert(c < Children.size() && "Child access out of range!");
100 return Children[c].second;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000101 }
102
Chris Lattner018c9e42003-08-07 05:40:14 +0000103 Init *getValue() const {
104 assert(Operator == 0 && "This is not a leaf node!");
105 return Value;
106 }
107
Chris Lattner955c1be2003-08-08 22:29:23 +0000108 /// getValueRecord - Returns the value of this tree node as a record. For now
109 /// we only allow DefInit's as our leaf values, so this is used.
110 Record *getValueRecord() const;
111
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000112 /// clone - Make a copy of this tree and all of its children.
113 ///
114 TreePatternNode *clone() const;
115
Chris Lattner018c9e42003-08-07 05:40:14 +0000116 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000117
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000118 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
119 /// are not themselves completely resolved, clone the nonterminal and resolve
120 /// it with the using context we provide.
121 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000122
Chris Lattner955c1be2003-08-08 22:29:23 +0000123 /// UpdateNodeType - Set the node type of N to VT if VT contains information.
124 /// If N already contains a conflicting type, then throw an exception. This
125 /// returns true if any information was updated.
126 ///
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000127 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000128};
129
130std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
131
132
133
Chris Lattnerf8e96832003-08-07 19:12:24 +0000134/// Pattern - Represent a pattern of one form or another. Currently, three
135/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
136///
137struct Pattern {
138 enum PatternType {
139 Nonterminal, Instruction, Expander
140 };
141private:
142 /// PTy - The type of pattern this is.
143 ///
144 PatternType PTy;
145
146 /// Tree - The tree pattern which corresponds to this pattern. Note that if
147 /// there was a (set) node on the outside level that it has been stripped off.
148 ///
149 TreePatternNode *Tree;
Misha Brukman3da94ae2005-04-22 00:00:37 +0000150
Chris Lattnerf8e96832003-08-07 19:12:24 +0000151 /// Result - If this is an instruction or expander pattern, this is the
152 /// register result, specified with a (set) in the pattern.
153 ///
Chris Lattnerabb215e2003-08-11 21:28:59 +0000154 std::string ResultName; // The name of the result value...
155 TreePatternNode *ResultNode; // The leaf node for the result register...
Chris Lattnerf8e96832003-08-07 19:12:24 +0000156
157 /// TheRecord - The actual TableGen record corresponding to this pattern.
158 ///
159 Record *TheRecord;
160
161 /// Resolved - This is true of the pattern is useful in practice. In
162 /// particular, some non-terminals will have non-resolvable types. When a
163 /// user of the non-terminal is later found, they will have inferred a type
164 /// for the result of the non-terminal, which cause a clone of an unresolved
165 /// nonterminal to be made which is "resolved".
166 ///
167 bool Resolved;
168
Chris Lattner053a2052003-08-10 23:51:52 +0000169 /// Args - This is a list of all of the arguments to this pattern, which are
170 /// the non-void leaf nodes in this pattern.
171 std::vector<std::pair<TreePatternNode*, std::string> > Args;
172
Chris Lattnerf8e96832003-08-07 19:12:24 +0000173 /// ISE - the instruction selector emitter coordinating this madness.
174 ///
175 InstrSelectorEmitter &ISE;
176public:
177
178 /// Pattern constructor - Parse the specified DagInitializer into the current
179 /// record.
180 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
181 InstrSelectorEmitter &ise);
182
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000183 /// Pattern - Constructor used for cloning nonterminal patterns
184 Pattern(TreePatternNode *tree, Record *rec, bool res,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000185 InstrSelectorEmitter &ise)
186 : PTy(Nonterminal), Tree(tree), ResultNode(0), TheRecord(rec),
187 Resolved(res), ISE(ise) {
Chris Lattner053a2052003-08-10 23:51:52 +0000188 calculateArgs(Tree, "");
189 }
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000190
Chris Lattnerf8e96832003-08-07 19:12:24 +0000191 /// getPatternType - Return what flavor of Record this pattern originated from
192 ///
193 PatternType getPatternType() const { return PTy; }
194
195 /// getTree - Return the tree pattern which corresponds to this pattern.
196 ///
197 TreePatternNode *getTree() const { return Tree; }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000198
Chris Lattnerabb215e2003-08-11 21:28:59 +0000199 Record *getResult() const {
200 return ResultNode ? ResultNode->getValueRecord() : 0;
201 }
Chris Lattner57fb6ab2003-08-11 20:32:02 +0000202 const std::string &getResultName() const { return ResultName; }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000203 TreePatternNode *getResultNode() const { return ResultNode; }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000204
205 /// getRecord - Return the actual TableGen record corresponding to this
206 /// pattern.
207 ///
208 Record *getRecord() const { return TheRecord; }
209
Chris Lattner053a2052003-08-10 23:51:52 +0000210 unsigned getNumArgs() const { return Args.size(); }
211 TreePatternNode *getArg(unsigned i) const {
212 assert(i < Args.size() && "Argument reference out of range!");
213 return Args[i].first;
214 }
215 Record *getArgRec(unsigned i) const {
216 return getArg(i)->getValueRecord();
217 }
Chris Lattnerabb215e2003-08-11 21:28:59 +0000218 Init *getArgVal(unsigned i) const {
219 return getArg(i)->getValue();
220 }
Chris Lattner053a2052003-08-10 23:51:52 +0000221 const std::string &getArgName(unsigned i) const {
222 assert(i < Args.size() && "Argument reference out of range!");
223 return Args[i].second;
224 }
225
Chris Lattnerf8e96832003-08-07 19:12:24 +0000226 bool isResolved() const { return Resolved; }
227
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000228 /// InferAllTypes - Runs the type inference engine on the current pattern,
229 /// stopping when nothing can be inferred, then updating the Resolved field.
230 void InferAllTypes();
231
232 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
233 /// are not themselves completely resolved, clone the nonterminal and resolve
234 /// it with the using context we provide.
235 void InstantiateNonterminals() {
236 Tree->InstantiateNonterminals(ISE);
237 }
238
239 /// clone - This method is used to make an exact copy of the current pattern,
240 /// then change the "TheRecord" instance variable to the specified record.
241 ///
242 Pattern *clone(Record *R) const;
243
244 /// error - Throw an exception, prefixing it with information about this
245 /// pattern.
246 void error(const std::string &Msg) const;
247
Chris Lattner955c1be2003-08-08 22:29:23 +0000248 /// getSlotName - If this is a leaf node, return the slot name that the
249 /// operand will update.
250 std::string getSlotName() const;
251 static std::string getSlotName(Record *R);
252
Chris Lattner9552b8c2003-08-10 19:50:51 +0000253 void dump() const;
254
Chris Lattnerf8e96832003-08-07 19:12:24 +0000255private:
Chris Lattner053a2052003-08-10 23:51:52 +0000256 void calculateArgs(TreePatternNode *N, const std::string &Name);
Chris Lattner5709e512003-08-07 21:02:56 +0000257 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000258 TreePatternNode *ParseTreePattern(DagInit *DI);
259 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000260};
261
262std::ostream &operator<<(std::ostream &OS, const Pattern &P);
263
264
Chris Lattner6dafd392003-08-08 16:30:10 +0000265/// PatternOrganizer - This class represents all of the patterns which are
266/// useful for the instruction selector, neatly catagorized in a hierarchical
267/// structure.
268struct PatternOrganizer {
269 /// PatternsForNode - The list of patterns which can produce a value of a
270 /// particular slot type, given a particular root node in the tree. All of
271 /// the patterns in this vector produce the same value type and have the same
272 /// root DAG node.
273 typedef std::vector<Pattern*> PatternsForNode;
274
275 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
276 /// lead to the production of a value for this slot. All of the patterns in
277 /// this data structure produces values of the same slot.
278 typedef std::map<Record*, PatternsForNode> NodesForSlot;
279
280 /// AllPatterns - This data structure contains all patterns in the instruction
281 /// selector.
282 std::map<std::string, NodesForSlot> AllPatterns;
283
284 // Forwarding functions...
285 typedef std::map<std::string, NodesForSlot>::iterator iterator;
286 iterator begin() { return AllPatterns.begin(); }
287 iterator end() { return AllPatterns.end(); }
288
289
290 /// addPattern - Add the specified pattern to the appropriate location in the
291 /// collection.
292 void addPattern(Pattern *P);
293};
294
Chris Lattnerf8e96832003-08-07 19:12:24 +0000295
296/// InstrSelectorEmitter - The top-level class which coordinates construction
297/// and emission of the instruction selector.
298///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000299class InstrSelectorEmitter : public TableGenBackend {
300 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000301 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000302
303 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000304
305 /// Patterns - a list of all of the patterns defined by the target description
306 ///
307 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000308
309 /// InstantiatedNTs - A data structure to keep track of which nonterminals
310 /// have been instantiated already...
311 ///
312 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattner6dafd392003-08-08 16:30:10 +0000313
314 /// ComputableValues - This map indicates which patterns can be used to
315 /// generate a value that is used by the selector. The keys of this map
316 /// implicitly define the values that are used by the selector.
317 ///
318 PatternOrganizer ComputableValues;
319
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000320public:
321 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
Misha Brukman3da94ae2005-04-22 00:00:37 +0000322
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000323 // run - Output the instruction set description, returning true on failure.
324 void run(std::ostream &OS);
325
Chris Lattnerf8e96832003-08-07 19:12:24 +0000326 const CodeGenTarget &getTarget() const { return Target; }
327 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
Chris Lattner955c1be2003-08-08 22:29:23 +0000328 const NodeType &getNodeType(Record *R) const {
329 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
330 assert(I != NodeTypes.end() && "Unknown node type!");
331 return I->second;
332 }
Chris Lattnerf8e96832003-08-07 19:12:24 +0000333
Chris Lattner5709e512003-08-07 21:02:56 +0000334 /// getPattern - return the pattern corresponding to the specified record, or
335 /// null if there is none.
336 Pattern *getPattern(Record *R) const {
337 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
338 return I != Patterns.end() ? I->second : 0;
339 }
340
341 /// ReadNonterminal - This method parses the specified record as a
342 /// nonterminal, but only if it hasn't been read in already.
343 Pattern *ReadNonterminal(Record *R);
344
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000345 /// InstantiateNonterminal - This method takes the nonterminal specified by
346 /// NT, which should not be completely resolved, clones it, applies ResultTy
347 /// to its root, then runs the type inference stuff on it. This should
348 /// produce a newly resolved nonterminal, which we make a record for and
349 /// return. To be extra fancy and efficient, this only makes one clone for
350 /// each type it is instantiated with.
351 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
352
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000353private:
Chris Lattneree858d22003-08-07 20:42:23 +0000354 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
355 // turning them into the more accessible NodeTypes data structure.
356 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000357
Chris Lattneree858d22003-08-07 20:42:23 +0000358 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
359 // pattern database.
360 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000361
Chris Lattneree858d22003-08-07 20:42:23 +0000362 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000363 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000364 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000365
Chris Lattneree858d22003-08-07 20:42:23 +0000366 // ReadExpanderPatterns - Read in all of the expanded patterns.
367 void ReadExpanderPatterns();
368
369 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
370 // information from the context that they are used in.
371 void InstantiateNonterminals();
Misha Brukman3da94ae2005-04-22 00:00:37 +0000372
Chris Lattner6dafd392003-08-08 16:30:10 +0000373 // CalculateComputableValues - Fill in the ComputableValues map through
374 // analysis of the patterns we are playing with.
375 void CalculateComputableValues();
Chris Lattner955c1be2003-08-08 22:29:23 +0000376
377 // EmitMatchCosters - Given a list of patterns, which all have the same root
378 // pattern operator, emit an efficient decision tree to decide which one to
379 // pick. This is structured this way to avoid reevaluations of non-obvious
380 // subexpressions.
381 void EmitMatchCosters(std::ostream &OS,
382 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
383 const std::string &VarPrefix, unsigned Indent);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000384
Chris Lattner053a2052003-08-10 23:51:52 +0000385 /// PrintExpanderOperand - Print out Arg as part of the instruction emission
386 /// process for the expander pattern P. This argument may be referencing some
387 /// values defined in P, or may just be physical register references or
388 /// something like that. If PrintArg is true, we are printing out arguments
389 /// to the BuildMI call. If it is false, we are printing the result register
390 /// name.
391 void PrintExpanderOperand(Init *Arg, const std::string &NameVar,
Chris Lattnerabb215e2003-08-11 21:28:59 +0000392 TreePatternNode *ArgDecl, Pattern *P,
Chris Lattner053a2052003-08-10 23:51:52 +0000393 bool PrintArg, std::ostream &OS);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000394};
395
Brian Gaeked0fde302003-11-11 22:41:34 +0000396} // End llvm namespace
397
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000398#endif