blob: 35606f7787c2319387600a7c75d20fb19ca87983 [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"
Evan Cheng21ad3922006-02-07 00:37:41 +000019#include <set>
Chris Lattner4a24c642005-09-03 01:14:03 +000020
21namespace llvm {
Chris Lattner54cb8fd2005-09-07 23:44:43 +000022 class Record;
Jeff Cohen8337b072005-09-10 02:00:02 +000023 struct Init;
Chris Lattnera28aec12005-09-15 22:23:50 +000024 class ListInit;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000025 class DagInit;
Chris Lattner32707602005-09-08 23:22:48 +000026 class SDNodeInfo;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000027 class TreePattern;
Chris Lattner32707602005-09-08 23:22:48 +000028 class TreePatternNode;
Chris Lattner54cb8fd2005-09-07 23:44:43 +000029 class DAGISelEmitter;
Evan Cheng0fc71982005-12-08 02:00:36 +000030 class ComplexPattern;
Chris Lattnerca559d02005-09-08 21:03:01 +000031
Chris Lattner3c7e18d2005-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 Lattner33c92e92005-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 Lattner5b21be72005-12-09 22:57:42 +000049 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs,
Chris Lattner697f8842006-03-20 05:39:48 +000050 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisIntVectorOfSameSize
Chris Lattner33c92e92005-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 Lattner03ebd802005-10-14 04:53:53 +000063 struct {
64 unsigned BigOperandNum;
65 } SDTCisOpSmallerThanOp_Info;
Chris Lattner697f8842006-03-20 05:39:48 +000066 struct {
67 unsigned OtherOperandNum;
68 } SDTCisIntVectorOfSameSize_Info;
Chris Lattner33c92e92005-09-08 21:27:15 +000069 } x;
Chris Lattner32707602005-09-08 23:22:48 +000070
71 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
72 /// constraint to the nodes operands. This returns true if it makes a
73 /// change, false otherwise. If a type contradiction is found, throw an
74 /// exception.
75 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
76 TreePattern &TP) const;
77
78 /// getOperandNum - Return the node corresponding to operand #OpNo in tree
79 /// N, which has NumResults results.
80 TreePatternNode *getOperandNum(unsigned OpNo, TreePatternNode *N,
81 unsigned NumResults) const;
Chris Lattner33c92e92005-09-08 21:27:15 +000082 };
83
Chris Lattnerca559d02005-09-08 21:03:01 +000084 /// SDNodeInfo - One of these records is created for each SDNode instance in
85 /// the target .td file. This represents the various dag nodes we will be
86 /// processing.
87 class SDNodeInfo {
88 Record *Def;
89 std::string EnumName;
90 std::string SDClassName;
Chris Lattnera1a68ae2005-09-28 18:28:29 +000091 unsigned Properties;
Chris Lattner32707602005-09-08 23:22:48 +000092 unsigned NumResults;
93 int NumOperands;
Chris Lattner33c92e92005-09-08 21:27:15 +000094 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000095 public:
96 SDNodeInfo(Record *R); // Parse the specified record.
97
Chris Lattner32707602005-09-08 23:22:48 +000098 unsigned getNumResults() const { return NumResults; }
Chris Lattner33c92e92005-09-08 21:27:15 +000099 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +0000100 Record *getRecord() const { return Def; }
101 const std::string &getEnumName() const { return EnumName; }
102 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +0000103
Chris Lattner32707602005-09-08 23:22:48 +0000104 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
Chris Lattner33c92e92005-09-08 21:27:15 +0000105 return TypeConstraints;
106 }
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000107
108 // SelectionDAG node properties.
Evan Cheng51fecc82006-01-09 18:27:06 +0000109 enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain,
110 SDNPOutFlag, SDNPInFlag, SDNPOptInFlag };
Chris Lattnera1a68ae2005-09-28 18:28:29 +0000111
112 /// hasProperty - Return true if this node has the specified property.
113 ///
114 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
Chris Lattner32707602005-09-08 23:22:48 +0000115
116 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
117 /// constraints for this node to the operands of the node. This returns
118 /// true if it makes a change, false otherwise. If a type contradiction is
119 /// found, throw an exception.
120 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
121 bool MadeChange = false;
122 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
123 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
124 return MadeChange;
125 }
Chris Lattnerca559d02005-09-08 21:03:01 +0000126 };
Chris Lattner4a24c642005-09-03 01:14:03 +0000127
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000128 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
129 /// patterns), and as such should be ref counted. We currently just leak all
130 /// TreePatternNode objects!
131 class TreePatternNode {
Nate Begemanb73628b2005-12-30 00:12:56 +0000132 /// The inferred type for this node, or MVT::isUnknown if it hasn't
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000133 /// been determined yet.
Nate Begemanb73628b2005-12-30 00:12:56 +0000134 std::vector<unsigned char> Types;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000135
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000136 /// Operator - The Record for the operator if this is an interior node (not
137 /// a leaf).
138 Record *Operator;
139
140 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
141 ///
142 Init *Val;
143
144 /// Name - The name given to this node with the :$foo notation.
145 ///
146 std::string Name;
147
148 /// PredicateFn - The predicate function to execute on this node to check
149 /// for a match. If this string is empty, no predicate is involved.
150 std::string PredicateFn;
151
Chris Lattner24eeeb82005-09-13 21:51:00 +0000152 /// TransformFn - The transformation function to execute on this node before
153 /// it can be substituted into the resulting instruction on a pattern match.
Chris Lattnerb0276202005-09-14 22:55:26 +0000154 Record *TransformFn;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000155
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000156 std::vector<TreePatternNode*> Children;
157 public:
158 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
Nate Begemanb73628b2005-12-30 00:12:56 +0000159 : Types(), Operator(Op), Val(0), TransformFn(0),
160 Children(Ch) { Types.push_back(MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000161 TreePatternNode(Init *val) // leaf ctor
Chris Lattner355408b2006-01-29 02:43:35 +0000162 : Types(), Operator(0), Val(val), TransformFn(0) {
163 Types.push_back(MVT::isUnknown);
164 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000165 ~TreePatternNode();
166
167 const std::string &getName() const { return Name; }
168 void setName(const std::string &N) { Name = N; }
169
170 bool isLeaf() const { return Val != 0; }
Nate Begemanb73628b2005-12-30 00:12:56 +0000171 bool hasTypeSet() const { return Types[0] < MVT::LAST_VALUETYPE; }
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000172 bool isTypeCompletelyUnknown() const {
Nate Begemanb73628b2005-12-30 00:12:56 +0000173 return Types[0] == MVT::isUnknown;
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000174 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000175 MVT::ValueType getTypeNum(unsigned Num) const {
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000176 assert(hasTypeSet() && "Doesn't have a type yet!");
Nate Begemanb73628b2005-12-30 00:12:56 +0000177 assert(Types.size() > Num && "Type num out of range!");
178 return (MVT::ValueType)Types[Num];
Chris Lattner3c7e18d2005-10-14 06:12:03 +0000179 }
Nate Begemanb73628b2005-12-30 00:12:56 +0000180 unsigned char getExtTypeNum(unsigned Num) const {
181 assert(Types.size() > Num && "Extended type num out of range!");
182 return Types[Num];
183 }
184 const std::vector<unsigned char> &getExtTypes() const { return Types; }
185 void setTypes(const std::vector<unsigned char> &T) { Types = T; }
186 void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000187
188 Init *getLeafValue() const { assert(isLeaf()); return Val; }
189 Record *getOperator() const { assert(!isLeaf()); return Operator; }
190
191 unsigned getNumChildren() const { return Children.size(); }
192 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
193 void setChild(unsigned i, TreePatternNode *N) {
194 Children[i] = N;
195 }
196
Nate Begemanb73628b2005-12-30 00:12:56 +0000197
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000198 const std::string &getPredicateFn() const { return PredicateFn; }
199 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
Chris Lattner24eeeb82005-09-13 21:51:00 +0000200
Chris Lattnerb0276202005-09-14 22:55:26 +0000201 Record *getTransformFn() const { return TransformFn; }
202 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000203
204 void print(std::ostream &OS) const;
205 void dump() const;
206
207 public: // Higher level manipulation routines.
208
209 /// clone - Return a new copy of this tree.
210 ///
211 TreePatternNode *clone() const;
212
Chris Lattnere46e17b2005-09-29 19:28:10 +0000213 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
214 /// the specified node. For this comparison, all of the state of the node
215 /// is considered, except for the assigned name. Nodes with differing names
216 /// that are otherwise identical are considered isomorphic.
217 bool isIsomorphicTo(const TreePatternNode *N) const;
218
Chris Lattner32707602005-09-08 23:22:48 +0000219 /// SubstituteFormalArguments - Replace the formal arguments in this tree
220 /// with actual values specified by ArgMap.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000221 void SubstituteFormalArguments(std::map<std::string,
222 TreePatternNode*> &ArgMap);
223
224 /// InlinePatternFragments - If this pattern refers to any pattern
225 /// fragments, inline them into place, giving us a pattern without any
226 /// PatFrag references.
227 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Chris Lattner32707602005-09-08 23:22:48 +0000228
229 /// ApplyTypeConstraints - Apply all of the type constraints relevent to
230 /// this node and its children in the tree. This returns true if it makes a
231 /// change, false otherwise. If a type contradiction is found, throw an
232 /// exception.
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000233 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Chris Lattner32707602005-09-08 23:22:48 +0000234
235 /// UpdateNodeType - Set the node type of N to VT if VT contains
236 /// information. If N already contains a conflicting type, then throw an
237 /// exception. This returns true if any information was updated.
238 ///
Nate Begemanb73628b2005-12-30 00:12:56 +0000239 bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
240 TreePattern &TP);
241 bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
242 std::vector<unsigned char> ExtVTs(1, ExtVT);
243 return UpdateNodeType(ExtVTs, TP);
244 }
Chris Lattner32707602005-09-08 23:22:48 +0000245
246 /// ContainsUnresolvedType - Return true if this tree contains any
247 /// unresolved types.
248 bool ContainsUnresolvedType() const {
Chris Lattner2ac85102005-10-19 04:12:14 +0000249 if (!hasTypeSet()) return true;
Chris Lattner32707602005-09-08 23:22:48 +0000250 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
251 if (getChild(i)->ContainsUnresolvedType()) return true;
252 return false;
253 }
Chris Lattnere97603f2005-09-28 19:27:25 +0000254
Chris Lattner7cf2fe62005-09-28 20:58:06 +0000255 /// canPatternMatch - If it is impossible for this pattern to match on this
256 /// target, fill in Reason and return false. Otherwise, return true.
Chris Lattnere97603f2005-09-28 19:27:25 +0000257 bool canPatternMatch(std::string &Reason, DAGISelEmitter &ISE);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000258 };
259
260
Chris Lattneree9f0c32005-09-13 21:20:49 +0000261 /// TreePattern - Represent a pattern, used for instructions, pattern
262 /// fragments, etc.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000263 ///
264 class TreePattern {
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000265 /// Trees - The list of pattern trees which corresponds to this pattern.
266 /// Note that PatFrag's only have a single tree.
267 ///
268 std::vector<TreePatternNode*> Trees;
269
270 /// TheRecord - The actual TableGen record corresponding to this pattern.
271 ///
272 Record *TheRecord;
273
274 /// Args - This is a list of all of the arguments to this pattern (for
275 /// PatFrag patterns), which are the 'node' markers in this pattern.
276 std::vector<std::string> Args;
277
278 /// ISE - the DAG isel emitter coordinating this madness.
279 ///
280 DAGISelEmitter &ISE;
Chris Lattneredbd8712005-10-21 01:19:59 +0000281
282 /// isInputPattern - True if this is an input pattern, something to match.
283 /// False if this is an output pattern, something to emit.
284 bool isInputPattern;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000285 public:
286
287 /// TreePattern constructor - Parse the specified DagInits into the
288 /// current record.
Chris Lattneredbd8712005-10-21 01:19:59 +0000289 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
290 DAGISelEmitter &ise);
291 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
292 DAGISelEmitter &ise);
293 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
294 DAGISelEmitter &ise);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000295
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000296 /// getTrees - Return the tree patterns which corresponds to this pattern.
297 ///
298 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner20180052005-09-09 01:11:17 +0000299 unsigned getNumTrees() const { return Trees.size(); }
300 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Chris Lattner37937092005-09-09 01:15:01 +0000301 TreePatternNode *getOnlyTree() const {
302 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
303 return Trees[0];
304 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000305
306 /// getRecord - Return the actual TableGen record corresponding to this
307 /// pattern.
308 ///
309 Record *getRecord() const { return TheRecord; }
310
311 unsigned getNumArgs() const { return Args.size(); }
312 const std::string &getArgName(unsigned i) const {
313 assert(i < Args.size() && "Argument reference out of range!");
314 return Args[i];
315 }
Chris Lattneree9f0c32005-09-13 21:20:49 +0000316 std::vector<std::string> &getArgList() { return Args; }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000317
318 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
319
320 /// InlinePatternFragments - If this pattern refers to any pattern
321 /// fragments, inline them into place, giving us a pattern without any
322 /// PatFrag references.
323 void InlinePatternFragments() {
324 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
325 Trees[i] = Trees[i]->InlinePatternFragments(*this);
326 }
327
Chris Lattner32707602005-09-08 23:22:48 +0000328 /// InferAllTypes - Infer/propagate as many types throughout the expression
329 /// patterns as possible. Return true if all types are infered, false
330 /// otherwise. Throw an exception if a type contradiction is found.
331 bool InferAllTypes();
332
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000333 /// error - Throw an exception, prefixing it with information about this
334 /// pattern.
335 void error(const std::string &Msg) const;
336
337 void print(std::ostream &OS) const;
338 void dump() const;
339
340 private:
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000341 TreePatternNode *ParseTreePattern(DagInit *DI);
342 };
Chris Lattnerec676432005-09-14 04:03:16 +0000343
344
345 class DAGInstruction {
346 TreePattern *Pattern;
Nate Begemanddb39542005-12-01 00:06:14 +0000347 std::vector<Record*> Results;
348 std::vector<Record*> Operands;
Evan Chengbcecf332005-12-17 01:19:28 +0000349 std::vector<Record*> ImpResults;
Evan Cheng7b05bd52005-12-23 22:11:47 +0000350 std::vector<Record*> ImpOperands;
Chris Lattnerb0276202005-09-14 22:55:26 +0000351 TreePatternNode *ResultPattern;
Chris Lattnerec676432005-09-14 04:03:16 +0000352 public:
Chris Lattnerae6d8282005-09-15 21:51:12 +0000353 DAGInstruction(TreePattern *TP,
Nate Begemanddb39542005-12-01 00:06:14 +0000354 const std::vector<Record*> &results,
Evan Chengbcecf332005-12-17 01:19:28 +0000355 const std::vector<Record*> &operands,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000356 const std::vector<Record*> &impresults,
357 const std::vector<Record*> &impoperands)
Nate Begemanddb39542005-12-01 00:06:14 +0000358 : Pattern(TP), Results(results), Operands(operands),
Evan Cheng7b05bd52005-12-23 22:11:47 +0000359 ImpResults(impresults), ImpOperands(impoperands),
360 ResultPattern(0) {}
Chris Lattnerec676432005-09-14 04:03:16 +0000361
Chris Lattnerec676432005-09-14 04:03:16 +0000362 TreePattern *getPattern() const { return Pattern; }
Nate Begemanddb39542005-12-01 00:06:14 +0000363 unsigned getNumResults() const { return Results.size(); }
364 unsigned getNumOperands() const { return Operands.size(); }
Evan Chengbcecf332005-12-17 01:19:28 +0000365 unsigned getNumImpResults() const { return ImpResults.size(); }
Evan Cheng7b05bd52005-12-23 22:11:47 +0000366 unsigned getNumImpOperands() const { return ImpOperands.size(); }
Chris Lattnerae6d8282005-09-15 21:51:12 +0000367
Chris Lattnera28aec12005-09-15 22:23:50 +0000368 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
369
Nate Begemanddb39542005-12-01 00:06:14 +0000370 Record *getResult(unsigned RN) const {
371 assert(RN < Results.size());
372 return Results[RN];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000373 }
374
Nate Begemanddb39542005-12-01 00:06:14 +0000375 Record *getOperand(unsigned ON) const {
376 assert(ON < Operands.size());
377 return Operands[ON];
Chris Lattnerae6d8282005-09-15 21:51:12 +0000378 }
Evan Chengbcecf332005-12-17 01:19:28 +0000379
380 Record *getImpResult(unsigned RN) const {
381 assert(RN < ImpResults.size());
382 return ImpResults[RN];
383 }
384
Evan Cheng7b05bd52005-12-23 22:11:47 +0000385 Record *getImpOperand(unsigned ON) const {
386 assert(ON < ImpOperands.size());
387 return ImpOperands[ON];
388 }
389
Chris Lattnerb0276202005-09-14 22:55:26 +0000390 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattnerec676432005-09-14 04:03:16 +0000391 };
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000392
Evan Cheng58e84a62005-12-14 22:02:59 +0000393/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
394/// to produce isel.
395struct PatternToMatch {
396 PatternToMatch(ListInit *preds, TreePatternNode *src, TreePatternNode *dst):
397 Predicates(preds), SrcPattern(src), DstPattern(dst) {};
398
399 ListInit *Predicates; // Top level predicate conditions to match.
400 TreePatternNode *SrcPattern; // Source pattern to match.
401 TreePatternNode *DstPattern; // Resulting pattern.
402
403 ListInit *getPredicates() const { return Predicates; }
404 TreePatternNode *getSrcPattern() const { return SrcPattern; }
405 TreePatternNode *getDstPattern() const { return DstPattern; }
406};
407
Chris Lattner09fb7d42006-01-17 21:31:18 +0000408/// DAGISelEmitter - The top-level class which coordinates construction
Chris Lattner4a24c642005-09-03 01:14:03 +0000409/// and emission of the instruction selector.
410///
411class DAGISelEmitter : public TableGenBackend {
Chris Lattner3f7e9142005-09-23 20:52:47 +0000412private:
Chris Lattner4a24c642005-09-03 01:14:03 +0000413 RecordKeeper &Records;
414 CodeGenTarget Target;
Evan Cheng21ad3922006-02-07 00:37:41 +0000415
Chris Lattnerca559d02005-09-08 21:03:01 +0000416 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner24eeeb82005-09-13 21:51:00 +0000417 std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
Evan Cheng0fc71982005-12-08 02:00:36 +0000418 std::map<Record*, ComplexPattern> ComplexPatterns;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000419 std::map<Record*, TreePattern*> PatternFragments;
Chris Lattnerae5b3502005-09-15 21:57:35 +0000420 std::map<Record*, DAGInstruction> Instructions;
Chris Lattner1f39e292005-09-14 00:09:24 +0000421
422 /// PatternsToMatch - All of the things we are matching on the DAG. The first
423 /// value is the pattern to match, the second pattern is the result to
424 /// emit.
Chris Lattner81303322005-09-23 19:36:15 +0000425 std::vector<PatternToMatch> PatternsToMatch;
Chris Lattner4a24c642005-09-03 01:14:03 +0000426public:
427 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
428
429 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000430 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000431
Chris Lattner0ee7cff2005-10-14 04:11:13 +0000432 const CodeGenTarget &getTargetInfo() const { return Target; }
433
Chris Lattner0614b622005-11-02 06:49:14 +0000434 Record *getSDNodeNamed(const std::string &Name) const;
435
Chris Lattnerca559d02005-09-08 21:03:01 +0000436 const SDNodeInfo &getSDNodeInfo(Record *R) const {
437 assert(SDNodes.count(R) && "Unknown node!");
438 return SDNodes.find(R)->second;
439 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000440
Chris Lattner6de8b532005-09-13 21:59:15 +0000441 const std::pair<Record*, std::string> &getSDNodeTransform(Record *R) const {
442 assert(SDNodeXForms.count(R) && "Invalid transform!");
443 return SDNodeXForms.find(R)->second;
444 }
Evan Cheng0fc71982005-12-08 02:00:36 +0000445
446 const ComplexPattern &getComplexPattern(Record *R) const {
447 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
448 return ComplexPatterns.find(R)->second;
449 }
450
451 TreePattern *getPatternFragment(Record *R) const {
452 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
453 return PatternFragments.find(R)->second;
454 }
Chris Lattner6de8b532005-09-13 21:59:15 +0000455
Chris Lattnerae5b3502005-09-15 21:57:35 +0000456 const DAGInstruction &getInstruction(Record *R) const {
457 assert(Instructions.count(R) && "Unknown instruction!");
458 return Instructions.find(R)->second;
459 }
460
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000461private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000462 void ParseNodeInfo();
Chris Lattner24eeeb82005-09-13 21:51:00 +0000463 void ParseNodeTransforms(std::ostream &OS);
Evan Cheng0fc71982005-12-08 02:00:36 +0000464 void ParseComplexPatterns();
Chris Lattnerb39e4be2005-09-15 02:38:02 +0000465 void ParsePatternFragments(std::ostream &OS);
466 void ParseInstructions();
467 void ParsePatterns();
Chris Lattnere97603f2005-09-28 19:27:25 +0000468 void GenerateVariants();
Chris Lattnerd8a3bde2005-09-14 20:53:42 +0000469 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
470 std::map<std::string,
471 TreePatternNode*> &InstInputs,
Evan Cheng420132e2006-03-20 06:04:09 +0000472 std::map<std::string,
473 TreePatternNode*> &InstResults,
Evan Cheng7b05bd52005-12-23 22:11:47 +0000474 std::vector<Record*> &InstImpInputs,
Evan Chengbcecf332005-12-17 01:19:28 +0000475 std::vector<Record*> &InstImpResults);
Chris Lattner8bc74722006-01-29 04:25:26 +0000476 void GenerateCodeForPattern(PatternToMatch &Pattern,
Evan Chenge41bf822006-02-05 06:43:12 +0000477 std::vector<std::pair<bool, std::string> > &GeneratedCode,
Evan Chengd7805a72006-02-09 07:16:09 +0000478 std::set<std::pair<bool, std::string> > &GeneratedDecl,
Evan Chenge41bf822006-02-05 06:43:12 +0000479 bool UseGoto);
Chris Lattner8bc74722006-01-29 04:25:26 +0000480 void EmitPatterns(std::vector<std::pair<PatternToMatch*,
481 std::vector<std::pair<bool, std::string> > > > &Patterns,
482 unsigned Indent, std::ostream &OS);
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000483 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000484};
485
486} // End llvm namespace
487
488#endif