LLVM 19.0.0git
ScalarEvolutionDivision.cpp
Go to the documentation of this file.
1//===- ScalarEvolutionDivision.h - See below --------------------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the class that knows how to divide SCEV's.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/DenseMap.h"
19#include <cassert>
20#include <cstdint>
21
22namespace llvm {
23class Type;
24}
25
26using namespace llvm;
27
28namespace {
29
30static inline int sizeOfSCEV(const SCEV *S) {
31 struct FindSCEVSize {
32 int Size = 0;
33
34 FindSCEVSize() = default;
35
36 bool follow(const SCEV *S) {
37 ++Size;
38 // Keep looking at all operands of S.
39 return true;
40 }
41
42 bool isDone() const { return false; }
43 };
44
45 FindSCEVSize F;
47 ST.visitAll(S);
48 return F.Size;
49}
50
51} // namespace
52
53// Computes the Quotient and Remainder of the division of Numerator by
54// Denominator.
55void SCEVDivision::divide(ScalarEvolution &SE, const SCEV *Numerator,
56 const SCEV *Denominator, const SCEV **Quotient,
57 const SCEV **Remainder) {
58 assert(Numerator && Denominator && "Uninitialized SCEV");
59
60 SCEVDivision D(SE, Numerator, Denominator);
61
62 // Check for the trivial case here to avoid having to check for it in the
63 // rest of the code.
64 if (Numerator == Denominator) {
65 *Quotient = D.One;
66 *Remainder = D.Zero;
67 return;
68 }
69
70 if (Numerator->isZero()) {
71 *Quotient = D.Zero;
72 *Remainder = D.Zero;
73 return;
74 }
75
76 // A simple case when N/1. The quotient is N.
77 if (Denominator->isOne()) {
78 *Quotient = Numerator;
79 *Remainder = D.Zero;
80 return;
81 }
82
83 // Split the Denominator when it is a product.
84 if (const SCEVMulExpr *T = dyn_cast<SCEVMulExpr>(Denominator)) {
85 const SCEV *Q, *R;
86 *Quotient = Numerator;
87 for (const SCEV *Op : T->operands()) {
88 divide(SE, *Quotient, Op, &Q, &R);
89 *Quotient = Q;
90
91 // Bail out when the Numerator is not divisible by one of the terms of
92 // the Denominator.
93 if (!R->isZero()) {
94 *Quotient = D.Zero;
95 *Remainder = Numerator;
96 return;
97 }
98 }
99 *Remainder = D.Zero;
100 return;
101 }
102
103 D.visit(Numerator);
104 *Quotient = D.Quotient;
105 *Remainder = D.Remainder;
106}
107
109 if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
110 APInt NumeratorVal = Numerator->getAPInt();
111 APInt DenominatorVal = D->getAPInt();
112 uint32_t NumeratorBW = NumeratorVal.getBitWidth();
113 uint32_t DenominatorBW = DenominatorVal.getBitWidth();
114
115 if (NumeratorBW > DenominatorBW)
116 DenominatorVal = DenominatorVal.sext(NumeratorBW);
117 else if (NumeratorBW < DenominatorBW)
118 NumeratorVal = NumeratorVal.sext(DenominatorBW);
119
120 APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
121 APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
122 APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
123 Quotient = SE.getConstant(QuotientVal);
124 Remainder = SE.getConstant(RemainderVal);
125 return;
126 }
127}
128
129void SCEVDivision::visitVScale(const SCEVVScale *Numerator) {
130 return cannotDivide(Numerator);
131}
132
134 const SCEV *StartQ, *StartR, *StepQ, *StepR;
135 if (!Numerator->isAffine())
136 return cannotDivide(Numerator);
137 divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
138 divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
139 // Bail out if the types do not match.
140 Type *Ty = Denominator->getType();
141 if (Ty != StartQ->getType() || Ty != StartR->getType() ||
142 Ty != StepQ->getType() || Ty != StepR->getType())
143 return cannotDivide(Numerator);
144 Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
145 Numerator->getNoWrapFlags());
146 Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
147 Numerator->getNoWrapFlags());
148}
149
152 Type *Ty = Denominator->getType();
153
154 for (const SCEV *Op : Numerator->operands()) {
155 const SCEV *Q, *R;
156 divide(SE, Op, Denominator, &Q, &R);
157
158 // Bail out if types do not match.
159 if (Ty != Q->getType() || Ty != R->getType())
160 return cannotDivide(Numerator);
161
162 Qs.push_back(Q);
163 Rs.push_back(R);
164 }
165
166 if (Qs.size() == 1) {
167 Quotient = Qs[0];
168 Remainder = Rs[0];
169 return;
170 }
171
172 Quotient = SE.getAddExpr(Qs);
173 Remainder = SE.getAddExpr(Rs);
174}
175
178 Type *Ty = Denominator->getType();
179
180 bool FoundDenominatorTerm = false;
181 for (const SCEV *Op : Numerator->operands()) {
182 // Bail out if types do not match.
183 if (Ty != Op->getType())
184 return cannotDivide(Numerator);
185
186 if (FoundDenominatorTerm) {
187 Qs.push_back(Op);
188 continue;
189 }
190
191 // Check whether Denominator divides one of the product operands.
192 const SCEV *Q, *R;
193 divide(SE, Op, Denominator, &Q, &R);
194 if (!R->isZero()) {
195 Qs.push_back(Op);
196 continue;
197 }
198
199 // Bail out if types do not match.
200 if (Ty != Q->getType())
201 return cannotDivide(Numerator);
202
203 FoundDenominatorTerm = true;
204 Qs.push_back(Q);
205 }
206
207 if (FoundDenominatorTerm) {
208 Remainder = Zero;
209 if (Qs.size() == 1)
210 Quotient = Qs[0];
211 else
212 Quotient = SE.getMulExpr(Qs);
213 return;
214 }
215
216 if (!isa<SCEVUnknown>(Denominator))
217 return cannotDivide(Numerator);
218
219 // The Remainder is obtained by replacing Denominator by 0 in Numerator.
220 ValueToSCEVMapTy RewriteMap;
221 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = Zero;
222 Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap);
223
224 if (Remainder->isZero()) {
225 // The Quotient is obtained by replacing Denominator by 1 in Numerator.
226 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = One;
227 Quotient = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap);
228 return;
229 }
230
231 // Quotient is (Numerator - Remainder) divided by Denominator.
232 const SCEV *Q, *R;
233 const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
234 // This SCEV does not seem to simplify: fail the division here.
235 if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator))
236 return cannotDivide(Numerator);
237 divide(SE, Diff, Denominator, &Q, &R);
238 if (R != Zero)
239 return cannotDivide(Numerator);
240 Quotient = Q;
241}
242
243SCEVDivision::SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
244 const SCEV *Denominator)
245 : SE(S), Denominator(Denominator) {
246 Zero = SE.getZero(Denominator->getType());
247 One = SE.getOne(Denominator->getType());
248
249 // We generally do not know how to divide Expr by Denominator. We initialize
250 // the division to a "cannot divide" state to simplify the rest of the code.
251 cannotDivide(Numerator);
252}
253
254// Convenience function for giving up on the division. We set the quotient to
255// be equal to zero and the remainder to be equal to the numerator.
256void SCEVDivision::cannotDivide(const SCEV *Numerator) {
257 Quotient = Zero;
258 Remainder = Numerator;
259}
This file implements a class to represent arbitrary precision integral constant values and operations...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file defines the DenseMap class.
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Class for arbitrary precision integers.
Definition: APInt.h:76
static void sdivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Definition: APInt.cpp:1896
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1433
APInt sext(unsigned width) const
Sign extend to a new width.
Definition: APInt.cpp:954
This class represents an Operation in the Expression.
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
This class represents a constant integer value.
const APInt & getAPInt() const
This node represents multiplication of some number of SCEVs.
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
ArrayRef< const SCEV * > operands() const
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, ValueToSCEVMapTy &Map)
Visit all nodes in the expression tree using worklist traversal.
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
This class represents an analyzed expression in the program.
bool isOne() const
Return true if the expression is a constant one.
bool isZero() const
Return true if the expression is a constant zero.
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
const SCEV * getConstant(ConstantInt *V)
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
static void divide(ScalarEvolution &SE, const SCEV *Numerator, const SCEV *Denominator, const SCEV **Quotient, const SCEV **Remainder)
void visitVScale(const SCEVVScale *Numerator)
void visitAddRecExpr(const SCEVAddRecExpr *Numerator)
void visitConstant(const SCEVConstant *Numerator)
void visitAddExpr(const SCEVAddExpr *Numerator)
void visitMulExpr(const SCEVMulExpr *Numerator)