blob: 6bc0668117b1e635d931f04bf57e54de4c921dba [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"
Evan Cheng1630c282006-02-07 00:37:41 +000019#include <set>
Chris Lattner90d00042005-09-03 01:14:03 +000020
21namespace llvm {
Chris Lattnerd2a5b362005-09-07 23:44:43 +000022 class Record;
Jeff Cohen0dce12d2005-09-10 02:00:02 +000023 struct Init;
Chris Lattner59e96142005-09-15 22:23:50 +000024 class ListInit;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000025 class DagInit;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000026 class SDNodeInfo;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000027 class TreePattern;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000028 class TreePatternNode;
Chris Lattnerd2a5b362005-09-07 23:44:43 +000029 class DAGISelEmitter;
Evan Cheng9b9567b2005-12-08 02:00:36 +000030 class ComplexPattern;
Chris Lattner35bcd142005-09-08 21:03:01 +000031
Chris Lattnerecaf56b2005-10-14 06:12:03 +000032 /// MVT::DAGISelGenValueType - These are some extended forms of MVT::ValueType
33 /// that we use as lattice values during type inferrence.
34 namespace MVT {
35 enum DAGISelGenValueType {
36 isFP = MVT::LAST_VALUETYPE,
37 isInt,
38 isUnknown
39 };
40 }
41
Chris Lattner1c331042005-09-08 21:27:15 +000042 /// SDTypeConstraint - This is a discriminated union of constraints,
43 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
44 struct SDTypeConstraint {
45 SDTypeConstraint(Record *R);
46
47 unsigned OperandNo; // The operand # this constraint applies to.
48 enum {
Chris Lattner433573f2005-12-09 22:57:42 +000049 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs,
50 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp
Chris Lattner1c331042005-09-08 21:27:15 +000051 } ConstraintType;
52
53 union { // The discriminated union.
54 struct {
55 MVT::ValueType VT;
56 } SDTCisVT_Info;
57 struct {
58 unsigned OtherOperandNum;
59 } SDTCisSameAs_Info;
60 struct {
61 unsigned OtherOperandNum;
62 } SDTCisVTSmallerThanOp_Info;
Chris Lattner4892df32005-10-14 04:53:53 +000063 struct {
64 unsigned BigOperandNum;
65 } SDTCisOpSmallerThanOp_Info;
Chris Lattner1c331042005-09-08 21:27:15 +000066 } x;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000067
68 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
69 /// constraint to the nodes operands. This returns true if it makes a
70 /// change, false otherwise. If a type contradiction is found, throw an
71 /// exception.
72 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
73 TreePattern &TP) const;
74
75 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
76 /// N, which has NumResults results.
77 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
78 unsigned NumResults) const;
Chris Lattner1c331042005-09-08 21:27:15 +000079 };
80
Chris Lattner35bcd142005-09-08 21:03:01 +000081 /// SDNodeInfo - One of these records is created for each SDNode instance in
82 /// the target .td file. This represents the various dag nodes we will be
83 /// processing.
84 class SDNodeInfo {
85 Record *Def;
86 std::string EnumName;
87 std::string SDClassName;
Chris Lattnerf74c30c2005-09-28 18:28:29 +000088 unsigned Properties;
Chris Lattnerd7d31f32005-09-08 23:22:48 +000089 unsigned NumResults;
90 int NumOperands;
Chris Lattner1c331042005-09-08 21:27:15 +000091 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattner35bcd142005-09-08 21:03:01 +000092 public:
93 SDNodeInfo(Record *R); // Parse the specified record.
94
Chris Lattnerd7d31f32005-09-08 23:22:48 +000095 unsigned getNumResults() const { return NumResults; }
Chris Lattner1c331042005-09-08 21:27:15 +000096 int getNumOperands() const { return NumOperands; }
Chris Lattner35bcd142005-09-08 21:03:01 +000097 Record *getRecord() const { return Def; }
98 const std::string &getEnumName() const { return EnumName; }
99 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner1c331042005-09-08 21:27:15 +0000100
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000101 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner1c331042005-09-08 21:27:15 +0000102 return TypeConstraints;
103 }
Chris Lattnerf74c30c2005-09-28 18:28:29 +0000104
105 // SelectionDAG node properties.
Evan Cheng4b0623e2006-01-09 18:27:06 +0000106 enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain,
107 SDNPOutFlag, SDNPInFlag, SDNPOptInFlag };
Chris Lattnerf74c30c2005-09-28 18:28:29 +0000108
109 /// hasProperty - Return true if this node has the specified property.
110 ///
111 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattnerd7d31f32005-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 Lattner35bcd142005-09-08 21:03:01 +0000123 };
Chris Lattner90d00042005-09-03 01:14:03 +0000124
Chris Lattnerd2a5b362005-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 Begeman336dba62005-12-30 00:12:56 +0000129 /// The inferred type for this node, or MVT::isUnknown if it hasn't
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000130 /// been determined yet.
Nate Begeman336dba62005-12-30 00:12:56 +0000131 std::vector<unsigned char> Types;
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000132
Chris Lattnerd2a5b362005-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 Lattner2617de42005-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 Lattnerbc7aabc2005-09-14 22:55:26 +0000151 Record *TransformFn;
Chris Lattner2617de42005-09-13 21:51:00 +0000152
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000153 std::vector<TreePatternNode*> Children;
154 public:
155 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Nate Begeman336dba62005-12-30 00:12:56 +0000156 : Types(), Operator(Op), Val(0), TransformFn(0),
157 Children(Ch) { Types.push_back(MVT::isUnknown); }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000158 TreePatternNode(Init *val) // leaf ctor
Chris Lattnerafe9bae2006-01-29 02:43:35 +0000159 : Types(), Operator(0), Val(val), TransformFn(0) {
160 Types.push_back(MVT::isUnknown);
161 }
Chris Lattnerd2a5b362005-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; }
Nate Begeman336dba62005-12-30 00:12:56 +0000168 bool hasTypeSet() const { return Types[0] < MVT::LAST_VALUETYPE; }
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000169 bool isTypeCompletelyUnknown() const {
Nate Begeman336dba62005-12-30 00:12:56 +0000170 return Types[0] == MVT::isUnknown;
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000171 }
Nate Begeman336dba62005-12-30 00:12:56 +0000172 MVT::ValueType getTypeNum(unsigned Num) const {
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000173 assert(hasTypeSet() && "Doesn't have a type yet!");
Nate Begeman336dba62005-12-30 00:12:56 +0000174 assert(Types.size() > Num && "Type num out of range!");
175 return (MVT::ValueType)Types[Num];
Chris Lattnerecaf56b2005-10-14 06:12:03 +0000176 }
Nate Begeman336dba62005-12-30 00:12:56 +0000177 unsigned char getExtTypeNum(unsigned Num) const {
178 assert(Types.size() > Num && "Extended type num out of range!");
179 return Types[Num];
180 }
181 const std::vector<unsigned char> &getExtTypes() const { return Types; }
182 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
183 void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000184
185 Init *getLeafValue() const { assert(isLeaf()); return Val; }
186 Record *getOperator() const { assert(!isLeaf()); return Operator; }
187
188 unsigned getNumChildren() const { return Children.size(); }
189 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
190 void setChild(unsigned i, TreePatternNode *N) {
191 Children[i] = N;
192 }
193
Nate Begeman336dba62005-12-30 00:12:56 +0000194
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000195 const std::string &getPredicateFn() const { return PredicateFn; }
196 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner2617de42005-09-13 21:51:00 +0000197
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000198 Record *getTransformFn() const { return TransformFn; }
199 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000200
201 void print(std::ostream &OS) const;
202 void dump() const;
203
204 public: // Higher level manipulation routines.
205
206 /// clone - Return a new copy of this tree.
207 ///
208 TreePatternNode *clone() const;
209
Chris Lattnere86824e2005-09-29 19:28:10 +0000210 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
211 /// the specified node. For this comparison, all of the state of the node
212 /// is considered, except for the assigned name. Nodes with differing names
213 /// that are otherwise identical are considered isomorphic.
214 bool isIsomorphicTo(const TreePatternNode *N) const;
215
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000216 /// SubstituteFormalArguments - Replace the formal arguments in this tree
217 /// with actual values specified by ArgMap.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000218 void SubstituteFormalArguments(std::map<std::string,
219 TreePatternNode*> &ArgMap);
220
221 /// InlinePatternFragments - If this pattern refers to any pattern
222 /// fragments, inline them into place, giving us a pattern without any
223 /// PatFrag references.
224 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000225
226 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
227 /// this node and its children in the tree. This returns true if it makes a
228 /// change, false otherwise. If a type contradiction is found, throw an
229 /// exception.
Chris Lattner7b0275b2005-10-14 04:11:13 +0000230 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000231
232 /// UpdateNodeType - Set the node type of N to VT if VT contains
233 /// information. If N already contains a conflicting type, then throw an
234 /// exception. This returns true if any information was updated.
235 ///
Nate Begeman336dba62005-12-30 00:12:56 +0000236 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
237 TreePattern &TP);
238 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
239 std::vector<unsigned char> ExtVTs(1, ExtVT);
240 return UpdateNodeType(ExtVTs, TP);
241 }
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000242
243 /// ContainsUnresolvedType - Return true if this tree contains any
244 /// unresolved types.
245 bool ContainsUnresolvedType() const {
Chris Lattner2ae2f992005-10-19 04:12:14 +0000246 if (!hasTypeSet()) return true;
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000247 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
248 if (getChild(i)->ContainsUnresolvedType()) return true;
249 return false;
250 }
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000251
Chris Lattner492e70f2005-09-28 20:58:06 +0000252 /// canPatternMatch - If it is impossible for this pattern to match on this
253 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000254 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000255 };
256
257
Chris Lattnerf365e25a52005-09-13 21:20:49 +0000258 /// TreePattern - Represent a pattern, used for instructions, pattern
259 /// fragments, etc.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000260 ///
261 class TreePattern {
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000262 /// Trees - The list of pattern trees which corresponds to this pattern.
263 /// Note that PatFrag's only have a single tree.
264 ///
265 std::vector<TreePatternNode*> Trees;
266
267 /// TheRecord - The actual TableGen record corresponding to this pattern.
268 ///
269 Record *TheRecord;
270
271 /// Args - This is a list of all of the arguments to this pattern (for
272 /// PatFrag patterns), which are the 'node' markers in this pattern.
273 std::vector<std::string> Args;
274
275 /// ISE - the DAG isel emitter coordinating this madness.
276 ///
277 DAGISelEmitter &ISE;
Chris Lattner8bda5af2005-10-21 01:19:59 +0000278
279 /// isInputPattern - True if this is an input pattern, something to match.
280 /// False if this is an output pattern, something to emit.
281 bool isInputPattern;
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000282 public:
283
284 /// TreePattern constructor - Parse the specified DagInits into the
285 /// current record.
Chris Lattner8bda5af2005-10-21 01:19:59 +0000286 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
287 DAGISelEmitter &ise);
288 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
289 DAGISelEmitter &ise);
290 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
291 DAGISelEmitter &ise);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000292
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000293 /// getTrees - Return the tree patterns which corresponds to this pattern.
294 ///
295 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner91d86722005-09-09 01:11:17 +0000296 unsigned getNumTrees() const { return Trees.size(); }
297 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattnerce2173d2005-09-09 01:15:01 +0000298 TreePatternNode *getOnlyTree() const {
299 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
300 return Trees[0];
301 }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000302
303 /// getRecord - Return the actual TableGen record corresponding to this
304 /// pattern.
305 ///
306 Record *getRecord() const { return TheRecord; }
307
308 unsigned getNumArgs() const { return Args.size(); }
309 const std::string &getArgName(unsigned i) const {
310 assert(i < Args.size() && "Argument reference out of range!");
311 return Args[i];
312 }
Chris Lattnerf365e25a52005-09-13 21:20:49 +0000313 std::vector<std::string> &getArgList() { return Args; }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000314
315 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
316
317 /// InlinePatternFragments - If this pattern refers to any pattern
318 /// fragments, inline them into place, giving us a pattern without any
319 /// PatFrag references.
320 void InlinePatternFragments() {
321 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
322 Trees[i] = Trees[i]->InlinePatternFragments(*this);
323 }
324
Chris Lattnerd7d31f32005-09-08 23:22:48 +0000325 /// InferAllTypes - Infer/propagate as many types throughout the expression
326 /// patterns as possible. Return true if all types are infered, false
327 /// otherwise. Throw an exception if a type contradiction is found.
328 bool InferAllTypes();
329
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000330 /// error - Throw an exception, prefixing it with information about this
331 /// pattern.
332 void error(const std::string &Msg) const;
333
334 void print(std::ostream &OS) const;
335 void dump() const;
336
337 private:
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000338 TreePatternNode *ParseTreePattern(DagInit *DI);
339 };
Chris Lattner3361eab2005-09-14 04:03:16 +0000340
341
342 class DAGInstruction {
343 TreePattern *Pattern;
Nate Begemancdf2c672005-12-01 00:06:14 +0000344 std::vector<Record*> Results;
345 std::vector<Record*> Operands;
Evan Chenge22f9182005-12-17 01:19:28 +0000346 std::vector<Record*> ImpResults;
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000347 std::vector<Record*> ImpOperands;
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000348 TreePatternNode *ResultPattern;
Chris Lattner3361eab2005-09-14 04:03:16 +0000349 public:
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000350 DAGInstruction(TreePattern *TP,
Nate Begemancdf2c672005-12-01 00:06:14 +0000351 const std::vector<Record*> &results,
Evan Chenge22f9182005-12-17 01:19:28 +0000352 const std::vector<Record*> &operands,
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000353 const std::vector<Record*> &impresults,
354 const std::vector<Record*> &impoperands)
Nate Begemancdf2c672005-12-01 00:06:14 +0000355 : Pattern(TP), Results(results), Operands(operands),
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000356 ImpResults(impresults), ImpOperands(impoperands),
357 ResultPattern(0) {}
Chris Lattner3361eab2005-09-14 04:03:16 +0000358
Chris Lattner3361eab2005-09-14 04:03:16 +0000359 TreePattern *getPattern() const { return Pattern; }
Nate Begemancdf2c672005-12-01 00:06:14 +0000360 unsigned getNumResults() const { return Results.size(); }
361 unsigned getNumOperands() const { return Operands.size(); }
Evan Chenge22f9182005-12-17 01:19:28 +0000362 unsigned getNumImpResults() const { return ImpResults.size(); }
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000363 unsigned getNumImpOperands() const { return ImpOperands.size(); }
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000364
Chris Lattner59e96142005-09-15 22:23:50 +0000365 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
366
Nate Begemancdf2c672005-12-01 00:06:14 +0000367 Record *getResult(unsigned RN) const {
368 assert(RN < Results.size());
369 return Results[RN];
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000370 }
371
Nate Begemancdf2c672005-12-01 00:06:14 +0000372 Record *getOperand(unsigned ON) const {
373 assert(ON < Operands.size());
374 return Operands[ON];
Chris Lattnerf38ce8f2005-09-15 21:51:12 +0000375 }
Evan Chenge22f9182005-12-17 01:19:28 +0000376
377 Record *getImpResult(unsigned RN) const {
378 assert(RN < ImpResults.size());
379 return ImpResults[RN];
380 }
381
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000382 Record *getImpOperand(unsigned ON) const {
383 assert(ON < ImpOperands.size());
384 return ImpOperands[ON];
385 }
386
Chris Lattnerbc7aabc2005-09-14 22:55:26 +0000387 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner3361eab2005-09-14 04:03:16 +0000388 };
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000389
Evan Chengd296a432005-12-14 22:02:59 +0000390/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
391/// to produce isel.
392struct PatternToMatch {
393 PatternToMatch(ListInit *preds, TreePatternNode *src, TreePatternNode *dst):
394 Predicates(preds), SrcPattern(src), DstPattern(dst) {};
395
396 ListInit *Predicates; // Top level predicate conditions to match.
397 TreePatternNode *SrcPattern; // Source pattern to match.
398 TreePatternNode *DstPattern; // Resulting pattern.
399
400 ListInit *getPredicates() const { return Predicates; }
401 TreePatternNode *getSrcPattern() const { return SrcPattern; }
402 TreePatternNode *getDstPattern() const { return DstPattern; }
403};
404
Chris Lattnerdce31c8d2006-01-17 21:31:18 +0000405/// DAGISelEmitter - The top-level class which coordinates construction
Chris Lattner90d00042005-09-03 01:14:03 +0000406/// and emission of the instruction selector.
407///
408class DAGISelEmitter : public TableGenBackend {
Chris Lattner323a4792005-09-23 20:52:47 +0000409private:
Chris Lattner90d00042005-09-03 01:14:03 +0000410 RecordKeeper &Records;
411 CodeGenTarget Target;
Evan Cheng1630c282006-02-07 00:37:41 +0000412
Chris Lattner35bcd142005-09-08 21:03:01 +0000413 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner2617de42005-09-13 21:51:00 +0000414 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Evan Cheng9b9567b2005-12-08 02:00:36 +0000415 std::map<Record*, ComplexPattern> ComplexPatterns;
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000416 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerfedd9a52005-09-15 21:57:35 +0000417 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner4c7b6042005-09-14 00:09:24 +0000418
419 /// PatternsToMatch - All of the things we are matching on the DAG. The first
420 /// value is the pattern to match, the second pattern is the result to
421 /// emit.
Chris Lattnerabb430b2005-09-23 19:36:15 +0000422 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner90d00042005-09-03 01:14:03 +0000423public:
424 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
425
426 // run - Output the isel, returning true on failure.
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000427 void run(std::ostream &OS);
Chris Lattner35bcd142005-09-08 21:03:01 +0000428
Chris Lattner7b0275b2005-10-14 04:11:13 +0000429 const CodeGenTarget &getTargetInfo() const { return Target; }
430
Chris Lattner590176b2005-11-02 06:49:14 +0000431 Record *getSDNodeNamed(const std::string &Name) const;
432
Chris Lattner35bcd142005-09-08 21:03:01 +0000433 const SDNodeInfo &getSDNodeInfo(Record *R) const {
434 assert(SDNodes.count(R) && "Unknown node!");
435 return SDNodes.find(R)->second;
436 }
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000437
Chris Lattner3556d842005-09-13 21:59:15 +0000438 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
439 assert(SDNodeXForms.count(R) && "Invalid transform!");
440 return SDNodeXForms.find(R)->second;
441 }
Evan Cheng9b9567b2005-12-08 02:00:36 +0000442
443 const ComplexPattern &getComplexPattern(Record *R) const {
444 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
445 return ComplexPatterns.find(R)->second;
446 }
447
448 TreePattern *getPatternFragment(Record *R) const {
449 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
450 return PatternFragments.find(R)->second;
451 }
Chris Lattner3556d842005-09-13 21:59:15 +0000452
Chris Lattnerfedd9a52005-09-15 21:57:35 +0000453 const DAGInstruction &getInstruction(Record *R) const {
454 assert(Instructions.count(R) && "Unknown instruction!");
455 return Instructions.find(R)->second;
456 }
457
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000458private:
Chris Lattner35bcd142005-09-08 21:03:01 +0000459 void ParseNodeInfo();
Chris Lattner2617de42005-09-13 21:51:00 +0000460 void ParseNodeTransforms(std::ostream &OS);
Evan Cheng9b9567b2005-12-08 02:00:36 +0000461 void ParseComplexPatterns();
Chris Lattnerf79ad4c2005-09-15 02:38:02 +0000462 void ParsePatternFragments(std::ostream &OS);
463 void ParseInstructions();
464 void ParsePatterns();
Chris Lattner8bb25cd2005-09-28 19:27:25 +0000465 void GenerateVariants();
Chris Lattner3ba60bf2005-09-14 20:53:42 +0000466 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
467 std::map<std::string,
468 TreePatternNode*> &InstInputs,
Evan Chenge22f9182005-12-17 01:19:28 +0000469 std::map<std::string, Record*> &InstResults,
Evan Cheng72aaf8e2005-12-23 22:11:47 +0000470 std::vector<Record*> &InstImpInputs,
Evan Chenge22f9182005-12-17 01:19:28 +0000471 std::vector<Record*> &InstImpResults);
Chris Lattner7c0149d2006-01-29 04:25:26 +0000472 void GenerateCodeForPattern(PatternToMatch &Pattern,
Evan Cheng368f20e2006-02-05 06:43:12 +0000473 std::vector<std::pair<bool, std::string> > &GeneratedCode,
Evan Cheng1630c282006-02-07 00:37:41 +0000474 std::set<std::string> &GeneratedDecl,
Evan Cheng368f20e2006-02-05 06:43:12 +0000475 bool UseGoto);
Chris Lattner7c0149d2006-01-29 04:25:26 +0000476 void EmitPatterns(std::vector<std::pair<PatternToMatch*,
477 std::vector<std::pair<bool, std::string> > > > &Patterns,
478 unsigned Indent, std::ostream &OS);
Chris Lattnerd2a5b362005-09-07 23:44:43 +0000479 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner90d00042005-09-03 01:14:03 +0000480};
481
482} // End llvm namespace
483
484#endif