blob: d3bb99a5a8fee98463692fab6792c299cda694ff [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;
489
490 /// Is this predicate the predefined unindexed load predicate?
491 /// Is this predicate the predefined unindexed store predicate?
492 bool isUnindexed() const;
493 /// Is this predicate the predefined non-extending load predicate?
494 bool isNonExtLoad() const;
495 /// Is this predicate the predefined any-extend load predicate?
496 bool isAnyExtLoad() const;
497 /// Is this predicate the predefined sign-extend load predicate?
498 bool isSignExtLoad() const;
499 /// Is this predicate the predefined zero-extend load predicate?
500 bool isZeroExtLoad() const;
501 /// Is this predicate the predefined non-truncating store predicate?
502 bool isNonTruncStore() const;
503 /// Is this predicate the predefined truncating store predicate?
504 bool isTruncStore() const;
505
506 /// If non-null, indicates that this predicate is a predefined memory VT
507 /// predicate for a load/store and returns the ValueType record for the memory VT.
508 Record *getMemoryVT() const;
509 /// If non-null, indicates that this predicate is a predefined memory VT
510 /// predicate (checking only the scalar type) for load/store and returns the
511 /// ValueType record for the memory VT.
512 Record *getScalarMemoryVT() const;
513
Chris Lattner514e2922011-04-17 21:38:24 +0000514private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000515 bool hasPredCode() const;
516 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000517 std::string getPredCode() const;
518 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000519 bool immCodeUsesAPInt() const;
520 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000521
522 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000523};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000524
Chris Lattner8cab0212008-01-05 22:25:12 +0000525
526/// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
527/// patterns), and as such should be ref counted. We currently just leak all
528/// TreePatternNode objects!
529class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000530 /// The type of each node result. Before and during type inference, each
531 /// result may be a set of possible types. After (successful) type inference,
532 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000533 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000534
Chris Lattner8cab0212008-01-05 22:25:12 +0000535 /// Operator - The Record for the operator if this is an interior node (not
536 /// a leaf).
537 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000538
Chris Lattner8cab0212008-01-05 22:25:12 +0000539 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
540 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000541 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000542
Chris Lattner8cab0212008-01-05 22:25:12 +0000543 /// Name - The name given to this node with the :$foo notation.
544 ///
545 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000546
Dan Gohman6e979022008-10-15 06:17:21 +0000547 /// PredicateFns - The predicate functions to execute on this node to check
548 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000549 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000550
Chris Lattner8cab0212008-01-05 22:25:12 +0000551 /// TransformFn - The transformation function to execute on this node before
552 /// it can be substituted into the resulting instruction on a pattern match.
553 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000554
Chris Lattner8cab0212008-01-05 22:25:12 +0000555 std::vector<TreePatternNode*> Children;
556public:
Chris Lattnerf1447252010-03-19 21:37:09 +0000557 TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000558 unsigned NumResults)
Craig Topperada08572014-04-16 04:21:27 +0000559 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000560 Types.resize(NumResults);
561 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000562 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000563 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000564 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000565 }
566 ~TreePatternNode();
Jim Grosbach50986b52010-12-24 05:06:32 +0000567
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000568 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000569 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000570 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000571
Craig Topperada08572014-04-16 04:21:27 +0000572 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000573
Chris Lattnercabe0372010-03-15 06:00:16 +0000574 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000575 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000576 ValueTypeByHwMode getType(unsigned ResNo) const {
577 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000578 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000579 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
580 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
581 return Types[ResNo];
582 }
583 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
584 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
585 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
586 return Types[ResNo].getMachineValueType().SimpleTy;
587 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000588
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000589 bool hasConcreteType(unsigned ResNo) const {
590 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000591 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000592 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
593 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000594 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000595
David Greeneaf8ee2c2011-07-29 22:43:06 +0000596 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000597 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000598
Chris Lattner8cab0212008-01-05 22:25:12 +0000599 unsigned getNumChildren() const { return Children.size(); }
600 TreePatternNode *getChild(unsigned N) const { return Children[N]; }
601 void setChild(unsigned i, TreePatternNode *N) {
602 Children[i] = N;
603 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000604
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000605 /// hasChild - Return true if N is any of our children.
606 bool hasChild(const TreePatternNode *N) const {
607 for (unsigned i = 0, e = Children.size(); i != e; ++i)
608 if (Children[i] == N) return true;
609 return false;
610 }
Chris Lattner89c65662008-01-06 05:36:50 +0000611
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000612 bool hasProperTypeByHwMode() const;
613 bool hasPossibleType() const;
614 bool setDefaultMode(unsigned Mode);
615
Chris Lattner514e2922011-04-17 21:38:24 +0000616 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000617
Chris Lattner514e2922011-04-17 21:38:24 +0000618 const std::vector<TreePredicateFn> &getPredicateFns() const {
619 return PredicateFns;
620 }
Dan Gohman6e979022008-10-15 06:17:21 +0000621 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000622 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000623 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
624 PredicateFns = Fns;
625 }
Chris Lattner514e2922011-04-17 21:38:24 +0000626 void addPredicateFn(const TreePredicateFn &Fn) {
627 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000628 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000629 PredicateFns.push_back(Fn);
630 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000631
632 Record *getTransformFn() const { return TransformFn; }
633 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000634
Chris Lattner89c65662008-01-06 05:36:50 +0000635 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
636 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
637 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000638
Chris Lattner53c39ba2010-02-14 22:22:58 +0000639 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
640 /// return the ComplexPattern information, otherwise return null.
641 const ComplexPattern *
642 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
643
Tim Northoverc807a172014-05-20 11:52:46 +0000644 /// Returns the number of MachineInstr operands that would be produced by this
645 /// node if it mapped directly to an output Instruction's
646 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
647 /// for Operands; otherwise 1.
648 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
649
Chris Lattner53c39ba2010-02-14 22:22:58 +0000650 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000651 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000652
Chris Lattner53c39ba2010-02-14 22:22:58 +0000653 /// TreeHasProperty - Return true if any node in this tree has the specified
654 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000655 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000656
Evan Cheng49bad4c2008-06-16 20:29:38 +0000657 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
658 /// marked isCommutative.
659 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000660
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000661 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000662 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000663
Chris Lattner8cab0212008-01-05 22:25:12 +0000664public: // Higher level manipulation routines.
665
666 /// clone - Return a new copy of this tree.
667 ///
668 TreePatternNode *clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000669
670 /// RemoveAllTypes - Recursively strip all the types of this tree.
671 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000672
Chris Lattner8cab0212008-01-05 22:25:12 +0000673 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
674 /// the specified node. For this comparison, all of the state of the node
675 /// is considered, except for the assigned name. Nodes with differing names
676 /// that are otherwise identical are considered isomorphic.
Scott Michel94420742008-03-05 17:49:05 +0000677 bool isIsomorphicTo(const TreePatternNode *N,
678 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000679
Chris Lattner8cab0212008-01-05 22:25:12 +0000680 /// SubstituteFormalArguments - Replace the formal arguments in this tree
681 /// with actual values specified by ArgMap.
682 void SubstituteFormalArguments(std::map<std::string,
683 TreePatternNode*> &ArgMap);
684
685 /// InlinePatternFragments - If this pattern refers to any pattern
686 /// fragments, inline them into place, giving us a pattern without any
687 /// PatFrag references.
688 TreePatternNode *InlinePatternFragments(TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000689
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000690 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000691 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000692 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000693 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000694
Chris Lattner8cab0212008-01-05 22:25:12 +0000695 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000696 /// information. If N already contains a conflicting type, then flag an
697 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000698 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000699 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
700 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000701 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000702 TreePattern &TP);
703 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
704 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000705
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000706 // Update node type with types inferred from an instruction operand or result
707 // def from the ins/outs lists.
708 // Return true if the type changed.
709 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
710
Chris Lattner8cab0212008-01-05 22:25:12 +0000711 /// ContainsUnresolvedType - Return true if this tree contains any
712 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000713 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000714
Chris Lattner8cab0212008-01-05 22:25:12 +0000715 /// canPatternMatch - If it is impossible for this pattern to match on this
716 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000717 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000718};
719
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000720inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
721 TPN.print(OS);
722 return OS;
723}
Jim Grosbach50986b52010-12-24 05:06:32 +0000724
Chris Lattner8cab0212008-01-05 22:25:12 +0000725
726/// TreePattern - Represent a pattern, used for instructions, pattern
727/// fragments, etc.
728///
729class TreePattern {
730 /// Trees - The list of pattern trees which corresponds to this pattern.
731 /// Note that PatFrag's only have a single tree.
732 ///
David Blaikiecf195302014-11-17 22:55:41 +0000733 std::vector<TreePatternNode*> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000734
Chris Lattnercabe0372010-03-15 06:00:16 +0000735 /// NamedNodes - This is all of the nodes that have names in the trees in this
736 /// pattern.
737 StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000738
Chris Lattner8cab0212008-01-05 22:25:12 +0000739 /// TheRecord - The actual TableGen record corresponding to this pattern.
740 ///
741 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000742
Chris Lattner8cab0212008-01-05 22:25:12 +0000743 /// Args - This is a list of all of the arguments to this pattern (for
744 /// PatFrag patterns), which are the 'node' markers in this pattern.
745 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000746
Chris Lattner8cab0212008-01-05 22:25:12 +0000747 /// CDP - the top-level object coordinating this madness.
748 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000749 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000750
751 /// isInputPattern - True if this is an input pattern, something to match.
752 /// False if this is an output pattern, something to emit.
753 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000754
755 /// hasError - True if the currently processed nodes have unresolvable types
756 /// or other non-fatal errors
757 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000758
759 /// It's important that the usage of operands in ComplexPatterns is
760 /// consistent: each named operand can be defined by at most one
761 /// ComplexPattern. This records the ComplexPattern instance and the operand
762 /// number for each operand encountered in a ComplexPattern to aid in that
763 /// check.
764 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000765
766 TypeInfer Infer;
767
Chris Lattner8cab0212008-01-05 22:25:12 +0000768public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000769
Chris Lattner8cab0212008-01-05 22:25:12 +0000770 /// TreePattern constructor - Parse the specified DagInits into the
771 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000772 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000773 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000774 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000775 CodeGenDAGPatterns &ise);
David Blaikiecf195302014-11-17 22:55:41 +0000776 TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
777 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000778
Chris Lattner8cab0212008-01-05 22:25:12 +0000779 /// getTrees - Return the tree patterns which corresponds to this pattern.
780 ///
David Blaikiecf195302014-11-17 22:55:41 +0000781 const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000782 unsigned getNumTrees() const { return Trees.size(); }
David Blaikiecf195302014-11-17 22:55:41 +0000783 TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
Daniel Sanders7e523672017-11-11 03:23:44 +0000784 void setTree(unsigned i, TreePatternNode *Tree) { Trees[i] = Tree; }
David Blaikiecf195302014-11-17 22:55:41 +0000785 TreePatternNode *getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000786 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
787 return Trees[0];
788 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000789
Chris Lattnercabe0372010-03-15 06:00:16 +0000790 const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
791 if (NamedNodes.empty())
792 ComputeNamedNodes();
793 return NamedNodes;
794 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000795
Chris Lattner8cab0212008-01-05 22:25:12 +0000796 /// getRecord - Return the actual TableGen record corresponding to this
797 /// pattern.
798 ///
799 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000800
Chris Lattner8cab0212008-01-05 22:25:12 +0000801 unsigned getNumArgs() const { return Args.size(); }
802 const std::string &getArgName(unsigned i) const {
803 assert(i < Args.size() && "Argument reference out of range!");
804 return Args[i];
805 }
806 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000807
Chris Lattnerab3242f2008-01-06 01:10:31 +0000808 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000809
810 /// InlinePatternFragments - If this pattern refers to any pattern
811 /// fragments, inline them into place, giving us a pattern without any
812 /// PatFrag references.
813 void InlinePatternFragments() {
814 for (unsigned i = 0, e = Trees.size(); i != e; ++i)
David Blaikiecf195302014-11-17 22:55:41 +0000815 Trees[i] = Trees[i]->InlinePatternFragments(*this);
Chris Lattner8cab0212008-01-05 22:25:12 +0000816 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000817
Chris Lattner8cab0212008-01-05 22:25:12 +0000818 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000819 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000820 /// otherwise. Bail out if a type contradiction is found.
Chris Lattnercabe0372010-03-15 06:00:16 +0000821 bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
Craig Topperada08572014-04-16 04:21:27 +0000822 *NamedTypes=nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000823
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000824 /// error - If this is the first error in the current resolution step,
825 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000826 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000827 bool hasError() const {
828 return HasError;
829 }
830 void resetError() {
831 HasError = false;
832 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000833
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000834 TypeInfer &getInfer() { return Infer; }
835
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000836 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000837 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000838
Chris Lattner8cab0212008-01-05 22:25:12 +0000839private:
David Blaikiecf195302014-11-17 22:55:41 +0000840 TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000841 void ComputeNamedNodes();
842 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000843};
844
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000845
846inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
847 const TypeSetByHwMode &InTy,
848 TreePattern &TP) {
849 TypeSetByHwMode VTS(InTy);
850 TP.getInfer().expandOverloads(VTS);
851 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
852}
853
854inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
855 MVT::SimpleValueType InTy,
856 TreePattern &TP) {
857 TypeSetByHwMode VTS(InTy);
858 TP.getInfer().expandOverloads(VTS);
859 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
860}
861
862inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
863 ValueTypeByHwMode InTy,
864 TreePattern &TP) {
865 TypeSetByHwMode VTS(InTy);
866 TP.getInfer().expandOverloads(VTS);
867 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
868}
869
870
Tom Stellardb7246a72012-09-06 14:15:52 +0000871/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
872/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000873struct DAGDefaultOperand {
874 std::vector<TreePatternNode*> DefaultOps;
875};
876
877class DAGInstruction {
878 TreePattern *Pattern;
879 std::vector<Record*> Results;
880 std::vector<Record*> Operands;
881 std::vector<Record*> ImpResults;
David Blaikiecf195302014-11-17 22:55:41 +0000882 TreePatternNode *ResultPattern;
Chris Lattner8cab0212008-01-05 22:25:12 +0000883public:
884 DAGInstruction(TreePattern *TP,
885 const std::vector<Record*> &results,
886 const std::vector<Record*> &operands,
Chris Lattner9dc68d32010-04-20 06:28:43 +0000887 const std::vector<Record*> &impresults)
Jim Grosbach50986b52010-12-24 05:06:32 +0000888 : Pattern(TP), Results(results), Operands(operands),
David Blaikiecf195302014-11-17 22:55:41 +0000889 ImpResults(impresults), ResultPattern(nullptr) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000890
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000891 TreePattern *getPattern() const { return Pattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000892 unsigned getNumResults() const { return Results.size(); }
893 unsigned getNumOperands() const { return Operands.size(); }
894 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000895 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000896
David Blaikiecf195302014-11-17 22:55:41 +0000897 void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000898
Chris Lattner8cab0212008-01-05 22:25:12 +0000899 Record *getResult(unsigned RN) const {
900 assert(RN < Results.size());
901 return Results[RN];
902 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000903
Chris Lattner8cab0212008-01-05 22:25:12 +0000904 Record *getOperand(unsigned ON) const {
905 assert(ON < Operands.size());
906 return Operands[ON];
907 }
908
909 Record *getImpResult(unsigned RN) const {
910 assert(RN < ImpResults.size());
911 return ImpResults[RN];
912 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000913
David Blaikiecf195302014-11-17 22:55:41 +0000914 TreePatternNode *getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000915};
Jim Grosbach50986b52010-12-24 05:06:32 +0000916
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000917/// This class represents a condition that has to be satisfied for a pattern
918/// to be tried. It is a generalization of a class "Pattern" from Target.td:
919/// in addition to the Target.td's predicates, this class can also represent
920/// conditions associated with HW modes. Both types will eventually become
921/// strings containing C++ code to be executed, the difference is in how
922/// these strings are generated.
923class Predicate {
924public:
925 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
926 assert(R->isSubClassOf("Predicate") &&
927 "Predicate objects should only be created for records derived"
928 "from Predicate class");
929 }
930 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
931 IfCond(C), IsHwMode(true) {}
932
933 /// Return a string which contains the C++ condition code that will serve
934 /// as a predicate during instruction selection.
935 std::string getCondString() const {
936 // The string will excute in a subclass of SelectionDAGISel.
937 // Cast to std::string explicitly to avoid ambiguity with StringRef.
938 std::string C = IsHwMode
939 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
940 : std::string(Def->getValueAsString("CondString"));
941 return IfCond ? C : "!("+C+')';
942 }
943 bool operator==(const Predicate &P) const {
944 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
945 }
946 bool operator<(const Predicate &P) const {
947 if (IsHwMode != P.IsHwMode)
948 return IsHwMode < P.IsHwMode;
949 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
950 if (IfCond != P.IfCond)
951 return IfCond < P.IfCond;
952 if (Def)
953 return LessRecord()(Def, P.Def);
954 return Features < P.Features;
955 }
956 Record *Def; ///< Predicate definition from .td file, null for
957 ///< HW modes.
958 std::string Features; ///< Feature string for HW mode.
959 bool IfCond; ///< The boolean value that the condition has to
960 ///< evaluate to for this predicate to be true.
961 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
962};
963
Chris Lattnerab3242f2008-01-06 01:10:31 +0000964/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +0000965/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +0000966class PatternToMatch {
967public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000968 PatternToMatch(Record *srcrecord, const std::vector<Predicate> &preds,
969 TreePatternNode *src, TreePatternNode *dst,
970 const std::vector<Record*> &dstregs,
971 int complexity, unsigned uid, unsigned setmode = 0)
972 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
973 Predicates(preds), Dstregs(std::move(dstregs)),
974 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
975
976 PatternToMatch(Record *srcrecord, std::vector<Predicate> &&preds,
977 TreePatternNode *src, TreePatternNode *dst,
978 std::vector<Record*> &&dstregs,
979 int complexity, unsigned uid, unsigned setmode = 0)
980 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
981 Predicates(preds), Dstregs(std::move(dstregs)),
982 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000983
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000984 Record *SrcRecord; // Originating Record for the pattern.
Chris Lattner8cab0212008-01-05 22:25:12 +0000985 TreePatternNode *SrcPattern; // Source pattern to match.
986 TreePatternNode *DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000987 std::vector<Predicate> Predicates; // Top level predicate conditions
988 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +0000989 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +0000990 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +0000991 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000992 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +0000993
Jim Grosbachfb116ae2010-12-07 23:05:49 +0000994 Record *getSrcRecord() const { return SrcRecord; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000995 TreePatternNode *getSrcPattern() const { return SrcPattern; }
996 TreePatternNode *getDstPattern() const { return DstPattern; }
997 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +0000998 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000999 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001000
1001 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001002
Chris Lattner05925fe2010-03-29 01:40:38 +00001003 /// Compute the complexity metric for the input pattern. This roughly
1004 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001005 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001006};
1007
Chris Lattnerab3242f2008-01-06 01:10:31 +00001008class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001009 RecordKeeper &Records;
1010 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001011 CodeGenIntrinsicTable Intrinsics;
1012 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001013
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001014 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001015 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1016 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001017 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001018 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1019 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001020 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1021 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001022
Chris Lattner8cab0212008-01-05 22:25:12 +00001023 // Specific SDNode definitions:
1024 Record *intrinsic_void_sdnode;
1025 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001026
Chris Lattner8cab0212008-01-05 22:25:12 +00001027 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1028 /// value is the pattern to match, the second pattern is the result to
1029 /// emit.
1030 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001031
1032 TypeSetByHwMode LegalVTS;
1033
Daniel Sanders7e523672017-11-11 03:23:44 +00001034 using PatternRewriterFn = std::function<void (TreePattern *)>;
1035 PatternRewriterFn PatternRewriter;
1036
Chris Lattner8cab0212008-01-05 22:25:12 +00001037public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001038 CodeGenDAGPatterns(RecordKeeper &R,
1039 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001040
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001041 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001042 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001043 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001044
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001045 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001046
Chris Lattner8cab0212008-01-05 22:25:12 +00001047 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001048 auto F = SDNodes.find(R);
1049 assert(F != SDNodes.end() && "Unknown node!");
1050 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001051 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001052
Chris Lattnercc43e792008-01-05 22:54:53 +00001053 // Node transformation lookups.
1054 typedef std::pair<Record*, std::string> NodeXForm;
1055 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001056 auto F = SDNodeXForms.find(R);
1057 assert(F != SDNodeXForms.end() && "Invalid transform!");
1058 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001059 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001060
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001061 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001062 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001063 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1064 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1065
Jim Grosbach50986b52010-12-24 05:06:32 +00001066
Chris Lattner8cab0212008-01-05 22:25:12 +00001067 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001068 auto F = ComplexPatterns.find(R);
1069 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1070 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001071 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001072
Chris Lattner8cab0212008-01-05 22:25:12 +00001073 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1074 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1075 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001076 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1077 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001078 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001079 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001080
Chris Lattner8cab0212008-01-05 22:25:12 +00001081 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001082 if (IID-1 < Intrinsics.size())
1083 return Intrinsics[IID-1];
1084 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1085 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001086 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001087 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001088
Chris Lattner8cab0212008-01-05 22:25:12 +00001089 unsigned getIntrinsicID(Record *R) const {
1090 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1091 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001092 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1093 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001094 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001095 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001096
Chris Lattner7ed81692010-02-18 06:47:49 +00001097 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001098 auto F = DefaultOperands.find(R);
1099 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1100 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001101 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001102
Chris Lattner8cab0212008-01-05 22:25:12 +00001103 // Pattern Fragment information.
1104 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001105 auto F = PatternFragments.find(R);
1106 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1107 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001108 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001109 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001110 auto F = PatternFragments.find(R);
1111 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001112 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001113 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001114 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001115
David Blaikiefcacc742014-11-13 21:56:57 +00001116 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1117 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001118 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1119 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001120 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001121
1122 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001123 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1124 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1125 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001126 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001127
Ahmed Bougacha14107512013-10-28 18:07:21 +00001128 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1129 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
1130 const DAGInstruction &parseInstructionPattern(
1131 CodeGenInstruction &CGI, ListInit *Pattern,
1132 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001133
Chris Lattner8cab0212008-01-05 22:25:12 +00001134 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001135 auto F = Instructions.find(R);
1136 assert(F != Instructions.end() && "Unknown instruction!");
1137 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001138 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001139
Chris Lattner8cab0212008-01-05 22:25:12 +00001140 Record *get_intrinsic_void_sdnode() const {
1141 return intrinsic_void_sdnode;
1142 }
1143 Record *get_intrinsic_w_chain_sdnode() const {
1144 return intrinsic_w_chain_sdnode;
1145 }
1146 Record *get_intrinsic_wo_chain_sdnode() const {
1147 return intrinsic_wo_chain_sdnode;
1148 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001149
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001150 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1151
Chris Lattner8cab0212008-01-05 22:25:12 +00001152private:
1153 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001154 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001155 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001156 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001157 void ParseDefaultOperands();
1158 void ParseInstructions();
1159 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001160 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001161 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001162 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001163 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001164
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001165 std::vector<Predicate> makePredList(ListInit *L);
1166
Craig Topper18e6b572017-06-25 17:33:49 +00001167 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Chris Lattner8cab0212008-01-05 22:25:12 +00001168 void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
1169 std::map<std::string,
1170 TreePatternNode*> &InstInputs,
1171 std::map<std::string,
1172 TreePatternNode*> &InstResults,
Chris Lattner8cab0212008-01-05 22:25:12 +00001173 std::vector<Record*> &InstImpResults);
1174};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001175
1176
1177inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
1178 TreePattern &TP) const {
1179 bool MadeChange = false;
1180 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1181 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1182 return MadeChange;
1183 }
Chris Lattner8cab0212008-01-05 22:25:12 +00001184} // end namespace llvm
1185
1186#endif