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, SCEV::FlagAnyWrap, true>
268m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1) {
270 Op0, Op1);
271}
272
273template <typename Op0_t, typename Op1_t>
274inline SCEVBinaryExpr_match<SCEVUMaxExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true>
275m_scev_UMax(const Op0_t &Op0, const Op1_t &Op1) {
277 Op0, Op1);
278}
279
280template <typename Op0_t, typename Op1_t>
281inline SCEVBinaryExpr_match<SCEVMinMaxExpr, Op0_t, Op1_t>
282m_scev_MinMax(const Op0_t &Op0, const Op1_t &Op1) {
283 return m_scev_Binary<SCEVMinMaxExpr>(Op0, Op1);
284}
285
286/// Match unsigned remainder pattern.
287/// Matches patterns generated by getURemExpr.
288template <typename Op0_t, typename Op1_t> struct SCEVURem_match {
289 Op0_t Op0;
292
295
296 bool match(const SCEV *Expr) const {
297 if (Expr->getType()->isPointerTy())
298 return false;
299
300 // Try to match 'zext (trunc A to iB) to iY', which is used
301 // for URem with constant power-of-2 second operands. Make sure the size of
302 // the operand A matches the size of the whole expressions.
303 const SCEV *LHS;
305 Type *TruncTy = cast<SCEVZeroExtendExpr>(Expr)->getOperand()->getType();
306 // Bail out if the type of the LHS is larger than the type of the
307 // expression for now.
308 if (SE.getTypeSizeInBits(LHS->getType()) >
309 SE.getTypeSizeInBits(Expr->getType()))
310 return false;
311 if (LHS->getType() != Expr->getType())
312 LHS = SE.getZeroExtendExpr(LHS, Expr->getType());
313 const SCEV *RHS =
314 SE.getConstant(APInt(SE.getTypeSizeInBits(Expr->getType()), 1)
315 << SE.getTypeSizeInBits(TruncTy));
316 return Op0.match(LHS) && Op1.match(RHS);
317 }
318
319 const SCEV *A;
320 const SCEVMulExpr *Mul;
322 return false;
323
324 // URem is represented as `A - ((A udiv B) * B)`. Only construct the complex
325 // SCEV expression, if the multiply of the expression to check has a UDiv
326 // operand.
327 if (none_of(Mul->operands(),
328 [](const SCEV *Op) { return isa<SCEVUDivExpr>(Op); }))
329 return false;
330
331 const auto MatchURemWithDivisor = [&](const SCEV *B) {
332 // (SomeExpr + (-(SomeExpr / B) * B)).
333 if (Expr == SE.getURemExpr(A, B))
334 return Op0.match(A) && Op1.match(B);
335 return false;
336 };
337
338 // (SomeExpr + (-1 * (SomeExpr / B) * B)).
339 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
340 return MatchURemWithDivisor(Mul->getOperand(1)) ||
341 MatchURemWithDivisor(Mul->getOperand(2));
342
343 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
344 if (Mul->getNumOperands() == 2)
345 return MatchURemWithDivisor(Mul->getOperand(1)) ||
346 MatchURemWithDivisor(Mul->getOperand(0)) ||
347 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(1))) ||
348 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(0)));
349 return false;
350 }
351};
352
353/// Match the mathematical pattern A - (A / B) * B, where A and B can be
354/// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used
355/// for URem with constant power-of-2 second operands. It's not always easy, as
356/// A and B can be folded (imagine A is X / 2, and B is 4, A / B becomes X / 8).
357template <typename Op0_t, typename Op1_t>
362
363inline auto m_Loop() { return m_Isa<const Loop>(); }
364
365/// Match an affine SCEVAddRecExpr.
366template <typename Op0_t, typename Op1_t, typename Loop_t>
369 Loop_t Loop;
370
371 SCEVAffineAddRec_match(Op0_t Op0, Op1_t Op1, Loop_t Loop)
372 : Ops(Op0, Op1), Loop(Loop) {}
373
374 bool match(const SCEV *S) const {
375 return Ops.match(S) && Loop.match(cast<SCEVAddRecExpr>(S)->getLoop());
376 }
377};
378
379/// Match a specified const Loop*.
381 const Loop *L;
382
383 specificloop_ty(const Loop *L) : L(L) {}
384
385 bool match(const Loop *L) const { return L == this->L; }
386};
387
388inline specificloop_ty m_SpecificLoop(const Loop *L) { return L; }
389
390inline match_bind<const Loop> m_Loop(const Loop *&L) { return L; }
391
392template <typename Op0_t, typename Op1_t>
393inline SCEVAffineAddRec_match<Op0_t, Op1_t, match_isa<const Loop>>
394m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1) {
396 m_Loop());
397}
398
399template <typename Op0_t, typename Op1_t, typename Loop_t>
400inline SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>
401m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1, const Loop_t &L) {
403}
404
406 bool match(const SCEV *S) const {
407 const SCEVUnknown *Unknown;
409 isa<UndefValue>(Unknown->getValue());
410 }
411};
412
413/// Match an SCEVUnknown wrapping undef or poison.
417
418} // namespace SCEVPatternMatch
419} // namespace llvm
420
421#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
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)
SCEVBinaryExpr_match< SCEVUMaxExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_UMax(const Op0_t &Op0, const Op1_t &Op1)
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< SCEVSMaxExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_c_Mul(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)