blob: c682f3d3268732098865e3fdb1e24091248b5300 [file] [log] [blame]
Chris Lattnerab3242f2008-01-06 01:10:31 +00001//===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- C++ -*-===//
Chris Lattner8cab0212008-01-05 22:25:12 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerab3242f2008-01-06 01:10:31 +000010// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner8cab0212008-01-05 22:25:12 +000011// represent the patterns present in a .td file for instructions.
12//
13//===----------------------------------------------------------------------===//
14
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
16#define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
Chris Lattner8cab0212008-01-05 22:25:12 +000017
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000018#include "CodeGenHwModes.h"
Chris Lattner8cab0212008-01-05 22:25:12 +000019#include "CodeGenIntrinsics.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000020#include "CodeGenTarget.h"
Matt Arsenault303327d2017-12-20 19:36:28 +000021#include "SDNodeProperties.h"
Chris Lattnercabe0372010-03-15 06:00:16 +000022#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringMap.h"
Zachary Turner249dc142017-09-20 18:01:40 +000024#include "llvm/ADT/StringSet.h"
Craig Topperc4965bc2012-02-05 07:21:30 +000025#include "llvm/Support/ErrorHandling.h"
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner1802b172010-03-19 01:07:44 +000027#include <algorithm>
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000028#include <array>
Daniel Sanders7e523672017-11-11 03:23:44 +000029#include <functional>
Chris Lattner1802b172010-03-19 01:07:44 +000030#include <map>
Chandler Carruth91d19d82012-12-04 10:37:14 +000031#include <set>
32#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000033
34namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000035
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000036class Record;
37class Init;
38class ListInit;
39class DagInit;
40class SDNodeInfo;
41class TreePattern;
42class TreePatternNode;
43class CodeGenDAGPatterns;
44class ComplexPattern;
45
Florian Hahn75e87c32018-05-30 21:00:18 +000046/// Shared pointer for TreePatternNode.
47using TreePatternNodePtr = std::shared_ptr<TreePatternNode>;
48
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000049/// This represents a set of MVTs. Since the underlying type for the MVT
50/// is uint8_t, there are at most 256 values. To reduce the number of memory
51/// allocations and deallocations, represent the set as a sequence of bits.
52/// To reduce the allocations even further, make MachineValueTypeSet own
53/// the storage and use std::array as the bit container.
54struct MachineValueTypeSet {
55 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
56 uint8_t>::value,
57 "Change uint8_t here to the SimpleValueType's type");
58 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
59 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000060 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000061 static unsigned constexpr NumWords = Capacity/WordWidth;
62 static_assert(NumWords*WordWidth == Capacity,
63 "Capacity should be a multiple of WordWidth");
64
65 LLVM_ATTRIBUTE_ALWAYS_INLINE
66 MachineValueTypeSet() {
67 clear();
68 }
69
70 LLVM_ATTRIBUTE_ALWAYS_INLINE
71 unsigned size() const {
72 unsigned Count = 0;
73 for (WordType W : Words)
74 Count += countPopulation(W);
75 return Count;
76 }
77 LLVM_ATTRIBUTE_ALWAYS_INLINE
78 void clear() {
79 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
80 }
81 LLVM_ATTRIBUTE_ALWAYS_INLINE
82 bool empty() const {
83 for (WordType W : Words)
84 if (W != 0)
85 return false;
86 return true;
87 }
88 LLVM_ATTRIBUTE_ALWAYS_INLINE
89 unsigned count(MVT T) const {
90 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
91 }
92 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
93 bool V = count(T.SimpleTy);
94 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
95 return {*this, V};
96 }
97 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
98 for (unsigned i = 0; i != NumWords; ++i)
99 Words[i] |= S.Words[i];
100 return *this;
101 }
102 LLVM_ATTRIBUTE_ALWAYS_INLINE
103 void erase(MVT T) {
104 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
105 }
106
107 struct const_iterator {
108 // Some implementations of the C++ library require these traits to be
109 // defined.
110 using iterator_category = std::forward_iterator_tag;
111 using value_type = MVT;
112 using difference_type = ptrdiff_t;
113 using pointer = const MVT*;
114 using reference = const MVT&;
115
116 LLVM_ATTRIBUTE_ALWAYS_INLINE
117 MVT operator*() const {
118 assert(Pos != Capacity);
119 return MVT::SimpleValueType(Pos);
120 }
121 LLVM_ATTRIBUTE_ALWAYS_INLINE
122 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
123 Pos = End ? Capacity : find_from_pos(0);
124 }
125 LLVM_ATTRIBUTE_ALWAYS_INLINE
126 const_iterator &operator++() {
127 assert(Pos != Capacity);
128 Pos = find_from_pos(Pos+1);
129 return *this;
130 }
131
132 LLVM_ATTRIBUTE_ALWAYS_INLINE
133 bool operator==(const const_iterator &It) const {
134 return Set == It.Set && Pos == It.Pos;
135 }
136 LLVM_ATTRIBUTE_ALWAYS_INLINE
137 bool operator!=(const const_iterator &It) const {
138 return !operator==(It);
139 }
140
141 private:
142 unsigned find_from_pos(unsigned P) const {
143 unsigned SkipWords = P / WordWidth;
144 unsigned SkipBits = P % WordWidth;
145 unsigned Count = SkipWords * WordWidth;
146
147 // If P is in the middle of a word, process it manually here, because
148 // the trailing bits need to be masked off to use findFirstSet.
149 if (SkipBits != 0) {
150 WordType W = Set->Words[SkipWords];
151 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
152 if (W != 0)
153 return Count + findFirstSet(W);
154 Count += WordWidth;
155 SkipWords++;
156 }
157
158 for (unsigned i = SkipWords; i != NumWords; ++i) {
159 WordType W = Set->Words[i];
160 if (W != 0)
161 return Count + findFirstSet(W);
162 Count += WordWidth;
163 }
164 return Capacity;
165 }
166
167 const MachineValueTypeSet *Set;
168 unsigned Pos;
169 };
170
171 LLVM_ATTRIBUTE_ALWAYS_INLINE
172 const_iterator begin() const { return const_iterator(this, false); }
173 LLVM_ATTRIBUTE_ALWAYS_INLINE
174 const_iterator end() const { return const_iterator(this, true); }
175
176 LLVM_ATTRIBUTE_ALWAYS_INLINE
177 bool operator==(const MachineValueTypeSet &S) const {
178 return Words == S.Words;
179 }
180 LLVM_ATTRIBUTE_ALWAYS_INLINE
181 bool operator!=(const MachineValueTypeSet &S) const {
182 return !operator==(S);
183 }
184
185private:
186 friend struct const_iterator;
187 std::array<WordType,NumWords> Words;
188};
189
190struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
191 using SetType = MachineValueTypeSet;
Jim Grosbach50986b52010-12-24 05:06:32 +0000192
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000193 TypeSetByHwMode() = default;
194 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
195 TypeSetByHwMode(MVT::SimpleValueType VT)
196 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
197 TypeSetByHwMode(ValueTypeByHwMode VT)
198 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
199 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000200
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000201 SetType &getOrCreate(unsigned Mode) {
202 if (hasMode(Mode))
203 return get(Mode);
204 return Map.insert({Mode,SetType()}).first->second;
205 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000206
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000207 bool isValueTypeByHwMode(bool AllowEmpty) const;
208 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000209
210 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000211 bool isMachineValueType() const {
212 return isDefaultOnly() && Map.begin()->second.size() == 1;
213 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000214
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000215 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000216 MVT getMachineValueType() const {
217 assert(isMachineValueType());
218 return *Map.begin()->second.begin();
219 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000220
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000221 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000222
223 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000224 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000225 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000226 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000227
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000228 bool insert(const ValueTypeByHwMode &VVT);
229 bool constrain(const TypeSetByHwMode &VTS);
230 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000231 template <typename Predicate>
232 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000233
Zachary Turner249dc142017-09-20 18:01:40 +0000234 void writeToStream(raw_ostream &OS) const;
235 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000236
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000237 bool operator==(const TypeSetByHwMode &VTS) const;
238 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000239
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000240 void dump() const;
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000241 bool validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000242
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000243private:
244 /// Intersect two sets. Return true if anything has changed.
245 bool intersect(SetType &Out, const SetType &In);
246};
Jim Grosbach50986b52010-12-24 05:06:32 +0000247
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000248raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
249
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000250struct TypeInfer {
251 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000252
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000253 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
254 return VTS.isValueTypeByHwMode(AllowEmpty);
255 }
256 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
257 bool AllowEmpty) const {
258 assert(VTS.isValueTypeByHwMode(AllowEmpty));
259 return VTS.getValueTypeByHwMode();
260 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000261
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000262 /// The protocol in the following functions (Merge*, force*, Enforce*,
263 /// expand*) is to return "true" if a change has been made, "false"
264 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000265
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000266 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
267 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
268 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
269 }
270 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
271 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
272 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000273
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000274 /// Reduce the set \p Out to have at most one element for each mode.
275 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000276
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000277 /// The following four functions ensure that upon return the set \p Out
278 /// will only contain types of the specified kind: integer, floating-point,
279 /// scalar, or vector.
280 /// If \p Out is empty, all legal types of the specified kind will be added
281 /// to it. Otherwise, all types that are not of the specified kind will be
282 /// removed from \p Out.
283 bool EnforceInteger(TypeSetByHwMode &Out);
284 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
285 bool EnforceScalar(TypeSetByHwMode &Out);
286 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000287
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000288 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
289 /// unchanged.
290 bool EnforceAny(TypeSetByHwMode &Out);
291 /// Make sure that for each type in \p Small, there exists a larger type
292 /// in \p Big.
293 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
294 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
295 /// for each type U in \p Elem, U is a scalar type.
296 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
297 /// (vector) type T in \p Vec, such that U is the element type of T.
298 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
299 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
300 const ValueTypeByHwMode &VVT);
301 /// Ensure that for each type T in \p Sub, T is a vector type, and there
302 /// exists a type U in \p Vec such that U is a vector type with the same
303 /// element type as T and at least as many elements as T.
304 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
305 TypeSetByHwMode &Sub);
306 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
307 /// 2. Ensure that for each vector type T in \p V, there exists a vector
308 /// type U in \p W, such that T and U have the same number of elements.
309 /// 3. Ensure that for each vector type U in \p W, there exists a vector
310 /// type T in \p V, such that T and U have the same number of elements
311 /// (reverse of 2).
312 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
313 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
314 /// such that T and U have equal size in bits.
315 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
316 /// such that T and U have equal size in bits (reverse of 1).
317 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000318
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000319 /// For each overloaded type (i.e. of form *Any), replace it with the
320 /// corresponding subset of legal, specific types.
321 void expandOverloads(TypeSetByHwMode &VTS);
322 void expandOverloads(TypeSetByHwMode::SetType &Out,
323 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000324
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000325 struct ValidateOnExit {
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000326 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
327 #ifndef NDEBUG
328 ~ValidateOnExit();
329 #else
330 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
331 #endif
332 TypeInfer &Infer;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000333 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000334 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000335
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000336 struct SuppressValidation {
337 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
338 Infer.Validate = false;
339 }
340 ~SuppressValidation() {
341 Infer.Validate = SavedValidate;
342 }
343 TypeInfer &Infer;
344 bool SavedValidate;
345 };
346
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000347 TreePattern &TP;
348 unsigned ForceMode; // Mode to use when set.
349 bool CodeGen = false; // Set during generation of matcher code.
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000350 bool Validate = true; // Indicate whether to validate types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000351
352private:
353 TypeSetByHwMode getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000354
355 /// Cached legal types.
356 bool LegalTypesCached = false;
357 TypeSetByHwMode::SetType LegalCache = {};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000358};
Chris Lattner8cab0212008-01-05 22:25:12 +0000359
Scott Michel94420742008-03-05 17:49:05 +0000360/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000361typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000362
Chris Lattner8cab0212008-01-05 22:25:12 +0000363/// SDTypeConstraint - This is a discriminated union of constraints,
364/// corresponding to the SDTypeConstraint tablegen class in Target.td.
365struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000366 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000367
Chris Lattner8cab0212008-01-05 22:25:12 +0000368 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000369 enum {
370 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000371 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000372 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000373 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000374
Chris Lattner8cab0212008-01-05 22:25:12 +0000375 union { // The discriminated union.
376 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000377 unsigned OtherOperandNum;
378 } SDTCisSameAs_Info;
379 struct {
380 unsigned OtherOperandNum;
381 } SDTCisVTSmallerThanOp_Info;
382 struct {
383 unsigned BigOperandNum;
384 } SDTCisOpSmallerThanOp_Info;
385 struct {
386 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000387 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000388 struct {
389 unsigned OtherOperandNum;
390 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000391 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000392 unsigned OtherOperandNum;
393 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000394 struct {
395 unsigned OtherOperandNum;
396 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000397 } x;
398
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000399 // The VT for SDTCisVT and SDTCVecEltisVT.
400 // Must not be in the union because it has a non-trivial destructor.
401 ValueTypeByHwMode VVT;
402
Chris Lattner8cab0212008-01-05 22:25:12 +0000403 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
404 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000405 /// change, false otherwise. If a type contradiction is found, an error
406 /// is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000407 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
Chris Lattner8cab0212008-01-05 22:25:12 +0000408 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000409};
410
411/// SDNodeInfo - One of these records is created for each SDNode instance in
412/// the target .td file. This represents the various dag nodes we will be
413/// processing.
414class SDNodeInfo {
415 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000416 StringRef EnumName;
417 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000418 unsigned Properties;
419 unsigned NumResults;
420 int NumOperands;
421 std::vector<SDTypeConstraint> TypeConstraints;
422public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000423 // Parse the specified record.
424 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000425
Chris Lattner8cab0212008-01-05 22:25:12 +0000426 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000427
Chris Lattner135091b2010-03-28 08:48:47 +0000428 /// getNumOperands - This is the number of operands required or -1 if
429 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000430 int getNumOperands() const { return NumOperands; }
431 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000432 StringRef getEnumName() const { return EnumName; }
433 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000434
Chris Lattner8cab0212008-01-05 22:25:12 +0000435 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
436 return TypeConstraints;
437 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000438
Chris Lattner99e53b32010-02-28 00:22:30 +0000439 /// getKnownType - If the type constraints on this node imply a fixed type
440 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000441 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000442 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000443
Chris Lattner8cab0212008-01-05 22:25:12 +0000444 /// hasProperty - Return true if this node has the specified property.
445 ///
446 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
447
448 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
449 /// constraints for this node to the operands of the node. This returns
450 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000451 /// found, an error is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000452 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000453};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000454
Chris Lattner514e2922011-04-17 21:38:24 +0000455/// TreePredicateFn - This is an abstraction that represents the predicates on
456/// a PatFrag node. This is a simple one-word wrapper around a pointer to
457/// provide nice accessors.
458class TreePredicateFn {
459 /// PatFragRec - This is the TreePattern for the PatFrag that we
460 /// originally came from.
461 TreePattern *PatFragRec;
462public:
463 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000464 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000465
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000466
Chris Lattner514e2922011-04-17 21:38:24 +0000467 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000468
Chris Lattner514e2922011-04-17 21:38:24 +0000469 /// isAlwaysTrue - Return true if this is a noop predicate.
470 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000471
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000472 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000473
Chris Lattner07add492011-04-18 06:22:33 +0000474 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
475 /// this is an immediate predicate. It is an error to call this on a
476 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000477 std::string getImmediatePredicateCode() const {
478 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000479 assert(!Result.empty() && "Isn't an immediate pattern!");
480 return Result;
481 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000482
Chris Lattner514e2922011-04-17 21:38:24 +0000483 bool operator==(const TreePredicateFn &RHS) const {
484 return PatFragRec == RHS.PatFragRec;
485 }
486
487 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
488
489 /// Return the name to use in the generated code to reference this, this is
490 /// "Predicate_foo" if from a pattern fragment "foo".
491 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000492
Chris Lattner514e2922011-04-17 21:38:24 +0000493 /// getCodeToRunOnSDNode - Return the code for the function body that
494 /// evaluates this predicate. The argument is expected to be in "Node",
495 /// not N. This handles casting and conversion to a concrete node type as
496 /// appropriate.
497 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000498
Daniel Sanders649c5852017-10-13 20:42:18 +0000499 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000500 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000501
Daniel Sanders11300ce2017-10-13 21:28:03 +0000502 /// Get a string that describes the type returned by getImmType() but is
503 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000504 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000505
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000506 // Is the desired predefined predicate for a load?
507 bool isLoad() const;
508 // Is the desired predefined predicate for a store?
509 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000510 // Is the desired predefined predicate for an atomic?
511 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000512
513 /// Is this predicate the predefined unindexed load predicate?
514 /// Is this predicate the predefined unindexed store predicate?
515 bool isUnindexed() const;
516 /// Is this predicate the predefined non-extending load predicate?
517 bool isNonExtLoad() const;
518 /// Is this predicate the predefined any-extend load predicate?
519 bool isAnyExtLoad() const;
520 /// Is this predicate the predefined sign-extend load predicate?
521 bool isSignExtLoad() const;
522 /// Is this predicate the predefined zero-extend load predicate?
523 bool isZeroExtLoad() const;
524 /// Is this predicate the predefined non-truncating store predicate?
525 bool isNonTruncStore() const;
526 /// Is this predicate the predefined truncating store predicate?
527 bool isTruncStore() const;
528
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000529 /// Is this predicate the predefined monotonic atomic predicate?
530 bool isAtomicOrderingMonotonic() const;
531 /// Is this predicate the predefined acquire atomic predicate?
532 bool isAtomicOrderingAcquire() const;
533 /// Is this predicate the predefined release atomic predicate?
534 bool isAtomicOrderingRelease() const;
535 /// Is this predicate the predefined acquire-release atomic predicate?
536 bool isAtomicOrderingAcquireRelease() const;
537 /// Is this predicate the predefined sequentially consistent atomic predicate?
538 bool isAtomicOrderingSequentiallyConsistent() const;
539
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000540 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
541 bool isAtomicOrderingAcquireOrStronger() const;
542 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
543 bool isAtomicOrderingWeakerThanAcquire() const;
544
545 /// Is this predicate the predefined release-or-stronger atomic predicate?
546 bool isAtomicOrderingReleaseOrStronger() const;
547 /// Is this predicate the predefined weaker-than-release atomic predicate?
548 bool isAtomicOrderingWeakerThanRelease() const;
549
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000550 /// If non-null, indicates that this predicate is a predefined memory VT
551 /// predicate for a load/store and returns the ValueType record for the memory VT.
552 Record *getMemoryVT() const;
553 /// If non-null, indicates that this predicate is a predefined memory VT
554 /// predicate (checking only the scalar type) for load/store and returns the
555 /// ValueType record for the memory VT.
556 Record *getScalarMemoryVT() const;
557
Daniel Sanders8ead1292018-06-15 23:13:43 +0000558 // If true, indicates that GlobalISel-based C++ code was supplied.
559 bool hasGISelPredicateCode() const;
560 std::string getGISelPredicateCode() const;
561
Chris Lattner514e2922011-04-17 21:38:24 +0000562private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000563 bool hasPredCode() const;
564 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000565 std::string getPredCode() const;
566 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000567 bool immCodeUsesAPInt() const;
568 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000569
570 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000571};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000572
Chris Lattner8cab0212008-01-05 22:25:12 +0000573
Chris Lattner8cab0212008-01-05 22:25:12 +0000574class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000575 /// The type of each node result. Before and during type inference, each
576 /// result may be a set of possible types. After (successful) type inference,
577 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000578 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000579
Chris Lattner8cab0212008-01-05 22:25:12 +0000580 /// Operator - The Record for the operator if this is an interior node (not
581 /// a leaf).
582 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000583
Chris Lattner8cab0212008-01-05 22:25:12 +0000584 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
585 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000586 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000587
Chris Lattner8cab0212008-01-05 22:25:12 +0000588 /// Name - The name given to this node with the :$foo notation.
589 ///
590 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000591
Dan Gohman6e979022008-10-15 06:17:21 +0000592 /// PredicateFns - The predicate functions to execute on this node to check
593 /// for a match. If this list is empty, no predicate is involved.
Chris Lattner514e2922011-04-17 21:38:24 +0000594 std::vector<TreePredicateFn> PredicateFns;
Jim Grosbach50986b52010-12-24 05:06:32 +0000595
Chris Lattner8cab0212008-01-05 22:25:12 +0000596 /// TransformFn - The transformation function to execute on this node before
597 /// it can be substituted into the resulting instruction on a pattern match.
598 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000599
Florian Hahn75e87c32018-05-30 21:00:18 +0000600 std::vector<TreePatternNodePtr> Children;
601
Chris Lattner8cab0212008-01-05 22:25:12 +0000602public:
Florian Hahn75e87c32018-05-30 21:00:18 +0000603 TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> &Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000604 unsigned NumResults)
Florian Hahn75e87c32018-05-30 21:00:18 +0000605 : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000606 Types.resize(NumResults);
607 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000608 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000609 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000610 Types.resize(NumResults);
Chris Lattner8cab0212008-01-05 22:25:12 +0000611 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000612
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000613 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000614 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000615 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000616
Craig Topperada08572014-04-16 04:21:27 +0000617 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000618
Chris Lattnercabe0372010-03-15 06:00:16 +0000619 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000620 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000621 ValueTypeByHwMode getType(unsigned ResNo) const {
622 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000623 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000624 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
625 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
626 return Types[ResNo];
627 }
628 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
629 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
630 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
631 return Types[ResNo].getMachineValueType().SimpleTy;
632 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000633
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000634 bool hasConcreteType(unsigned ResNo) const {
635 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000636 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000637 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
638 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000639 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000640
David Greeneaf8ee2c2011-07-29 22:43:06 +0000641 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000642 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000643
Chris Lattner8cab0212008-01-05 22:25:12 +0000644 unsigned getNumChildren() const { return Children.size(); }
Florian Hahn6b1db822018-06-14 20:32:58 +0000645 TreePatternNode *getChild(unsigned N) const { return Children[N].get(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000646 const TreePatternNodePtr &getChildShared(unsigned N) const {
647 return Children[N];
Chris Lattner8cab0212008-01-05 22:25:12 +0000648 }
Florian Hahn75e87c32018-05-30 21:00:18 +0000649 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000650
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000651 /// hasChild - Return true if N is any of our children.
652 bool hasChild(const TreePatternNode *N) const {
653 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Florian Hahn75e87c32018-05-30 21:00:18 +0000654 if (Children[i].get() == N)
655 return true;
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000656 return false;
657 }
Chris Lattner89c65662008-01-06 05:36:50 +0000658
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000659 bool hasProperTypeByHwMode() const;
660 bool hasPossibleType() const;
661 bool setDefaultMode(unsigned Mode);
662
Chris Lattner514e2922011-04-17 21:38:24 +0000663 bool hasAnyPredicate() const { return !PredicateFns.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000664
Chris Lattner514e2922011-04-17 21:38:24 +0000665 const std::vector<TreePredicateFn> &getPredicateFns() const {
666 return PredicateFns;
667 }
Dan Gohman6e979022008-10-15 06:17:21 +0000668 void clearPredicateFns() { PredicateFns.clear(); }
Chris Lattner514e2922011-04-17 21:38:24 +0000669 void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
Dan Gohman6e979022008-10-15 06:17:21 +0000670 assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
671 PredicateFns = Fns;
672 }
Chris Lattner514e2922011-04-17 21:38:24 +0000673 void addPredicateFn(const TreePredicateFn &Fn) {
674 assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
David Majnemer0d955d02016-08-11 22:21:41 +0000675 if (!is_contained(PredicateFns, Fn))
Dan Gohman6e979022008-10-15 06:17:21 +0000676 PredicateFns.push_back(Fn);
677 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000678
679 Record *getTransformFn() const { return TransformFn; }
680 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000681
Chris Lattner89c65662008-01-06 05:36:50 +0000682 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
683 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
684 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000685
Chris Lattner53c39ba2010-02-14 22:22:58 +0000686 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
687 /// return the ComplexPattern information, otherwise return null.
688 const ComplexPattern *
689 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
690
Tim Northoverc807a172014-05-20 11:52:46 +0000691 /// Returns the number of MachineInstr operands that would be produced by this
692 /// node if it mapped directly to an output Instruction's
693 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
694 /// for Operands; otherwise 1.
695 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
696
Chris Lattner53c39ba2010-02-14 22:22:58 +0000697 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000698 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000699
Chris Lattner53c39ba2010-02-14 22:22:58 +0000700 /// TreeHasProperty - Return true if any node in this tree has the specified
701 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000702 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000703
Evan Cheng49bad4c2008-06-16 20:29:38 +0000704 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
705 /// marked isCommutative.
706 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000707
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000708 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000709 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000710
Chris Lattner8cab0212008-01-05 22:25:12 +0000711public: // Higher level manipulation routines.
712
713 /// clone - Return a new copy of this tree.
714 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000715 TreePatternNodePtr clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000716
717 /// RemoveAllTypes - Recursively strip all the types of this tree.
718 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000719
Chris Lattner8cab0212008-01-05 22:25:12 +0000720 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
721 /// the specified node. For this comparison, all of the state of the node
722 /// is considered, except for the assigned name. Nodes with differing names
723 /// that are otherwise identical are considered isomorphic.
Florian Hahn6b1db822018-06-14 20:32:58 +0000724 bool isIsomorphicTo(const TreePatternNode *N,
Scott Michel94420742008-03-05 17:49:05 +0000725 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000726
Chris Lattner8cab0212008-01-05 22:25:12 +0000727 /// SubstituteFormalArguments - Replace the formal arguments in this tree
728 /// with actual values specified by ArgMap.
Florian Hahn75e87c32018-05-30 21:00:18 +0000729 void
730 SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
Chris Lattner8cab0212008-01-05 22:25:12 +0000731
732 /// InlinePatternFragments - If this pattern refers to any pattern
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000733 /// fragments, return the set of inlined versions (this can be more than
734 /// one if a PatFrags record has multiple alternatives).
735 void InlinePatternFragments(TreePatternNodePtr T,
736 TreePattern &TP,
737 std::vector<TreePatternNodePtr> &OutAlternatives);
Jim Grosbach50986b52010-12-24 05:06:32 +0000738
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000739 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000740 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000741 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000742 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000743
Chris Lattner8cab0212008-01-05 22:25:12 +0000744 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000745 /// information. If N already contains a conflicting type, then flag an
746 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000747 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000748 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
749 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000750 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000751 TreePattern &TP);
752 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
753 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000754
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000755 // Update node type with types inferred from an instruction operand or result
756 // def from the ins/outs lists.
757 // Return true if the type changed.
758 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
759
Chris Lattner8cab0212008-01-05 22:25:12 +0000760 /// ContainsUnresolvedType - Return true if this tree contains any
761 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000762 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000763
Chris Lattner8cab0212008-01-05 22:25:12 +0000764 /// canPatternMatch - If it is impossible for this pattern to match on this
765 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000766 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000767};
768
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000769inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
770 TPN.print(OS);
771 return OS;
772}
Jim Grosbach50986b52010-12-24 05:06:32 +0000773
Chris Lattner8cab0212008-01-05 22:25:12 +0000774
775/// TreePattern - Represent a pattern, used for instructions, pattern
776/// fragments, etc.
777///
778class TreePattern {
779 /// Trees - The list of pattern trees which corresponds to this pattern.
780 /// Note that PatFrag's only have a single tree.
781 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000782 std::vector<TreePatternNodePtr> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000783
Chris Lattnercabe0372010-03-15 06:00:16 +0000784 /// NamedNodes - This is all of the nodes that have names in the trees in this
785 /// pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +0000786 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000787
Chris Lattner8cab0212008-01-05 22:25:12 +0000788 /// TheRecord - The actual TableGen record corresponding to this pattern.
789 ///
790 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000791
Chris Lattner8cab0212008-01-05 22:25:12 +0000792 /// Args - This is a list of all of the arguments to this pattern (for
793 /// PatFrag patterns), which are the 'node' markers in this pattern.
794 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000795
Chris Lattner8cab0212008-01-05 22:25:12 +0000796 /// CDP - the top-level object coordinating this madness.
797 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000798 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000799
800 /// isInputPattern - True if this is an input pattern, something to match.
801 /// False if this is an output pattern, something to emit.
802 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000803
804 /// hasError - True if the currently processed nodes have unresolvable types
805 /// or other non-fatal errors
806 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000807
808 /// It's important that the usage of operands in ComplexPatterns is
809 /// consistent: each named operand can be defined by at most one
810 /// ComplexPattern. This records the ComplexPattern instance and the operand
811 /// number for each operand encountered in a ComplexPattern to aid in that
812 /// check.
813 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000814
815 TypeInfer Infer;
816
Chris Lattner8cab0212008-01-05 22:25:12 +0000817public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000818
Chris Lattner8cab0212008-01-05 22:25:12 +0000819 /// TreePattern constructor - Parse the specified DagInits into the
820 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000821 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000822 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000823 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000824 CodeGenDAGPatterns &ise);
Florian Hahn75e87c32018-05-30 21:00:18 +0000825 TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
David Blaikiecf195302014-11-17 22:55:41 +0000826 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000827
Chris Lattner8cab0212008-01-05 22:25:12 +0000828 /// getTrees - Return the tree patterns which corresponds to this pattern.
829 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000830 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000831 unsigned getNumTrees() const { return Trees.size(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000832 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
Florian Hahn53b14db2018-06-10 21:06:24 +0000833 void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
Florian Hahn4dd569c2018-06-13 20:59:53 +0000834 const TreePatternNodePtr &getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000835 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
836 return Trees[0];
837 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000838
Florian Hahn75e87c32018-05-30 21:00:18 +0000839 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
Chris Lattnercabe0372010-03-15 06:00:16 +0000840 if (NamedNodes.empty())
841 ComputeNamedNodes();
842 return NamedNodes;
843 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000844
Chris Lattner8cab0212008-01-05 22:25:12 +0000845 /// getRecord - Return the actual TableGen record corresponding to this
846 /// pattern.
847 ///
848 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000849
Chris Lattner8cab0212008-01-05 22:25:12 +0000850 unsigned getNumArgs() const { return Args.size(); }
851 const std::string &getArgName(unsigned i) const {
852 assert(i < Args.size() && "Argument reference out of range!");
853 return Args[i];
854 }
855 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000856
Chris Lattnerab3242f2008-01-06 01:10:31 +0000857 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000858
859 /// InlinePatternFragments - If this pattern refers to any pattern
860 /// fragments, inline them into place, giving us a pattern without any
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000861 /// PatFrags references. This may increase the number of trees in the
862 /// pattern if a PatFrags has multiple alternatives.
Chris Lattner8cab0212008-01-05 22:25:12 +0000863 void InlinePatternFragments() {
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000864 std::vector<TreePatternNodePtr> Copy = Trees;
865 Trees.clear();
866 for (unsigned i = 0, e = Copy.size(); i != e; ++i)
867 Copy[i]->InlinePatternFragments(Copy[i], *this, Trees);
Chris Lattner8cab0212008-01-05 22:25:12 +0000868 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000869
Chris Lattner8cab0212008-01-05 22:25:12 +0000870 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000871 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000872 /// otherwise. Bail out if a type contradiction is found.
Florian Hahn75e87c32018-05-30 21:00:18 +0000873 bool InferAllTypes(
874 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000875
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000876 /// error - If this is the first error in the current resolution step,
877 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000878 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000879 bool hasError() const {
880 return HasError;
881 }
882 void resetError() {
883 HasError = false;
884 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000885
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000886 TypeInfer &getInfer() { return Infer; }
887
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000888 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000889 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000890
Chris Lattner8cab0212008-01-05 22:25:12 +0000891private:
Florian Hahn75e87c32018-05-30 21:00:18 +0000892 TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000893 void ComputeNamedNodes();
Florian Hahn6b1db822018-06-14 20:32:58 +0000894 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000895};
896
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000897
898inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
899 const TypeSetByHwMode &InTy,
900 TreePattern &TP) {
901 TypeSetByHwMode VTS(InTy);
902 TP.getInfer().expandOverloads(VTS);
903 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
904}
905
906inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
907 MVT::SimpleValueType InTy,
908 TreePattern &TP) {
909 TypeSetByHwMode VTS(InTy);
910 TP.getInfer().expandOverloads(VTS);
911 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
912}
913
914inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
915 ValueTypeByHwMode InTy,
916 TreePattern &TP) {
917 TypeSetByHwMode VTS(InTy);
918 TP.getInfer().expandOverloads(VTS);
919 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
920}
921
922
Tom Stellardb7246a72012-09-06 14:15:52 +0000923/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
924/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +0000925struct DAGDefaultOperand {
Florian Hahn75e87c32018-05-30 21:00:18 +0000926 std::vector<TreePatternNodePtr> DefaultOps;
Chris Lattner8cab0212008-01-05 22:25:12 +0000927};
928
929class DAGInstruction {
Chris Lattner8cab0212008-01-05 22:25:12 +0000930 std::vector<Record*> Results;
931 std::vector<Record*> Operands;
932 std::vector<Record*> ImpResults;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000933 TreePatternNodePtr SrcPattern;
Florian Hahn75e87c32018-05-30 21:00:18 +0000934 TreePatternNodePtr ResultPattern;
935
Chris Lattner8cab0212008-01-05 22:25:12 +0000936public:
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000937 DAGInstruction(const std::vector<Record*> &results,
Chris Lattner8cab0212008-01-05 22:25:12 +0000938 const std::vector<Record*> &operands,
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000939 const std::vector<Record*> &impresults,
940 TreePatternNodePtr srcpattern = nullptr,
941 TreePatternNodePtr resultpattern = nullptr)
942 : Results(results), Operands(operands), ImpResults(impresults),
943 SrcPattern(srcpattern), ResultPattern(resultpattern) {}
Chris Lattner8cab0212008-01-05 22:25:12 +0000944
Chris Lattner8cab0212008-01-05 22:25:12 +0000945 unsigned getNumResults() const { return Results.size(); }
946 unsigned getNumOperands() const { return Operands.size(); }
947 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000948 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000949
Chris Lattner8cab0212008-01-05 22:25:12 +0000950 Record *getResult(unsigned RN) const {
951 assert(RN < Results.size());
952 return Results[RN];
953 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000954
Chris Lattner8cab0212008-01-05 22:25:12 +0000955 Record *getOperand(unsigned ON) const {
956 assert(ON < Operands.size());
957 return Operands[ON];
958 }
959
960 Record *getImpResult(unsigned RN) const {
961 assert(RN < ImpResults.size());
962 return ImpResults[RN];
963 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000964
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000965 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
Florian Hahn75e87c32018-05-30 21:00:18 +0000966 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000967};
Jim Grosbach50986b52010-12-24 05:06:32 +0000968
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000969/// This class represents a condition that has to be satisfied for a pattern
970/// to be tried. It is a generalization of a class "Pattern" from Target.td:
971/// in addition to the Target.td's predicates, this class can also represent
972/// conditions associated with HW modes. Both types will eventually become
973/// strings containing C++ code to be executed, the difference is in how
974/// these strings are generated.
975class Predicate {
976public:
977 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
978 assert(R->isSubClassOf("Predicate") &&
979 "Predicate objects should only be created for records derived"
980 "from Predicate class");
981 }
982 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
983 IfCond(C), IsHwMode(true) {}
984
985 /// Return a string which contains the C++ condition code that will serve
986 /// as a predicate during instruction selection.
987 std::string getCondString() const {
988 // The string will excute in a subclass of SelectionDAGISel.
989 // Cast to std::string explicitly to avoid ambiguity with StringRef.
990 std::string C = IsHwMode
991 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
992 : std::string(Def->getValueAsString("CondString"));
993 return IfCond ? C : "!("+C+')';
994 }
995 bool operator==(const Predicate &P) const {
996 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
997 }
998 bool operator<(const Predicate &P) const {
999 if (IsHwMode != P.IsHwMode)
1000 return IsHwMode < P.IsHwMode;
1001 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
1002 if (IfCond != P.IfCond)
1003 return IfCond < P.IfCond;
1004 if (Def)
1005 return LessRecord()(Def, P.Def);
1006 return Features < P.Features;
1007 }
1008 Record *Def; ///< Predicate definition from .td file, null for
1009 ///< HW modes.
1010 std::string Features; ///< Feature string for HW mode.
1011 bool IfCond; ///< The boolean value that the condition has to
1012 ///< evaluate to for this predicate to be true.
1013 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
1014};
1015
Chris Lattnerab3242f2008-01-06 01:10:31 +00001016/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +00001017/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +00001018class PatternToMatch {
1019public:
Craig Topperd78567f2018-06-10 23:15:48 +00001020 PatternToMatch(Record *srcrecord, std::vector<Predicate> preds,
Florian Hahn75e87c32018-05-30 21:00:18 +00001021 TreePatternNodePtr src, TreePatternNodePtr dst,
Craig Topperd78567f2018-06-10 23:15:48 +00001022 std::vector<Record *> dstregs, int complexity,
Florian Hahn75e87c32018-05-30 21:00:18 +00001023 unsigned uid, unsigned setmode = 0)
1024 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001025 Predicates(preds), Dstregs(dstregs),
Florian Hahn75e87c32018-05-30 21:00:18 +00001026 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001027
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001028 Record *SrcRecord; // Originating Record for the pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +00001029 TreePatternNodePtr SrcPattern; // Source pattern to match.
1030 TreePatternNodePtr DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001031 std::vector<Predicate> Predicates; // Top level predicate conditions
1032 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001033 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001034 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001035 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001036 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001037
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001038 Record *getSrcRecord() const { return SrcRecord; }
Florian Hahn75e87c32018-05-30 21:00:18 +00001039 TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
1040 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
1041 TreePatternNode *getDstPattern() const { return DstPattern.get(); }
1042 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001043 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001044 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001045 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001046
1047 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001048
Chris Lattner05925fe2010-03-29 01:40:38 +00001049 /// Compute the complexity metric for the input pattern. This roughly
1050 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001051 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001052};
1053
Chris Lattnerab3242f2008-01-06 01:10:31 +00001054class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001055 RecordKeeper &Records;
1056 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001057 CodeGenIntrinsicTable Intrinsics;
1058 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001059
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001060 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001061 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1062 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001063 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001064 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1065 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001066 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1067 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001068
Chris Lattner8cab0212008-01-05 22:25:12 +00001069 // Specific SDNode definitions:
1070 Record *intrinsic_void_sdnode;
1071 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001072
Chris Lattner8cab0212008-01-05 22:25:12 +00001073 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1074 /// value is the pattern to match, the second pattern is the result to
1075 /// emit.
1076 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001077
1078 TypeSetByHwMode LegalVTS;
1079
Daniel Sanders7e523672017-11-11 03:23:44 +00001080 using PatternRewriterFn = std::function<void (TreePattern *)>;
1081 PatternRewriterFn PatternRewriter;
1082
Chris Lattner8cab0212008-01-05 22:25:12 +00001083public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001084 CodeGenDAGPatterns(RecordKeeper &R,
1085 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001086
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001087 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001088 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001089 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001090
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001091 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001092
Chris Lattner8cab0212008-01-05 22:25:12 +00001093 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001094 auto F = SDNodes.find(R);
1095 assert(F != SDNodes.end() && "Unknown node!");
1096 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001097 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001098
Chris Lattnercc43e792008-01-05 22:54:53 +00001099 // Node transformation lookups.
1100 typedef std::pair<Record*, std::string> NodeXForm;
1101 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001102 auto F = SDNodeXForms.find(R);
1103 assert(F != SDNodeXForms.end() && "Invalid transform!");
1104 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001105 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001106
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001107 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001108 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001109 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1110 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1111
Jim Grosbach50986b52010-12-24 05:06:32 +00001112
Chris Lattner8cab0212008-01-05 22:25:12 +00001113 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001114 auto F = ComplexPatterns.find(R);
1115 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1116 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001117 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001118
Chris Lattner8cab0212008-01-05 22:25:12 +00001119 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1120 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1121 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001122 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1123 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001124 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001125 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001126
Chris Lattner8cab0212008-01-05 22:25:12 +00001127 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001128 if (IID-1 < Intrinsics.size())
1129 return Intrinsics[IID-1];
1130 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1131 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001132 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001133 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001134
Chris Lattner8cab0212008-01-05 22:25:12 +00001135 unsigned getIntrinsicID(Record *R) const {
1136 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1137 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001138 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1139 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001140 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001141 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001142
Chris Lattner7ed81692010-02-18 06:47:49 +00001143 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001144 auto F = DefaultOperands.find(R);
1145 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1146 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001147 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001148
Chris Lattner8cab0212008-01-05 22:25:12 +00001149 // Pattern Fragment information.
1150 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001151 auto F = PatternFragments.find(R);
1152 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1153 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001154 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001155 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001156 auto F = PatternFragments.find(R);
1157 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001158 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001159 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001160 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001161
David Blaikiefcacc742014-11-13 21:56:57 +00001162 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1163 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001164 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1165 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001166 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001167
1168 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001169 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1170 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1171 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001172 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001173
Ahmed Bougacha14107512013-10-28 18:07:21 +00001174 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1175 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001176 void parseInstructionPattern(
Ahmed Bougacha14107512013-10-28 18:07:21 +00001177 CodeGenInstruction &CGI, ListInit *Pattern,
1178 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001179
Chris Lattner8cab0212008-01-05 22:25:12 +00001180 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001181 auto F = Instructions.find(R);
1182 assert(F != Instructions.end() && "Unknown instruction!");
1183 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001184 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001185
Chris Lattner8cab0212008-01-05 22:25:12 +00001186 Record *get_intrinsic_void_sdnode() const {
1187 return intrinsic_void_sdnode;
1188 }
1189 Record *get_intrinsic_w_chain_sdnode() const {
1190 return intrinsic_w_chain_sdnode;
1191 }
1192 Record *get_intrinsic_wo_chain_sdnode() const {
1193 return intrinsic_wo_chain_sdnode;
1194 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001195
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001196 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1197
Chris Lattner8cab0212008-01-05 22:25:12 +00001198private:
1199 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001200 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001201 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001202 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001203 void ParseDefaultOperands();
1204 void ParseInstructions();
1205 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001206 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001207 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001208 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001209 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001210
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001211 std::vector<Predicate> makePredList(ListInit *L);
1212
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001213 void ParseOnePattern(Record *TheDef,
1214 TreePattern &Pattern, TreePattern &Result,
1215 const std::vector<Record *> &InstImpResults);
Craig Topper18e6b572017-06-25 17:33:49 +00001216 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Florian Hahn75e87c32018-05-30 21:00:18 +00001217 void FindPatternInputsAndOutputs(
Florian Hahn6b1db822018-06-14 20:32:58 +00001218 TreePattern &I, TreePatternNodePtr Pat,
Florian Hahn75e87c32018-05-30 21:00:18 +00001219 std::map<std::string, TreePatternNodePtr> &InstInputs,
1220 std::map<std::string, TreePatternNodePtr> &InstResults,
1221 std::vector<Record *> &InstImpResults);
Chris Lattner8cab0212008-01-05 22:25:12 +00001222};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001223
1224
Florian Hahn6b1db822018-06-14 20:32:58 +00001225inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001226 TreePattern &TP) const {
1227 bool MadeChange = false;
1228 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1229 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1230 return MadeChange;
1231 }
Matt Arsenault303327d2017-12-20 19:36:28 +00001232
Chris Lattner8cab0212008-01-05 22:25:12 +00001233} // end namespace llvm
1234
1235#endif