blob: c547c368451d66c3e66d05544efaee0e91411f04 [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 Lattner3c7e18d2005-10-14 06:12:03 +000030 /// MVT::DAGISelGenValueType - These are some extended forms of MVT::ValueType
31 /// that we use as lattice values during type inferrence.
32 namespace MVT {
33 enum DAGISelGenValueType {
34 isFP = MVT::LAST_VALUETYPE,
35 isInt,
36 isUnknown
37 };
38 }
39
Chris Lattner33c92e92005-09-08 21:27:15 +000040 /// SDTypeConstraint - This is a discriminated union of constraints,
41 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
42 struct SDTypeConstraint {
43 SDTypeConstraint(Record *R);
44
45 unsigned OperandNo; // The operand # this constraint applies to.
46 enum {
Chris Lattner03ebd802005-10-14 04:53:53 +000047 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp,
48 SDTCisOpSmallerThanOp
Chris Lattner33c92e92005-09-08 21:27:15 +000049 } ConstraintType;
50
51 union { // The discriminated union.
52 struct {
53 MVT::ValueType VT;
54 } SDTCisVT_Info;
55 struct {
56 unsigned OtherOperandNum;
57 } SDTCisSameAs_Info;
58 struct {
59 unsigned OtherOperandNum;
60 } SDTCisVTSmallerThanOp_Info;
Chris Lattner03ebd802005-10-14 04:53:53 +000061 struct {
62 unsigned BigOperandNum;
63 } SDTCisOpSmallerThanOp_Info;
Chris Lattner33c92e92005-09-08 21:27:15 +000064 } x;
Chris Lattner32707602005-09-08 23:22:48 +000065
66 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
67 /// constraint to the nodes operands. This returns true if it makes a
68 /// change, false otherwise. If a type contradiction is found, throw an
69 /// exception.
70 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
71 TreePattern &TP) const;
72
73 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
74 /// N, which has NumResults results.
75 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
76 unsigned NumResults) const;
Chris Lattner33c92e92005-09-08 21:27:15 +000077 };
78
Chris Lattnerca559d02005-09-08 21:03:01 +000079 /// SDNodeInfo - One of these records is created for each SDNode instance in
80 /// the target .td file. This represents the various dag nodes we will be
81 /// processing.
82 class SDNodeInfo {
83 Record *Def;
84 std::string EnumName;
85 std::string SDClassName;
Chris Lattnera1a68ae2005-09-28 18:28:29 +000086 unsigned Properties;
Chris Lattner32707602005-09-08 23:22:48 +000087 unsigned NumResults;
88 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000089 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000090 public:
91 SDNodeInfo(Record *R); // Parse the specified record.
92
Chris Lattner32707602005-09-08 23:22:48 +000093 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +000094 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +000095 Record *getRecord() const { return Def; }
96 const std::string &getEnumName() const { return EnumName; }
97 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +000098
Chris Lattner32707602005-09-08 23:22:48 +000099 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +0000100 return TypeConstraints;
101 }
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000102
103 // SelectionDAG node properties.
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000104 enum SDNP { SDNPCommutative, SDNPAssociative };
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000105
106 /// hasProperty - Return true if this node has the specified property.
107 ///
108 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +0000109
110 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
111 /// constraints for this node to the operands of the node. This returns
112 /// true if it makes a change, false otherwise. If a type contradiction is
113 /// found, throw an exception.
114 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
115 bool MadeChange = false;
116 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
117 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
118 return MadeChange;
119 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000120 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000121
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000122 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
123 /// patterns), and as such should be ref counted. We currently just leak all
124 /// TreePatternNode objects!
125 class TreePatternNode {
126 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
127 /// been determined yet.
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000128 unsigned char Ty;
129
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000130 /// Operator - The Record for the operator if this is an interior node (not
131 /// a leaf).
132 Record *Operator;
133
134 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
135 ///
136 Init *Val;
137
138 /// Name - The name given to this node with the :$foo notation.
139 ///
140 std::string Name;
141
142 /// PredicateFn - The predicate function to execute on this node to check
143 /// for a match. If this string is empty, no predicate is involved.
144 std::string PredicateFn;
145
Chris Lattner24eeeb82005-09-13 21:51:00 +0000146 /// TransformFn - The transformation function to execute on this node before
147 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000148 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000149
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000150 std::vector<TreePatternNode*> Children;
151 public:
152 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000153 : Ty(MVT::isUnknown), Operator(Op), Val(0), TransformFn(0),
Chris Lattnerb0276202005-09-14 22:55:26 +0000154 Children(Ch) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000155 TreePatternNode(Init *val) // leaf ctor
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000156 : Ty(MVT::isUnknown), Operator(0), Val(val), TransformFn(0) {}
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000157 ~TreePatternNode();
158
159 const std::string &getName() const { return Name; }
160 void setName(const std::string &N) { Name = N; }
161
162 bool isLeaf() const { return Val != 0; }
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000163 bool hasTypeSet() const { return Ty < MVT::LAST_VALUETYPE; }
164 bool isTypeCompletelyUnknown() const {
165 return Ty == MVT::isUnknown;
166 }
167 MVT::ValueType getType() const {
168 assert(hasTypeSet() && "Doesn't have a type yet!");
169 return (MVT::ValueType)Ty;
170 }
171 unsigned char getExtType() const { return Ty; }
172 void setType(unsigned char VT) { Ty = VT; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000173
174 Init *getLeafValue() const { assert(isLeaf()); return Val; }
175 Record *getOperator() const { assert(!isLeaf()); return Operator; }
176
177 unsigned getNumChildren() const { return Children.size(); }
178 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
179 void setChild(unsigned i, TreePatternNode *N) {
180 Children[i] = N;
181 }
182
183 const std::string &getPredicateFn() const { return PredicateFn; }
184 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000185
Chris Lattnerb0276202005-09-14 22:55:26 +0000186 Record *getTransformFn() const { return TransformFn; }
187 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000188
189 void print(std::ostream &OS) const;
190 void dump() const;
191
192 public: // Higher level manipulation routines.
193
194 /// clone - Return a new copy of this tree.
195 ///
196 TreePatternNode *clone() const;
197
Chris Lattnere46e17b2005-09-29 19:28:10 +0000198 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
199 /// the specified node. For this comparison, all of the state of the node
200 /// is considered, except for the assigned name. Nodes with differing names
201 /// that are otherwise identical are considered isomorphic.
202 bool isIsomorphicTo(const TreePatternNode *N) const;
203
Chris Lattner32707602005-09-08 23:22:48 +0000204 /// SubstituteFormalArguments - Replace the formal arguments in this tree
205 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000206 void SubstituteFormalArguments(std::map<std::string,
207 TreePatternNode*> &ArgMap);
208
209 /// InlinePatternFragments - If this pattern refers to any pattern
210 /// fragments, inline them into place, giving us a pattern without any
211 /// PatFrag references.
212 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000213
214 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
215 /// this node and its children in the tree. This returns true if it makes a
216 /// change, false otherwise. If a type contradiction is found, throw an
217 /// exception.
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000218 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattner32707602005-09-08 23:22:48 +0000219
220 /// UpdateNodeType - Set the node type of N to VT if VT contains
221 /// information. If N already contains a conflicting type, then throw an
222 /// exception. This returns true if any information was updated.
223 ///
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000224 bool UpdateNodeType(unsigned char EVT, TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000225
226 /// ContainsUnresolvedType - Return true if this tree contains any
227 /// unresolved types.
228 bool ContainsUnresolvedType() const {
229 if (Ty == MVT::LAST_VALUETYPE) return true;
230 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
231 if (getChild(i)->ContainsUnresolvedType()) return true;
232 return false;
233 }
Chris Lattnere97603f2005-09-28 19:27:25 +0000234
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000235 /// canPatternMatch - If it is impossible for this pattern to match on this
236 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattnere97603f2005-09-28 19:27:25 +0000237 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000238 };
239
240
Chris Lattneree9f0c32005-09-13 21:20:49 +0000241 /// TreePattern - Represent a pattern, used for instructions, pattern
242 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000243 ///
244 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000245 /// Trees - The list of pattern trees which corresponds to this pattern.
246 /// Note that PatFrag's only have a single tree.
247 ///
248 std::vector<TreePatternNode*> Trees;
249
250 /// TheRecord - The actual TableGen record corresponding to this pattern.
251 ///
252 Record *TheRecord;
253
254 /// Args - This is a list of all of the arguments to this pattern (for
255 /// PatFrag patterns), which are the 'node' markers in this pattern.
256 std::vector<std::string> Args;
257
258 /// ISE - the DAG isel emitter coordinating this madness.
259 ///
260 DAGISelEmitter &ISE;
261 public:
262
263 /// TreePattern constructor - Parse the specified DagInits into the
264 /// current record.
Chris Lattnera28aec12005-09-15 22:23:50 +0000265 TreePattern(Record *TheRec, ListInit *RawPat, DAGISelEmitter &ise);
266 TreePattern(Record *TheRec, DagInit *Pat, DAGISelEmitter &ise);
267 TreePattern(Record *TheRec, TreePatternNode *Pat, DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000268
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000269 /// getTrees - Return the tree patterns which corresponds to this pattern.
270 ///
271 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000272 unsigned getNumTrees() const { return Trees.size(); }
273 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000274 TreePatternNode *getOnlyTree() const {
275 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
276 return Trees[0];
277 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000278
279 /// getRecord - Return the actual TableGen record corresponding to this
280 /// pattern.
281 ///
282 Record *getRecord() const { return TheRecord; }
283
284 unsigned getNumArgs() const { return Args.size(); }
285 const std::string &getArgName(unsigned i) const {
286 assert(i < Args.size() && "Argument reference out of range!");
287 return Args[i];
288 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000289 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000290
291 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
292
293 /// InlinePatternFragments - If this pattern refers to any pattern
294 /// fragments, inline them into place, giving us a pattern without any
295 /// PatFrag references.
296 void InlinePatternFragments() {
297 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
298 Trees[i] = Trees[i]->InlinePatternFragments(*this);
299 }
300
Chris Lattner32707602005-09-08 23:22:48 +0000301 /// InferAllTypes - Infer/propagate as many types throughout the expression
302 /// patterns as possible. Return true if all types are infered, false
303 /// otherwise. Throw an exception if a type contradiction is found.
304 bool InferAllTypes();
305
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000306 /// error - Throw an exception, prefixing it with information about this
307 /// pattern.
308 void error(const std::string &Msg) const;
309
310 void print(std::ostream &OS) const;
311 void dump() const;
312
313 private:
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000314 TreePatternNode *ParseTreePattern(DagInit *DI);
315 };
Chris Lattnerec676432005-09-14 04:03:16 +0000316
317
318 class DAGInstruction {
319 TreePattern *Pattern;
320 unsigned NumResults;
321 unsigned NumOperands;
Chris Lattnerae6d8282005-09-15 21:51:12 +0000322 std::vector<MVT::ValueType> ResultTypes;
323 std::vector<MVT::ValueType> OperandTypes;
Chris Lattnerb0276202005-09-14 22:55:26 +0000324 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000325 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000326 DAGInstruction(TreePattern *TP,
327 const std::vector<MVT::ValueType> &resultTypes,
Chris Lattnera28aec12005-09-15 22:23:50 +0000328 const std::vector<MVT::ValueType> &operandTypes)
Chris Lattnerae6d8282005-09-15 21:51:12 +0000329 : Pattern(TP), ResultTypes(resultTypes), OperandTypes(operandTypes),
Chris Lattnera28aec12005-09-15 22:23:50 +0000330 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000331
Chris Lattnerec676432005-09-14 04:03:16 +0000332 TreePattern *getPattern() const { return Pattern; }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000333 unsigned getNumResults() const { return ResultTypes.size(); }
334 unsigned getNumOperands() const { return OperandTypes.size(); }
335
Chris Lattnera28aec12005-09-15 22:23:50 +0000336 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
337
Chris Lattnerae6d8282005-09-15 21:51:12 +0000338 MVT::ValueType getResultType(unsigned RN) const {
339 assert(RN < ResultTypes.size());
340 return ResultTypes[RN];
341 }
342
343 MVT::ValueType getOperandType(unsigned ON) const {
344 assert(ON < OperandTypes.size());
345 return OperandTypes[ON];
346 }
Chris Lattnerb0276202005-09-14 22:55:26 +0000347 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000348 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000349
350
Chris Lattner4a24c642005-09-03 01:14:03 +0000351/// InstrSelectorEmitter - The top-level class which coordinates construction
352/// and emission of the instruction selector.
353///
354class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000355public:
356 typedef std::pair<TreePatternNode*, TreePatternNode*> PatternToMatch;
357private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000358 RecordKeeper &Records;
359 CodeGenTarget Target;
360
Chris Lattnerca559d02005-09-08 21:03:01 +0000361 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000362 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000363 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000364 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000365
366 /// PatternsToMatch - All of the things we are matching on the DAG. The first
367 /// value is the pattern to match, the second pattern is the result to
368 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000369 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000370public:
371 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
372
373 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000374 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000375
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000376 const CodeGenTarget &getTargetInfo() const { return Target; }
377
Chris Lattnerca559d02005-09-08 21:03:01 +0000378 const SDNodeInfo &getSDNodeInfo(Record *R) const {
379 assert(SDNodes.count(R) && "Unknown node!");
380 return SDNodes.find(R)->second;
381 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000382
383 TreePattern *getPatternFragment(Record *R) const {
384 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
385 return PatternFragments.find(R)->second;
386 }
387
Chris Lattner6de8b532005-09-13 21:59:15 +0000388 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
389 assert(SDNodeXForms.count(R) && "Invalid transform!");
390 return SDNodeXForms.find(R)->second;
391 }
392
Chris Lattnerae5b3502005-09-15 21:57:35 +0000393 const DAGInstruction &getInstruction(Record *R) const {
394 assert(Instructions.count(R) && "Unknown instruction!");
395 return Instructions.find(R)->second;
396 }
397
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000398private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000399 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000400 void ParseNodeTransforms(std::ostream &OS);
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000401 void ParsePatternFragments(std::ostream &OS);
402 void ParseInstructions();
403 void ParsePatterns();
Chris Lattnere97603f2005-09-28 19:27:25 +0000404 void GenerateVariants();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000405 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
406 std::map<std::string,
407 TreePatternNode*> &InstInputs,
408 std::map<std::string, Record*> &InstResults);
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000409 void EmitMatchForPattern(TreePatternNode *N, const std::string &RootName,
Chris Lattner8fc35682005-09-23 23:16:51 +0000410 std::map<std::string,std::string> &VarMap,
Chris Lattnerd1ff35a2005-09-23 21:33:23 +0000411 unsigned PatternNo, std::ostream &OS);
Chris Lattner72fe91c2005-09-24 00:40:24 +0000412 unsigned CodeGenPatternResult(TreePatternNode *N, unsigned &Ctr,
413 std::map<std::string,std::string> &VariableMap,
414 std::ostream &OS);
Chris Lattner3f7e9142005-09-23 20:52:47 +0000415 void EmitCodeForPattern(PatternToMatch &Pattern, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000416 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000417};
418
419} // End llvm namespace
420
421#endif