llvm-project
259 строк · 7.5 Кб
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
13#include "llvm/Analysis/ScalarEvolutionDivision.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/Analysis/ScalarEvolution.h"
18#include "llvm/Support/Casting.h"
19#include <cassert>
20#include <cstdint>
21
22namespace llvm {
23class Type;
24} // namespace llvm
25
26using namespace llvm;
27
28namespace {
29
30static inline int sizeOfSCEV(const SCEV *S) {
31struct FindSCEVSize {
32int Size = 0;
33
34FindSCEVSize() = default;
35
36bool follow(const SCEV *S) {
37++Size;
38// Keep looking at all operands of S.
39return true;
40}
41
42bool isDone() const { return false; }
43};
44
45FindSCEVSize F;
46SCEVTraversal<FindSCEVSize> ST(F);
47ST.visitAll(S);
48return 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,
56const SCEV *Denominator, const SCEV **Quotient,
57const SCEV **Remainder) {
58assert(Numerator && Denominator && "Uninitialized SCEV");
59
60SCEVDivision 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.
64if (Numerator == Denominator) {
65*Quotient = D.One;
66*Remainder = D.Zero;
67return;
68}
69
70if (Numerator->isZero()) {
71*Quotient = D.Zero;
72*Remainder = D.Zero;
73return;
74}
75
76// A simple case when N/1. The quotient is N.
77if (Denominator->isOne()) {
78*Quotient = Numerator;
79*Remainder = D.Zero;
80return;
81}
82
83// Split the Denominator when it is a product.
84if (const SCEVMulExpr *T = dyn_cast<SCEVMulExpr>(Denominator)) {
85const SCEV *Q, *R;
86*Quotient = Numerator;
87for (const SCEV *Op : T->operands()) {
88divide(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.
93if (!R->isZero()) {
94*Quotient = D.Zero;
95*Remainder = Numerator;
96return;
97}
98}
99*Remainder = D.Zero;
100return;
101}
102
103D.visit(Numerator);
104*Quotient = D.Quotient;
105*Remainder = D.Remainder;
106}
107
108void SCEVDivision::visitConstant(const SCEVConstant *Numerator) {
109if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
110APInt NumeratorVal = Numerator->getAPInt();
111APInt DenominatorVal = D->getAPInt();
112uint32_t NumeratorBW = NumeratorVal.getBitWidth();
113uint32_t DenominatorBW = DenominatorVal.getBitWidth();
114
115if (NumeratorBW > DenominatorBW)
116DenominatorVal = DenominatorVal.sext(NumeratorBW);
117else if (NumeratorBW < DenominatorBW)
118NumeratorVal = NumeratorVal.sext(DenominatorBW);
119
120APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
121APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
122APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
123Quotient = SE.getConstant(QuotientVal);
124Remainder = SE.getConstant(RemainderVal);
125return;
126}
127}
128
129void SCEVDivision::visitVScale(const SCEVVScale *Numerator) {
130return cannotDivide(Numerator);
131}
132
133void SCEVDivision::visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
134const SCEV *StartQ, *StartR, *StepQ, *StepR;
135if (!Numerator->isAffine())
136return cannotDivide(Numerator);
137divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
138divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
139// Bail out if the types do not match.
140Type *Ty = Denominator->getType();
141if (Ty != StartQ->getType() || Ty != StartR->getType() ||
142Ty != StepQ->getType() || Ty != StepR->getType())
143return cannotDivide(Numerator);
144Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
145Numerator->getNoWrapFlags());
146Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
147Numerator->getNoWrapFlags());
148}
149
150void SCEVDivision::visitAddExpr(const SCEVAddExpr *Numerator) {
151SmallVector<const SCEV *, 2> Qs, Rs;
152Type *Ty = Denominator->getType();
153
154for (const SCEV *Op : Numerator->operands()) {
155const SCEV *Q, *R;
156divide(SE, Op, Denominator, &Q, &R);
157
158// Bail out if types do not match.
159if (Ty != Q->getType() || Ty != R->getType())
160return cannotDivide(Numerator);
161
162Qs.push_back(Q);
163Rs.push_back(R);
164}
165
166if (Qs.size() == 1) {
167Quotient = Qs[0];
168Remainder = Rs[0];
169return;
170}
171
172Quotient = SE.getAddExpr(Qs);
173Remainder = SE.getAddExpr(Rs);
174}
175
176void SCEVDivision::visitMulExpr(const SCEVMulExpr *Numerator) {
177SmallVector<const SCEV *, 2> Qs;
178Type *Ty = Denominator->getType();
179
180bool FoundDenominatorTerm = false;
181for (const SCEV *Op : Numerator->operands()) {
182// Bail out if types do not match.
183if (Ty != Op->getType())
184return cannotDivide(Numerator);
185
186if (FoundDenominatorTerm) {
187Qs.push_back(Op);
188continue;
189}
190
191// Check whether Denominator divides one of the product operands.
192const SCEV *Q, *R;
193divide(SE, Op, Denominator, &Q, &R);
194if (!R->isZero()) {
195Qs.push_back(Op);
196continue;
197}
198
199// Bail out if types do not match.
200if (Ty != Q->getType())
201return cannotDivide(Numerator);
202
203FoundDenominatorTerm = true;
204Qs.push_back(Q);
205}
206
207if (FoundDenominatorTerm) {
208Remainder = Zero;
209if (Qs.size() == 1)
210Quotient = Qs[0];
211else
212Quotient = SE.getMulExpr(Qs);
213return;
214}
215
216if (!isa<SCEVUnknown>(Denominator))
217return cannotDivide(Numerator);
218
219// The Remainder is obtained by replacing Denominator by 0 in Numerator.
220ValueToSCEVMapTy RewriteMap;
221RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = Zero;
222Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap);
223
224if (Remainder->isZero()) {
225// The Quotient is obtained by replacing Denominator by 1 in Numerator.
226RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = One;
227Quotient = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap);
228return;
229}
230
231// Quotient is (Numerator - Remainder) divided by Denominator.
232const SCEV *Q, *R;
233const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
234// This SCEV does not seem to simplify: fail the division here.
235if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator))
236return cannotDivide(Numerator);
237divide(SE, Diff, Denominator, &Q, &R);
238if (R != Zero)
239return cannotDivide(Numerator);
240Quotient = Q;
241}
242
243SCEVDivision::SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
244const SCEV *Denominator)
245: SE(S), Denominator(Denominator) {
246Zero = SE.getZero(Denominator->getType());
247One = 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.
251cannotDivide(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) {
257Quotient = Zero;
258Remainder = Numerator;
259}
260