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"
26 class TreePatternNode;
29 /// SDTypeConstraint - This is a discriminated union of constraints,
30 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
31 struct SDTypeConstraint {
32 SDTypeConstraint(Record *R);
34 unsigned OperandNo; // The operand # this constraint applies to.
36 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
39 union { // The discriminated union.
44 unsigned OtherOperandNum;
47 unsigned OtherOperandNum;
48 } SDTCisVTSmallerThanOp_Info;
51 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
52 /// constraint to the nodes operands. This returns true if it makes a
53 /// change, false otherwise. If a type contradiction is found, throw an
55 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
56 TreePattern &TP) const;
58 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
59 /// N, which has NumResults results.
60 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
61 unsigned NumResults) const;
64 /// SDNodeInfo - One of these records is created for each SDNode instance in
65 /// the target .td file. This represents the various dag nodes we will be
70 std::string SDClassName;
73 std::vector<SDTypeConstraint> TypeConstraints;
75 SDNodeInfo(Record *R); // Parse the specified record.
77 unsigned getNumResults() const { return NumResults; }
78 int getNumOperands() const { return NumOperands; }
79 Record *getRecord() const { return Def; }
80 const std::string &getEnumName() const { return EnumName; }
81 const std::string &getSDClassName() const { return SDClassName; }
83 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
84 return TypeConstraints;
87 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
88 /// constraints for this node to the operands of the node. This returns
89 /// true if it makes a change, false otherwise. If a type contradiction is
90 /// found, throw an exception.
91 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
92 bool MadeChange = false;
93 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
94 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
99 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
100 /// patterns), and as such should be ref counted. We currently just leak all
101 /// TreePatternNode objects!
102 class TreePatternNode {
103 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
104 /// been determined yet.
107 /// Operator - The Record for the operator if this is an interior node (not
111 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
115 /// Name - The name given to this node with the :$foo notation.
119 /// PredicateFn - The predicate function to execute on this node to check
120 /// for a match. If this string is empty, no predicate is involved.
121 std::string PredicateFn;
123 std::vector<TreePatternNode*> Children;
125 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
126 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
127 TreePatternNode(Init *val) // leaf ctor
128 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
131 const std::string &getName() const { return Name; }
132 void setName(const std::string &N) { Name = N; }
134 bool isLeaf() const { return Val != 0; }
135 bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; }
136 MVT::ValueType getType() const { return Ty; }
137 void setType(MVT::ValueType VT) { Ty = VT; }
139 Init *getLeafValue() const { assert(isLeaf()); return Val; }
140 Record *getOperator() const { assert(!isLeaf()); return Operator; }
142 unsigned getNumChildren() const { return Children.size(); }
143 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
144 void setChild(unsigned i, TreePatternNode *N) {
148 const std::string &getPredicateFn() const { return PredicateFn; }
149 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
151 void print(std::ostream &OS) const;
154 public: // Higher level manipulation routines.
156 /// clone - Return a new copy of this tree.
158 TreePatternNode *clone() const;
160 /// SubstituteFormalArguments - Replace the formal arguments in this tree
161 /// with actual values specified by ArgMap.
162 void SubstituteFormalArguments(std::map<std::string,
163 TreePatternNode*> &ArgMap);
165 /// InlinePatternFragments - If this pattern refers to any pattern
166 /// fragments, inline them into place, giving us a pattern without any
167 /// PatFrag references.
168 TreePatternNode *InlinePatternFragments(TreePattern &TP);
170 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
171 /// this node and its children in the tree. This returns true if it makes a
172 /// change, false otherwise. If a type contradiction is found, throw an
174 bool ApplyTypeConstraints(TreePattern &TP);
176 /// UpdateNodeType - Set the node type of N to VT if VT contains
177 /// information. If N already contains a conflicting type, then throw an
178 /// exception. This returns true if any information was updated.
180 bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP);
182 /// ContainsUnresolvedType - Return true if this tree contains any
183 /// unresolved types.
184 bool ContainsUnresolvedType() const {
185 if (Ty == MVT::LAST_VALUETYPE) return true;
186 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
187 if (getChild(i)->ContainsUnresolvedType()) return true;
193 /// TreePattern - Represent a pattern, used for instructions, pattern
197 /// Trees - The list of pattern trees which corresponds to this pattern.
198 /// Note that PatFrag's only have a single tree.
200 std::vector<TreePatternNode*> Trees;
202 /// TheRecord - The actual TableGen record corresponding to this pattern.
206 /// Args - This is a list of all of the arguments to this pattern (for
207 /// PatFrag patterns), which are the 'node' markers in this pattern.
208 std::vector<std::string> Args;
210 /// ISE - the DAG isel emitter coordinating this madness.
215 /// TreePattern constructor - Parse the specified DagInits into the
217 TreePattern(Record *TheRec,
218 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
220 /// getTrees - Return the tree patterns which corresponds to this pattern.
222 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
223 unsigned getNumTrees() const { return Trees.size(); }
224 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
225 TreePatternNode *getOnlyTree() const {
226 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
230 /// getRecord - Return the actual TableGen record corresponding to this
233 Record *getRecord() const { return TheRecord; }
235 unsigned getNumArgs() const { return Args.size(); }
236 const std::string &getArgName(unsigned i) const {
237 assert(i < Args.size() && "Argument reference out of range!");
240 std::vector<std::string> &getArgList() { return Args; }
242 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
244 /// InlinePatternFragments - If this pattern refers to any pattern
245 /// fragments, inline them into place, giving us a pattern without any
246 /// PatFrag references.
247 void InlinePatternFragments() {
248 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
249 Trees[i] = Trees[i]->InlinePatternFragments(*this);
252 /// InferAllTypes - Infer/propagate as many types throughout the expression
253 /// patterns as possible. Return true if all types are infered, false
254 /// otherwise. Throw an exception if a type contradiction is found.
255 bool InferAllTypes();
257 /// error - Throw an exception, prefixing it with information about this
259 void error(const std::string &Msg) const;
261 void print(std::ostream &OS) const;
265 MVT::ValueType getIntrinsicType(Record *R) const;
266 TreePatternNode *ParseTreePattern(DagInit *DI);
271 /// InstrSelectorEmitter - The top-level class which coordinates construction
272 /// and emission of the instruction selector.
274 class DAGISelEmitter : public TableGenBackend {
275 RecordKeeper &Records;
276 CodeGenTarget Target;
278 std::map<Record*, SDNodeInfo> SDNodes;
279 std::map<Record*, TreePattern*> PatternFragments;
280 std::vector<TreePattern*> Instructions;
282 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
284 // run - Output the isel, returning true on failure.
285 void run(std::ostream &OS);
287 const SDNodeInfo &getSDNodeInfo(Record *R) const {
288 assert(SDNodes.count(R) && "Unknown node!");
289 return SDNodes.find(R)->second;
292 TreePattern *getPatternFragment(Record *R) const {
293 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
294 return PatternFragments.find(R)->second;
298 void ParseNodeInfo();
299 void ParseAndResolvePatternFragments(std::ostream &OS);
300 void ParseAndResolveInstructions();
301 void EmitInstructionSelector(std::ostream &OS);
304 } // End llvm namespace