[SCEV] Move mustprogress based no-self-wrap logic so it applies to all exit conditions

This change moves logic which we'd added specifically for less than tests so that it applies to equalities and greater than tests as well. The basic idea is that if we can show an IV cycles infinitely through the same series on self-wrap, and that the exit condition must be taken to prevent UB, we can conclude that it must be taken before self-wrap and thus infer said flag.

The motivation here is simple loops with unsigned induction variables w/non-one steps and inequality tests. A toy example would be:
for (unsigned i = 0; i != N; i += 2) { body; }

If body contains no side effects, and this is a mustprogress function, we can assume that this must be a finite loop and thus that the exit count is N/2.

Differential Revision: https://reviews.llvm.org/D103991
This commit is contained in:
Philip Reames
2021-11-18 10:05:04 -08:00
parent 7a14244cc6
commit ea12c2cb9c
3 changed files with 55 additions and 24 deletions

View File

@@ -8316,6 +8316,32 @@ ScalarEvolution::computeExitLimitFromICmp(const Loop *L,
if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
}
// If this loop must exit based on this condition (or execute undefined
// behaviour), and we can prove the test sequence produced must repeat
// the same values on self-wrap of the IV, then we can infer that IV
// doesn't self wrap because if it did, we'd have an infinite (undefined)
// loop.
if (ControlsExit && isLoopInvariant(RHS, L) && loopHasNoAbnormalExits(L) &&
loopIsFiniteByAssumption(L)) {
// TODO: We can peel off any functions which are invertible *in L*. Loop
// invariant terms are effectively constants for our purposes here.
auto *InnerLHS = LHS;
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
InnerLHS = ZExt->getOperand();
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS)) {
auto *StrideC = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this));
if (!AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
StrideC && StrideC->getAPInt().isPowerOf2()) {
auto Flags = AR->getNoWrapFlags();
Flags = setFlags(Flags, SCEV::FlagNW);
SmallVector<const SCEV*> Operands{AR->operands()};
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
}
}
}
switch (Pred) {
case ICmpInst::ICMP_NE: { // while (X != Y)
// Convert to: while (X-Y != 0)
@@ -11821,14 +11847,6 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS)) {
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(ZExt->getOperand());
if (AR && AR->getLoop() == L && AR->isAffine()) {
auto Flags = AR->getNoWrapFlags();
if (!hasFlags(Flags, SCEV::FlagNW) && canAssumeNoSelfWrap(AR)) {
Flags = setFlags(Flags, SCEV::FlagNW);
SmallVector<const SCEV*> Operands{AR->operands()};
Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
}
auto canProveNUW = [&]() {
if (!isLoopInvariant(RHS, L))
return false;
@@ -11851,6 +11869,7 @@ ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
Limit = Limit.zext(OuterBitWidth);
return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit);
};
auto Flags = AR->getNoWrapFlags();
if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW())
Flags = setFlags(Flags, SCEV::FlagNUW);

View File

@@ -10,9 +10,11 @@ target triple = "x86_64-unknown-linux-gnu"
define void @test(i32 %N) mustprogress {
; CHECK-LABEL: 'test'
; CHECK-NEXT: Determining loop execution counts for: @test
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is ((-2 + %N) /u 2)
; CHECK-NEXT: Loop %for.body: max backedge-taken count is 2147483647
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((-2 + %N) /u 2)
; CHECK-NEXT: Predicates:
; CHECK: Loop %for.body: Trip multiple is 1
;
entry:
br label %for.body
@@ -30,9 +32,11 @@ for.cond.cleanup:
define void @test_preinc(i32 %N) mustprogress {
; CHECK-LABEL: 'test_preinc'
; CHECK-NEXT: Determining loop execution counts for: @test_preinc
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is (%N /u 2)
; CHECK-NEXT: Loop %for.body: max backedge-taken count is 2147483647
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (%N /u 2)
; CHECK-NEXT: Predicates:
; CHECK: Loop %for.body: Trip multiple is 1
;
entry:
br label %for.body
@@ -117,9 +121,11 @@ for.cond.cleanup:
define void @test_1024(i32 %N) mustprogress {
; CHECK-LABEL: 'test_1024'
; CHECK-NEXT: Determining loop execution counts for: @test_1024
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is ((-1024 + %N) /u 1024)
; CHECK-NEXT: Loop %for.body: max backedge-taken count is 4194303
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((-1024 + %N) /u 1024)
; CHECK-NEXT: Predicates:
; CHECK: Loop %for.body: Trip multiple is 1
;
entry:
br label %for.body
@@ -232,7 +238,9 @@ define void @test_zext(i64 %N) mustprogress {
; CHECK-NEXT: Determining loop execution counts for: @test_zext
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (%N /u 2)
; CHECK-NEXT: Predicates:
; CHECK-NEXT: {0,+,2}<nuw><%for.body> Added Flags: <nusw>
;
entry:
br label %for.body

View File

@@ -56,9 +56,11 @@ for.end: ; preds = %for.body, %entry
define void @ult_infinite_ub() mustprogress {
; CHECK-LABEL: 'ult_infinite_ub'
; CHECK-NEXT: Determining loop execution counts for: @ult_infinite_ub
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is 1
; CHECK-NEXT: Loop %for.body: max backedge-taken count is 1
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is 1
; CHECK-NEXT: Predicates:
; CHECK: Loop %for.body: Trip multiple is 2
;
entry:
br label %for.body
@@ -340,9 +342,11 @@ for.end: ; preds = %for.body, %entry
define void @slt_infinite_ub() mustprogress {
; CHECK-LABEL: 'slt_infinite_ub'
; CHECK-NEXT: Determining loop execution counts for: @slt_infinite_ub
; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable max backedge-taken count.
; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
; CHECK-NEXT: Loop %for.body: backedge-taken count is 0
; CHECK-NEXT: Loop %for.body: max backedge-taken count is 0
; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is 0
; CHECK-NEXT: Predicates:
; CHECK: Loop %for.body: Trip multiple is 1
;
entry:
br label %for.body