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 /// TransformFn - The transformation function to execute on this node before
124 /// it can be substituted into the resulting instruction on a pattern match.
127 std::vector<TreePatternNode*> Children;
129 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
130 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), TransformFn(0),
132 TreePatternNode(Init *val) // leaf ctor
133 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val), TransformFn(0) {}
136 const std::string &getName() const { return Name; }
137 void setName(const std::string &N) { Name = N; }
139 bool isLeaf() const { return Val != 0; }
140 bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; }
141 MVT::ValueType getType() const { return Ty; }
142 void setType(MVT::ValueType VT) { Ty = VT; }
144 Init *getLeafValue() const { assert(isLeaf()); return Val; }
145 Record *getOperator() const { assert(!isLeaf()); return Operator; }
147 unsigned getNumChildren() const { return Children.size(); }
148 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
149 void setChild(unsigned i, TreePatternNode *N) {
153 const std::string &getPredicateFn() const { return PredicateFn; }
154 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
156 Record *getTransformFn() const { return TransformFn; }
157 void setTransformFn(Record *Fn) { TransformFn = Fn; }
159 void print(std::ostream &OS) const;
162 public: // Higher level manipulation routines.
164 /// clone - Return a new copy of this tree.
166 TreePatternNode *clone() const;
168 /// SubstituteFormalArguments - Replace the formal arguments in this tree
169 /// with actual values specified by ArgMap.
170 void SubstituteFormalArguments(std::map<std::string,
171 TreePatternNode*> &ArgMap);
173 /// InlinePatternFragments - If this pattern refers to any pattern
174 /// fragments, inline them into place, giving us a pattern without any
175 /// PatFrag references.
176 TreePatternNode *InlinePatternFragments(TreePattern &TP);
178 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
179 /// this node and its children in the tree. This returns true if it makes a
180 /// change, false otherwise. If a type contradiction is found, throw an
182 bool ApplyTypeConstraints(TreePattern &TP);
184 /// UpdateNodeType - Set the node type of N to VT if VT contains
185 /// information. If N already contains a conflicting type, then throw an
186 /// exception. This returns true if any information was updated.
188 bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP);
190 /// ContainsUnresolvedType - Return true if this tree contains any
191 /// unresolved types.
192 bool ContainsUnresolvedType() const {
193 if (Ty == MVT::LAST_VALUETYPE) return true;
194 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
195 if (getChild(i)->ContainsUnresolvedType()) return true;
201 /// TreePattern - Represent a pattern, used for instructions, pattern
205 /// Trees - The list of pattern trees which corresponds to this pattern.
206 /// Note that PatFrag's only have a single tree.
208 std::vector<TreePatternNode*> Trees;
210 /// TheRecord - The actual TableGen record corresponding to this pattern.
214 /// Args - This is a list of all of the arguments to this pattern (for
215 /// PatFrag patterns), which are the 'node' markers in this pattern.
216 std::vector<std::string> Args;
218 /// ISE - the DAG isel emitter coordinating this madness.
223 /// TreePattern constructor - Parse the specified DagInits into the
225 TreePattern(Record *TheRec,
226 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
228 /// getTrees - Return the tree patterns which corresponds to this pattern.
230 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
231 unsigned getNumTrees() const { return Trees.size(); }
232 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
233 TreePatternNode *getOnlyTree() const {
234 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
238 /// getRecord - Return the actual TableGen record corresponding to this
241 Record *getRecord() const { return TheRecord; }
243 unsigned getNumArgs() const { return Args.size(); }
244 const std::string &getArgName(unsigned i) const {
245 assert(i < Args.size() && "Argument reference out of range!");
248 std::vector<std::string> &getArgList() { return Args; }
250 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
252 /// InlinePatternFragments - If this pattern refers to any pattern
253 /// fragments, inline them into place, giving us a pattern without any
254 /// PatFrag references.
255 void InlinePatternFragments() {
256 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
257 Trees[i] = Trees[i]->InlinePatternFragments(*this);
260 /// InferAllTypes - Infer/propagate as many types throughout the expression
261 /// patterns as possible. Return true if all types are infered, false
262 /// otherwise. Throw an exception if a type contradiction is found.
263 bool InferAllTypes();
265 /// error - Throw an exception, prefixing it with information about this
267 void error(const std::string &Msg) const;
269 void print(std::ostream &OS) const;
273 MVT::ValueType getIntrinsicType(Record *R) const;
274 TreePatternNode *ParseTreePattern(DagInit *DI);
278 class DAGInstruction {
279 TreePattern *Pattern;
281 unsigned NumOperands;
282 std::vector<MVT::ValueType> ResultTypes;
283 std::vector<MVT::ValueType> OperandTypes;
284 TreePatternNode *ResultPattern;
286 DAGInstruction(TreePattern *TP,
287 const std::vector<MVT::ValueType> &resultTypes,
288 const std::vector<MVT::ValueType> &operandTypes,
289 TreePatternNode *resultPattern)
290 : Pattern(TP), ResultTypes(resultTypes), OperandTypes(operandTypes),
291 ResultPattern(resultPattern) {}
293 TreePattern *getPattern() const { return Pattern; }
294 unsigned getNumResults() const { return ResultTypes.size(); }
295 unsigned getNumOperands() const { return OperandTypes.size(); }
297 MVT::ValueType getResultType(unsigned RN) const {
298 assert(RN < ResultTypes.size());
299 return ResultTypes[RN];
302 MVT::ValueType getOperandType(unsigned ON) const {
303 assert(ON < OperandTypes.size());
304 return OperandTypes[ON];
306 TreePatternNode *getResultPattern() const { return ResultPattern; }
310 /// InstrSelectorEmitter - The top-level class which coordinates construction
311 /// and emission of the instruction selector.
313 class DAGISelEmitter : public TableGenBackend {
314 RecordKeeper &Records;
315 CodeGenTarget Target;
317 std::map<Record*, SDNodeInfo> SDNodes;
318 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
319 std::map<Record*, TreePattern*> PatternFragments;
320 std::vector<DAGInstruction> Instructions;
322 /// PatternsToMatch - All of the things we are matching on the DAG. The first
323 /// value is the pattern to match, the second pattern is the result to
325 std::vector<std::pair<TreePatternNode*, TreePatternNode*> > PatternsToMatch;
327 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
329 // run - Output the isel, returning true on failure.
330 void run(std::ostream &OS);
332 const SDNodeInfo &getSDNodeInfo(Record *R) const {
333 assert(SDNodes.count(R) && "Unknown node!");
334 return SDNodes.find(R)->second;
337 TreePattern *getPatternFragment(Record *R) const {
338 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
339 return PatternFragments.find(R)->second;
342 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
343 assert(SDNodeXForms.count(R) && "Invalid transform!");
344 return SDNodeXForms.find(R)->second;
348 void ParseNodeInfo();
349 void ParseNodeTransforms(std::ostream &OS);
350 void ParsePatternFragments(std::ostream &OS);
351 void ParseInstructions();
352 void ParsePatterns();
353 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
354 std::map<std::string,
355 TreePatternNode*> &InstInputs,
356 std::map<std::string, Record*> &InstResults);
357 void EmitInstructionSelector(std::ostream &OS);
360 } // End llvm namespace