LLVM 24.0.0git
ScalarEvolutionPatternMatch.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7//
8// This file provides a simple and efficient mechanism for performing general
9// tree-based pattern matches on SCEVs, based on LLVM's IR pattern matchers.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONPATTERNMATCH_H
14#define LLVM_ANALYSIS_SCALAREVOLUTIONPATTERNMATCH_H
15
19
20namespace llvm {
22template <typename SCEVPtrT> struct match_bind<SCEVUseT<SCEVPtrT>> {
24
26
27 template <typename ITy> bool match(ITy *V) const {
28 VR = V;
29 return true;
30 }
31};
32} // namespace PatternMatchHelpers
33
35
36using namespace llvm::PatternMatchHelpers;
37
38template <typename Pattern> bool match(const SCEV *S, const Pattern &P) {
39 return P.match(S);
40}
41
42template <typename SCEVPtrT, typename Pattern>
43bool match(const SCEVUseT<SCEVPtrT> U, const Pattern &P) {
44 return P.match(U.getPointer());
45}
46
47template <typename Predicate> struct cst_pred_ty : public Predicate {
48 cst_pred_ty() = default;
49 cst_pred_ty(uint64_t V) : Predicate(V) {}
50 bool match(const SCEV *S) const {
52 "no vector types expected from SCEVs");
53 auto *C = dyn_cast<SCEVConstant>(S);
54 return C && this->isValue(C->getAPInt());
55 }
56};
57
58struct is_zero {
59 bool isValue(const APInt &C) const { return C.isZero(); }
60};
61
62/// Match an integer 0.
64
65struct is_one {
66 bool isValue(const APInt &C) const { return C.isOne(); }
67};
68
69/// Match an integer 1.
71
73 bool isValue(const APInt &C) const { return C.isAllOnes(); }
74};
75
76/// Match an integer with all bits set.
80
81inline auto m_SCEV() { return m_Isa<const SCEV>(); }
82inline auto m_SCEVConstant() { return m_Isa<const SCEVConstant>(); }
83inline auto m_SCEVVScale() { return m_Isa<const SCEVVScale>(); }
84
85/// Match a SCEV, capturing it if we match.
86inline match_bind<const SCEV> m_SCEV(const SCEV *&V) { return V; }
87
88template <typename SCEVPtrT>
93 return V;
94}
96 return V;
97}
98
100 return V;
101}
102
104 return V;
105}
106
107/// Match a specified const SCEV *.
109 const SCEV *Expr;
110
112
113 template <typename ITy> bool match(ITy *S) const { return S == Expr; }
114};
115
116/// Match if we have a specific specified SCEV.
117inline specificscev_ty m_scev_Specific(const SCEV *S) { return S; }
118
122 bool isValue(const APInt &C) const { return C == CV; }
123};
124
125/// Match an SCEV constant with a plain unsigned integer.
127
129 int64_t CV;
131 bool isValue(const APInt &C) const { return C.trySExtValue() == CV; }
132};
133
134/// Match an SCEV constant with a plain signed integer (sign-extended value will
135/// be matched)
137 return V;
138}
139
141 const APInt *&CR;
142
143 bind_cst_ty(const APInt *&Op0) : CR(Op0) {}
144
145 bool match(const SCEV *S) const {
147 "no vector types expected from SCEVs");
148 auto *C = dyn_cast<SCEVConstant>(S);
149 if (!C)
150 return false;
151 CR = &C->getAPInt();
152 return true;
153 }
154};
155
156/// Match an SCEV constant and bind it to an APInt.
157inline bind_cst_ty m_scev_APInt(const APInt *&C) { return C; }
158
159/// Match a unary SCEV.
160template <typename SCEVTy, typename Op0_t> struct SCEVUnaryExpr_match {
161 Op0_t Op0;
162
164
165 bool match(const SCEV *S) const {
166 auto *E = dyn_cast<SCEVTy>(S);
167 return E && E->getNumOperands() == 1 &&
168 Op0.match(E->getOperand(0).getPointer());
169 }
170};
171
172template <typename SCEVTy, typename Op0_t>
176
177template <typename Op0_t>
178inline SCEVUnaryExpr_match<SCEVSignExtendExpr, Op0_t>
179m_scev_SExt(const Op0_t &Op0) {
181}
182
183template <typename Op0_t>
184inline SCEVUnaryExpr_match<SCEVZeroExtendExpr, Op0_t>
185m_scev_ZExt(const Op0_t &Op0) {
187}
188
189template <typename Op0_t>
190inline SCEVUnaryExpr_match<SCEVPtrToAddrExpr, Op0_t>
191m_scev_PtrToAddr(const Op0_t &Op0) {
193}
194
195template <typename Op0_t>
196inline SCEVUnaryExpr_match<SCEVTruncateExpr, Op0_t>
197m_scev_Trunc(const Op0_t &Op0) {
199}
200
201/// Match a binary SCEV.
202template <typename SCEVTy, typename Op0_t, typename Op1_t,
204 bool Commutable = false>
206 Op0_t Op0;
208
210
211 bool match(const SCEV *S) const {
212 if (auto WrappingS = dyn_cast<SCEVNAryExpr>(S))
213 if (WrappingS->getNoWrapFlags(WrapFlags) != WrapFlags)
214 return false;
215
216 auto *E = dyn_cast<SCEVTy>(S);
217 return E && E->getNumOperands() == 2 &&
218 ((Op0.match(E->getOperand(0).getPointer()) &&
219 Op1.match(E->getOperand(1).getPointer())) ||
220 (Commutable && Op0.match(E->getOperand(1).getPointer()) &&
221 Op1.match(E->getOperand(0).getPointer())));
222 }
223};
224
225template <typename SCEVTy, typename Op0_t, typename Op1_t,
227 bool Commutable = false>
228inline SCEVBinaryExpr_match<SCEVTy, Op0_t, Op1_t, WrapFlags, Commutable>
229m_scev_Binary(const Op0_t &Op0, const Op1_t &Op1) {
231 Op1);
232}
233
234template <typename Op0_t, typename Op1_t>
235inline SCEVBinaryExpr_match<SCEVAddExpr, Op0_t, Op1_t>
236m_scev_Add(const Op0_t &Op0, const Op1_t &Op1) {
237 return m_scev_Binary<SCEVAddExpr>(Op0, Op1);
238}
239
240template <typename Op0_t, typename Op1_t>
241inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t>
242m_scev_Mul(const Op0_t &Op0, const Op1_t &Op1) {
243 return m_scev_Binary<SCEVMulExpr>(Op0, Op1);
244}
245
246template <typename Op0_t, typename Op1_t>
247inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true>
248m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1) {
250 Op1);
251}
252
253template <typename Op0_t, typename Op1_t>
254inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNUW, true>
255m_scev_c_NUWMul(const Op0_t &Op0, const Op1_t &Op1) {
257 Op1);
258}
259
260template <typename Op0_t, typename Op1_t>
261inline SCEVBinaryExpr_match<SCEVUDivExpr, Op0_t, Op1_t>
262m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
263 return m_scev_Binary<SCEVUDivExpr>(Op0, Op1);
264}
265
266template <typename Op0_t, typename Op1_t>
267inline SCEVBinaryExpr_match<SCEVSMaxExpr, Op0_t, Op1_t>
268m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1) {
269 return m_scev_Binary<SCEVSMaxExpr>(Op0, Op1);
270}
271
272template <typename Op0_t, typename Op1_t>
273inline SCEVBinaryExpr_match<SCEVMinMaxExpr, Op0_t, Op1_t>
274m_scev_MinMax(const Op0_t &Op0, const Op1_t &Op1) {
275 return m_scev_Binary<SCEVMinMaxExpr>(Op0, Op1);
276}
277
278/// Match unsigned remainder pattern.
279/// Matches patterns generated by getURemExpr.
280template <typename Op0_t, typename Op1_t> struct SCEVURem_match {
281 Op0_t Op0;
284
287
288 bool match(const SCEV *Expr) const {
289 if (Expr->getType()->isPointerTy())
290 return false;
291
292 // Try to match 'zext (trunc A to iB) to iY', which is used
293 // for URem with constant power-of-2 second operands. Make sure the size of
294 // the operand A matches the size of the whole expressions.
295 const SCEV *LHS;
297 Type *TruncTy = cast<SCEVZeroExtendExpr>(Expr)->getOperand()->getType();
298 // Bail out if the type of the LHS is larger than the type of the
299 // expression for now.
300 if (SE.getTypeSizeInBits(LHS->getType()) >
301 SE.getTypeSizeInBits(Expr->getType()))
302 return false;
303 if (LHS->getType() != Expr->getType())
304 LHS = SE.getZeroExtendExpr(LHS, Expr->getType());
305 const SCEV *RHS =
306 SE.getConstant(APInt(SE.getTypeSizeInBits(Expr->getType()), 1)
307 << SE.getTypeSizeInBits(TruncTy));
308 return Op0.match(LHS) && Op1.match(RHS);
309 }
310
311 const SCEV *A;
312 const SCEVMulExpr *Mul;
314 return false;
315
316 // URem is represented as `A - ((A udiv B) * B)`. Only construct the complex
317 // SCEV expression, if the multiply of the expression to check has a UDiv
318 // operand.
319 if (none_of(Mul->operands(),
320 [](const SCEV *Op) { return isa<SCEVUDivExpr>(Op); }))
321 return false;
322
323 const auto MatchURemWithDivisor = [&](const SCEV *B) {
324 // (SomeExpr + (-(SomeExpr / B) * B)).
325 if (Expr == SE.getURemExpr(A, B))
326 return Op0.match(A) && Op1.match(B);
327 return false;
328 };
329
330 // (SomeExpr + (-1 * (SomeExpr / B) * B)).
331 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
332 return MatchURemWithDivisor(Mul->getOperand(1)) ||
333 MatchURemWithDivisor(Mul->getOperand(2));
334
335 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
336 if (Mul->getNumOperands() == 2)
337 return MatchURemWithDivisor(Mul->getOperand(1)) ||
338 MatchURemWithDivisor(Mul->getOperand(0)) ||
339 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(1))) ||
340 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(0)));
341 return false;
342 }
343};
344
345/// Match the mathematical pattern A - (A / B) * B, where A and B can be
346/// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used
347/// for URem with constant power-of-2 second operands. It's not always easy, as
348/// A and B can be folded (imagine A is X / 2, and B is 4, A / B becomes X / 8).
349template <typename Op0_t, typename Op1_t>
354
355inline auto m_Loop() { return m_Isa<const Loop>(); }
356
357/// Match an affine SCEVAddRecExpr.
358template <typename Op0_t, typename Op1_t, typename Loop_t>
361 Loop_t Loop;
362
363 SCEVAffineAddRec_match(Op0_t Op0, Op1_t Op1, Loop_t Loop)
364 : Ops(Op0, Op1), Loop(Loop) {}
365
366 bool match(const SCEV *S) const {
367 return Ops.match(S) && Loop.match(cast<SCEVAddRecExpr>(S)->getLoop());
368 }
369};
370
371/// Match a specified const Loop*.
373 const Loop *L;
374
375 specificloop_ty(const Loop *L) : L(L) {}
376
377 bool match(const Loop *L) const { return L == this->L; }
378};
379
380inline specificloop_ty m_SpecificLoop(const Loop *L) { return L; }
381
382inline match_bind<const Loop> m_Loop(const Loop *&L) { return L; }
383
384template <typename Op0_t, typename Op1_t>
385inline SCEVAffineAddRec_match<Op0_t, Op1_t, match_isa<const Loop>>
386m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1) {
388 m_Loop());
389}
390
391template <typename Op0_t, typename Op1_t, typename Loop_t>
392inline SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>
393m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1, const Loop_t &L) {
395}
396
398 bool match(const SCEV *S) const {
399 const SCEVUnknown *Unknown;
401 isa<UndefValue>(Unknown->getValue());
402 }
403};
404
405/// Match an SCEVUnknown wrapping undef or poison.
409
410} // namespace SCEVPatternMatch
411} // namespace llvm
412
413#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define P(N)
Value * RHS
Value * LHS
Class for arbitrary precision integers.
Definition APInt.h:78
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
This node represents an addition of some number of SCEVs.
This class represents a constant integer value.
This node represents multiplication of some number of SCEVs.
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents an analyzed expression in the program.
SCEVNoWrapFlags NoWrapFlags
static constexpr auto FlagAnyWrap
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
cstval_pred_ty< Predicate, ConstantInt, AllowPoison > cst_pred_ty
specialization of cstval_pred_ty for ConstantInt
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
cst_pred_ty< is_all_ones > m_scev_AllOnes()
Match an integer with all bits set.
SCEVUnaryExpr_match< SCEVZeroExtendExpr, Op0_t > m_scev_ZExt(const Op0_t &Op0)
is_undef_or_poison m_scev_UndefOrPoison()
Match an SCEVUnknown wrapping undef or poison.
SCEVBinaryExpr_match< SCEVMinMaxExpr, Op0_t, Op1_t > m_scev_MinMax(const Op0_t &Op0, const Op1_t &Op1)
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
cst_pred_ty< is_specific_signed_cst > m_scev_SpecificSInt(int64_t V)
Match an SCEV constant with a plain signed integer (sign-extended value will be matched)
SCEVBinaryExpr_match< SCEVTy, Op0_t, Op1_t, WrapFlags, Commutable > m_scev_Binary(const Op0_t &Op0, const Op1_t &Op1)
SCEVUnaryExpr_match< SCEVTy, Op0_t > m_scev_Unary(const Op0_t &Op0)
SCEVUnaryExpr_match< SCEVSignExtendExpr, Op0_t > m_scev_SExt(const Op0_t &Op0)
SCEVUnaryExpr_match< SCEVPtrToAddrExpr, Op0_t > m_scev_PtrToAddr(const Op0_t &Op0)
match_bind< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
cst_pred_ty< is_zero > m_scev_Zero()
Match an integer 0.
SCEVUnaryExpr_match< SCEVTruncateExpr, Op0_t > m_scev_Trunc(const Op0_t &Op0)
bool match(const SCEV *S, const Pattern &P)
SCEVBinaryExpr_match< SCEVUDivExpr, Op0_t, Op1_t > m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1)
specificscev_ty m_scev_Specific(const SCEV *S)
Match if we have a specific specified SCEV.
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
match_bind< const SCEVUnknown > m_SCEVUnknown(const SCEVUnknown *&V)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNUW, true > m_scev_c_NUWMul(const Op0_t &Op0, const Op1_t &Op1)
match_bind< const SCEVAddExpr > m_scev_Add(const SCEVAddExpr *&V)
cst_pred_ty< is_specific_cst > m_scev_SpecificInt(uint64_t V)
Match an SCEV constant with a plain unsigned integer.
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1)
SCEVBinaryExpr_match< SCEVSMaxExpr, Op0_t, Op1_t > m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1)
SCEVURem_match< Op0_t, Op1_t > m_scev_URem(Op0_t LHS, Op1_t RHS, ScalarEvolution &SE)
Match the mathematical pattern A - (A / B) * B, where A and B can be arbitrary expressions.
This is an optimization pass for GlobalISel generic memory operations.
@ Unknown
Not known to have no common set bits.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1753
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Mul
Product of integers.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
Matcher to bind the captured value.
SCEVBinaryExpr_match< SCEVAddRecExpr, Op0_t, Op1_t > Ops
SCEVURem_match(Op0_t Op0, Op1_t Op1, ScalarEvolution &SE)