blob: 6e1eb085c0ca8162e90739d6b8d692551b12cadc [file] [log] [blame]
Chris Lattnerfe718932008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
Chris Lattner6cefb772008-01-05 22:25:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfe718932008-01-06 01:10:31 +000010// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner6cefb772008-01-05 22:25:12 +000011// represent the patterns present in a .td file for instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CODEGEN_DAGPATTERNS_H
16#define CODEGEN_DAGPATTERNS_H
17
Chris Lattner6cefb772008-01-05 22:25:12 +000018#include "CodeGenTarget.h"
19#include "CodeGenIntrinsics.h"
Chris Lattner2cacec52010-03-15 06:00:16 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
Chris Lattnere14d2e22010-03-19 01:07:44 +000022#include <set>
23#include <algorithm>
24#include <vector>
25#include <map>
Chris Lattner6cefb772008-01-05 22:25:12 +000026
27namespace llvm {
28 class Record;
Evan Cheng9bcc3992011-07-11 20:23:13 +000029 class Init;
Chris Lattner6cefb772008-01-05 22:25:12 +000030 class ListInit;
31 class DagInit;
32 class SDNodeInfo;
33 class TreePattern;
34 class TreePatternNode;
Chris Lattnerfe718932008-01-06 01:10:31 +000035 class CodeGenDAGPatterns;
Chris Lattner6cefb772008-01-05 22:25:12 +000036 class ComplexPattern;
37
Owen Andersone50ed302009-08-10 22:56:29 +000038/// EEVT::DAGISelGenValueType - These are some extended forms of
Owen Anderson825b72b2009-08-11 20:47:22 +000039/// MVT::SimpleValueType that we use as lattice values during type inference.
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000040/// The existing MVT iAny, fAny and vAny types suffice to represent
41/// arbitrary integer, floating-point, and vector types, so only an unknown
42/// value is needed.
Owen Andersone50ed302009-08-10 22:56:29 +000043namespace EEVT {
Chris Lattner2cacec52010-03-15 06:00:16 +000044 /// TypeSet - This is either empty if it's completely unknown, or holds a set
45 /// of types. It is used during type inference because register classes can
46 /// have multiple possible types and we don't know which one they get until
47 /// type inference is complete.
48 ///
49 /// TypeSet can have three states:
50 /// Vector is empty: The type is completely unknown, it can be any valid
51 /// target type.
52 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
53 /// of those types only.
54 /// Vector has one concrete type: The type is completely known.
55 ///
56 class TypeSet {
Chris Lattner774ce292010-03-19 17:41:26 +000057 SmallVector<MVT::SimpleValueType, 4> TypeVec;
Chris Lattner2cacec52010-03-15 06:00:16 +000058 public:
59 TypeSet() {}
60 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +000061 TypeSet(const std::vector<MVT::SimpleValueType> &VTList);
62
Chris Lattner2cacec52010-03-15 06:00:16 +000063 bool isCompletelyUnknown() const { return TypeVec.empty(); }
Jim Grosbach398abb42010-12-24 05:06:32 +000064
Chris Lattner2cacec52010-03-15 06:00:16 +000065 bool isConcrete() const {
66 if (TypeVec.size() != 1) return false;
67 unsigned char T = TypeVec[0]; (void)T;
68 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
69 return true;
70 }
Jim Grosbach398abb42010-12-24 05:06:32 +000071
Chris Lattner2cacec52010-03-15 06:00:16 +000072 MVT::SimpleValueType getConcrete() const {
73 assert(isConcrete() && "Type isn't concrete yet");
74 return (MVT::SimpleValueType)TypeVec[0];
75 }
Jim Grosbach398abb42010-12-24 05:06:32 +000076
Chris Lattner2cacec52010-03-15 06:00:16 +000077 bool isDynamicallyResolved() const {
78 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
79 }
Jim Grosbach398abb42010-12-24 05:06:32 +000080
Chris Lattner2cacec52010-03-15 06:00:16 +000081 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
82 assert(!TypeVec.empty() && "Not a type list!");
83 return TypeVec;
84 }
Jim Grosbach398abb42010-12-24 05:06:32 +000085
Chris Lattner6c6ba362010-03-18 23:15:10 +000086 bool isVoid() const {
87 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
88 }
Jim Grosbach398abb42010-12-24 05:06:32 +000089
Chris Lattner2cacec52010-03-15 06:00:16 +000090 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
91 /// types.
92 bool hasIntegerTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +000093
Chris Lattner2cacec52010-03-15 06:00:16 +000094 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
95 /// a floating point value type.
96 bool hasFloatingPointTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +000097
Chris Lattner2cacec52010-03-15 06:00:16 +000098 /// hasVectorTypes - Return true if this TypeSet contains a vector value
99 /// type.
100 bool hasVectorTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000101
Chris Lattner2cacec52010-03-15 06:00:16 +0000102 /// getName() - Return this TypeSet as a string.
103 std::string getName() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000104
Chris Lattner2cacec52010-03-15 06:00:16 +0000105 /// MergeInTypeInfo - This merges in type information from the specified
106 /// argument. If 'this' changes, it returns true. If the two types are
107 /// contradictory (e.g. merge f32 into i32) then this throws an exception.
108 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000109
Chris Lattner2cacec52010-03-15 06:00:16 +0000110 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
111 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
112 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000113
Chris Lattner2cacec52010-03-15 06:00:16 +0000114 /// Force this type list to only contain integer types.
115 bool EnforceInteger(TreePattern &TP);
Bob Wilson36e3e662009-08-12 22:30:59 +0000116
Chris Lattner2cacec52010-03-15 06:00:16 +0000117 /// Force this type list to only contain floating point types.
118 bool EnforceFloatingPoint(TreePattern &TP);
119
120 /// EnforceScalar - Remove all vector types from this type list.
121 bool EnforceScalar(TreePattern &TP);
122
123 /// EnforceVector - Remove all non-vector types from this type list.
124 bool EnforceVector(TreePattern &TP);
125
126 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
127 /// this an other based on this information.
128 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000129
Chris Lattner2cacec52010-03-15 06:00:16 +0000130 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
131 /// whose element is VT.
Chris Lattner66fb9d22010-03-24 00:01:16 +0000132 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000133
David Greene60322692011-01-24 20:53:18 +0000134 /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to
135 /// be a vector type VT.
136 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
137
Chris Lattner2cacec52010-03-15 06:00:16 +0000138 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
139 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000140
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000141 private:
142 /// FillWithPossibleTypes - Set to all legal types and return true, only
Chris Lattner774ce292010-03-19 17:41:26 +0000143 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
144 /// that pass the predicate are added.
145 bool FillWithPossibleTypes(TreePattern &TP,
146 bool (*Pred)(MVT::SimpleValueType) = 0,
147 const char *PredicateName = 0);
Chris Lattner2cacec52010-03-15 06:00:16 +0000148 };
Chris Lattner6cefb772008-01-05 22:25:12 +0000149}
150
Scott Michel327d0652008-03-05 17:49:05 +0000151/// Set type used to track multiply used variables in patterns
152typedef std::set<std::string> MultipleUseVarSet;
153
Chris Lattner6cefb772008-01-05 22:25:12 +0000154/// SDTypeConstraint - This is a discriminated union of constraints,
155/// corresponding to the SDTypeConstraint tablegen class in Target.td.
156struct SDTypeConstraint {
157 SDTypeConstraint(Record *R);
Jim Grosbach398abb42010-12-24 05:06:32 +0000158
Chris Lattner6cefb772008-01-05 22:25:12 +0000159 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach398abb42010-12-24 05:06:32 +0000160 enum {
161 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene60322692011-01-24 20:53:18 +0000162 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
163 SDTCisSubVecOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +0000164 } ConstraintType;
Jim Grosbach398abb42010-12-24 05:06:32 +0000165
Chris Lattner6cefb772008-01-05 22:25:12 +0000166 union { // The discriminated union.
167 struct {
Chris Lattner2cacec52010-03-15 06:00:16 +0000168 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +0000169 } SDTCisVT_Info;
170 struct {
171 unsigned OtherOperandNum;
172 } SDTCisSameAs_Info;
173 struct {
174 unsigned OtherOperandNum;
175 } SDTCisVTSmallerThanOp_Info;
176 struct {
177 unsigned BigOperandNum;
178 } SDTCisOpSmallerThanOp_Info;
179 struct {
180 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +0000181 } SDTCisEltOfVec_Info;
David Greene60322692011-01-24 20:53:18 +0000182 struct {
183 unsigned OtherOperandNum;
184 } SDTCisSubVecOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +0000185 } x;
186
187 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
188 /// constraint to the nodes operands. This returns true if it makes a
189 /// change, false otherwise. If a type contradiction is found, throw an
190 /// exception.
191 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
192 TreePattern &TP) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000193};
194
195/// SDNodeInfo - One of these records is created for each SDNode instance in
196/// the target .td file. This represents the various dag nodes we will be
197/// processing.
198class SDNodeInfo {
199 Record *Def;
200 std::string EnumName;
201 std::string SDClassName;
202 unsigned Properties;
203 unsigned NumResults;
204 int NumOperands;
205 std::vector<SDTypeConstraint> TypeConstraints;
206public:
207 SDNodeInfo(Record *R); // Parse the specified record.
Jim Grosbach398abb42010-12-24 05:06:32 +0000208
Chris Lattner6cefb772008-01-05 22:25:12 +0000209 unsigned getNumResults() const { return NumResults; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000210
Chris Lattner2a22cdc2010-03-28 08:48:47 +0000211 /// getNumOperands - This is the number of operands required or -1 if
212 /// variadic.
Chris Lattner6cefb772008-01-05 22:25:12 +0000213 int getNumOperands() const { return NumOperands; }
214 Record *getRecord() const { return Def; }
215 const std::string &getEnumName() const { return EnumName; }
216 const std::string &getSDClassName() const { return SDClassName; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000217
Chris Lattner6cefb772008-01-05 22:25:12 +0000218 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
219 return TypeConstraints;
220 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000221
Chris Lattner22579812010-02-28 00:22:30 +0000222 /// getKnownType - If the type constraints on this node imply a fixed type
223 /// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000224 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner084df622010-03-24 00:41:19 +0000225 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000226
Chris Lattner6cefb772008-01-05 22:25:12 +0000227 /// hasProperty - Return true if this node has the specified property.
228 ///
229 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
230
231 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
232 /// constraints for this node to the operands of the node. This returns
233 /// true if it makes a change, false otherwise. If a type contradiction is
234 /// found, throw an exception.
235 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
236 bool MadeChange = false;
237 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
238 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
239 return MadeChange;
240 }
241};
Chris Lattner54379062011-04-17 21:38:24 +0000242
243/// TreePredicateFn - This is an abstraction that represents the predicates on
244/// a PatFrag node. This is a simple one-word wrapper around a pointer to
245/// provide nice accessors.
246class TreePredicateFn {
247 /// PatFragRec - This is the TreePattern for the PatFrag that we
248 /// originally came from.
249 TreePattern *PatFragRec;
250public:
251 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner7ed13912011-04-17 22:05:17 +0000252 TreePredicateFn(TreePattern *N);
Chris Lattner54379062011-04-17 21:38:24 +0000253
254
255 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
256
257 /// isAlwaysTrue - Return true if this is a noop predicate.
258 bool isAlwaysTrue() const;
259
Chris Lattner1518afd2011-04-18 06:22:33 +0000260 bool isImmediatePattern() const { return !getImmCode().empty(); }
261
262 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
263 /// this is an immediate predicate. It is an error to call this on a
264 /// non-immediate pattern.
265 std::string getImmediatePredicateCode() const {
266 std::string Result = getImmCode();
267 assert(!Result.empty() && "Isn't an immediate pattern!");
268 return Result;
269 }
270
Chris Lattner54379062011-04-17 21:38:24 +0000271
272 bool operator==(const TreePredicateFn &RHS) const {
273 return PatFragRec == RHS.PatFragRec;
274 }
275
276 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
277
278 /// Return the name to use in the generated code to reference this, this is
279 /// "Predicate_foo" if from a pattern fragment "foo".
280 std::string getFnName() const;
281
282 /// getCodeToRunOnSDNode - Return the code for the function body that
283 /// evaluates this predicate. The argument is expected to be in "Node",
284 /// not N. This handles casting and conversion to a concrete node type as
285 /// appropriate.
286 std::string getCodeToRunOnSDNode() const;
287
288private:
289 std::string getPredCode() const;
Chris Lattner7ed13912011-04-17 22:05:17 +0000290 std::string getImmCode() const;
Chris Lattner54379062011-04-17 21:38:24 +0000291};
292
Chris Lattner6cefb772008-01-05 22:25:12 +0000293
294/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
295/// patterns), and as such should be ref counted. We currently just leak all
296/// TreePatternNode objects!
297class TreePatternNode {
Chris Lattnerd7349192010-03-19 21:37:09 +0000298 /// The type of each node result. Before and during type inference, each
299 /// result may be a set of possible types. After (successful) type inference,
300 /// each is a single concrete type.
301 SmallVector<EEVT::TypeSet, 1> Types;
Jim Grosbach398abb42010-12-24 05:06:32 +0000302
Chris Lattner6cefb772008-01-05 22:25:12 +0000303 /// Operator - The Record for the operator if this is an interior node (not
304 /// a leaf).
305 Record *Operator;
Jim Grosbach398abb42010-12-24 05:06:32 +0000306
Chris Lattner6cefb772008-01-05 22:25:12 +0000307 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
308 ///
David Greened4a90662011-07-11 18:25:51 +0000309 const Init *Val;
Jim Grosbach398abb42010-12-24 05:06:32 +0000310
Chris Lattner6cefb772008-01-05 22:25:12 +0000311 /// Name - The name given to this node with the :$foo notation.
312 ///
313 std::string Name;
Jim Grosbach398abb42010-12-24 05:06:32 +0000314
Dan Gohman0540e172008-10-15 06:17:21 +0000315 /// PredicateFns - The predicate functions to execute on this node to check
316 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner54379062011-04-17 21:38:24 +0000317 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach398abb42010-12-24 05:06:32 +0000318
Chris Lattner6cefb772008-01-05 22:25:12 +0000319 /// TransformFn - The transformation function to execute on this node before
320 /// it can be substituted into the resulting instruction on a pattern match.
321 Record *TransformFn;
Jim Grosbach398abb42010-12-24 05:06:32 +0000322
Chris Lattner6cefb772008-01-05 22:25:12 +0000323 std::vector<TreePatternNode*> Children;
324public:
Chris Lattnerd7349192010-03-19 21:37:09 +0000325 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach398abb42010-12-24 05:06:32 +0000326 unsigned NumResults)
Chris Lattnerd7349192010-03-19 21:37:09 +0000327 : Operator(Op), Val(0), TransformFn(0), Children(Ch) {
328 Types.resize(NumResults);
329 }
David Greened4a90662011-07-11 18:25:51 +0000330 TreePatternNode(const Init *val, unsigned NumResults) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000331 : Operator(0), Val(val), TransformFn(0) {
Chris Lattnerd7349192010-03-19 21:37:09 +0000332 Types.resize(NumResults);
Chris Lattner6cefb772008-01-05 22:25:12 +0000333 }
334 ~TreePatternNode();
Jim Grosbach398abb42010-12-24 05:06:32 +0000335
Chris Lattner6cefb772008-01-05 22:25:12 +0000336 const std::string &getName() const { return Name; }
Chris Lattnerc2173052010-03-28 06:50:34 +0000337 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach398abb42010-12-24 05:06:32 +0000338
Chris Lattner6cefb772008-01-05 22:25:12 +0000339 bool isLeaf() const { return Val != 0; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000340
Chris Lattner2cacec52010-03-15 06:00:16 +0000341 // Type accessors.
Chris Lattnerd7349192010-03-19 21:37:09 +0000342 unsigned getNumTypes() const { return Types.size(); }
343 MVT::SimpleValueType getType(unsigned ResNo) const {
344 return Types[ResNo].getConcrete();
345 }
346 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
347 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
348 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
349 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000350
Chris Lattnerd7349192010-03-19 21:37:09 +0000351 bool hasTypeSet(unsigned ResNo) const {
352 return Types[ResNo].isConcrete();
353 }
354 bool isTypeCompletelyUnknown(unsigned ResNo) const {
355 return Types[ResNo].isCompletelyUnknown();
356 }
357 bool isTypeDynamicallyResolved(unsigned ResNo) const {
358 return Types[ResNo].isDynamicallyResolved();
359 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000360
David Greened4a90662011-07-11 18:25:51 +0000361 const Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000362 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000363
Chris Lattner6cefb772008-01-05 22:25:12 +0000364 unsigned getNumChildren() const { return Children.size(); }
365 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
366 void setChild(unsigned i, TreePatternNode *N) {
367 Children[i] = N;
368 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000369
Chris Lattnere39650a2010-02-16 06:10:58 +0000370 /// hasChild - Return true if N is any of our children.
371 bool hasChild(const TreePatternNode *N) const {
372 for (unsigned i = 0, e = Children.size(); i != e; ++i)
373 if (Children[i] == N) return true;
374 return false;
375 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000376
Chris Lattner54379062011-04-17 21:38:24 +0000377 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
378
379 const std::vector<TreePredicateFn> &getPredicateFns() const {
380 return PredicateFns;
381 }
Dan Gohman0540e172008-10-15 06:17:21 +0000382 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner54379062011-04-17 21:38:24 +0000383 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman0540e172008-10-15 06:17:21 +0000384 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
385 PredicateFns = Fns;
386 }
Chris Lattner54379062011-04-17 21:38:24 +0000387 void addPredicateFn(const TreePredicateFn &Fn) {
388 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
Dan Gohman0540e172008-10-15 06:17:21 +0000389 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
390 PredicateFns.end())
391 PredicateFns.push_back(Fn);
392 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000393
394 Record *getTransformFn() const { return TransformFn; }
395 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000396
Chris Lattnere67bde52008-01-06 05:36:50 +0000397 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
398 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
399 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000400
Chris Lattner47661322010-02-14 22:22:58 +0000401 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
402 /// return the ComplexPattern information, otherwise return null.
403 const ComplexPattern *
404 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
405
406 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000407 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000408
Chris Lattner47661322010-02-14 22:22:58 +0000409 /// TreeHasProperty - Return true if any node in this tree has the specified
410 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000411 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000412
Evan Cheng6bd95672008-06-16 20:29:38 +0000413 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
414 /// marked isCommutative.
415 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000416
Daniel Dunbar1a551802009-07-03 00:10:29 +0000417 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000418 void dump() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000419
Chris Lattner6cefb772008-01-05 22:25:12 +0000420public: // Higher level manipulation routines.
421
422 /// clone - Return a new copy of this tree.
423 ///
424 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000425
426 /// RemoveAllTypes - Recursively strip all the types of this tree.
427 void RemoveAllTypes();
Jim Grosbach398abb42010-12-24 05:06:32 +0000428
Chris Lattner6cefb772008-01-05 22:25:12 +0000429 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
430 /// the specified node. For this comparison, all of the state of the node
431 /// is considered, except for the assigned name. Nodes with differing names
432 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000433 bool isIsomorphicTo(const TreePatternNode *N,
434 const MultipleUseVarSet &DepVars) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000435
Chris Lattner6cefb772008-01-05 22:25:12 +0000436 /// SubstituteFormalArguments - Replace the formal arguments in this tree
437 /// with actual values specified by ArgMap.
438 void SubstituteFormalArguments(std::map<std::string,
439 TreePatternNode*> &ArgMap);
440
441 /// InlinePatternFragments - If this pattern refers to any pattern
442 /// fragments, inline them into place, giving us a pattern without any
443 /// PatFrag references.
444 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000445
Bob Wilson6c01ca92009-01-05 17:23:09 +0000446 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000447 /// this node and its children in the tree. This returns true if it makes a
448 /// change, false otherwise. If a type contradiction is found, throw an
449 /// exception.
450 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach398abb42010-12-24 05:06:32 +0000451
Chris Lattner6cefb772008-01-05 22:25:12 +0000452 /// UpdateNodeType - Set the node type of N to VT if VT contains
453 /// information. If N already contains a conflicting type, then throw an
454 /// exception. This returns true if any information was updated.
455 ///
Chris Lattnerd7349192010-03-19 21:37:09 +0000456 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
457 TreePattern &TP) {
458 return Types[ResNo].MergeInTypeInfo(InTy, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000459 }
460
Chris Lattnerd7349192010-03-19 21:37:09 +0000461 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
462 TreePattern &TP) {
463 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000464 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000465
Chris Lattner6cefb772008-01-05 22:25:12 +0000466 /// ContainsUnresolvedType - Return true if this tree contains any
467 /// unresolved types.
468 bool ContainsUnresolvedType() const {
Chris Lattnerd7349192010-03-19 21:37:09 +0000469 for (unsigned i = 0, e = Types.size(); i != e; ++i)
470 if (!Types[i].isConcrete()) return true;
Jim Grosbach398abb42010-12-24 05:06:32 +0000471
Chris Lattner6cefb772008-01-05 22:25:12 +0000472 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
473 if (getChild(i)->ContainsUnresolvedType()) return true;
474 return false;
475 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000476
Chris Lattner6cefb772008-01-05 22:25:12 +0000477 /// canPatternMatch - If it is impossible for this pattern to match on this
478 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000479 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000480};
481
Chris Lattner383fed92010-02-14 21:10:33 +0000482inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
483 TPN.print(OS);
484 return OS;
485}
Jim Grosbach398abb42010-12-24 05:06:32 +0000486
Chris Lattner6cefb772008-01-05 22:25:12 +0000487
488/// TreePattern - Represent a pattern, used for instructions, pattern
489/// fragments, etc.
490///
491class TreePattern {
492 /// Trees - The list of pattern trees which corresponds to this pattern.
493 /// Note that PatFrag's only have a single tree.
494 ///
495 std::vector<TreePatternNode*> Trees;
Jim Grosbach398abb42010-12-24 05:06:32 +0000496
Chris Lattner2cacec52010-03-15 06:00:16 +0000497 /// NamedNodes - This is all of the nodes that have names in the trees in this
498 /// pattern.
499 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach398abb42010-12-24 05:06:32 +0000500
Chris Lattner6cefb772008-01-05 22:25:12 +0000501 /// TheRecord - The actual TableGen record corresponding to this pattern.
502 ///
503 Record *TheRecord;
Jim Grosbach398abb42010-12-24 05:06:32 +0000504
Chris Lattner6cefb772008-01-05 22:25:12 +0000505 /// Args - This is a list of all of the arguments to this pattern (for
506 /// PatFrag patterns), which are the 'node' markers in this pattern.
507 std::vector<std::string> Args;
Jim Grosbach398abb42010-12-24 05:06:32 +0000508
Chris Lattner6cefb772008-01-05 22:25:12 +0000509 /// CDP - the top-level object coordinating this madness.
510 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000511 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000512
513 /// isInputPattern - True if this is an input pattern, something to match.
514 /// False if this is an output pattern, something to emit.
515 bool isInputPattern;
516public:
Jim Grosbach398abb42010-12-24 05:06:32 +0000517
Chris Lattner6cefb772008-01-05 22:25:12 +0000518 /// TreePattern constructor - Parse the specified DagInits into the
519 /// current record.
David Greened4a90662011-07-11 18:25:51 +0000520 TreePattern(Record *TheRec, const ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000521 CodeGenDAGPatterns &ise);
David Greened4a90662011-07-11 18:25:51 +0000522 TreePattern(Record *TheRec, const DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000523 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000524 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000525 CodeGenDAGPatterns &ise);
Jim Grosbach398abb42010-12-24 05:06:32 +0000526
Chris Lattner6cefb772008-01-05 22:25:12 +0000527 /// getTrees - Return the tree patterns which corresponds to this pattern.
528 ///
529 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
530 unsigned getNumTrees() const { return Trees.size(); }
531 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
532 TreePatternNode *getOnlyTree() const {
533 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
534 return Trees[0];
535 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000536
Chris Lattner2cacec52010-03-15 06:00:16 +0000537 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
538 if (NamedNodes.empty())
539 ComputeNamedNodes();
540 return NamedNodes;
541 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000542
Chris Lattner6cefb772008-01-05 22:25:12 +0000543 /// getRecord - Return the actual TableGen record corresponding to this
544 /// pattern.
545 ///
546 Record *getRecord() const { return TheRecord; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000547
Chris Lattner6cefb772008-01-05 22:25:12 +0000548 unsigned getNumArgs() const { return Args.size(); }
549 const std::string &getArgName(unsigned i) const {
550 assert(i < Args.size() && "Argument reference out of range!");
551 return Args[i];
552 }
553 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000554
Chris Lattnerfe718932008-01-06 01:10:31 +0000555 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000556
557 /// InlinePatternFragments - If this pattern refers to any pattern
558 /// fragments, inline them into place, giving us a pattern without any
559 /// PatFrag references.
560 void InlinePatternFragments() {
561 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
562 Trees[i] = Trees[i]->InlinePatternFragments(*this);
563 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000564
Chris Lattner6cefb772008-01-05 22:25:12 +0000565 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000566 /// patterns as possible. Return true if all types are inferred, false
Chris Lattner6cefb772008-01-05 22:25:12 +0000567 /// otherwise. Throw an exception if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000568 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
569 *NamedTypes=0);
Jim Grosbach398abb42010-12-24 05:06:32 +0000570
Chris Lattner6cefb772008-01-05 22:25:12 +0000571 /// error - Throw an exception, prefixing it with information about this
572 /// pattern.
573 void error(const std::string &Msg) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000574
Daniel Dunbar1a551802009-07-03 00:10:29 +0000575 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000576 void dump() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000577
Chris Lattner6cefb772008-01-05 22:25:12 +0000578private:
David Greened4a90662011-07-11 18:25:51 +0000579 TreePatternNode *ParseTreePattern(const Init *DI, StringRef OpName);
Chris Lattner2cacec52010-03-15 06:00:16 +0000580 void ComputeNamedNodes();
581 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000582};
583
584/// DAGDefaultOperand - One of these is created for each PredicateOperand
585/// or OptionalDefOperand that has a set ExecuteAlways / DefaultOps field.
586struct DAGDefaultOperand {
587 std::vector<TreePatternNode*> DefaultOps;
588};
589
590class DAGInstruction {
591 TreePattern *Pattern;
592 std::vector<Record*> Results;
593 std::vector<Record*> Operands;
594 std::vector<Record*> ImpResults;
Chris Lattner6cefb772008-01-05 22:25:12 +0000595 TreePatternNode *ResultPattern;
596public:
597 DAGInstruction(TreePattern *TP,
598 const std::vector<Record*> &results,
599 const std::vector<Record*> &operands,
Chris Lattner62bcec82010-04-20 06:28:43 +0000600 const std::vector<Record*> &impresults)
Jim Grosbach398abb42010-12-24 05:06:32 +0000601 : Pattern(TP), Results(results), Operands(operands),
Chris Lattner62bcec82010-04-20 06:28:43 +0000602 ImpResults(impresults), ResultPattern(0) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000603
Chris Lattnerf1ab4f12008-01-06 01:52:22 +0000604 const TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000605 unsigned getNumResults() const { return Results.size(); }
606 unsigned getNumOperands() const { return Operands.size(); }
607 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000608 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000609
Chris Lattner6cefb772008-01-05 22:25:12 +0000610 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000611
Chris Lattner6cefb772008-01-05 22:25:12 +0000612 Record *getResult(unsigned RN) const {
613 assert(RN < Results.size());
614 return Results[RN];
615 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000616
Chris Lattner6cefb772008-01-05 22:25:12 +0000617 Record *getOperand(unsigned ON) const {
618 assert(ON < Operands.size());
619 return Operands[ON];
620 }
621
622 Record *getImpResult(unsigned RN) const {
623 assert(RN < ImpResults.size());
624 return ImpResults[RN];
625 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000626
Chris Lattner6cefb772008-01-05 22:25:12 +0000627 TreePatternNode *getResultPattern() const { return ResultPattern; }
628};
Jim Grosbach398abb42010-12-24 05:06:32 +0000629
Chris Lattnerfe718932008-01-06 01:10:31 +0000630/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000631/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000632class PatternToMatch {
633public:
David Greened4a90662011-07-11 18:25:51 +0000634 PatternToMatch(Record *srcrecord, const ListInit *preds,
Chris Lattner6cefb772008-01-05 22:25:12 +0000635 TreePatternNode *src, TreePatternNode *dst,
636 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000637 unsigned complexity, unsigned uid)
Jim Grosbach997759a2010-12-07 23:05:49 +0000638 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
Chris Lattner117ccb72010-03-01 22:09:11 +0000639 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000640
Jim Grosbach997759a2010-12-07 23:05:49 +0000641 Record *SrcRecord; // Originating Record for the pattern.
David Greened4a90662011-07-11 18:25:51 +0000642 const ListInit *Predicates; // Top level predicate conditions to match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000643 TreePatternNode *SrcPattern; // Source pattern to match.
644 TreePatternNode *DstPattern; // Resulting pattern.
645 std::vector<Record*> Dstregs; // Physical register defs being matched.
646 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000647 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000648
Jim Grosbach997759a2010-12-07 23:05:49 +0000649 Record *getSrcRecord() const { return SrcRecord; }
David Greened4a90662011-07-11 18:25:51 +0000650 const ListInit *getPredicates() const { return Predicates; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000651 TreePatternNode *getSrcPattern() const { return SrcPattern; }
652 TreePatternNode *getDstPattern() const { return DstPattern; }
653 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
654 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000655
656 std::string getPredicateCheck() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000657
Chris Lattner48e86db2010-03-29 01:40:38 +0000658 /// Compute the complexity metric for the input pattern. This roughly
659 /// corresponds to the number of nodes that are covered.
660 unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000661};
662
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000663// Deterministic comparison of Record*.
664struct RecordPtrCmp {
665 bool operator()(const Record *LHS, const Record *RHS) const;
666};
Jim Grosbach398abb42010-12-24 05:06:32 +0000667
Chris Lattnerfe718932008-01-06 01:10:31 +0000668class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000669 RecordKeeper &Records;
670 CodeGenTarget Target;
671 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000672 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Jim Grosbach398abb42010-12-24 05:06:32 +0000673
Daniel Dunbar6f5cc822009-08-23 09:47:37 +0000674 std::map<Record*, SDNodeInfo, RecordPtrCmp> SDNodes;
675 std::map<Record*, std::pair<Record*, std::string>, RecordPtrCmp> SDNodeXForms;
676 std::map<Record*, ComplexPattern, RecordPtrCmp> ComplexPatterns;
677 std::map<Record*, TreePattern*, RecordPtrCmp> PatternFragments;
678 std::map<Record*, DAGDefaultOperand, RecordPtrCmp> DefaultOperands;
679 std::map<Record*, DAGInstruction, RecordPtrCmp> Instructions;
Jim Grosbach398abb42010-12-24 05:06:32 +0000680
Chris Lattner6cefb772008-01-05 22:25:12 +0000681 // Specific SDNode definitions:
682 Record *intrinsic_void_sdnode;
683 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach398abb42010-12-24 05:06:32 +0000684
Chris Lattner6cefb772008-01-05 22:25:12 +0000685 /// PatternsToMatch - All of the things we are matching on the DAG. The first
686 /// value is the pattern to match, the second pattern is the result to
687 /// emit.
688 std::vector<PatternToMatch> PatternsToMatch;
689public:
Jim Grosbach398abb42010-12-24 05:06:32 +0000690 CodeGenDAGPatterns(RecordKeeper &R);
Chris Lattnerfe718932008-01-06 01:10:31 +0000691 ~CodeGenDAGPatterns();
Jim Grosbach398abb42010-12-24 05:06:32 +0000692
Dan Gohmanee4fa192008-04-03 00:02:49 +0000693 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000694 const CodeGenTarget &getTargetInfo() const { return Target; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000695
Chris Lattner6cefb772008-01-05 22:25:12 +0000696 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000697
Chris Lattner6cefb772008-01-05 22:25:12 +0000698 const SDNodeInfo &getSDNodeInfo(Record *R) const {
699 assert(SDNodes.count(R) && "Unknown node!");
700 return SDNodes.find(R)->second;
701 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000702
Chris Lattner443e3f92008-01-05 22:54:53 +0000703 // Node transformation lookups.
704 typedef std::pair<Record*, std::string> NodeXForm;
705 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000706 assert(SDNodeXForms.count(R) && "Invalid transform!");
707 return SDNodeXForms.find(R)->second;
708 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000709
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000710 typedef std::map<Record*, NodeXForm, RecordPtrCmp>::const_iterator
711 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000712 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
713 nx_iterator nx_end() const { return SDNodeXForms.end(); }
714
Jim Grosbach398abb42010-12-24 05:06:32 +0000715
Chris Lattner6cefb772008-01-05 22:25:12 +0000716 const ComplexPattern &getComplexPattern(Record *R) const {
717 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
718 return ComplexPatterns.find(R)->second;
719 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000720
Chris Lattner6cefb772008-01-05 22:25:12 +0000721 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
722 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
723 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000724 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
725 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Chris Lattner6cefb772008-01-05 22:25:12 +0000726 assert(0 && "Unknown intrinsic!");
727 abort();
728 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000729
Chris Lattner6cefb772008-01-05 22:25:12 +0000730 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000731 if (IID-1 < Intrinsics.size())
732 return Intrinsics[IID-1];
733 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
734 return TgtIntrinsics[IID-Intrinsics.size()-1];
735 assert(0 && "Bad intrinsic ID!");
Evan Cheng32c1a4c2009-02-09 03:07:24 +0000736 abort();
Chris Lattner6cefb772008-01-05 22:25:12 +0000737 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000738
Chris Lattner6cefb772008-01-05 22:25:12 +0000739 unsigned getIntrinsicID(Record *R) const {
740 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
741 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000742 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
743 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Chris Lattner6cefb772008-01-05 22:25:12 +0000744 assert(0 && "Unknown intrinsic!");
745 abort();
746 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000747
Chris Lattnerb49985a2010-02-18 06:47:49 +0000748 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000749 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
750 return DefaultOperands.find(R)->second;
751 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000752
Chris Lattner6cefb772008-01-05 22:25:12 +0000753 // Pattern Fragment information.
754 TreePattern *getPatternFragment(Record *R) const {
755 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
756 return PatternFragments.find(R)->second;
757 }
Chris Lattnerd7349192010-03-19 21:37:09 +0000758 TreePattern *getPatternFragmentIfRead(Record *R) const {
759 if (!PatternFragments.count(R)) return 0;
760 return PatternFragments.find(R)->second;
761 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000762
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000763 typedef std::map<Record*, TreePattern*, RecordPtrCmp>::const_iterator
764 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000765 pf_iterator pf_begin() const { return PatternFragments.begin(); }
766 pf_iterator pf_end() const { return PatternFragments.end(); }
767
768 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000769 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
770 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
771 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Jim Grosbach398abb42010-12-24 05:06:32 +0000772
773
774
Chris Lattner6cefb772008-01-05 22:25:12 +0000775 const DAGInstruction &getInstruction(Record *R) const {
776 assert(Instructions.count(R) && "Unknown instruction!");
777 return Instructions.find(R)->second;
778 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000779
Chris Lattner6cefb772008-01-05 22:25:12 +0000780 Record *get_intrinsic_void_sdnode() const {
781 return intrinsic_void_sdnode;
782 }
783 Record *get_intrinsic_w_chain_sdnode() const {
784 return intrinsic_w_chain_sdnode;
785 }
786 Record *get_intrinsic_wo_chain_sdnode() const {
787 return intrinsic_wo_chain_sdnode;
788 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000789
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000790 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
791
Chris Lattner6cefb772008-01-05 22:25:12 +0000792private:
793 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000794 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000795 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000796 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000797 void ParseDefaultOperands();
798 void ParseInstructions();
799 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000800 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000801 void GenerateVariants();
Jim Grosbach398abb42010-12-24 05:06:32 +0000802
Chris Lattner25b6f912010-02-23 06:16:51 +0000803 void AddPatternToMatch(const TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000804 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
805 std::map<std::string,
806 TreePatternNode*> &InstInputs,
807 std::map<std::string,
808 TreePatternNode*> &InstResults,
Chris Lattner6cefb772008-01-05 22:25:12 +0000809 std::vector<Record*> &InstImpResults);
810};
811} // end namespace llvm
812
813#endif