blob: 59b3dbec0416c45844e4c4d7f687ba86367b155d [file] [log] [blame]
Chris Lattnerab3242f2008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
Chris Lattner8cab0212008-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 Lattnerab3242f2008-01-06 01:10:31 +000010// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner8cab0212008-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 Lattner8cab0212008-01-05 22:25:12 +000018#include "CodeGenIntrinsics.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000019#include "CodeGenTarget.h"
Chris Lattnercabe0372010-03-15 06:00:16 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000022#include "llvm/Support/ErrorHandling.h"
Chris Lattner1802b172010-03-19 01:07:44 +000023#include <algorithm>
Chris Lattner1802b172010-03-19 01:07:44 +000024#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000025#include <set>
26#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000027
28namespace llvm {
29 class Record;
David Greene9908c172011-07-13 22:25:51 +000030 class Init;
Chris Lattner8cab0212008-01-05 22:25:12 +000031 class ListInit;
32 class DagInit;
33 class SDNodeInfo;
34 class TreePattern;
35 class TreePatternNode;
Chris Lattnerab3242f2008-01-06 01:10:31 +000036 class CodeGenDAGPatterns;
Chris Lattner8cab0212008-01-05 22:25:12 +000037 class ComplexPattern;
38
Owen Anderson53aa7a92009-08-10 22:56:29 +000039/// EEVT::DAGISelGenValueType - These are some extended forms of
Owen Anderson9f944592009-08-11 20:47:22 +000040/// MVT::SimpleValueType that we use as lattice values during type inference.
Bob Wilson57b946c2009-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 Anderson53aa7a92009-08-10 22:56:29 +000044namespace EEVT {
Chris Lattnercabe0372010-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 Lattner6d765eb2010-03-19 17:41:26 +000058 SmallVector<MVT::SimpleValueType, 4> TypeVec;
Chris Lattnercabe0372010-03-15 06:00:16 +000059 public:
60 TypeSet() {}
61 TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
Jakob Stoklund Olesen13d4a072013-03-17 17:26:09 +000062 TypeSet(ArrayRef<MVT::SimpleValueType> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +000063
Chris Lattnercabe0372010-03-15 06:00:16 +000064 bool isCompletelyUnknown() const { return TypeVec.empty(); }
Jim Grosbach50986b52010-12-24 05:06:32 +000065
Chris Lattnercabe0372010-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 Grosbach50986b52010-12-24 05:06:32 +000072
Chris Lattnercabe0372010-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 Grosbach50986b52010-12-24 05:06:32 +000077
Chris Lattnercabe0372010-03-15 06:00:16 +000078 bool isDynamicallyResolved() const {
79 return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
80 }
Jim Grosbach50986b52010-12-24 05:06:32 +000081
Chris Lattnercabe0372010-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 Grosbach50986b52010-12-24 05:06:32 +000086
Chris Lattnerfdc20712010-03-18 23:15:10 +000087 bool isVoid() const {
88 return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
89 }
Jim Grosbach50986b52010-12-24 05:06:32 +000090
Chris Lattnercabe0372010-03-15 06:00:16 +000091 /// hasIntegerTypes - Return true if this TypeSet contains any integer value
92 /// types.
93 bool hasIntegerTypes() const;
Jim Grosbach50986b52010-12-24 05:06:32 +000094
Chris Lattnercabe0372010-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 Grosbach50986b52010-12-24 05:06:32 +000098
Craig Topper74169dc2014-01-28 04:49:01 +000099 /// hasScalarTypes - Return true if this TypeSet contains a scalar value
100 /// type.
101 bool hasScalarTypes() const;
102
Chris Lattnercabe0372010-03-15 06:00:16 +0000103 /// hasVectorTypes - Return true if this TypeSet contains a vector value
104 /// type.
105 bool hasVectorTypes() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000106
Chris Lattnercabe0372010-03-15 06:00:16 +0000107 /// getName() - Return this TypeSet as a string.
108 std::string getName() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000109
Chris Lattnercabe0372010-03-15 06:00:16 +0000110 /// MergeInTypeInfo - This merges in type information from the specified
111 /// argument. If 'this' changes, it returns true. If the two types are
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000112 /// contradictory (e.g. merge f32 into i32) then this flags an error.
Chris Lattnercabe0372010-03-15 06:00:16 +0000113 bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
Duncan Sands13237ac2008-06-06 12:08:01 +0000114
Chris Lattnercabe0372010-03-15 06:00:16 +0000115 bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
116 return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
117 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000118
Chris Lattnercabe0372010-03-15 06:00:16 +0000119 /// Force this type list to only contain integer types.
120 bool EnforceInteger(TreePattern &TP);
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000121
Chris Lattnercabe0372010-03-15 06:00:16 +0000122 /// Force this type list to only contain floating point types.
123 bool EnforceFloatingPoint(TreePattern &TP);
124
125 /// EnforceScalar - Remove all vector types from this type list.
126 bool EnforceScalar(TreePattern &TP);
127
128 /// EnforceVector - Remove all non-vector types from this type list.
129 bool EnforceVector(TreePattern &TP);
130
131 /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
132 /// this an other based on this information.
133 bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000134
Chris Lattnercabe0372010-03-15 06:00:16 +0000135 /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
136 /// whose element is VT.
Chris Lattner57ebf632010-03-24 00:01:16 +0000137 bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000138
David Greene127fd1d2011-01-24 20:53:18 +0000139 /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to
140 /// be a vector type VT.
141 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
142
Chris Lattnercabe0372010-03-15 06:00:16 +0000143 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
144 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000145
Chris Lattnerbe6b17f2010-03-19 04:54:36 +0000146 private:
147 /// FillWithPossibleTypes - Set to all legal types and return true, only
Chris Lattner6d765eb2010-03-19 17:41:26 +0000148 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
149 /// that pass the predicate are added.
150 bool FillWithPossibleTypes(TreePattern &TP,
Craig Topperada08572014-04-16 04:21:27 +0000151 bool (*Pred)(MVT::SimpleValueType) = nullptr,
152 const char *PredicateName = nullptr);
Chris Lattnercabe0372010-03-15 06:00:16 +0000153 };
Chris Lattner8cab0212008-01-05 22:25:12 +0000154}
155
Scott Michel94420742008-03-05 17:49:05 +0000156/// Set type used to track multiply used variables in patterns
157typedef std::set<std::string> MultipleUseVarSet;
158
Chris Lattner8cab0212008-01-05 22:25:12 +0000159/// SDTypeConstraint - This is a discriminated union of constraints,
160/// corresponding to the SDTypeConstraint tablegen class in Target.td.
161struct SDTypeConstraint {
162 SDTypeConstraint(Record *R);
Jim Grosbach50986b52010-12-24 05:06:32 +0000163
Chris Lattner8cab0212008-01-05 22:25:12 +0000164 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000165 enum {
166 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000167 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
168 SDTCisSubVecOfVec
Chris Lattner8cab0212008-01-05 22:25:12 +0000169 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000170
Chris Lattner8cab0212008-01-05 22:25:12 +0000171 union { // The discriminated union.
172 struct {
Chris Lattnercabe0372010-03-15 06:00:16 +0000173 MVT::SimpleValueType VT;
Chris Lattner8cab0212008-01-05 22:25:12 +0000174 } SDTCisVT_Info;
175 struct {
176 unsigned OtherOperandNum;
177 } SDTCisSameAs_Info;
178 struct {
179 unsigned OtherOperandNum;
180 } SDTCisVTSmallerThanOp_Info;
181 struct {
182 unsigned BigOperandNum;
183 } SDTCisOpSmallerThanOp_Info;
184 struct {
185 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000186 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000187 struct {
188 unsigned OtherOperandNum;
189 } SDTCisSubVecOfVec_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000190 } x;
191
192 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
193 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000194 /// change, false otherwise. If a type contradiction is found, an error
195 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000196 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
197 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000198};
199
200/// SDNodeInfo - One of these records is created for each SDNode instance in
201/// the target .td file. This represents the various dag nodes we will be
202/// processing.
203class SDNodeInfo {
204 Record *Def;
205 std::string EnumName;
206 std::string SDClassName;
207 unsigned Properties;
208 unsigned NumResults;
209 int NumOperands;
210 std::vector<SDTypeConstraint> TypeConstraints;
211public:
212 SDNodeInfo(Record *R); // Parse the specified record.
Jim Grosbach50986b52010-12-24 05:06:32 +0000213
Chris Lattner8cab0212008-01-05 22:25:12 +0000214 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000215
Chris Lattner135091b2010-03-28 08:48:47 +0000216 /// getNumOperands - This is the number of operands required or -1 if
217 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000218 int getNumOperands() const { return NumOperands; }
219 Record *getRecord() const { return Def; }
220 const std::string &getEnumName() const { return EnumName; }
221 const std::string &getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000222
Chris Lattner8cab0212008-01-05 22:25:12 +0000223 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
224 return TypeConstraints;
225 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000226
Chris Lattner99e53b32010-02-28 00:22:30 +0000227 /// getKnownType - If the type constraints on this node imply a fixed type
228 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000229 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000230 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000231
Chris Lattner8cab0212008-01-05 22:25:12 +0000232 /// hasProperty - Return true if this node has the specified property.
233 ///
234 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
235
236 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
237 /// constraints for this node to the operands of the node. This returns
238 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000239 /// found, an error is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000240 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
241 bool MadeChange = false;
242 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
243 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
244 return MadeChange;
245 }
246};
Chris Lattner514e2922011-04-17 21:38:24 +0000247
248/// TreePredicateFn - This is an abstraction that represents the predicates on
249/// a PatFrag node. This is a simple one-word wrapper around a pointer to
250/// provide nice accessors.
251class TreePredicateFn {
252 /// PatFragRec - This is the TreePattern for the PatFrag that we
253 /// originally came from.
254 TreePattern *PatFragRec;
255public:
256 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000257 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000258
259
260 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
261
262 /// isAlwaysTrue - Return true if this is a noop predicate.
263 bool isAlwaysTrue() const;
264
Chris Lattner07add492011-04-18 06:22:33 +0000265 bool isImmediatePattern() const { return !getImmCode().empty(); }
266
267 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
268 /// this is an immediate predicate. It is an error to call this on a
269 /// non-immediate pattern.
270 std::string getImmediatePredicateCode() const {
271 std::string Result = getImmCode();
272 assert(!Result.empty() && "Isn't an immediate pattern!");
273 return Result;
274 }
275
Chris Lattner514e2922011-04-17 21:38:24 +0000276
277 bool operator==(const TreePredicateFn &RHS) const {
278 return PatFragRec == RHS.PatFragRec;
279 }
280
281 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
282
283 /// Return the name to use in the generated code to reference this, this is
284 /// "Predicate_foo" if from a pattern fragment "foo".
285 std::string getFnName() const;
286
287 /// getCodeToRunOnSDNode - Return the code for the function body that
288 /// evaluates this predicate. The argument is expected to be in "Node",
289 /// not N. This handles casting and conversion to a concrete node type as
290 /// appropriate.
291 std::string getCodeToRunOnSDNode() const;
292
293private:
294 std::string getPredCode() const;
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000295 std::string getImmCode() const;
Chris Lattner514e2922011-04-17 21:38:24 +0000296};
297
Chris Lattner8cab0212008-01-05 22:25:12 +0000298
299/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
300/// patterns), and as such should be ref counted. We currently just leak all
301/// TreePatternNode objects!
302class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000303 /// The type of each node result. Before and during type inference, each
304 /// result may be a set of possible types. After (successful) type inference,
305 /// each is a single concrete type.
306 SmallVector<EEVT::TypeSet, 1> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000307
Chris Lattner8cab0212008-01-05 22:25:12 +0000308 /// Operator - The Record for the operator if this is an interior node (not
309 /// a leaf).
310 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000311
Chris Lattner8cab0212008-01-05 22:25:12 +0000312 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
313 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000314 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000315
Chris Lattner8cab0212008-01-05 22:25:12 +0000316 /// Name - The name given to this node with the :$foo notation.
317 ///
318 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000319
Dan Gohman6e979022008-10-15 06:17:21 +0000320 /// PredicateFns - The predicate functions to execute on this node to check
321 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000322 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000323
Chris Lattner8cab0212008-01-05 22:25:12 +0000324 /// TransformFn - The transformation function to execute on this node before
325 /// it can be substituted into the resulting instruction on a pattern match.
326 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000327
Chris Lattner8cab0212008-01-05 22:25:12 +0000328 std::vector<TreePatternNode*> Children;
329public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000330 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000331 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000332 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000333 Types.resize(NumResults);
334 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000335 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000336 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000337 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000338 }
339 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000340
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000341 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000342 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000343 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000344
Craig Topperada08572014-04-16 04:21:27 +0000345 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000346
Chris Lattnercabe0372010-03-15 06:00:16 +0000347 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000348 unsigned getNumTypes() const { return Types.size(); }
349 MVT::SimpleValueType getType(unsigned ResNo) const {
350 return Types[ResNo].getConcrete();
351 }
352 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
353 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
354 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
355 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000356
Chris Lattnerf1447252010-03-19 21:37:09 +0000357 bool hasTypeSet(unsigned ResNo) const {
358 return Types[ResNo].isConcrete();
359 }
360 bool isTypeCompletelyUnknown(unsigned ResNo) const {
361 return Types[ResNo].isCompletelyUnknown();
362 }
363 bool isTypeDynamicallyResolved(unsigned ResNo) const {
364 return Types[ResNo].isDynamicallyResolved();
365 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000366
David Greeneaf8ee2c2011-07-29 22:43:06 +0000367 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000368 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000369
Chris Lattner8cab0212008-01-05 22:25:12 +0000370 unsigned getNumChildren() const { return Children.size(); }
371 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
372 void setChild(unsigned i, TreePatternNode *N) {
373 Children[i] = N;
374 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000375
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000376 /// hasChild - Return true if N is any of our children.
377 bool hasChild(const TreePatternNode *N) const {
378 for (unsigned i = 0, e = Children.size(); i != e; ++i)
379 if (Children[i] == N) return true;
380 return false;
381 }
Chris Lattner89c65662008-01-06 05:36:50 +0000382
Chris Lattner514e2922011-04-17 21:38:24 +0000383 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
384
385 const std::vector<TreePredicateFn> &getPredicateFns() const {
386 return PredicateFns;
387 }
Dan Gohman6e979022008-10-15 06:17:21 +0000388 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000389 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000390 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
391 PredicateFns = Fns;
392 }
Chris Lattner514e2922011-04-17 21:38:24 +0000393 void addPredicateFn(const TreePredicateFn &Fn) {
394 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
Dan Gohman6e979022008-10-15 06:17:21 +0000395 if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
396 PredicateFns.end())
397 PredicateFns.push_back(Fn);
398 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000399
400 Record *getTransformFn() const { return TransformFn; }
401 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000402
Chris Lattner89c65662008-01-06 05:36:50 +0000403 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
404 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
405 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000406
Chris Lattner53c39ba2010-02-14 22:22:58 +0000407 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
408 /// return the ComplexPattern information, otherwise return null.
409 const ComplexPattern *
410 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
411
412 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000413 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000414
Chris Lattner53c39ba2010-02-14 22:22:58 +0000415 /// TreeHasProperty - Return true if any node in this tree has the specified
416 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000417 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000418
Evan Cheng49bad4c2008-06-16 20:29:38 +0000419 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
420 /// marked isCommutative.
421 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000422
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000423 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000424 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000425
Chris Lattner8cab0212008-01-05 22:25:12 +0000426public: // Higher level manipulation routines.
427
428 /// clone - Return a new copy of this tree.
429 ///
430 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000431
432 /// RemoveAllTypes - Recursively strip all the types of this tree.
433 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000434
Chris Lattner8cab0212008-01-05 22:25:12 +0000435 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
436 /// the specified node. For this comparison, all of the state of the node
437 /// is considered, except for the assigned name. Nodes with differing names
438 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000439 bool isIsomorphicTo(const TreePatternNode *N,
440 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000441
Chris Lattner8cab0212008-01-05 22:25:12 +0000442 /// SubstituteFormalArguments - Replace the formal arguments in this tree
443 /// with actual values specified by ArgMap.
444 void SubstituteFormalArguments(std::map<std::string,
445 TreePatternNode*> &ArgMap);
446
447 /// InlinePatternFragments - If this pattern refers to any pattern
448 /// fragments, inline them into place, giving us a pattern without any
449 /// PatFrag references.
450 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000451
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000452 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000453 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000454 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000455 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000456
Chris Lattner8cab0212008-01-05 22:25:12 +0000457 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000458 /// information. If N already contains a conflicting type, then flag an
459 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000460 ///
Chris Lattnerf1447252010-03-19 21:37:09 +0000461 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
462 TreePattern &TP) {
463 return Types[ResNo].MergeInTypeInfo(InTy, TP);
Chris Lattnercabe0372010-03-15 06:00:16 +0000464 }
465
Chris Lattnerf1447252010-03-19 21:37:09 +0000466 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
467 TreePattern &TP) {
468 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000469 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000470
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000471 // Update node type with types inferred from an instruction operand or result
472 // def from the ins/outs lists.
473 // Return true if the type changed.
474 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
475
Chris Lattner8cab0212008-01-05 22:25:12 +0000476 /// ContainsUnresolvedType - Return true if this tree contains any
477 /// unresolved types.
478 bool ContainsUnresolvedType() const {
Chris Lattnerf1447252010-03-19 21:37:09 +0000479 for (unsigned i = 0, e = Types.size(); i != e; ++i)
480 if (!Types[i].isConcrete()) return true;
Jim Grosbach50986b52010-12-24 05:06:32 +0000481
Chris Lattner8cab0212008-01-05 22:25:12 +0000482 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
483 if (getChild(i)->ContainsUnresolvedType()) return true;
484 return false;
485 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000486
Chris Lattner8cab0212008-01-05 22:25:12 +0000487 /// canPatternMatch - If it is impossible for this pattern to match on this
488 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000489 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000490};
491
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000492inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
493 TPN.print(OS);
494 return OS;
495}
Jim Grosbach50986b52010-12-24 05:06:32 +0000496
Chris Lattner8cab0212008-01-05 22:25:12 +0000497
498/// TreePattern - Represent a pattern, used for instructions, pattern
499/// fragments, etc.
500///
501class TreePattern {
502 /// Trees - The list of pattern trees which corresponds to this pattern.
503 /// Note that PatFrag's only have a single tree.
504 ///
505 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000506
Chris Lattnercabe0372010-03-15 06:00:16 +0000507 /// NamedNodes - This is all of the nodes that have names in the trees in this
508 /// pattern.
509 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000510
Chris Lattner8cab0212008-01-05 22:25:12 +0000511 /// TheRecord - The actual TableGen record corresponding to this pattern.
512 ///
513 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000514
Chris Lattner8cab0212008-01-05 22:25:12 +0000515 /// Args - This is a list of all of the arguments to this pattern (for
516 /// PatFrag patterns), which are the 'node' markers in this pattern.
517 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000518
Chris Lattner8cab0212008-01-05 22:25:12 +0000519 /// CDP - the top-level object coordinating this madness.
520 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000521 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000522
523 /// isInputPattern - True if this is an input pattern, something to match.
524 /// False if this is an output pattern, something to emit.
525 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000526
527 /// hasError - True if the currently processed nodes have unresolvable types
528 /// or other non-fatal errors
529 bool HasError;
Chris Lattner8cab0212008-01-05 22:25:12 +0000530public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000531
Chris Lattner8cab0212008-01-05 22:25:12 +0000532 /// TreePattern constructor - Parse the specified DagInits into the
533 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000534 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000535 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000536 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000537 CodeGenDAGPatterns &ise);
Chris Lattner8cab0212008-01-05 22:25:12 +0000538 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000539 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000540
Chris Lattner8cab0212008-01-05 22:25:12 +0000541 /// getTrees - Return the tree patterns which corresponds to this pattern.
542 ///
543 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
544 unsigned getNumTrees() const { return Trees.size(); }
545 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
546 TreePatternNode *getOnlyTree() const {
547 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
548 return Trees[0];
549 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000550
Chris Lattnercabe0372010-03-15 06:00:16 +0000551 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
552 if (NamedNodes.empty())
553 ComputeNamedNodes();
554 return NamedNodes;
555 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000556
Chris Lattner8cab0212008-01-05 22:25:12 +0000557 /// getRecord - Return the actual TableGen record corresponding to this
558 /// pattern.
559 ///
560 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000561
Chris Lattner8cab0212008-01-05 22:25:12 +0000562 unsigned getNumArgs() const { return Args.size(); }
563 const std::string &getArgName(unsigned i) const {
564 assert(i < Args.size() && "Argument reference out of range!");
565 return Args[i];
566 }
567 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000568
Chris Lattnerab3242f2008-01-06 01:10:31 +0000569 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000570
571 /// InlinePatternFragments - If this pattern refers to any pattern
572 /// fragments, inline them into place, giving us a pattern without any
573 /// PatFrag references.
574 void InlinePatternFragments() {
575 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
576 Trees[i] = Trees[i]->InlinePatternFragments(*this);
577 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000578
Chris Lattner8cab0212008-01-05 22:25:12 +0000579 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000580 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000581 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000582 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000583 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000584
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000585 /// error - If this is the first error in the current resolution step,
586 /// print it and set the error flag. Otherwise, continue silently.
587 void error(const std::string &Msg);
588 bool hasError() const {
589 return HasError;
590 }
591 void resetError() {
592 HasError = false;
593 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000594
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000595 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000596 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000597
Chris Lattner8cab0212008-01-05 22:25:12 +0000598private:
David Greeneaf8ee2c2011-07-29 22:43:06 +0000599 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000600 void ComputeNamedNodes();
601 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000602};
603
Tom Stellardb7246a72012-09-06 14:15:52 +0000604/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
605/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000606struct DAGDefaultOperand {
607 std::vector<TreePatternNode*> DefaultOps;
608};
609
610class DAGInstruction {
611 TreePattern *Pattern;
612 std::vector<Record*> Results;
613 std::vector<Record*> Operands;
614 std::vector<Record*> ImpResults;
Chris Lattner8cab0212008-01-05 22:25:12 +0000615 TreePatternNode *ResultPattern;
616public:
617 DAGInstruction(TreePattern *TP,
618 const std::vector<Record*> &results,
619 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000620 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000621 : Pattern(TP), Results(results), Operands(operands),
Craig Topperada08572014-04-16 04:21:27 +0000622 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000623
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000624 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000625 unsigned getNumResults() const { return Results.size(); }
626 unsigned getNumOperands() const { return Operands.size(); }
627 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000628 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000629
Chris Lattner8cab0212008-01-05 22:25:12 +0000630 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000631
Chris Lattner8cab0212008-01-05 22:25:12 +0000632 Record *getResult(unsigned RN) const {
633 assert(RN < Results.size());
634 return Results[RN];
635 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000636
Chris Lattner8cab0212008-01-05 22:25:12 +0000637 Record *getOperand(unsigned ON) const {
638 assert(ON < Operands.size());
639 return Operands[ON];
640 }
641
642 Record *getImpResult(unsigned RN) const {
643 assert(RN < ImpResults.size());
644 return ImpResults[RN];
645 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000646
Chris Lattner8cab0212008-01-05 22:25:12 +0000647 TreePatternNode *getResultPattern() const { return ResultPattern; }
648};
Jim Grosbach50986b52010-12-24 05:06:32 +0000649
Chris Lattnerab3242f2008-01-06 01:10:31 +0000650/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000651/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000652class PatternToMatch {
653public:
David Greeneaf8ee2c2011-07-29 22:43:06 +0000654 PatternToMatch(Record *srcrecord, ListInit *preds,
Chris Lattner8cab0212008-01-05 22:25:12 +0000655 TreePatternNode *src, TreePatternNode *dst,
656 const std::vector<Record*> &dstregs,
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000657 unsigned complexity, unsigned uid)
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000658 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000659 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000660
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000661 Record *SrcRecord; // Originating Record for the pattern.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000662 ListInit *Predicates; // Top level predicate conditions to match.
Chris Lattner8cab0212008-01-05 22:25:12 +0000663 TreePatternNode *SrcPattern; // Source pattern to match.
664 TreePatternNode *DstPattern; // Resulting pattern.
665 std::vector<Record*> Dstregs; // Physical register defs being matched.
666 unsigned AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000667 unsigned ID; // Unique ID for the record.
Chris Lattner8cab0212008-01-05 22:25:12 +0000668
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000669 Record *getSrcRecord() const { return SrcRecord; }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000670 ListInit *getPredicates() const { return Predicates; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000671 TreePatternNode *getSrcPattern() const { return SrcPattern; }
672 TreePatternNode *getDstPattern() const { return DstPattern; }
673 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
674 unsigned getAddedComplexity() const { return AddedComplexity; }
Dan Gohman49e19e92008-08-22 00:20:26 +0000675
676 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000677
Chris Lattner05925fe2010-03-29 01:40:38 +0000678 /// Compute the complexity metric for the input pattern. This roughly
679 /// corresponds to the number of nodes that are covered.
680 unsigned getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000681};
682
Chris Lattnerab3242f2008-01-06 01:10:31 +0000683class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +0000684 RecordKeeper &Records;
685 CodeGenTarget Target;
686 std::vector<CodeGenIntrinsic> Intrinsics;
Dale Johannesenb842d522009-02-05 01:49:45 +0000687 std::vector<CodeGenIntrinsic> TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +0000688
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000689 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
690 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms;
691 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
692 std::map<Record*, TreePattern*, LessRecordByID> PatternFragments;
693 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
694 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +0000695
Chris Lattner8cab0212008-01-05 22:25:12 +0000696 // Specific SDNode definitions:
697 Record *intrinsic_void_sdnode;
698 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +0000699
Chris Lattner8cab0212008-01-05 22:25:12 +0000700 /// PatternsToMatch - All of the things we are matching on the DAG. The first
701 /// value is the pattern to match, the second pattern is the result to
702 /// emit.
703 std::vector<PatternToMatch> PatternsToMatch;
704public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000705 CodeGenDAGPatterns(RecordKeeper &R);
Chris Lattnerab3242f2008-01-06 01:10:31 +0000706 ~CodeGenDAGPatterns();
Jim Grosbach50986b52010-12-24 05:06:32 +0000707
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000708 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000709 const CodeGenTarget &getTargetInfo() const { return Target; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000710
Chris Lattner8cab0212008-01-05 22:25:12 +0000711 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000712
Chris Lattner8cab0212008-01-05 22:25:12 +0000713 const SDNodeInfo &getSDNodeInfo(Record *R) const {
714 assert(SDNodes.count(R) && "Unknown node!");
715 return SDNodes.find(R)->second;
716 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000717
Chris Lattnercc43e792008-01-05 22:54:53 +0000718 // Node transformation lookups.
719 typedef std::pair<Record*, std::string> NodeXForm;
720 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000721 assert(SDNodeXForms.count(R) && "Invalid transform!");
722 return SDNodeXForms.find(R)->second;
723 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000724
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000725 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +0000726 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +0000727 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
728 nx_iterator nx_end() const { return SDNodeXForms.end(); }
729
Jim Grosbach50986b52010-12-24 05:06:32 +0000730
Chris Lattner8cab0212008-01-05 22:25:12 +0000731 const ComplexPattern &getComplexPattern(Record *R) const {
732 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
733 return ComplexPatterns.find(R)->second;
734 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000735
Chris Lattner8cab0212008-01-05 22:25:12 +0000736 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
737 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
738 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +0000739 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
740 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +0000741 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000742 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000743
Chris Lattner8cab0212008-01-05 22:25:12 +0000744 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +0000745 if (IID-1 < Intrinsics.size())
746 return Intrinsics[IID-1];
747 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
748 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +0000749 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000750 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000751
Chris Lattner8cab0212008-01-05 22:25:12 +0000752 unsigned getIntrinsicID(Record *R) const {
753 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
754 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +0000755 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
756 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +0000757 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000758 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000759
Chris Lattner7ed81692010-02-18 06:47:49 +0000760 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000761 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
762 return DefaultOperands.find(R)->second;
763 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000764
Chris Lattner8cab0212008-01-05 22:25:12 +0000765 // Pattern Fragment information.
766 TreePattern *getPatternFragment(Record *R) const {
767 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
768 return PatternFragments.find(R)->second;
769 }
Chris Lattnerf1447252010-03-19 21:37:09 +0000770 TreePattern *getPatternFragmentIfRead(Record *R) const {
Craig Topperada08572014-04-16 04:21:27 +0000771 if (!PatternFragments.count(R)) return nullptr;
Chris Lattnerf1447252010-03-19 21:37:09 +0000772 return PatternFragments.find(R)->second;
773 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000774
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000775 typedef std::map<Record*, TreePattern*, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +0000776 pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +0000777 pf_iterator pf_begin() const { return PatternFragments.begin(); }
778 pf_iterator pf_end() const { return PatternFragments.end(); }
779
780 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +0000781 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
782 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
783 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000784
Ahmed Bougacha14107512013-10-28 18:07:21 +0000785 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
786 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
787 const DAGInstruction &parseInstructionPattern(
788 CodeGenInstruction &CGI, ListInit *Pattern,
789 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +0000790
Chris Lattner8cab0212008-01-05 22:25:12 +0000791 const DAGInstruction &getInstruction(Record *R) const {
792 assert(Instructions.count(R) && "Unknown instruction!");
793 return Instructions.find(R)->second;
794 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000795
Chris Lattner8cab0212008-01-05 22:25:12 +0000796 Record *get_intrinsic_void_sdnode() const {
797 return intrinsic_void_sdnode;
798 }
799 Record *get_intrinsic_w_chain_sdnode() const {
800 return intrinsic_w_chain_sdnode;
801 }
802 Record *get_intrinsic_wo_chain_sdnode() const {
803 return intrinsic_wo_chain_sdnode;
804 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000805
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +0000806 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
807
Chris Lattner8cab0212008-01-05 22:25:12 +0000808private:
809 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +0000810 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +0000811 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +0000812 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +0000813 void ParseDefaultOperands();
814 void ParseInstructions();
815 void ParsePatterns();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000816 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +0000817 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +0000818 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +0000819
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000820 void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +0000821 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
822 std::map<std::string,
823 TreePatternNode*> &InstInputs,
824 std::map<std::string,
825 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +0000826 std::vector<Record*> &InstImpResults);
827};
828} // end namespace llvm
829
830#endif