blob: a3b84d76fde9e24d0c36a02ecfe5905cecdb9699 [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;
Dávid Bolvanský745b6de2019-11-23 23:08:22 +0100197 TypeSetByHwMode &operator=(const TypeSetByHwMode &) = default;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000198 TypeSetByHwMode(MVT::SimpleValueType VT)
199 : TypeSetByHwMode(ValueTypeByHwMode(VT)) {}
200 TypeSetByHwMode(ValueTypeByHwMode VT)
201 : TypeSetByHwMode(ArrayRef<ValueTypeByHwMode>(&VT, 1)) {}
202 TypeSetByHwMode(ArrayRef<ValueTypeByHwMode> VTList);
Jim Grosbach50986b52010-12-24 05:06:32 +0000203
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000204 SetType &getOrCreate(unsigned Mode) {
205 if (hasMode(Mode))
206 return get(Mode);
207 return Map.insert({Mode,SetType()}).first->second;
208 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000209
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000210 bool isValueTypeByHwMode(bool AllowEmpty) const;
211 ValueTypeByHwMode getValueTypeByHwMode() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000212
213 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000214 bool isMachineValueType() const {
215 return isDefaultOnly() && Map.begin()->second.size() == 1;
216 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000217
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000218 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000219 MVT getMachineValueType() const {
220 assert(isMachineValueType());
221 return *Map.begin()->second.begin();
222 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000223
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000224 bool isPossible() const;
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000225
226 LLVM_ATTRIBUTE_ALWAYS_INLINE
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000227 bool isDefaultOnly() const {
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000228 return Map.size() == 1 && Map.begin()->first == DefaultMode;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000229 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000230
Tom Stellard9ad714f2019-02-20 19:43:47 +0000231 bool isPointer() const {
232 return getValueTypeByHwMode().isPointer();
233 }
234
235 unsigned getPtrAddrSpace() const {
236 assert(isPointer());
237 return getValueTypeByHwMode().PtrAddrSpace;
238 }
239
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000240 bool insert(const ValueTypeByHwMode &VVT);
241 bool constrain(const TypeSetByHwMode &VTS);
242 template <typename Predicate> bool constrain(Predicate P);
Zachary Turner249dc142017-09-20 18:01:40 +0000243 template <typename Predicate>
244 bool assign_if(const TypeSetByHwMode &VTS, Predicate P);
Jim Grosbach50986b52010-12-24 05:06:32 +0000245
Zachary Turner249dc142017-09-20 18:01:40 +0000246 void writeToStream(raw_ostream &OS) const;
247 static void writeToStream(const SetType &S, raw_ostream &OS);
Jim Grosbach50986b52010-12-24 05:06:32 +0000248
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000249 bool operator==(const TypeSetByHwMode &VTS) const;
250 bool operator!=(const TypeSetByHwMode &VTS) const { return !(*this == VTS); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000251
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000252 void dump() const;
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000253 bool validate() const;
Craig Topper74169dc2014-01-28 04:49:01 +0000254
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000255private:
Tom Stellard9ad714f2019-02-20 19:43:47 +0000256 unsigned PtrAddrSpace = std::numeric_limits<unsigned>::max();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000257 /// Intersect two sets. Return true if anything has changed.
258 bool intersect(SetType &Out, const SetType &In);
259};
Jim Grosbach50986b52010-12-24 05:06:32 +0000260
Krzysztof Parzyszek7725e492017-09-22 18:29:37 +0000261raw_ostream &operator<<(raw_ostream &OS, const TypeSetByHwMode &T);
262
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000263struct TypeInfer {
264 TypeInfer(TreePattern &T) : TP(T), ForceMode(0) {}
Jim Grosbach50986b52010-12-24 05:06:32 +0000265
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000266 bool isConcrete(const TypeSetByHwMode &VTS, bool AllowEmpty) const {
267 return VTS.isValueTypeByHwMode(AllowEmpty);
268 }
269 ValueTypeByHwMode getConcrete(const TypeSetByHwMode &VTS,
270 bool AllowEmpty) const {
271 assert(VTS.isValueTypeByHwMode(AllowEmpty));
272 return VTS.getValueTypeByHwMode();
273 }
Duncan Sands13237ac2008-06-06 12:08:01 +0000274
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000275 /// The protocol in the following functions (Merge*, force*, Enforce*,
276 /// expand*) is to return "true" if a change has been made, "false"
277 /// otherwise.
Chris Lattner8cab0212008-01-05 22:25:12 +0000278
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000279 bool MergeInTypeInfo(TypeSetByHwMode &Out, const TypeSetByHwMode &In);
280 bool MergeInTypeInfo(TypeSetByHwMode &Out, MVT::SimpleValueType InVT) {
281 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
282 }
283 bool MergeInTypeInfo(TypeSetByHwMode &Out, ValueTypeByHwMode InVT) {
284 return MergeInTypeInfo(Out, TypeSetByHwMode(InVT));
285 }
Bob Wilsonf7e587f2009-08-12 22:30:59 +0000286
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000287 /// Reduce the set \p Out to have at most one element for each mode.
288 bool forceArbitrary(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000289
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000290 /// The following four functions ensure that upon return the set \p Out
291 /// will only contain types of the specified kind: integer, floating-point,
292 /// scalar, or vector.
293 /// If \p Out is empty, all legal types of the specified kind will be added
294 /// to it. Otherwise, all types that are not of the specified kind will be
295 /// removed from \p Out.
296 bool EnforceInteger(TypeSetByHwMode &Out);
297 bool EnforceFloatingPoint(TypeSetByHwMode &Out);
298 bool EnforceScalar(TypeSetByHwMode &Out);
299 bool EnforceVector(TypeSetByHwMode &Out);
Chris Lattnercabe0372010-03-15 06:00:16 +0000300
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000301 /// If \p Out is empty, fill it with all legal types. Otherwise, leave it
302 /// unchanged.
303 bool EnforceAny(TypeSetByHwMode &Out);
304 /// Make sure that for each type in \p Small, there exists a larger type
305 /// in \p Big.
306 bool EnforceSmallerThan(TypeSetByHwMode &Small, TypeSetByHwMode &Big);
307 /// 1. Ensure that for each type T in \p Vec, T is a vector type, and that
308 /// for each type U in \p Elem, U is a scalar type.
309 /// 2. Ensure that for each (scalar) type U in \p Elem, there exists a
310 /// (vector) type T in \p Vec, such that U is the element type of T.
311 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec, TypeSetByHwMode &Elem);
312 bool EnforceVectorEltTypeIs(TypeSetByHwMode &Vec,
313 const ValueTypeByHwMode &VVT);
314 /// Ensure that for each type T in \p Sub, T is a vector type, and there
315 /// exists a type U in \p Vec such that U is a vector type with the same
316 /// element type as T and at least as many elements as T.
317 bool EnforceVectorSubVectorTypeIs(TypeSetByHwMode &Vec,
318 TypeSetByHwMode &Sub);
319 /// 1. Ensure that \p V has a scalar type iff \p W has a scalar type.
320 /// 2. Ensure that for each vector type T in \p V, there exists a vector
321 /// type U in \p W, such that T and U have the same number of elements.
322 /// 3. Ensure that for each vector type U in \p W, there exists a vector
323 /// type T in \p V, such that T and U have the same number of elements
324 /// (reverse of 2).
325 bool EnforceSameNumElts(TypeSetByHwMode &V, TypeSetByHwMode &W);
326 /// 1. Ensure that for each type T in \p A, there exists a type U in \p B,
327 /// such that T and U have equal size in bits.
328 /// 2. Ensure that for each type U in \p B, there exists a type T in \p A
329 /// such that T and U have equal size in bits (reverse of 1).
330 bool EnforceSameSize(TypeSetByHwMode &A, TypeSetByHwMode &B);
Chris Lattnercabe0372010-03-15 06:00:16 +0000331
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000332 /// For each overloaded type (i.e. of form *Any), replace it with the
333 /// corresponding subset of legal, specific types.
334 void expandOverloads(TypeSetByHwMode &VTS);
335 void expandOverloads(TypeSetByHwMode::SetType &Out,
336 const TypeSetByHwMode::SetType &Legal);
Jim Grosbach50986b52010-12-24 05:06:32 +0000337
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000338 struct ValidateOnExit {
Krzysztof Parzyszek2e0f7bd2017-12-21 17:12:43 +0000339 ValidateOnExit(TypeSetByHwMode &T, TypeInfer &TI) : Infer(TI), VTS(T) {}
340 #ifndef NDEBUG
341 ~ValidateOnExit();
342 #else
343 ~ValidateOnExit() {} // Empty destructor with NDEBUG.
344 #endif
345 TypeInfer &Infer;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000346 TypeSetByHwMode &VTS;
Chris Lattnercabe0372010-03-15 06:00:16 +0000347 };
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000348
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000349 struct SuppressValidation {
350 SuppressValidation(TypeInfer &TI) : Infer(TI), SavedValidate(TI.Validate) {
351 Infer.Validate = false;
352 }
353 ~SuppressValidation() {
354 Infer.Validate = SavedValidate;
355 }
356 TypeInfer &Infer;
357 bool SavedValidate;
358 };
359
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000360 TreePattern &TP;
361 unsigned ForceMode; // Mode to use when set.
362 bool CodeGen = false; // Set during generation of matcher code.
Ulrich Weigand22b1af82018-07-13 16:42:15 +0000363 bool Validate = true; // Indicate whether to validate types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000364
365private:
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000366 const TypeSetByHwMode &getLegalTypes();
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000367
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000368 /// Cached legal types (in default mode).
Krzysztof Parzyszekaffd2012017-09-19 18:42:34 +0000369 bool LegalTypesCached = false;
Simon Pilgrim45e61c52018-08-17 15:54:07 +0000370 TypeSetByHwMode LegalCache;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000371};
Chris Lattner8cab0212008-01-05 22:25:12 +0000372
Scott Michel94420742008-03-05 17:49:05 +0000373/// Set type used to track multiply used variables in patterns
Zachary Turner249dc142017-09-20 18:01:40 +0000374typedef StringSet<> MultipleUseVarSet;
Scott Michel94420742008-03-05 17:49:05 +0000375
Chris Lattner8cab0212008-01-05 22:25:12 +0000376/// SDTypeConstraint - This is a discriminated union of constraints,
377/// corresponding to the SDTypeConstraint tablegen class in Target.td.
378struct SDTypeConstraint {
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000379 SDTypeConstraint(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000380
Chris Lattner8cab0212008-01-05 22:25:12 +0000381 unsigned OperandNo; // The operand # this constraint applies to.
Jim Grosbach50986b52010-12-24 05:06:32 +0000382 enum {
383 SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
David Greene127fd1d2011-01-24 20:53:18 +0000384 SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
Craig Topper9a44b3f2015-11-26 07:02:18 +0000385 SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs, SDTCisSameSizeAs
Chris Lattner8cab0212008-01-05 22:25:12 +0000386 } ConstraintType;
Jim Grosbach50986b52010-12-24 05:06:32 +0000387
Chris Lattner8cab0212008-01-05 22:25:12 +0000388 union { // The discriminated union.
389 struct {
Chris Lattner8cab0212008-01-05 22:25:12 +0000390 unsigned OtherOperandNum;
391 } SDTCisSameAs_Info;
392 struct {
393 unsigned OtherOperandNum;
394 } SDTCisVTSmallerThanOp_Info;
395 struct {
396 unsigned BigOperandNum;
397 } SDTCisOpSmallerThanOp_Info;
398 struct {
399 unsigned OtherOperandNum;
Nate Begeman17bedbc2008-02-09 01:37:05 +0000400 } SDTCisEltOfVec_Info;
David Greene127fd1d2011-01-24 20:53:18 +0000401 struct {
402 unsigned OtherOperandNum;
403 } SDTCisSubVecOfVec_Info;
Craig Topper0be34582015-03-05 07:11:34 +0000404 struct {
Craig Topper0be34582015-03-05 07:11:34 +0000405 unsigned OtherOperandNum;
406 } SDTCisSameNumEltsAs_Info;
Craig Topper9a44b3f2015-11-26 07:02:18 +0000407 struct {
408 unsigned OtherOperandNum;
409 } SDTCisSameSizeAs_Info;
Chris Lattner8cab0212008-01-05 22:25:12 +0000410 } x;
411
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000412 // The VT for SDTCisVT and SDTCVecEltisVT.
413 // Must not be in the union because it has a non-trivial destructor.
414 ValueTypeByHwMode VVT;
415
Chris Lattner8cab0212008-01-05 22:25:12 +0000416 /// ApplyTypeConstraint - Given a node in a pattern, apply this type
417 /// constraint to the nodes operands. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000418 /// change, false otherwise. If a type contradiction is found, an error
419 /// is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000420 bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
Chris Lattner8cab0212008-01-05 22:25:12 +0000421 TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000422};
423
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000424/// ScopedName - A name of a node associated with a "scope" that indicates
425/// the context (e.g. instance of Pattern or PatFrag) in which the name was
426/// used. This enables substitution of pattern fragments while keeping track
427/// of what name(s) were originally given to various nodes in the tree.
428class ScopedName {
429 unsigned Scope;
430 std::string Identifier;
431public:
432 ScopedName(unsigned Scope, StringRef Identifier)
Benjamin Krameradcd0262020-01-28 20:23:46 +0100433 : Scope(Scope), Identifier(std::string(Identifier)) {
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000434 assert(Scope != 0 &&
435 "Scope == 0 is used to indicate predicates without arguments");
436 }
437
438 unsigned getScope() const { return Scope; }
439 const std::string &getIdentifier() const { return Identifier; }
440
441 std::string getFullName() const;
442
443 bool operator==(const ScopedName &o) const;
444 bool operator!=(const ScopedName &o) const;
445};
446
Chris Lattner8cab0212008-01-05 22:25:12 +0000447/// SDNodeInfo - One of these records is created for each SDNode instance in
448/// the target .td file. This represents the various dag nodes we will be
449/// processing.
450class SDNodeInfo {
451 Record *Def;
Craig Topperbcd3c372017-05-31 21:12:46 +0000452 StringRef EnumName;
453 StringRef SDClassName;
Chris Lattner8cab0212008-01-05 22:25:12 +0000454 unsigned Properties;
455 unsigned NumResults;
456 int NumOperands;
457 std::vector<SDTypeConstraint> TypeConstraints;
458public:
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000459 // Parse the specified record.
460 SDNodeInfo(Record *R, const CodeGenHwModes &CGH);
Jim Grosbach50986b52010-12-24 05:06:32 +0000461
Chris Lattner8cab0212008-01-05 22:25:12 +0000462 unsigned getNumResults() const { return NumResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000463
Chris Lattner135091b2010-03-28 08:48:47 +0000464 /// getNumOperands - This is the number of operands required or -1 if
465 /// variadic.
Chris Lattner8cab0212008-01-05 22:25:12 +0000466 int getNumOperands() const { return NumOperands; }
467 Record *getRecord() const { return Def; }
Craig Topperbcd3c372017-05-31 21:12:46 +0000468 StringRef getEnumName() const { return EnumName; }
469 StringRef getSDClassName() const { return SDClassName; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000470
Chris Lattner8cab0212008-01-05 22:25:12 +0000471 const std::vector<SDTypeConstraint> &getTypeConstraints() const {
472 return TypeConstraints;
473 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000474
Chris Lattner99e53b32010-02-28 00:22:30 +0000475 /// getKnownType - If the type constraints on this node imply a fixed type
476 /// (e.g. all stores return void, etc), then return it as an
Chris Lattnerda5b4ad2010-03-19 01:14:27 +0000477 /// MVT::SimpleValueType. Otherwise, return MVT::Other.
Chris Lattner6c2d1782010-03-24 00:41:19 +0000478 MVT::SimpleValueType getKnownType(unsigned ResNo) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000479
Chris Lattner8cab0212008-01-05 22:25:12 +0000480 /// hasProperty - Return true if this node has the specified property.
481 ///
482 bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
483
484 /// ApplyTypeConstraints - Given a node in a pattern, apply the type
485 /// constraints for this node to the operands of the node. This returns
486 /// true if it makes a change, false otherwise. If a type contradiction is
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000487 /// found, an error is flagged.
Florian Hahn6b1db822018-06-14 20:32:58 +0000488 bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000489};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000490
Chris Lattner514e2922011-04-17 21:38:24 +0000491/// TreePredicateFn - This is an abstraction that represents the predicates on
492/// a PatFrag node. This is a simple one-word wrapper around a pointer to
493/// provide nice accessors.
494class TreePredicateFn {
495 /// PatFragRec - This is the TreePattern for the PatFrag that we
496 /// originally came from.
497 TreePattern *PatFragRec;
498public:
499 /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
Chris Lattner2ff8c1a2011-04-17 22:05:17 +0000500 TreePredicateFn(TreePattern *N);
Chris Lattner514e2922011-04-17 21:38:24 +0000501
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000502
Chris Lattner514e2922011-04-17 21:38:24 +0000503 TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000504
Chris Lattner514e2922011-04-17 21:38:24 +0000505 /// isAlwaysTrue - Return true if this is a noop predicate.
506 bool isAlwaysTrue() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000507
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000508 bool isImmediatePattern() const { return hasImmCode(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000509
Chris Lattner07add492011-04-18 06:22:33 +0000510 /// getImmediatePredicateCode - Return the code that evaluates this pattern if
511 /// this is an immediate predicate. It is an error to call this on a
512 /// non-immediate pattern.
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000513 std::string getImmediatePredicateCode() const {
514 std::string Result = getImmCode();
Chris Lattner07add492011-04-18 06:22:33 +0000515 assert(!Result.empty() && "Isn't an immediate pattern!");
516 return Result;
517 }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000518
Chris Lattner514e2922011-04-17 21:38:24 +0000519 bool operator==(const TreePredicateFn &RHS) const {
520 return PatFragRec == RHS.PatFragRec;
521 }
522
523 bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
524
525 /// Return the name to use in the generated code to reference this, this is
526 /// "Predicate_foo" if from a pattern fragment "foo".
527 std::string getFnName() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000528
Chris Lattner514e2922011-04-17 21:38:24 +0000529 /// getCodeToRunOnSDNode - Return the code for the function body that
530 /// evaluates this predicate. The argument is expected to be in "Node",
531 /// not N. This handles casting and conversion to a concrete node type as
532 /// appropriate.
533 std::string getCodeToRunOnSDNode() const;
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000534
Daniel Sanders649c5852017-10-13 20:42:18 +0000535 /// Get the data type of the argument to getImmediatePredicateCode().
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000536 StringRef getImmType() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000537
Daniel Sanders11300ce2017-10-13 21:28:03 +0000538 /// Get a string that describes the type returned by getImmType() but is
539 /// usable as part of an identifier.
Simon Pilgrim6ecae9f2017-10-14 21:27:53 +0000540 StringRef getImmTypeIdentifier() const;
Daniel Sanders11300ce2017-10-13 21:28:03 +0000541
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000542 // Predicate code uses the PatFrag's captured operands.
543 bool usesOperands() const;
544
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000545 // Is the desired predefined predicate for a load?
546 bool isLoad() const;
547 // Is the desired predefined predicate for a store?
548 bool isStore() const;
Daniel Sanders87d196c2017-11-13 22:26:13 +0000549 // Is the desired predefined predicate for an atomic?
550 bool isAtomic() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000551
552 /// Is this predicate the predefined unindexed load predicate?
553 /// Is this predicate the predefined unindexed store predicate?
554 bool isUnindexed() const;
555 /// Is this predicate the predefined non-extending load predicate?
556 bool isNonExtLoad() const;
557 /// Is this predicate the predefined any-extend load predicate?
558 bool isAnyExtLoad() const;
559 /// Is this predicate the predefined sign-extend load predicate?
560 bool isSignExtLoad() const;
561 /// Is this predicate the predefined zero-extend load predicate?
562 bool isZeroExtLoad() const;
563 /// Is this predicate the predefined non-truncating store predicate?
564 bool isNonTruncStore() const;
565 /// Is this predicate the predefined truncating store predicate?
566 bool isTruncStore() const;
567
Daniel Sanders6d9d30a2017-11-13 23:03:47 +0000568 /// Is this predicate the predefined monotonic atomic predicate?
569 bool isAtomicOrderingMonotonic() const;
570 /// Is this predicate the predefined acquire atomic predicate?
571 bool isAtomicOrderingAcquire() const;
572 /// Is this predicate the predefined release atomic predicate?
573 bool isAtomicOrderingRelease() const;
574 /// Is this predicate the predefined acquire-release atomic predicate?
575 bool isAtomicOrderingAcquireRelease() const;
576 /// Is this predicate the predefined sequentially consistent atomic predicate?
577 bool isAtomicOrderingSequentiallyConsistent() const;
578
Daniel Sanders0c43b3a2017-11-30 21:05:59 +0000579 /// Is this predicate the predefined acquire-or-stronger atomic predicate?
580 bool isAtomicOrderingAcquireOrStronger() const;
581 /// Is this predicate the predefined weaker-than-acquire atomic predicate?
582 bool isAtomicOrderingWeakerThanAcquire() const;
583
584 /// Is this predicate the predefined release-or-stronger atomic predicate?
585 bool isAtomicOrderingReleaseOrStronger() const;
586 /// Is this predicate the predefined weaker-than-release atomic predicate?
587 bool isAtomicOrderingWeakerThanRelease() const;
588
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000589 /// If non-null, indicates that this predicate is a predefined memory VT
590 /// predicate for a load/store and returns the ValueType record for the memory VT.
591 Record *getMemoryVT() const;
592 /// If non-null, indicates that this predicate is a predefined memory VT
593 /// predicate (checking only the scalar type) for load/store and returns the
594 /// ValueType record for the memory VT.
595 Record *getScalarMemoryVT() const;
596
Matt Arsenaultd00d8572019-07-15 20:59:42 +0000597 ListInit *getAddressSpaces() const;
Matt Arsenault52c26242019-07-31 00:14:43 +0000598 int64_t getMinAlignment() const;
Matt Arsenaultd00d8572019-07-15 20:59:42 +0000599
Daniel Sanders8ead1292018-06-15 23:13:43 +0000600 // If true, indicates that GlobalISel-based C++ code was supplied.
601 bool hasGISelPredicateCode() const;
602 std::string getGISelPredicateCode() const;
603
Chris Lattner514e2922011-04-17 21:38:24 +0000604private:
Daniel Sandersadbf58d2017-10-15 19:01:32 +0000605 bool hasPredCode() const;
606 bool hasImmCode() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000607 std::string getPredCode() const;
608 std::string getImmCode() const;
Daniel Sanders649c5852017-10-13 20:42:18 +0000609 bool immCodeUsesAPInt() const;
610 bool immCodeUsesAPFloat() const;
Daniel Sanders3f267bf2017-10-15 02:06:44 +0000611
612 bool isPredefinedPredicateEqualTo(StringRef Field, bool Value) const;
Chris Lattner514e2922011-04-17 21:38:24 +0000613};
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000614
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000615struct TreePredicateCall {
616 TreePredicateFn Fn;
617
618 // Scope -- unique identifier for retrieving named arguments. 0 is used when
619 // the predicate does not use named arguments.
620 unsigned Scope;
621
622 TreePredicateCall(const TreePredicateFn &Fn, unsigned Scope)
623 : Fn(Fn), Scope(Scope) {}
624
625 bool operator==(const TreePredicateCall &o) const {
626 return Fn == o.Fn && Scope == o.Scope;
627 }
628 bool operator!=(const TreePredicateCall &o) const {
629 return !(*this == o);
630 }
631};
Chris Lattner8cab0212008-01-05 22:25:12 +0000632
Chris Lattner8cab0212008-01-05 22:25:12 +0000633class TreePatternNode {
Chris Lattnerf1447252010-03-19 21:37:09 +0000634 /// The type of each node result. Before and during type inference, each
635 /// result may be a set of possible types. After (successful) type inference,
636 /// each is a single concrete type.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000637 std::vector<TypeSetByHwMode> Types;
Jim Grosbach50986b52010-12-24 05:06:32 +0000638
Craig Topperbd199f82018-12-05 00:47:59 +0000639 /// The index of each result in results of the pattern.
640 std::vector<unsigned> ResultPerm;
641
Chris Lattner8cab0212008-01-05 22:25:12 +0000642 /// Operator - The Record for the operator if this is an interior node (not
643 /// a leaf).
644 Record *Operator;
Jim Grosbach50986b52010-12-24 05:06:32 +0000645
Chris Lattner8cab0212008-01-05 22:25:12 +0000646 /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
647 ///
David Greeneaf8ee2c2011-07-29 22:43:06 +0000648 Init *Val;
Jim Grosbach50986b52010-12-24 05:06:32 +0000649
Chris Lattner8cab0212008-01-05 22:25:12 +0000650 /// Name - The name given to this node with the :$foo notation.
651 ///
652 std::string Name;
Jim Grosbach50986b52010-12-24 05:06:32 +0000653
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000654 std::vector<ScopedName> NamesAsPredicateArg;
655
656 /// PredicateCalls - The predicate functions to execute on this node to check
Dan Gohman6e979022008-10-15 06:17:21 +0000657 /// for a match. If this list is empty, no predicate is involved.
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000658 std::vector<TreePredicateCall> PredicateCalls;
Jim Grosbach50986b52010-12-24 05:06:32 +0000659
Chris Lattner8cab0212008-01-05 22:25:12 +0000660 /// TransformFn - The transformation function to execute on this node before
661 /// it can be substituted into the resulting instruction on a pattern match.
662 Record *TransformFn;
Jim Grosbach50986b52010-12-24 05:06:32 +0000663
Florian Hahn75e87c32018-05-30 21:00:18 +0000664 std::vector<TreePatternNodePtr> Children;
665
Chris Lattner8cab0212008-01-05 22:25:12 +0000666public:
Craig Topper26fc06352018-07-15 06:52:49 +0000667 TreePatternNode(Record *Op, std::vector<TreePatternNodePtr> Ch,
Jim Grosbach50986b52010-12-24 05:06:32 +0000668 unsigned NumResults)
Craig Topper26fc06352018-07-15 06:52:49 +0000669 : Operator(Op), Val(nullptr), TransformFn(nullptr),
670 Children(std::move(Ch)) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000671 Types.resize(NumResults);
Craig Topperbd199f82018-12-05 00:47:59 +0000672 ResultPerm.resize(NumResults);
673 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
Chris Lattnerf1447252010-03-19 21:37:09 +0000674 }
David Greeneaf8ee2c2011-07-29 22:43:06 +0000675 TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
Craig Topperada08572014-04-16 04:21:27 +0000676 : Operator(nullptr), Val(val), TransformFn(nullptr) {
Chris Lattnerf1447252010-03-19 21:37:09 +0000677 Types.resize(NumResults);
Craig Topperbd199f82018-12-05 00:47:59 +0000678 ResultPerm.resize(NumResults);
679 std::iota(ResultPerm.begin(), ResultPerm.end(), 0);
Chris Lattner8cab0212008-01-05 22:25:12 +0000680 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000681
Jakob Stoklund Olesenb5b91102013-03-23 18:08:44 +0000682 bool hasName() const { return !Name.empty(); }
Chris Lattner8cab0212008-01-05 22:25:12 +0000683 const std::string &getName() const { return Name; }
Chris Lattneradf7ecf2010-03-28 06:50:34 +0000684 void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
Jim Grosbach50986b52010-12-24 05:06:32 +0000685
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000686 const std::vector<ScopedName> &getNamesAsPredicateArg() const {
687 return NamesAsPredicateArg;
688 }
689 void setNamesAsPredicateArg(const std::vector<ScopedName>& Names) {
690 NamesAsPredicateArg = Names;
691 }
692 void addNameAsPredicateArg(const ScopedName &N) {
693 NamesAsPredicateArg.push_back(N);
694 }
695
Craig Topperada08572014-04-16 04:21:27 +0000696 bool isLeaf() const { return Val != nullptr; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000697
Chris Lattnercabe0372010-03-15 06:00:16 +0000698 // Type accessors.
Chris Lattnerf1447252010-03-19 21:37:09 +0000699 unsigned getNumTypes() const { return Types.size(); }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000700 ValueTypeByHwMode getType(unsigned ResNo) const {
701 return Types[ResNo].getValueTypeByHwMode();
Chris Lattnerf1447252010-03-19 21:37:09 +0000702 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000703 const std::vector<TypeSetByHwMode> &getExtTypes() const { return Types; }
704 const TypeSetByHwMode &getExtType(unsigned ResNo) const {
705 return Types[ResNo];
706 }
707 TypeSetByHwMode &getExtType(unsigned ResNo) { return Types[ResNo]; }
708 void setType(unsigned ResNo, const TypeSetByHwMode &T) { Types[ResNo] = T; }
709 MVT::SimpleValueType getSimpleType(unsigned ResNo) const {
710 return Types[ResNo].getMachineValueType().SimpleTy;
711 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000712
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000713 bool hasConcreteType(unsigned ResNo) const {
714 return Types[ResNo].isValueTypeByHwMode(false);
Chris Lattnerf1447252010-03-19 21:37:09 +0000715 }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000716 bool isTypeCompletelyUnknown(unsigned ResNo, TreePattern &TP) const {
717 return Types[ResNo].empty();
Chris Lattnerf1447252010-03-19 21:37:09 +0000718 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000719
Craig Topperbd199f82018-12-05 00:47:59 +0000720 unsigned getNumResults() const { return ResultPerm.size(); }
721 unsigned getResultIndex(unsigned ResNo) const { return ResultPerm[ResNo]; }
722 void setResultIndex(unsigned ResNo, unsigned RI) { ResultPerm[ResNo] = RI; }
723
David Greeneaf8ee2c2011-07-29 22:43:06 +0000724 Init *getLeafValue() const { assert(isLeaf()); return Val; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000725 Record *getOperator() const { assert(!isLeaf()); return Operator; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000726
Chris Lattner8cab0212008-01-05 22:25:12 +0000727 unsigned getNumChildren() const { return Children.size(); }
Florian Hahn6b1db822018-06-14 20:32:58 +0000728 TreePatternNode *getChild(unsigned N) const { return Children[N].get(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000729 const TreePatternNodePtr &getChildShared(unsigned N) const {
730 return Children[N];
Chris Lattner8cab0212008-01-05 22:25:12 +0000731 }
Florian Hahn75e87c32018-05-30 21:00:18 +0000732 void setChild(unsigned i, TreePatternNodePtr N) { Children[i] = N; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000733
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000734 /// hasChild - Return true if N is any of our children.
735 bool hasChild(const TreePatternNode *N) const {
736 for (unsigned i = 0, e = Children.size(); i != e; ++i)
Florian Hahn75e87c32018-05-30 21:00:18 +0000737 if (Children[i].get() == N)
738 return true;
Chris Lattneraa7d3e02010-02-16 06:10:58 +0000739 return false;
740 }
Chris Lattner89c65662008-01-06 05:36:50 +0000741
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000742 bool hasProperTypeByHwMode() const;
743 bool hasPossibleType() const;
744 bool setDefaultMode(unsigned Mode);
745
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000746 bool hasAnyPredicate() const { return !PredicateCalls.empty(); }
Simon Pilgrim8c4d0612017-09-22 16:57:28 +0000747
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000748 const std::vector<TreePredicateCall> &getPredicateCalls() const {
749 return PredicateCalls;
Chris Lattner514e2922011-04-17 21:38:24 +0000750 }
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000751 void clearPredicateCalls() { PredicateCalls.clear(); }
752 void setPredicateCalls(const std::vector<TreePredicateCall> &Calls) {
753 assert(PredicateCalls.empty() && "Overwriting non-empty predicate list!");
754 PredicateCalls = Calls;
Dan Gohman6e979022008-10-15 06:17:21 +0000755 }
Nicolai Haehnle445b0b62018-11-30 14:15:13 +0000756 void addPredicateCall(const TreePredicateCall &Call) {
757 assert(!Call.Fn.isAlwaysTrue() && "Empty predicate string!");
758 assert(!is_contained(PredicateCalls, Call) && "predicate applied recursively");
759 PredicateCalls.push_back(Call);
760 }
761 void addPredicateCall(const TreePredicateFn &Fn, unsigned Scope) {
762 assert((Scope != 0) == Fn.usesOperands());
763 addPredicateCall(TreePredicateCall(Fn, Scope));
Dan Gohman6e979022008-10-15 06:17:21 +0000764 }
Chris Lattner8cab0212008-01-05 22:25:12 +0000765
766 Record *getTransformFn() const { return TransformFn; }
767 void setTransformFn(Record *Fn) { TransformFn = Fn; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000768
Chris Lattner89c65662008-01-06 05:36:50 +0000769 /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
770 /// CodeGenIntrinsic information for it, otherwise return a null pointer.
771 const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
Evan Cheng49bad4c2008-06-16 20:29:38 +0000772
Chris Lattner53c39ba2010-02-14 22:22:58 +0000773 /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
774 /// return the ComplexPattern information, otherwise return null.
775 const ComplexPattern *
776 getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
777
Tim Northoverc807a172014-05-20 11:52:46 +0000778 /// Returns the number of MachineInstr operands that would be produced by this
779 /// node if it mapped directly to an output Instruction's
780 /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
781 /// for Operands; otherwise 1.
782 unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
783
Chris Lattner53c39ba2010-02-14 22:22:58 +0000784 /// NodeHasProperty - Return true if this node has the specified property.
Chris Lattner450d5042010-02-14 22:33:49 +0000785 bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000786
Chris Lattner53c39ba2010-02-14 22:22:58 +0000787 /// TreeHasProperty - Return true if any node in this tree has the specified
788 /// property.
Chris Lattner450d5042010-02-14 22:33:49 +0000789 bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000790
Evan Cheng49bad4c2008-06-16 20:29:38 +0000791 /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
792 /// marked isCommutative.
793 bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000794
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000795 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000796 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000797
Chris Lattner8cab0212008-01-05 22:25:12 +0000798public: // Higher level manipulation routines.
799
800 /// clone - Return a new copy of this tree.
801 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000802 TreePatternNodePtr clone() const;
Chris Lattner53c39ba2010-02-14 22:22:58 +0000803
804 /// RemoveAllTypes - Recursively strip all the types of this tree.
805 void RemoveAllTypes();
Jim Grosbach50986b52010-12-24 05:06:32 +0000806
Chris Lattner8cab0212008-01-05 22:25:12 +0000807 /// isIsomorphicTo - Return true if this node is recursively isomorphic to
808 /// the specified node. For this comparison, all of the state of the node
809 /// is considered, except for the assigned name. Nodes with differing names
810 /// that are otherwise identical are considered isomorphic.
Florian Hahn6b1db822018-06-14 20:32:58 +0000811 bool isIsomorphicTo(const TreePatternNode *N,
Scott Michel94420742008-03-05 17:49:05 +0000812 const MultipleUseVarSet &DepVars) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000813
Chris Lattner8cab0212008-01-05 22:25:12 +0000814 /// SubstituteFormalArguments - Replace the formal arguments in this tree
815 /// with actual values specified by ArgMap.
Florian Hahn75e87c32018-05-30 21:00:18 +0000816 void
817 SubstituteFormalArguments(std::map<std::string, TreePatternNodePtr> &ArgMap);
Chris Lattner8cab0212008-01-05 22:25:12 +0000818
819 /// InlinePatternFragments - If this pattern refers to any pattern
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000820 /// fragments, return the set of inlined versions (this can be more than
821 /// one if a PatFrags record has multiple alternatives).
822 void InlinePatternFragments(TreePatternNodePtr T,
823 TreePattern &TP,
824 std::vector<TreePatternNodePtr> &OutAlternatives);
Jim Grosbach50986b52010-12-24 05:06:32 +0000825
Bob Wilson1b97f3f2009-01-05 17:23:09 +0000826 /// ApplyTypeConstraints - Apply all of the type constraints relevant to
Chris Lattner8cab0212008-01-05 22:25:12 +0000827 /// this node and its children in the tree. This returns true if it makes a
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000828 /// change, false otherwise. If a type contradiction is found, flag an error.
Chris Lattner8cab0212008-01-05 22:25:12 +0000829 bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
Jim Grosbach50986b52010-12-24 05:06:32 +0000830
Chris Lattner8cab0212008-01-05 22:25:12 +0000831 /// UpdateNodeType - Set the node type of N to VT if VT contains
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000832 /// information. If N already contains a conflicting type, then flag an
833 /// error. This returns true if any information was updated.
Chris Lattner8cab0212008-01-05 22:25:12 +0000834 ///
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000835 bool UpdateNodeType(unsigned ResNo, const TypeSetByHwMode &InTy,
836 TreePattern &TP);
Chris Lattnerf1447252010-03-19 21:37:09 +0000837 bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000838 TreePattern &TP);
839 bool UpdateNodeType(unsigned ResNo, ValueTypeByHwMode InTy,
840 TreePattern &TP);
Jim Grosbach50986b52010-12-24 05:06:32 +0000841
Jakob Stoklund Olesen57a86502013-03-18 04:08:07 +0000842 // Update node type with types inferred from an instruction operand or result
843 // def from the ins/outs lists.
844 // Return true if the type changed.
845 bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
846
Chris Lattner8cab0212008-01-05 22:25:12 +0000847 /// ContainsUnresolvedType - Return true if this tree contains any
848 /// unresolved types.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000849 bool ContainsUnresolvedType(TreePattern &TP) const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000850
Chris Lattner8cab0212008-01-05 22:25:12 +0000851 /// canPatternMatch - If it is impossible for this pattern to match on this
852 /// target, fill in Reason and return false. Otherwise, return true.
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +0000853 bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
Chris Lattner8cab0212008-01-05 22:25:12 +0000854};
855
Chris Lattnerdd2ec582010-02-14 21:10:33 +0000856inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
857 TPN.print(OS);
858 return OS;
859}
Jim Grosbach50986b52010-12-24 05:06:32 +0000860
Chris Lattner8cab0212008-01-05 22:25:12 +0000861
862/// TreePattern - Represent a pattern, used for instructions, pattern
863/// fragments, etc.
864///
865class TreePattern {
866 /// Trees - The list of pattern trees which corresponds to this pattern.
867 /// Note that PatFrag's only have a single tree.
868 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000869 std::vector<TreePatternNodePtr> Trees;
Jim Grosbach50986b52010-12-24 05:06:32 +0000870
Chris Lattnercabe0372010-03-15 06:00:16 +0000871 /// NamedNodes - This is all of the nodes that have names in the trees in this
872 /// pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +0000873 StringMap<SmallVector<TreePatternNode *, 1>> NamedNodes;
Jim Grosbach50986b52010-12-24 05:06:32 +0000874
Chris Lattner8cab0212008-01-05 22:25:12 +0000875 /// TheRecord - The actual TableGen record corresponding to this pattern.
876 ///
877 Record *TheRecord;
Jim Grosbach50986b52010-12-24 05:06:32 +0000878
Chris Lattner8cab0212008-01-05 22:25:12 +0000879 /// Args - This is a list of all of the arguments to this pattern (for
880 /// PatFrag patterns), which are the 'node' markers in this pattern.
881 std::vector<std::string> Args;
Jim Grosbach50986b52010-12-24 05:06:32 +0000882
Chris Lattner8cab0212008-01-05 22:25:12 +0000883 /// CDP - the top-level object coordinating this madness.
884 ///
Chris Lattnerab3242f2008-01-06 01:10:31 +0000885 CodeGenDAGPatterns &CDP;
Chris Lattner8cab0212008-01-05 22:25:12 +0000886
887 /// isInputPattern - True if this is an input pattern, something to match.
888 /// False if this is an output pattern, something to emit.
889 bool isInputPattern;
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000890
891 /// hasError - True if the currently processed nodes have unresolvable types
892 /// or other non-fatal errors
893 bool HasError;
Tim Northoverc807a172014-05-20 11:52:46 +0000894
895 /// It's important that the usage of operands in ComplexPatterns is
896 /// consistent: each named operand can be defined by at most one
897 /// ComplexPattern. This records the ComplexPattern instance and the operand
898 /// number for each operand encountered in a ComplexPattern to aid in that
899 /// check.
900 StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000901
902 TypeInfer Infer;
903
Chris Lattner8cab0212008-01-05 22:25:12 +0000904public:
Jim Grosbach50986b52010-12-24 05:06:32 +0000905
Chris Lattner8cab0212008-01-05 22:25:12 +0000906 /// TreePattern constructor - Parse the specified DagInits into the
907 /// current record.
David Greeneaf8ee2c2011-07-29 22:43:06 +0000908 TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000909 CodeGenDAGPatterns &ise);
David Greeneaf8ee2c2011-07-29 22:43:06 +0000910 TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
Chris Lattnerab3242f2008-01-06 01:10:31 +0000911 CodeGenDAGPatterns &ise);
Florian Hahn75e87c32018-05-30 21:00:18 +0000912 TreePattern(Record *TheRec, TreePatternNodePtr Pat, bool isInput,
David Blaikiecf195302014-11-17 22:55:41 +0000913 CodeGenDAGPatterns &ise);
Jim Grosbach50986b52010-12-24 05:06:32 +0000914
Chris Lattner8cab0212008-01-05 22:25:12 +0000915 /// getTrees - Return the tree patterns which corresponds to this pattern.
916 ///
Florian Hahn75e87c32018-05-30 21:00:18 +0000917 const std::vector<TreePatternNodePtr> &getTrees() const { return Trees; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000918 unsigned getNumTrees() const { return Trees.size(); }
Florian Hahn75e87c32018-05-30 21:00:18 +0000919 const TreePatternNodePtr &getTree(unsigned i) const { return Trees[i]; }
Florian Hahn53b14db2018-06-10 21:06:24 +0000920 void setTree(unsigned i, TreePatternNodePtr Tree) { Trees[i] = Tree; }
Florian Hahn4dd569c2018-06-13 20:59:53 +0000921 const TreePatternNodePtr &getOnlyTree() const {
Chris Lattner8cab0212008-01-05 22:25:12 +0000922 assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
923 return Trees[0];
924 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000925
Florian Hahn75e87c32018-05-30 21:00:18 +0000926 const StringMap<SmallVector<TreePatternNode *, 1>> &getNamedNodesMap() {
Chris Lattnercabe0372010-03-15 06:00:16 +0000927 if (NamedNodes.empty())
928 ComputeNamedNodes();
929 return NamedNodes;
930 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000931
Chris Lattner8cab0212008-01-05 22:25:12 +0000932 /// getRecord - Return the actual TableGen record corresponding to this
933 /// pattern.
934 ///
935 Record *getRecord() const { return TheRecord; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000936
Chris Lattner8cab0212008-01-05 22:25:12 +0000937 unsigned getNumArgs() const { return Args.size(); }
938 const std::string &getArgName(unsigned i) const {
939 assert(i < Args.size() && "Argument reference out of range!");
940 return Args[i];
941 }
942 std::vector<std::string> &getArgList() { return Args; }
Jim Grosbach50986b52010-12-24 05:06:32 +0000943
Chris Lattnerab3242f2008-01-06 01:10:31 +0000944 CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
Chris Lattner8cab0212008-01-05 22:25:12 +0000945
946 /// InlinePatternFragments - If this pattern refers to any pattern
947 /// fragments, inline them into place, giving us a pattern without any
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000948 /// PatFrags references. This may increase the number of trees in the
949 /// pattern if a PatFrags has multiple alternatives.
Chris Lattner8cab0212008-01-05 22:25:12 +0000950 void InlinePatternFragments() {
Ulrich Weigandc48aefb2018-07-13 13:18:00 +0000951 std::vector<TreePatternNodePtr> Copy = Trees;
952 Trees.clear();
953 for (unsigned i = 0, e = Copy.size(); i != e; ++i)
954 Copy[i]->InlinePatternFragments(Copy[i], *this, Trees);
Chris Lattner8cab0212008-01-05 22:25:12 +0000955 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000956
Chris Lattner8cab0212008-01-05 22:25:12 +0000957 /// InferAllTypes - Infer/propagate as many types throughout the expression
Jim Grosbach975c1cb2009-03-26 16:17:51 +0000958 /// patterns as possible. Return true if all types are inferred, false
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000959 /// otherwise. Bail out if a type contradiction is found.
Florian Hahn75e87c32018-05-30 21:00:18 +0000960 bool InferAllTypes(
961 const StringMap<SmallVector<TreePatternNode *, 1>> *NamedTypes = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +0000962
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000963 /// error - If this is the first error in the current resolution step,
964 /// print it and set the error flag. Otherwise, continue silently.
Matt Arsenaultea8df3a2014-11-11 23:48:11 +0000965 void error(const Twine &Msg);
Joerg Sonnenberger635debe2012-10-25 20:33:17 +0000966 bool hasError() const {
967 return HasError;
968 }
969 void resetError() {
970 HasError = false;
971 }
Jim Grosbach50986b52010-12-24 05:06:32 +0000972
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000973 TypeInfer &getInfer() { return Infer; }
974
Daniel Dunbar38a22bf2009-07-03 00:10:29 +0000975 void print(raw_ostream &OS) const;
Chris Lattner8cab0212008-01-05 22:25:12 +0000976 void dump() const;
Jim Grosbach50986b52010-12-24 05:06:32 +0000977
Chris Lattner8cab0212008-01-05 22:25:12 +0000978private:
Florian Hahn75e87c32018-05-30 21:00:18 +0000979 TreePatternNodePtr ParseTreePattern(Init *DI, StringRef OpName);
Chris Lattnercabe0372010-03-15 06:00:16 +0000980 void ComputeNamedNodes();
Florian Hahn6b1db822018-06-14 20:32:58 +0000981 void ComputeNamedNodes(TreePatternNode *N);
Chris Lattner8cab0212008-01-05 22:25:12 +0000982};
983
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +0000984
985inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
986 const TypeSetByHwMode &InTy,
987 TreePattern &TP) {
988 TypeSetByHwMode VTS(InTy);
989 TP.getInfer().expandOverloads(VTS);
990 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
991}
992
993inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
994 MVT::SimpleValueType InTy,
995 TreePattern &TP) {
996 TypeSetByHwMode VTS(InTy);
997 TP.getInfer().expandOverloads(VTS);
998 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
999}
1000
1001inline bool TreePatternNode::UpdateNodeType(unsigned ResNo,
1002 ValueTypeByHwMode InTy,
1003 TreePattern &TP) {
1004 TypeSetByHwMode VTS(InTy);
1005 TP.getInfer().expandOverloads(VTS);
1006 return TP.getInfer().MergeInTypeInfo(Types[ResNo], VTS);
1007}
1008
1009
Tom Stellardb7246a72012-09-06 14:15:52 +00001010/// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
1011/// that has a set ExecuteAlways / DefaultOps field.
Chris Lattner8cab0212008-01-05 22:25:12 +00001012struct DAGDefaultOperand {
Florian Hahn75e87c32018-05-30 21:00:18 +00001013 std::vector<TreePatternNodePtr> DefaultOps;
Chris Lattner8cab0212008-01-05 22:25:12 +00001014};
1015
1016class DAGInstruction {
Chris Lattner8cab0212008-01-05 22:25:12 +00001017 std::vector<Record*> Results;
1018 std::vector<Record*> Operands;
1019 std::vector<Record*> ImpResults;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001020 TreePatternNodePtr SrcPattern;
Florian Hahn75e87c32018-05-30 21:00:18 +00001021 TreePatternNodePtr ResultPattern;
1022
Chris Lattner8cab0212008-01-05 22:25:12 +00001023public:
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001024 DAGInstruction(const std::vector<Record*> &results,
Chris Lattner8cab0212008-01-05 22:25:12 +00001025 const std::vector<Record*> &operands,
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001026 const std::vector<Record*> &impresults,
1027 TreePatternNodePtr srcpattern = nullptr,
1028 TreePatternNodePtr resultpattern = nullptr)
1029 : Results(results), Operands(operands), ImpResults(impresults),
1030 SrcPattern(srcpattern), ResultPattern(resultpattern) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001031
Chris Lattner8cab0212008-01-05 22:25:12 +00001032 unsigned getNumResults() const { return Results.size(); }
1033 unsigned getNumOperands() const { return Operands.size(); }
1034 unsigned getNumImpResults() const { return ImpResults.size(); }
Chris Lattner8cab0212008-01-05 22:25:12 +00001035 const std::vector<Record*>& getImpResults() const { return ImpResults; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001036
Chris Lattner8cab0212008-01-05 22:25:12 +00001037 Record *getResult(unsigned RN) const {
1038 assert(RN < Results.size());
1039 return Results[RN];
1040 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001041
Chris Lattner8cab0212008-01-05 22:25:12 +00001042 Record *getOperand(unsigned ON) const {
1043 assert(ON < Operands.size());
1044 return Operands[ON];
1045 }
1046
1047 Record *getImpResult(unsigned RN) const {
1048 assert(RN < ImpResults.size());
1049 return ImpResults[RN];
1050 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001051
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001052 TreePatternNodePtr getSrcPattern() const { return SrcPattern; }
Florian Hahn75e87c32018-05-30 21:00:18 +00001053 TreePatternNodePtr getResultPattern() const { return ResultPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001054};
Jim Grosbach50986b52010-12-24 05:06:32 +00001055
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001056/// This class represents a condition that has to be satisfied for a pattern
1057/// to be tried. It is a generalization of a class "Pattern" from Target.td:
1058/// in addition to the Target.td's predicates, this class can also represent
1059/// conditions associated with HW modes. Both types will eventually become
1060/// strings containing C++ code to be executed, the difference is in how
1061/// these strings are generated.
1062class Predicate {
1063public:
1064 Predicate(Record *R, bool C = true) : Def(R), IfCond(C), IsHwMode(false) {
1065 assert(R->isSubClassOf("Predicate") &&
1066 "Predicate objects should only be created for records derived"
1067 "from Predicate class");
1068 }
1069 Predicate(StringRef FS, bool C = true) : Def(nullptr), Features(FS.str()),
1070 IfCond(C), IsHwMode(true) {}
1071
1072 /// Return a string which contains the C++ condition code that will serve
1073 /// as a predicate during instruction selection.
1074 std::string getCondString() const {
1075 // The string will excute in a subclass of SelectionDAGISel.
1076 // Cast to std::string explicitly to avoid ambiguity with StringRef.
1077 std::string C = IsHwMode
Benjamin Krameradcd0262020-01-28 20:23:46 +01001078 ? std::string("MF->getSubtarget().checkFeatures(\"" +
1079 Features + "\")")
1080 : std::string(Def->getValueAsString("CondString"));
Matt Arsenault57ef94f2019-07-30 15:56:43 +00001081 if (C.empty())
1082 return "";
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001083 return IfCond ? C : "!("+C+')';
1084 }
Matt Arsenault57ef94f2019-07-30 15:56:43 +00001085
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001086 bool operator==(const Predicate &P) const {
1087 return IfCond == P.IfCond && IsHwMode == P.IsHwMode && Def == P.Def;
1088 }
1089 bool operator<(const Predicate &P) const {
1090 if (IsHwMode != P.IsHwMode)
1091 return IsHwMode < P.IsHwMode;
1092 assert(!Def == !P.Def && "Inconsistency between Def and IsHwMode");
1093 if (IfCond != P.IfCond)
1094 return IfCond < P.IfCond;
1095 if (Def)
1096 return LessRecord()(Def, P.Def);
1097 return Features < P.Features;
1098 }
1099 Record *Def; ///< Predicate definition from .td file, null for
1100 ///< HW modes.
1101 std::string Features; ///< Feature string for HW mode.
1102 bool IfCond; ///< The boolean value that the condition has to
1103 ///< evaluate to for this predicate to be true.
1104 bool IsHwMode; ///< Does this predicate correspond to a HW mode?
1105};
1106
Chris Lattnerab3242f2008-01-06 01:10:31 +00001107/// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
Chris Lattner8cab0212008-01-05 22:25:12 +00001108/// processed to produce isel.
Chris Lattner7ed81692010-02-18 06:47:49 +00001109class PatternToMatch {
1110public:
Craig Topperd78567f2018-06-10 23:15:48 +00001111 PatternToMatch(Record *srcrecord, std::vector<Predicate> preds,
Florian Hahn75e87c32018-05-30 21:00:18 +00001112 TreePatternNodePtr src, TreePatternNodePtr dst,
Craig Topperd78567f2018-06-10 23:15:48 +00001113 std::vector<Record *> dstregs, int complexity,
Florian Hahn75e87c32018-05-30 21:00:18 +00001114 unsigned uid, unsigned setmode = 0)
1115 : SrcRecord(srcrecord), SrcPattern(src), DstPattern(dst),
Craig Topper73ed2e62018-07-15 01:10:28 +00001116 Predicates(std::move(preds)), Dstregs(std::move(dstregs)),
Florian Hahn75e87c32018-05-30 21:00:18 +00001117 AddedComplexity(complexity), ID(uid), ForceMode(setmode) {}
Chris Lattner8cab0212008-01-05 22:25:12 +00001118
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001119 Record *SrcRecord; // Originating Record for the pattern.
Florian Hahn75e87c32018-05-30 21:00:18 +00001120 TreePatternNodePtr SrcPattern; // Source pattern to match.
1121 TreePatternNodePtr DstPattern; // Resulting pattern.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001122 std::vector<Predicate> Predicates; // Top level predicate conditions
1123 // to match.
Chris Lattner8cab0212008-01-05 22:25:12 +00001124 std::vector<Record*> Dstregs; // Physical register defs being matched.
Tom Stellard6655dd62014-08-01 00:32:36 +00001125 int AddedComplexity; // Add to matching pattern complexity.
Chris Lattnerd39f75b2010-03-01 22:09:11 +00001126 unsigned ID; // Unique ID for the record.
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001127 unsigned ForceMode; // Force this mode in type inference when set.
Chris Lattner8cab0212008-01-05 22:25:12 +00001128
Jim Grosbachfb116ae2010-12-07 23:05:49 +00001129 Record *getSrcRecord() const { return SrcRecord; }
Florian Hahn75e87c32018-05-30 21:00:18 +00001130 TreePatternNode *getSrcPattern() const { return SrcPattern.get(); }
1131 TreePatternNodePtr getSrcPatternShared() const { return SrcPattern; }
1132 TreePatternNode *getDstPattern() const { return DstPattern.get(); }
1133 TreePatternNodePtr getDstPatternShared() const { return DstPattern; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001134 const std::vector<Record*> &getDstRegs() const { return Dstregs; }
Tom Stellard6655dd62014-08-01 00:32:36 +00001135 int getAddedComplexity() const { return AddedComplexity; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001136 const std::vector<Predicate> &getPredicates() const { return Predicates; }
Dan Gohman49e19e92008-08-22 00:20:26 +00001137
1138 std::string getPredicateCheck() const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001139
Chris Lattner05925fe2010-03-29 01:40:38 +00001140 /// Compute the complexity metric for the input pattern. This roughly
1141 /// corresponds to the number of nodes that are covered.
Tom Stellard6655dd62014-08-01 00:32:36 +00001142 int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
Chris Lattner8cab0212008-01-05 22:25:12 +00001143};
1144
Chris Lattnerab3242f2008-01-06 01:10:31 +00001145class CodeGenDAGPatterns {
Chris Lattner8cab0212008-01-05 22:25:12 +00001146 RecordKeeper &Records;
1147 CodeGenTarget Target;
Justin Bogner92a8c612016-07-15 16:31:37 +00001148 CodeGenIntrinsicTable Intrinsics;
Jim Grosbach50986b52010-12-24 05:06:32 +00001149
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001150 std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
Krzysztof Parzyszek426bf362017-09-12 15:31:26 +00001151 std::map<Record*, std::pair<Record*, std::string>, LessRecordByID>
1152 SDNodeXForms;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001153 std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
David Blaikie3c6ca232014-11-13 21:40:02 +00001154 std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
1155 PatternFragments;
Sean Silvaa4e2c5f2012-09-19 01:47:00 +00001156 std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
1157 std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
Jim Grosbach50986b52010-12-24 05:06:32 +00001158
Chris Lattner8cab0212008-01-05 22:25:12 +00001159 // Specific SDNode definitions:
1160 Record *intrinsic_void_sdnode;
1161 Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
Jim Grosbach50986b52010-12-24 05:06:32 +00001162
Chris Lattner8cab0212008-01-05 22:25:12 +00001163 /// PatternsToMatch - All of the things we are matching on the DAG. The first
1164 /// value is the pattern to match, the second pattern is the result to
1165 /// emit.
1166 std::vector<PatternToMatch> PatternsToMatch;
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001167
1168 TypeSetByHwMode LegalVTS;
1169
Daniel Sanders7e523672017-11-11 03:23:44 +00001170 using PatternRewriterFn = std::function<void (TreePattern *)>;
1171 PatternRewriterFn PatternRewriter;
1172
Nicolai Haehnle445b0b62018-11-30 14:15:13 +00001173 unsigned NumScopes = 0;
1174
Chris Lattner8cab0212008-01-05 22:25:12 +00001175public:
Daniel Sanders7e523672017-11-11 03:23:44 +00001176 CodeGenDAGPatterns(RecordKeeper &R,
1177 PatternRewriterFn PatternRewriter = nullptr);
Jim Grosbach50986b52010-12-24 05:06:32 +00001178
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001179 CodeGenTarget &getTargetInfo() { return Target; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001180 const CodeGenTarget &getTargetInfo() const { return Target; }
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001181 const TypeSetByHwMode &getLegalTypes() const { return LegalVTS; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001182
Daniel Sanders9e0ae7b2017-10-13 19:00:01 +00001183 Record *getSDNodeNamed(const std::string &Name) const;
Jim Grosbach50986b52010-12-24 05:06:32 +00001184
Chris Lattner8cab0212008-01-05 22:25:12 +00001185 const SDNodeInfo &getSDNodeInfo(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001186 auto F = SDNodes.find(R);
1187 assert(F != SDNodes.end() && "Unknown node!");
1188 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001189 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001190
Chris Lattnercc43e792008-01-05 22:54:53 +00001191 // Node transformation lookups.
1192 typedef std::pair<Record*, std::string> NodeXForm;
1193 const NodeXForm &getSDNodeTransform(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001194 auto F = SDNodeXForms.find(R);
1195 assert(F != SDNodeXForms.end() && "Invalid transform!");
1196 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001197 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001198
Chris Lattner8cab0212008-01-05 22:25:12 +00001199 const ComplexPattern &getComplexPattern(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001200 auto F = ComplexPatterns.find(R);
1201 assert(F != ComplexPatterns.end() && "Unknown addressing mode!");
1202 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001203 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001204
Chris Lattner8cab0212008-01-05 22:25:12 +00001205 const CodeGenIntrinsic &getIntrinsic(Record *R) const {
1206 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1207 if (Intrinsics[i].TheDef == R) return Intrinsics[i];
Craig Topperc4965bc2012-02-05 07:21:30 +00001208 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001209 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001210
Chris Lattner8cab0212008-01-05 22:25:12 +00001211 const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
Dale Johannesenb842d522009-02-05 01:49:45 +00001212 if (IID-1 < Intrinsics.size())
1213 return Intrinsics[IID-1];
Craig Topperc4965bc2012-02-05 07:21:30 +00001214 llvm_unreachable("Bad intrinsic ID!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001215 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001216
Chris Lattner8cab0212008-01-05 22:25:12 +00001217 unsigned getIntrinsicID(Record *R) const {
1218 for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
1219 if (Intrinsics[i].TheDef == R) return i;
Craig Topperc4965bc2012-02-05 07:21:30 +00001220 llvm_unreachable("Unknown intrinsic!");
Chris Lattner8cab0212008-01-05 22:25:12 +00001221 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001222
Chris Lattner7ed81692010-02-18 06:47:49 +00001223 const DAGDefaultOperand &getDefaultOperand(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001224 auto F = DefaultOperands.find(R);
1225 assert(F != DefaultOperands.end() &&"Isn't an analyzed default operand!");
1226 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001227 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001228
Chris Lattner8cab0212008-01-05 22:25:12 +00001229 // Pattern Fragment information.
1230 TreePattern *getPatternFragment(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001231 auto F = PatternFragments.find(R);
1232 assert(F != PatternFragments.end() && "Invalid pattern fragment request!");
1233 return F->second.get();
Chris Lattner8cab0212008-01-05 22:25:12 +00001234 }
Chris Lattnerf1447252010-03-19 21:37:09 +00001235 TreePattern *getPatternFragmentIfRead(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001236 auto F = PatternFragments.find(R);
1237 if (F == PatternFragments.end())
David Blaikie3c6ca232014-11-13 21:40:02 +00001238 return nullptr;
Simon Pilgrimb021b132017-10-07 14:34:24 +00001239 return F->second.get();
Chris Lattnerf1447252010-03-19 21:37:09 +00001240 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001241
David Blaikiefcacc742014-11-13 21:56:57 +00001242 typedef std::map<Record *, std::unique_ptr<TreePattern>,
1243 LessRecordByID>::const_iterator pf_iterator;
Chris Lattner8cab0212008-01-05 22:25:12 +00001244 pf_iterator pf_begin() const { return PatternFragments.begin(); }
1245 pf_iterator pf_end() const { return PatternFragments.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001246 iterator_range<pf_iterator> ptfs() const { return PatternFragments; }
Chris Lattner8cab0212008-01-05 22:25:12 +00001247
1248 // Patterns to match information.
Chris Lattner9abe77b2008-01-05 22:30:17 +00001249 typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
1250 ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
1251 ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
Ahmed Bougacha36f70352016-12-21 23:26:20 +00001252 iterator_range<ptm_iterator> ptms() const { return PatternsToMatch; }
Jim Grosbach50986b52010-12-24 05:06:32 +00001253
Ahmed Bougacha14107512013-10-28 18:07:21 +00001254 /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
1255 typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001256 void parseInstructionPattern(
Ahmed Bougacha14107512013-10-28 18:07:21 +00001257 CodeGenInstruction &CGI, ListInit *Pattern,
1258 DAGInstMap &DAGInsts);
Jim Grosbach50986b52010-12-24 05:06:32 +00001259
Chris Lattner8cab0212008-01-05 22:25:12 +00001260 const DAGInstruction &getInstruction(Record *R) const {
Simon Pilgrimb021b132017-10-07 14:34:24 +00001261 auto F = Instructions.find(R);
1262 assert(F != Instructions.end() && "Unknown instruction!");
1263 return F->second;
Chris Lattner8cab0212008-01-05 22:25:12 +00001264 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001265
Chris Lattner8cab0212008-01-05 22:25:12 +00001266 Record *get_intrinsic_void_sdnode() const {
1267 return intrinsic_void_sdnode;
1268 }
1269 Record *get_intrinsic_w_chain_sdnode() const {
1270 return intrinsic_w_chain_sdnode;
1271 }
1272 Record *get_intrinsic_wo_chain_sdnode() const {
1273 return intrinsic_wo_chain_sdnode;
1274 }
Jim Grosbach50986b52010-12-24 05:06:32 +00001275
Nicolai Haehnle445b0b62018-11-30 14:15:13 +00001276 unsigned allocateScope() { return ++NumScopes; }
1277
Simon Tathamc74322a2019-07-04 08:43:20 +00001278 bool operandHasDefault(Record *Op) const {
1279 return Op->isSubClassOf("OperandWithDefaultOps") &&
1280 !getDefaultOperand(Op).DefaultOps.empty();
1281 }
1282
Chris Lattner8cab0212008-01-05 22:25:12 +00001283private:
1284 void ParseNodeInfo();
Chris Lattnercc43e792008-01-05 22:54:53 +00001285 void ParseNodeTransforms();
Chris Lattner8cab0212008-01-05 22:25:12 +00001286 void ParseComplexPatterns();
Hal Finkel2756dc12014-02-28 00:26:56 +00001287 void ParsePatternFragments(bool OutFrags = false);
Chris Lattner8cab0212008-01-05 22:25:12 +00001288 void ParseDefaultOperands();
1289 void ParseInstructions();
1290 void ParsePatterns();
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001291 void ExpandHwModeBasedTypes();
Dan Gohmanfc4ad7de2008-04-03 00:02:49 +00001292 void InferInstructionFlags();
Chris Lattner8cab0212008-01-05 22:25:12 +00001293 void GenerateVariants();
Jakob Stoklund Olesena9d322a2012-08-28 03:26:49 +00001294 void VerifyInstructionFlags();
Jim Grosbach50986b52010-12-24 05:06:32 +00001295
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001296 std::vector<Predicate> makePredList(ListInit *L);
1297
Ulrich Weigandc48aefb2018-07-13 13:18:00 +00001298 void ParseOnePattern(Record *TheDef,
1299 TreePattern &Pattern, TreePattern &Result,
1300 const std::vector<Record *> &InstImpResults);
Craig Topper18e6b572017-06-25 17:33:49 +00001301 void AddPatternToMatch(TreePattern *Pattern, PatternToMatch &&PTM);
Florian Hahn75e87c32018-05-30 21:00:18 +00001302 void FindPatternInputsAndOutputs(
Florian Hahn6b1db822018-06-14 20:32:58 +00001303 TreePattern &I, TreePatternNodePtr Pat,
Florian Hahn75e87c32018-05-30 21:00:18 +00001304 std::map<std::string, TreePatternNodePtr> &InstInputs,
Craig Topperbd199f82018-12-05 00:47:59 +00001305 MapVector<std::string, TreePatternNodePtr,
1306 std::map<std::string, unsigned>> &InstResults,
Florian Hahn75e87c32018-05-30 21:00:18 +00001307 std::vector<Record *> &InstImpResults);
Chris Lattner8cab0212008-01-05 22:25:12 +00001308};
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001309
1310
Florian Hahn6b1db822018-06-14 20:32:58 +00001311inline bool SDNodeInfo::ApplyTypeConstraints(TreePatternNode *N,
Krzysztof Parzyszek779d98e2017-09-14 16:56:21 +00001312 TreePattern &TP) const {
1313 bool MadeChange = false;
1314 for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
1315 MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
1316 return MadeChange;
1317 }
Matt Arsenault303327d2017-12-20 19:36:28 +00001318
Chris Lattner8cab0212008-01-05 22:25:12 +00001319} // end namespace llvm
1320
1321#endif