blob: afbcb10a4b66b2b23b1ce11c3cac50d4c60f4bbc [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"
Chris Lattnercabe0372010-03-15 06:00:16 +000021#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringMap.h"
Zachary Turner249dc142017-09-20 18:01:40 +000023#include "llvm/ADT/StringSet.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000024#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner1802b172010-03-19 01:07:44 +000026#include <algorithm>
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000027#include <array>
Daniel Sanders7e523672017-11-11 03:23:44 +000028#include <functional>
Chris Lattner1802b172010-03-19 01:07:44 +000029#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000030#include <set>
31#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000032
33namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000034
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000035class Record;
36class Init;
37class ListInit;
38class DagInit;
39class SDNodeInfo;
40class TreePattern;
41class TreePatternNode;
42class CodeGenDAGPatterns;
43class ComplexPattern;
44
45/// This represents a set of MVTs. Since the underlying type for the MVT
46/// is uint8_t, there are at most 256 values. To reduce the number of memory
47/// allocations and deallocations, represent the set as a sequence of bits.
48/// To reduce the allocations even further, make MachineValueTypeSet own
49/// the storage and use std::array as the bit container.
50struct MachineValueTypeSet {
51 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
52 uint8_t>::value,
53 "Change uint8_t here to the SimpleValueType's type");
54 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
55 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000056 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000057 static unsigned constexpr NumWords = Capacity/WordWidth;
58 static_assert(NumWords*WordWidth == Capacity,
59 "Capacity should be a multiple of WordWidth");
60
61 LLVM_ATTRIBUTE_ALWAYS_INLINE
62 MachineValueTypeSet() {
63 clear();
64 }
65
66 LLVM_ATTRIBUTE_ALWAYS_INLINE
67 unsigned size() const {
68 unsigned Count = 0;
69 for (WordType W : Words)
70 Count += countPopulation(W);
71 return Count;
72 }
73 LLVM_ATTRIBUTE_ALWAYS_INLINE
74 void clear() {
75 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
76 }
77 LLVM_ATTRIBUTE_ALWAYS_INLINE
78 bool empty() const {
79 for (WordType W : Words)
80 if (W != 0)
81 return false;
82 return true;
83 }
84 LLVM_ATTRIBUTE_ALWAYS_INLINE
85 unsigned count(MVT T) const {
86 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
87 }
88 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
89 bool V = count(T.SimpleTy);
90 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
91 return {*this, V};
92 }
93 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
94 for (unsigned i = 0; i != NumWords; ++i)
95 Words[i] |= S.Words[i];
96 return *this;
97 }
98 LLVM_ATTRIBUTE_ALWAYS_INLINE
99 void erase(MVT T) {
100 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
101 }
102
103 struct const_iterator {
104 // Some implementations of the C++ library require these traits to be
105 // defined.
106 using iterator_category = std::forward_iterator_tag;
107 using value_type = MVT;
108 using difference_type = ptrdiff_t;
109 using pointer = const MVT*;
110 using reference = const MVT&;
111
112 LLVM_ATTRIBUTE_ALWAYS_INLINE
113 MVT operator*() const {
114 assert(Pos != Capacity);
115 return MVT::SimpleValueType(Pos);
116 }
117 LLVM_ATTRIBUTE_ALWAYS_INLINE
118 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
119 Pos = End ? Capacity : find_from_pos(0);
120 }
121 LLVM_ATTRIBUTE_ALWAYS_INLINE
122 const_iterator &operator++() {
123 assert(Pos != Capacity);
124 Pos = find_from_pos(Pos+1);
125 return *this;
126 }
127
128 LLVM_ATTRIBUTE_ALWAYS_INLINE
129 bool operator==(const const_iterator &It) const {
130 return Set == It.Set && Pos == It.Pos;
131 }
132 LLVM_ATTRIBUTE_ALWAYS_INLINE
133 bool operator!=(const const_iterator &It) const {
134 return !operator==(It);
135 }
136
137 private:
138 unsigned find_from_pos(unsigned P) const {
139 unsigned SkipWords = P / WordWidth;
140 unsigned SkipBits = P % WordWidth;
141 unsigned Count = SkipWords * WordWidth;
142
143 // If P is in the middle of a word, process it manually here, because
144 // the trailing bits need to be masked off to use findFirstSet.
145 if (SkipBits != 0) {
146 WordType W = Set->Words[SkipWords];
147 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
148 if (W != 0)
149 return Count + findFirstSet(W);
150 Count += WordWidth;
151 SkipWords++;
152 }
153
154 for (unsigned i = SkipWords; i != NumWords; ++i) {
155 WordType W = Set->Words[i];
156 if (W != 0)
157 return Count + findFirstSet(W);
158 Count += WordWidth;
159 }
160 return Capacity;
161 }
162
163 const MachineValueTypeSet *Set;
164 unsigned Pos;
165 };
166
167 LLVM_ATTRIBUTE_ALWAYS_INLINE
168 const_iterator begin() const { return const_iterator(this, false); }
169 LLVM_ATTRIBUTE_ALWAYS_INLINE
170 const_iterator end() const { return const_iterator(this, true); }
171
172 LLVM_ATTRIBUTE_ALWAYS_INLINE
173 bool operator==(const MachineValueTypeSet &S) const {
174 return Words == S.Words;
175 }
176 LLVM_ATTRIBUTE_ALWAYS_INLINE
177 bool operator!=(const MachineValueTypeSet &S) const {
178 return !operator==(S);
179 }
180
181private:
182 friend struct const_iterator;
183 std::array<WordType,NumWords> Words;
184};
185
186struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
187 using SetType = MachineValueTypeSet;
Jim Grosbach50986b52010-12-24 05:06:32 +0000188
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000189 TypeSetByHwMode() = default;
190 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
191 TypeSetByHwMode(MVT::SimpleValueType VT)
192 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
193 TypeSetByHwMode(ValueTypeByHwMode VT)
194 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
195 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000196
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000197 SetType &getOrCreate(unsigned Mode) {
198 if (hasMode(Mode))
199 return get(Mode);
200 return Map.insert({Mode,SetType()}).first->second;
201 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000202
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000203 bool isValueTypeByHwMode(bool AllowEmpty) const;
204 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000205
206 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000207 bool isMachineValueType() const {
208 return isDefaultOnly() && Map.begin()->second.size() == 1;
209 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000210
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000211 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000212 MVT getMachineValueType() const {
213 assert(isMachineValueType());
214 return *Map.begin()->second.begin();
215 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000216
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000217 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000218
219 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000220 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000221 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000222 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000223
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000224 bool insert(const ValueTypeByHwMode &VVT);
225 bool constrain(const TypeSetByHwMode &VTS);
226 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000227 template <typename Predicate>
228 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000229
Zachary Turner249dc142017-09-20 18:01:40 +0000230 void writeToStream(raw_ostream &OS) const;
231 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000232
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000233 bool operator==(const TypeSetByHwMode &VTS) const;
234 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000235
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000236 void dump() const;
237 void validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000238
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000239private:
240 /// Intersect two sets. Return true if anything has changed.
241 bool intersect(SetType &Out, const SetType &In);
242};
Jim Grosbach50986b52010-12-24 05:06:32 +0000243
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000244raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
245
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000246struct TypeInfer {
247 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000248
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000249 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
250 return VTS.isValueTypeByHwMode(AllowEmpty);
251 }
252 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
253 bool AllowEmpty) const {
254 assert(VTS.isValueTypeByHwMode(AllowEmpty));
255 return VTS.getValueTypeByHwMode();
256 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000257
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000258 /// The protocol in the following functions (Merge*, force*, Enforce*,
259 /// expand*) is to return "true" if a change has been made, "false"
260 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000261
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000262 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
263 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
264 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
265 }
266 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
267 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
268 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000269
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000270 /// Reduce the set \p Out to have at most one element for each mode.
271 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000272
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000273 /// The following four functions ensure that upon return the set \p Out
274 /// will only contain types of the specified kind: integer, floating-point,
275 /// scalar, or vector.
276 /// If \p Out is empty, all legal types of the specified kind will be added
277 /// to it. Otherwise, all types that are not of the specified kind will be
278 /// removed from \p Out.
279 bool EnforceInteger(TypeSetByHwMode &Out);
280 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
281 bool EnforceScalar(TypeSetByHwMode &Out);
282 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000283
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000284 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
285 /// unchanged.
286 bool EnforceAny(TypeSetByHwMode &Out);
287 /// Make sure that for each type in \p Small, there exists a larger type
288 /// in \p Big.
289 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
290 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
291 /// for each type U in \p Elem, U is a scalar type.
292 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
293 /// (vector) type T in \p Vec, such that U is the element type of T.
294 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
295 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
296 const ValueTypeByHwMode &VVT);
297 /// Ensure that for each type T in \p Sub, T is a vector type, and there
298 /// exists a type U in \p Vec such that U is a vector type with the same
299 /// element type as T and at least as many elements as T.
300 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
301 TypeSetByHwMode &Sub);
302 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
303 /// 2. Ensure that for each vector type T in \p V, there exists a vector
304 /// type U in \p W, such that T and U have the same number of elements.
305 /// 3. Ensure that for each vector type U in \p W, there exists a vector
306 /// type T in \p V, such that T and U have the same number of elements
307 /// (reverse of 2).
308 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
309 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
310 /// such that T and U have equal size in bits.
311 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
312 /// such that T and U have equal size in bits (reverse of 1).
313 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000314
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000315 /// For each overloaded type (i.e. of form *Any), replace it with the
316 /// corresponding subset of legal, specific types.
317 void expandOverloads(TypeSetByHwMode &VTS);
318 void expandOverloads(TypeSetByHwMode::SetType &Out,
319 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000320
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000321 struct ValidateOnExit {
322 ValidateOnExit(TypeSetByHwMode &T) : VTS(T) {}
323 ~ValidateOnExit() { VTS.validate(); }
324 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000325 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000326
327 TreePattern &TP;
328 unsigned ForceMode; // Mode to use when set.
329 bool CodeGen = false; // Set during generation of matcher code.
330
331private:
332 TypeSetByHwMode getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000333
334 /// Cached legal types.
335 bool LegalTypesCached = false;
336 TypeSetByHwMode::SetType LegalCache = {};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000337};
Chris Lattner8cab0212008-01-05 22:25:12 +0000338
Scott Michel94420742008-03-05 17:49:05 +0000339/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000340typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000341
Chris Lattner8cab0212008-01-05 22:25:12 +0000342/// SDTypeConstraint - This is a discriminated union of constraints,
343/// corresponding to the SDTypeConstraint tablegen class in Target.td.
344struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000345 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000346
Chris Lattner8cab0212008-01-05 22:25:12 +0000347 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000348 enum {
349 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000350 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000351 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000352 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000353
Chris Lattner8cab0212008-01-05 22:25:12 +0000354 union { // The discriminated union.
355 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000356 unsigned OtherOperandNum;
357 } SDTCisSameAs_Info;
358 struct {
359 unsigned OtherOperandNum;
360 } SDTCisVTSmallerThanOp_Info;
361 struct {
362 unsigned BigOperandNum;
363 } SDTCisOpSmallerThanOp_Info;
364 struct {
365 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000366 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000367 struct {
368 unsigned OtherOperandNum;
369 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000370 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000371 unsigned OtherOperandNum;
372 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000373 struct {
374 unsigned OtherOperandNum;
375 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000376 } x;
377
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000378 // The VT for SDTCisVT and SDTCVecEltisVT.
379 // Must not be in the union because it has a non-trivial destructor.
380 ValueTypeByHwMode VVT;
381
Chris Lattner8cab0212008-01-05 22:25:12 +0000382 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
383 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000384 /// change, false otherwise. If a type contradiction is found, an error
385 /// is flagged.
Chris Lattner8cab0212008-01-05 22:25:12 +0000386 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
387 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000388};
389
390/// SDNodeInfo - One of these records is created for each SDNode instance in
391/// the target .td file. This represents the various dag nodes we will be
392/// processing.
393class SDNodeInfo {
394 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000395 StringRef EnumName;
396 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000397 unsigned Properties;
398 unsigned NumResults;
399 int NumOperands;
400 std::vector<SDTypeConstraint> TypeConstraints;
401public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000402 // Parse the specified record.
403 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000404
Chris Lattner8cab0212008-01-05 22:25:12 +0000405 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000406
Chris Lattner135091b2010-03-28 08:48:47 +0000407 /// getNumOperands - This is the number of operands required or -1 if
408 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000409 int getNumOperands() const { return NumOperands; }
410 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000411 StringRef getEnumName() const { return EnumName; }
412 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000413
Chris Lattner8cab0212008-01-05 22:25:12 +0000414 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
415 return TypeConstraints;
416 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000417
Chris Lattner99e53b32010-02-28 00:22:30 +0000418 /// getKnownType - If the type constraints on this node imply a fixed type
419 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000420 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000421 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000422
Chris Lattner8cab0212008-01-05 22:25:12 +0000423 /// hasProperty - Return true if this node has the specified property.
424 ///
425 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
426
427 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
428 /// constraints for this node to the operands of the node. This returns
429 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000430 /// found, an error is flagged.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000431 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000432};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000433
Chris Lattner514e2922011-04-17 21:38:24 +0000434/// TreePredicateFn - This is an abstraction that represents the predicates on
435/// a PatFrag node. This is a simple one-word wrapper around a pointer to
436/// provide nice accessors.
437class TreePredicateFn {
438 /// PatFragRec - This is the TreePattern for the PatFrag that we
439 /// originally came from.
440 TreePattern *PatFragRec;
441public:
442 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000443 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000444
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000445
Chris Lattner514e2922011-04-17 21:38:24 +0000446 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000447
Chris Lattner514e2922011-04-17 21:38:24 +0000448 /// isAlwaysTrue - Return true if this is a noop predicate.
449 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000450
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000451 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000452
Chris Lattner07add492011-04-18 06:22:33 +0000453 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
454 /// this is an immediate predicate. It is an error to call this on a
455 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000456 std::string getImmediatePredicateCode() const {
457 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000458 assert(!Result.empty() && "Isn't an immediate pattern!");
459 return Result;
460 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000461
Chris Lattner514e2922011-04-17 21:38:24 +0000462 bool operator==(const TreePredicateFn &RHS) const {
463 return PatFragRec == RHS.PatFragRec;
464 }
465
466 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
467
468 /// Return the name to use in the generated code to reference this, this is
469 /// "Predicate_foo" if from a pattern fragment "foo".
470 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000471
Chris Lattner514e2922011-04-17 21:38:24 +0000472 /// getCodeToRunOnSDNode - Return the code for the function body that
473 /// evaluates this predicate. The argument is expected to be in "Node",
474 /// not N. This handles casting and conversion to a concrete node type as
475 /// appropriate.
476 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000477
Daniel Sanders649c5852017-10-13 20:42:18 +0000478 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000479 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000480
Daniel Sanders11300ce2017-10-13 21:28:03 +0000481 /// Get a string that describes the type returned by getImmType() but is
482 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000483 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000484
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000485 // Is the desired predefined predicate for a load?
486 bool isLoad() const;
487 // Is the desired predefined predicate for a store?
488 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000489 // Is the desired predefined predicate for an atomic?
490 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000491
492 /// Is this predicate the predefined unindexed load predicate?
493 /// Is this predicate the predefined unindexed store predicate?
494 bool isUnindexed() const;
495 /// Is this predicate the predefined non-extending load predicate?
496 bool isNonExtLoad() const;
497 /// Is this predicate the predefined any-extend load predicate?
498 bool isAnyExtLoad() const;
499 /// Is this predicate the predefined sign-extend load predicate?
500 bool isSignExtLoad() const;
501 /// Is this predicate the predefined zero-extend load predicate?
502 bool isZeroExtLoad() const;
503 /// Is this predicate the predefined non-truncating store predicate?
504 bool isNonTruncStore() const;
505 /// Is this predicate the predefined truncating store predicate?
506 bool isTruncStore() const;
507
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000508 /// Is this predicate the predefined monotonic atomic predicate?
509 bool isAtomicOrderingMonotonic() const;
510 /// Is this predicate the predefined acquire atomic predicate?
511 bool isAtomicOrderingAcquire() const;
512 /// Is this predicate the predefined release atomic predicate?
513 bool isAtomicOrderingRelease() const;
514 /// Is this predicate the predefined acquire-release atomic predicate?
515 bool isAtomicOrderingAcquireRelease() const;
516 /// Is this predicate the predefined sequentially consistent atomic predicate?
517 bool isAtomicOrderingSequentiallyConsistent() const;
518
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000519 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
520 bool isAtomicOrderingAcquireOrStronger() const;
521 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
522 bool isAtomicOrderingWeakerThanAcquire() const;
523
524 /// Is this predicate the predefined release-or-stronger atomic predicate?
525 bool isAtomicOrderingReleaseOrStronger() const;
526 /// Is this predicate the predefined weaker-than-release atomic predicate?
527 bool isAtomicOrderingWeakerThanRelease() const;
528
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000529 /// If non-null, indicates that this predicate is a predefined memory VT
530 /// predicate for a load/store and returns the ValueType record for the memory VT.
531 Record *getMemoryVT() const;
532 /// If non-null, indicates that this predicate is a predefined memory VT
533 /// predicate (checking only the scalar type) for load/store and returns the
534 /// ValueType record for the memory VT.
535 Record *getScalarMemoryVT() const;
536
Chris Lattner514e2922011-04-17 21:38:24 +0000537private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000538 bool hasPredCode() const;
539 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000540 std::string getPredCode() const;
541 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000542 bool immCodeUsesAPInt() const;
543 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000544
545 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000546};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000547
Chris Lattner8cab0212008-01-05 22:25:12 +0000548
549/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
550/// patterns), and as such should be ref counted. We currently just leak all
551/// TreePatternNode objects!
552class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000553 /// The type of each node result. Before and during type inference, each
554 /// result may be a set of possible types. After (successful) type inference,
555 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000556 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000557
Chris Lattner8cab0212008-01-05 22:25:12 +0000558 /// Operator - The Record for the operator if this is an interior node (not
559 /// a leaf).
560 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000561
Chris Lattner8cab0212008-01-05 22:25:12 +0000562 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
563 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000564 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000565
Chris Lattner8cab0212008-01-05 22:25:12 +0000566 /// Name - The name given to this node with the :$foo notation.
567 ///
568 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000569
Dan Gohman6e979022008-10-15 06:17:21 +0000570 /// PredicateFns - The predicate functions to execute on this node to check
571 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000572 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000573
Chris Lattner8cab0212008-01-05 22:25:12 +0000574 /// TransformFn - The transformation function to execute on this node before
575 /// it can be substituted into the resulting instruction on a pattern match.
576 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000577
Chris Lattner8cab0212008-01-05 22:25:12 +0000578 std::vector<TreePatternNode*> Children;
579public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000580 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000581 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000582 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000583 Types.resize(NumResults);
584 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000585 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000586 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000587 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000588 }
589 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000590
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000591 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000592 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000593 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000594
Craig Topperada08572014-04-16 04:21:27 +0000595 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000596
Chris Lattnercabe0372010-03-15 06:00:16 +0000597 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000598 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000599 ValueTypeByHwMode getType(unsigned ResNo) const {
600 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000601 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000602 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
603 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
604 return Types[ResNo];
605 }
606 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
607 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
608 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
609 return Types[ResNo].getMachineValueType().SimpleTy;
610 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000611
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000612 bool hasConcreteType(unsigned ResNo) const {
613 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000614 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000615 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
616 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000617 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000618
David Greeneaf8ee2c2011-07-29 22:43:06 +0000619 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000620 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000621
Chris Lattner8cab0212008-01-05 22:25:12 +0000622 unsigned getNumChildren() const { return Children.size(); }
623 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
624 void setChild(unsigned i, TreePatternNode *N) {
625 Children[i] = N;
626 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000627
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000628 /// hasChild - Return true if N is any of our children.
629 bool hasChild(const TreePatternNode *N) const {
630 for (unsigned i = 0, e = Children.size(); i != e; ++i)
631 if (Children[i] == N) return true;
632 return false;
633 }
Chris Lattner89c65662008-01-06 05:36:50 +0000634
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000635 bool hasProperTypeByHwMode() const;
636 bool hasPossibleType() const;
637 bool setDefaultMode(unsigned Mode);
638
Chris Lattner514e2922011-04-17 21:38:24 +0000639 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000640
Chris Lattner514e2922011-04-17 21:38:24 +0000641 const std::vector<TreePredicateFn> &getPredicateFns() const {
642 return PredicateFns;
643 }
Dan Gohman6e979022008-10-15 06:17:21 +0000644 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000645 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000646 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
647 PredicateFns = Fns;
648 }
Chris Lattner514e2922011-04-17 21:38:24 +0000649 void addPredicateFn(const TreePredicateFn &Fn) {
650 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000651 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000652 PredicateFns.push_back(Fn);
653 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000654
655 Record *getTransformFn() const { return TransformFn; }
656 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000657
Chris Lattner89c65662008-01-06 05:36:50 +0000658 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
659 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
660 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000661
Chris Lattner53c39ba2010-02-14 22:22:58 +0000662 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
663 /// return the ComplexPattern information, otherwise return null.
664 const ComplexPattern *
665 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
666
Tim Northoverc807a172014-05-20 11:52:46 +0000667 /// Returns the number of MachineInstr operands that would be produced by this
668 /// node if it mapped directly to an output Instruction's
669 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
670 /// for Operands; otherwise 1.
671 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
672
Chris Lattner53c39ba2010-02-14 22:22:58 +0000673 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000674 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000675
Chris Lattner53c39ba2010-02-14 22:22:58 +0000676 /// TreeHasProperty - Return true if any node in this tree has the specified
677 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000678 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000679
Evan Cheng49bad4c2008-06-16 20:29:38 +0000680 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
681 /// marked isCommutative.
682 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000683
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000684 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000685 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000686
Chris Lattner8cab0212008-01-05 22:25:12 +0000687public: // Higher level manipulation routines.
688
689 /// clone - Return a new copy of this tree.
690 ///
691 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000692
693 /// RemoveAllTypes - Recursively strip all the types of this tree.
694 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000695
Chris Lattner8cab0212008-01-05 22:25:12 +0000696 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
697 /// the specified node. For this comparison, all of the state of the node
698 /// is considered, except for the assigned name. Nodes with differing names
699 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000700 bool isIsomorphicTo(const TreePatternNode *N,
701 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000702
Chris Lattner8cab0212008-01-05 22:25:12 +0000703 /// SubstituteFormalArguments - Replace the formal arguments in this tree
704 /// with actual values specified by ArgMap.
705 void SubstituteFormalArguments(std::map<std::string,
706 TreePatternNode*> &ArgMap);
707
708 /// InlinePatternFragments - If this pattern refers to any pattern
709 /// fragments, inline them into place, giving us a pattern without any
710 /// PatFrag references.
711 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000712
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000713 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000714 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000715 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000716 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000717
Chris Lattner8cab0212008-01-05 22:25:12 +0000718 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000719 /// information. If N already contains a conflicting type, then flag an
720 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000721 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000722 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
723 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000724 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000725 TreePattern &TP);
726 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
727 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000728
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000729 // Update node type with types inferred from an instruction operand or result
730 // def from the ins/outs lists.
731 // Return true if the type changed.
732 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
733
Chris Lattner8cab0212008-01-05 22:25:12 +0000734 /// ContainsUnresolvedType - Return true if this tree contains any
735 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000736 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000737
Chris Lattner8cab0212008-01-05 22:25:12 +0000738 /// canPatternMatch - If it is impossible for this pattern to match on this
739 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000740 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000741};
742
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000743inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
744 TPN.print(OS);
745 return OS;
746}
Jim Grosbach50986b52010-12-24 05:06:32 +0000747
Chris Lattner8cab0212008-01-05 22:25:12 +0000748
749/// TreePattern - Represent a pattern, used for instructions, pattern
750/// fragments, etc.
751///
752class TreePattern {
753 /// Trees - The list of pattern trees which corresponds to this pattern.
754 /// Note that PatFrag's only have a single tree.
755 ///
David Blaikiecf195302014-11-17 22:55:41 +0000756 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000757
Chris Lattnercabe0372010-03-15 06:00:16 +0000758 /// NamedNodes - This is all of the nodes that have names in the trees in this
759 /// pattern.
760 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000761
Chris Lattner8cab0212008-01-05 22:25:12 +0000762 /// TheRecord - The actual TableGen record corresponding to this pattern.
763 ///
764 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000765
Chris Lattner8cab0212008-01-05 22:25:12 +0000766 /// Args - This is a list of all of the arguments to this pattern (for
767 /// PatFrag patterns), which are the 'node' markers in this pattern.
768 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000769
Chris Lattner8cab0212008-01-05 22:25:12 +0000770 /// CDP - the top-level object coordinating this madness.
771 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000772 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000773
774 /// isInputPattern - True if this is an input pattern, something to match.
775 /// False if this is an output pattern, something to emit.
776 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000777
778 /// hasError - True if the currently processed nodes have unresolvable types
779 /// or other non-fatal errors
780 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000781
782 /// It's important that the usage of operands in ComplexPatterns is
783 /// consistent: each named operand can be defined by at most one
784 /// ComplexPattern. This records the ComplexPattern instance and the operand
785 /// number for each operand encountered in a ComplexPattern to aid in that
786 /// check.
787 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000788
789 TypeInfer Infer;
790
Chris Lattner8cab0212008-01-05 22:25:12 +0000791public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000792
Chris Lattner8cab0212008-01-05 22:25:12 +0000793 /// TreePattern constructor - Parse the specified DagInits into the
794 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000795 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000796 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000797 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000798 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000799 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
800 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000801
Chris Lattner8cab0212008-01-05 22:25:12 +0000802 /// getTrees - Return the tree patterns which corresponds to this pattern.
803 ///
David Blaikiecf195302014-11-17 22:55:41 +0000804 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000805 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000806 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Daniel Sanders7e523672017-11-11 03:23:44 +0000807 void setTree(unsigned i, TreePatternNode *Tree) { Trees[i] = Tree; }
David Blaikiecf195302014-11-17 22:55:41 +0000808 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000809 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
810 return Trees[0];
811 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000812
Chris Lattnercabe0372010-03-15 06:00:16 +0000813 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
814 if (NamedNodes.empty())
815 ComputeNamedNodes();
816 return NamedNodes;
817 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000818
Chris Lattner8cab0212008-01-05 22:25:12 +0000819 /// getRecord - Return the actual TableGen record corresponding to this
820 /// pattern.
821 ///
822 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000823
Chris Lattner8cab0212008-01-05 22:25:12 +0000824 unsigned getNumArgs() const { return Args.size(); }
825 const std::string &getArgName(unsigned i) const {
826 assert(i < Args.size() && "Argument reference out of range!");
827 return Args[i];
828 }
829 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000830
Chris Lattnerab3242f2008-01-06 01:10:31 +0000831 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000832
833 /// InlinePatternFragments - If this pattern refers to any pattern
834 /// fragments, inline them into place, giving us a pattern without any
835 /// PatFrag references.
836 void InlinePatternFragments() {
837 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000838 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000839 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000840
Chris Lattner8cab0212008-01-05 22:25:12 +0000841 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000842 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000843 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000844 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000845 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000846
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000847 /// error - If this is the first error in the current resolution step,
848 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000849 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000850 bool hasError() const {
851 return HasError;
852 }
853 void resetError() {
854 HasError = false;
855 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000856
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000857 TypeInfer &getInfer() { return Infer; }
858
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000859 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000860 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000861
Chris Lattner8cab0212008-01-05 22:25:12 +0000862private:
David Blaikiecf195302014-11-17 22:55:41 +0000863 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000864 void ComputeNamedNodes();
865 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000866};
867
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000868
869inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
870 const TypeSetByHwMode &InTy,
871 TreePattern &TP) {
872 TypeSetByHwMode VTS(InTy);
873 TP.getInfer().expandOverloads(VTS);
874 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
875}
876
877inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
878 MVT::SimpleValueType InTy,
879 TreePattern &TP) {
880 TypeSetByHwMode VTS(InTy);
881 TP.getInfer().expandOverloads(VTS);
882 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
883}
884
885inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
886 ValueTypeByHwMode InTy,
887 TreePattern &TP) {
888 TypeSetByHwMode VTS(InTy);
889 TP.getInfer().expandOverloads(VTS);
890 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
891}
892
893
Tom Stellardb7246a72012-09-06 14:15:52 +0000894/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
895/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000896struct DAGDefaultOperand {
897 std::vector<TreePatternNode*> DefaultOps;
898};
899
900class DAGInstruction {
901 TreePattern *Pattern;
902 std::vector<Record*> Results;
903 std::vector<Record*> Operands;
904 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000905 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000906public:
907 DAGInstruction(TreePattern *TP,
908 const std::vector<Record*> &results,
909 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000910 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000911 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000912 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000913
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000914 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000915 unsigned getNumResults() const { return Results.size(); }
916 unsigned getNumOperands() const { return Operands.size(); }
917 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000918 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000919
David Blaikiecf195302014-11-17 22:55:41 +0000920 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000921
Chris Lattner8cab0212008-01-05 22:25:12 +0000922 Record *getResult(unsigned RN) const {
923 assert(RN < Results.size());
924 return Results[RN];
925 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000926
Chris Lattner8cab0212008-01-05 22:25:12 +0000927 Record *getOperand(unsigned ON) const {
928 assert(ON < Operands.size());
929 return Operands[ON];
930 }
931
932 Record *getImpResult(unsigned RN) const {
933 assert(RN < ImpResults.size());
934 return ImpResults[RN];
935 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000936
David Blaikiecf195302014-11-17 22:55:41 +0000937 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000938};
Jim Grosbach50986b52010-12-24 05:06:32 +0000939
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000940/// This class represents a condition that has to be satisfied for a pattern
941/// to be tried. It is a generalization of a class "Pattern" from Target.td:
942/// in addition to the Target.td's predicates, this class can also represent
943/// conditions associated with HW modes. Both types will eventually become
944/// strings containing C++ code to be executed, the difference is in how
945/// these strings are generated.
946class Predicate {
947public:
948 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
949 assert(R->isSubClassOf("Predicate") &&
950 "Predicate objects should only be created for records derived"
951 "from Predicate class");
952 }
953 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
954 IfCond(C), IsHwMode(true) {}
955
956 /// Return a string which contains the C++ condition code that will serve
957 /// as a predicate during instruction selection.
958 std::string getCondString() const {
959 // The string will excute in a subclass of SelectionDAGISel.
960 // Cast to std::string explicitly to avoid ambiguity with StringRef.
961 std::string C = IsHwMode
962 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
963 : std::string(Def->getValueAsString("CondString"));
964 return IfCond ? C : "!("+C+')';
965 }
966 bool operator==(const Predicate &P) const {
967 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
968 }
969 bool operator<(const Predicate &P) const {
970 if (IsHwMode != P.IsHwMode)
971 return IsHwMode < P.IsHwMode;
972 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
973 if (IfCond != P.IfCond)
974 return IfCond < P.IfCond;
975 if (Def)
976 return LessRecord()(Def, P.Def);
977 return Features < P.Features;
978 }
979 Record *Def; ///< Predicate definition from .td file, null for
980 ///< HW modes.
981 std::string Features; ///< Feature string for HW mode.
982 bool IfCond; ///< The boolean value that the condition has to
983 ///< evaluate to for this predicate to be true.
984 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
985};
986
Chris Lattnerab3242f2008-01-06 01:10:31 +0000987/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000988/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000989class PatternToMatch {
990public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000991 PatternToMatch(Record *srcrecord, const std::vector<Predicate> &preds,
992 TreePatternNode *src, TreePatternNode *dst,
993 const std::vector<Record*> &dstregs,
994 int complexity, unsigned uid, unsigned setmode = 0)
995 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
996 Predicates(preds), Dstregs(std::move(dstregs)),
997 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
998
999 PatternToMatch(Record *srcrecord, std::vector<Predicate> &&preds,
1000 TreePatternNode *src, TreePatternNode *dst,
1001 std::vector<Record*> &&dstregs,
1002 int complexity, unsigned uid, unsigned setmode = 0)
1003 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
1004 Predicates(preds), Dstregs(std::move(dstregs)),
1005 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001006
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001007 Record *SrcRecord; // Originating Record for the pattern.
Chris Lattner8cab0212008-01-05 22:25:12 +00001008 TreePatternNode *SrcPattern; // Source pattern to match.
1009 TreePatternNode *DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001010 std::vector<Predicate> Predicates; // Top level predicate conditions
1011 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001012 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001013 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001014 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001015 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001016
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001017 Record *getSrcRecord() const { return SrcRecord; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001018 TreePatternNode *getSrcPattern() const { return SrcPattern; }
1019 TreePatternNode *getDstPattern() const { return DstPattern; }
1020 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001021 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001022 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001023
1024 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001025
Chris Lattner05925fe2010-03-29 01:40:38 +00001026 /// Compute the complexity metric for the input pattern. This roughly
1027 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001028 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001029};
1030
Chris Lattnerab3242f2008-01-06 01:10:31 +00001031class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001032 RecordKeeper &Records;
1033 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001034 CodeGenIntrinsicTable Intrinsics;
1035 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001036
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001037 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001038 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1039 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001040 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001041 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1042 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001043 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1044 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001045
Chris Lattner8cab0212008-01-05 22:25:12 +00001046 // Specific SDNode definitions:
1047 Record *intrinsic_void_sdnode;
1048 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001049
Chris Lattner8cab0212008-01-05 22:25:12 +00001050 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1051 /// value is the pattern to match, the second pattern is the result to
1052 /// emit.
1053 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001054
1055 TypeSetByHwMode LegalVTS;
1056
Daniel Sanders7e523672017-11-11 03:23:44 +00001057 using PatternRewriterFn = std::function<void (TreePattern *)>;
1058 PatternRewriterFn PatternRewriter;
1059
Chris Lattner8cab0212008-01-05 22:25:12 +00001060public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001061 CodeGenDAGPatterns(RecordKeeper &R,
1062 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001063
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001064 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001065 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001066 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001067
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001068 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001069
Chris Lattner8cab0212008-01-05 22:25:12 +00001070 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001071 auto F = SDNodes.find(R);
1072 assert(F != SDNodes.end() && "Unknown node!");
1073 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001074 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001075
Chris Lattnercc43e792008-01-05 22:54:53 +00001076 // Node transformation lookups.
1077 typedef std::pair<Record*, std::string> NodeXForm;
1078 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001079 auto F = SDNodeXForms.find(R);
1080 assert(F != SDNodeXForms.end() && "Invalid transform!");
1081 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001082 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001083
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001084 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001085 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001086 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1087 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1088
Jim Grosbach50986b52010-12-24 05:06:32 +00001089
Chris Lattner8cab0212008-01-05 22:25:12 +00001090 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001091 auto F = ComplexPatterns.find(R);
1092 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1093 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001094 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001095
Chris Lattner8cab0212008-01-05 22:25:12 +00001096 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1097 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1098 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001099 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1100 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001101 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001102 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001103
Chris Lattner8cab0212008-01-05 22:25:12 +00001104 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001105 if (IID-1 < Intrinsics.size())
1106 return Intrinsics[IID-1];
1107 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1108 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001109 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001110 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001111
Chris Lattner8cab0212008-01-05 22:25:12 +00001112 unsigned getIntrinsicID(Record *R) const {
1113 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1114 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001115 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1116 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001117 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001118 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001119
Chris Lattner7ed81692010-02-18 06:47:49 +00001120 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001121 auto F = DefaultOperands.find(R);
1122 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1123 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001124 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001125
Chris Lattner8cab0212008-01-05 22:25:12 +00001126 // Pattern Fragment information.
1127 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001128 auto F = PatternFragments.find(R);
1129 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1130 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001131 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001132 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001133 auto F = PatternFragments.find(R);
1134 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001135 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001136 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001137 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001138
David Blaikiefcacc742014-11-13 21:56:57 +00001139 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1140 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001141 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1142 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001143 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001144
1145 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001146 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1147 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1148 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001149 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001150
Ahmed Bougacha14107512013-10-28 18:07:21 +00001151 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1152 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1153 const DAGInstruction &parseInstructionPattern(
1154 CodeGenInstruction &CGI, ListInit *Pattern,
1155 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001156
Chris Lattner8cab0212008-01-05 22:25:12 +00001157 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001158 auto F = Instructions.find(R);
1159 assert(F != Instructions.end() && "Unknown instruction!");
1160 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001161 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001162
Chris Lattner8cab0212008-01-05 22:25:12 +00001163 Record *get_intrinsic_void_sdnode() const {
1164 return intrinsic_void_sdnode;
1165 }
1166 Record *get_intrinsic_w_chain_sdnode() const {
1167 return intrinsic_w_chain_sdnode;
1168 }
1169 Record *get_intrinsic_wo_chain_sdnode() const {
1170 return intrinsic_wo_chain_sdnode;
1171 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001172
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001173 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1174
Chris Lattner8cab0212008-01-05 22:25:12 +00001175private:
1176 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001177 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001178 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001179 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001180 void ParseDefaultOperands();
1181 void ParseInstructions();
1182 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001183 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001184 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001185 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001186 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001187
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001188 std::vector<Predicate> makePredList(ListInit *L);
1189
Craig Topper18e6b572017-06-25 17:33:49 +00001190 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +00001191 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
1192 std::map<std::string,
1193 TreePatternNode*> &InstInputs,
1194 std::map<std::string,
1195 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +00001196 std::vector<Record*> &InstImpResults);
1197};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001198
1199
1200inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1201 TreePattern &TP) const {
1202 bool MadeChange = false;
1203 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1204 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1205 return MadeChange;
1206 }
Chris Lattner8cab0212008-01-05 22:25:12 +00001207} // end namespace llvm
1208
1209#endif