blob: 8ed66be800a214826ccfb320c3ab92b54c5c6ae5 [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"
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
109 // SelectionDAG node properties.
Evan Cheng51fecc82006-01-09 18:27:06 +0000110 enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain,
111 SDNPOutFlag, SDNPInFlag, SDNPOptInFlag };
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000112
113 /// hasProperty - Return true if this node has the specified property.
114 ///
115 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +0000116
117 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
118 /// constraints for this node to the operands of the node. This returns
119 /// true if it makes a change, false otherwise. If a type contradiction is
120 /// found, throw an exception.
121 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
122 bool MadeChange = false;
123 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
124 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
125 return MadeChange;
126 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000127 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000128
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000129 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
130 /// patterns), and as such should be ref counted. We currently just leak all
131 /// TreePatternNode objects!
132 class TreePatternNode {
Nate Begemanb73628b2005-12-30 00:12:56 +0000133 /// The inferred type for this node, or MVT::isUnknown if it hasn't
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000134 /// been determined yet.
Nate Begemanb73628b2005-12-30 00:12:56 +0000135 std::vector<unsigned char> Types;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000136
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000137 /// Operator - The Record for the operator if this is an interior node (not
138 /// a leaf).
139 Record *Operator;
140
141 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
142 ///
143 Init *Val;
144
145 /// Name - The name given to this node with the :$foo notation.
146 ///
147 std::string Name;
148
149 /// PredicateFn - The predicate function to execute on this node to check
150 /// for a match. If this string is empty, no predicate is involved.
151 std::string PredicateFn;
152
Chris Lattner24eeeb82005-09-13 21:51:00 +0000153 /// TransformFn - The transformation function to execute on this node before
154 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000155 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000156
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000157 std::vector<TreePatternNode*> Children;
158 public:
159 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Nate Begemanb73628b2005-12-30 00:12:56 +0000160 : Types(), Operator(Op), Val(0), TransformFn(0),
161 Children(Ch) { Types.push_back(MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000162 TreePatternNode(Init *val) // leaf ctor
Chris Lattner355408b2006-01-29 02:43:35 +0000163 : Types(), Operator(0), Val(val), TransformFn(0) {
164 Types.push_back(MVT::isUnknown);
165 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000166 ~TreePatternNode();
167
168 const std::string &getName() const { return Name; }
169 void setName(const std::string &N) { Name = N; }
170
171 bool isLeaf() const { return Val != 0; }
Nate Begemanb73628b2005-12-30 00:12:56 +0000172 bool hasTypeSet() const { return Types[0] < MVT::LAST_VALUETYPE; }
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000173 bool isTypeCompletelyUnknown() const {
Nate Begemanb73628b2005-12-30 00:12:56 +0000174 return Types[0] == MVT::isUnknown;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000175 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000176 MVT::ValueType getTypeNum(unsigned Num) const {
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000177 assert(hasTypeSet() && "Doesn't have a type yet!");
Nate Begemanb73628b2005-12-30 00:12:56 +0000178 assert(Types.size() > Num && "Type num out of range!");
179 return (MVT::ValueType)Types[Num];
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000180 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000181 unsigned char getExtTypeNum(unsigned Num) const {
182 assert(Types.size() > Num && "Extended type num out of range!");
183 return Types[Num];
184 }
185 const std::vector<unsigned char> &getExtTypes() const { return Types; }
186 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
187 void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000188
189 Init *getLeafValue() const { assert(isLeaf()); return Val; }
190 Record *getOperator() const { assert(!isLeaf()); return Operator; }
191
192 unsigned getNumChildren() const { return Children.size(); }
193 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
194 void setChild(unsigned i, TreePatternNode *N) {
195 Children[i] = N;
196 }
197
Nate Begemanb73628b2005-12-30 00:12:56 +0000198
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000199 const std::string &getPredicateFn() const { return PredicateFn; }
200 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000201
Chris Lattnerb0276202005-09-14 22:55:26 +0000202 Record *getTransformFn() const { return TransformFn; }
203 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000204
205 void print(std::ostream &OS) const;
206 void dump() const;
207
208 public: // Higher level manipulation routines.
209
210 /// clone - Return a new copy of this tree.
211 ///
212 TreePatternNode *clone() const;
213
Chris Lattnere46e17b2005-09-29 19:28:10 +0000214 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
215 /// the specified node. For this comparison, all of the state of the node
216 /// is considered, except for the assigned name. Nodes with differing names
217 /// that are otherwise identical are considered isomorphic.
218 bool isIsomorphicTo(const TreePatternNode *N) const;
219
Chris Lattner32707602005-09-08 23:22:48 +0000220 /// SubstituteFormalArguments - Replace the formal arguments in this tree
221 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000222 void SubstituteFormalArguments(std::map<std::string,
223 TreePatternNode*> &ArgMap);
224
225 /// InlinePatternFragments - If this pattern refers to any pattern
226 /// fragments, inline them into place, giving us a pattern without any
227 /// PatFrag references.
228 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000229
230 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
231 /// this node and its children in the tree. This returns true if it makes a
232 /// change, false otherwise. If a type contradiction is found, throw an
233 /// exception.
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000234 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattner32707602005-09-08 23:22:48 +0000235
236 /// UpdateNodeType - Set the node type of N to VT if VT contains
237 /// information. If N already contains a conflicting type, then throw an
238 /// exception. This returns true if any information was updated.
239 ///
Nate Begemanb73628b2005-12-30 00:12:56 +0000240 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
241 TreePattern &TP);
242 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
243 std::vector<unsigned char> ExtVTs(1, ExtVT);
244 return UpdateNodeType(ExtVTs, TP);
245 }
Chris Lattner32707602005-09-08 23:22:48 +0000246
247 /// ContainsUnresolvedType - Return true if this tree contains any
248 /// unresolved types.
249 bool ContainsUnresolvedType() const {
Chris Lattner2ac85102005-10-19 04:12:14 +0000250 if (!hasTypeSet()) return true;
Chris Lattner32707602005-09-08 23:22:48 +0000251 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
252 if (getChild(i)->ContainsUnresolvedType()) return true;
253 return false;
254 }
Chris Lattnere97603f2005-09-28 19:27:25 +0000255
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000256 /// canPatternMatch - If it is impossible for this pattern to match on this
257 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattnere97603f2005-09-28 19:27:25 +0000258 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000259 };
260
261
Chris Lattneree9f0c32005-09-13 21:20:49 +0000262 /// TreePattern - Represent a pattern, used for instructions, pattern
263 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000264 ///
265 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000266 /// Trees - The list of pattern trees which corresponds to this pattern.
267 /// Note that PatFrag's only have a single tree.
268 ///
269 std::vector<TreePatternNode*> Trees;
270
271 /// TheRecord - The actual TableGen record corresponding to this pattern.
272 ///
273 Record *TheRecord;
274
275 /// Args - This is a list of all of the arguments to this pattern (for
276 /// PatFrag patterns), which are the 'node' markers in this pattern.
277 std::vector<std::string> Args;
278
279 /// ISE - the DAG isel emitter coordinating this madness.
280 ///
281 DAGISelEmitter &ISE;
Chris Lattneredbd8712005-10-21 01:19:59 +0000282
283 /// isInputPattern - True if this is an input pattern, something to match.
284 /// False if this is an output pattern, something to emit.
285 bool isInputPattern;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000286 public:
287
288 /// TreePattern constructor - Parse the specified DagInits into the
289 /// current record.
Chris Lattneredbd8712005-10-21 01:19:59 +0000290 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
291 DAGISelEmitter &ise);
292 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
293 DAGISelEmitter &ise);
294 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
295 DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000296
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000297 /// getTrees - Return the tree patterns which corresponds to this pattern.
298 ///
299 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000300 unsigned getNumTrees() const { return Trees.size(); }
301 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000302 TreePatternNode *getOnlyTree() const {
303 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
304 return Trees[0];
305 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000306
307 /// getRecord - Return the actual TableGen record corresponding to this
308 /// pattern.
309 ///
310 Record *getRecord() const { return TheRecord; }
311
312 unsigned getNumArgs() const { return Args.size(); }
313 const std::string &getArgName(unsigned i) const {
314 assert(i < Args.size() && "Argument reference out of range!");
315 return Args[i];
316 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000317 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000318
319 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
320
321 /// InlinePatternFragments - If this pattern refers to any pattern
322 /// fragments, inline them into place, giving us a pattern without any
323 /// PatFrag references.
324 void InlinePatternFragments() {
325 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
326 Trees[i] = Trees[i]->InlinePatternFragments(*this);
327 }
328
Chris Lattner32707602005-09-08 23:22:48 +0000329 /// InferAllTypes - Infer/propagate as many types throughout the expression
330 /// patterns as possible. Return true if all types are infered, false
331 /// otherwise. Throw an exception if a type contradiction is found.
332 bool InferAllTypes();
333
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000334 /// error - Throw an exception, prefixing it with information about this
335 /// pattern.
336 void error(const std::string &Msg) const;
337
338 void print(std::ostream &OS) const;
339 void dump() const;
340
341 private:
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000342 TreePatternNode *ParseTreePattern(DagInit *DI);
343 };
Chris Lattnerec676432005-09-14 04:03:16 +0000344
345
346 class DAGInstruction {
347 TreePattern *Pattern;
Nate Begemanddb39542005-12-01 00:06:14 +0000348 std::vector<Record*> Results;
349 std::vector<Record*> Operands;
Evan Chengbcecf332005-12-17 01:19:28 +0000350 std::vector<Record*> ImpResults;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000351 std::vector<Record*> ImpOperands;
Chris Lattnerb0276202005-09-14 22:55:26 +0000352 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000353 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000354 DAGInstruction(TreePattern *TP,
Nate Begemanddb39542005-12-01 00:06:14 +0000355 const std::vector<Record*> &results,
Evan Chengbcecf332005-12-17 01:19:28 +0000356 const std::vector<Record*> &operands,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000357 const std::vector<Record*> &impresults,
358 const std::vector<Record*> &impoperands)
Nate Begemanddb39542005-12-01 00:06:14 +0000359 : Pattern(TP), Results(results), Operands(operands),
Evan Cheng7b05bd52005-12-23 22:11:47 +0000360 ImpResults(impresults), ImpOperands(impoperands),
361 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000362
Chris Lattnerec676432005-09-14 04:03:16 +0000363 TreePattern *getPattern() const { return Pattern; }
Nate Begemanddb39542005-12-01 00:06:14 +0000364 unsigned getNumResults() const { return Results.size(); }
365 unsigned getNumOperands() const { return Operands.size(); }
Evan Chengbcecf332005-12-17 01:19:28 +0000366 unsigned getNumImpResults() const { return ImpResults.size(); }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000367 unsigned getNumImpOperands() const { return ImpOperands.size(); }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000368
Chris Lattnera28aec12005-09-15 22:23:50 +0000369 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
370
Nate Begemanddb39542005-12-01 00:06:14 +0000371 Record *getResult(unsigned RN) const {
372 assert(RN < Results.size());
373 return Results[RN];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000374 }
375
Nate Begemanddb39542005-12-01 00:06:14 +0000376 Record *getOperand(unsigned ON) const {
377 assert(ON < Operands.size());
378 return Operands[ON];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000379 }
Evan Chengbcecf332005-12-17 01:19:28 +0000380
381 Record *getImpResult(unsigned RN) const {
382 assert(RN < ImpResults.size());
383 return ImpResults[RN];
384 }
385
Evan Cheng7b05bd52005-12-23 22:11:47 +0000386 Record *getImpOperand(unsigned ON) const {
387 assert(ON < ImpOperands.size());
388 return ImpOperands[ON];
389 }
390
Chris Lattnerb0276202005-09-14 22:55:26 +0000391 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000392 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000393
Evan Cheng58e84a62005-12-14 22:02:59 +0000394/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
395/// to produce isel.
396struct PatternToMatch {
397 PatternToMatch(ListInit *preds, TreePatternNode *src, TreePatternNode *dst):
398 Predicates(preds), SrcPattern(src), DstPattern(dst) {};
399
400 ListInit *Predicates; // Top level predicate conditions to match.
401 TreePatternNode *SrcPattern; // Source pattern to match.
402 TreePatternNode *DstPattern; // Resulting pattern.
403
404 ListInit *getPredicates() const { return Predicates; }
405 TreePatternNode *getSrcPattern() const { return SrcPattern; }
406 TreePatternNode *getDstPattern() const { return DstPattern; }
407};
408
Chris Lattner09fb7d42006-01-17 21:31:18 +0000409/// DAGISelEmitter - The top-level class which coordinates construction
Chris Lattner4a24c642005-09-03 01:14:03 +0000410/// and emission of the instruction selector.
411///
412class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000413private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000414 RecordKeeper &Records;
415 CodeGenTarget Target;
Chris Lattner550525e2006-03-24 21:48:51 +0000416 std::vector<CodeGenIntrinsic> Intrinsics;
Evan Cheng21ad3922006-02-07 00:37:41 +0000417
Chris Lattnerca559d02005-09-08 21:03:01 +0000418 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000419 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Evan Cheng0fc71982005-12-08 02:00:36 +0000420 std::map<Record*, ComplexPattern> ComplexPatterns;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000421 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000422 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000423
Chris Lattner5a1df382006-03-24 23:10:39 +0000424 // Specific SDNode definitions:
425 Record *intrinsic_void_sdnode;
426 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
427
Chris Lattner1f39e292005-09-14 00:09:24 +0000428 /// PatternsToMatch - All of the things we are matching on the DAG. The first
429 /// value is the pattern to match, the second pattern is the result to
430 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000431 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000432public:
433 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
434
435 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000436 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000437
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000438 const CodeGenTarget &getTargetInfo() const { return Target; }
439
Chris Lattner0614b622005-11-02 06:49:14 +0000440 Record *getSDNodeNamed(const std::string &Name) const;
441
Chris Lattnerca559d02005-09-08 21:03:01 +0000442 const SDNodeInfo &getSDNodeInfo(Record *R) const {
443 assert(SDNodes.count(R) && "Unknown node!");
444 return SDNodes.find(R)->second;
445 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000446
Chris Lattner6de8b532005-09-13 21:59:15 +0000447 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
448 assert(SDNodeXForms.count(R) && "Invalid transform!");
449 return SDNodeXForms.find(R)->second;
450 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000451
452 const ComplexPattern &getComplexPattern(Record *R) const {
453 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
454 return ComplexPatterns.find(R)->second;
455 }
456
Chris Lattner550525e2006-03-24 21:48:51 +0000457 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
458 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
459 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
460 assert(0 && "Unknown intrinsic!");
461 abort();
462 }
463
Chris Lattner5a1df382006-03-24 23:10:39 +0000464 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
465 assert(IID-1 < Intrinsics.size() && "Bad intrinsic ID!");
466 return Intrinsics[IID-1];
467 }
468
469 unsigned getIntrinsicID(Record *R) const {
470 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
471 if (Intrinsics[i].TheDef == R) return i;
472 assert(0 && "Unknown intrinsic!");
473 abort();
474 }
475
Evan Cheng0fc71982005-12-08 02:00:36 +0000476 TreePattern *getPatternFragment(Record *R) const {
477 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
478 return PatternFragments.find(R)->second;
479 }
Chris Lattner6de8b532005-09-13 21:59:15 +0000480
Chris Lattnerae5b3502005-09-15 21:57:35 +0000481 const DAGInstruction &getInstruction(Record *R) const {
482 assert(Instructions.count(R) && "Unknown instruction!");
483 return Instructions.find(R)->second;
484 }
485
Chris Lattner5a1df382006-03-24 23:10:39 +0000486 Record *get_intrinsic_void_sdnode() const {
487 return intrinsic_void_sdnode;
488 }
489 Record *get_intrinsic_w_chain_sdnode() const {
490 return intrinsic_w_chain_sdnode;
491 }
492 Record *get_intrinsic_wo_chain_sdnode() const {
493 return intrinsic_wo_chain_sdnode;
494 }
495
496
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000497private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000498 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000499 void ParseNodeTransforms(std::ostream &OS);
Evan Cheng0fc71982005-12-08 02:00:36 +0000500 void ParseComplexPatterns();
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000501 void ParsePatternFragments(std::ostream &OS);
502 void ParseInstructions();
503 void ParsePatterns();
Chris Lattnere97603f2005-09-28 19:27:25 +0000504 void GenerateVariants();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000505 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
506 std::map<std::string,
507 TreePatternNode*> &InstInputs,
Evan Cheng420132e2006-03-20 06:04:09 +0000508 std::map<std::string,
509 TreePatternNode*> &InstResults,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000510 std::vector<Record*> &InstImpInputs,
Evan Chengbcecf332005-12-17 01:19:28 +0000511 std::vector<Record*> &InstImpResults);
Chris Lattner8bc74722006-01-29 04:25:26 +0000512 void GenerateCodeForPattern(PatternToMatch &Pattern,
Evan Chenge41bf822006-02-05 06:43:12 +0000513 std::vector<std::pair<bool, std::string> > &GeneratedCode,
Evan Chengd7805a72006-02-09 07:16:09 +0000514 std::set<std::pair<bool, std::string> > &GeneratedDecl,
Evan Chenge41bf822006-02-05 06:43:12 +0000515 bool UseGoto);
Chris Lattner8bc74722006-01-29 04:25:26 +0000516 void EmitPatterns(std::vector<std::pair<PatternToMatch*,
517 std::vector<std::pair<bool, std::string> > > > &Patterns,
518 unsigned Indent, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000519 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000520};
521
522} // End llvm namespace
523
524#endif