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 /// SDTypeConstraint - This is a discriminated union of constraints,
28 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
29 struct SDTypeConstraint {
30 SDTypeConstraint(Record *R);
32 unsigned OperandNo; // The operand # this constraint applies to.
34 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
37 union { // The discriminated union.
42 unsigned OtherOperandNum;
45 unsigned OtherOperandNum;
46 } SDTCisVTSmallerThanOp_Info;
50 /// SDNodeInfo - One of these records is created for each SDNode instance in
51 /// the target .td file. This represents the various dag nodes we will be
56 std::string SDClassName;
57 int NumResults, NumOperands;
58 std::vector<SDTypeConstraint> TypeConstraints;
60 SDNodeInfo(Record *R); // Parse the specified record.
62 int getNumResults() const { return NumResults; }
63 int getNumOperands() const { return NumOperands; }
64 Record *getRecord() const { return Def; }
65 const std::string &getEnumName() const { return EnumName; }
66 const std::string &getSDClassName() const { return SDClassName; }
68 const std::vector<SDTypeConstraint> &getTypeConstraints() {
69 return TypeConstraints;
73 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
74 /// patterns), and as such should be ref counted. We currently just leak all
75 /// TreePatternNode objects!
76 class TreePatternNode {
77 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
78 /// been determined yet.
81 /// Operator - The Record for the operator if this is an interior node (not
85 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
89 /// Name - The name given to this node with the :$foo notation.
93 /// PredicateFn - The predicate function to execute on this node to check
94 /// for a match. If this string is empty, no predicate is involved.
95 std::string PredicateFn;
97 std::vector<TreePatternNode*> Children;
99 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
100 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
101 TreePatternNode(Init *val) // leaf ctor
102 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
105 const std::string &getName() const { return Name; }
106 void setName(const std::string &N) { Name = N; }
108 bool isLeaf() const { return Val != 0; }
109 MVT::ValueType getType() const { return Ty; }
110 void setType(MVT::ValueType VT) { Ty = VT; }
112 Init *getLeafValue() const { assert(isLeaf()); return Val; }
113 Record *getOperator() const { assert(!isLeaf()); return Operator; }
115 unsigned getNumChildren() const { return Children.size(); }
116 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
117 void setChild(unsigned i, TreePatternNode *N) {
121 const std::string &getPredicateFn() const { return PredicateFn; }
122 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
124 void print(std::ostream &OS) const;
127 public: // Higher level manipulation routines.
129 /// clone - Return a new copy of this tree.
131 TreePatternNode *clone() const;
133 void SubstituteFormalArguments(std::map<std::string,
134 TreePatternNode*> &ArgMap);
136 /// InlinePatternFragments - If this pattern refers to any pattern
137 /// fragments, inline them into place, giving us a pattern without any
138 /// PatFrag references.
139 TreePatternNode *InlinePatternFragments(TreePattern &TP);
144 /// TreePattern - Represent a pattern of one form or another. Currently, two
145 /// types of patterns are possible: Instructions and PatFrags.
153 /// PTy - The type of pattern this is.
157 /// Trees - The list of pattern trees which corresponds to this pattern.
158 /// Note that PatFrag's only have a single tree.
160 std::vector<TreePatternNode*> Trees;
162 /// TheRecord - The actual TableGen record corresponding to this pattern.
166 /// Args - This is a list of all of the arguments to this pattern (for
167 /// PatFrag patterns), which are the 'node' markers in this pattern.
168 std::vector<std::string> Args;
170 /// ISE - the DAG isel emitter coordinating this madness.
175 /// TreePattern constructor - Parse the specified DagInits into the
177 TreePattern(PatternType pty, Record *TheRec,
178 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
180 /// getPatternType - Return what flavor of Record this pattern originated from
182 PatternType getPatternType() const { return PTy; }
184 /// getTrees - Return the tree patterns which corresponds to this pattern.
186 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
188 /// getRecord - Return the actual TableGen record corresponding to this
191 Record *getRecord() const { return TheRecord; }
193 unsigned getNumArgs() const { return Args.size(); }
194 const std::string &getArgName(unsigned i) const {
195 assert(i < Args.size() && "Argument reference out of range!");
199 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
201 /// InlinePatternFragments - If this pattern refers to any pattern
202 /// fragments, inline them into place, giving us a pattern without any
203 /// PatFrag references.
204 void InlinePatternFragments() {
205 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
206 Trees[i] = Trees[i]->InlinePatternFragments(*this);
209 /// error - Throw an exception, prefixing it with information about this
211 void error(const std::string &Msg) const;
213 void print(std::ostream &OS) const;
217 MVT::ValueType getIntrinsicType(Record *R) const;
218 TreePatternNode *ParseTreePattern(DagInit *DI);
223 /// InstrSelectorEmitter - The top-level class which coordinates construction
224 /// and emission of the instruction selector.
226 class DAGISelEmitter : public TableGenBackend {
227 RecordKeeper &Records;
228 CodeGenTarget Target;
230 std::map<Record*, SDNodeInfo> SDNodes;
231 std::map<Record*, TreePattern*> PatternFragments;
232 std::vector<TreePattern*> Instructions;
234 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
236 // run - Output the isel, returning true on failure.
237 void run(std::ostream &OS);
239 const SDNodeInfo &getSDNodeInfo(Record *R) const {
240 assert(SDNodes.count(R) && "Unknown node!");
241 return SDNodes.find(R)->second;
244 TreePattern *getPatternFragment(Record *R) const {
245 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
246 return PatternFragments.find(R)->second;
250 void ParseNodeInfo();
251 void ParseAndResolvePatternFragments(std::ostream &OS);
252 void ParseAndResolveInstructions();
253 void EmitInstructionSelector(std::ostream &OS);
256 } // End llvm namespace