Skip to content

Commit

Permalink
[SCEV] Use const SCEV * explicitly in more places.
Browse files Browse the repository at this point in the history
Use const SCEV * explicitly in more places to prepare for
llvm#91961. Split off as suggested.
  • Loading branch information
fhahn committed Aug 3, 2024
1 parent 2fe3bbd commit edf46f3
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 104 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/Delinearization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct SCEVCollectAddRecMultiplies {
if (auto *Mul = dyn_cast<SCEVMulExpr>(S)) {
bool HasAddRec = false;
SmallVector<const SCEV *, 0> Operands;
for (const auto *Op : Mul->operands()) {
for (const SCEV *Op : Mul->operands()) {
const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Op);
if (Unknown && !isa<CallInst>(Unknown->getValue())) {
Operands.push_back(Op);
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/IVUsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
// An add is interesting if exactly one of its operands is interesting.
if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
bool AnyInterestingYet = false;
for (const auto *Op : Add->operands())
for (const SCEV *Op : Add->operands())
if (isInteresting(Op, I, L, SE, LI)) {
if (AnyInterestingYet)
return false;
Expand Down Expand Up @@ -346,7 +346,7 @@ static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {
}

if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
for (const auto *Op : Add->operands())
for (const SCEV *Op : Add->operands())
if (const SCEVAddRecExpr *AR = findAddRecForLoop(Op, L))
return AR;
return nullptr;
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ const SCEV *llvm::replaceSymbolicStrideSCEV(PredicatedScalarEvolution &PSE,
assert(isa<SCEVUnknown>(StrideSCEV) && "shouldn't be in map");

ScalarEvolution *SE = PSE.getSE();
const auto *CT = SE->getOne(StrideSCEV->getType());
const SCEV *CT = SE->getOne(StrideSCEV->getType());
PSE.addPredicate(*SE->getEqualPredicate(StrideSCEV, CT));
auto *Expr = PSE.getSCEV(Ptr);
const SCEV *Expr = PSE.getSCEV(Ptr);

LLVM_DEBUG(dbgs() << "LAA: Replacing SCEV: " << *OrigSCEV
<< " by: " << *Expr << "\n");
Expand Down Expand Up @@ -1084,7 +1084,7 @@ bool AccessAnalysis::createCheckForAccess(RuntimePointerChecking &RtCheck,
return false;

if (!isNoWrap(PSE, StridesMap, Ptr, AccessTy, TheLoop)) {
auto *Expr = PSE.getSCEV(Ptr);
const SCEV *Expr = PSE.getSCEV(Ptr);
if (!Assume || !isa<SCEVAddRecExpr>(Expr))
return false;
PSE.setNoOverflow(Ptr, SCEVWrapPredicate::IncrementNUSW);
Expand Down Expand Up @@ -1440,7 +1440,7 @@ static bool isNoWrapAddRec(Value *Ptr, const SCEVAddRecExpr *AR,
// Assume constant for other the operand so that the AddRec can be
// easily found.
isa<ConstantInt>(OBO->getOperand(1))) {
auto *OpScev = PSE.getSCEV(OBO->getOperand(0));
const SCEV *OpScev = PSE.getSCEV(OBO->getOperand(0));

if (auto *OpAR = dyn_cast<SCEVAddRecExpr>(OpScev))
return OpAR->getLoop() == L && OpAR->getNoWrapFlags(SCEV::FlagNSW);
Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/Target/ARM/MVETailPredication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ const SCEV *MVETailPredication::IsSafeActiveMask(IntrinsicInst *ActiveLaneMask,
if (!L->makeLoopInvariant(ElemCount, Changed))
return nullptr;

auto *EC= SE->getSCEV(ElemCount);
auto *TC = SE->getSCEV(TripCount);
const SCEV *EC = SE->getSCEV(ElemCount);
const SCEV *TC = SE->getSCEV(TripCount);
int VectorWidth =
cast<FixedVectorType>(ActiveLaneMask->getType())->getNumElements();
if (VectorWidth != 2 && VectorWidth != 4 && VectorWidth != 8 &&
Expand All @@ -228,7 +228,7 @@ const SCEV *MVETailPredication::IsSafeActiveMask(IntrinsicInst *ActiveLaneMask,
// different counter. Using SCEV, we check that the induction is of the
// form i = i + 4, where the increment must be equal to the VectorWidth.
auto *IV = ActiveLaneMask->getOperand(0);
auto *IVExpr = SE->getSCEV(IV);
const SCEV *IVExpr = SE->getSCEV(IV);
auto *AddExpr = dyn_cast<SCEVAddRecExpr>(IVExpr);

if (!AddExpr) {
Expand Down Expand Up @@ -291,14 +291,16 @@ const SCEV *MVETailPredication::IsSafeActiveMask(IntrinsicInst *ActiveLaneMask,
//
// which what we will be using here.
//
auto *VW = SE->getSCEV(ConstantInt::get(TripCount->getType(), VectorWidth));
const SCEV *VW =
SE->getSCEV(ConstantInt::get(TripCount->getType(), VectorWidth));
// ElementCount + (VW-1):
auto *Start = AddExpr->getStart();
auto *ECPlusVWMinus1 = SE->getAddExpr(EC,
const SCEV *Start = AddExpr->getStart();
const SCEV *ECPlusVWMinus1 = SE->getAddExpr(
EC,
SE->getSCEV(ConstantInt::get(TripCount->getType(), VectorWidth - 1)));

// Ceil = ElementCount + (VW-1) / VW
auto *Ceil = SE->getUDivExpr(ECPlusVWMinus1, VW);
const SCEV *Ceil = SE->getUDivExpr(ECPlusVWMinus1, VW);

// Prevent unused variable warnings with TC
(void)TC;
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ createReplacement(ICmpInst *ICmp, const Loop *L, BasicBlock *ExitingBB,
MaxIter = SE->getZeroExtendExpr(MaxIter, ARTy);
else if (SE->getTypeSizeInBits(ARTy) < SE->getTypeSizeInBits(MaxIterTy)) {
const SCEV *MinusOne = SE->getMinusOne(ARTy);
auto *MaxAllowedIter = SE->getZeroExtendExpr(MinusOne, MaxIterTy);
const SCEV *MaxAllowedIter = SE->getZeroExtendExpr(MinusOne, MaxIterTy);
if (SE->isKnownPredicateAt(ICmpInst::ICMP_ULE, MaxIter, MaxAllowedIter, BI))
MaxIter = SE->getTruncateExpr(MaxIter, ARTy);
}
Expand All @@ -1299,7 +1299,7 @@ createReplacement(ICmpInst *ICmp, const Loop *L, BasicBlock *ExitingBB,
// So we manually construct umin(a - 1, b - 1).
SmallVector<const SCEV *, 4> Elements;
if (auto *UMin = dyn_cast<SCEVUMinExpr>(MaxIter)) {
for (auto *Op : UMin->operands())
for (const SCEV *Op : UMin->operands())
Elements.push_back(SE->getMinusSCEV(Op, SE->getOne(Op->getType())));
MaxIter = SE->getUMinFromMismatchedTypes(Elements);
} else
Expand Down Expand Up @@ -1376,15 +1376,15 @@ static bool optimizeLoopExitWithUnknownExitCount(
for (auto *ICmp : LeafConditions) {
auto EL = SE->computeExitLimitFromCond(L, ICmp, Inverted,
/*ControlsExit*/ false);
auto *ExitMax = EL.SymbolicMaxNotTaken;
const SCEV *ExitMax = EL.SymbolicMaxNotTaken;
if (isa<SCEVCouldNotCompute>(ExitMax))
continue;
// They could be of different types (specifically this happens after
// IV widening).
auto *WiderType =
SE->getWiderType(ExitMax->getType(), MaxIter->getType());
auto *WideExitMax = SE->getNoopOrZeroExtend(ExitMax, WiderType);
auto *WideMaxIter = SE->getNoopOrZeroExtend(MaxIter, WiderType);
const SCEV *WideExitMax = SE->getNoopOrZeroExtend(ExitMax, WiderType);
const SCEV *WideMaxIter = SE->getNoopOrZeroExtend(MaxIter, WiderType);
if (WideExitMax == WideMaxIter)
ICmpsFailingOnLastIter.insert(ICmp);
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/LoopDeletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
if (!L->getLoopLatch())
return LoopDeletionResult::Unmodified;

auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
const SCEV *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
if (!BTCMax->isZero()) {
auto *BTC = SE.getBackedgeTakenCount(L);
const SCEV *BTC = SE.getBackedgeTakenCount(L);
if (!BTC->isZero()) {
if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))
return LoopDeletionResult::Unmodified;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Scalar/LoopPredication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ LoopPredication::widenICmpRangeCheck(ICmpInst *ICI, SCEVExpander &Expander,
LLVM_DEBUG(dbgs() << "Range check IV is not affine!\n");
return std::nullopt;
}
auto *Step = RangeCheckIV->getStepRecurrence(*SE);
const SCEV *Step = RangeCheckIV->getStepRecurrence(*SE);
// We cannot just compare with latch IV step because the latch and range IVs
// may have different types.
if (!isSupportedStep(Step)) {
Expand Down Expand Up @@ -845,7 +845,7 @@ std::optional<LoopICmp> LoopPredication::parseLoopLatchICmp() {
return std::nullopt;
}

auto *Step = Result->IV->getStepRecurrence(*SE);
const SCEV *Step = Result->IV->getStepRecurrence(*SE);
if (!isSupportedStep(Step)) {
LLVM_DEBUG(dbgs() << "Unsupported loop stride(" << *Step << ")!\n");
return std::nullopt;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/LowerMemIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,8 @@ static void createMemSetLoop(Instruction *InsertBefore, Value *DstAddr,
template <typename T>
static bool canOverlap(MemTransferBase<T> *Memcpy, ScalarEvolution *SE) {
if (SE) {
auto *SrcSCEV = SE->getSCEV(Memcpy->getRawSource());
auto *DestSCEV = SE->getSCEV(Memcpy->getRawDest());
const SCEV *SrcSCEV = SE->getSCEV(Memcpy->getRawSource());
const SCEV *DestSCEV = SE->getSCEV(Memcpy->getRawDest());
if (SE->isKnownPredicateAt(CmpInst::ICMP_NE, SrcSCEV, DestSCEV, Memcpy))
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand)
D = ConstantInt::get(UseInst->getContext(),
APInt::getOneBitSet(BitWidth, D->getZExtValue()));
}
const auto *LHS = SE->getSCEV(IVSrc);
const auto *RHS = SE->getSCEV(D);
const SCEV *LHS = SE->getSCEV(IVSrc);
const SCEV *RHS = SE->getSCEV(D);
FoldedExpr = SE->getUDivExpr(LHS, RHS);
// We might have 'exact' flag set at this point which will no longer be
// correct after we make the replacement.
Expand Down Expand Up @@ -297,8 +297,8 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,

bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
// Get the SCEVs for the ICmp operands.
auto *N = SE->getSCEV(SDiv->getOperand(0));
auto *D = SE->getSCEV(SDiv->getOperand(1));
const SCEV *N = SE->getSCEV(SDiv->getOperand(0));
const SCEV *D = SE->getSCEV(SDiv->getOperand(1));

// Simplify unnecessary loops away.
const Loop *L = LI->getLoopFor(SDiv->getParent());
Expand Down Expand Up @@ -397,7 +397,7 @@ void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem,
}

auto *T = Rem->getType();
const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
const SCEV *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
if (SE->isKnownPredicate(LT, NLessOne, D)) {
replaceRemWithNumeratorOrZero(Rem);
return;
Expand Down
9 changes: 5 additions & 4 deletions llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,15 @@ class SCEVAddRecForUniformityRewriter
// Build a new AddRec by multiplying the step by StepMultiplier and
// incrementing the start by Offset * step.
Type *Ty = Expr->getType();
auto *Step = Expr->getStepRecurrence(SE);
const SCEV *Step = Expr->getStepRecurrence(SE);
if (!SE.isLoopInvariant(Step, TheLoop)) {
CannotAnalyze = true;
return Expr;
}
auto *NewStep = SE.getMulExpr(Step, SE.getConstant(Ty, StepMultiplier));
auto *ScaledOffset = SE.getMulExpr(Step, SE.getConstant(Ty, Offset));
auto *NewStart = SE.getAddExpr(Expr->getStart(), ScaledOffset);
const SCEV *NewStep =
SE.getMulExpr(Step, SE.getConstant(Ty, StepMultiplier));
const SCEV *ScaledOffset = SE.getMulExpr(Step, SE.getConstant(Ty, Offset));
const SCEV *NewStart = SE.getAddExpr(Expr->getStart(), ScaledOffset);
return SE.getAddRecExpr(NewStart, NewStep, TheLoop, SCEV::FlagAnyWrap);
}

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19062,10 +19062,10 @@ bool SLPVectorizerPass::vectorizeGEPIndices(BasicBlock *BB, BoUpSLP &R) {
auto *GEPI = GEPList[I];
if (!Candidates.count(GEPI))
continue;
auto *SCEVI = SE->getSCEV(GEPList[I]);
const SCEV *SCEVI = SE->getSCEV(GEPList[I]);
for (int J = I + 1; J < E && Candidates.size() > 1; ++J) {
auto *GEPJ = GEPList[J];
auto *SCEVJ = SE->getSCEV(GEPList[J]);
const SCEV *SCEVJ = SE->getSCEV(GEPList[J]);
if (isa<SCEVConstant>(SE->getMinusSCEV(SCEVI, SCEVJ))) {
Candidates.remove(GEPI);
Candidates.remove(GEPJ);
Expand Down
Loading

0 comments on commit edf46f3

Please sign in to comment.