Skip to content

[SCEV] Use context sensitive reasoning in howFarToZero #94525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10485,16 +10485,19 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);

if (!isLoopInvariant(Step, L) || !isKnownNonZero(Step))
if (!isLoopInvariant(Step, L))
return getCouldNotCompute();

// Specialize step for this loop so we get context sensitive facts below.
const SCEV *StepWLG = applyLoopGuards(Step, L);

// For positive steps (counting up until unsigned overflow):
// N = -Start/Step (as unsigned)
// For negative steps (counting down to zero):
// N = Start/-Step
// First compute the unsigned distance from zero in the direction of Step.
bool CountDown = isKnownNegative(Step);
if (!CountDown && !isKnownNonNegative(Step))
bool CountDown = isKnownNegative(StepWLG);
if (!CountDown && !isKnownNonNegative(StepWLG))
return getCouldNotCompute();

const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
Expand Down Expand Up @@ -10533,6 +10536,13 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
// will have undefined behavior due to wrapping.
if (ControlsOnlyExit && AddRec->hasNoSelfWrap() &&
loopHasNoAbnormalExits(AddRec->getLoop())) {

// If the stride is zero, the loop must be infinite. In C++, most loops
// are finite by assumption, in which case the step being zero implies
// UB must execute if the loop is entered.
if (!loopIsFiniteByAssumption(L) && !isKnownNonZero(StepWLG))
return getCouldNotCompute();

const SCEV *Exact =
getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
const SCEV *ConstantMax = getCouldNotCompute();
Expand All @@ -10547,7 +10557,7 @@ ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
}

// Solve the general equation.
if (!StepC)
if (!StepC || StepC->getValue()->isZero())
return getCouldNotCompute();
const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
getNegativeSCEV(Start), *this);
Expand Down
42 changes: 24 additions & 18 deletions llvm/test/Analysis/ScalarEvolution/trip-count-unknown-stride.ll
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ define void @ne_nsw_pos_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nsw_pos_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nsw_pos_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -1
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%pos_step = icmp sgt i32 %s, 0
Expand All @@ -299,9 +300,10 @@ define void @ne_nsw_neg_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nsw_neg_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nsw_neg_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %n) + %s) /u (-1 * %s))
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -2
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %n) + %s) /u (-1 * %s))
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%neg_step = icmp slt i32 %s, 0
Expand All @@ -327,9 +329,10 @@ define void @ne_nsw_nonneg_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nsw_nonneg_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nsw_nonneg_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -1
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%nonneg_step = icmp sge i32 %s, 0
Expand Down Expand Up @@ -381,9 +384,10 @@ define void @ne_nuw_pos_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nuw_pos_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nuw_pos_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -1
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%pos_step = icmp sgt i32 %s, 0
Expand All @@ -409,9 +413,10 @@ define void @ne_nuw_neg_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nuw_neg_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nuw_neg_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %n) + %s) /u (-1 * %s))
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -2
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %n) + %s) /u (-1 * %s))
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%neg_step = icmp slt i32 %s, 0
Expand All @@ -437,9 +442,10 @@ define void @ne_nuw_nonneg_step(ptr nocapture %A, i32 %n, i32 %s) mustprogress {
;
; CHECK-LABEL: 'ne_nuw_nonneg_step'
; CHECK-NEXT: Determining loop execution counts for: @ne_nuw_nonneg_step
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is i32 -1
; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (((-1 * %s) + %n) /u %s)
; CHECK-NEXT: Loop %for.body: Trip multiple is 1
;
entry:
%nonneg_step = icmp sge i32 %s, 0
Expand Down
11 changes: 1 addition & 10 deletions llvm/test/Transforms/LoopRotate/pr56260.ll
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,7 @@ define void @main() {
; CHECK-NEXT: [[TOBOOL3_NOT1:%.*]] = icmp eq i32 [[INC]], 0
; CHECK-NEXT: br i1 [[TOBOOL3_NOT1]], label [[L0_PREHEADER_LOOPEXIT]], label [[L1_PREHEADER_LR_PH:%.*]]
; CHECK: L1.preheader.lr.ph:
; CHECK-NEXT: br label [[L1_PREHEADER:%.*]]
; CHECK: L1.preheader:
; CHECK-NEXT: [[SPEC_SELECT3:%.*]] = phi i32 [ [[INC]], [[L1_PREHEADER_LR_PH]] ], [ [[SPEC_SELECT:%.*]], [[L0_LATCH:%.*]] ]
; CHECK-NEXT: [[K_02:%.*]] = phi i32 [ 0, [[L1_PREHEADER_LR_PH]] ], [ [[SPEC_SELECT3]], [[L0_LATCH]] ]
; CHECK-NEXT: [[TOBOOL8_NOT:%.*]] = icmp eq i32 [[K_02]], 0
; CHECK-NEXT: br label [[L0_LATCH]]
; CHECK: L0.latch:
; CHECK-NEXT: [[SPEC_SELECT]] = add nsw i32 [[SPEC_SELECT3]], [[INC]]
; CHECK-NEXT: [[TOBOOL3_NOT:%.*]] = icmp eq i32 [[SPEC_SELECT]], 0
; CHECK-NEXT: br i1 [[TOBOOL3_NOT]], label [[L0_L0_PREHEADER_LOOPEXIT_CRIT_EDGE:%.*]], label [[L1_PREHEADER]]
; CHECK-NEXT: br label [[L0_L0_PREHEADER_LOOPEXIT_CRIT_EDGE:%.*]]
;
entry:
br label %L0.preheader
Expand Down
Loading