blob: 280cab35560af604372a8011bfacdadd53f81c62 [file] [log] [blame]
Chris Lattner4a24c642005-09-03 01:14:03 +00001//===- DAGISelEmitter.h - Generate an instruction selector ------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits a DAG instruction selector.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef DAGISEL_EMITTER_H
15#define DAGISEL_EMITTER_H
16
17#include "TableGenBackend.h"
18#include "CodeGenTarget.h"
19
20namespace llvm {
Chris Lattner54cb8fd2005-09-07 23:44:43 +000021 class Record;
Jeff Cohen8337b072005-09-10 02:00:02 +000022 struct Init;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000023 class DagInit;
Chris Lattner32707602005-09-08 23:22:48 +000024 class SDNodeInfo;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000025 class TreePattern;
Chris Lattner32707602005-09-08 23:22:48 +000026 class TreePatternNode;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000027 class DAGISelEmitter;
Chris Lattnerca559d02005-09-08 21:03:01 +000028
Chris Lattner33c92e92005-09-08 21:27:15 +000029 /// 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);
33
34 unsigned OperandNo; // The operand # this constraint applies to.
35 enum {
36 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
37 } ConstraintType;
38
39 union { // The discriminated union.
40 struct {
41 MVT::ValueType VT;
42 } SDTCisVT_Info;
43 struct {
44 unsigned OtherOperandNum;
45 } SDTCisSameAs_Info;
46 struct {
47 unsigned OtherOperandNum;
48 } SDTCisVTSmallerThanOp_Info;
49 } x;
Chris Lattner32707602005-09-08 23:22:48 +000050
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
54 /// exception.
55 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
56 TreePattern &TP) const;
57
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;
Chris Lattner33c92e92005-09-08 21:27:15 +000062 };
63
Chris Lattnerca559d02005-09-08 21:03:01 +000064 /// 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
66 /// processing.
67 class SDNodeInfo {
68 Record *Def;
69 std::string EnumName;
70 std::string SDClassName;
Chris Lattner32707602005-09-08 23:22:48 +000071 unsigned NumResults;
72 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000073 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000074 public:
75 SDNodeInfo(Record *R); // Parse the specified record.
76
Chris Lattner32707602005-09-08 23:22:48 +000077 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +000078 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +000079 Record *getRecord() const { return Def; }
80 const std::string &getEnumName() const { return EnumName; }
81 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +000082
Chris Lattner32707602005-09-08 23:22:48 +000083 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +000084 return TypeConstraints;
85 }
Chris Lattner32707602005-09-08 23:22:48 +000086
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);
95 return MadeChange;
96 }
Chris Lattnerca559d02005-09-08 21:03:01 +000097 };
Chris Lattner4a24c642005-09-03 01:14:03 +000098
Chris Lattner54cb8fd2005-09-07 23:44:43 +000099 /// 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.
105 MVT::ValueType Ty;
106
107 /// Operator - The Record for the operator if this is an interior node (not
108 /// a leaf).
109 Record *Operator;
110
111 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
112 ///
113 Init *Val;
114
115 /// Name - The name given to this node with the :$foo notation.
116 ///
117 std::string Name;
118
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;
122
Chris Lattner24eeeb82005-09-13 21:51:00 +0000123 /// TransformFn - The transformation function to execute on this node before
124 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000125 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000126
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000127 std::vector<TreePatternNode*> Children;
128 public:
129 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattnerb0276202005-09-14 22:55:26 +0000130 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), TransformFn(0),
131 Children(Ch) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000132 TreePatternNode(Init *val) // leaf ctor
Chris Lattnerb0276202005-09-14 22:55:26 +0000133 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val), TransformFn(0) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000134 ~TreePatternNode();
135
136 const std::string &getName() const { return Name; }
137 void setName(const std::string &N) { Name = N; }
138
139 bool isLeaf() const { return Val != 0; }
Chris Lattner32707602005-09-08 23:22:48 +0000140 bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000141 MVT::ValueType getType() const { return Ty; }
142 void setType(MVT::ValueType VT) { Ty = VT; }
143
144 Init *getLeafValue() const { assert(isLeaf()); return Val; }
145 Record *getOperator() const { assert(!isLeaf()); return Operator; }
146
147 unsigned getNumChildren() const { return Children.size(); }
148 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
149 void setChild(unsigned i, TreePatternNode *N) {
150 Children[i] = N;
151 }
152
153 const std::string &getPredicateFn() const { return PredicateFn; }
154 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000155
Chris Lattnerb0276202005-09-14 22:55:26 +0000156 Record *getTransformFn() const { return TransformFn; }
157 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000158
159 void print(std::ostream &OS) const;
160 void dump() const;
161
162 public: // Higher level manipulation routines.
163
164 /// clone - Return a new copy of this tree.
165 ///
166 TreePatternNode *clone() const;
167
Chris Lattner32707602005-09-08 23:22:48 +0000168 /// SubstituteFormalArguments - Replace the formal arguments in this tree
169 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000170 void SubstituteFormalArguments(std::map<std::string,
171 TreePatternNode*> &ArgMap);
172
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);
Chris Lattner32707602005-09-08 23:22:48 +0000177
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
181 /// exception.
182 bool ApplyTypeConstraints(TreePattern &TP);
183
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.
187 ///
188 bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP);
189
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;
196 return false;
197 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000198 };
199
200
Chris Lattneree9f0c32005-09-13 21:20:49 +0000201 /// TreePattern - Represent a pattern, used for instructions, pattern
202 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000203 ///
204 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000205 /// Trees - The list of pattern trees which corresponds to this pattern.
206 /// Note that PatFrag's only have a single tree.
207 ///
208 std::vector<TreePatternNode*> Trees;
209
210 /// TheRecord - The actual TableGen record corresponding to this pattern.
211 ///
212 Record *TheRecord;
213
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;
217
218 /// ISE - the DAG isel emitter coordinating this madness.
219 ///
220 DAGISelEmitter &ISE;
221 public:
222
223 /// TreePattern constructor - Parse the specified DagInits into the
224 /// current record.
Chris Lattneree9f0c32005-09-13 21:20:49 +0000225 TreePattern(Record *TheRec,
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000226 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
227
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000228 /// getTrees - Return the tree patterns which corresponds to this pattern.
229 ///
230 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000231 unsigned getNumTrees() const { return Trees.size(); }
232 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000233 TreePatternNode *getOnlyTree() const {
234 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
235 return Trees[0];
236 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000237
238 /// getRecord - Return the actual TableGen record corresponding to this
239 /// pattern.
240 ///
241 Record *getRecord() const { return TheRecord; }
242
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!");
246 return Args[i];
247 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000248 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000249
250 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
251
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);
258 }
259
Chris Lattner32707602005-09-08 23:22:48 +0000260 /// 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();
264
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000265 /// error - Throw an exception, prefixing it with information about this
266 /// pattern.
267 void error(const std::string &Msg) const;
268
269 void print(std::ostream &OS) const;
270 void dump() const;
271
272 private:
273 MVT::ValueType getIntrinsicType(Record *R) const;
274 TreePatternNode *ParseTreePattern(DagInit *DI);
275 };
Chris Lattnerec676432005-09-14 04:03:16 +0000276
277
278 class DAGInstruction {
279 TreePattern *Pattern;
280 unsigned NumResults;
281 unsigned NumOperands;
Chris Lattnerb0276202005-09-14 22:55:26 +0000282 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000283 public:
Chris Lattnerb0276202005-09-14 22:55:26 +0000284 DAGInstruction(TreePattern *TP, unsigned results, unsigned ops,
285 TreePatternNode *resultPattern)
286 : Pattern(TP), NumResults(results), NumOperands(ops),
287 ResultPattern(resultPattern) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000288
Chris Lattnerec676432005-09-14 04:03:16 +0000289 TreePattern *getPattern() const { return Pattern; }
290 unsigned getNumResults() const { return NumResults; }
291 unsigned getNumOperands() const { return NumOperands; }
Chris Lattnerb0276202005-09-14 22:55:26 +0000292 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000293 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000294
295
Chris Lattner4a24c642005-09-03 01:14:03 +0000296/// InstrSelectorEmitter - The top-level class which coordinates construction
297/// and emission of the instruction selector.
298///
299class DAGISelEmitter : public TableGenBackend {
300 RecordKeeper &Records;
301 CodeGenTarget Target;
302
Chris Lattnerca559d02005-09-08 21:03:01 +0000303 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000304 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000305 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerec676432005-09-14 04:03:16 +0000306 std::vector<DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000307
308 /// PatternsToMatch - All of the things we are matching on the DAG. The first
309 /// value is the pattern to match, the second pattern is the result to
310 /// emit.
311 std::vector<std::pair<TreePatternNode*, TreePatternNode*> > PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000312public:
313 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
314
315 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000316 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000317
318 const SDNodeInfo &getSDNodeInfo(Record *R) const {
319 assert(SDNodes.count(R) && "Unknown node!");
320 return SDNodes.find(R)->second;
321 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000322
323 TreePattern *getPatternFragment(Record *R) const {
324 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
325 return PatternFragments.find(R)->second;
326 }
327
Chris Lattner6de8b532005-09-13 21:59:15 +0000328 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
329 assert(SDNodeXForms.count(R) && "Invalid transform!");
330 return SDNodeXForms.find(R)->second;
331 }
332
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000333private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000334 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000335 void ParseNodeTransforms(std::ostream &OS);
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000336 void ParsePatternFragments(std::ostream &OS);
337 void ParseInstructions();
338 void ParsePatterns();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000339 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
340 std::map<std::string,
341 TreePatternNode*> &InstInputs,
342 std::map<std::string, Record*> &InstResults);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000343 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000344};
345
346} // End llvm namespace
347
348#endif