blob: 904091b5a4f7ceab520ecee0d5f0211704336b6a [file] [log] [blame]
Chris Lattner90d00042005-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 Lattnerd2a5b362005-09-07 23:44:43 +000021 class Record;
Jeff Cohen0dce12d2005-09-10 02:00:02 +000022 struct Init;
Chris Lattner59e96142005-09-15 22:23:50 +000023 class ListInit;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000024 class DagInit;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000025 class SDNodeInfo;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000026 class TreePattern;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000027 class TreePatternNode;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000028 class DAGISelEmitter;
Evan Cheng9b9567b2005-12-08 02:00:36 +000029 class ComplexPattern;
Chris Lattner35bcd142005-09-08 21:03:01 +000030
Chris Lattnerecaf56b2005-10-14 06:12:03 +000031 /// MVT::DAGISelGenValueType - These are some extended forms of MVT::ValueType
32 /// that we use as lattice values during type inferrence.
33 namespace MVT {
34 enum DAGISelGenValueType {
35 isFP = MVT::LAST_VALUETYPE,
36 isInt,
37 isUnknown
38 };
39 }
40
Chris Lattner1c331042005-09-08 21:27:15 +000041 /// SDTypeConstraint - This is a discriminated union of constraints,
42 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
43 struct SDTypeConstraint {
44 SDTypeConstraint(Record *R);
45
46 unsigned OperandNo; // The operand # this constraint applies to.
47 enum {
Chris Lattner433573f2005-12-09 22:57:42 +000048 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs,
49 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp
Chris Lattner1c331042005-09-08 21:27:15 +000050 } ConstraintType;
51
52 union { // The discriminated union.
53 struct {
54 MVT::ValueType VT;
55 } SDTCisVT_Info;
56 struct {
57 unsigned OtherOperandNum;
58 } SDTCisSameAs_Info;
59 struct {
60 unsigned OtherOperandNum;
61 } SDTCisVTSmallerThanOp_Info;
Chris Lattner4892df32005-10-14 04:53:53 +000062 struct {
63 unsigned BigOperandNum;
64 } SDTCisOpSmallerThanOp_Info;
Chris Lattner1c331042005-09-08 21:27:15 +000065 } x;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000066
67 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
68 /// constraint to the nodes operands. This returns true if it makes a
69 /// change, false otherwise. If a type contradiction is found, throw an
70 /// exception.
71 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
72 TreePattern &TP) const;
73
74 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
75 /// N, which has NumResults results.
76 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
77 unsigned NumResults) const;
Chris Lattner1c331042005-09-08 21:27:15 +000078 };
79
Chris Lattner35bcd142005-09-08 21:03:01 +000080 /// SDNodeInfo - One of these records is created for each SDNode instance in
81 /// the target .td file. This represents the various dag nodes we will be
82 /// processing.
83 class SDNodeInfo {
84 Record *Def;
85 std::string EnumName;
86 std::string SDClassName;
Chris Lattnerf74c30c2005-09-28 18:28:29 +000087 unsigned Properties;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000088 unsigned NumResults;
89 int NumOperands;
Chris Lattner1c331042005-09-08 21:27:15 +000090 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattner35bcd142005-09-08 21:03:01 +000091 public:
92 SDNodeInfo(Record *R); // Parse the specified record.
93
Chris Lattnerd7d31f32005-09-08 23:22:48 +000094 unsigned getNumResults() const { return NumResults; }
Chris Lattner1c331042005-09-08 21:27:15 +000095 int getNumOperands() const { return NumOperands; }
Chris Lattner35bcd142005-09-08 21:03:01 +000096 Record *getRecord() const { return Def; }
97 const std::string &getEnumName() const { return EnumName; }
98 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner1c331042005-09-08 21:27:15 +000099
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000100 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner1c331042005-09-08 21:27:15 +0000101 return TypeConstraints;
102 }
Chris Lattnerf74c30c2005-09-28 18:28:29 +0000103
104 // SelectionDAG node properties.
Evan Cheng4b0623e2006-01-09 18:27:06 +0000105 enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain,
106 SDNPOutFlag, SDNPInFlag, SDNPOptInFlag };
Chris Lattnerf74c30c2005-09-28 18:28:29 +0000107
108 /// hasProperty - Return true if this node has the specified property.
109 ///
110 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000111
112 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
113 /// constraints for this node to the operands of the node. This returns
114 /// true if it makes a change, false otherwise. If a type contradiction is
115 /// found, throw an exception.
116 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
117 bool MadeChange = false;
118 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
119 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
120 return MadeChange;
121 }
Chris Lattner35bcd142005-09-08 21:03:01 +0000122 };
Chris Lattner90d00042005-09-03 01:14:03 +0000123
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000124 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
125 /// patterns), and as such should be ref counted. We currently just leak all
126 /// TreePatternNode objects!
127 class TreePatternNode {
Nate Begeman336dba62005-12-30 00:12:56 +0000128 /// The inferred type for this node, or MVT::isUnknown if it hasn't
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000129 /// been determined yet.
Nate Begeman336dba62005-12-30 00:12:56 +0000130 std::vector<unsigned char> Types;
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000131
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000132 /// Operator - The Record for the operator if this is an interior node (not
133 /// a leaf).
134 Record *Operator;
135
136 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
137 ///
138 Init *Val;
139
140 /// Name - The name given to this node with the :$foo notation.
141 ///
142 std::string Name;
143
144 /// PredicateFn - The predicate function to execute on this node to check
145 /// for a match. If this string is empty, no predicate is involved.
146 std::string PredicateFn;
147
Chris Lattner2617de42005-09-13 21:51:00 +0000148 /// TransformFn - The transformation function to execute on this node before
149 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000150 Record *TransformFn;
Chris Lattner2617de42005-09-13 21:51:00 +0000151
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000152 std::vector<TreePatternNode*> Children;
153 public:
154 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Nate Begeman336dba62005-12-30 00:12:56 +0000155 : Types(), Operator(Op), Val(0), TransformFn(0),
156 Children(Ch) { Types.push_back(MVT::isUnknown); }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000157 TreePatternNode(Init *val) // leaf ctor
Nate Begeman336dba62005-12-30 00:12:56 +0000158 : Types(), Operator(0), Val(val), TransformFn(0) { Types.push_back(MVT::isUnknown); }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000159 ~TreePatternNode();
160
161 const std::string &getName() const { return Name; }
162 void setName(const std::string &N) { Name = N; }
163
164 bool isLeaf() const { return Val != 0; }
Nate Begeman336dba62005-12-30 00:12:56 +0000165 bool hasTypeSet() const { return Types[0] < MVT::LAST_VALUETYPE; }
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000166 bool isTypeCompletelyUnknown() const {
Nate Begeman336dba62005-12-30 00:12:56 +0000167 return Types[0] == MVT::isUnknown;
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000168 }
Nate Begeman336dba62005-12-30 00:12:56 +0000169 MVT::ValueType getTypeNum(unsigned Num) const {
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000170 assert(hasTypeSet() && "Doesn't have a type yet!");
Nate Begeman336dba62005-12-30 00:12:56 +0000171 assert(Types.size() > Num && "Type num out of range!");
172 return (MVT::ValueType)Types[Num];
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000173 }
Nate Begeman336dba62005-12-30 00:12:56 +0000174 unsigned char getExtTypeNum(unsigned Num) const {
175 assert(Types.size() > Num && "Extended type num out of range!");
176 return Types[Num];
177 }
178 const std::vector<unsigned char> &getExtTypes() const { return Types; }
179 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
180 void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000181
182 Init *getLeafValue() const { assert(isLeaf()); return Val; }
183 Record *getOperator() const { assert(!isLeaf()); return Operator; }
184
185 unsigned getNumChildren() const { return Children.size(); }
186 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
187 void setChild(unsigned i, TreePatternNode *N) {
188 Children[i] = N;
189 }
190
Nate Begeman336dba62005-12-30 00:12:56 +0000191
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000192 const std::string &getPredicateFn() const { return PredicateFn; }
193 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner2617de42005-09-13 21:51:00 +0000194
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000195 Record *getTransformFn() const { return TransformFn; }
196 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000197
198 void print(std::ostream &OS) const;
199 void dump() const;
200
201 public: // Higher level manipulation routines.
202
203 /// clone - Return a new copy of this tree.
204 ///
205 TreePatternNode *clone() const;
206
Chris Lattnere86824e2005-09-29 19:28:10 +0000207 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
208 /// the specified node. For this comparison, all of the state of the node
209 /// is considered, except for the assigned name. Nodes with differing names
210 /// that are otherwise identical are considered isomorphic.
211 bool isIsomorphicTo(const TreePatternNode *N) const;
212
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000213 /// SubstituteFormalArguments - Replace the formal arguments in this tree
214 /// with actual values specified by ArgMap.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000215 void SubstituteFormalArguments(std::map<std::string,
216 TreePatternNode*> &ArgMap);
217
218 /// InlinePatternFragments - If this pattern refers to any pattern
219 /// fragments, inline them into place, giving us a pattern without any
220 /// PatFrag references.
221 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000222
223 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
224 /// this node and its children in the tree. This returns true if it makes a
225 /// change, false otherwise. If a type contradiction is found, throw an
226 /// exception.
Chris Lattner7b0275b2005-10-14 04:11:13 +0000227 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000228
229 /// UpdateNodeType - Set the node type of N to VT if VT contains
230 /// information. If N already contains a conflicting type, then throw an
231 /// exception. This returns true if any information was updated.
232 ///
Nate Begeman336dba62005-12-30 00:12:56 +0000233 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
234 TreePattern &TP);
235 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
236 std::vector<unsigned char> ExtVTs(1, ExtVT);
237 return UpdateNodeType(ExtVTs, TP);
238 }
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000239
240 /// ContainsUnresolvedType - Return true if this tree contains any
241 /// unresolved types.
242 bool ContainsUnresolvedType() const {
Chris Lattner2ae2f992005-10-19 04:12:14 +0000243 if (!hasTypeSet()) return true;
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000244 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
245 if (getChild(i)->ContainsUnresolvedType()) return true;
246 return false;
247 }
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000248
Chris Lattner492e70f2005-09-28 20:58:06 +0000249 /// canPatternMatch - If it is impossible for this pattern to match on this
250 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000251 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000252 };
253
254
Chris Lattnerf365e25a52005-09-13 21:20:49 +0000255 /// TreePattern - Represent a pattern, used for instructions, pattern
256 /// fragments, etc.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000257 ///
258 class TreePattern {
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000259 /// Trees - The list of pattern trees which corresponds to this pattern.
260 /// Note that PatFrag's only have a single tree.
261 ///
262 std::vector<TreePatternNode*> Trees;
263
264 /// TheRecord - The actual TableGen record corresponding to this pattern.
265 ///
266 Record *TheRecord;
267
268 /// Args - This is a list of all of the arguments to this pattern (for
269 /// PatFrag patterns), which are the 'node' markers in this pattern.
270 std::vector<std::string> Args;
271
272 /// ISE - the DAG isel emitter coordinating this madness.
273 ///
274 DAGISelEmitter &ISE;
Chris Lattner8bda5af2005-10-21 01:19:59 +0000275
276 /// isInputPattern - True if this is an input pattern, something to match.
277 /// False if this is an output pattern, something to emit.
278 bool isInputPattern;
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000279 public:
280
281 /// TreePattern constructor - Parse the specified DagInits into the
282 /// current record.
Chris Lattner8bda5af2005-10-21 01:19:59 +0000283 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
284 DAGISelEmitter &ise);
285 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
286 DAGISelEmitter &ise);
287 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
288 DAGISelEmitter &ise);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000289
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000290 /// getTrees - Return the tree patterns which corresponds to this pattern.
291 ///
292 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner91d86722005-09-09 01:11:17 +0000293 unsigned getNumTrees() const { return Trees.size(); }
294 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattnerce2173d2005-09-09 01:15:01 +0000295 TreePatternNode *getOnlyTree() const {
296 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
297 return Trees[0];
298 }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000299
300 /// getRecord - Return the actual TableGen record corresponding to this
301 /// pattern.
302 ///
303 Record *getRecord() const { return TheRecord; }
304
305 unsigned getNumArgs() const { return Args.size(); }
306 const std::string &getArgName(unsigned i) const {
307 assert(i < Args.size() && "Argument reference out of range!");
308 return Args[i];
309 }
Chris Lattnerf365e25a52005-09-13 21:20:49 +0000310 std::vector<std::string> &getArgList() { return Args; }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000311
312 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
313
314 /// InlinePatternFragments - If this pattern refers to any pattern
315 /// fragments, inline them into place, giving us a pattern without any
316 /// PatFrag references.
317 void InlinePatternFragments() {
318 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
319 Trees[i] = Trees[i]->InlinePatternFragments(*this);
320 }
321
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000322 /// InferAllTypes - Infer/propagate as many types throughout the expression
323 /// patterns as possible. Return true if all types are infered, false
324 /// otherwise. Throw an exception if a type contradiction is found.
325 bool InferAllTypes();
326
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000327 /// error - Throw an exception, prefixing it with information about this
328 /// pattern.
329 void error(const std::string &Msg) const;
330
331 void print(std::ostream &OS) const;
332 void dump() const;
333
334 private:
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000335 TreePatternNode *ParseTreePattern(DagInit *DI);
336 };
Chris Lattner3361eab2005-09-14 04:03:16 +0000337
338
339 class DAGInstruction {
340 TreePattern *Pattern;
Nate Begemancdf2c672005-12-01 00:06:14 +0000341 std::vector<Record*> Results;
342 std::vector<Record*> Operands;
Evan Chenge22f9182005-12-17 01:19:28 +0000343 std::vector<Record*> ImpResults;
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000344 std::vector<Record*> ImpOperands;
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000345 TreePatternNode *ResultPattern;
Chris Lattner3361eab2005-09-14 04:03:16 +0000346 public:
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000347 DAGInstruction(TreePattern *TP,
Nate Begemancdf2c672005-12-01 00:06:14 +0000348 const std::vector<Record*> &results,
Evan Chenge22f9182005-12-17 01:19:28 +0000349 const std::vector<Record*> &operands,
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000350 const std::vector<Record*> &impresults,
351 const std::vector<Record*> &impoperands)
Nate Begemancdf2c672005-12-01 00:06:14 +0000352 : Pattern(TP), Results(results), Operands(operands),
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000353 ImpResults(impresults), ImpOperands(impoperands),
354 ResultPattern(0) {}
Chris Lattner3361eab2005-09-14 04:03:16 +0000355
Chris Lattner3361eab2005-09-14 04:03:16 +0000356 TreePattern *getPattern() const { return Pattern; }
Nate Begemancdf2c672005-12-01 00:06:14 +0000357 unsigned getNumResults() const { return Results.size(); }
358 unsigned getNumOperands() const { return Operands.size(); }
Evan Chenge22f9182005-12-17 01:19:28 +0000359 unsigned getNumImpResults() const { return ImpResults.size(); }
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000360 unsigned getNumImpOperands() const { return ImpOperands.size(); }
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000361
Chris Lattner59e96142005-09-15 22:23:50 +0000362 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
363
Nate Begemancdf2c672005-12-01 00:06:14 +0000364 Record *getResult(unsigned RN) const {
365 assert(RN < Results.size());
366 return Results[RN];
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000367 }
368
Nate Begemancdf2c672005-12-01 00:06:14 +0000369 Record *getOperand(unsigned ON) const {
370 assert(ON < Operands.size());
371 return Operands[ON];
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000372 }
Evan Chenge22f9182005-12-17 01:19:28 +0000373
374 Record *getImpResult(unsigned RN) const {
375 assert(RN < ImpResults.size());
376 return ImpResults[RN];
377 }
378
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000379 Record *getImpOperand(unsigned ON) const {
380 assert(ON < ImpOperands.size());
381 return ImpOperands[ON];
382 }
383
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000384 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner3361eab2005-09-14 04:03:16 +0000385 };
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000386
Evan Chengd296a432005-12-14 22:02:59 +0000387/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
388/// to produce isel.
389struct PatternToMatch {
390 PatternToMatch(ListInit *preds, TreePatternNode *src, TreePatternNode *dst):
391 Predicates(preds), SrcPattern(src), DstPattern(dst) {};
392
393 ListInit *Predicates; // Top level predicate conditions to match.
394 TreePatternNode *SrcPattern; // Source pattern to match.
395 TreePatternNode *DstPattern; // Resulting pattern.
396
397 ListInit *getPredicates() const { return Predicates; }
398 TreePatternNode *getSrcPattern() const { return SrcPattern; }
399 TreePatternNode *getDstPattern() const { return DstPattern; }
400};
401
Chris Lattnerdce31c8d2006-01-17 21:31:18 +0000402/// DAGISelEmitter - The top-level class which coordinates construction
Chris Lattner90d00042005-09-03 01:14:03 +0000403/// and emission of the instruction selector.
404///
405class DAGISelEmitter : public TableGenBackend {
Chris Lattner323a4792005-09-23 20:52:47 +0000406public:
Evan Chengd296a432005-12-14 22:02:59 +0000407 //typedef std::pair<TreePatternNode*, TreePatternNode*> PatternToMatch;
Chris Lattner323a4792005-09-23 20:52:47 +0000408private:
Chris Lattner90d00042005-09-03 01:14:03 +0000409 RecordKeeper &Records;
410 CodeGenTarget Target;
411
Chris Lattner35bcd142005-09-08 21:03:01 +0000412 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner2617de42005-09-13 21:51:00 +0000413 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000414 std::map<Record*, ComplexPattern> ComplexPatterns;
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000415 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerfedd9a52005-09-15 21:57:35 +0000416 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner4c7b6042005-09-14 00:09:24 +0000417
418 /// PatternsToMatch - All of the things we are matching on the DAG. The first
419 /// value is the pattern to match, the second pattern is the result to
420 /// emit.
Chris Lattnerabb430b2005-09-23 19:36:15 +0000421 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner90d00042005-09-03 01:14:03 +0000422public:
423 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
424
425 // run - Output the isel, returning true on failure.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000426 void run(std::ostream &OS);
Chris Lattner35bcd142005-09-08 21:03:01 +0000427
Chris Lattner7b0275b2005-10-14 04:11:13 +0000428 const CodeGenTarget &getTargetInfo() const { return Target; }
429
Chris Lattner590176b2005-11-02 06:49:14 +0000430 Record *getSDNodeNamed(const std::string &Name) const;
431
Chris Lattner35bcd142005-09-08 21:03:01 +0000432 const SDNodeInfo &getSDNodeInfo(Record *R) const {
433 assert(SDNodes.count(R) && "Unknown node!");
434 return SDNodes.find(R)->second;
435 }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000436
Chris Lattner3556d842005-09-13 21:59:15 +0000437 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
438 assert(SDNodeXForms.count(R) && "Invalid transform!");
439 return SDNodeXForms.find(R)->second;
440 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000441
442 const ComplexPattern &getComplexPattern(Record *R) const {
443 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
444 return ComplexPatterns.find(R)->second;
445 }
446
447 TreePattern *getPatternFragment(Record *R) const {
448 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
449 return PatternFragments.find(R)->second;
450 }
Chris Lattner3556d842005-09-13 21:59:15 +0000451
Chris Lattnerfedd9a52005-09-15 21:57:35 +0000452 const DAGInstruction &getInstruction(Record *R) const {
453 assert(Instructions.count(R) && "Unknown instruction!");
454 return Instructions.find(R)->second;
455 }
456
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000457private:
Chris Lattner35bcd142005-09-08 21:03:01 +0000458 void ParseNodeInfo();
Chris Lattner2617de42005-09-13 21:51:00 +0000459 void ParseNodeTransforms(std::ostream &OS);
Evan Cheng9b9567b2005-12-08 02:00:36 +0000460 void ParseComplexPatterns();
Chris Lattnerf79ad4c2005-09-15 02:38:02 +0000461 void ParsePatternFragments(std::ostream &OS);
462 void ParseInstructions();
463 void ParsePatterns();
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000464 void GenerateVariants();
Chris Lattner3ba60bf2005-09-14 20:53:42 +0000465 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
466 std::map<std::string,
467 TreePatternNode*> &InstInputs,
Evan Chenge22f9182005-12-17 01:19:28 +0000468 std::map<std::string, Record*> &InstResults,
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000469 std::vector<Record*> &InstImpInputs,
Evan Chenge22f9182005-12-17 01:19:28 +0000470 std::vector<Record*> &InstImpResults);
Jeff Cohene251e922006-01-27 22:22:28 +0000471 bool EmitCodeForPattern(PatternToMatch &Pattern, std::ostream &OS);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000472 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner90d00042005-09-03 01:14:03 +0000473};
474
475} // End llvm namespace
476
477#endif