blob: 7c2fa367410860c569d9a9dd8b29e47fa20d6c2f [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 "CodeGenIntrinsics.h"
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000019#include "CodeGenTarget.h"
Chris Lattner2cacec52010-03-15 06:00:16 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
Craig Topper655b8de2012-02-05 07:21:30 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattnere14d2e22010-03-19 01:07:44 +000023#include <algorithm>
Chris Lattnere14d2e22010-03-19 01:07:44 +000024#include <map>
Chandler Carruth4ffd89f2012-12-04 10:37:14 +000025#include <set>
26#include <vector>
Chris Lattner6cefb772008-01-05 22:25:12 +000027
28namespace llvm {
29 class Record;
David Greeneafd54262011-07-13 22:25:51 +000030 class Init;
Chris Lattner6cefb772008-01-05 22:25:12 +000031 class ListInit;
32 class DagInit;
33 class SDNodeInfo;
34 class TreePattern;
35 class TreePatternNode;
Chris Lattnerfe718932008-01-06 01:10:31 +000036 class CodeGenDAGPatterns;
Chris Lattner6cefb772008-01-05 22:25:12 +000037 class ComplexPattern;
38
Owen Andersone50ed302009-08-10 22:56:29 +000039/// EEVT::DAGISelGenValueType - These are some extended forms of
Owen Anderson825b72b2009-08-11 20:47:22 +000040/// MVT::SimpleValueType that we use as lattice values during type inference.
Bob Wilsoncdfa01b2009-08-29 05:53:25 +000041/// The existing MVT iAny, fAny and vAny types suffice to represent
42/// arbitrary integer, floating-point, and vector types, so only an unknown
43/// value is needed.
Owen Andersone50ed302009-08-10 22:56:29 +000044namespace EEVT {
Chris Lattner2cacec52010-03-15 06:00:16 +000045 /// TypeSet - This is either empty if it's completely unknown, or holds a set
46 /// of types. It is used during type inference because register classes can
47 /// have multiple possible types and we don't know which one they get until
48 /// type inference is complete.
49 ///
50 /// TypeSet can have three states:
51 /// Vector is empty: The type is completely unknown, it can be any valid
52 /// target type.
53 /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
54 /// of those types only.
55 /// Vector has one concrete type: The type is completely known.
56 ///
57 class TypeSet {
Chris Lattner774ce292010-03-19 17:41:26 +000058 SmallVector<MVT::SimpleValueType, 4> TypeVec;
Chris Lattner2cacec52010-03-15 06:00:16 +000059 public:
60 TypeSet() {}
61 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
Jakob Stoklund Olesen26369a92013-03-17 17:26:09 +000062 TypeSet(ArrayRef<MVT::SimpleValueType> VTList);
Jim Grosbach398abb42010-12-24 05:06:32 +000063
Chris Lattner2cacec52010-03-15 06:00:16 +000064 bool isCompletelyUnknown() const { return TypeVec.empty(); }
Jim Grosbach398abb42010-12-24 05:06:32 +000065
Chris Lattner2cacec52010-03-15 06:00:16 +000066 bool isConcrete() const {
67 if (TypeVec.size() != 1) return false;
68 unsigned char T = TypeVec[0]; (void)T;
69 assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
70 return true;
71 }
Jim Grosbach398abb42010-12-24 05:06:32 +000072
Chris Lattner2cacec52010-03-15 06:00:16 +000073 MVT::SimpleValueType getConcrete() const {
74 assert(isConcrete() && "Type isn't concrete yet");
75 return (MVT::SimpleValueType)TypeVec[0];
76 }
Jim Grosbach398abb42010-12-24 05:06:32 +000077
Chris Lattner2cacec52010-03-15 06:00:16 +000078 bool isDynamicallyResolved() const {
79 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
80 }
Jim Grosbach398abb42010-12-24 05:06:32 +000081
Chris Lattner2cacec52010-03-15 06:00:16 +000082 const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
83 assert(!TypeVec.empty() && "Not a type list!");
84 return TypeVec;
85 }
Jim Grosbach398abb42010-12-24 05:06:32 +000086
Chris Lattner6c6ba362010-03-18 23:15:10 +000087 bool isVoid() const {
88 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
89 }
Jim Grosbach398abb42010-12-24 05:06:32 +000090
Chris Lattner2cacec52010-03-15 06:00:16 +000091 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
92 /// types.
93 bool hasIntegerTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +000094
Chris Lattner2cacec52010-03-15 06:00:16 +000095 /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
96 /// a floating point value type.
97 bool hasFloatingPointTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +000098
Chris Lattner2cacec52010-03-15 06:00:16 +000099 /// hasVectorTypes - Return true if this TypeSet contains a vector value
100 /// type.
101 bool hasVectorTypes() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000102
Chris Lattner2cacec52010-03-15 06:00:16 +0000103 /// getName() - Return this TypeSet as a string.
104 std::string getName() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000105
Chris Lattner2cacec52010-03-15 06:00:16 +0000106 /// MergeInTypeInfo - This merges in type information from the specified
107 /// argument. If 'this' changes, it returns true. If the two types are
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000108 /// contradictory (e.g. merge f32 into i32) then this flags an error.
Chris Lattner2cacec52010-03-15 06:00:16 +0000109 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000110
Chris Lattner2cacec52010-03-15 06:00:16 +0000111 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
112 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
113 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000114
Chris Lattner2cacec52010-03-15 06:00:16 +0000115 /// Force this type list to only contain integer types.
116 bool EnforceInteger(TreePattern &TP);
Bob Wilson36e3e662009-08-12 22:30:59 +0000117
Chris Lattner2cacec52010-03-15 06:00:16 +0000118 /// Force this type list to only contain floating point types.
119 bool EnforceFloatingPoint(TreePattern &TP);
120
121 /// EnforceScalar - Remove all vector types from this type list.
122 bool EnforceScalar(TreePattern &TP);
123
124 /// EnforceVector - Remove all non-vector types from this type list.
125 bool EnforceVector(TreePattern &TP);
126
127 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
128 /// this an other based on this information.
129 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000130
Chris Lattner2cacec52010-03-15 06:00:16 +0000131 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
132 /// whose element is VT.
Chris Lattner66fb9d22010-03-24 00:01:16 +0000133 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000134
David Greene60322692011-01-24 20:53:18 +0000135 /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to
136 /// be a vector type VT.
137 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
138
Chris Lattner2cacec52010-03-15 06:00:16 +0000139 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
140 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000141
Chris Lattner5a9b8fb2010-03-19 04:54:36 +0000142 private:
143 /// FillWithPossibleTypes - Set to all legal types and return true, only
Chris Lattner774ce292010-03-19 17:41:26 +0000144 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
145 /// that pass the predicate are added.
146 bool FillWithPossibleTypes(TreePattern &TP,
147 bool (*Pred)(MVT::SimpleValueType) = 0,
148 const char *PredicateName = 0);
Chris Lattner2cacec52010-03-15 06:00:16 +0000149 };
Chris Lattner6cefb772008-01-05 22:25:12 +0000150}
151
Scott Michel327d0652008-03-05 17:49:05 +0000152/// Set type used to track multiply used variables in patterns
153typedef std::set<std::string> MultipleUseVarSet;
154
Chris Lattner6cefb772008-01-05 22:25:12 +0000155/// SDTypeConstraint - This is a discriminated union of constraints,
156/// corresponding to the SDTypeConstraint tablegen class in Target.td.
157struct SDTypeConstraint {
158 SDTypeConstraint(Record *R);
Jim Grosbach398abb42010-12-24 05:06:32 +0000159
Chris Lattner6cefb772008-01-05 22:25:12 +0000160 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach398abb42010-12-24 05:06:32 +0000161 enum {
162 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene60322692011-01-24 20:53:18 +0000163 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
164 SDTCisSubVecOfVec
Chris Lattner6cefb772008-01-05 22:25:12 +0000165 } ConstraintType;
Jim Grosbach398abb42010-12-24 05:06:32 +0000166
Chris Lattner6cefb772008-01-05 22:25:12 +0000167 union { // The discriminated union.
168 struct {
Chris Lattner2cacec52010-03-15 06:00:16 +0000169 MVT::SimpleValueType VT;
Chris Lattner6cefb772008-01-05 22:25:12 +0000170 } SDTCisVT_Info;
171 struct {
172 unsigned OtherOperandNum;
173 } SDTCisSameAs_Info;
174 struct {
175 unsigned OtherOperandNum;
176 } SDTCisVTSmallerThanOp_Info;
177 struct {
178 unsigned BigOperandNum;
179 } SDTCisOpSmallerThanOp_Info;
180 struct {
181 unsigned OtherOperandNum;
Nate Begemanb5af3342008-02-09 01:37:05 +0000182 } SDTCisEltOfVec_Info;
David Greene60322692011-01-24 20:53:18 +0000183 struct {
184 unsigned OtherOperandNum;
185 } SDTCisSubVecOfVec_Info;
Chris Lattner6cefb772008-01-05 22:25:12 +0000186 } x;
187
188 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
189 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000190 /// change, false otherwise. If a type contradiction is found, an error
191 /// is flagged.
Chris Lattner6cefb772008-01-05 22:25:12 +0000192 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
193 TreePattern &TP) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000194};
195
196/// SDNodeInfo - One of these records is created for each SDNode instance in
197/// the target .td file. This represents the various dag nodes we will be
198/// processing.
199class SDNodeInfo {
200 Record *Def;
201 std::string EnumName;
202 std::string SDClassName;
203 unsigned Properties;
204 unsigned NumResults;
205 int NumOperands;
206 std::vector<SDTypeConstraint> TypeConstraints;
207public:
208 SDNodeInfo(Record *R); // Parse the specified record.
Jim Grosbach398abb42010-12-24 05:06:32 +0000209
Chris Lattner6cefb772008-01-05 22:25:12 +0000210 unsigned getNumResults() const { return NumResults; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000211
Chris Lattner2a22cdc2010-03-28 08:48:47 +0000212 /// getNumOperands - This is the number of operands required or -1 if
213 /// variadic.
Chris Lattner6cefb772008-01-05 22:25:12 +0000214 int getNumOperands() const { return NumOperands; }
215 Record *getRecord() const { return Def; }
216 const std::string &getEnumName() const { return EnumName; }
217 const std::string &getSDClassName() const { return SDClassName; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000218
Chris Lattner6cefb772008-01-05 22:25:12 +0000219 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
220 return TypeConstraints;
221 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000222
Chris Lattner22579812010-02-28 00:22:30 +0000223 /// getKnownType - If the type constraints on this node imply a fixed type
224 /// (e.g. all stores return void, etc), then return it as an
Chris Lattneraac5b5b2010-03-19 01:14:27 +0000225 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner084df622010-03-24 00:41:19 +0000226 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000227
Chris Lattner6cefb772008-01-05 22:25:12 +0000228 /// hasProperty - Return true if this node has the specified property.
229 ///
230 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
231
232 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
233 /// constraints for this node to the operands of the node. This returns
234 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000235 /// found, an error is flagged.
Chris Lattner6cefb772008-01-05 22:25:12 +0000236 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
237 bool MadeChange = false;
238 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
239 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
240 return MadeChange;
241 }
242};
Chris Lattner54379062011-04-17 21:38:24 +0000243
244/// TreePredicateFn - This is an abstraction that represents the predicates on
245/// a PatFrag node. This is a simple one-word wrapper around a pointer to
246/// provide nice accessors.
247class TreePredicateFn {
248 /// PatFragRec - This is the TreePattern for the PatFrag that we
249 /// originally came from.
250 TreePattern *PatFragRec;
251public:
252 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner7ed13912011-04-17 22:05:17 +0000253 TreePredicateFn(TreePattern *N);
Chris Lattner54379062011-04-17 21:38:24 +0000254
255
256 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
257
258 /// isAlwaysTrue - Return true if this is a noop predicate.
259 bool isAlwaysTrue() const;
260
Chris Lattner1518afd2011-04-18 06:22:33 +0000261 bool isImmediatePattern() const { return !getImmCode().empty(); }
262
263 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
264 /// this is an immediate predicate. It is an error to call this on a
265 /// non-immediate pattern.
266 std::string getImmediatePredicateCode() const {
267 std::string Result = getImmCode();
268 assert(!Result.empty() && "Isn't an immediate pattern!");
269 return Result;
270 }
271
Chris Lattner54379062011-04-17 21:38:24 +0000272
273 bool operator==(const TreePredicateFn &RHS) const {
274 return PatFragRec == RHS.PatFragRec;
275 }
276
277 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
278
279 /// Return the name to use in the generated code to reference this, this is
280 /// "Predicate_foo" if from a pattern fragment "foo".
281 std::string getFnName() const;
282
283 /// getCodeToRunOnSDNode - Return the code for the function body that
284 /// evaluates this predicate. The argument is expected to be in "Node",
285 /// not N. This handles casting and conversion to a concrete node type as
286 /// appropriate.
287 std::string getCodeToRunOnSDNode() const;
288
289private:
290 std::string getPredCode() const;
Chris Lattner7ed13912011-04-17 22:05:17 +0000291 std::string getImmCode() const;
Chris Lattner54379062011-04-17 21:38:24 +0000292};
293
Chris Lattner6cefb772008-01-05 22:25:12 +0000294
295/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
296/// patterns), and as such should be ref counted. We currently just leak all
297/// TreePatternNode objects!
298class TreePatternNode {
Chris Lattnerd7349192010-03-19 21:37:09 +0000299 /// The type of each node result. Before and during type inference, each
300 /// result may be a set of possible types. After (successful) type inference,
301 /// each is a single concrete type.
302 SmallVector<EEVT::TypeSet, 1> Types;
Jim Grosbach398abb42010-12-24 05:06:32 +0000303
Chris Lattner6cefb772008-01-05 22:25:12 +0000304 /// Operator - The Record for the operator if this is an interior node (not
305 /// a leaf).
306 Record *Operator;
Jim Grosbach398abb42010-12-24 05:06:32 +0000307
Chris Lattner6cefb772008-01-05 22:25:12 +0000308 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
309 ///
David Greene05bce0b2011-07-29 22:43:06 +0000310 Init *Val;
Jim Grosbach398abb42010-12-24 05:06:32 +0000311
Chris Lattner6cefb772008-01-05 22:25:12 +0000312 /// Name - The name given to this node with the :$foo notation.
313 ///
314 std::string Name;
Jim Grosbach398abb42010-12-24 05:06:32 +0000315
Dan Gohman0540e172008-10-15 06:17:21 +0000316 /// PredicateFns - The predicate functions to execute on this node to check
317 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner54379062011-04-17 21:38:24 +0000318 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach398abb42010-12-24 05:06:32 +0000319
Chris Lattner6cefb772008-01-05 22:25:12 +0000320 /// TransformFn - The transformation function to execute on this node before
321 /// it can be substituted into the resulting instruction on a pattern match.
322 Record *TransformFn;
Jim Grosbach398abb42010-12-24 05:06:32 +0000323
Chris Lattner6cefb772008-01-05 22:25:12 +0000324 std::vector<TreePatternNode*> Children;
325public:
Chris Lattnerd7349192010-03-19 21:37:09 +0000326 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach398abb42010-12-24 05:06:32 +0000327 unsigned NumResults)
Chris Lattnerd7349192010-03-19 21:37:09 +0000328 : Operator(Op), Val(0), TransformFn(0), Children(Ch) {
329 Types.resize(NumResults);
330 }
David Greene05bce0b2011-07-29 22:43:06 +0000331 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Chris Lattner2cacec52010-03-15 06:00:16 +0000332 : Operator(0), Val(val), TransformFn(0) {
Chris Lattnerd7349192010-03-19 21:37:09 +0000333 Types.resize(NumResults);
Chris Lattner6cefb772008-01-05 22:25:12 +0000334 }
335 ~TreePatternNode();
Jim Grosbach398abb42010-12-24 05:06:32 +0000336
Jakob Stoklund Olesen7a42fb32013-03-23 18:08:44 +0000337 bool hasName() const { return !Name.empty(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000338 const std::string &getName() const { return Name; }
Chris Lattnerc2173052010-03-28 06:50:34 +0000339 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach398abb42010-12-24 05:06:32 +0000340
Chris Lattner6cefb772008-01-05 22:25:12 +0000341 bool isLeaf() const { return Val != 0; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000342
Chris Lattner2cacec52010-03-15 06:00:16 +0000343 // Type accessors.
Chris Lattnerd7349192010-03-19 21:37:09 +0000344 unsigned getNumTypes() const { return Types.size(); }
345 MVT::SimpleValueType getType(unsigned ResNo) const {
346 return Types[ResNo].getConcrete();
347 }
348 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
349 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
350 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
351 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000352
Chris Lattnerd7349192010-03-19 21:37:09 +0000353 bool hasTypeSet(unsigned ResNo) const {
354 return Types[ResNo].isConcrete();
355 }
356 bool isTypeCompletelyUnknown(unsigned ResNo) const {
357 return Types[ResNo].isCompletelyUnknown();
358 }
359 bool isTypeDynamicallyResolved(unsigned ResNo) const {
360 return Types[ResNo].isDynamicallyResolved();
361 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000362
David Greene05bce0b2011-07-29 22:43:06 +0000363 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000364 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000365
Chris Lattner6cefb772008-01-05 22:25:12 +0000366 unsigned getNumChildren() const { return Children.size(); }
367 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
368 void setChild(unsigned i, TreePatternNode *N) {
369 Children[i] = N;
370 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000371
Chris Lattnere39650a2010-02-16 06:10:58 +0000372 /// hasChild - Return true if N is any of our children.
373 bool hasChild(const TreePatternNode *N) const {
374 for (unsigned i = 0, e = Children.size(); i != e; ++i)
375 if (Children[i] == N) return true;
376 return false;
377 }
Chris Lattnere67bde52008-01-06 05:36:50 +0000378
Chris Lattner54379062011-04-17 21:38:24 +0000379 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
380
381 const std::vector<TreePredicateFn> &getPredicateFns() const {
382 return PredicateFns;
383 }
Dan Gohman0540e172008-10-15 06:17:21 +0000384 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner54379062011-04-17 21:38:24 +0000385 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman0540e172008-10-15 06:17:21 +0000386 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
387 PredicateFns = Fns;
388 }
Chris Lattner54379062011-04-17 21:38:24 +0000389 void addPredicateFn(const TreePredicateFn &Fn) {
390 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
Dan Gohman0540e172008-10-15 06:17:21 +0000391 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
392 PredicateFns.end())
393 PredicateFns.push_back(Fn);
394 }
Chris Lattner6cefb772008-01-05 22:25:12 +0000395
396 Record *getTransformFn() const { return TransformFn; }
397 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000398
Chris Lattnere67bde52008-01-06 05:36:50 +0000399 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
400 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
401 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng6bd95672008-06-16 20:29:38 +0000402
Chris Lattner47661322010-02-14 22:22:58 +0000403 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
404 /// return the ComplexPattern information, otherwise return null.
405 const ComplexPattern *
406 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
407
408 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000409 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000410
Chris Lattner47661322010-02-14 22:22:58 +0000411 /// TreeHasProperty - Return true if any node in this tree has the specified
412 /// property.
Chris Lattner751d5aa2010-02-14 22:33:49 +0000413 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000414
Evan Cheng6bd95672008-06-16 20:29:38 +0000415 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
416 /// marked isCommutative.
417 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000418
Daniel Dunbar1a551802009-07-03 00:10:29 +0000419 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000420 void dump() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000421
Chris Lattner6cefb772008-01-05 22:25:12 +0000422public: // Higher level manipulation routines.
423
424 /// clone - Return a new copy of this tree.
425 ///
426 TreePatternNode *clone() const;
Chris Lattner47661322010-02-14 22:22:58 +0000427
428 /// RemoveAllTypes - Recursively strip all the types of this tree.
429 void RemoveAllTypes();
Jim Grosbach398abb42010-12-24 05:06:32 +0000430
Chris Lattner6cefb772008-01-05 22:25:12 +0000431 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
432 /// the specified node. For this comparison, all of the state of the node
433 /// is considered, except for the assigned name. Nodes with differing names
434 /// that are otherwise identical are considered isomorphic.
Scott Michel327d0652008-03-05 17:49:05 +0000435 bool isIsomorphicTo(const TreePatternNode *N,
436 const MultipleUseVarSet &DepVars) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000437
Chris Lattner6cefb772008-01-05 22:25:12 +0000438 /// SubstituteFormalArguments - Replace the formal arguments in this tree
439 /// with actual values specified by ArgMap.
440 void SubstituteFormalArguments(std::map<std::string,
441 TreePatternNode*> &ArgMap);
442
443 /// InlinePatternFragments - If this pattern refers to any pattern
444 /// fragments, inline them into place, giving us a pattern without any
445 /// PatFrag references.
446 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach398abb42010-12-24 05:06:32 +0000447
Bob Wilson6c01ca92009-01-05 17:23:09 +0000448 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner6cefb772008-01-05 22:25:12 +0000449 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000450 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner6cefb772008-01-05 22:25:12 +0000451 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach398abb42010-12-24 05:06:32 +0000452
Chris Lattner6cefb772008-01-05 22:25:12 +0000453 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000454 /// information. If N already contains a conflicting type, then flag an
455 /// error. This returns true if any information was updated.
Chris Lattner6cefb772008-01-05 22:25:12 +0000456 ///
Chris Lattnerd7349192010-03-19 21:37:09 +0000457 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
458 TreePattern &TP) {
459 return Types[ResNo].MergeInTypeInfo(InTy, TP);
Chris Lattner2cacec52010-03-15 06:00:16 +0000460 }
461
Chris Lattnerd7349192010-03-19 21:37:09 +0000462 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
463 TreePattern &TP) {
464 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000465 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000466
Jakob Stoklund Olesen4c169162013-03-18 04:08:07 +0000467 // Update node type with types inferred from an instruction operand or result
468 // def from the ins/outs lists.
469 // Return true if the type changed.
470 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
471
Chris Lattner6cefb772008-01-05 22:25:12 +0000472 /// ContainsUnresolvedType - Return true if this tree contains any
473 /// unresolved types.
474 bool ContainsUnresolvedType() const {
Chris Lattnerd7349192010-03-19 21:37:09 +0000475 for (unsigned i = 0, e = Types.size(); i != e; ++i)
476 if (!Types[i].isConcrete()) return true;
Jim Grosbach398abb42010-12-24 05:06:32 +0000477
Chris Lattner6cefb772008-01-05 22:25:12 +0000478 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
479 if (getChild(i)->ContainsUnresolvedType()) return true;
480 return false;
481 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000482
Chris Lattner6cefb772008-01-05 22:25:12 +0000483 /// canPatternMatch - If it is impossible for this pattern to match on this
484 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanee4fa192008-04-03 00:02:49 +0000485 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner6cefb772008-01-05 22:25:12 +0000486};
487
Chris Lattner383fed92010-02-14 21:10:33 +0000488inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
489 TPN.print(OS);
490 return OS;
491}
Jim Grosbach398abb42010-12-24 05:06:32 +0000492
Chris Lattner6cefb772008-01-05 22:25:12 +0000493
494/// TreePattern - Represent a pattern, used for instructions, pattern
495/// fragments, etc.
496///
497class TreePattern {
498 /// Trees - The list of pattern trees which corresponds to this pattern.
499 /// Note that PatFrag's only have a single tree.
500 ///
501 std::vector<TreePatternNode*> Trees;
Jim Grosbach398abb42010-12-24 05:06:32 +0000502
Chris Lattner2cacec52010-03-15 06:00:16 +0000503 /// NamedNodes - This is all of the nodes that have names in the trees in this
504 /// pattern.
505 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach398abb42010-12-24 05:06:32 +0000506
Chris Lattner6cefb772008-01-05 22:25:12 +0000507 /// TheRecord - The actual TableGen record corresponding to this pattern.
508 ///
509 Record *TheRecord;
Jim Grosbach398abb42010-12-24 05:06:32 +0000510
Chris Lattner6cefb772008-01-05 22:25:12 +0000511 /// Args - This is a list of all of the arguments to this pattern (for
512 /// PatFrag patterns), which are the 'node' markers in this pattern.
513 std::vector<std::string> Args;
Jim Grosbach398abb42010-12-24 05:06:32 +0000514
Chris Lattner6cefb772008-01-05 22:25:12 +0000515 /// CDP - the top-level object coordinating this madness.
516 ///
Chris Lattnerfe718932008-01-06 01:10:31 +0000517 CodeGenDAGPatterns &CDP;
Chris Lattner6cefb772008-01-05 22:25:12 +0000518
519 /// isInputPattern - True if this is an input pattern, something to match.
520 /// False if this is an output pattern, something to emit.
521 bool isInputPattern;
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000522
523 /// hasError - True if the currently processed nodes have unresolvable types
524 /// or other non-fatal errors
525 bool HasError;
Chris Lattner6cefb772008-01-05 22:25:12 +0000526public:
Jim Grosbach398abb42010-12-24 05:06:32 +0000527
Chris Lattner6cefb772008-01-05 22:25:12 +0000528 /// TreePattern constructor - Parse the specified DagInits into the
529 /// current record.
David Greene05bce0b2011-07-29 22:43:06 +0000530 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000531 CodeGenDAGPatterns &ise);
David Greene05bce0b2011-07-29 22:43:06 +0000532 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000533 CodeGenDAGPatterns &ise);
Chris Lattner6cefb772008-01-05 22:25:12 +0000534 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerfe718932008-01-06 01:10:31 +0000535 CodeGenDAGPatterns &ise);
Jim Grosbach398abb42010-12-24 05:06:32 +0000536
Chris Lattner6cefb772008-01-05 22:25:12 +0000537 /// getTrees - Return the tree patterns which corresponds to this pattern.
538 ///
539 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
540 unsigned getNumTrees() const { return Trees.size(); }
541 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
542 TreePatternNode *getOnlyTree() const {
543 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
544 return Trees[0];
545 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000546
Chris Lattner2cacec52010-03-15 06:00:16 +0000547 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
548 if (NamedNodes.empty())
549 ComputeNamedNodes();
550 return NamedNodes;
551 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000552
Chris Lattner6cefb772008-01-05 22:25:12 +0000553 /// getRecord - Return the actual TableGen record corresponding to this
554 /// pattern.
555 ///
556 Record *getRecord() const { return TheRecord; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000557
Chris Lattner6cefb772008-01-05 22:25:12 +0000558 unsigned getNumArgs() const { return Args.size(); }
559 const std::string &getArgName(unsigned i) const {
560 assert(i < Args.size() && "Argument reference out of range!");
561 return Args[i];
562 }
563 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000564
Chris Lattnerfe718932008-01-06 01:10:31 +0000565 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000566
567 /// InlinePatternFragments - If this pattern refers to any pattern
568 /// fragments, inline them into place, giving us a pattern without any
569 /// PatFrag references.
570 void InlinePatternFragments() {
571 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
572 Trees[i] = Trees[i]->InlinePatternFragments(*this);
573 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000574
Chris Lattner6cefb772008-01-05 22:25:12 +0000575 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbachda4231f2009-03-26 16:17:51 +0000576 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000577 /// otherwise. Bail out if a type contradiction is found.
Chris Lattner2cacec52010-03-15 06:00:16 +0000578 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
579 *NamedTypes=0);
Jim Grosbach398abb42010-12-24 05:06:32 +0000580
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000581 /// error - If this is the first error in the current resolution step,
582 /// print it and set the error flag. Otherwise, continue silently.
583 void error(const std::string &Msg);
584 bool hasError() const {
585 return HasError;
586 }
587 void resetError() {
588 HasError = false;
589 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000590
Daniel Dunbar1a551802009-07-03 00:10:29 +0000591 void print(raw_ostream &OS) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000592 void dump() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000593
Chris Lattner6cefb772008-01-05 22:25:12 +0000594private:
David Greene05bce0b2011-07-29 22:43:06 +0000595 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattner2cacec52010-03-15 06:00:16 +0000596 void ComputeNamedNodes();
597 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner6cefb772008-01-05 22:25:12 +0000598};
599
Tom Stellard6d3d7652012-09-06 14:15:52 +0000600/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
601/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner6cefb772008-01-05 22:25:12 +0000602struct DAGDefaultOperand {
603 std::vector<TreePatternNode*> DefaultOps;
604};
605
606class DAGInstruction {
607 TreePattern *Pattern;
608 std::vector<Record*> Results;
609 std::vector<Record*> Operands;
610 std::vector<Record*> ImpResults;
Chris Lattner6cefb772008-01-05 22:25:12 +0000611 TreePatternNode *ResultPattern;
612public:
613 DAGInstruction(TreePattern *TP,
614 const std::vector<Record*> &results,
615 const std::vector<Record*> &operands,
Chris Lattner62bcec82010-04-20 06:28:43 +0000616 const std::vector<Record*> &impresults)
Jim Grosbach398abb42010-12-24 05:06:32 +0000617 : Pattern(TP), Results(results), Operands(operands),
Chris Lattner62bcec82010-04-20 06:28:43 +0000618 ImpResults(impresults), ResultPattern(0) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000619
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000620 TreePattern *getPattern() const { return Pattern; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000621 unsigned getNumResults() const { return Results.size(); }
622 unsigned getNumOperands() const { return Operands.size(); }
623 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner6cefb772008-01-05 22:25:12 +0000624 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000625
Chris Lattner6cefb772008-01-05 22:25:12 +0000626 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000627
Chris Lattner6cefb772008-01-05 22:25:12 +0000628 Record *getResult(unsigned RN) const {
629 assert(RN < Results.size());
630 return Results[RN];
631 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000632
Chris Lattner6cefb772008-01-05 22:25:12 +0000633 Record *getOperand(unsigned ON) const {
634 assert(ON < Operands.size());
635 return Operands[ON];
636 }
637
638 Record *getImpResult(unsigned RN) const {
639 assert(RN < ImpResults.size());
640 return ImpResults[RN];
641 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000642
Chris Lattner6cefb772008-01-05 22:25:12 +0000643 TreePatternNode *getResultPattern() const { return ResultPattern; }
644};
Jim Grosbach398abb42010-12-24 05:06:32 +0000645
Chris Lattnerfe718932008-01-06 01:10:31 +0000646/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner6cefb772008-01-05 22:25:12 +0000647/// processed to produce isel.
Chris Lattnerb49985a2010-02-18 06:47:49 +0000648class PatternToMatch {
649public:
David Greene05bce0b2011-07-29 22:43:06 +0000650 PatternToMatch(Record *srcrecord, ListInit *preds,
Chris Lattner6cefb772008-01-05 22:25:12 +0000651 TreePatternNode *src, TreePatternNode *dst,
652 const std::vector<Record*> &dstregs,
Chris Lattner117ccb72010-03-01 22:09:11 +0000653 unsigned complexity, unsigned uid)
Jim Grosbach997759a2010-12-07 23:05:49 +0000654 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
Chris Lattner117ccb72010-03-01 22:09:11 +0000655 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner6cefb772008-01-05 22:25:12 +0000656
Jim Grosbach997759a2010-12-07 23:05:49 +0000657 Record *SrcRecord; // Originating Record for the pattern.
David Greene05bce0b2011-07-29 22:43:06 +0000658 ListInit *Predicates; // Top level predicate conditions to match.
Chris Lattner6cefb772008-01-05 22:25:12 +0000659 TreePatternNode *SrcPattern; // Source pattern to match.
660 TreePatternNode *DstPattern; // Resulting pattern.
661 std::vector<Record*> Dstregs; // Physical register defs being matched.
662 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattner117ccb72010-03-01 22:09:11 +0000663 unsigned ID; // Unique ID for the record.
Chris Lattner6cefb772008-01-05 22:25:12 +0000664
Jim Grosbach997759a2010-12-07 23:05:49 +0000665 Record *getSrcRecord() const { return SrcRecord; }
David Greene05bce0b2011-07-29 22:43:06 +0000666 ListInit *getPredicates() const { return Predicates; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000667 TreePatternNode *getSrcPattern() const { return SrcPattern; }
668 TreePatternNode *getDstPattern() const { return DstPattern; }
669 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
670 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman22bb3112008-08-22 00:20:26 +0000671
672 std::string getPredicateCheck() const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000673
Chris Lattner48e86db2010-03-29 01:40:38 +0000674 /// Compute the complexity metric for the input pattern. This roughly
675 /// corresponds to the number of nodes that are covered.
676 unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner6cefb772008-01-05 22:25:12 +0000677};
678
Chris Lattnerfe718932008-01-06 01:10:31 +0000679class CodeGenDAGPatterns {
Chris Lattner6cefb772008-01-05 22:25:12 +0000680 RecordKeeper &Records;
681 CodeGenTarget Target;
682 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesen49de9822009-02-05 01:49:45 +0000683 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Jim Grosbach398abb42010-12-24 05:06:32 +0000684
Sean Silva90fee072012-09-19 01:47:00 +0000685 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
686 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms;
687 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
688 std::map<Record*, TreePattern*, LessRecordByID> PatternFragments;
689 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
690 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach398abb42010-12-24 05:06:32 +0000691
Chris Lattner6cefb772008-01-05 22:25:12 +0000692 // Specific SDNode definitions:
693 Record *intrinsic_void_sdnode;
694 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach398abb42010-12-24 05:06:32 +0000695
Chris Lattner6cefb772008-01-05 22:25:12 +0000696 /// PatternsToMatch - All of the things we are matching on the DAG. The first
697 /// value is the pattern to match, the second pattern is the result to
698 /// emit.
699 std::vector<PatternToMatch> PatternsToMatch;
700public:
Jim Grosbach398abb42010-12-24 05:06:32 +0000701 CodeGenDAGPatterns(RecordKeeper &R);
Chris Lattnerfe718932008-01-06 01:10:31 +0000702 ~CodeGenDAGPatterns();
Jim Grosbach398abb42010-12-24 05:06:32 +0000703
Dan Gohmanee4fa192008-04-03 00:02:49 +0000704 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner6cefb772008-01-05 22:25:12 +0000705 const CodeGenTarget &getTargetInfo() const { return Target; }
Jim Grosbach398abb42010-12-24 05:06:32 +0000706
Chris Lattner6cefb772008-01-05 22:25:12 +0000707 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach398abb42010-12-24 05:06:32 +0000708
Chris Lattner6cefb772008-01-05 22:25:12 +0000709 const SDNodeInfo &getSDNodeInfo(Record *R) const {
710 assert(SDNodes.count(R) && "Unknown node!");
711 return SDNodes.find(R)->second;
712 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000713
Chris Lattner443e3f92008-01-05 22:54:53 +0000714 // Node transformation lookups.
715 typedef std::pair<Record*, std::string> NodeXForm;
716 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000717 assert(SDNodeXForms.count(R) && "Invalid transform!");
718 return SDNodeXForms.find(R)->second;
719 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000720
Sean Silva90fee072012-09-19 01:47:00 +0000721 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000722 nx_iterator;
Chris Lattner443e3f92008-01-05 22:54:53 +0000723 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
724 nx_iterator nx_end() const { return SDNodeXForms.end(); }
725
Jim Grosbach398abb42010-12-24 05:06:32 +0000726
Chris Lattner6cefb772008-01-05 22:25:12 +0000727 const ComplexPattern &getComplexPattern(Record *R) const {
728 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
729 return ComplexPatterns.find(R)->second;
730 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000731
Chris Lattner6cefb772008-01-05 22:25:12 +0000732 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
733 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
734 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesen49de9822009-02-05 01:49:45 +0000735 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
736 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topper655b8de2012-02-05 07:21:30 +0000737 llvm_unreachable("Unknown intrinsic!");
Chris Lattner6cefb772008-01-05 22:25:12 +0000738 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000739
Chris Lattner6cefb772008-01-05 22:25:12 +0000740 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesen49de9822009-02-05 01:49:45 +0000741 if (IID-1 < Intrinsics.size())
742 return Intrinsics[IID-1];
743 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
744 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topper655b8de2012-02-05 07:21:30 +0000745 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner6cefb772008-01-05 22:25:12 +0000746 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000747
Chris Lattner6cefb772008-01-05 22:25:12 +0000748 unsigned getIntrinsicID(Record *R) const {
749 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
750 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesen49de9822009-02-05 01:49:45 +0000751 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
752 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topper655b8de2012-02-05 07:21:30 +0000753 llvm_unreachable("Unknown intrinsic!");
Chris Lattner6cefb772008-01-05 22:25:12 +0000754 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000755
Chris Lattnerb49985a2010-02-18 06:47:49 +0000756 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner6cefb772008-01-05 22:25:12 +0000757 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
758 return DefaultOperands.find(R)->second;
759 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000760
Chris Lattner6cefb772008-01-05 22:25:12 +0000761 // Pattern Fragment information.
762 TreePattern *getPatternFragment(Record *R) const {
763 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
764 return PatternFragments.find(R)->second;
765 }
Chris Lattnerd7349192010-03-19 21:37:09 +0000766 TreePattern *getPatternFragmentIfRead(Record *R) const {
767 if (!PatternFragments.count(R)) return 0;
768 return PatternFragments.find(R)->second;
769 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000770
Sean Silva90fee072012-09-19 01:47:00 +0000771 typedef std::map<Record*, TreePattern*, LessRecordByID>::const_iterator
Benjamin Kramer5b9e7ef2009-08-23 10:39:21 +0000772 pf_iterator;
Chris Lattner6cefb772008-01-05 22:25:12 +0000773 pf_iterator pf_begin() const { return PatternFragments.begin(); }
774 pf_iterator pf_end() const { return PatternFragments.end(); }
775
776 // Patterns to match information.
Chris Lattner60d81392008-01-05 22:30:17 +0000777 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
778 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
779 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Jim Grosbach398abb42010-12-24 05:06:32 +0000780
781
782
Chris Lattner6cefb772008-01-05 22:25:12 +0000783 const DAGInstruction &getInstruction(Record *R) const {
784 assert(Instructions.count(R) && "Unknown instruction!");
785 return Instructions.find(R)->second;
786 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000787
Chris Lattner6cefb772008-01-05 22:25:12 +0000788 Record *get_intrinsic_void_sdnode() const {
789 return intrinsic_void_sdnode;
790 }
791 Record *get_intrinsic_w_chain_sdnode() const {
792 return intrinsic_w_chain_sdnode;
793 }
794 Record *get_intrinsic_wo_chain_sdnode() const {
795 return intrinsic_wo_chain_sdnode;
796 }
Jim Grosbach398abb42010-12-24 05:06:32 +0000797
Jakob Stoklund Olesen11ee5082009-10-15 18:50:03 +0000798 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
799
Chris Lattner6cefb772008-01-05 22:25:12 +0000800private:
801 void ParseNodeInfo();
Chris Lattner443e3f92008-01-05 22:54:53 +0000802 void ParseNodeTransforms();
Chris Lattner6cefb772008-01-05 22:25:12 +0000803 void ParseComplexPatterns();
Chris Lattnerdc32f982008-01-05 22:43:57 +0000804 void ParsePatternFragments();
Chris Lattner6cefb772008-01-05 22:25:12 +0000805 void ParseDefaultOperands();
806 void ParseInstructions();
807 void ParsePatterns();
Dan Gohmanee4fa192008-04-03 00:02:49 +0000808 void InferInstructionFlags();
Chris Lattner6cefb772008-01-05 22:25:12 +0000809 void GenerateVariants();
Jakob Stoklund Olesen325907d2012-08-28 03:26:49 +0000810 void VerifyInstructionFlags();
Jim Grosbach398abb42010-12-24 05:06:32 +0000811
Joerg Sonnenberger61131ab2012-10-25 20:33:17 +0000812 void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner6cefb772008-01-05 22:25:12 +0000813 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
814 std::map<std::string,
815 TreePatternNode*> &InstInputs,
816 std::map<std::string,
817 TreePatternNode*> &InstResults,
Chris Lattner6cefb772008-01-05 22:25:12 +0000818 std::vector<Record*> &InstImpResults);
819};
820} // end namespace llvm
821
822#endif