LLVM 23.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
20using namespace llvm::PatternMatchHelpers;
21
22namespace llvm {
24template <typename SCEVPtrT> struct match_bind<SCEVUseT<SCEVPtrT>> {
26
28
29 template <typename ITy> bool match(ITy *V) const {
30 VR = V;
31 return true;
32 }
33};
34} // namespace PatternMatchHelpers
35
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<SCEVPtrToIntExpr, Op0_t>
191m_scev_PtrToInt(const Op0_t &Op0) {
193}
194
195template <typename Op0_t>
196inline SCEVUnaryExpr_match<SCEVPtrToAddrExpr, Op0_t>
197m_scev_PtrToAddr(const Op0_t &Op0) {
199}
200
201template <typename Op0_t>
202inline SCEVUnaryExpr_match<SCEVTruncateExpr, Op0_t>
203m_scev_Trunc(const Op0_t &Op0) {
205}
206
207/// Match a binary SCEV.
208template <typename SCEVTy, typename Op0_t, typename Op1_t,
210 bool Commutable = false>
212 Op0_t Op0;
214
216
217 bool match(const SCEV *S) const {
218 if (auto WrappingS = dyn_cast<SCEVNAryExpr>(S))
219 if (WrappingS->getNoWrapFlags(WrapFlags) != WrapFlags)
220 return false;
221
222 auto *E = dyn_cast<SCEVTy>(S);
223 return E && E->getNumOperands() == 2 &&
224 ((Op0.match(E->getOperand(0).getPointer()) &&
225 Op1.match(E->getOperand(1).getPointer())) ||
226 (Commutable && Op0.match(E->getOperand(1).getPointer()) &&
227 Op1.match(E->getOperand(0).getPointer())));
228 }
229};
230
231template <typename SCEVTy, typename Op0_t, typename Op1_t,
233 bool Commutable = false>
234inline SCEVBinaryExpr_match<SCEVTy, Op0_t, Op1_t, WrapFlags, Commutable>
235m_scev_Binary(const Op0_t &Op0, const Op1_t &Op1) {
237 Op1);
238}
239
240template <typename Op0_t, typename Op1_t>
241inline SCEVBinaryExpr_match<SCEVAddExpr, Op0_t, Op1_t>
242m_scev_Add(const Op0_t &Op0, const Op1_t &Op1) {
243 return m_scev_Binary<SCEVAddExpr>(Op0, Op1);
244}
245
246template <typename Op0_t, typename Op1_t>
247inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t>
248m_scev_Mul(const Op0_t &Op0, const Op1_t &Op1) {
249 return m_scev_Binary<SCEVMulExpr>(Op0, Op1);
250}
251
252template <typename Op0_t, typename Op1_t>
253inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true>
254m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1) {
256 Op1);
257}
258
259template <typename Op0_t, typename Op1_t>
260inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNUW, true>
261m_scev_c_NUWMul(const Op0_t &Op0, const Op1_t &Op1) {
263 Op1);
264}
265
266template <typename Op0_t, typename Op1_t>
267inline SCEVBinaryExpr_match<SCEVUDivExpr, Op0_t, Op1_t>
268m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
269 return m_scev_Binary<SCEVUDivExpr>(Op0, Op1);
270}
271
272template <typename Op0_t, typename Op1_t>
273inline SCEVBinaryExpr_match<SCEVSMaxExpr, Op0_t, Op1_t>
274m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1) {
275 return m_scev_Binary<SCEVSMaxExpr>(Op0, Op1);
276}
277
278template <typename Op0_t, typename Op1_t>
279inline SCEVBinaryExpr_match<SCEVMinMaxExpr, Op0_t, Op1_t>
280m_scev_MinMax(const Op0_t &Op0, const Op1_t &Op1) {
281 return m_scev_Binary<SCEVMinMaxExpr>(Op0, Op1);
282}
283
284/// Match unsigned remainder pattern.
285/// Matches patterns generated by getURemExpr.
286template <typename Op0_t, typename Op1_t> struct SCEVURem_match {
287 Op0_t Op0;
290
293
294 bool match(const SCEV *Expr) const {
295 if (Expr->getType()->isPointerTy())
296 return false;
297
298 // Try to match 'zext (trunc A to iB) to iY', which is used
299 // for URem with constant power-of-2 second operands. Make sure the size of
300 // the operand A matches the size of the whole expressions.
301 const SCEV *LHS;
303 Type *TruncTy = cast<SCEVZeroExtendExpr>(Expr)->getOperand()->getType();
304 // Bail out if the type of the LHS is larger than the type of the
305 // expression for now.
306 if (SE.getTypeSizeInBits(LHS->getType()) >
307 SE.getTypeSizeInBits(Expr->getType()))
308 return false;
309 if (LHS->getType() != Expr->getType())
310 LHS = SE.getZeroExtendExpr(LHS, Expr->getType());
311 const SCEV *RHS =
312 SE.getConstant(APInt(SE.getTypeSizeInBits(Expr->getType()), 1)
313 << SE.getTypeSizeInBits(TruncTy));
314 return Op0.match(LHS) && Op1.match(RHS);
315 }
316
317 const SCEV *A;
318 const SCEVMulExpr *Mul;
320 return false;
321
322 const auto MatchURemWithDivisor = [&](const SCEV *B) {
323 // (SomeExpr + (-(SomeExpr / B) * B)).
324 if (Expr == SE.getURemExpr(A, B))
325 return Op0.match(A) && Op1.match(B);
326 return false;
327 };
328
329 // (SomeExpr + (-1 * (SomeExpr / B) * B)).
330 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
331 return MatchURemWithDivisor(Mul->getOperand(1)) ||
332 MatchURemWithDivisor(Mul->getOperand(2));
333
334 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
335 if (Mul->getNumOperands() == 2)
336 return MatchURemWithDivisor(Mul->getOperand(1)) ||
337 MatchURemWithDivisor(Mul->getOperand(0)) ||
338 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(1))) ||
339 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(0)));
340 return false;
341 }
342};
343
344/// Match the mathematical pattern A - (A / B) * B, where A and B can be
345/// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used
346/// for URem with constant power-of-2 second operands. It's not always easy, as
347/// A and B can be folded (imagine A is X / 2, and B is 4, A / B becomes X / 8).
348template <typename Op0_t, typename Op1_t>
350 ScalarEvolution &SE) {
351 return SCEVURem_match<Op0_t, Op1_t>(LHS, RHS, SE);
352}
353
354inline auto m_Loop() { return m_Isa<const Loop>(); }
355
356/// Match an affine SCEVAddRecExpr.
357template <typename Op0_t, typename Op1_t, typename Loop_t>
360 Loop_t Loop;
361
362 SCEVAffineAddRec_match(Op0_t Op0, Op1_t Op1, Loop_t Loop)
363 : Ops(Op0, Op1), Loop(Loop) {}
364
365 bool match(const SCEV *S) const {
366 return Ops.match(S) && Loop.match(cast<SCEVAddRecExpr>(S)->getLoop());
367 }
368};
369
370/// Match a specified const Loop*.
372 const Loop *L;
373
374 specificloop_ty(const Loop *L) : L(L) {}
375
376 bool match(const Loop *L) const { return L == this->L; }
377};
378
379inline specificloop_ty m_SpecificLoop(const Loop *L) { return L; }
380
381inline match_bind<const Loop> m_Loop(const Loop *&L) { return L; }
382
383template <typename Op0_t, typename Op1_t>
384inline SCEVAffineAddRec_match<Op0_t, Op1_t, match_isa<const Loop>>
385m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1) {
387 m_Loop());
388}
389
390template <typename Op0_t, typename Op1_t, typename Loop_t>
391inline SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>
392m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1, const Loop_t &L) {
394}
395
397 bool match(const SCEV *S) const {
398 const SCEVUnknown *Unknown;
400 isa<UndefValue>(Unknown->getValue());
401 }
402};
403
404/// Match an SCEVUnknown wrapping undef or poison.
408
409} // namespace SCEVPatternMatch
410} // namespace llvm
411
412#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define P(N)
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
LLVM_ABI 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:290
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:284
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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)
SCEVUnaryExpr_match< SCEVPtrToIntExpr, Op0_t > m_scev_PtrToInt(const Op0_t &Op0)
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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.
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)