blob: 348ae4d06cb20401e24452881aa531304ec46778 [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
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000018#include "CodeGenHwModes.h"
Chris Lattner8cab0212008-01-05 22:25:12 +000019#include "CodeGenIntrinsics.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "CodeGenTarget.h"
Matt Arsenault303327d2017-12-20 19:36:28 +000021#include "SDNodeProperties.h"
Chris Lattnercabe0372010-03-15 06:00:16 +000022#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringMap.h"
Zachary Turner249dc142017-09-20 18:01:40 +000024#include "llvm/ADT/StringSet.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000025#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner1802b172010-03-19 01:07:44 +000027#include <algorithm>
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000028#include <array>
Daniel Sanders7e523672017-11-11 03:23:44 +000029#include <functional>
Chris Lattner1802b172010-03-19 01:07:44 +000030#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000031#include <set>
32#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000033
34namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000035
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000036class Record;
37class Init;
38class ListInit;
39class DagInit;
40class SDNodeInfo;
41class TreePattern;
42class TreePatternNode;
43class CodeGenDAGPatterns;
44class ComplexPattern;
45
46/// This represents a set of MVTs. Since the underlying type for the MVT
47/// is uint8_t, there are at most 256 values. To reduce the number of memory
48/// allocations and deallocations, represent the set as a sequence of bits.
49/// To reduce the allocations even further, make MachineValueTypeSet own
50/// the storage and use std::array as the bit container.
51struct MachineValueTypeSet {
52 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
53 uint8_t>::value,
54 "Change uint8_t here to the SimpleValueType's type");
55 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
56 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000057 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000058 static unsigned constexpr NumWords = Capacity/WordWidth;
59 static_assert(NumWords*WordWidth == Capacity,
60 "Capacity should be a multiple of WordWidth");
61
62 LLVM_ATTRIBUTE_ALWAYS_INLINE
63 MachineValueTypeSet() {
64 clear();
65 }
66
67 LLVM_ATTRIBUTE_ALWAYS_INLINE
68 unsigned size() const {
69 unsigned Count = 0;
70 for (WordType W : Words)
71 Count += countPopulation(W);
72 return Count;
73 }
74 LLVM_ATTRIBUTE_ALWAYS_INLINE
75 void clear() {
76 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
77 }
78 LLVM_ATTRIBUTE_ALWAYS_INLINE
79 bool empty() const {
80 for (WordType W : Words)
81 if (W != 0)
82 return false;
83 return true;
84 }
85 LLVM_ATTRIBUTE_ALWAYS_INLINE
86 unsigned count(MVT T) const {
87 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
88 }
89 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
90 bool V = count(T.SimpleTy);
91 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
92 return {*this, V};
93 }
94 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
95 for (unsigned i = 0; i != NumWords; ++i)
96 Words[i] |= S.Words[i];
97 return *this;
98 }
99 LLVM_ATTRIBUTE_ALWAYS_INLINE
100 void erase(MVT T) {
101 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
102 }
103
104 struct const_iterator {
105 // Some implementations of the C++ library require these traits to be
106 // defined.
107 using iterator_category = std::forward_iterator_tag;
108 using value_type = MVT;
109 using difference_type = ptrdiff_t;
110 using pointer = const MVT*;
111 using reference = const MVT&;
112
113 LLVM_ATTRIBUTE_ALWAYS_INLINE
114 MVT operator*() const {
115 assert(Pos != Capacity);
116 return MVT::SimpleValueType(Pos);
117 }
118 LLVM_ATTRIBUTE_ALWAYS_INLINE
119 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
120 Pos = End ? Capacity : find_from_pos(0);
121 }
122 LLVM_ATTRIBUTE_ALWAYS_INLINE
123 const_iterator &operator++() {
124 assert(Pos != Capacity);
125 Pos = find_from_pos(Pos+1);
126 return *this;
127 }
128
129 LLVM_ATTRIBUTE_ALWAYS_INLINE
130 bool operator==(const const_iterator &It) const {
131 return Set == It.Set && Pos == It.Pos;
132 }
133 LLVM_ATTRIBUTE_ALWAYS_INLINE
134 bool operator!=(const const_iterator &It) const {
135 return !operator==(It);
136 }
137
138 private:
139 unsigned find_from_pos(unsigned P) const {
140 unsigned SkipWords = P / WordWidth;
141 unsigned SkipBits = P % WordWidth;
142 unsigned Count = SkipWords * WordWidth;
143
144 // If P is in the middle of a word, process it manually here, because
145 // the trailing bits need to be masked off to use findFirstSet.
146 if (SkipBits != 0) {
147 WordType W = Set->Words[SkipWords];
148 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
149 if (W != 0)
150 return Count + findFirstSet(W);
151 Count += WordWidth;
152 SkipWords++;
153 }
154
155 for (unsigned i = SkipWords; i != NumWords; ++i) {
156 WordType W = Set->Words[i];
157 if (W != 0)
158 return Count + findFirstSet(W);
159 Count += WordWidth;
160 }
161 return Capacity;
162 }
163
164 const MachineValueTypeSet *Set;
165 unsigned Pos;
166 };
167
168 LLVM_ATTRIBUTE_ALWAYS_INLINE
169 const_iterator begin() const { return const_iterator(this, false); }
170 LLVM_ATTRIBUTE_ALWAYS_INLINE
171 const_iterator end() const { return const_iterator(this, true); }
172
173 LLVM_ATTRIBUTE_ALWAYS_INLINE
174 bool operator==(const MachineValueTypeSet &S) const {
175 return Words == S.Words;
176 }
177 LLVM_ATTRIBUTE_ALWAYS_INLINE
178 bool operator!=(const MachineValueTypeSet &S) const {
179 return !operator==(S);
180 }
181
182private:
183 friend struct const_iterator;
184 std::array<WordType,NumWords> Words;
185};
186
187struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
188 using SetType = MachineValueTypeSet;
Jim Grosbach50986b52010-12-24 05:06:32 +0000189
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000190 TypeSetByHwMode() = default;
191 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
192 TypeSetByHwMode(MVT::SimpleValueType VT)
193 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
194 TypeSetByHwMode(ValueTypeByHwMode VT)
195 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
196 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000197
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000198 SetType &getOrCreate(unsigned Mode) {
199 if (hasMode(Mode))
200 return get(Mode);
201 return Map.insert({Mode,SetType()}).first->second;
202 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000203
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000204 bool isValueTypeByHwMode(bool AllowEmpty) const;
205 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000206
207 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000208 bool isMachineValueType() const {
209 return isDefaultOnly() && Map.begin()->second.size() == 1;
210 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000211
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000212 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000213 MVT getMachineValueType() const {
214 assert(isMachineValueType());
215 return *Map.begin()->second.begin();
216 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000217
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000218 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000219
220 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000221 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000222 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000223 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000224
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000225 bool insert(const ValueTypeByHwMode &VVT);
226 bool constrain(const TypeSetByHwMode &VTS);
227 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000228 template <typename Predicate>
229 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000230
Zachary Turner249dc142017-09-20 18:01:40 +0000231 void writeToStream(raw_ostream &OS) const;
232 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000233
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000234 bool operator==(const TypeSetByHwMode &VTS) const;
235 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000236
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000237 void dump() const;
238 void validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000239
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000240private:
241 /// Intersect two sets. Return true if anything has changed.
242 bool intersect(SetType &Out, const SetType &In);
243};
Jim Grosbach50986b52010-12-24 05:06:32 +0000244
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000245raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
246
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000247struct TypeInfer {
248 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000249
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000250 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
251 return VTS.isValueTypeByHwMode(AllowEmpty);
252 }
253 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
254 bool AllowEmpty) const {
255 assert(VTS.isValueTypeByHwMode(AllowEmpty));
256 return VTS.getValueTypeByHwMode();
257 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000258
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000259 /// The protocol in the following functions (Merge*, force*, Enforce*,
260 /// expand*) is to return "true" if a change has been made, "false"
261 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000262
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000263 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
264 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
265 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
266 }
267 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
268 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
269 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000270
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000271 /// Reduce the set \p Out to have at most one element for each mode.
272 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000273
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000274 /// The following four functions ensure that upon return the set \p Out
275 /// will only contain types of the specified kind: integer, floating-point,
276 /// scalar, or vector.
277 /// If \p Out is empty, all legal types of the specified kind will be added
278 /// to it. Otherwise, all types that are not of the specified kind will be
279 /// removed from \p Out.
280 bool EnforceInteger(TypeSetByHwMode &Out);
281 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
282 bool EnforceScalar(TypeSetByHwMode &Out);
283 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000284
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000285 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
286 /// unchanged.
287 bool EnforceAny(TypeSetByHwMode &Out);
288 /// Make sure that for each type in \p Small, there exists a larger type
289 /// in \p Big.
290 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
291 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
292 /// for each type U in \p Elem, U is a scalar type.
293 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
294 /// (vector) type T in \p Vec, such that U is the element type of T.
295 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
296 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
297 const ValueTypeByHwMode &VVT);
298 /// Ensure that for each type T in \p Sub, T is a vector type, and there
299 /// exists a type U in \p Vec such that U is a vector type with the same
300 /// element type as T and at least as many elements as T.
301 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
302 TypeSetByHwMode &Sub);
303 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
304 /// 2. Ensure that for each vector type T in \p V, there exists a vector
305 /// type U in \p W, such that T and U have the same number of elements.
306 /// 3. Ensure that for each vector type U in \p W, there exists a vector
307 /// type T in \p V, such that T and U have the same number of elements
308 /// (reverse of 2).
309 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
310 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
311 /// such that T and U have equal size in bits.
312 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
313 /// such that T and U have equal size in bits (reverse of 1).
314 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000315
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000316 /// For each overloaded type (i.e. of form *Any), replace it with the
317 /// corresponding subset of legal, specific types.
318 void expandOverloads(TypeSetByHwMode &VTS);
319 void expandOverloads(TypeSetByHwMode::SetType &Out,
320 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000321
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000322 struct ValidateOnExit {
323 ValidateOnExit(TypeSetByHwMode &T) : VTS(T) {}
324 ~ValidateOnExit() { VTS.validate(); }
325 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000326 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000327
328 TreePattern &TP;
329 unsigned ForceMode; // Mode to use when set.
330 bool CodeGen = false; // Set during generation of matcher code.
331
332private:
333 TypeSetByHwMode getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000334
335 /// Cached legal types.
336 bool LegalTypesCached = false;
337 TypeSetByHwMode::SetType LegalCache = {};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000338};
Chris Lattner8cab0212008-01-05 22:25:12 +0000339
Scott Michel94420742008-03-05 17:49:05 +0000340/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000341typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000342
Chris Lattner8cab0212008-01-05 22:25:12 +0000343/// SDTypeConstraint - This is a discriminated union of constraints,
344/// corresponding to the SDTypeConstraint tablegen class in Target.td.
345struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000346 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000347
Chris Lattner8cab0212008-01-05 22:25:12 +0000348 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000349 enum {
350 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000351 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000352 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000353 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000354
Chris Lattner8cab0212008-01-05 22:25:12 +0000355 union { // The discriminated union.
356 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000357 unsigned OtherOperandNum;
358 } SDTCisSameAs_Info;
359 struct {
360 unsigned OtherOperandNum;
361 } SDTCisVTSmallerThanOp_Info;
362 struct {
363 unsigned BigOperandNum;
364 } SDTCisOpSmallerThanOp_Info;
365 struct {
366 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000367 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000368 struct {
369 unsigned OtherOperandNum;
370 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000371 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000372 unsigned OtherOperandNum;
373 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000374 struct {
375 unsigned OtherOperandNum;
376 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000377 } x;
378
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000379 // The VT for SDTCisVT and SDTCVecEltisVT.
380 // Must not be in the union because it has a non-trivial destructor.
381 ValueTypeByHwMode VVT;
382
Chris Lattner8cab0212008-01-05 22:25:12 +0000383 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
384 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000385 /// change, false otherwise. If a type contradiction is found, an error
386 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000387 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
388 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000389};
390
391/// SDNodeInfo - One of these records is created for each SDNode instance in
392/// the target .td file. This represents the various dag nodes we will be
393/// processing.
394class SDNodeInfo {
395 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000396 StringRef EnumName;
397 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000398 unsigned Properties;
399 unsigned NumResults;
400 int NumOperands;
401 std::vector<SDTypeConstraint> TypeConstraints;
402public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000403 // Parse the specified record.
404 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000405
Chris Lattner8cab0212008-01-05 22:25:12 +0000406 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000407
Chris Lattner135091b2010-03-28 08:48:47 +0000408 /// getNumOperands - This is the number of operands required or -1 if
409 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000410 int getNumOperands() const { return NumOperands; }
411 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000412 StringRef getEnumName() const { return EnumName; }
413 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000414
Chris Lattner8cab0212008-01-05 22:25:12 +0000415 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
416 return TypeConstraints;
417 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000418
Chris Lattner99e53b32010-02-28 00:22:30 +0000419 /// getKnownType - If the type constraints on this node imply a fixed type
420 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000421 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000422 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000423
Chris Lattner8cab0212008-01-05 22:25:12 +0000424 /// hasProperty - Return true if this node has the specified property.
425 ///
426 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
427
428 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
429 /// constraints for this node to the operands of the node. This returns
430 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000431 /// found, an error is flagged.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000432 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000433};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000434
Chris Lattner514e2922011-04-17 21:38:24 +0000435/// TreePredicateFn - This is an abstraction that represents the predicates on
436/// a PatFrag node. This is a simple one-word wrapper around a pointer to
437/// provide nice accessors.
438class TreePredicateFn {
439 /// PatFragRec - This is the TreePattern for the PatFrag that we
440 /// originally came from.
441 TreePattern *PatFragRec;
442public:
443 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000444 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000445
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000446
Chris Lattner514e2922011-04-17 21:38:24 +0000447 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000448
Chris Lattner514e2922011-04-17 21:38:24 +0000449 /// isAlwaysTrue - Return true if this is a noop predicate.
450 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000451
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000452 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000453
Chris Lattner07add492011-04-18 06:22:33 +0000454 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
455 /// this is an immediate predicate. It is an error to call this on a
456 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000457 std::string getImmediatePredicateCode() const {
458 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000459 assert(!Result.empty() && "Isn't an immediate pattern!");
460 return Result;
461 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000462
Chris Lattner514e2922011-04-17 21:38:24 +0000463 bool operator==(const TreePredicateFn &RHS) const {
464 return PatFragRec == RHS.PatFragRec;
465 }
466
467 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
468
469 /// Return the name to use in the generated code to reference this, this is
470 /// "Predicate_foo" if from a pattern fragment "foo".
471 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000472
Chris Lattner514e2922011-04-17 21:38:24 +0000473 /// getCodeToRunOnSDNode - Return the code for the function body that
474 /// evaluates this predicate. The argument is expected to be in "Node",
475 /// not N. This handles casting and conversion to a concrete node type as
476 /// appropriate.
477 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000478
Daniel Sanders649c5852017-10-13 20:42:18 +0000479 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000480 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000481
Daniel Sanders11300ce2017-10-13 21:28:03 +0000482 /// Get a string that describes the type returned by getImmType() but is
483 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000484 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000485
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000486 // Is the desired predefined predicate for a load?
487 bool isLoad() const;
488 // Is the desired predefined predicate for a store?
489 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000490 // Is the desired predefined predicate for an atomic?
491 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000492
493 /// Is this predicate the predefined unindexed load predicate?
494 /// Is this predicate the predefined unindexed store predicate?
495 bool isUnindexed() const;
496 /// Is this predicate the predefined non-extending load predicate?
497 bool isNonExtLoad() const;
498 /// Is this predicate the predefined any-extend load predicate?
499 bool isAnyExtLoad() const;
500 /// Is this predicate the predefined sign-extend load predicate?
501 bool isSignExtLoad() const;
502 /// Is this predicate the predefined zero-extend load predicate?
503 bool isZeroExtLoad() const;
504 /// Is this predicate the predefined non-truncating store predicate?
505 bool isNonTruncStore() const;
506 /// Is this predicate the predefined truncating store predicate?
507 bool isTruncStore() const;
508
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000509 /// Is this predicate the predefined monotonic atomic predicate?
510 bool isAtomicOrderingMonotonic() const;
511 /// Is this predicate the predefined acquire atomic predicate?
512 bool isAtomicOrderingAcquire() const;
513 /// Is this predicate the predefined release atomic predicate?
514 bool isAtomicOrderingRelease() const;
515 /// Is this predicate the predefined acquire-release atomic predicate?
516 bool isAtomicOrderingAcquireRelease() const;
517 /// Is this predicate the predefined sequentially consistent atomic predicate?
518 bool isAtomicOrderingSequentiallyConsistent() const;
519
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000520 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
521 bool isAtomicOrderingAcquireOrStronger() const;
522 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
523 bool isAtomicOrderingWeakerThanAcquire() const;
524
525 /// Is this predicate the predefined release-or-stronger atomic predicate?
526 bool isAtomicOrderingReleaseOrStronger() const;
527 /// Is this predicate the predefined weaker-than-release atomic predicate?
528 bool isAtomicOrderingWeakerThanRelease() const;
529
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000530 /// If non-null, indicates that this predicate is a predefined memory VT
531 /// predicate for a load/store and returns the ValueType record for the memory VT.
532 Record *getMemoryVT() const;
533 /// If non-null, indicates that this predicate is a predefined memory VT
534 /// predicate (checking only the scalar type) for load/store and returns the
535 /// ValueType record for the memory VT.
536 Record *getScalarMemoryVT() const;
537
Chris Lattner514e2922011-04-17 21:38:24 +0000538private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000539 bool hasPredCode() const;
540 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000541 std::string getPredCode() const;
542 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000543 bool immCodeUsesAPInt() const;
544 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000545
546 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000547};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000548
Chris Lattner8cab0212008-01-05 22:25:12 +0000549
550/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
551/// patterns), and as such should be ref counted. We currently just leak all
552/// TreePatternNode objects!
553class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000554 /// The type of each node result. Before and during type inference, each
555 /// result may be a set of possible types. After (successful) type inference,
556 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000557 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000558
Chris Lattner8cab0212008-01-05 22:25:12 +0000559 /// Operator - The Record for the operator if this is an interior node (not
560 /// a leaf).
561 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000562
Chris Lattner8cab0212008-01-05 22:25:12 +0000563 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
564 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000565 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000566
Chris Lattner8cab0212008-01-05 22:25:12 +0000567 /// Name - The name given to this node with the :$foo notation.
568 ///
569 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000570
Dan Gohman6e979022008-10-15 06:17:21 +0000571 /// PredicateFns - The predicate functions to execute on this node to check
572 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000573 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000574
Chris Lattner8cab0212008-01-05 22:25:12 +0000575 /// TransformFn - The transformation function to execute on this node before
576 /// it can be substituted into the resulting instruction on a pattern match.
577 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000578
Chris Lattner8cab0212008-01-05 22:25:12 +0000579 std::vector<TreePatternNode*> Children;
580public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000581 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000582 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000583 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000584 Types.resize(NumResults);
585 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000586 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000587 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000588 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000589 }
590 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000591
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000592 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000593 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000594 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000595
Craig Topperada08572014-04-16 04:21:27 +0000596 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000597
Chris Lattnercabe0372010-03-15 06:00:16 +0000598 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000599 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000600 ValueTypeByHwMode getType(unsigned ResNo) const {
601 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000602 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000603 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
604 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
605 return Types[ResNo];
606 }
607 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
608 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
609 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
610 return Types[ResNo].getMachineValueType().SimpleTy;
611 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000612
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000613 bool hasConcreteType(unsigned ResNo) const {
614 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000615 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000616 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
617 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000618 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000619
David Greeneaf8ee2c2011-07-29 22:43:06 +0000620 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000621 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000622
Chris Lattner8cab0212008-01-05 22:25:12 +0000623 unsigned getNumChildren() const { return Children.size(); }
624 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
625 void setChild(unsigned i, TreePatternNode *N) {
626 Children[i] = N;
627 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000628
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000629 /// hasChild - Return true if N is any of our children.
630 bool hasChild(const TreePatternNode *N) const {
631 for (unsigned i = 0, e = Children.size(); i != e; ++i)
632 if (Children[i] == N) return true;
633 return false;
634 }
Chris Lattner89c65662008-01-06 05:36:50 +0000635
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000636 bool hasProperTypeByHwMode() const;
637 bool hasPossibleType() const;
638 bool setDefaultMode(unsigned Mode);
639
Chris Lattner514e2922011-04-17 21:38:24 +0000640 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000641
Chris Lattner514e2922011-04-17 21:38:24 +0000642 const std::vector<TreePredicateFn> &getPredicateFns() const {
643 return PredicateFns;
644 }
Dan Gohman6e979022008-10-15 06:17:21 +0000645 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000646 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000647 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
648 PredicateFns = Fns;
649 }
Chris Lattner514e2922011-04-17 21:38:24 +0000650 void addPredicateFn(const TreePredicateFn &Fn) {
651 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000652 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000653 PredicateFns.push_back(Fn);
654 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000655
656 Record *getTransformFn() const { return TransformFn; }
657 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000658
Chris Lattner89c65662008-01-06 05:36:50 +0000659 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
660 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
661 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000662
Chris Lattner53c39ba2010-02-14 22:22:58 +0000663 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
664 /// return the ComplexPattern information, otherwise return null.
665 const ComplexPattern *
666 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
667
Tim Northoverc807a172014-05-20 11:52:46 +0000668 /// Returns the number of MachineInstr operands that would be produced by this
669 /// node if it mapped directly to an output Instruction's
670 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
671 /// for Operands; otherwise 1.
672 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
673
Chris Lattner53c39ba2010-02-14 22:22:58 +0000674 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000675 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000676
Chris Lattner53c39ba2010-02-14 22:22:58 +0000677 /// TreeHasProperty - Return true if any node in this tree has the specified
678 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000679 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000680
Evan Cheng49bad4c2008-06-16 20:29:38 +0000681 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
682 /// marked isCommutative.
683 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000684
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000685 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000686 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000687
Chris Lattner8cab0212008-01-05 22:25:12 +0000688public: // Higher level manipulation routines.
689
690 /// clone - Return a new copy of this tree.
691 ///
692 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000693
694 /// RemoveAllTypes - Recursively strip all the types of this tree.
695 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000696
Chris Lattner8cab0212008-01-05 22:25:12 +0000697 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
698 /// the specified node. For this comparison, all of the state of the node
699 /// is considered, except for the assigned name. Nodes with differing names
700 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000701 bool isIsomorphicTo(const TreePatternNode *N,
702 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000703
Chris Lattner8cab0212008-01-05 22:25:12 +0000704 /// SubstituteFormalArguments - Replace the formal arguments in this tree
705 /// with actual values specified by ArgMap.
706 void SubstituteFormalArguments(std::map<std::string,
707 TreePatternNode*> &ArgMap);
708
709 /// InlinePatternFragments - If this pattern refers to any pattern
710 /// fragments, inline them into place, giving us a pattern without any
711 /// PatFrag references.
712 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000713
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000714 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000715 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000716 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000717 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000718
Chris Lattner8cab0212008-01-05 22:25:12 +0000719 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000720 /// information. If N already contains a conflicting type, then flag an
721 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000722 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000723 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
724 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000725 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000726 TreePattern &TP);
727 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
728 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000729
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000730 // Update node type with types inferred from an instruction operand or result
731 // def from the ins/outs lists.
732 // Return true if the type changed.
733 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
734
Chris Lattner8cab0212008-01-05 22:25:12 +0000735 /// ContainsUnresolvedType - Return true if this tree contains any
736 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000737 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000738
Chris Lattner8cab0212008-01-05 22:25:12 +0000739 /// canPatternMatch - If it is impossible for this pattern to match on this
740 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000741 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000742};
743
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000744inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
745 TPN.print(OS);
746 return OS;
747}
Jim Grosbach50986b52010-12-24 05:06:32 +0000748
Chris Lattner8cab0212008-01-05 22:25:12 +0000749
750/// TreePattern - Represent a pattern, used for instructions, pattern
751/// fragments, etc.
752///
753class TreePattern {
754 /// Trees - The list of pattern trees which corresponds to this pattern.
755 /// Note that PatFrag's only have a single tree.
756 ///
David Blaikiecf195302014-11-17 22:55:41 +0000757 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000758
Chris Lattnercabe0372010-03-15 06:00:16 +0000759 /// NamedNodes - This is all of the nodes that have names in the trees in this
760 /// pattern.
761 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000762
Chris Lattner8cab0212008-01-05 22:25:12 +0000763 /// TheRecord - The actual TableGen record corresponding to this pattern.
764 ///
765 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000766
Chris Lattner8cab0212008-01-05 22:25:12 +0000767 /// Args - This is a list of all of the arguments to this pattern (for
768 /// PatFrag patterns), which are the 'node' markers in this pattern.
769 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000770
Chris Lattner8cab0212008-01-05 22:25:12 +0000771 /// CDP - the top-level object coordinating this madness.
772 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000773 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000774
775 /// isInputPattern - True if this is an input pattern, something to match.
776 /// False if this is an output pattern, something to emit.
777 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000778
779 /// hasError - True if the currently processed nodes have unresolvable types
780 /// or other non-fatal errors
781 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000782
783 /// It's important that the usage of operands in ComplexPatterns is
784 /// consistent: each named operand can be defined by at most one
785 /// ComplexPattern. This records the ComplexPattern instance and the operand
786 /// number for each operand encountered in a ComplexPattern to aid in that
787 /// check.
788 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000789
790 TypeInfer Infer;
791
Chris Lattner8cab0212008-01-05 22:25:12 +0000792public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000793
Chris Lattner8cab0212008-01-05 22:25:12 +0000794 /// TreePattern constructor - Parse the specified DagInits into the
795 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000796 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000797 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000798 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000799 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000800 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
801 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000802
Chris Lattner8cab0212008-01-05 22:25:12 +0000803 /// getTrees - Return the tree patterns which corresponds to this pattern.
804 ///
David Blaikiecf195302014-11-17 22:55:41 +0000805 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000806 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000807 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Daniel Sanders7e523672017-11-11 03:23:44 +0000808 void setTree(unsigned i, TreePatternNode *Tree) { Trees[i] = Tree; }
David Blaikiecf195302014-11-17 22:55:41 +0000809 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000810 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
811 return Trees[0];
812 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000813
Chris Lattnercabe0372010-03-15 06:00:16 +0000814 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
815 if (NamedNodes.empty())
816 ComputeNamedNodes();
817 return NamedNodes;
818 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000819
Chris Lattner8cab0212008-01-05 22:25:12 +0000820 /// getRecord - Return the actual TableGen record corresponding to this
821 /// pattern.
822 ///
823 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000824
Chris Lattner8cab0212008-01-05 22:25:12 +0000825 unsigned getNumArgs() const { return Args.size(); }
826 const std::string &getArgName(unsigned i) const {
827 assert(i < Args.size() && "Argument reference out of range!");
828 return Args[i];
829 }
830 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000831
Chris Lattnerab3242f2008-01-06 01:10:31 +0000832 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000833
834 /// InlinePatternFragments - If this pattern refers to any pattern
835 /// fragments, inline them into place, giving us a pattern without any
836 /// PatFrag references.
837 void InlinePatternFragments() {
838 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000839 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000840 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000841
Chris Lattner8cab0212008-01-05 22:25:12 +0000842 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000843 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000844 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000845 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000846 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000847
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000848 /// error - If this is the first error in the current resolution step,
849 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000850 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000851 bool hasError() const {
852 return HasError;
853 }
854 void resetError() {
855 HasError = false;
856 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000857
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000858 TypeInfer &getInfer() { return Infer; }
859
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000860 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000861 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000862
Chris Lattner8cab0212008-01-05 22:25:12 +0000863private:
David Blaikiecf195302014-11-17 22:55:41 +0000864 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000865 void ComputeNamedNodes();
866 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000867};
868
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000869
870inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
871 const TypeSetByHwMode &InTy,
872 TreePattern &TP) {
873 TypeSetByHwMode VTS(InTy);
874 TP.getInfer().expandOverloads(VTS);
875 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
876}
877
878inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
879 MVT::SimpleValueType InTy,
880 TreePattern &TP) {
881 TypeSetByHwMode VTS(InTy);
882 TP.getInfer().expandOverloads(VTS);
883 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
884}
885
886inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
887 ValueTypeByHwMode InTy,
888 TreePattern &TP) {
889 TypeSetByHwMode VTS(InTy);
890 TP.getInfer().expandOverloads(VTS);
891 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
892}
893
894
Tom Stellardb7246a72012-09-06 14:15:52 +0000895/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
896/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000897struct DAGDefaultOperand {
898 std::vector<TreePatternNode*> DefaultOps;
899};
900
901class DAGInstruction {
902 TreePattern *Pattern;
903 std::vector<Record*> Results;
904 std::vector<Record*> Operands;
905 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000906 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000907public:
908 DAGInstruction(TreePattern *TP,
909 const std::vector<Record*> &results,
910 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000911 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000912 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000913 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000914
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000915 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000916 unsigned getNumResults() const { return Results.size(); }
917 unsigned getNumOperands() const { return Operands.size(); }
918 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000919 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000920
David Blaikiecf195302014-11-17 22:55:41 +0000921 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000922
Chris Lattner8cab0212008-01-05 22:25:12 +0000923 Record *getResult(unsigned RN) const {
924 assert(RN < Results.size());
925 return Results[RN];
926 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000927
Chris Lattner8cab0212008-01-05 22:25:12 +0000928 Record *getOperand(unsigned ON) const {
929 assert(ON < Operands.size());
930 return Operands[ON];
931 }
932
933 Record *getImpResult(unsigned RN) const {
934 assert(RN < ImpResults.size());
935 return ImpResults[RN];
936 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000937
David Blaikiecf195302014-11-17 22:55:41 +0000938 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000939};
Jim Grosbach50986b52010-12-24 05:06:32 +0000940
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000941/// This class represents a condition that has to be satisfied for a pattern
942/// to be tried. It is a generalization of a class "Pattern" from Target.td:
943/// in addition to the Target.td's predicates, this class can also represent
944/// conditions associated with HW modes. Both types will eventually become
945/// strings containing C++ code to be executed, the difference is in how
946/// these strings are generated.
947class Predicate {
948public:
949 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
950 assert(R->isSubClassOf("Predicate") &&
951 "Predicate objects should only be created for records derived"
952 "from Predicate class");
953 }
954 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
955 IfCond(C), IsHwMode(true) {}
956
957 /// Return a string which contains the C++ condition code that will serve
958 /// as a predicate during instruction selection.
959 std::string getCondString() const {
960 // The string will excute in a subclass of SelectionDAGISel.
961 // Cast to std::string explicitly to avoid ambiguity with StringRef.
962 std::string C = IsHwMode
963 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
964 : std::string(Def->getValueAsString("CondString"));
965 return IfCond ? C : "!("+C+')';
966 }
967 bool operator==(const Predicate &P) const {
968 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
969 }
970 bool operator<(const Predicate &P) const {
971 if (IsHwMode != P.IsHwMode)
972 return IsHwMode < P.IsHwMode;
973 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
974 if (IfCond != P.IfCond)
975 return IfCond < P.IfCond;
976 if (Def)
977 return LessRecord()(Def, P.Def);
978 return Features < P.Features;
979 }
980 Record *Def; ///< Predicate definition from .td file, null for
981 ///< HW modes.
982 std::string Features; ///< Feature string for HW mode.
983 bool IfCond; ///< The boolean value that the condition has to
984 ///< evaluate to for this predicate to be true.
985 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
986};
987
Chris Lattnerab3242f2008-01-06 01:10:31 +0000988/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000989/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000990class PatternToMatch {
991public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000992 PatternToMatch(Record *srcrecord, const std::vector<Predicate> &preds,
993 TreePatternNode *src, TreePatternNode *dst,
994 const std::vector<Record*> &dstregs,
995 int complexity, unsigned uid, unsigned setmode = 0)
996 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
997 Predicates(preds), Dstregs(std::move(dstregs)),
998 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
999
1000 PatternToMatch(Record *srcrecord, std::vector<Predicate> &&preds,
1001 TreePatternNode *src, TreePatternNode *dst,
1002 std::vector<Record*> &&dstregs,
1003 int complexity, unsigned uid, unsigned setmode = 0)
1004 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
1005 Predicates(preds), Dstregs(std::move(dstregs)),
1006 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001007
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001008 Record *SrcRecord; // Originating Record for the pattern.
Chris Lattner8cab0212008-01-05 22:25:12 +00001009 TreePatternNode *SrcPattern; // Source pattern to match.
1010 TreePatternNode *DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001011 std::vector<Predicate> Predicates; // Top level predicate conditions
1012 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001013 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001014 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001015 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001016 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001017
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001018 Record *getSrcRecord() const { return SrcRecord; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001019 TreePatternNode *getSrcPattern() const { return SrcPattern; }
1020 TreePatternNode *getDstPattern() const { return DstPattern; }
1021 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001022 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001023 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001024
1025 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001026
Chris Lattner05925fe2010-03-29 01:40:38 +00001027 /// Compute the complexity metric for the input pattern. This roughly
1028 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001029 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001030};
1031
Chris Lattnerab3242f2008-01-06 01:10:31 +00001032class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001033 RecordKeeper &Records;
1034 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001035 CodeGenIntrinsicTable Intrinsics;
1036 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001037
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001038 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001039 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1040 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001041 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001042 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1043 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001044 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1045 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001046
Chris Lattner8cab0212008-01-05 22:25:12 +00001047 // Specific SDNode definitions:
1048 Record *intrinsic_void_sdnode;
1049 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001050
Chris Lattner8cab0212008-01-05 22:25:12 +00001051 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1052 /// value is the pattern to match, the second pattern is the result to
1053 /// emit.
1054 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001055
1056 TypeSetByHwMode LegalVTS;
1057
Daniel Sanders7e523672017-11-11 03:23:44 +00001058 using PatternRewriterFn = std::function<void (TreePattern *)>;
1059 PatternRewriterFn PatternRewriter;
1060
Chris Lattner8cab0212008-01-05 22:25:12 +00001061public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001062 CodeGenDAGPatterns(RecordKeeper &R,
1063 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001064
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001065 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001066 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001067 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001068
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001069 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001070
Chris Lattner8cab0212008-01-05 22:25:12 +00001071 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001072 auto F = SDNodes.find(R);
1073 assert(F != SDNodes.end() && "Unknown node!");
1074 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001075 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001076
Chris Lattnercc43e792008-01-05 22:54:53 +00001077 // Node transformation lookups.
1078 typedef std::pair<Record*, std::string> NodeXForm;
1079 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001080 auto F = SDNodeXForms.find(R);
1081 assert(F != SDNodeXForms.end() && "Invalid transform!");
1082 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001083 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001084
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001085 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001086 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001087 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1088 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1089
Jim Grosbach50986b52010-12-24 05:06:32 +00001090
Chris Lattner8cab0212008-01-05 22:25:12 +00001091 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001092 auto F = ComplexPatterns.find(R);
1093 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1094 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001095 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001096
Chris Lattner8cab0212008-01-05 22:25:12 +00001097 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1098 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1099 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001100 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1101 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001102 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001103 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001104
Chris Lattner8cab0212008-01-05 22:25:12 +00001105 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001106 if (IID-1 < Intrinsics.size())
1107 return Intrinsics[IID-1];
1108 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1109 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001110 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001111 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001112
Chris Lattner8cab0212008-01-05 22:25:12 +00001113 unsigned getIntrinsicID(Record *R) const {
1114 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1115 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001116 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1117 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001118 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001119 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001120
Chris Lattner7ed81692010-02-18 06:47:49 +00001121 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001122 auto F = DefaultOperands.find(R);
1123 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1124 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001125 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001126
Chris Lattner8cab0212008-01-05 22:25:12 +00001127 // Pattern Fragment information.
1128 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001129 auto F = PatternFragments.find(R);
1130 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1131 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001132 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001133 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001134 auto F = PatternFragments.find(R);
1135 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001136 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001137 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001138 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001139
David Blaikiefcacc742014-11-13 21:56:57 +00001140 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1141 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001142 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1143 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001144 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001145
1146 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001147 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1148 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1149 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001150 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001151
Ahmed Bougacha14107512013-10-28 18:07:21 +00001152 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1153 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1154 const DAGInstruction &parseInstructionPattern(
1155 CodeGenInstruction &CGI, ListInit *Pattern,
1156 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001157
Chris Lattner8cab0212008-01-05 22:25:12 +00001158 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001159 auto F = Instructions.find(R);
1160 assert(F != Instructions.end() && "Unknown instruction!");
1161 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001162 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001163
Chris Lattner8cab0212008-01-05 22:25:12 +00001164 Record *get_intrinsic_void_sdnode() const {
1165 return intrinsic_void_sdnode;
1166 }
1167 Record *get_intrinsic_w_chain_sdnode() const {
1168 return intrinsic_w_chain_sdnode;
1169 }
1170 Record *get_intrinsic_wo_chain_sdnode() const {
1171 return intrinsic_wo_chain_sdnode;
1172 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001173
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001174 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1175
Chris Lattner8cab0212008-01-05 22:25:12 +00001176private:
1177 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001178 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001179 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001180 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001181 void ParseDefaultOperands();
1182 void ParseInstructions();
1183 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001184 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001185 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001186 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001187 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001188
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001189 std::vector<Predicate> makePredList(ListInit *L);
1190
Craig Topper18e6b572017-06-25 17:33:49 +00001191 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +00001192 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
1193 std::map<std::string,
1194 TreePatternNode*> &InstInputs,
1195 std::map<std::string,
1196 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +00001197 std::vector<Record*> &InstImpResults);
1198};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001199
1200
1201inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1202 TreePattern &TP) const {
1203 bool MadeChange = false;
1204 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1205 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1206 return MadeChange;
1207 }
Matt Arsenault303327d2017-12-20 19:36:28 +00001208
Chris Lattner8cab0212008-01-05 22:25:12 +00001209} // end namespace llvm
1210
1211#endif