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 {
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>
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,
203 SCEV::NoWrapFlags WrapFlags = SCEV::FlagNone, bool Commutable = false>
207
209
210 bool match(const SCEV *S) const {
211 if (auto WrappingS = dyn_cast<SCEVNAryExpr>(S))
212 if (WrappingS->getNoWrapFlags(WrapFlags) != WrapFlags)
213 return false;
214
215 auto *E = dyn_cast<SCEVTy>(S);
216 return E && E->getNumOperands() == 2 &&
217 ((Op0.match(E->getOperand(0).getPointer()) &&
218 Op1.match(E->getOperand(1).getPointer())) ||
219 (Commutable && Op0.match(E->getOperand(1).getPointer()) &&
220 Op1.match(E->getOperand(0).getPointer())));
221 }
222};
223
224template <typename SCEVTy, typename Op0_t, typename Op1_t,
225 SCEV::NoWrapFlags WrapFlags = SCEV::FlagNone, bool Commutable = false>
226inline SCEVBinaryExpr_match<SCEVTy, Op0_t, Op1_t, WrapFlags, Commutable>
231
232template <typename Op0_t, typename Op1_t>
233inline SCEVBinaryExpr_match<SCEVAddExpr, Op0_t, Op1_t>
234m_scev_Add(const Op0_t &Op0, const Op1_t &Op1) {
235 return m_scev_Binary<SCEVAddExpr>(Op0, Op1);
236}
237
238template <typename Op0_t, typename Op1_t>
239inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t>
240m_scev_Mul(const Op0_t &Op0, const Op1_t &Op1) {
241 return m_scev_Binary<SCEVMulExpr>(Op0, Op1);
242}
243
244template <typename Op0_t, typename Op1_t>
245inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNone, true>
246m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1) {
248 Op1);
249}
250
251template <typename Op0_t, typename Op1_t>
252inline SCEVBinaryExpr_match<SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNUW, true>
253m_scev_c_NUWMul(const Op0_t &Op0, const Op1_t &Op1) {
255 Op1);
256}
257
258template <typename Op0_t, typename Op1_t>
259inline SCEVBinaryExpr_match<SCEVUDivExpr, Op0_t, Op1_t>
260m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1) {
261 return m_scev_Binary<SCEVUDivExpr>(Op0, Op1);
262}
263
264template <typename Op0_t, typename Op1_t>
265inline SCEVBinaryExpr_match<SCEVSMaxExpr, Op0_t, Op1_t, SCEV::FlagNone, true>
266m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1) {
268 Op1);
269}
270
271template <typename Op0_t, typename Op1_t>
272inline SCEVBinaryExpr_match<SCEVUMaxExpr, Op0_t, Op1_t, SCEV::FlagNone, true>
273m_scev_UMax(const Op0_t &Op0, const Op1_t &Op1) {
275 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 {
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 // URem is represented as `A - ((A udiv B) * B)`. Only construct the complex
323 // SCEV expression, if the multiply of the expression to check has a UDiv
324 // operand.
325 if (none_of(Mul->operands(),
326 [](const SCEV *Op) { return isa<SCEVUDivExpr>(Op); }))
327 return false;
328
329 const auto MatchURemWithDivisor = [&](const SCEV *B) {
330 // (SomeExpr + (-(SomeExpr / B) * B)).
331 if (Expr == SE.getURemExpr(A, B))
332 return Op0.match(A) && Op1.match(B);
333 return false;
334 };
335
336 // (SomeExpr + (-1 * (SomeExpr / B) * B)).
337 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
338 return MatchURemWithDivisor(Mul->getOperand(1)) ||
339 MatchURemWithDivisor(Mul->getOperand(2));
340
341 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
342 if (Mul->getNumOperands() == 2)
343 return MatchURemWithDivisor(Mul->getOperand(1)) ||
344 MatchURemWithDivisor(Mul->getOperand(0)) ||
345 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(1))) ||
346 MatchURemWithDivisor(SE.getNegativeSCEV(Mul->getOperand(0)));
347 return false;
348 }
349};
350
351/// Match the mathematical pattern A - (A / B) * B, where A and B can be
352/// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used
353/// for URem with constant power-of-2 second operands. It's not always easy, as
354/// A and B can be folded (imagine A is X / 2, and B is 4, A / B becomes X / 8).
355template <typename Op0_t, typename Op1_t>
360
361inline auto m_Loop() { return m_Isa<const Loop>(); }
362
363/// Match an affine SCEVAddRecExpr.
364template <typename Op0_t, typename Op1_t, typename Loop_t>
367 Loop_t Loop;
368
370 : Ops(Op0, Op1), Loop(Loop) {}
371
372 bool match(const SCEV *S) const {
373 return Ops.match(S) && Loop.match(cast<SCEVAddRecExpr>(S)->getLoop());
374 }
375};
376
377/// Match a specified const Loop*.
379 const Loop *L;
380
381 specificloop_ty(const Loop *L) : L(L) {}
382
383 bool match(const Loop *L) const { return L == this->L; }
384};
385
386inline specificloop_ty m_SpecificLoop(const Loop *L) { return L; }
387
388inline match_bind<const Loop> m_Loop(const Loop *&L) { return L; }
389
390template <typename Op0_t, typename Op1_t>
391inline SCEVAffineAddRec_match<Op0_t, Op1_t, match_isa<const Loop>>
392m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1) {
394 m_Loop());
395}
396
397template <typename Op0_t, typename Op1_t, typename Loop_t>
398inline SCEVAffineAddRec_match<Op0_t, Op1_t, Loop_t>
399m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1, const Loop_t &L) {
401}
402
404 bool match(const SCEV *S) const {
405 const SCEVUnknown *Unknown;
407 isa<UndefValue>(Unknown->getValue());
408 }
409};
410
411/// Match an SCEVUnknown wrapping undef or poison.
415
416} // namespace SCEVPatternMatch
417} // namespace llvm
418
419#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
Type * getType() const
Return the LLVM type of this SCEV expression.
static constexpr auto FlagNone
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:283
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
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)
SCEVBinaryExpr_match< SCEVUMaxExpr, Op0_t, Op1_t, SCEV::FlagNone, true > m_scev_UMax(const Op0_t &Op0, const Op1_t &Op1)
SCEVBinaryExpr_match< SCEVSMaxExpr, Op0_t, Op1_t, SCEV::FlagNone, true > m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNone, true > m_scev_c_Mul(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.
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:1769
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)