blob: d6107d98f32ed1955d9757ddddba02267d85adb4 [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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
16#define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
Chris Lattner8cab0212008-01-05 22:25:12 +000017
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
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000135 /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type
Chris Lattnercabe0372010-03-15 06:00:16 +0000136 /// 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
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000139 /// EnforceVectorEltTypeIs - 'this' is now constrained to be a vector type
Craig Topper0be34582015-03-05 07:11:34 +0000140 /// whose element is VT.
141 bool EnforceVectorEltTypeIs(MVT::SimpleValueType VT, TreePattern &TP);
142
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000143 /// EnforceVectorSubVectorTypeIs - 'this' is now constrained to
David Greene127fd1d2011-01-24 20:53:18 +0000144 /// be a vector type VT.
145 bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
146
Bruce Mitchenere9ffb452015-09-12 01:17:08 +0000147 /// EnforceVectorSameNumElts - 'this' is now constrained to
Craig Topper0be34582015-03-05 07:11:34 +0000148 /// be a vector with same num elements as VT.
149 bool EnforceVectorSameNumElts(EEVT::TypeSet &VT, TreePattern &TP);
150
Craig Topper9a44b3f2015-11-26 07:02:18 +0000151 /// EnforceSameSize - 'this' is now constrained to be the same size as VT.
152 bool EnforceSameSize(EEVT::TypeSet &VT, TreePattern &TP);
153
Chris Lattnercabe0372010-03-15 06:00:16 +0000154 bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
155 bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000156
Chris Lattnerbe6b17f2010-03-19 04:54:36 +0000157 private:
158 /// FillWithPossibleTypes - Set to all legal types and return true, only
Chris Lattner6d765eb2010-03-19 17:41:26 +0000159 /// valid on completely unknown type sets. If Pred is non-null, only MVTs
160 /// that pass the predicate are added.
161 bool FillWithPossibleTypes(TreePattern &TP,
Craig Topperada08572014-04-16 04:21:27 +0000162 bool (*Pred)(MVT::SimpleValueType) = nullptr,
163 const char *PredicateName = nullptr);
Chris Lattnercabe0372010-03-15 06:00:16 +0000164 };
Chris Lattner8cab0212008-01-05 22:25:12 +0000165}
166
Scott Michel94420742008-03-05 17:49:05 +0000167/// Set type used to track multiply used variables in patterns
168typedef std::set<std::string> MultipleUseVarSet;
169
Chris Lattner8cab0212008-01-05 22:25:12 +0000170/// SDTypeConstraint - This is a discriminated union of constraints,
171/// corresponding to the SDTypeConstraint tablegen class in Target.td.
172struct SDTypeConstraint {
173 SDTypeConstraint(Record *R);
Jim Grosbach50986b52010-12-24 05:06:32 +0000174
Chris Lattner8cab0212008-01-05 22:25:12 +0000175 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000176 enum {
177 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000178 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000179 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000180 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000181
Chris Lattner8cab0212008-01-05 22:25:12 +0000182 union { // The discriminated union.
183 struct {
Chris Lattnercabe0372010-03-15 06:00:16 +0000184 MVT::SimpleValueType VT;
Chris Lattner8cab0212008-01-05 22:25:12 +0000185 } SDTCisVT_Info;
186 struct {
187 unsigned OtherOperandNum;
188 } SDTCisSameAs_Info;
189 struct {
190 unsigned OtherOperandNum;
191 } SDTCisVTSmallerThanOp_Info;
192 struct {
193 unsigned BigOperandNum;
194 } SDTCisOpSmallerThanOp_Info;
195 struct {
196 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000197 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000198 struct {
199 unsigned OtherOperandNum;
200 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000201 struct {
202 MVT::SimpleValueType VT;
203 } SDTCVecEltisVT_Info;
204 struct {
205 unsigned OtherOperandNum;
206 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000207 struct {
208 unsigned OtherOperandNum;
209 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000210 } x;
211
212 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
213 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000214 /// change, false otherwise. If a type contradiction is found, an error
215 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000216 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
217 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000218};
219
220/// SDNodeInfo - One of these records is created for each SDNode instance in
221/// the target .td file. This represents the various dag nodes we will be
222/// processing.
223class SDNodeInfo {
224 Record *Def;
225 std::string EnumName;
226 std::string SDClassName;
227 unsigned Properties;
228 unsigned NumResults;
229 int NumOperands;
230 std::vector<SDTypeConstraint> TypeConstraints;
231public:
232 SDNodeInfo(Record *R); // Parse the specified record.
Jim Grosbach50986b52010-12-24 05:06:32 +0000233
Chris Lattner8cab0212008-01-05 22:25:12 +0000234 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000235
Chris Lattner135091b2010-03-28 08:48:47 +0000236 /// getNumOperands - This is the number of operands required or -1 if
237 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000238 int getNumOperands() const { return NumOperands; }
239 Record *getRecord() const { return Def; }
240 const std::string &getEnumName() const { return EnumName; }
241 const std::string &getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000242
Chris Lattner8cab0212008-01-05 22:25:12 +0000243 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
244 return TypeConstraints;
245 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000246
Chris Lattner99e53b32010-02-28 00:22:30 +0000247 /// getKnownType - If the type constraints on this node imply a fixed type
248 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000249 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000250 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000251
Chris Lattner8cab0212008-01-05 22:25:12 +0000252 /// hasProperty - Return true if this node has the specified property.
253 ///
254 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
255
256 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
257 /// constraints for this node to the operands of the node. This returns
258 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000259 /// found, an error is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000260 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
261 bool MadeChange = false;
262 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
263 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
264 return MadeChange;
265 }
266};
Chris Lattner514e2922011-04-17 21:38:24 +0000267
268/// TreePredicateFn - This is an abstraction that represents the predicates on
269/// a PatFrag node. This is a simple one-word wrapper around a pointer to
270/// provide nice accessors.
271class TreePredicateFn {
272 /// PatFragRec - This is the TreePattern for the PatFrag that we
273 /// originally came from.
274 TreePattern *PatFragRec;
275public:
276 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000277 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000278
279
280 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
281
282 /// isAlwaysTrue - Return true if this is a noop predicate.
283 bool isAlwaysTrue() const;
284
Chris Lattner07add492011-04-18 06:22:33 +0000285 bool isImmediatePattern() const { return !getImmCode().empty(); }
286
287 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
288 /// this is an immediate predicate. It is an error to call this on a
289 /// non-immediate pattern.
290 std::string getImmediatePredicateCode() const {
291 std::string Result = getImmCode();
292 assert(!Result.empty() && "Isn't an immediate pattern!");
293 return Result;
294 }
295
Chris Lattner514e2922011-04-17 21:38:24 +0000296
297 bool operator==(const TreePredicateFn &RHS) const {
298 return PatFragRec == RHS.PatFragRec;
299 }
300
301 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
302
303 /// Return the name to use in the generated code to reference this, this is
304 /// "Predicate_foo" if from a pattern fragment "foo".
305 std::string getFnName() const;
306
307 /// getCodeToRunOnSDNode - Return the code for the function body that
308 /// evaluates this predicate. The argument is expected to be in "Node",
309 /// not N. This handles casting and conversion to a concrete node type as
310 /// appropriate.
311 std::string getCodeToRunOnSDNode() const;
312
313private:
314 std::string getPredCode() const;
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000315 std::string getImmCode() const;
Chris Lattner514e2922011-04-17 21:38:24 +0000316};
David Blaikiecf195302014-11-17 22:55:41 +0000317
Chris Lattner8cab0212008-01-05 22:25:12 +0000318
319/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
320/// patterns), and as such should be ref counted. We currently just leak all
321/// TreePatternNode objects!
322class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000323 /// The type of each node result. Before and during type inference, each
324 /// result may be a set of possible types. After (successful) type inference,
325 /// each is a single concrete type.
326 SmallVector<EEVT::TypeSet, 1> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000327
Chris Lattner8cab0212008-01-05 22:25:12 +0000328 /// Operator - The Record for the operator if this is an interior node (not
329 /// a leaf).
330 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000331
Chris Lattner8cab0212008-01-05 22:25:12 +0000332 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
333 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000334 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000335
Chris Lattner8cab0212008-01-05 22:25:12 +0000336 /// Name - The name given to this node with the :$foo notation.
337 ///
338 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000339
Dan Gohman6e979022008-10-15 06:17:21 +0000340 /// PredicateFns - The predicate functions to execute on this node to check
341 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000342 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000343
Chris Lattner8cab0212008-01-05 22:25:12 +0000344 /// TransformFn - The transformation function to execute on this node before
345 /// it can be substituted into the resulting instruction on a pattern match.
346 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000347
Chris Lattner8cab0212008-01-05 22:25:12 +0000348 std::vector<TreePatternNode*> Children;
349public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000350 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000351 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000352 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000353 Types.resize(NumResults);
354 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000355 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000356 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000357 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000358 }
359 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000360
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000361 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000362 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000363 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000364
Craig Topperada08572014-04-16 04:21:27 +0000365 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000366
Chris Lattnercabe0372010-03-15 06:00:16 +0000367 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000368 unsigned getNumTypes() const { return Types.size(); }
369 MVT::SimpleValueType getType(unsigned ResNo) const {
370 return Types[ResNo].getConcrete();
371 }
372 const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
373 const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
374 EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
375 void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000376
Chris Lattnerf1447252010-03-19 21:37:09 +0000377 bool hasTypeSet(unsigned ResNo) const {
378 return Types[ResNo].isConcrete();
379 }
380 bool isTypeCompletelyUnknown(unsigned ResNo) const {
381 return Types[ResNo].isCompletelyUnknown();
382 }
383 bool isTypeDynamicallyResolved(unsigned ResNo) const {
384 return Types[ResNo].isDynamicallyResolved();
385 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000386
David Greeneaf8ee2c2011-07-29 22:43:06 +0000387 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000388 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000389
Chris Lattner8cab0212008-01-05 22:25:12 +0000390 unsigned getNumChildren() const { return Children.size(); }
391 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
392 void setChild(unsigned i, TreePatternNode *N) {
393 Children[i] = N;
394 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000395
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000396 /// hasChild - Return true if N is any of our children.
397 bool hasChild(const TreePatternNode *N) const {
398 for (unsigned i = 0, e = Children.size(); i != e; ++i)
399 if (Children[i] == N) return true;
400 return false;
401 }
Chris Lattner89c65662008-01-06 05:36:50 +0000402
Chris Lattner514e2922011-04-17 21:38:24 +0000403 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
404
405 const std::vector<TreePredicateFn> &getPredicateFns() const {
406 return PredicateFns;
407 }
Dan Gohman6e979022008-10-15 06:17:21 +0000408 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000409 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000410 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
411 PredicateFns = Fns;
412 }
Chris Lattner514e2922011-04-17 21:38:24 +0000413 void addPredicateFn(const TreePredicateFn &Fn) {
414 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000415 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000416 PredicateFns.push_back(Fn);
417 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000418
419 Record *getTransformFn() const { return TransformFn; }
420 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000421
Chris Lattner89c65662008-01-06 05:36:50 +0000422 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
423 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
424 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000425
Chris Lattner53c39ba2010-02-14 22:22:58 +0000426 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
427 /// return the ComplexPattern information, otherwise return null.
428 const ComplexPattern *
429 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
430
Tim Northoverc807a172014-05-20 11:52:46 +0000431 /// Returns the number of MachineInstr operands that would be produced by this
432 /// node if it mapped directly to an output Instruction's
433 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
434 /// for Operands; otherwise 1.
435 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
436
Chris Lattner53c39ba2010-02-14 22:22:58 +0000437 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000438 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000439
Chris Lattner53c39ba2010-02-14 22:22:58 +0000440 /// TreeHasProperty - Return true if any node in this tree has the specified
441 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000442 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000443
Evan Cheng49bad4c2008-06-16 20:29:38 +0000444 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
445 /// marked isCommutative.
446 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000447
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000448 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000449 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000450
Chris Lattner8cab0212008-01-05 22:25:12 +0000451public: // Higher level manipulation routines.
452
453 /// clone - Return a new copy of this tree.
454 ///
455 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000456
457 /// RemoveAllTypes - Recursively strip all the types of this tree.
458 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000459
Chris Lattner8cab0212008-01-05 22:25:12 +0000460 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
461 /// the specified node. For this comparison, all of the state of the node
462 /// is considered, except for the assigned name. Nodes with differing names
463 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000464 bool isIsomorphicTo(const TreePatternNode *N,
465 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000466
Chris Lattner8cab0212008-01-05 22:25:12 +0000467 /// SubstituteFormalArguments - Replace the formal arguments in this tree
468 /// with actual values specified by ArgMap.
469 void SubstituteFormalArguments(std::map<std::string,
470 TreePatternNode*> &ArgMap);
471
472 /// InlinePatternFragments - If this pattern refers to any pattern
473 /// fragments, inline them into place, giving us a pattern without any
474 /// PatFrag references.
475 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000476
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000477 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000478 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000479 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000480 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000481
Chris Lattner8cab0212008-01-05 22:25:12 +0000482 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000483 /// information. If N already contains a conflicting type, then flag an
484 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000485 ///
Chris Lattnerf1447252010-03-19 21:37:09 +0000486 bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
487 TreePattern &TP) {
488 return Types[ResNo].MergeInTypeInfo(InTy, TP);
Chris Lattnercabe0372010-03-15 06:00:16 +0000489 }
490
Chris Lattnerf1447252010-03-19 21:37:09 +0000491 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
492 TreePattern &TP) {
493 return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000494 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000495
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000496 // Update node type with types inferred from an instruction operand or result
497 // def from the ins/outs lists.
498 // Return true if the type changed.
499 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
500
Chris Lattner8cab0212008-01-05 22:25:12 +0000501 /// ContainsUnresolvedType - Return true if this tree contains any
502 /// unresolved types.
503 bool ContainsUnresolvedType() const {
Chris Lattnerf1447252010-03-19 21:37:09 +0000504 for (unsigned i = 0, e = Types.size(); i != e; ++i)
505 if (!Types[i].isConcrete()) return true;
Jim Grosbach50986b52010-12-24 05:06:32 +0000506
Chris Lattner8cab0212008-01-05 22:25:12 +0000507 for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
508 if (getChild(i)->ContainsUnresolvedType()) return true;
509 return false;
510 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000511
Chris Lattner8cab0212008-01-05 22:25:12 +0000512 /// canPatternMatch - If it is impossible for this pattern to match on this
513 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000514 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000515};
516
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000517inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
518 TPN.print(OS);
519 return OS;
520}
Jim Grosbach50986b52010-12-24 05:06:32 +0000521
Chris Lattner8cab0212008-01-05 22:25:12 +0000522
523/// TreePattern - Represent a pattern, used for instructions, pattern
524/// fragments, etc.
525///
526class TreePattern {
527 /// Trees - The list of pattern trees which corresponds to this pattern.
528 /// Note that PatFrag's only have a single tree.
529 ///
David Blaikiecf195302014-11-17 22:55:41 +0000530 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000531
Chris Lattnercabe0372010-03-15 06:00:16 +0000532 /// NamedNodes - This is all of the nodes that have names in the trees in this
533 /// pattern.
534 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000535
Chris Lattner8cab0212008-01-05 22:25:12 +0000536 /// TheRecord - The actual TableGen record corresponding to this pattern.
537 ///
538 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000539
Chris Lattner8cab0212008-01-05 22:25:12 +0000540 /// Args - This is a list of all of the arguments to this pattern (for
541 /// PatFrag patterns), which are the 'node' markers in this pattern.
542 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000543
Chris Lattner8cab0212008-01-05 22:25:12 +0000544 /// CDP - the top-level object coordinating this madness.
545 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000546 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000547
548 /// isInputPattern - True if this is an input pattern, something to match.
549 /// False if this is an output pattern, something to emit.
550 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000551
552 /// hasError - True if the currently processed nodes have unresolvable types
553 /// or other non-fatal errors
554 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000555
556 /// It's important that the usage of operands in ComplexPatterns is
557 /// consistent: each named operand can be defined by at most one
558 /// ComplexPattern. This records the ComplexPattern instance and the operand
559 /// number for each operand encountered in a ComplexPattern to aid in that
560 /// check.
561 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Chris Lattner8cab0212008-01-05 22:25:12 +0000562public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000563
Chris Lattner8cab0212008-01-05 22:25:12 +0000564 /// TreePattern constructor - Parse the specified DagInits into the
565 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000566 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000567 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000568 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000569 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000570 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
571 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000572
Chris Lattner8cab0212008-01-05 22:25:12 +0000573 /// getTrees - Return the tree patterns which corresponds to this pattern.
574 ///
David Blaikiecf195302014-11-17 22:55:41 +0000575 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000576 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000577 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
578 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000579 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
580 return Trees[0];
581 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000582
Chris Lattnercabe0372010-03-15 06:00:16 +0000583 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
584 if (NamedNodes.empty())
585 ComputeNamedNodes();
586 return NamedNodes;
587 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000588
Chris Lattner8cab0212008-01-05 22:25:12 +0000589 /// getRecord - Return the actual TableGen record corresponding to this
590 /// pattern.
591 ///
592 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000593
Chris Lattner8cab0212008-01-05 22:25:12 +0000594 unsigned getNumArgs() const { return Args.size(); }
595 const std::string &getArgName(unsigned i) const {
596 assert(i < Args.size() && "Argument reference out of range!");
597 return Args[i];
598 }
599 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000600
Chris Lattnerab3242f2008-01-06 01:10:31 +0000601 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000602
603 /// InlinePatternFragments - If this pattern refers to any pattern
604 /// fragments, inline them into place, giving us a pattern without any
605 /// PatFrag references.
606 void InlinePatternFragments() {
607 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000608 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000609 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000610
Chris Lattner8cab0212008-01-05 22:25:12 +0000611 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000612 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000613 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000614 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000615 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000616
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000617 /// error - If this is the first error in the current resolution step,
618 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000619 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000620 bool hasError() const {
621 return HasError;
622 }
623 void resetError() {
624 HasError = false;
625 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000626
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000627 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000628 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000629
Chris Lattner8cab0212008-01-05 22:25:12 +0000630private:
David Blaikiecf195302014-11-17 22:55:41 +0000631 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000632 void ComputeNamedNodes();
633 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000634};
635
Tom Stellardb7246a72012-09-06 14:15:52 +0000636/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
637/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000638struct DAGDefaultOperand {
639 std::vector<TreePatternNode*> DefaultOps;
640};
641
642class DAGInstruction {
643 TreePattern *Pattern;
644 std::vector<Record*> Results;
645 std::vector<Record*> Operands;
646 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000647 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000648public:
649 DAGInstruction(TreePattern *TP,
650 const std::vector<Record*> &results,
651 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000652 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000653 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000654 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000655
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000656 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000657 unsigned getNumResults() const { return Results.size(); }
658 unsigned getNumOperands() const { return Operands.size(); }
659 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000660 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000661
David Blaikiecf195302014-11-17 22:55:41 +0000662 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000663
Chris Lattner8cab0212008-01-05 22:25:12 +0000664 Record *getResult(unsigned RN) const {
665 assert(RN < Results.size());
666 return Results[RN];
667 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000668
Chris Lattner8cab0212008-01-05 22:25:12 +0000669 Record *getOperand(unsigned ON) const {
670 assert(ON < Operands.size());
671 return Operands[ON];
672 }
673
674 Record *getImpResult(unsigned RN) const {
675 assert(RN < ImpResults.size());
676 return ImpResults[RN];
677 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000678
David Blaikiecf195302014-11-17 22:55:41 +0000679 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000680};
Jim Grosbach50986b52010-12-24 05:06:32 +0000681
Chris Lattnerab3242f2008-01-06 01:10:31 +0000682/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000683/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000684class PatternToMatch {
685public:
David Greeneaf8ee2c2011-07-29 22:43:06 +0000686 PatternToMatch(Record *srcrecord, ListInit *preds,
Chris Lattner8cab0212008-01-05 22:25:12 +0000687 TreePatternNode *src, TreePatternNode *dst,
688 const std::vector<Record*> &dstregs,
Tom Stellard6655dd62014-08-01 00:32:36 +0000689 int complexity, unsigned uid)
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000690 : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000691 Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000692
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000693 Record *SrcRecord; // Originating Record for the pattern.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000694 ListInit *Predicates; // Top level predicate conditions to match.
Chris Lattner8cab0212008-01-05 22:25:12 +0000695 TreePatternNode *SrcPattern; // Source pattern to match.
696 TreePatternNode *DstPattern; // Resulting pattern.
697 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +0000698 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000699 unsigned ID; // Unique ID for the record.
Chris Lattner8cab0212008-01-05 22:25:12 +0000700
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000701 Record *getSrcRecord() const { return SrcRecord; }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000702 ListInit *getPredicates() const { return Predicates; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000703 TreePatternNode *getSrcPattern() const { return SrcPattern; }
704 TreePatternNode *getDstPattern() const { return DstPattern; }
705 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +0000706 int getAddedComplexity() const { return AddedComplexity; }
Dan Gohman49e19e92008-08-22 00:20:26 +0000707
708 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000709
Chris Lattner05925fe2010-03-29 01:40:38 +0000710 /// Compute the complexity metric for the input pattern. This roughly
711 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +0000712 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000713};
714
Chris Lattnerab3242f2008-01-06 01:10:31 +0000715class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +0000716 RecordKeeper &Records;
717 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +0000718 CodeGenIntrinsicTable Intrinsics;
719 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +0000720
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000721 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
722 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms;
723 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +0000724 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
725 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000726 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
727 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +0000728
Chris Lattner8cab0212008-01-05 22:25:12 +0000729 // Specific SDNode definitions:
730 Record *intrinsic_void_sdnode;
731 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +0000732
Chris Lattner8cab0212008-01-05 22:25:12 +0000733 /// PatternsToMatch - All of the things we are matching on the DAG. The first
734 /// value is the pattern to match, the second pattern is the result to
735 /// emit.
736 std::vector<PatternToMatch> PatternsToMatch;
737public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000738 CodeGenDAGPatterns(RecordKeeper &R);
Jim Grosbach50986b52010-12-24 05:06:32 +0000739
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000740 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000741 const CodeGenTarget &getTargetInfo() const { return Target; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000742
Chris Lattner8cab0212008-01-05 22:25:12 +0000743 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000744
Chris Lattner8cab0212008-01-05 22:25:12 +0000745 const SDNodeInfo &getSDNodeInfo(Record *R) const {
746 assert(SDNodes.count(R) && "Unknown node!");
747 return SDNodes.find(R)->second;
748 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000749
Chris Lattnercc43e792008-01-05 22:54:53 +0000750 // Node transformation lookups.
751 typedef std::pair<Record*, std::string> NodeXForm;
752 const NodeXForm &getSDNodeTransform(Record *R) const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000753 assert(SDNodeXForms.count(R) && "Invalid transform!");
754 return SDNodeXForms.find(R)->second;
755 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000756
Sean Silvaa4e2c5f2012-09-19 01:47:00 +0000757 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +0000758 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +0000759 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
760 nx_iterator nx_end() const { return SDNodeXForms.end(); }
761
Jim Grosbach50986b52010-12-24 05:06:32 +0000762
Chris Lattner8cab0212008-01-05 22:25:12 +0000763 const ComplexPattern &getComplexPattern(Record *R) const {
764 assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
765 return ComplexPatterns.find(R)->second;
766 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000767
Chris Lattner8cab0212008-01-05 22:25:12 +0000768 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
769 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
770 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +0000771 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
772 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +0000773 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000774 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000775
Chris Lattner8cab0212008-01-05 22:25:12 +0000776 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +0000777 if (IID-1 < Intrinsics.size())
778 return Intrinsics[IID-1];
779 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
780 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +0000781 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000782 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000783
Chris Lattner8cab0212008-01-05 22:25:12 +0000784 unsigned getIntrinsicID(Record *R) const {
785 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
786 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +0000787 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
788 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +0000789 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +0000790 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000791
Chris Lattner7ed81692010-02-18 06:47:49 +0000792 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000793 assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
794 return DefaultOperands.find(R)->second;
795 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000796
Chris Lattner8cab0212008-01-05 22:25:12 +0000797 // Pattern Fragment information.
798 TreePattern *getPatternFragment(Record *R) const {
799 assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
David Blaikie3c6ca232014-11-13 21:40:02 +0000800 return PatternFragments.find(R)->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +0000801 }
Chris Lattnerf1447252010-03-19 21:37:09 +0000802 TreePattern *getPatternFragmentIfRead(Record *R) const {
David Blaikie3c6ca232014-11-13 21:40:02 +0000803 if (!PatternFragments.count(R))
804 return nullptr;
805 return PatternFragments.find(R)->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +0000806 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000807
David Blaikiefcacc742014-11-13 21:56:57 +0000808 typedef std::map<Record *, std::unique_ptr<TreePattern>,
809 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +0000810 pf_iterator pf_begin() const { return PatternFragments.begin(); }
811 pf_iterator pf_end() const { return PatternFragments.end(); }
812
813 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +0000814 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
815 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
816 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000817
Ahmed Bougacha14107512013-10-28 18:07:21 +0000818 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
819 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
820 const DAGInstruction &parseInstructionPattern(
821 CodeGenInstruction &CGI, ListInit *Pattern,
822 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +0000823
Chris Lattner8cab0212008-01-05 22:25:12 +0000824 const DAGInstruction &getInstruction(Record *R) const {
825 assert(Instructions.count(R) && "Unknown instruction!");
826 return Instructions.find(R)->second;
827 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000828
Chris Lattner8cab0212008-01-05 22:25:12 +0000829 Record *get_intrinsic_void_sdnode() const {
830 return intrinsic_void_sdnode;
831 }
832 Record *get_intrinsic_w_chain_sdnode() const {
833 return intrinsic_w_chain_sdnode;
834 }
835 Record *get_intrinsic_wo_chain_sdnode() const {
836 return intrinsic_wo_chain_sdnode;
837 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000838
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +0000839 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
840
Chris Lattner8cab0212008-01-05 22:25:12 +0000841private:
842 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +0000843 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +0000844 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +0000845 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +0000846 void ParseDefaultOperands();
847 void ParseInstructions();
848 void ParsePatterns();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000849 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +0000850 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +0000851 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +0000852
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000853 void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +0000854 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
855 std::map<std::string,
856 TreePatternNode*> &InstInputs,
857 std::map<std::string,
858 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +0000859 std::vector<Record*> &InstImpResults);
860};
861} // end namespace llvm
862
863#endif