Skip to content

Commit

Permalink
[SCEV][NFC] GetMinTrailingZeros switch case and naming cleanup
Browse files Browse the repository at this point in the history
* combine zext and sext into the one switch case
* combine vscale and udiv into one switch case
* renames according to LLVM style
  • Loading branch information
caojoshua committed Apr 11, 2023
1 parent 915a45c commit 921b8f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions llvm/include/llvm/Analysis/ScalarEvolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ class ScalarEvolution {
/// (at every loop iteration). It is, at the same time, the minimum number
/// of times S is divisible by 2. For example, given {4,+,8} it returns 2.
/// If S is guaranteed to be 0, it returns the bitwidth of S.
uint32_t GetMinTrailingZeros(const SCEV *S);
uint32_t getMinTrailingZeros(const SCEV *S);

/// Determine the unsigned range for a particular SCEV.
/// NOTE: This returns a copy of the reference returned by getRangeRef.
Expand Down Expand Up @@ -1438,7 +1438,7 @@ class ScalarEvolution {
ArrayRef<Value *> getSCEVValues(const SCEV *S);

/// Private helper method for the GetMinTrailingZeros method
uint32_t GetMinTrailingZerosImpl(const SCEV *S);
uint32_t getMinTrailingZerosImpl(const SCEV *S);

/// Information about the number of times a particular loop exit may be
/// reached before exiting the loop.
Expand Down
48 changes: 21 additions & 27 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
}

// Return zero if truncating to known zeros.
uint32_t MinTrailingZeros = GetMinTrailingZeros(Op);
uint32_t MinTrailingZeros = getMinTrailingZeros(Op);
if (MinTrailingZeros >= getTypeSizeInBits(Ty))
return getZero(Ty);

Expand Down Expand Up @@ -1497,7 +1497,7 @@ static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
// Find number of trailing zeros of (x + y + ...) w/o the C first:
uint32_t TZ = BitWidth;
for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
TZ = std::min(TZ, SE.GetMinTrailingZeros(WholeAddExpr->getOperand(I)));
TZ = std::min(TZ, SE.getMinTrailingZeros(WholeAddExpr->getOperand(I)));
if (TZ) {
// Set D to be as many least significant bits of C as possible while still
// guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
Expand All @@ -1514,7 +1514,7 @@ static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
const APInt &ConstantStart,
const SCEV *Step) {
const unsigned BitWidth = ConstantStart.getBitWidth();
const uint32_t TZ = SE.GetMinTrailingZeros(Step);
const uint32_t TZ = SE.getMinTrailingZeros(Step);
if (TZ)
return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
: ConstantStart;
Expand Down Expand Up @@ -6270,42 +6270,36 @@ const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
return getGEPExpr(GEP, IndexExprs);
}

uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
uint32_t ScalarEvolution::getMinTrailingZerosImpl(const SCEV *S) {
switch (S->getSCEVType()) {
case scConstant:
return cast<SCEVConstant>(S)->getAPInt().countr_zero();
case scVScale:
return 0;
case scTruncate: {
const SCEVTruncateExpr *T = cast<SCEVTruncateExpr>(S);
return std::min(GetMinTrailingZeros(T->getOperand()),
return std::min(getMinTrailingZeros(T->getOperand()),
(uint32_t)getTypeSizeInBits(T->getType()));
}
case scZeroExtend: {
const SCEVZeroExtendExpr *E = cast<SCEVZeroExtendExpr>(S);
uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
return OpRes == getTypeSizeInBits(E->getOperand()->getType())
? getTypeSizeInBits(E->getType())
: OpRes;
}
case scZeroExtend:
case scSignExtend: {
const SCEVSignExtendExpr *E = cast<SCEVSignExtendExpr>(S);
uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
const SCEVIntegralCastExpr *E = cast<SCEVIntegralCastExpr>(S);
uint32_t OpRes = getMinTrailingZeros(E->getOperand());
return OpRes == getTypeSizeInBits(E->getOperand()->getType())
? getTypeSizeInBits(E->getType())
: OpRes;
}
case scMulExpr: {
const SCEVMulExpr *M = cast<SCEVMulExpr>(S);
// The result is the sum of all operands results.
uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
uint32_t SumOpRes = getMinTrailingZeros(M->getOperand(0));
uint32_t BitWidth = getTypeSizeInBits(M->getType());
for (unsigned i = 1, e = M->getNumOperands();
SumOpRes != BitWidth && i != e; ++i)
for (unsigned I = 1, E = M->getNumOperands();
SumOpRes != BitWidth && I != E; ++I)
SumOpRes =
std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
std::min(SumOpRes + getMinTrailingZeros(M->getOperand(I)), BitWidth);
return SumOpRes;
}
case scVScale:
return 0;
case scUDivExpr:
return 0;
case scPtrToInt:
Expand All @@ -6318,9 +6312,9 @@ uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
case scSequentialUMinExpr: {
// The result is the min of all operands results.
ArrayRef<const SCEV *> Ops = S->operands();
uint32_t MinOpRes = GetMinTrailingZeros(Ops[0]);
uint32_t MinOpRes = getMinTrailingZeros(Ops[0]);
for (unsigned I = 1, E = Ops.size(); MinOpRes && I != E; ++I)
MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(Ops[I]));
MinOpRes = std::min(MinOpRes, getMinTrailingZeros(Ops[I]));
return MinOpRes;
}
case scUnknown: {
Expand All @@ -6336,12 +6330,12 @@ uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
llvm_unreachable("Unknown SCEV kind!");
}

uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
uint32_t ScalarEvolution::getMinTrailingZeros(const SCEV *S) {
auto I = MinTrailingZerosCache.find(S);
if (I != MinTrailingZerosCache.end())
return I->second;

uint32_t Result = GetMinTrailingZerosImpl(S);
uint32_t Result = getMinTrailingZerosImpl(S);
auto InsertPair = MinTrailingZerosCache.insert({S, Result});
assert(InsertPair.second && "Should insert a new key");
return InsertPair.first->second;
Expand Down Expand Up @@ -6595,7 +6589,7 @@ const ConstantRange &ScalarEvolution::getRangeRef(

// If the value has known zeros, the maximum value will have those known zeros
// as well.
uint32_t TZ = GetMinTrailingZeros(S);
uint32_t TZ = getMinTrailingZeros(S);
if (TZ != 0) {
if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
ConservativeResult =
Expand Down Expand Up @@ -8245,7 +8239,7 @@ unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
if (!TC)
// Attempt to factor more general cases. Returns the greatest power of
// two divisor.
return GetSmallMultiple(GetMinTrailingZeros(TCExpr));
return GetSmallMultiple(getMinTrailingZeros(TCExpr));

ConstantInt *Result = TC->getValue();
assert(Result && "SCEVConstant expected to have non-null ConstantInt");
Expand Down Expand Up @@ -10108,7 +10102,7 @@ static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
//
// B is divisible by D if and only if the multiplicity of prime factor 2 for B
// is not less than multiplicity of this prime factor for D.
if (SE.GetMinTrailingZeros(B) < Mult2)
if (SE.getMinTrailingZeros(B) < Mult2)
return SE.getCouldNotCompute();

// 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
Expand Down

0 comments on commit 921b8f4

Please sign in to comment.