1 //===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
6 //===----------------------------------------------------------------------===//
8 #ifndef INSTRSELECTOR_EMITTER_H
9 #define INSTRSELECTOR_EMITTER_H
11 #include "TableGenBackend.h"
12 #include "CodeGenWrappers.h"
17 class InstrSelectorEmitter;
19 /// NodeType - Represents Information parsed from the DagNode entries.
23 // Both argument and return types...
24 Val, // A non-void type
25 Arg0, // Value matches the type of Arg0
26 Ptr, // Tree node is the type of the target pointer
29 Void, // Tree node always returns void
32 ArgResultTypes ResultType;
33 std::vector<ArgResultTypes> ArgTypes;
35 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
39 NodeType() : ResultType(Val) {}
40 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
42 static ArgResultTypes Translate(Record *R);
47 /// TreePatternNode - Represent a node of the tree patterns.
49 class TreePatternNode {
50 /// Operator - The operation that this node represents... this is null if this
54 /// Type - The inferred value type...
58 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
60 std::vector<TreePatternNode*> Children;
62 /// Value - If this node is a leaf, this indicates what the thing is.
66 TreePatternNode(Record *o, const std::vector<TreePatternNode*> &c)
67 : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
68 TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
70 Record *getOperator() const {
71 assert(Operator && "This is a leaf node!");
74 MVT::ValueType getType() const { return Type; }
75 void setType(MVT::ValueType T) { Type = T; }
77 bool isLeaf() const { return Operator == 0; }
79 const std::vector<TreePatternNode*> &getChildren() const {
80 assert(Operator != 0 && "This is a leaf node!");
83 unsigned getNumChildren() const { return Children.size(); }
84 TreePatternNode *getChild(unsigned c) const {
85 assert(c < Children.size() && "Child access out of range!");
86 return getChildren()[c];
89 Init *getValue() const {
90 assert(Operator == 0 && "This is not a leaf node!");
94 /// getValueRecord - Returns the value of this tree node as a record. For now
95 /// we only allow DefInit's as our leaf values, so this is used.
96 Record *getValueRecord() const;
98 /// clone - Make a copy of this tree and all of its children.
100 TreePatternNode *clone() const;
104 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
105 /// are not themselves completely resolved, clone the nonterminal and resolve
106 /// it with the using context we provide.
107 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
109 /// UpdateNodeType - Set the node type of N to VT if VT contains information.
110 /// If N already contains a conflicting type, then throw an exception. This
111 /// returns true if any information was updated.
113 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
116 std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
120 /// Pattern - Represent a pattern of one form or another. Currently, three
121 /// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
125 Nonterminal, Instruction, Expander
128 /// PTy - The type of pattern this is.
132 /// Tree - The tree pattern which corresponds to this pattern. Note that if
133 /// there was a (set) node on the outside level that it has been stripped off.
135 TreePatternNode *Tree;
137 /// Result - If this is an instruction or expander pattern, this is the
138 /// register result, specified with a (set) in the pattern.
142 /// TheRecord - The actual TableGen record corresponding to this pattern.
146 /// Resolved - This is true of the pattern is useful in practice. In
147 /// particular, some non-terminals will have non-resolvable types. When a
148 /// user of the non-terminal is later found, they will have inferred a type
149 /// for the result of the non-terminal, which cause a clone of an unresolved
150 /// nonterminal to be made which is "resolved".
154 /// ISE - the instruction selector emitter coordinating this madness.
156 InstrSelectorEmitter &ISE;
159 /// Pattern constructor - Parse the specified DagInitializer into the current
161 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
162 InstrSelectorEmitter &ise);
164 /// Pattern - Constructor used for cloning nonterminal patterns
165 Pattern(TreePatternNode *tree, Record *rec, bool res,
166 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
167 TheRecord(rec), Resolved(res), ISE(ise){}
169 /// getPatternType - Return what flavor of Record this pattern originated from
171 PatternType getPatternType() const { return PTy; }
173 /// getTree - Return the tree pattern which corresponds to this pattern.
175 TreePatternNode *getTree() const { return Tree; }
177 Record *getResult() const { return Result; }
179 /// getRecord - Return the actual TableGen record corresponding to this
182 Record *getRecord() const { return TheRecord; }
184 bool isResolved() const { return Resolved; }
186 /// InferAllTypes - Runs the type inference engine on the current pattern,
187 /// stopping when nothing can be inferred, then updating the Resolved field.
188 void InferAllTypes();
190 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
191 /// are not themselves completely resolved, clone the nonterminal and resolve
192 /// it with the using context we provide.
193 void InstantiateNonterminals() {
194 Tree->InstantiateNonterminals(ISE);
197 /// clone - This method is used to make an exact copy of the current pattern,
198 /// then change the "TheRecord" instance variable to the specified record.
200 Pattern *clone(Record *R) const;
202 /// error - Throw an exception, prefixing it with information about this
204 void error(const std::string &Msg) const;
206 /// getSlotName - If this is a leaf node, return the slot name that the
207 /// operand will update.
208 std::string getSlotName() const;
209 static std::string getSlotName(Record *R);
214 MVT::ValueType getIntrinsicType(Record *R) const;
215 TreePatternNode *ParseTreePattern(DagInit *DI);
216 bool InferTypes(TreePatternNode *N, bool &MadeChange);
219 std::ostream &operator<<(std::ostream &OS, const Pattern &P);
222 /// PatternOrganizer - This class represents all of the patterns which are
223 /// useful for the instruction selector, neatly catagorized in a hierarchical
225 struct PatternOrganizer {
226 /// PatternsForNode - The list of patterns which can produce a value of a
227 /// particular slot type, given a particular root node in the tree. All of
228 /// the patterns in this vector produce the same value type and have the same
230 typedef std::vector<Pattern*> PatternsForNode;
232 /// NodesForSlot - This map keeps track of all of the root DAG nodes which can
233 /// lead to the production of a value for this slot. All of the patterns in
234 /// this data structure produces values of the same slot.
235 typedef std::map<Record*, PatternsForNode> NodesForSlot;
237 /// AllPatterns - This data structure contains all patterns in the instruction
239 std::map<std::string, NodesForSlot> AllPatterns;
241 // Forwarding functions...
242 typedef std::map<std::string, NodesForSlot>::iterator iterator;
243 iterator begin() { return AllPatterns.begin(); }
244 iterator end() { return AllPatterns.end(); }
247 /// addPattern - Add the specified pattern to the appropriate location in the
249 void addPattern(Pattern *P);
253 /// InstrSelectorEmitter - The top-level class which coordinates construction
254 /// and emission of the instruction selector.
256 class InstrSelectorEmitter : public TableGenBackend {
257 RecordKeeper &Records;
258 CodeGenTarget Target;
260 std::map<Record*, NodeType> NodeTypes;
262 /// Patterns - a list of all of the patterns defined by the target description
264 std::map<Record*, Pattern*> Patterns;
266 /// InstantiatedNTs - A data structure to keep track of which nonterminals
267 /// have been instantiated already...
269 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
271 /// ComputableValues - This map indicates which patterns can be used to
272 /// generate a value that is used by the selector. The keys of this map
273 /// implicitly define the values that are used by the selector.
275 PatternOrganizer ComputableValues;
278 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
280 // run - Output the instruction set description, returning true on failure.
281 void run(std::ostream &OS);
283 const CodeGenTarget &getTarget() const { return Target; }
284 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
285 const NodeType &getNodeType(Record *R) const {
286 std::map<Record*, NodeType>::const_iterator I = NodeTypes.find(R);
287 assert(I != NodeTypes.end() && "Unknown node type!");
291 /// getPattern - return the pattern corresponding to the specified record, or
292 /// null if there is none.
293 Pattern *getPattern(Record *R) const {
294 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
295 return I != Patterns.end() ? I->second : 0;
298 /// ReadNonterminal - This method parses the specified record as a
299 /// nonterminal, but only if it hasn't been read in already.
300 Pattern *ReadNonterminal(Record *R);
302 /// InstantiateNonterminal - This method takes the nonterminal specified by
303 /// NT, which should not be completely resolved, clones it, applies ResultTy
304 /// to its root, then runs the type inference stuff on it. This should
305 /// produce a newly resolved nonterminal, which we make a record for and
306 /// return. To be extra fancy and efficient, this only makes one clone for
307 /// each type it is instantiated with.
308 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
311 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
312 // turning them into the more accessible NodeTypes data structure.
313 void ReadNodeTypes();
315 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
317 void ReadNonterminals();
319 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
320 // process those with a useful Pattern field.
321 void ReadInstructionPatterns();
323 // ReadExpanderPatterns - Read in all of the expanded patterns.
324 void ReadExpanderPatterns();
326 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
327 // information from the context that they are used in.
328 void InstantiateNonterminals();
330 // CalculateComputableValues - Fill in the ComputableValues map through
331 // analysis of the patterns we are playing with.
332 void CalculateComputableValues();
334 // EmitMatchCosters - Given a list of patterns, which all have the same root
335 // pattern operator, emit an efficient decision tree to decide which one to
336 // pick. This is structured this way to avoid reevaluations of non-obvious
338 void EmitMatchCosters(std::ostream &OS,
339 const std::vector<std::pair<Pattern*, TreePatternNode*> > &Patterns,
340 const std::string &VarPrefix, unsigned Indent);