1 //===- DAGISelEmitter.h - Generate an instruction selector ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This tablegen backend emits a DAG instruction selector.
12 //===----------------------------------------------------------------------===//
14 #ifndef DAGISEL_EMITTER_H
15 #define DAGISEL_EMITTER_H
17 #include "TableGenBackend.h"
18 #include "CodeGenTarget.h"
27 /// SDNodeInfo - One of these records is created for each SDNode instance in
28 /// the target .td file. This represents the various dag nodes we will be
33 std::string SDClassName;
35 SDNodeInfo(Record *R); // Parse the specified record.
37 Record *getRecord() const { return Def; }
38 const std::string &getEnumName() const { return EnumName; }
39 const std::string &getSDClassName() const { return SDClassName; }
42 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
43 /// patterns), and as such should be ref counted. We currently just leak all
44 /// TreePatternNode objects!
45 class TreePatternNode {
46 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
47 /// been determined yet.
50 /// Operator - The Record for the operator if this is an interior node (not
54 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
58 /// Name - The name given to this node with the :$foo notation.
62 /// PredicateFn - The predicate function to execute on this node to check
63 /// for a match. If this string is empty, no predicate is involved.
64 std::string PredicateFn;
66 std::vector<TreePatternNode*> Children;
68 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
69 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
70 TreePatternNode(Init *val) // leaf ctor
71 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
74 const std::string &getName() const { return Name; }
75 void setName(const std::string &N) { Name = N; }
77 bool isLeaf() const { return Val != 0; }
78 MVT::ValueType getType() const { return Ty; }
79 void setType(MVT::ValueType VT) { Ty = VT; }
81 Init *getLeafValue() const { assert(isLeaf()); return Val; }
82 Record *getOperator() const { assert(!isLeaf()); return Operator; }
84 unsigned getNumChildren() const { return Children.size(); }
85 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
86 void setChild(unsigned i, TreePatternNode *N) {
90 const std::string &getPredicateFn() const { return PredicateFn; }
91 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
93 void print(std::ostream &OS) const;
96 public: // Higher level manipulation routines.
98 /// clone - Return a new copy of this tree.
100 TreePatternNode *clone() const;
102 void SubstituteFormalArguments(std::map<std::string,
103 TreePatternNode*> &ArgMap);
105 /// InlinePatternFragments - If this pattern refers to any pattern
106 /// fragments, inline them into place, giving us a pattern without any
107 /// PatFrag references.
108 TreePatternNode *InlinePatternFragments(TreePattern &TP);
113 /// TreePattern - Represent a pattern of one form or another. Currently, two
114 /// types of patterns are possible: Instructions and PatFrags.
122 /// PTy - The type of pattern this is.
126 /// Trees - The list of pattern trees which corresponds to this pattern.
127 /// Note that PatFrag's only have a single tree.
129 std::vector<TreePatternNode*> Trees;
131 /// TheRecord - The actual TableGen record corresponding to this pattern.
135 /// Args - This is a list of all of the arguments to this pattern (for
136 /// PatFrag patterns), which are the 'node' markers in this pattern.
137 std::vector<std::string> Args;
139 /// ISE - the DAG isel emitter coordinating this madness.
144 /// TreePattern constructor - Parse the specified DagInits into the
146 TreePattern(PatternType pty, Record *TheRec,
147 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
149 /// getPatternType - Return what flavor of Record this pattern originated from
151 PatternType getPatternType() const { return PTy; }
153 /// getTrees - Return the tree patterns which corresponds to this pattern.
155 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
157 /// getRecord - Return the actual TableGen record corresponding to this
160 Record *getRecord() const { return TheRecord; }
162 unsigned getNumArgs() const { return Args.size(); }
163 const std::string &getArgName(unsigned i) const {
164 assert(i < Args.size() && "Argument reference out of range!");
168 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
170 /// InlinePatternFragments - If this pattern refers to any pattern
171 /// fragments, inline them into place, giving us a pattern without any
172 /// PatFrag references.
173 void InlinePatternFragments() {
174 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
175 Trees[i] = Trees[i]->InlinePatternFragments(*this);
178 /// error - Throw an exception, prefixing it with information about this
180 void error(const std::string &Msg) const;
182 void print(std::ostream &OS) const;
186 MVT::ValueType getIntrinsicType(Record *R) const;
187 TreePatternNode *ParseTreePattern(DagInit *DI);
192 /// InstrSelectorEmitter - The top-level class which coordinates construction
193 /// and emission of the instruction selector.
195 class DAGISelEmitter : public TableGenBackend {
196 RecordKeeper &Records;
197 CodeGenTarget Target;
199 std::map<Record*, SDNodeInfo> SDNodes;
200 std::map<Record*, TreePattern*> PatternFragments;
201 std::vector<TreePattern*> Instructions;
203 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
205 // run - Output the isel, returning true on failure.
206 void run(std::ostream &OS);
208 const SDNodeInfo &getSDNodeInfo(Record *R) const {
209 assert(SDNodes.count(R) && "Unknown node!");
210 return SDNodes.find(R)->second;
213 TreePattern *getPatternFragment(Record *R) const {
214 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
215 return PatternFragments.find(R)->second;
219 void ParseNodeInfo();
220 void ParseAndResolvePatternFragments(std::ostream &OS);
221 void ParseAndResolveInstructions();
222 void EmitInstructionSelector(std::ostream &OS);
225 } // End llvm namespace