blob: 8b2bf06ae4330a82ca7fcac1a7988c91666a1be3 [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//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4a24c642005-09-03 01:14:03 +00007//
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"
Chris Lattner550525e2006-03-24 21:48:51 +000019#include "CodeGenIntrinsics.h"
Evan Cheng21ad3922006-02-07 00:37:41 +000020#include <set>
Chris Lattner4a24c642005-09-03 01:14:03 +000021
22namespace llvm {
Chris Lattner54cb8fd2005-09-07 23:44:43 +000023 class Record;
Jeff Cohen8337b072005-09-10 02:00:02 +000024 struct Init;
Chris Lattnera28aec12005-09-15 22:23:50 +000025 class ListInit;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000026 class DagInit;
Chris Lattner32707602005-09-08 23:22:48 +000027 class SDNodeInfo;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000028 class TreePattern;
Chris Lattner32707602005-09-08 23:22:48 +000029 class TreePatternNode;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000030 class DAGISelEmitter;
Evan Cheng0fc71982005-12-08 02:00:36 +000031 class ComplexPattern;
Chris Lattnerca559d02005-09-08 21:03:01 +000032
Chris Lattner3c7e18d2005-10-14 06:12:03 +000033 /// MVT::DAGISelGenValueType - These are some extended forms of MVT::ValueType
34 /// that we use as lattice values during type inferrence.
35 namespace MVT {
36 enum DAGISelGenValueType {
37 isFP = MVT::LAST_VALUETYPE,
38 isInt,
39 isUnknown
40 };
41 }
42
Chris Lattner33c92e92005-09-08 21:27:15 +000043 /// SDTypeConstraint - This is a discriminated union of constraints,
44 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
45 struct SDTypeConstraint {
46 SDTypeConstraint(Record *R);
47
48 unsigned OperandNo; // The operand # this constraint applies to.
49 enum {
Chris Lattner5b21be72005-12-09 22:57:42 +000050 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs,
Chris Lattner697f8842006-03-20 05:39:48 +000051 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisIntVectorOfSameSize
Chris Lattner33c92e92005-09-08 21:27:15 +000052 } ConstraintType;
53
54 union { // The discriminated union.
55 struct {
56 MVT::ValueType VT;
57 } SDTCisVT_Info;
58 struct {
59 unsigned OtherOperandNum;
60 } SDTCisSameAs_Info;
61 struct {
62 unsigned OtherOperandNum;
63 } SDTCisVTSmallerThanOp_Info;
Chris Lattner03ebd802005-10-14 04:53:53 +000064 struct {
65 unsigned BigOperandNum;
66 } SDTCisOpSmallerThanOp_Info;
Chris Lattner697f8842006-03-20 05:39:48 +000067 struct {
68 unsigned OtherOperandNum;
69 } SDTCisIntVectorOfSameSize_Info;
Chris Lattner33c92e92005-09-08 21:27:15 +000070 } x;
Chris Lattner32707602005-09-08 23:22:48 +000071
72 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
73 /// constraint to the nodes operands. This returns true if it makes a
74 /// change, false otherwise. If a type contradiction is found, throw an
75 /// exception.
76 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
77 TreePattern &TP) const;
78
79 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
80 /// N, which has NumResults results.
81 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
82 unsigned NumResults) const;
Chris Lattner33c92e92005-09-08 21:27:15 +000083 };
84
Chris Lattnerca559d02005-09-08 21:03:01 +000085 /// SDNodeInfo - One of these records is created for each SDNode instance in
86 /// the target .td file. This represents the various dag nodes we will be
87 /// processing.
88 class SDNodeInfo {
89 Record *Def;
90 std::string EnumName;
91 std::string SDClassName;
Chris Lattnera1a68ae2005-09-28 18:28:29 +000092 unsigned Properties;
Chris Lattner32707602005-09-08 23:22:48 +000093 unsigned NumResults;
94 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000095 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000096 public:
97 SDNodeInfo(Record *R); // Parse the specified record.
98
Chris Lattner32707602005-09-08 23:22:48 +000099 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +0000100 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +0000101 Record *getRecord() const { return Def; }
102 const std::string &getEnumName() const { return EnumName; }
103 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +0000104
Chris Lattner32707602005-09-08 23:22:48 +0000105 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +0000106 return TypeConstraints;
107 }
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000108
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000109 /// hasProperty - Return true if this node has the specified property.
110 ///
111 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +0000112
113 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
114 /// constraints for this node to the operands of the node. This returns
115 /// true if it makes a change, false otherwise. If a type contradiction is
116 /// found, throw an exception.
117 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
118 bool MadeChange = false;
119 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
120 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
121 return MadeChange;
122 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000123 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000124
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000125 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
126 /// patterns), and as such should be ref counted. We currently just leak all
127 /// TreePatternNode objects!
128 class TreePatternNode {
Nate Begemanb73628b2005-12-30 00:12:56 +0000129 /// The inferred type for this node, or MVT::isUnknown if it hasn't
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000130 /// been determined yet.
Nate Begemanb73628b2005-12-30 00:12:56 +0000131 std::vector<unsigned char> Types;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000132
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000133 /// Operator - The Record for the operator if this is an interior node (not
134 /// a leaf).
135 Record *Operator;
136
137 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
138 ///
139 Init *Val;
140
141 /// Name - The name given to this node with the :$foo notation.
142 ///
143 std::string Name;
144
145 /// PredicateFn - The predicate function to execute on this node to check
146 /// for a match. If this string is empty, no predicate is involved.
147 std::string PredicateFn;
148
Chris Lattner24eeeb82005-09-13 21:51:00 +0000149 /// TransformFn - The transformation function to execute on this node before
150 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000151 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000152
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000153 std::vector<TreePatternNode*> Children;
154 public:
155 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Nate Begemanb73628b2005-12-30 00:12:56 +0000156 : Types(), Operator(Op), Val(0), TransformFn(0),
157 Children(Ch) { Types.push_back(MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000158 TreePatternNode(Init *val) // leaf ctor
Chris Lattner355408b2006-01-29 02:43:35 +0000159 : Types(), Operator(0), Val(val), TransformFn(0) {
160 Types.push_back(MVT::isUnknown);
161 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000162 ~TreePatternNode();
163
164 const std::string &getName() const { return Name; }
165 void setName(const std::string &N) { Name = N; }
166
167 bool isLeaf() const { return Val != 0; }
Evan Cheng2618d072006-05-17 20:37:59 +0000168 bool hasTypeSet() const {
169 return (Types[0] < MVT::LAST_VALUETYPE) || (Types[0] == MVT::iPTR);
170 }
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000171 bool isTypeCompletelyUnknown() const {
Nate Begemanb73628b2005-12-30 00:12:56 +0000172 return Types[0] == MVT::isUnknown;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000173 }
Evan Cheng2618d072006-05-17 20:37:59 +0000174 bool isTypeDynamicallyResolved() const {
175 return Types[0] == MVT::iPTR;
176 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000177 MVT::ValueType getTypeNum(unsigned Num) const {
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000178 assert(hasTypeSet() && "Doesn't have a type yet!");
Nate Begemanb73628b2005-12-30 00:12:56 +0000179 assert(Types.size() > Num && "Type num out of range!");
180 return (MVT::ValueType)Types[Num];
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000181 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000182 unsigned char getExtTypeNum(unsigned Num) const {
183 assert(Types.size() > Num && "Extended type num out of range!");
184 return Types[Num];
185 }
186 const std::vector<unsigned char> &getExtTypes() const { return Types; }
187 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
188 void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000189
190 Init *getLeafValue() const { assert(isLeaf()); return Val; }
191 Record *getOperator() const { assert(!isLeaf()); return Operator; }
192
193 unsigned getNumChildren() const { return Children.size(); }
194 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
195 void setChild(unsigned i, TreePatternNode *N) {
196 Children[i] = N;
197 }
198
Nate Begemanb73628b2005-12-30 00:12:56 +0000199
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000200 const std::string &getPredicateFn() const { return PredicateFn; }
201 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000202
Chris Lattnerb0276202005-09-14 22:55:26 +0000203 Record *getTransformFn() const { return TransformFn; }
204 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000205
206 void print(std::ostream &OS) const;
207 void dump() const;
208
209 public: // Higher level manipulation routines.
210
211 /// clone - Return a new copy of this tree.
212 ///
213 TreePatternNode *clone() const;
214
Chris Lattnere46e17b2005-09-29 19:28:10 +0000215 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
216 /// the specified node. For this comparison, all of the state of the node
217 /// is considered, except for the assigned name. Nodes with differing names
218 /// that are otherwise identical are considered isomorphic.
219 bool isIsomorphicTo(const TreePatternNode *N) const;
220
Chris Lattner32707602005-09-08 23:22:48 +0000221 /// SubstituteFormalArguments - Replace the formal arguments in this tree
222 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000223 void SubstituteFormalArguments(std::map<std::string,
224 TreePatternNode*> &ArgMap);
225
226 /// InlinePatternFragments - If this pattern refers to any pattern
227 /// fragments, inline them into place, giving us a pattern without any
228 /// PatFrag references.
229 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000230
231 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
232 /// this node and its children in the tree. This returns true if it makes a
233 /// change, false otherwise. If a type contradiction is found, throw an
234 /// exception.
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000235 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattner32707602005-09-08 23:22:48 +0000236
237 /// UpdateNodeType - Set the node type of N to VT if VT contains
238 /// information. If N already contains a conflicting type, then throw an
239 /// exception. This returns true if any information was updated.
240 ///
Nate Begemanb73628b2005-12-30 00:12:56 +0000241 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
242 TreePattern &TP);
243 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
244 std::vector<unsigned char> ExtVTs(1, ExtVT);
245 return UpdateNodeType(ExtVTs, TP);
246 }
Chris Lattner32707602005-09-08 23:22:48 +0000247
248 /// ContainsUnresolvedType - Return true if this tree contains any
249 /// unresolved types.
250 bool ContainsUnresolvedType() const {
Evan Cheng2618d072006-05-17 20:37:59 +0000251 if (!hasTypeSet() && !isTypeDynamicallyResolved()) return true;
Chris Lattner32707602005-09-08 23:22:48 +0000252 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
253 if (getChild(i)->ContainsUnresolvedType()) return true;
254 return false;
255 }
Chris Lattnere97603f2005-09-28 19:27:25 +0000256
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000257 /// canPatternMatch - If it is impossible for this pattern to match on this
258 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattnere97603f2005-09-28 19:27:25 +0000259 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000260 };
261
262
Chris Lattneree9f0c32005-09-13 21:20:49 +0000263 /// TreePattern - Represent a pattern, used for instructions, pattern
264 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000265 ///
266 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000267 /// Trees - The list of pattern trees which corresponds to this pattern.
268 /// Note that PatFrag's only have a single tree.
269 ///
270 std::vector<TreePatternNode*> Trees;
271
272 /// TheRecord - The actual TableGen record corresponding to this pattern.
273 ///
274 Record *TheRecord;
275
276 /// Args - This is a list of all of the arguments to this pattern (for
277 /// PatFrag patterns), which are the 'node' markers in this pattern.
278 std::vector<std::string> Args;
279
280 /// ISE - the DAG isel emitter coordinating this madness.
281 ///
282 DAGISelEmitter &ISE;
Chris Lattneredbd8712005-10-21 01:19:59 +0000283
284 /// isInputPattern - True if this is an input pattern, something to match.
285 /// False if this is an output pattern, something to emit.
286 bool isInputPattern;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000287 public:
288
289 /// TreePattern constructor - Parse the specified DagInits into the
290 /// current record.
Chris Lattneredbd8712005-10-21 01:19:59 +0000291 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
292 DAGISelEmitter &ise);
293 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
294 DAGISelEmitter &ise);
295 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
296 DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000297
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000298 /// getTrees - Return the tree patterns which corresponds to this pattern.
299 ///
300 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000301 unsigned getNumTrees() const { return Trees.size(); }
302 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000303 TreePatternNode *getOnlyTree() const {
304 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
305 return Trees[0];
306 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000307
308 /// getRecord - Return the actual TableGen record corresponding to this
309 /// pattern.
310 ///
311 Record *getRecord() const { return TheRecord; }
312
313 unsigned getNumArgs() const { return Args.size(); }
314 const std::string &getArgName(unsigned i) const {
315 assert(i < Args.size() && "Argument reference out of range!");
316 return Args[i];
317 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000318 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000319
320 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
321
322 /// InlinePatternFragments - If this pattern refers to any pattern
323 /// fragments, inline them into place, giving us a pattern without any
324 /// PatFrag references.
325 void InlinePatternFragments() {
326 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
327 Trees[i] = Trees[i]->InlinePatternFragments(*this);
328 }
329
Chris Lattner32707602005-09-08 23:22:48 +0000330 /// InferAllTypes - Infer/propagate as many types throughout the expression
331 /// patterns as possible. Return true if all types are infered, false
332 /// otherwise. Throw an exception if a type contradiction is found.
333 bool InferAllTypes();
334
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000335 /// error - Throw an exception, prefixing it with information about this
336 /// pattern.
337 void error(const std::string &Msg) const;
338
339 void print(std::ostream &OS) const;
340 void dump() const;
341
342 private:
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000343 TreePatternNode *ParseTreePattern(DagInit *DI);
344 };
Chris Lattnerec676432005-09-14 04:03:16 +0000345
Evan Chenga9559392007-07-06 01:05:26 +0000346 /// DAGDefaultOperand - One of these is created for each PredicateOperand
347 /// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
348 struct DAGDefaultOperand {
349 std::vector<TreePatternNode*> DefaultOps;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000350 };
Chris Lattnerec676432005-09-14 04:03:16 +0000351
352 class DAGInstruction {
353 TreePattern *Pattern;
Nate Begemanddb39542005-12-01 00:06:14 +0000354 std::vector<Record*> Results;
355 std::vector<Record*> Operands;
Evan Chengbcecf332005-12-17 01:19:28 +0000356 std::vector<Record*> ImpResults;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000357 std::vector<Record*> ImpOperands;
Chris Lattnerb0276202005-09-14 22:55:26 +0000358 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000359 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000360 DAGInstruction(TreePattern *TP,
Nate Begemanddb39542005-12-01 00:06:14 +0000361 const std::vector<Record*> &results,
Evan Chengbcecf332005-12-17 01:19:28 +0000362 const std::vector<Record*> &operands,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000363 const std::vector<Record*> &impresults,
364 const std::vector<Record*> &impoperands)
Nate Begemanddb39542005-12-01 00:06:14 +0000365 : Pattern(TP), Results(results), Operands(operands),
Evan Cheng7b05bd52005-12-23 22:11:47 +0000366 ImpResults(impresults), ImpOperands(impoperands),
367 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000368
Chris Lattnerec676432005-09-14 04:03:16 +0000369 TreePattern *getPattern() const { return Pattern; }
Nate Begemanddb39542005-12-01 00:06:14 +0000370 unsigned getNumResults() const { return Results.size(); }
371 unsigned getNumOperands() const { return Operands.size(); }
Evan Chengbcecf332005-12-17 01:19:28 +0000372 unsigned getNumImpResults() const { return ImpResults.size(); }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000373 unsigned getNumImpOperands() const { return ImpOperands.size(); }
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000374 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000375
Chris Lattnera28aec12005-09-15 22:23:50 +0000376 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
377
Nate Begemanddb39542005-12-01 00:06:14 +0000378 Record *getResult(unsigned RN) const {
379 assert(RN < Results.size());
380 return Results[RN];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000381 }
382
Nate Begemanddb39542005-12-01 00:06:14 +0000383 Record *getOperand(unsigned ON) const {
384 assert(ON < Operands.size());
385 return Operands[ON];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000386 }
Evan Chengbcecf332005-12-17 01:19:28 +0000387
388 Record *getImpResult(unsigned RN) const {
389 assert(RN < ImpResults.size());
390 return ImpResults[RN];
391 }
392
Evan Cheng7b05bd52005-12-23 22:11:47 +0000393 Record *getImpOperand(unsigned ON) const {
394 assert(ON < ImpOperands.size());
395 return ImpOperands[ON];
396 }
397
Chris Lattnerb0276202005-09-14 22:55:26 +0000398 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000399 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000400
Evan Cheng58e84a62005-12-14 22:02:59 +0000401/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
402/// to produce isel.
403struct PatternToMatch {
Evan Cheng59413202006-04-19 18:07:24 +0000404 PatternToMatch(ListInit *preds,
Evan Chengc81d2a02006-04-19 20:36:09 +0000405 TreePatternNode *src, TreePatternNode *dst,
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000406 const std::vector<Record*> &dstregs,
Evan Chengc81d2a02006-04-19 20:36:09 +0000407 unsigned complexity):
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000408 Predicates(preds), SrcPattern(src), DstPattern(dst), Dstregs(dstregs),
Evan Chengc81d2a02006-04-19 20:36:09 +0000409 AddedComplexity(complexity) {};
Evan Cheng58e84a62005-12-14 22:02:59 +0000410
411 ListInit *Predicates; // Top level predicate conditions to match.
412 TreePatternNode *SrcPattern; // Source pattern to match.
413 TreePatternNode *DstPattern; // Resulting pattern.
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000414 std::vector<Record*> Dstregs; // Physical register defs being matched.
Evan Chengc81d2a02006-04-19 20:36:09 +0000415 unsigned AddedComplexity; // Add to matching pattern complexity.
Evan Cheng58e84a62005-12-14 22:02:59 +0000416
417 ListInit *getPredicates() const { return Predicates; }
418 TreePatternNode *getSrcPattern() const { return SrcPattern; }
419 TreePatternNode *getDstPattern() const { return DstPattern; }
Evan Cheng85dbe1a2007-09-12 23:30:14 +0000420 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Evan Chengc81d2a02006-04-19 20:36:09 +0000421 unsigned getAddedComplexity() const { return AddedComplexity; }
Evan Cheng58e84a62005-12-14 22:02:59 +0000422};
423
Chris Lattner09fb7d42006-01-17 21:31:18 +0000424/// DAGISelEmitter - The top-level class which coordinates construction
Chris Lattner4a24c642005-09-03 01:14:03 +0000425/// and emission of the instruction selector.
426///
427class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000428private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000429 RecordKeeper &Records;
430 CodeGenTarget Target;
Chris Lattner550525e2006-03-24 21:48:51 +0000431 std::vector<CodeGenIntrinsic> Intrinsics;
Evan Cheng21ad3922006-02-07 00:37:41 +0000432
Chris Lattnerca559d02005-09-08 21:03:01 +0000433 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000434 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Evan Cheng0fc71982005-12-08 02:00:36 +0000435 std::map<Record*, ComplexPattern> ComplexPatterns;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000436 std::map<Record*, TreePattern*> PatternFragments;
Evan Chenga9559392007-07-06 01:05:26 +0000437 std::map<Record*, DAGDefaultOperand> DefaultOperands;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000438 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000439
Chris Lattner5a1df382006-03-24 23:10:39 +0000440 // Specific SDNode definitions:
441 Record *intrinsic_void_sdnode;
442 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
443
Chris Lattner1f39e292005-09-14 00:09:24 +0000444 /// PatternsToMatch - All of the things we are matching on the DAG. The first
445 /// value is the pattern to match, the second pattern is the result to
446 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000447 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000448public:
449 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
450
451 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000452 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000453
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000454 const CodeGenTarget &getTargetInfo() const { return Target; }
455
Chris Lattner0614b622005-11-02 06:49:14 +0000456 Record *getSDNodeNamed(const std::string &Name) const;
457
Chris Lattnerca559d02005-09-08 21:03:01 +0000458 const SDNodeInfo &getSDNodeInfo(Record *R) const {
459 assert(SDNodes.count(R) && "Unknown node!");
460 return SDNodes.find(R)->second;
461 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000462
Chris Lattner6de8b532005-09-13 21:59:15 +0000463 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
464 assert(SDNodeXForms.count(R) && "Invalid transform!");
465 return SDNodeXForms.find(R)->second;
466 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000467
468 const ComplexPattern &getComplexPattern(Record *R) const {
469 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
470 return ComplexPatterns.find(R)->second;
471 }
472
Chris Lattner550525e2006-03-24 21:48:51 +0000473 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
474 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
475 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
476 assert(0 && "Unknown intrinsic!");
477 abort();
478 }
479
Chris Lattner5a1df382006-03-24 23:10:39 +0000480 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
481 assert(IID-1 < Intrinsics.size() && "Bad intrinsic ID!");
482 return Intrinsics[IID-1];
483 }
484
485 unsigned getIntrinsicID(Record *R) const {
486 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
487 if (Intrinsics[i].TheDef == R) return i;
488 assert(0 && "Unknown intrinsic!");
489 abort();
490 }
491
Evan Chenga9559392007-07-06 01:05:26 +0000492 const DAGDefaultOperand &getDefaultOperand(Record *R) {
493 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
494 return DefaultOperands.find(R)->second;
Chris Lattnerefe9f4a2006-11-04 05:12:02 +0000495 }
496
Evan Cheng0fc71982005-12-08 02:00:36 +0000497 TreePattern *getPatternFragment(Record *R) const {
498 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
499 return PatternFragments.find(R)->second;
500 }
Chris Lattner6de8b532005-09-13 21:59:15 +0000501
Chris Lattnerae5b3502005-09-15 21:57:35 +0000502 const DAGInstruction &getInstruction(Record *R) const {
503 assert(Instructions.count(R) && "Unknown instruction!");
504 return Instructions.find(R)->second;
505 }
506
Chris Lattner5a1df382006-03-24 23:10:39 +0000507 Record *get_intrinsic_void_sdnode() const {
508 return intrinsic_void_sdnode;
509 }
510 Record *get_intrinsic_w_chain_sdnode() const {
511 return intrinsic_w_chain_sdnode;
512 }
513 Record *get_intrinsic_wo_chain_sdnode() const {
514 return intrinsic_wo_chain_sdnode;
515 }
516
517
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000518private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000519 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000520 void ParseNodeTransforms(std::ostream &OS);
Evan Cheng0fc71982005-12-08 02:00:36 +0000521 void ParseComplexPatterns();
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000522 void ParsePatternFragments(std::ostream &OS);
Evan Chenga9559392007-07-06 01:05:26 +0000523 void ParseDefaultOperands();
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000524 void ParseInstructions();
525 void ParsePatterns();
Chris Lattnere97603f2005-09-28 19:27:25 +0000526 void GenerateVariants();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000527 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
528 std::map<std::string,
529 TreePatternNode*> &InstInputs,
Evan Cheng420132e2006-03-20 06:04:09 +0000530 std::map<std::string,
531 TreePatternNode*> &InstResults,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000532 std::vector<Record*> &InstImpInputs,
Evan Chengbcecf332005-12-17 01:19:28 +0000533 std::vector<Record*> &InstImpResults);
Chris Lattner8bc74722006-01-29 04:25:26 +0000534 void GenerateCodeForPattern(PatternToMatch &Pattern,
Evan Cheng676d7312006-08-26 00:59:04 +0000535 std::vector<std::pair<unsigned, std::string> > &GeneratedCode,
Evan Chengf5493192006-08-26 01:02:19 +0000536 std::set<std::string> &GeneratedDecl,
Evan Chengfceb57a2006-07-15 08:45:20 +0000537 std::vector<std::string> &TargetOpcodes,
Evan Cheng9bdca032006-08-07 22:17:58 +0000538 std::vector<std::string> &TargetVTs);
Chris Lattner8bc74722006-01-29 04:25:26 +0000539 void EmitPatterns(std::vector<std::pair<PatternToMatch*,
Evan Cheng676d7312006-08-26 00:59:04 +0000540 std::vector<std::pair<unsigned, std::string> > > > &Patterns,
Chris Lattner8bc74722006-01-29 04:25:26 +0000541 unsigned Indent, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000542 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000543};
544
545} // End llvm namespace
546
547#endif