blob: 80fc932a7a50250f9c453c277dd807d82d246fb2 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner8cab0212008-01-05 22:25:12 +00006//
7//===----------------------------------------------------------------------===//
8//
Chris Lattnerab3242f2008-01-06 01:10:31 +00009// This file declares the CodeGenDAGPatterns class, which is used to read and
Chris Lattner8cab0212008-01-05 22:25:12 +000010// represent the patterns present in a .td file for instructions.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
15#define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
Chris Lattner8cab0212008-01-05 22:25:12 +000016
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +000017#include "CodeGenHwModes.h"
Chris Lattner8cab0212008-01-05 22:25:12 +000018#include "CodeGenIntrinsics.h"
Chandler Carruth91d19d82012-12-04 10:37:14 +000019#include "CodeGenTarget.h"
Matt Arsenault303327d2017-12-20 19:36:28 +000020#include "SDNodeProperties.h"
Craig Topperbd199f82018-12-05 00:47:59 +000021#include "llvm/ADT/MapVector.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>
Craig Topperbd199f82018-12-05 00:47:59 +000031#include <numeric>
Chandler Carruth91d19d82012-12-04 10:37:14 +000032#include <set>
33#include <vector>
Chris Lattner8cab0212008-01-05 22:25:12 +000034
35namespace llvm {
Chris Lattner8cab0212008-01-05 22:25:12 +000036
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000037class Record;
38class Init;
39class ListInit;
40class DagInit;
41class SDNodeInfo;
42class TreePattern;
43class TreePatternNode;
44class CodeGenDAGPatterns;
45class ComplexPattern;
46
Florian Hahn75e87c32018-05-30 21:00:18 +000047/// Shared pointer for TreePatternNode.
48using TreePatternNodePtr = std::shared_ptr<TreePatternNode>;
49
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000050/// This represents a set of MVTs. Since the underlying type for the MVT
51/// is uint8_t, there are at most 256 values. To reduce the number of memory
52/// allocations and deallocations, represent the set as a sequence of bits.
53/// To reduce the allocations even further, make MachineValueTypeSet own
54/// the storage and use std::array as the bit container.
55struct MachineValueTypeSet {
56 static_assert(std::is_same<std::underlying_type<MVT::SimpleValueType>::type,
57 uint8_t>::value,
58 "Change uint8_t here to the SimpleValueType's type");
59 static unsigned constexpr Capacity = std::numeric_limits<uint8_t>::max()+1;
60 using WordType = uint64_t;
Craig Topperd022d252017-09-21 04:55:04 +000061 static unsigned constexpr WordWidth = CHAR_BIT*sizeof(WordType);
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +000062 static unsigned constexpr NumWords = Capacity/WordWidth;
63 static_assert(NumWords*WordWidth == Capacity,
64 "Capacity should be a multiple of WordWidth");
65
66 LLVM_ATTRIBUTE_ALWAYS_INLINE
67 MachineValueTypeSet() {
68 clear();
69 }
70
71 LLVM_ATTRIBUTE_ALWAYS_INLINE
72 unsigned size() const {
73 unsigned Count = 0;
74 for (WordType W : Words)
75 Count += countPopulation(W);
76 return Count;
77 }
78 LLVM_ATTRIBUTE_ALWAYS_INLINE
79 void clear() {
80 std::memset(Words.data(), 0, NumWords*sizeof(WordType));
81 }
82 LLVM_ATTRIBUTE_ALWAYS_INLINE
83 bool empty() const {
84 for (WordType W : Words)
85 if (W != 0)
86 return false;
87 return true;
88 }
89 LLVM_ATTRIBUTE_ALWAYS_INLINE
90 unsigned count(MVT T) const {
91 return (Words[T.SimpleTy / WordWidth] >> (T.SimpleTy % WordWidth)) & 1;
92 }
93 std::pair<MachineValueTypeSet&,bool> insert(MVT T) {
94 bool V = count(T.SimpleTy);
95 Words[T.SimpleTy / WordWidth] |= WordType(1) << (T.SimpleTy % WordWidth);
96 return {*this, V};
97 }
98 MachineValueTypeSet &insert(const MachineValueTypeSet &S) {
99 for (unsigned i = 0; i != NumWords; ++i)
100 Words[i] |= S.Words[i];
101 return *this;
102 }
103 LLVM_ATTRIBUTE_ALWAYS_INLINE
104 void erase(MVT T) {
105 Words[T.SimpleTy / WordWidth] &= ~(WordType(1) << (T.SimpleTy % WordWidth));
106 }
107
108 struct const_iterator {
109 // Some implementations of the C++ library require these traits to be
110 // defined.
111 using iterator_category = std::forward_iterator_tag;
112 using value_type = MVT;
113 using difference_type = ptrdiff_t;
114 using pointer = const MVT*;
115 using reference = const MVT&;
116
117 LLVM_ATTRIBUTE_ALWAYS_INLINE
118 MVT operator*() const {
119 assert(Pos != Capacity);
120 return MVT::SimpleValueType(Pos);
121 }
122 LLVM_ATTRIBUTE_ALWAYS_INLINE
123 const_iterator(const MachineValueTypeSet *S, bool End) : Set(S) {
124 Pos = End ? Capacity : find_from_pos(0);
125 }
126 LLVM_ATTRIBUTE_ALWAYS_INLINE
127 const_iterator &operator++() {
128 assert(Pos != Capacity);
129 Pos = find_from_pos(Pos+1);
130 return *this;
131 }
132
133 LLVM_ATTRIBUTE_ALWAYS_INLINE
134 bool operator==(const const_iterator &It) const {
135 return Set == It.Set && Pos == It.Pos;
136 }
137 LLVM_ATTRIBUTE_ALWAYS_INLINE
138 bool operator!=(const const_iterator &It) const {
139 return !operator==(It);
140 }
141
142 private:
143 unsigned find_from_pos(unsigned P) const {
144 unsigned SkipWords = P / WordWidth;
145 unsigned SkipBits = P % WordWidth;
146 unsigned Count = SkipWords * WordWidth;
147
148 // If P is in the middle of a word, process it manually here, because
149 // the trailing bits need to be masked off to use findFirstSet.
150 if (SkipBits != 0) {
151 WordType W = Set->Words[SkipWords];
152 W &= maskLeadingOnes<WordType>(WordWidth-SkipBits);
153 if (W != 0)
154 return Count + findFirstSet(W);
155 Count += WordWidth;
156 SkipWords++;
157 }
158
159 for (unsigned i = SkipWords; i != NumWords; ++i) {
160 WordType W = Set->Words[i];
161 if (W != 0)
162 return Count + findFirstSet(W);
163 Count += WordWidth;
164 }
165 return Capacity;
166 }
167
168 const MachineValueTypeSet *Set;
169 unsigned Pos;
170 };
171
172 LLVM_ATTRIBUTE_ALWAYS_INLINE
173 const_iterator begin() const { return const_iterator(this, false); }
174 LLVM_ATTRIBUTE_ALWAYS_INLINE
175 const_iterator end() const { return const_iterator(this, true); }
176
177 LLVM_ATTRIBUTE_ALWAYS_INLINE
178 bool operator==(const MachineValueTypeSet &S) const {
179 return Words == S.Words;
180 }
181 LLVM_ATTRIBUTE_ALWAYS_INLINE
182 bool operator!=(const MachineValueTypeSet &S) const {
183 return !operator==(S);
184 }
185
186private:
187 friend struct const_iterator;
188 std::array<WordType,NumWords> Words;
189};
190
191struct TypeSetByHwMode : public InfoByHwMode<MachineValueTypeSet> {
192 using SetType = MachineValueTypeSet;
Tom Stellard9ad714f2019-02-20 19:43:47 +0000193 std::vector<unsigned> AddrSpaces;
Jim Grosbach50986b52010-12-24 05:06:32 +0000194
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000195 TypeSetByHwMode() = default;
196 TypeSetByHwMode(const TypeSetByHwMode &VTS) = default;
197 TypeSetByHwMode(MVT::SimpleValueType VT)
198 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
199 TypeSetByHwMode(ValueTypeByHwMode VT)
200 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
201 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000202
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000203 SetType &getOrCreate(unsigned Mode) {
204 if (hasMode(Mode))
205 return get(Mode);
206 return Map.insert({Mode,SetType()}).first->second;
207 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000208
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000209 bool isValueTypeByHwMode(bool AllowEmpty) const;
210 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000211
212 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000213 bool isMachineValueType() const {
214 return isDefaultOnly() && Map.begin()->second.size() == 1;
215 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000216
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000217 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000218 MVT getMachineValueType() const {
219 assert(isMachineValueType());
220 return *Map.begin()->second.begin();
221 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000222
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000223 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000224
225 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000226 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000227 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000228 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000229
Tom Stellard9ad714f2019-02-20 19:43:47 +0000230 bool isPointer() const {
231 return getValueTypeByHwMode().isPointer();
232 }
233
234 unsigned getPtrAddrSpace() const {
235 assert(isPointer());
236 return getValueTypeByHwMode().PtrAddrSpace;
237 }
238
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000239 bool insert(const ValueTypeByHwMode &VVT);
240 bool constrain(const TypeSetByHwMode &VTS);
241 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000242 template <typename Predicate>
243 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000244
Zachary Turner249dc142017-09-20 18:01:40 +0000245 void writeToStream(raw_ostream &OS) const;
246 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000247
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000248 bool operator==(const TypeSetByHwMode &VTS) const;
249 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000250
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000251 void dump() const;
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000252 bool validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000253
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000254private:
Tom Stellard9ad714f2019-02-20 19:43:47 +0000255 unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000256 /// Intersect two sets. Return true if anything has changed.
257 bool intersect(SetType &Out, const SetType &In);
258};
Jim Grosbach50986b52010-12-24 05:06:32 +0000259
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000260raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
261
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000262struct TypeInfer {
263 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000264
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000265 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
266 return VTS.isValueTypeByHwMode(AllowEmpty);
267 }
268 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
269 bool AllowEmpty) const {
270 assert(VTS.isValueTypeByHwMode(AllowEmpty));
271 return VTS.getValueTypeByHwMode();
272 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000273
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000274 /// The protocol in the following functions (Merge*, force*, Enforce*,
275 /// expand*) is to return "true" if a change has been made, "false"
276 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000277
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000278 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
279 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
280 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
281 }
282 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
283 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
284 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000285
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000286 /// Reduce the set \p Out to have at most one element for each mode.
287 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000288
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000289 /// The following four functions ensure that upon return the set \p Out
290 /// will only contain types of the specified kind: integer, floating-point,
291 /// scalar, or vector.
292 /// If \p Out is empty, all legal types of the specified kind will be added
293 /// to it. Otherwise, all types that are not of the specified kind will be
294 /// removed from \p Out.
295 bool EnforceInteger(TypeSetByHwMode &Out);
296 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
297 bool EnforceScalar(TypeSetByHwMode &Out);
298 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000299
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000300 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
301 /// unchanged.
302 bool EnforceAny(TypeSetByHwMode &Out);
303 /// Make sure that for each type in \p Small, there exists a larger type
304 /// in \p Big.
305 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
306 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
307 /// for each type U in \p Elem, U is a scalar type.
308 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
309 /// (vector) type T in \p Vec, such that U is the element type of T.
310 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
311 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
312 const ValueTypeByHwMode &VVT);
313 /// Ensure that for each type T in \p Sub, T is a vector type, and there
314 /// exists a type U in \p Vec such that U is a vector type with the same
315 /// element type as T and at least as many elements as T.
316 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
317 TypeSetByHwMode &Sub);
318 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
319 /// 2. Ensure that for each vector type T in \p V, there exists a vector
320 /// type U in \p W, such that T and U have the same number of elements.
321 /// 3. Ensure that for each vector type U in \p W, there exists a vector
322 /// type T in \p V, such that T and U have the same number of elements
323 /// (reverse of 2).
324 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
325 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
326 /// such that T and U have equal size in bits.
327 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
328 /// such that T and U have equal size in bits (reverse of 1).
329 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000330
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000331 /// For each overloaded type (i.e. of form *Any), replace it with the
332 /// corresponding subset of legal, specific types.
333 void expandOverloads(TypeSetByHwMode &VTS);
334 void expandOverloads(TypeSetByHwMode::SetType &Out,
335 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000336
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000337 struct ValidateOnExit {
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000338 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
339 #ifndef NDEBUG
340 ~ValidateOnExit();
341 #else
342 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
343 #endif
344 TypeInfer &Infer;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000345 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000346 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000347
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000348 struct SuppressValidation {
349 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
350 Infer.Validate = false;
351 }
352 ~SuppressValidation() {
353 Infer.Validate = SavedValidate;
354 }
355 TypeInfer &Infer;
356 bool SavedValidate;
357 };
358
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000359 TreePattern &TP;
360 unsigned ForceMode; // Mode to use when set.
361 bool CodeGen = false; // Set during generation of matcher code.
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000362 bool Validate = true; // Indicate whether to validate types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000363
364private:
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000365 const TypeSetByHwMode &getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000366
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000367 /// Cached legal types (in default mode).
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000368 bool LegalTypesCached = false;
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000369 TypeSetByHwMode LegalCache;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000370};
Chris Lattner8cab0212008-01-05 22:25:12 +0000371
Scott Michel94420742008-03-05 17:49:05 +0000372/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000373typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000374
Chris Lattner8cab0212008-01-05 22:25:12 +0000375/// SDTypeConstraint - This is a discriminated union of constraints,
376/// corresponding to the SDTypeConstraint tablegen class in Target.td.
377struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000378 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000379
Chris Lattner8cab0212008-01-05 22:25:12 +0000380 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000381 enum {
382 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000383 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000384 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000385 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000386
Chris Lattner8cab0212008-01-05 22:25:12 +0000387 union { // The discriminated union.
388 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000389 unsigned OtherOperandNum;
390 } SDTCisSameAs_Info;
391 struct {
392 unsigned OtherOperandNum;
393 } SDTCisVTSmallerThanOp_Info;
394 struct {
395 unsigned BigOperandNum;
396 } SDTCisOpSmallerThanOp_Info;
397 struct {
398 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000399 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000400 struct {
401 unsigned OtherOperandNum;
402 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000403 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000404 unsigned OtherOperandNum;
405 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000406 struct {
407 unsigned OtherOperandNum;
408 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000409 } x;
410
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000411 // The VT for SDTCisVT and SDTCVecEltisVT.
412 // Must not be in the union because it has a non-trivial destructor.
413 ValueTypeByHwMode VVT;
414
Chris Lattner8cab0212008-01-05 22:25:12 +0000415 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
416 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000417 /// change, false otherwise. If a type contradiction is found, an error
418 /// is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000419 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
Chris Lattner8cab0212008-01-05 22:25:12 +0000420 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000421};
422
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000423/// ScopedName - A name of a node associated with a "scope" that indicates
424/// the context (e.g. instance of Pattern or PatFrag) in which the name was
425/// used. This enables substitution of pattern fragments while keeping track
426/// of what name(s) were originally given to various nodes in the tree.
427class ScopedName {
428 unsigned Scope;
429 std::string Identifier;
430public:
431 ScopedName(unsigned Scope, StringRef Identifier)
432 : Scope(Scope), Identifier(Identifier) {
433 assert(Scope != 0 &&
434 "Scope == 0 is used to indicate predicates without arguments");
435 }
436
437 unsigned getScope() const { return Scope; }
438 const std::string &getIdentifier() const { return Identifier; }
439
440 std::string getFullName() const;
441
442 bool operator==(const ScopedName &o) const;
443 bool operator!=(const ScopedName &o) const;
444};
445
Chris Lattner8cab0212008-01-05 22:25:12 +0000446/// SDNodeInfo - One of these records is created for each SDNode instance in
447/// the target .td file. This represents the various dag nodes we will be
448/// processing.
449class SDNodeInfo {
450 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000451 StringRef EnumName;
452 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000453 unsigned Properties;
454 unsigned NumResults;
455 int NumOperands;
456 std::vector<SDTypeConstraint> TypeConstraints;
457public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000458 // Parse the specified record.
459 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000460
Chris Lattner8cab0212008-01-05 22:25:12 +0000461 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000462
Chris Lattner135091b2010-03-28 08:48:47 +0000463 /// getNumOperands - This is the number of operands required or -1 if
464 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000465 int getNumOperands() const { return NumOperands; }
466 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000467 StringRef getEnumName() const { return EnumName; }
468 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000469
Chris Lattner8cab0212008-01-05 22:25:12 +0000470 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
471 return TypeConstraints;
472 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000473
Chris Lattner99e53b32010-02-28 00:22:30 +0000474 /// getKnownType - If the type constraints on this node imply a fixed type
475 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000476 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000477 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000478
Chris Lattner8cab0212008-01-05 22:25:12 +0000479 /// hasProperty - Return true if this node has the specified property.
480 ///
481 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
482
483 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
484 /// constraints for this node to the operands of the node. This returns
485 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000486 /// found, an error is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000487 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000488};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000489
Chris Lattner514e2922011-04-17 21:38:24 +0000490/// TreePredicateFn - This is an abstraction that represents the predicates on
491/// a PatFrag node. This is a simple one-word wrapper around a pointer to
492/// provide nice accessors.
493class TreePredicateFn {
494 /// PatFragRec - This is the TreePattern for the PatFrag that we
495 /// originally came from.
496 TreePattern *PatFragRec;
497public:
498 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000499 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000500
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000501
Chris Lattner514e2922011-04-17 21:38:24 +0000502 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000503
Chris Lattner514e2922011-04-17 21:38:24 +0000504 /// isAlwaysTrue - Return true if this is a noop predicate.
505 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000506
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000507 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000508
Chris Lattner07add492011-04-18 06:22:33 +0000509 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
510 /// this is an immediate predicate. It is an error to call this on a
511 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000512 std::string getImmediatePredicateCode() const {
513 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000514 assert(!Result.empty() && "Isn't an immediate pattern!");
515 return Result;
516 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000517
Chris Lattner514e2922011-04-17 21:38:24 +0000518 bool operator==(const TreePredicateFn &RHS) const {
519 return PatFragRec == RHS.PatFragRec;
520 }
521
522 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
523
524 /// Return the name to use in the generated code to reference this, this is
525 /// "Predicate_foo" if from a pattern fragment "foo".
526 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000527
Chris Lattner514e2922011-04-17 21:38:24 +0000528 /// getCodeToRunOnSDNode - Return the code for the function body that
529 /// evaluates this predicate. The argument is expected to be in "Node",
530 /// not N. This handles casting and conversion to a concrete node type as
531 /// appropriate.
532 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000533
Daniel Sanders649c5852017-10-13 20:42:18 +0000534 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000535 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000536
Daniel Sanders11300ce2017-10-13 21:28:03 +0000537 /// Get a string that describes the type returned by getImmType() but is
538 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000539 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000540
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000541 // Predicate code uses the PatFrag's captured operands.
542 bool usesOperands() const;
543
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000544 // Is the desired predefined predicate for a load?
545 bool isLoad() const;
546 // Is the desired predefined predicate for a store?
547 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000548 // Is the desired predefined predicate for an atomic?
549 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000550
551 /// Is this predicate the predefined unindexed load predicate?
552 /// Is this predicate the predefined unindexed store predicate?
553 bool isUnindexed() const;
554 /// Is this predicate the predefined non-extending load predicate?
555 bool isNonExtLoad() const;
556 /// Is this predicate the predefined any-extend load predicate?
557 bool isAnyExtLoad() const;
558 /// Is this predicate the predefined sign-extend load predicate?
559 bool isSignExtLoad() const;
560 /// Is this predicate the predefined zero-extend load predicate?
561 bool isZeroExtLoad() const;
562 /// Is this predicate the predefined non-truncating store predicate?
563 bool isNonTruncStore() const;
564 /// Is this predicate the predefined truncating store predicate?
565 bool isTruncStore() const;
566
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000567 /// Is this predicate the predefined monotonic atomic predicate?
568 bool isAtomicOrderingMonotonic() const;
569 /// Is this predicate the predefined acquire atomic predicate?
570 bool isAtomicOrderingAcquire() const;
571 /// Is this predicate the predefined release atomic predicate?
572 bool isAtomicOrderingRelease() const;
573 /// Is this predicate the predefined acquire-release atomic predicate?
574 bool isAtomicOrderingAcquireRelease() const;
575 /// Is this predicate the predefined sequentially consistent atomic predicate?
576 bool isAtomicOrderingSequentiallyConsistent() const;
577
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000578 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
579 bool isAtomicOrderingAcquireOrStronger() const;
580 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
581 bool isAtomicOrderingWeakerThanAcquire() const;
582
583 /// Is this predicate the predefined release-or-stronger atomic predicate?
584 bool isAtomicOrderingReleaseOrStronger() const;
585 /// Is this predicate the predefined weaker-than-release atomic predicate?
586 bool isAtomicOrderingWeakerThanRelease() const;
587
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000588 /// If non-null, indicates that this predicate is a predefined memory VT
589 /// predicate for a load/store and returns the ValueType record for the memory VT.
590 Record *getMemoryVT() const;
591 /// If non-null, indicates that this predicate is a predefined memory VT
592 /// predicate (checking only the scalar type) for load/store and returns the
593 /// ValueType record for the memory VT.
594 Record *getScalarMemoryVT() const;
595
Matt Arsenaultd00d8572019-07-15 20:59:42 +0000596 ListInit *getAddressSpaces() const;
Matt Arsenault52c26242019-07-31 00:14:43 +0000597 int64_t getMinAlignment() const;
Matt Arsenaultd00d8572019-07-15 20:59:42 +0000598
Daniel Sanders8ead1292018-06-15 23:13:43 +0000599 // If true, indicates that GlobalISel-based C++ code was supplied.
600 bool hasGISelPredicateCode() const;
601 std::string getGISelPredicateCode() const;
602
Chris Lattner514e2922011-04-17 21:38:24 +0000603private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000604 bool hasPredCode() const;
605 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000606 std::string getPredCode() const;
607 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000608 bool immCodeUsesAPInt() const;
609 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000610
611 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000612};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000613
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000614struct TreePredicateCall {
615 TreePredicateFn Fn;
616
617 // Scope -- unique identifier for retrieving named arguments. 0 is used when
618 // the predicate does not use named arguments.
619 unsigned Scope;
620
621 TreePredicateCall(const TreePredicateFn &Fn, unsigned Scope)
622 : Fn(Fn), Scope(Scope) {}
623
624 bool operator==(const TreePredicateCall &o) const {
625 return Fn == o.Fn && Scope == o.Scope;
626 }
627 bool operator!=(const TreePredicateCall &o) const {
628 return !(*this == o);
629 }
630};
Chris Lattner8cab0212008-01-05 22:25:12 +0000631
Chris Lattner8cab0212008-01-05 22:25:12 +0000632class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000633 /// The type of each node result. Before and during type inference, each
634 /// result may be a set of possible types. After (successful) type inference,
635 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000636 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000637
Craig Topperbd199f82018-12-05 00:47:59 +0000638 /// The index of each result in results of the pattern.
639 std::vector<unsigned> ResultPerm;
640
Chris Lattner8cab0212008-01-05 22:25:12 +0000641 /// Operator - The Record for the operator if this is an interior node (not
642 /// a leaf).
643 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000644
Chris Lattner8cab0212008-01-05 22:25:12 +0000645 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
646 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000647 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000648
Chris Lattner8cab0212008-01-05 22:25:12 +0000649 /// Name - The name given to this node with the :$foo notation.
650 ///
651 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000652
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000653 std::vector<ScopedName> NamesAsPredicateArg;
654
655 /// PredicateCalls - The predicate functions to execute on this node to check
Dan Gohman6e979022008-10-15 06:17:21 +0000656 /// for a match. If this list is empty, no predicate is involved.
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000657 std::vector<TreePredicateCall> PredicateCalls;
Jim Grosbach50986b52010-12-24 05:06:32 +0000658
Chris Lattner8cab0212008-01-05 22:25:12 +0000659 /// TransformFn - The transformation function to execute on this node before
660 /// it can be substituted into the resulting instruction on a pattern match.
661 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000662
Florian Hahn75e87c32018-05-30 21:00:18 +0000663 std::vector<TreePatternNodePtr> Children;
664
Chris Lattner8cab0212008-01-05 22:25:12 +0000665public:
Craig Topper26fc06352018-07-15 06:52:49 +0000666 TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000667 unsigned NumResults)
Craig Topper26fc06352018-07-15 06:52:49 +0000668 : Operator(Op), Val(nullptr), TransformFn(nullptr),
669 Children(std::move(Ch)) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000670 Types.resize(NumResults);
Craig Topperbd199f82018-12-05 00:47:59 +0000671 ResultPerm.resize(NumResults);
672 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
Chris Lattnerf1447252010-03-19 21:37:09 +0000673 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000674 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000675 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000676 Types.resize(NumResults);
Craig Topperbd199f82018-12-05 00:47:59 +0000677 ResultPerm.resize(NumResults);
678 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
Chris Lattner8cab0212008-01-05 22:25:12 +0000679 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000680
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000681 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000682 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000683 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000684
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000685 const std::vector<ScopedName> &getNamesAsPredicateArg() const {
686 return NamesAsPredicateArg;
687 }
688 void setNamesAsPredicateArg(const std::vector<ScopedName>& Names) {
689 NamesAsPredicateArg = Names;
690 }
691 void addNameAsPredicateArg(const ScopedName &N) {
692 NamesAsPredicateArg.push_back(N);
693 }
694
Craig Topperada08572014-04-16 04:21:27 +0000695 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000696
Chris Lattnercabe0372010-03-15 06:00:16 +0000697 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000698 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000699 ValueTypeByHwMode getType(unsigned ResNo) const {
700 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000701 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000702 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
703 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
704 return Types[ResNo];
705 }
706 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
707 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
708 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
709 return Types[ResNo].getMachineValueType().SimpleTy;
710 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000711
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000712 bool hasConcreteType(unsigned ResNo) const {
713 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000714 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000715 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
716 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000717 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000718
Craig Topperbd199f82018-12-05 00:47:59 +0000719 unsigned getNumResults() const { return ResultPerm.size(); }
720 unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
721 void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
722
David Greeneaf8ee2c2011-07-29 22:43:06 +0000723 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000724 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000725
Chris Lattner8cab0212008-01-05 22:25:12 +0000726 unsigned getNumChildren() const { return Children.size(); }
Florian Hahn6b1db822018-06-14 20:32:58 +0000727 TreePatternNode *getChild(unsigned N) const { return Children[N].get(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000728 const TreePatternNodePtr &getChildShared(unsigned N) const {
729 return Children[N];
Chris Lattner8cab0212008-01-05 22:25:12 +0000730 }
Florian Hahn75e87c32018-05-30 21:00:18 +0000731 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000732
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000733 /// hasChild - Return true if N is any of our children.
734 bool hasChild(const TreePatternNode *N) const {
735 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Florian Hahn75e87c32018-05-30 21:00:18 +0000736 if (Children[i].get() == N)
737 return true;
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000738 return false;
739 }
Chris Lattner89c65662008-01-06 05:36:50 +0000740
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000741 bool hasProperTypeByHwMode() const;
742 bool hasPossibleType() const;
743 bool setDefaultMode(unsigned Mode);
744
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000745 bool hasAnyPredicate() const { return !PredicateCalls.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000746
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000747 const std::vector<TreePredicateCall> &getPredicateCalls() const {
748 return PredicateCalls;
Chris Lattner514e2922011-04-17 21:38:24 +0000749 }
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000750 void clearPredicateCalls() { PredicateCalls.clear(); }
751 void setPredicateCalls(const std::vector<TreePredicateCall> &Calls) {
752 assert(PredicateCalls.empty() && "Overwriting non-empty predicate list!");
753 PredicateCalls = Calls;
Dan Gohman6e979022008-10-15 06:17:21 +0000754 }
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000755 void addPredicateCall(const TreePredicateCall &Call) {
756 assert(!Call.Fn.isAlwaysTrue() && "Empty predicate string!");
757 assert(!is_contained(PredicateCalls, Call) && "predicate applied recursively");
758 PredicateCalls.push_back(Call);
759 }
760 void addPredicateCall(const TreePredicateFn &Fn, unsigned Scope) {
761 assert((Scope != 0) == Fn.usesOperands());
762 addPredicateCall(TreePredicateCall(Fn, Scope));
Dan Gohman6e979022008-10-15 06:17:21 +0000763 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000764
765 Record *getTransformFn() const { return TransformFn; }
766 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000767
Chris Lattner89c65662008-01-06 05:36:50 +0000768 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
769 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
770 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000771
Chris Lattner53c39ba2010-02-14 22:22:58 +0000772 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
773 /// return the ComplexPattern information, otherwise return null.
774 const ComplexPattern *
775 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
776
Tim Northoverc807a172014-05-20 11:52:46 +0000777 /// Returns the number of MachineInstr operands that would be produced by this
778 /// node if it mapped directly to an output Instruction's
779 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
780 /// for Operands; otherwise 1.
781 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
782
Chris Lattner53c39ba2010-02-14 22:22:58 +0000783 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000784 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000785
Chris Lattner53c39ba2010-02-14 22:22:58 +0000786 /// TreeHasProperty - Return true if any node in this tree has the specified
787 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000788 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000789
Evan Cheng49bad4c2008-06-16 20:29:38 +0000790 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
791 /// marked isCommutative.
792 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000793
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000794 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000795 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000796
Chris Lattner8cab0212008-01-05 22:25:12 +0000797public: // Higher level manipulation routines.
798
799 /// clone - Return a new copy of this tree.
800 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000801 TreePatternNodePtr clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000802
803 /// RemoveAllTypes - Recursively strip all the types of this tree.
804 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000805
Chris Lattner8cab0212008-01-05 22:25:12 +0000806 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
807 /// the specified node. For this comparison, all of the state of the node
808 /// is considered, except for the assigned name. Nodes with differing names
809 /// that are otherwise identical are considered isomorphic.
Florian Hahn6b1db822018-06-14 20:32:58 +0000810 bool isIsomorphicTo(const TreePatternNode *N,
Scott Michel94420742008-03-05 17:49:05 +0000811 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000812
Chris Lattner8cab0212008-01-05 22:25:12 +0000813 /// SubstituteFormalArguments - Replace the formal arguments in this tree
814 /// with actual values specified by ArgMap.
Florian Hahn75e87c32018-05-30 21:00:18 +0000815 void
816 SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
Chris Lattner8cab0212008-01-05 22:25:12 +0000817
818 /// InlinePatternFragments - If this pattern refers to any pattern
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000819 /// fragments, return the set of inlined versions (this can be more than
820 /// one if a PatFrags record has multiple alternatives).
821 void InlinePatternFragments(TreePatternNodePtr T,
822 TreePattern &TP,
823 std::vector<TreePatternNodePtr> &OutAlternatives);
Jim Grosbach50986b52010-12-24 05:06:32 +0000824
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000825 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000826 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000827 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000828 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000829
Chris Lattner8cab0212008-01-05 22:25:12 +0000830 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000831 /// information. If N already contains a conflicting type, then flag an
832 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000833 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000834 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
835 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000836 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000837 TreePattern &TP);
838 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
839 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000840
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000841 // Update node type with types inferred from an instruction operand or result
842 // def from the ins/outs lists.
843 // Return true if the type changed.
844 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
845
Chris Lattner8cab0212008-01-05 22:25:12 +0000846 /// ContainsUnresolvedType - Return true if this tree contains any
847 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000848 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000849
Chris Lattner8cab0212008-01-05 22:25:12 +0000850 /// canPatternMatch - If it is impossible for this pattern to match on this
851 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000852 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000853};
854
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000855inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
856 TPN.print(OS);
857 return OS;
858}
Jim Grosbach50986b52010-12-24 05:06:32 +0000859
Chris Lattner8cab0212008-01-05 22:25:12 +0000860
861/// TreePattern - Represent a pattern, used for instructions, pattern
862/// fragments, etc.
863///
864class TreePattern {
865 /// Trees - The list of pattern trees which corresponds to this pattern.
866 /// Note that PatFrag's only have a single tree.
867 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000868 std::vector<TreePatternNodePtr> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000869
Chris Lattnercabe0372010-03-15 06:00:16 +0000870 /// NamedNodes - This is all of the nodes that have names in the trees in this
871 /// pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +0000872 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000873
Chris Lattner8cab0212008-01-05 22:25:12 +0000874 /// TheRecord - The actual TableGen record corresponding to this pattern.
875 ///
876 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000877
Chris Lattner8cab0212008-01-05 22:25:12 +0000878 /// Args - This is a list of all of the arguments to this pattern (for
879 /// PatFrag patterns), which are the 'node' markers in this pattern.
880 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000881
Chris Lattner8cab0212008-01-05 22:25:12 +0000882 /// CDP - the top-level object coordinating this madness.
883 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000884 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000885
886 /// isInputPattern - True if this is an input pattern, something to match.
887 /// False if this is an output pattern, something to emit.
888 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000889
890 /// hasError - True if the currently processed nodes have unresolvable types
891 /// or other non-fatal errors
892 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000893
894 /// It's important that the usage of operands in ComplexPatterns is
895 /// consistent: each named operand can be defined by at most one
896 /// ComplexPattern. This records the ComplexPattern instance and the operand
897 /// number for each operand encountered in a ComplexPattern to aid in that
898 /// check.
899 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000900
901 TypeInfer Infer;
902
Chris Lattner8cab0212008-01-05 22:25:12 +0000903public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000904
Chris Lattner8cab0212008-01-05 22:25:12 +0000905 /// TreePattern constructor - Parse the specified DagInits into the
906 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000907 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000908 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000909 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000910 CodeGenDAGPatterns &ise);
Florian Hahn75e87c32018-05-30 21:00:18 +0000911 TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
David Blaikiecf195302014-11-17 22:55:41 +0000912 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000913
Chris Lattner8cab0212008-01-05 22:25:12 +0000914 /// getTrees - Return the tree patterns which corresponds to this pattern.
915 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000916 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000917 unsigned getNumTrees() const { return Trees.size(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000918 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
Florian Hahn53b14db2018-06-10 21:06:24 +0000919 void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
Florian Hahn4dd569c2018-06-13 20:59:53 +0000920 const TreePatternNodePtr &getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000921 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
922 return Trees[0];
923 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000924
Florian Hahn75e87c32018-05-30 21:00:18 +0000925 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
Chris Lattnercabe0372010-03-15 06:00:16 +0000926 if (NamedNodes.empty())
927 ComputeNamedNodes();
928 return NamedNodes;
929 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000930
Chris Lattner8cab0212008-01-05 22:25:12 +0000931 /// getRecord - Return the actual TableGen record corresponding to this
932 /// pattern.
933 ///
934 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000935
Chris Lattner8cab0212008-01-05 22:25:12 +0000936 unsigned getNumArgs() const { return Args.size(); }
937 const std::string &getArgName(unsigned i) const {
938 assert(i < Args.size() && "Argument reference out of range!");
939 return Args[i];
940 }
941 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000942
Chris Lattnerab3242f2008-01-06 01:10:31 +0000943 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000944
945 /// InlinePatternFragments - If this pattern refers to any pattern
946 /// fragments, inline them into place, giving us a pattern without any
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000947 /// PatFrags references. This may increase the number of trees in the
948 /// pattern if a PatFrags has multiple alternatives.
Chris Lattner8cab0212008-01-05 22:25:12 +0000949 void InlinePatternFragments() {
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000950 std::vector<TreePatternNodePtr> Copy = Trees;
951 Trees.clear();
952 for (unsigned i = 0, e = Copy.size(); i != e; ++i)
953 Copy[i]->InlinePatternFragments(Copy[i], *this, Trees);
Chris Lattner8cab0212008-01-05 22:25:12 +0000954 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000955
Chris Lattner8cab0212008-01-05 22:25:12 +0000956 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000957 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000958 /// otherwise. Bail out if a type contradiction is found.
Florian Hahn75e87c32018-05-30 21:00:18 +0000959 bool InferAllTypes(
960 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000961
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000962 /// error - If this is the first error in the current resolution step,
963 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000964 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000965 bool hasError() const {
966 return HasError;
967 }
968 void resetError() {
969 HasError = false;
970 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000971
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000972 TypeInfer &getInfer() { return Infer; }
973
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000974 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000975 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000976
Chris Lattner8cab0212008-01-05 22:25:12 +0000977private:
Florian Hahn75e87c32018-05-30 21:00:18 +0000978 TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000979 void ComputeNamedNodes();
Florian Hahn6b1db822018-06-14 20:32:58 +0000980 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000981};
982
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000983
984inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
985 const TypeSetByHwMode &InTy,
986 TreePattern &TP) {
987 TypeSetByHwMode VTS(InTy);
988 TP.getInfer().expandOverloads(VTS);
989 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
990}
991
992inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
993 MVT::SimpleValueType InTy,
994 TreePattern &TP) {
995 TypeSetByHwMode VTS(InTy);
996 TP.getInfer().expandOverloads(VTS);
997 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
998}
999
1000inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
1001 ValueTypeByHwMode InTy,
1002 TreePattern &TP) {
1003 TypeSetByHwMode VTS(InTy);
1004 TP.getInfer().expandOverloads(VTS);
1005 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
1006}
1007
1008
Tom Stellardb7246a72012-09-06 14:15:52 +00001009/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
1010/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +00001011struct DAGDefaultOperand {
Florian Hahn75e87c32018-05-30 21:00:18 +00001012 std::vector<TreePatternNodePtr> DefaultOps;
Chris Lattner8cab0212008-01-05 22:25:12 +00001013};
1014
1015class DAGInstruction {
Chris Lattner8cab0212008-01-05 22:25:12 +00001016 std::vector<Record*> Results;
1017 std::vector<Record*> Operands;
1018 std::vector<Record*> ImpResults;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001019 TreePatternNodePtr SrcPattern;
Florian Hahn75e87c32018-05-30 21:00:18 +00001020 TreePatternNodePtr ResultPattern;
1021
Chris Lattner8cab0212008-01-05 22:25:12 +00001022public:
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001023 DAGInstruction(const std::vector<Record*> &results,
Chris Lattner8cab0212008-01-05 22:25:12 +00001024 const std::vector<Record*> &operands,
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001025 const std::vector<Record*> &impresults,
1026 TreePatternNodePtr srcpattern = nullptr,
1027 TreePatternNodePtr resultpattern = nullptr)
1028 : Results(results), Operands(operands), ImpResults(impresults),
1029 SrcPattern(srcpattern), ResultPattern(resultpattern) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001030
Chris Lattner8cab0212008-01-05 22:25:12 +00001031 unsigned getNumResults() const { return Results.size(); }
1032 unsigned getNumOperands() const { return Operands.size(); }
1033 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +00001034 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001035
Chris Lattner8cab0212008-01-05 22:25:12 +00001036 Record *getResult(unsigned RN) const {
1037 assert(RN < Results.size());
1038 return Results[RN];
1039 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001040
Chris Lattner8cab0212008-01-05 22:25:12 +00001041 Record *getOperand(unsigned ON) const {
1042 assert(ON < Operands.size());
1043 return Operands[ON];
1044 }
1045
1046 Record *getImpResult(unsigned RN) const {
1047 assert(RN < ImpResults.size());
1048 return ImpResults[RN];
1049 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001050
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001051 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
Florian Hahn75e87c32018-05-30 21:00:18 +00001052 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001053};
Jim Grosbach50986b52010-12-24 05:06:32 +00001054
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001055/// This class represents a condition that has to be satisfied for a pattern
1056/// to be tried. It is a generalization of a class "Pattern" from Target.td:
1057/// in addition to the Target.td's predicates, this class can also represent
1058/// conditions associated with HW modes. Both types will eventually become
1059/// strings containing C++ code to be executed, the difference is in how
1060/// these strings are generated.
1061class Predicate {
1062public:
1063 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
1064 assert(R->isSubClassOf("Predicate") &&
1065 "Predicate objects should only be created for records derived"
1066 "from Predicate class");
1067 }
1068 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
1069 IfCond(C), IsHwMode(true) {}
1070
1071 /// Return a string which contains the C++ condition code that will serve
1072 /// as a predicate during instruction selection.
1073 std::string getCondString() const {
1074 // The string will excute in a subclass of SelectionDAGISel.
1075 // Cast to std::string explicitly to avoid ambiguity with StringRef.
1076 std::string C = IsHwMode
1077 ? std::string("MF->getSubtarget().checkFeatures(\"" + Features + "\")")
1078 : std::string(Def->getValueAsString("CondString"));
Matt Arsenault57ef94f2019-07-30 15:56:43 +00001079 if (C.empty())
1080 return "";
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001081 return IfCond ? C : "!("+C+')';
1082 }
Matt Arsenault57ef94f2019-07-30 15:56:43 +00001083
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001084 bool operator==(const Predicate &P) const {
1085 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
1086 }
1087 bool operator<(const Predicate &P) const {
1088 if (IsHwMode != P.IsHwMode)
1089 return IsHwMode < P.IsHwMode;
1090 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
1091 if (IfCond != P.IfCond)
1092 return IfCond < P.IfCond;
1093 if (Def)
1094 return LessRecord()(Def, P.Def);
1095 return Features < P.Features;
1096 }
1097 Record *Def; ///< Predicate definition from .td file, null for
1098 ///< HW modes.
1099 std::string Features; ///< Feature string for HW mode.
1100 bool IfCond; ///< The boolean value that the condition has to
1101 ///< evaluate to for this predicate to be true.
1102 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
1103};
1104
Chris Lattnerab3242f2008-01-06 01:10:31 +00001105/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +00001106/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +00001107class PatternToMatch {
1108public:
Craig Topperd78567f2018-06-10 23:15:48 +00001109 PatternToMatch(Record *srcrecord, std::vector<Predicate> preds,
Florian Hahn75e87c32018-05-30 21:00:18 +00001110 TreePatternNodePtr src, TreePatternNodePtr dst,
Craig Topperd78567f2018-06-10 23:15:48 +00001111 std::vector<Record *> dstregs, int complexity,
Florian Hahn75e87c32018-05-30 21:00:18 +00001112 unsigned uid, unsigned setmode = 0)
1113 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
Craig Topper73ed2e62018-07-15 01:10:28 +00001114 Predicates(std::move(preds)), Dstregs(std::move(dstregs)),
Florian Hahn75e87c32018-05-30 21:00:18 +00001115 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001116
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001117 Record *SrcRecord; // Originating Record for the pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +00001118 TreePatternNodePtr SrcPattern; // Source pattern to match.
1119 TreePatternNodePtr DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001120 std::vector<Predicate> Predicates; // Top level predicate conditions
1121 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001122 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001123 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001124 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001125 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001126
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001127 Record *getSrcRecord() const { return SrcRecord; }
Florian Hahn75e87c32018-05-30 21:00:18 +00001128 TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
1129 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
1130 TreePatternNode *getDstPattern() const { return DstPattern.get(); }
1131 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001132 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001133 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001134 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001135
1136 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001137
Chris Lattner05925fe2010-03-29 01:40:38 +00001138 /// Compute the complexity metric for the input pattern. This roughly
1139 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001140 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001141};
1142
Chris Lattnerab3242f2008-01-06 01:10:31 +00001143class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001144 RecordKeeper &Records;
1145 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001146 CodeGenIntrinsicTable Intrinsics;
1147 CodeGenIntrinsicTable TgtIntrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001148
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001149 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001150 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1151 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001152 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001153 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1154 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001155 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1156 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001157
Chris Lattner8cab0212008-01-05 22:25:12 +00001158 // Specific SDNode definitions:
1159 Record *intrinsic_void_sdnode;
1160 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001161
Chris Lattner8cab0212008-01-05 22:25:12 +00001162 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1163 /// value is the pattern to match, the second pattern is the result to
1164 /// emit.
1165 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001166
1167 TypeSetByHwMode LegalVTS;
1168
Daniel Sanders7e523672017-11-11 03:23:44 +00001169 using PatternRewriterFn = std::function<void (TreePattern *)>;
1170 PatternRewriterFn PatternRewriter;
1171
Nicolai Haehnle445b0b62018-11-30 14:15:13 +00001172 unsigned NumScopes = 0;
1173
Chris Lattner8cab0212008-01-05 22:25:12 +00001174public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001175 CodeGenDAGPatterns(RecordKeeper &R,
1176 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001177
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001178 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001179 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001180 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001181
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001182 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001183
Chris Lattner8cab0212008-01-05 22:25:12 +00001184 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001185 auto F = SDNodes.find(R);
1186 assert(F != SDNodes.end() && "Unknown node!");
1187 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001188 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001189
Chris Lattnercc43e792008-01-05 22:54:53 +00001190 // Node transformation lookups.
1191 typedef std::pair<Record*, std::string> NodeXForm;
1192 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001193 auto F = SDNodeXForms.find(R);
1194 assert(F != SDNodeXForms.end() && "Invalid transform!");
1195 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001196 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001197
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001198 typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
Benjamin Kramerc2dbd5d2009-08-23 10:39:21 +00001199 nx_iterator;
Chris Lattnercc43e792008-01-05 22:54:53 +00001200 nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
1201 nx_iterator nx_end() const { return SDNodeXForms.end(); }
1202
Jim Grosbach50986b52010-12-24 05:06:32 +00001203
Chris Lattner8cab0212008-01-05 22:25:12 +00001204 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001205 auto F = ComplexPatterns.find(R);
1206 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1207 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001208 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001209
Chris Lattner8cab0212008-01-05 22:25:12 +00001210 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1211 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1212 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Dale Johannesenb842d522009-02-05 01:49:45 +00001213 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1214 if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001215 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001216 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001217
Chris Lattner8cab0212008-01-05 22:25:12 +00001218 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001219 if (IID-1 < Intrinsics.size())
1220 return Intrinsics[IID-1];
1221 if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
1222 return TgtIntrinsics[IID-Intrinsics.size()-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001223 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001224 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001225
Chris Lattner8cab0212008-01-05 22:25:12 +00001226 unsigned getIntrinsicID(Record *R) const {
1227 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1228 if (Intrinsics[i].TheDef == R) return i;
Dale Johannesenb842d522009-02-05 01:49:45 +00001229 for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
1230 if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
Craig Topperc4965bc2012-02-05 07:21:30 +00001231 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001232 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001233
Chris Lattner7ed81692010-02-18 06:47:49 +00001234 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001235 auto F = DefaultOperands.find(R);
1236 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1237 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001238 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001239
Chris Lattner8cab0212008-01-05 22:25:12 +00001240 // Pattern Fragment information.
1241 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001242 auto F = PatternFragments.find(R);
1243 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1244 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001245 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001246 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001247 auto F = PatternFragments.find(R);
1248 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001249 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001250 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001251 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001252
David Blaikiefcacc742014-11-13 21:56:57 +00001253 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1254 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001255 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1256 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001257 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001258
1259 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001260 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1261 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1262 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001263 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001264
Ahmed Bougacha14107512013-10-28 18:07:21 +00001265 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1266 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001267 void parseInstructionPattern(
Ahmed Bougacha14107512013-10-28 18:07:21 +00001268 CodeGenInstruction &CGI, ListInit *Pattern,
1269 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001270
Chris Lattner8cab0212008-01-05 22:25:12 +00001271 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001272 auto F = Instructions.find(R);
1273 assert(F != Instructions.end() && "Unknown instruction!");
1274 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001275 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001276
Chris Lattner8cab0212008-01-05 22:25:12 +00001277 Record *get_intrinsic_void_sdnode() const {
1278 return intrinsic_void_sdnode;
1279 }
1280 Record *get_intrinsic_w_chain_sdnode() const {
1281 return intrinsic_w_chain_sdnode;
1282 }
1283 Record *get_intrinsic_wo_chain_sdnode() const {
1284 return intrinsic_wo_chain_sdnode;
1285 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001286
Jakob Stoklund Olesene4197252009-10-15 18:50:03 +00001287 bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
1288
Nicolai Haehnle445b0b62018-11-30 14:15:13 +00001289 unsigned allocateScope() { return ++NumScopes; }
1290
Simon Tathamc74322a2019-07-04 08:43:20 +00001291 bool operandHasDefault(Record *Op) const {
1292 return Op->isSubClassOf("OperandWithDefaultOps") &&
1293 !getDefaultOperand(Op).DefaultOps.empty();
1294 }
1295
Chris Lattner8cab0212008-01-05 22:25:12 +00001296private:
1297 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001298 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001299 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001300 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001301 void ParseDefaultOperands();
1302 void ParseInstructions();
1303 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001304 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001305 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001306 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001307 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001308
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001309 std::vector<Predicate> makePredList(ListInit *L);
1310
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001311 void ParseOnePattern(Record *TheDef,
1312 TreePattern &Pattern, TreePattern &Result,
1313 const std::vector<Record *> &InstImpResults);
Craig Topper18e6b572017-06-25 17:33:49 +00001314 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Florian Hahn75e87c32018-05-30 21:00:18 +00001315 void FindPatternInputsAndOutputs(
Florian Hahn6b1db822018-06-14 20:32:58 +00001316 TreePattern &I, TreePatternNodePtr Pat,
Florian Hahn75e87c32018-05-30 21:00:18 +00001317 std::map<std::string, TreePatternNodePtr> &InstInputs,
Craig Topperbd199f82018-12-05 00:47:59 +00001318 MapVector<std::string, TreePatternNodePtr,
1319 std::map<std::string, unsigned>> &InstResults,
Florian Hahn75e87c32018-05-30 21:00:18 +00001320 std::vector<Record *> &InstImpResults);
Chris Lattner8cab0212008-01-05 22:25:12 +00001321};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001322
1323
Florian Hahn6b1db822018-06-14 20:32:58 +00001324inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001325 TreePattern &TP) const {
1326 bool MadeChange = false;
1327 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1328 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1329 return MadeChange;
1330 }
Matt Arsenault303327d2017-12-20 19:36:28 +00001331
Chris Lattner8cab0212008-01-05 22:25:12 +00001332} // end namespace llvm
1333
1334#endif