blob: e2fade9917130d8aaf9213f077a91140c6cd1f76 [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"
19
20namespace llvm {
Chris Lattner54cb8fd2005-09-07 23:44:43 +000021 class Record;
22 class Init;
23 class DagInit;
24 class TreePattern;
25 class DAGISelEmitter;
Chris Lattnerca559d02005-09-08 21:03:01 +000026
Chris Lattner33c92e92005-09-08 21:27:15 +000027 /// SDTypeConstraint - This is a discriminated union of constraints,
28 /// corresponding to the SDTypeConstraint tablegen class in Target.td.
29 struct SDTypeConstraint {
30 SDTypeConstraint(Record *R);
31
32 unsigned OperandNo; // The operand # this constraint applies to.
33 enum {
34 SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp
35 } ConstraintType;
36
37 union { // The discriminated union.
38 struct {
39 MVT::ValueType VT;
40 } SDTCisVT_Info;
41 struct {
42 unsigned OtherOperandNum;
43 } SDTCisSameAs_Info;
44 struct {
45 unsigned OtherOperandNum;
46 } SDTCisVTSmallerThanOp_Info;
47 } x;
48 };
49
Chris Lattnerca559d02005-09-08 21:03:01 +000050 /// SDNodeInfo - One of these records is created for each SDNode instance in
51 /// the target .td file. This represents the various dag nodes we will be
52 /// processing.
53 class SDNodeInfo {
54 Record *Def;
55 std::string EnumName;
56 std::string SDClassName;
Chris Lattner33c92e92005-09-08 21:27:15 +000057 int NumResults, NumOperands;
58 std::vector<SDTypeConstraint> TypeConstraints;
Chris Lattnerca559d02005-09-08 21:03:01 +000059 public:
60 SDNodeInfo(Record *R); // Parse the specified record.
61
Chris Lattner33c92e92005-09-08 21:27:15 +000062 int getNumResults() const { return NumResults; }
63 int getNumOperands() const { return NumOperands; }
Chris Lattnerca559d02005-09-08 21:03:01 +000064 Record *getRecord() const { return Def; }
65 const std::string &getEnumName() const { return EnumName; }
66 const std::string &getSDClassName() const { return SDClassName; }
Chris Lattner33c92e92005-09-08 21:27:15 +000067
68 const std::vector<SDTypeConstraint> &getTypeConstraints() {
69 return TypeConstraints;
70 }
Chris Lattnerca559d02005-09-08 21:03:01 +000071 };
Chris Lattner4a24c642005-09-03 01:14:03 +000072
Chris Lattner54cb8fd2005-09-07 23:44:43 +000073 /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
74 /// patterns), and as such should be ref counted. We currently just leak all
75 /// TreePatternNode objects!
76 class TreePatternNode {
77 /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
78 /// been determined yet.
79 MVT::ValueType Ty;
80
81 /// Operator - The Record for the operator if this is an interior node (not
82 /// a leaf).
83 Record *Operator;
84
85 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
86 ///
87 Init *Val;
88
89 /// Name - The name given to this node with the :$foo notation.
90 ///
91 std::string Name;
92
93 /// PredicateFn - The predicate function to execute on this node to check
94 /// for a match. If this string is empty, no predicate is involved.
95 std::string PredicateFn;
96
97 std::vector<TreePatternNode*> Children;
98 public:
99 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch)
100 : Ty(MVT::LAST_VALUETYPE), Operator(Op), Val(0), Children(Ch) {}
101 TreePatternNode(Init *val) // leaf ctor
102 : Ty(MVT::LAST_VALUETYPE), Operator(0), Val(val) {}
103 ~TreePatternNode();
104
105 const std::string &getName() const { return Name; }
106 void setName(const std::string &N) { Name = N; }
107
108 bool isLeaf() const { return Val != 0; }
109 MVT::ValueType getType() const { return Ty; }
110 void setType(MVT::ValueType VT) { Ty = VT; }
111
112 Init *getLeafValue() const { assert(isLeaf()); return Val; }
113 Record *getOperator() const { assert(!isLeaf()); return Operator; }
114
115 unsigned getNumChildren() const { return Children.size(); }
116 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
117 void setChild(unsigned i, TreePatternNode *N) {
118 Children[i] = N;
119 }
120
121 const std::string &getPredicateFn() const { return PredicateFn; }
122 void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
123
124 void print(std::ostream &OS) const;
125 void dump() const;
126
127 public: // Higher level manipulation routines.
128
129 /// clone - Return a new copy of this tree.
130 ///
131 TreePatternNode *clone() const;
132
133 void SubstituteFormalArguments(std::map<std::string,
134 TreePatternNode*> &ArgMap);
135
136 /// InlinePatternFragments - If this pattern refers to any pattern
137 /// fragments, inline them into place, giving us a pattern without any
138 /// PatFrag references.
139 TreePatternNode *InlinePatternFragments(TreePattern &TP);
140
141 };
142
143
144 /// TreePattern - Represent a pattern of one form or another. Currently, two
145 /// types of patterns are possible: Instructions and PatFrags.
146 ///
147 class TreePattern {
148 public:
149 enum PatternType {
150 PatFrag, Instruction
151 };
152 private:
153 /// PTy - The type of pattern this is.
154 ///
155 PatternType PTy;
156
157 /// Trees - The list of pattern trees which corresponds to this pattern.
158 /// Note that PatFrag's only have a single tree.
159 ///
160 std::vector<TreePatternNode*> Trees;
161
162 /// TheRecord - The actual TableGen record corresponding to this pattern.
163 ///
164 Record *TheRecord;
165
166 /// Args - This is a list of all of the arguments to this pattern (for
167 /// PatFrag patterns), which are the 'node' markers in this pattern.
168 std::vector<std::string> Args;
169
170 /// ISE - the DAG isel emitter coordinating this madness.
171 ///
172 DAGISelEmitter &ISE;
173 public:
174
175 /// TreePattern constructor - Parse the specified DagInits into the
176 /// current record.
177 TreePattern(PatternType pty, Record *TheRec,
178 const std::vector<DagInit *> &RawPat, DAGISelEmitter &ise);
179
180 /// getPatternType - Return what flavor of Record this pattern originated from
181 ///
182 PatternType getPatternType() const { return PTy; }
183
184 /// getTrees - Return the tree patterns which corresponds to this pattern.
185 ///
186 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
187
188 /// getRecord - Return the actual TableGen record corresponding to this
189 /// pattern.
190 ///
191 Record *getRecord() const { return TheRecord; }
192
193 unsigned getNumArgs() const { return Args.size(); }
194 const std::string &getArgName(unsigned i) const {
195 assert(i < Args.size() && "Argument reference out of range!");
196 return Args[i];
197 }
198
199 DAGISelEmitter &getDAGISelEmitter() const { return ISE; }
200
201 /// InlinePatternFragments - If this pattern refers to any pattern
202 /// fragments, inline them into place, giving us a pattern without any
203 /// PatFrag references.
204 void InlinePatternFragments() {
205 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
206 Trees[i] = Trees[i]->InlinePatternFragments(*this);
207 }
208
209 /// error - Throw an exception, prefixing it with information about this
210 /// pattern.
211 void error(const std::string &Msg) const;
212
213 void print(std::ostream &OS) const;
214 void dump() const;
215
216 private:
217 MVT::ValueType getIntrinsicType(Record *R) const;
218 TreePatternNode *ParseTreePattern(DagInit *DI);
219 };
220
221
222
Chris Lattner4a24c642005-09-03 01:14:03 +0000223/// InstrSelectorEmitter - The top-level class which coordinates construction
224/// and emission of the instruction selector.
225///
226class DAGISelEmitter : public TableGenBackend {
227 RecordKeeper &Records;
228 CodeGenTarget Target;
229
Chris Lattnerca559d02005-09-08 21:03:01 +0000230 std::map<Record*, SDNodeInfo> SDNodes;
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000231 std::map<Record*, TreePattern*> PatternFragments;
232 std::vector<TreePattern*> Instructions;
Chris Lattner4a24c642005-09-03 01:14:03 +0000233public:
234 DAGISelEmitter(RecordKeeper &R) : Records(R) {}
235
236 // run - Output the isel, returning true on failure.
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000237 void run(std::ostream &OS);
Chris Lattnerca559d02005-09-08 21:03:01 +0000238
239 const SDNodeInfo &getSDNodeInfo(Record *R) const {
240 assert(SDNodes.count(R) && "Unknown node!");
241 return SDNodes.find(R)->second;
242 }
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000243
244 TreePattern *getPatternFragment(Record *R) const {
245 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
246 return PatternFragments.find(R)->second;
247 }
248
249private:
Chris Lattnerca559d02005-09-08 21:03:01 +0000250 void ParseNodeInfo();
Chris Lattner54cb8fd2005-09-07 23:44:43 +0000251 void ParseAndResolvePatternFragments(std::ostream &OS);
252 void ParseAndResolveInstructions();
253 void EmitInstructionSelector(std::ostream &OS);
Chris Lattner4a24c642005-09-03 01:14:03 +0000254};
255
256} // End llvm namespace
257
258#endif