blob: 4b2a2fb769e2f84576c7da3ec9710bd293a42a0b [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 {
Chris Lattner03ebd802005-10-14 04:53:53 +000037 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp,
38 SDTCisOpSmallerThanOp
Chris Lattner33c92e92005-09-08 21:27:15 +000039 } ConstraintType;
40
41 union { // The discriminated union.
42 struct {
43 MVT::ValueType VT;
44 } SDTCisVT_Info;
45 struct {
46 unsigned OtherOperandNum;
47 } SDTCisSameAs_Info;
48 struct {
49 unsigned OtherOperandNum;
50 } SDTCisVTSmallerThanOp_Info;
Chris Lattner03ebd802005-10-14 04:53:53 +000051 struct {
52 unsigned BigOperandNum;
53 } SDTCisOpSmallerThanOp_Info;
Chris Lattner33c92e92005-09-08 21:27:15 +000054 } x;
Chris Lattner32707602005-09-08 23:22:48 +000055
56 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
57 /// constraint to the nodes operands. This returns true if it makes a
58 /// change, false otherwise. If a type contradiction is found, throw an
59 /// exception.
60 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
61 TreePattern &TP) const;
62
63 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
64 /// N, which has NumResults results.
65 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
66 unsigned NumResults) const;
Chris Lattner33c92e92005-09-08 21:27:15 +000067 };
68
Chris Lattnerca559d02005-09-08 21:03:01 +000069 /// SDNodeInfo - One of these records is created for each SDNode instance in
70 /// the target .td file. This represents the various dag nodes we will be
71 /// processing.
72 class SDNodeInfo {
73 Record *Def;
74 std::string EnumName;
75 std::string SDClassName;
Chris Lattnera1a68ae2005-09-28 18:28:29 +000076 unsigned Properties;
Chris Lattner32707602005-09-08 23:22:48 +000077 unsigned NumResults;
78 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000079 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000080 public:
81 SDNodeInfo(Record *R); // Parse the specified record.
82
Chris Lattner32707602005-09-08 23:22:48 +000083 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +000084 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +000085 Record *getRecord() const { return Def; }
86 const std::string &getEnumName() const { return EnumName; }
87 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +000088
Chris Lattner32707602005-09-08 23:22:48 +000089 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +000090 return TypeConstraints;
91 }
Chris Lattnera1a68ae2005-09-28 18:28:29 +000092
93 // SelectionDAG node properties.
Chris Lattner7cf2fe62005-09-28 20:58:06 +000094 enum SDNP { SDNPCommutative, SDNPAssociative };
Chris Lattnera1a68ae2005-09-28 18:28:29 +000095
96 /// hasProperty - Return true if this node has the specified property.
97 ///
98 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +000099
100 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
101 /// constraints for this node to the operands of the node. This returns
102 /// true if it makes a change, false otherwise. If a type contradiction is
103 /// found, throw an exception.
104 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
105 bool MadeChange = false;
106 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
107 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
108 return MadeChange;
109 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000110 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000111
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000112 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
113 /// patterns), and as such should be ref counted. We currently just leak all
114 /// TreePatternNode objects!
115 class TreePatternNode {
116 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
117 /// been determined yet.
118 MVT::ValueType Ty;
119
120 /// Operator - The Record for the operator if this is an interior node (not
121 /// a leaf).
122 Record *Operator;
123
124 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
125 ///
126 Init *Val;
127
128 /// Name - The name given to this node with the :$foo notation.
129 ///
130 std::string Name;
131
132 /// PredicateFn - The predicate function to execute on this node to check
133 /// for a match. If this string is empty, no predicate is involved.
134 std::string PredicateFn;
135
Chris Lattner24eeeb82005-09-13 21:51:00 +0000136 /// TransformFn - The transformation function to execute on this node before
137 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000138 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000139
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000140 std::vector<TreePatternNode*> Children;
141 public:
142 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattnerb0276202005-09-14 22:55:26 +0000143 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), TransformFn(0),
144 Children(Ch) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000145 TreePatternNode(Init *val) // leaf ctor
Chris Lattnerb0276202005-09-14 22:55:26 +0000146 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val), TransformFn(0) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000147 ~TreePatternNode();
148
149 const std::string &getName() const { return Name; }
150 void setName(const std::string &N) { Name = N; }
151
152 bool isLeaf() const { return Val != 0; }
Chris Lattner32707602005-09-08 23:22:48 +0000153 bool hasTypeSet() const { return Ty != MVT::LAST_VALUETYPE; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000154 MVT::ValueType getType() const { return Ty; }
155 void setType(MVT::ValueType VT) { Ty = VT; }
156
157 Init *getLeafValue() const { assert(isLeaf()); return Val; }
158 Record *getOperator() const { assert(!isLeaf()); return Operator; }
159
160 unsigned getNumChildren() const { return Children.size(); }
161 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
162 void setChild(unsigned i, TreePatternNode *N) {
163 Children[i] = N;
164 }
165
166 const std::string &getPredicateFn() const { return PredicateFn; }
167 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000168
Chris Lattnerb0276202005-09-14 22:55:26 +0000169 Record *getTransformFn() const { return TransformFn; }
170 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000171
172 void print(std::ostream &OS) const;
173 void dump() const;
174
175 public: // Higher level manipulation routines.
176
177 /// clone - Return a new copy of this tree.
178 ///
179 TreePatternNode *clone() const;
180
Chris Lattnere46e17b2005-09-29 19:28:10 +0000181 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
182 /// the specified node. For this comparison, all of the state of the node
183 /// is considered, except for the assigned name. Nodes with differing names
184 /// that are otherwise identical are considered isomorphic.
185 bool isIsomorphicTo(const TreePatternNode *N) const;
186
Chris Lattner32707602005-09-08 23:22:48 +0000187 /// SubstituteFormalArguments - Replace the formal arguments in this tree
188 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000189 void SubstituteFormalArguments(std::map<std::string,
190 TreePatternNode*> &ArgMap);
191
192 /// InlinePatternFragments - If this pattern refers to any pattern
193 /// fragments, inline them into place, giving us a pattern without any
194 /// PatFrag references.
195 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000196
197 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
198 /// this node and its children in the tree. This returns true if it makes a
199 /// change, false otherwise. If a type contradiction is found, throw an
200 /// exception.
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000201 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattner32707602005-09-08 23:22:48 +0000202
203 /// UpdateNodeType - Set the node type of N to VT if VT contains
204 /// information. If N already contains a conflicting type, then throw an
205 /// exception. This returns true if any information was updated.
206 ///
207 bool UpdateNodeType(MVT::ValueType VT, TreePattern &TP);
208
209 /// ContainsUnresolvedType - Return true if this tree contains any
210 /// unresolved types.
211 bool ContainsUnresolvedType() const {
212 if (Ty == MVT::LAST_VALUETYPE) return true;
213 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
214 if (getChild(i)->ContainsUnresolvedType()) return true;
215 return false;
216 }
Chris Lattnere97603f2005-09-28 19:27:25 +0000217
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000218 /// canPatternMatch - If it is impossible for this pattern to match on this
219 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattnere97603f2005-09-28 19:27:25 +0000220 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000221 };
222
223
Chris Lattneree9f0c32005-09-13 21:20:49 +0000224 /// TreePattern - Represent a pattern, used for instructions, pattern
225 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000226 ///
227 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000228 /// Trees - The list of pattern trees which corresponds to this pattern.
229 /// Note that PatFrag's only have a single tree.
230 ///
231 std::vector<TreePatternNode*> Trees;
232
233 /// TheRecord - The actual TableGen record corresponding to this pattern.
234 ///
235 Record *TheRecord;
236
237 /// Args - This is a list of all of the arguments to this pattern (for
238 /// PatFrag patterns), which are the 'node' markers in this pattern.
239 std::vector<std::string> Args;
240
241 /// ISE - the DAG isel emitter coordinating this madness.
242 ///
243 DAGISelEmitter &ISE;
244 public:
245
246 /// TreePattern constructor - Parse the specified DagInits into the
247 /// current record.
Chris Lattnera28aec12005-09-15 22:23:50 +0000248 TreePattern(Record *TheRec, ListInit *RawPat, DAGISelEmitter &ise);
249 TreePattern(Record *TheRec, DagInit *Pat, DAGISelEmitter &ise);
250 TreePattern(Record *TheRec, TreePatternNode *Pat, DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000251
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000252 /// getTrees - Return the tree patterns which corresponds to this pattern.
253 ///
254 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000255 unsigned getNumTrees() const { return Trees.size(); }
256 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000257 TreePatternNode *getOnlyTree() const {
258 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
259 return Trees[0];
260 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000261
262 /// getRecord - Return the actual TableGen record corresponding to this
263 /// pattern.
264 ///
265 Record *getRecord() const { return TheRecord; }
266
267 unsigned getNumArgs() const { return Args.size(); }
268 const std::string &getArgName(unsigned i) const {
269 assert(i < Args.size() && "Argument reference out of range!");
270 return Args[i];
271 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000272 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000273
274 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
275
276 /// InlinePatternFragments - If this pattern refers to any pattern
277 /// fragments, inline them into place, giving us a pattern without any
278 /// PatFrag references.
279 void InlinePatternFragments() {
280 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
281 Trees[i] = Trees[i]->InlinePatternFragments(*this);
282 }
283
Chris Lattner32707602005-09-08 23:22:48 +0000284 /// InferAllTypes - Infer/propagate as many types throughout the expression
285 /// patterns as possible. Return true if all types are infered, false
286 /// otherwise. Throw an exception if a type contradiction is found.
287 bool InferAllTypes();
288
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000289 /// error - Throw an exception, prefixing it with information about this
290 /// pattern.
291 void error(const std::string &Msg) const;
292
293 void print(std::ostream &OS) const;
294 void dump() const;
295
296 private:
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000297 TreePatternNode *ParseTreePattern(DagInit *DI);
298 };
Chris Lattnerec676432005-09-14 04:03:16 +0000299
300
301 class DAGInstruction {
302 TreePattern *Pattern;
303 unsigned NumResults;
304 unsigned NumOperands;
Chris Lattnerae6d8282005-09-15 21:51:12 +0000305 std::vector<MVT::ValueType> ResultTypes;
306 std::vector<MVT::ValueType> OperandTypes;
Chris Lattnerb0276202005-09-14 22:55:26 +0000307 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000308 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000309 DAGInstruction(TreePattern *TP,
310 const std::vector<MVT::ValueType> &resultTypes,
Chris Lattnera28aec12005-09-15 22:23:50 +0000311 const std::vector<MVT::ValueType> &operandTypes)
Chris Lattnerae6d8282005-09-15 21:51:12 +0000312 : Pattern(TP), ResultTypes(resultTypes), OperandTypes(operandTypes),
Chris Lattnera28aec12005-09-15 22:23:50 +0000313 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000314
Chris Lattnerec676432005-09-14 04:03:16 +0000315 TreePattern *getPattern() const { return Pattern; }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000316 unsigned getNumResults() const { return ResultTypes.size(); }
317 unsigned getNumOperands() const { return OperandTypes.size(); }
318
Chris Lattnera28aec12005-09-15 22:23:50 +0000319 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
320
Chris Lattnerae6d8282005-09-15 21:51:12 +0000321 MVT::ValueType getResultType(unsigned RN) const {
322 assert(RN < ResultTypes.size());
323 return ResultTypes[RN];
324 }
325
326 MVT::ValueType getOperandType(unsigned ON) const {
327 assert(ON < OperandTypes.size());
328 return OperandTypes[ON];
329 }
Chris Lattnerb0276202005-09-14 22:55:26 +0000330 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000331 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000332
333
Chris Lattner4a24c642005-09-03 01:14:03 +0000334/// InstrSelectorEmitter - The top-level class which coordinates construction
335/// and emission of the instruction selector.
336///
337class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000338public:
339 typedef std::pair<TreePatternNode*, TreePatternNode*> PatternToMatch;
340private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000341 RecordKeeper &Records;
342 CodeGenTarget Target;
343
Chris Lattnerca559d02005-09-08 21:03:01 +0000344 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000345 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000346 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000347 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000348
349 /// PatternsToMatch - All of the things we are matching on the DAG. The first
350 /// value is the pattern to match, the second pattern is the result to
351 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000352 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000353public:
354 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
355
356 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000357 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000358
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000359 const CodeGenTarget &getTargetInfo() const { return Target; }
360
Chris Lattnerca559d02005-09-08 21:03:01 +0000361 const SDNodeInfo &getSDNodeInfo(Record *R) const {
362 assert(SDNodes.count(R) && "Unknown node!");
363 return SDNodes.find(R)->second;
364 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000365
366 TreePattern *getPatternFragment(Record *R) const {
367 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
368 return PatternFragments.find(R)->second;
369 }
370
Chris Lattner6de8b532005-09-13 21:59:15 +0000371 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
372 assert(SDNodeXForms.count(R) && "Invalid transform!");
373 return SDNodeXForms.find(R)->second;
374 }
375
Chris Lattnerae5b3502005-09-15 21:57:35 +0000376 const DAGInstruction &getInstruction(Record *R) const {
377 assert(Instructions.count(R) && "Unknown instruction!");
378 return Instructions.find(R)->second;
379 }
380
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000381private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000382 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000383 void ParseNodeTransforms(std::ostream &OS);
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000384 void ParsePatternFragments(std::ostream &OS);
385 void ParseInstructions();
386 void ParsePatterns();
Chris Lattnere97603f2005-09-28 19:27:25 +0000387 void GenerateVariants();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000388 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
389 std::map<std::string,
390 TreePatternNode*> &InstInputs,
391 std::map<std::string, Record*> &InstResults);
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000392 void EmitMatchForPattern(TreePatternNode *N, const std::string &RootName,
Chris Lattner8fc35682005-09-23 23:16:51 +0000393 std::map<std::string,std::string> &VarMap,
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000394 unsigned PatternNo, std::ostream &OS);
Chris Lattner72fe91c2005-09-24 00:40:24 +0000395 unsigned CodeGenPatternResult(TreePatternNode *N, unsigned &Ctr,
396 std::map<std::string,std::string> &VariableMap,
397 std::ostream &OS);
Chris Lattner3f7e9142005-09-23 20:52:47 +0000398 void EmitCodeForPattern(PatternToMatch &Pattern, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000399 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000400};
401
402} // End llvm namespace
403
404#endif