blob: 48f941d859346700c481aa16e1c287b17b057c5b [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 Lattnera28aec12005-09-15 22:23:50 +000023 class ListInit;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000024 class DagInit;
Chris Lattner32707602005-09-08 23:22:48 +000025 class SDNodeInfo;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000026 class TreePattern;
Chris Lattner32707602005-09-08 23:22:48 +000027 class TreePatternNode;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000028 class DAGISelEmitter;
Chris Lattnerca559d02005-09-08 21:03:01 +000029
Chris Lattner33c92e92005-09-08 21:27:15 +000030 /// SDTypeConstraint - This is a discriminated union of constraints,
31 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
32 struct SDTypeConstraint {
33 SDTypeConstraint(Record *R);
34
35 unsigned OperandNo; // The operand # this constraint applies to.
36 enum {
37 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
38 } ConstraintType;
39
40 union { // The discriminated union.
41 struct {
42 MVT::ValueType VT;
43 } SDTCisVT_Info;
44 struct {
45 unsigned OtherOperandNum;
46 } SDTCisSameAs_Info;
47 struct {
48 unsigned OtherOperandNum;
49 } SDTCisVTSmallerThanOp_Info;
50 } x;
Chris Lattner32707602005-09-08 23:22:48 +000051
52 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
53 /// constraint to the nodes operands. This returns true if it makes a
54 /// change, false otherwise. If a type contradiction is found, throw an
55 /// exception.
56 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
57 TreePattern &TP) const;
58
59 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
60 /// N, which has NumResults results.
61 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
62 unsigned NumResults) const;
Chris Lattner33c92e92005-09-08 21:27:15 +000063 };
64
Chris Lattnerca559d02005-09-08 21:03:01 +000065 /// SDNodeInfo - One of these records is created for each SDNode instance in
66 /// the target .td file. This represents the various dag nodes we will be
67 /// processing.
68 class SDNodeInfo {
69 Record *Def;
70 std::string EnumName;
71 std::string SDClassName;
Chris Lattnera1a68ae2005-09-28 18:28:29 +000072 unsigned Properties;
Chris Lattner32707602005-09-08 23:22:48 +000073 unsigned NumResults;
74 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000075 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000076 public:
77 SDNodeInfo(Record *R); // Parse the specified record.
78
Chris Lattner32707602005-09-08 23:22:48 +000079 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +000080 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +000081 Record *getRecord() const { return Def; }
82 const std::string &getEnumName() const { return EnumName; }
83 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +000084
Chris Lattner32707602005-09-08 23:22:48 +000085 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +000086 return TypeConstraints;
87 }
Chris Lattnera1a68ae2005-09-28 18:28:29 +000088
89 // SelectionDAG node properties.
90 enum SDNP { SDNPCommutative };
91
92 /// hasProperty - Return true if this node has the specified property.
93 ///
94 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +000095
96 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
97 /// constraints for this node to the operands of the node. This returns
98 /// true if it makes a change, false otherwise. If a type contradiction is
99 /// found, throw an exception.
100 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
101 bool MadeChange = false;
102 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
103 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
104 return MadeChange;
105 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000106 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000107
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000108 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
109 /// patterns), and as such should be ref counted. We currently just leak all
110 /// TreePatternNode objects!
111 class TreePatternNode {
112 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
113 /// been determined yet.
114 MVT::ValueType Ty;
115
116 /// Operator - The Record for the operator if this is an interior node (not
117 /// a leaf).
118 Record *Operator;
119
120 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
121 ///
122 Init *Val;
123
124 /// Name - The name given to this node with the :$foo notation.
125 ///
126 std::string Name;
127
128 /// PredicateFn - The predicate function to execute on this node to check
129 /// for a match. If this string is empty, no predicate is involved.
130 std::string PredicateFn;
131
Chris Lattner24eeeb82005-09-13 21:51:00 +0000132 /// TransformFn - The transformation function to execute on this node before
133 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000134 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000135
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000136 std::vector<TreePatternNode*> Children;
137 public:
138 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattnerb0276202005-09-14 22:55:26 +0000139 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), TransformFn(0),
140 Children(Ch) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000141 TreePatternNode(Init *val) // leaf ctor
Chris Lattnerb0276202005-09-14 22:55:26 +0000142 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val), TransformFn(0) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000143 ~TreePatternNode();
144
145 const std::string &getName() const { return Name; }
146 void setName(const std::string &N) { Name = N; }
147
148 bool isLeaf() const { return Val != 0; }
Chris Lattner32707602005-09-08 23:22:48 +0000149 bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000150 MVT::ValueType getType() const { return Ty; }
151 void setType(MVT::ValueType VT) { Ty = VT; }
152
153 Init *getLeafValue() const { assert(isLeaf()); return Val; }
154 Record *getOperator() const { assert(!isLeaf()); return Operator; }
155
156 unsigned getNumChildren() const { return Children.size(); }
157 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
158 void setChild(unsigned i, TreePatternNode *N) {
159 Children[i] = N;
160 }
161
162 const std::string &getPredicateFn() const { return PredicateFn; }
163 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000164
Chris Lattnerb0276202005-09-14 22:55:26 +0000165 Record *getTransformFn() const { return TransformFn; }
166 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000167
168 void print(std::ostream &OS) const;
169 void dump() const;
170
171 public: // Higher level manipulation routines.
172
173 /// clone - Return a new copy of this tree.
174 ///
175 TreePatternNode *clone() const;
176
Chris Lattner32707602005-09-08 23:22:48 +0000177 /// SubstituteFormalArguments - Replace the formal arguments in this tree
178 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000179 void SubstituteFormalArguments(std::map<std::string,
180 TreePatternNode*> &ArgMap);
181
182 /// InlinePatternFragments - If this pattern refers to any pattern
183 /// fragments, inline them into place, giving us a pattern without any
184 /// PatFrag references.
185 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000186
187 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
188 /// this node and its children in the tree. This returns true if it makes a
189 /// change, false otherwise. If a type contradiction is found, throw an
190 /// exception.
191 bool ApplyTypeConstraints(TreePattern &TP);
192
193 /// UpdateNodeType - Set the node type of N to VT if VT contains
194 /// information. If N already contains a conflicting type, then throw an
195 /// exception. This returns true if any information was updated.
196 ///
197 bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP);
198
199 /// ContainsUnresolvedType - Return true if this tree contains any
200 /// unresolved types.
201 bool ContainsUnresolvedType() const {
202 if (Ty == MVT::LAST_VALUETYPE) return true;
203 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
204 if (getChild(i)->ContainsUnresolvedType()) return true;
205 return false;
206 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000207 };
208
209
Chris Lattneree9f0c32005-09-13 21:20:49 +0000210 /// TreePattern - Represent a pattern, used for instructions, pattern
211 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000212 ///
213 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000214 /// Trees - The list of pattern trees which corresponds to this pattern.
215 /// Note that PatFrag's only have a single tree.
216 ///
217 std::vector<TreePatternNode*> Trees;
218
219 /// TheRecord - The actual TableGen record corresponding to this pattern.
220 ///
221 Record *TheRecord;
222
223 /// Args - This is a list of all of the arguments to this pattern (for
224 /// PatFrag patterns), which are the 'node' markers in this pattern.
225 std::vector<std::string> Args;
226
227 /// ISE - the DAG isel emitter coordinating this madness.
228 ///
229 DAGISelEmitter &ISE;
230 public:
231
232 /// TreePattern constructor - Parse the specified DagInits into the
233 /// current record.
Chris Lattnera28aec12005-09-15 22:23:50 +0000234 TreePattern(Record *TheRec, ListInit *RawPat, DAGISelEmitter &ise);
235 TreePattern(Record *TheRec, DagInit *Pat, DAGISelEmitter &ise);
236 TreePattern(Record *TheRec, TreePatternNode *Pat, DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000237
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000238 /// getTrees - Return the tree patterns which corresponds to this pattern.
239 ///
240 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000241 unsigned getNumTrees() const { return Trees.size(); }
242 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000243 TreePatternNode *getOnlyTree() const {
244 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
245 return Trees[0];
246 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000247
248 /// getRecord - Return the actual TableGen record corresponding to this
249 /// pattern.
250 ///
251 Record *getRecord() const { return TheRecord; }
252
253 unsigned getNumArgs() const { return Args.size(); }
254 const std::string &getArgName(unsigned i) const {
255 assert(i < Args.size() && "Argument reference out of range!");
256 return Args[i];
257 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000258 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000259
260 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
261
262 /// InlinePatternFragments - If this pattern refers to any pattern
263 /// fragments, inline them into place, giving us a pattern without any
264 /// PatFrag references.
265 void InlinePatternFragments() {
266 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
267 Trees[i] = Trees[i]->InlinePatternFragments(*this);
268 }
269
Chris Lattner32707602005-09-08 23:22:48 +0000270 /// InferAllTypes - Infer/propagate as many types throughout the expression
271 /// patterns as possible. Return true if all types are infered, false
272 /// otherwise. Throw an exception if a type contradiction is found.
273 bool InferAllTypes();
274
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000275 /// error - Throw an exception, prefixing it with information about this
276 /// pattern.
277 void error(const std::string &Msg) const;
278
279 void print(std::ostream &OS) const;
280 void dump() const;
281
282 private:
283 MVT::ValueType getIntrinsicType(Record *R) const;
284 TreePatternNode *ParseTreePattern(DagInit *DI);
285 };
Chris Lattnerec676432005-09-14 04:03:16 +0000286
287
288 class DAGInstruction {
289 TreePattern *Pattern;
290 unsigned NumResults;
291 unsigned NumOperands;
Chris Lattnerae6d8282005-09-15 21:51:12 +0000292 std::vector<MVT::ValueType> ResultTypes;
293 std::vector<MVT::ValueType> OperandTypes;
Chris Lattnerb0276202005-09-14 22:55:26 +0000294 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000295 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000296 DAGInstruction(TreePattern *TP,
297 const std::vector<MVT::ValueType> &resultTypes,
Chris Lattnera28aec12005-09-15 22:23:50 +0000298 const std::vector<MVT::ValueType> &operandTypes)
Chris Lattnerae6d8282005-09-15 21:51:12 +0000299 : Pattern(TP), ResultTypes(resultTypes), OperandTypes(operandTypes),
Chris Lattnera28aec12005-09-15 22:23:50 +0000300 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000301
Chris Lattnerec676432005-09-14 04:03:16 +0000302 TreePattern *getPattern() const { return Pattern; }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000303 unsigned getNumResults() const { return ResultTypes.size(); }
304 unsigned getNumOperands() const { return OperandTypes.size(); }
305
Chris Lattnera28aec12005-09-15 22:23:50 +0000306 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
307
Chris Lattnerae6d8282005-09-15 21:51:12 +0000308 MVT::ValueType getResultType(unsigned RN) const {
309 assert(RN < ResultTypes.size());
310 return ResultTypes[RN];
311 }
312
313 MVT::ValueType getOperandType(unsigned ON) const {
314 assert(ON < OperandTypes.size());
315 return OperandTypes[ON];
316 }
Chris Lattnerb0276202005-09-14 22:55:26 +0000317 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000318 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000319
320
Chris Lattner4a24c642005-09-03 01:14:03 +0000321/// InstrSelectorEmitter - The top-level class which coordinates construction
322/// and emission of the instruction selector.
323///
324class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000325public:
326 typedef std::pair<TreePatternNode*, TreePatternNode*> PatternToMatch;
327private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000328 RecordKeeper &Records;
329 CodeGenTarget Target;
330
Chris Lattnerca559d02005-09-08 21:03:01 +0000331 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000332 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000333 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000334 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000335
336 /// PatternsToMatch - All of the things we are matching on the DAG. The first
337 /// value is the pattern to match, the second pattern is the result to
338 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000339 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000340public:
341 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
342
343 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000344 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000345
346 const SDNodeInfo &getSDNodeInfo(Record *R) const {
347 assert(SDNodes.count(R) && "Unknown node!");
348 return SDNodes.find(R)->second;
349 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000350
351 TreePattern *getPatternFragment(Record *R) const {
352 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
353 return PatternFragments.find(R)->second;
354 }
355
Chris Lattner6de8b532005-09-13 21:59:15 +0000356 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
357 assert(SDNodeXForms.count(R) && "Invalid transform!");
358 return SDNodeXForms.find(R)->second;
359 }
360
Chris Lattnerae5b3502005-09-15 21:57:35 +0000361 const DAGInstruction &getInstruction(Record *R) const {
362 assert(Instructions.count(R) && "Unknown instruction!");
363 return Instructions.find(R)->second;
364 }
365
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000366private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000367 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000368 void ParseNodeTransforms(std::ostream &OS);
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000369 void ParsePatternFragments(std::ostream &OS);
370 void ParseInstructions();
371 void ParsePatterns();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000372 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
373 std::map<std::string,
374 TreePatternNode*> &InstInputs,
375 std::map<std::string, Record*> &InstResults);
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000376 void EmitMatchForPattern(TreePatternNode *N, const std::string &RootName,
Chris Lattner8fc35682005-09-23 23:16:51 +0000377 std::map<std::string,std::string> &VarMap,
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000378 unsigned PatternNo, std::ostream &OS);
Chris Lattner72fe91c2005-09-24 00:40:24 +0000379 unsigned CodeGenPatternResult(TreePatternNode *N, unsigned &Ctr,
380 std::map<std::string,std::string> &VariableMap,
381 std::ostream &OS);
Chris Lattner3f7e9142005-09-23 20:52:47 +0000382 void EmitCodeForPattern(PatternToMatch &Pattern, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000383 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000384};
385
386} // End llvm namespace
387
388#endif