Skip to content

Commit

Permalink
[LoopDeletion] Consider infinite loops alive, unless mustprogress.
Browse files Browse the repository at this point in the history
The current loop or any of its sub-loops may be infinite. Unless the
function or the loops are marked as mustprogress, this in itself makes
the loop *not* dead.

This patch moves the logic to check whether the current loop is finite
or mustprogress to `isLoopDead` and also extends it to check the
sub-loops. This should fix PR50511.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D103382

(cherry picked from commit 1b84acb)
  • Loading branch information
fhahn authored and nagisa committed Jun 6, 2021
1 parent 687621e commit 9e3de94
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 19 deletions.
35 changes: 24 additions & 11 deletions llvm/lib/Transforms/Scalar/LoopDeletion.cpp
Expand Up @@ -100,6 +100,30 @@ static bool isLoopDead(Loop *L, ScalarEvolution &SE,
return I.mayHaveSideEffects() && !I.isDroppable();
}))
return false;

// The loop or any of its sub-loops looping infinitely is legal. The loop can
// only be considered dead if either
// a. the function is mustprogress.
// b. all (sub-)loops are mustprogress or have a known trip-count.
if (L->getHeader()->getParent()->mustProgress())
return true;

SmallVector<Loop *, 8> WorkList;
WorkList.push_back(L);
while (!WorkList.empty()) {
Loop *Current = WorkList.pop_back_val();
if (hasMustProgress(Current))
continue;

const SCEV *S = SE.getConstantMaxBackedgeTakenCount(Current);
if (isa<SCEVCouldNotCompute>(S)) {
LLVM_DEBUG(
dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was "
"not required to make progress.\n");
return false;
}
WorkList.append(Current->begin(), Current->end());
}
return true;
}

Expand Down Expand Up @@ -230,17 +254,6 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
: LoopDeletionResult::Unmodified;
}

// Don't remove loops for which we can't solve the trip count unless the loop
// was required to make progress but has been determined to be dead.
const SCEV *S = SE.getConstantMaxBackedgeTakenCount(L);
if (isa<SCEVCouldNotCompute>(S) &&
!L->getHeader()->getParent()->mustProgress() && !hasMustProgress(L)) {
LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was "
"not required to make progress.\n");
return Changed ? LoopDeletionResult::Modified
: LoopDeletionResult::Unmodified;
}

LLVM_DEBUG(dbgs() << "Loop is invariant, delete it!");
ORE.emit([&]() {
return OptimizationRemark(DEBUG_TYPE, "Invariant", L->getStartLoc(),
Expand Down
52 changes: 49 additions & 3 deletions llvm/test/Transforms/LoopDeletion/noop-loops-with-subloops.ll
Expand Up @@ -158,7 +158,19 @@ exit:
; function/loop is mustprogress. Test case from PR50511.
define void @inner_loop_may_be_infinite(i1 %c1, i1 %c2) {
; CHECK-LABEL: @inner_loop_may_be_infinite(
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK-NEXT: br label [[LOOP1:%.*]]
; CHECK: loop1:
; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOOP1_LATCH:%.*]], label [[LOOP2_PREHEADER:%.*]]
; CHECK: loop2.preheader:
; CHECK-NEXT: br label [[LOOP2:%.*]]
; CHECK: loop2:
; CHECK-NEXT: br i1 [[C2:%.*]], label [[LOOP1_LATCH_LOOPEXIT:%.*]], label [[LOOP2]]
; CHECK: loop1.latch.loopexit:
; CHECK-NEXT: br label [[LOOP1_LATCH]]
; CHECK: loop1.latch:
; CHECK-NEXT: br i1 false, label [[LOOP1_LATCH_LOOP1_CRIT_EDGE:%.*]], label [[EXIT:%.*]]
; CHECK: loop1.latch.loop1_crit_edge:
; CHECK-NEXT: unreachable
; CHECK: exit:
; CHECK-NEXT: ret void
;
Expand Down Expand Up @@ -256,7 +268,21 @@ exit:
; mustprogress and can be removed.
define void @loop2_mustprogress_but_not_sibling_loop(i1 %c1, i1 %c2, i1 %c3) {
; CHECK-LABEL: @loop2_mustprogress_but_not_sibling_loop(
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK-NEXT: br label [[LOOP1:%.*]]
; CHECK: loop1:
; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOOP1_LATCH:%.*]], label [[LOOP2_PREHEADER:%.*]]
; CHECK: loop2.preheader:
; CHECK-NEXT: br label [[LOOP3_PREHEADER:%.*]]
; CHECK: loop3.preheader:
; CHECK-NEXT: br label [[LOOP3:%.*]]
; CHECK: loop3:
; CHECK-NEXT: br i1 [[C3:%.*]], label [[LOOP1_LATCH_LOOPEXIT:%.*]], label [[LOOP3]]
; CHECK: loop1.latch.loopexit:
; CHECK-NEXT: br label [[LOOP1_LATCH]]
; CHECK: loop1.latch:
; CHECK-NEXT: br i1 false, label [[LOOP1_LATCH_LOOP1_CRIT_EDGE:%.*]], label [[EXIT:%.*]]
; CHECK: loop1.latch.loop1_crit_edge:
; CHECK-NEXT: unreachable
; CHECK: exit:
; CHECK-NEXT: ret void
;
Expand All @@ -281,7 +307,27 @@ exit:
define void @loop2_finite_but_child_is_not(i1 %c1, i1 %c2, i1 %c3) {
; CHECK-LABEL: @loop2_finite_but_child_is_not(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK-NEXT: br label [[LOOP1:%.*]]
; CHECK: loop1:
; CHECK-NEXT: [[IV1:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV1_NEXT:%.*]], [[LOOP1_LATCH:%.*]] ]
; CHECK-NEXT: br i1 [[C1:%.*]], label [[LOOP1_LATCH]], label [[LOOP2_PREHEADER:%.*]]
; CHECK: loop2.preheader:
; CHECK-NEXT: br label [[LOOP2:%.*]]
; CHECK: loop2:
; CHECK-NEXT: [[IV:%.*]] = phi i32 [ [[IV_NEXT:%.*]], [[LOOP2_LATCH:%.*]] ], [ 0, [[LOOP2_PREHEADER]] ]
; CHECK-NEXT: br label [[LOOP3:%.*]]
; CHECK: loop3:
; CHECK-NEXT: br i1 [[C2:%.*]], label [[LOOP2_LATCH]], label [[LOOP3]]
; CHECK: loop2.latch:
; CHECK-NEXT: [[IV_NEXT]] = add nuw i32 [[IV]], 1
; CHECK-NEXT: [[C:%.*]] = icmp ugt i32 [[IV]], 200
; CHECK-NEXT: br i1 [[C]], label [[LOOP1_LATCH_LOOPEXIT:%.*]], label [[LOOP2]]
; CHECK: loop1.latch.loopexit:
; CHECK-NEXT: br label [[LOOP1_LATCH]]
; CHECK: loop1.latch:
; CHECK-NEXT: [[IV1_NEXT]] = add nuw i32 [[IV1]], 1
; CHECK-NEXT: [[C4:%.*]] = icmp ult i32 [[IV1_NEXT]], 200
; CHECK-NEXT: br i1 [[C4]], label [[LOOP1]], label [[EXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
Expand Down
25 changes: 20 additions & 5 deletions llvm/test/Transforms/LoopDeletion/unreachable-loops.ll
Expand Up @@ -245,11 +245,26 @@ exit:
; Delete a loop (L2) which has subloop (L3).
; Here we delete loop L2, but leave L3 as is.
define void @test9(i64 %n) {
; CHECK-LABEL: test9
; CHECK-LABEL: entry:
; CHECK-NEXT: br label %exit
; CHECK-LABEL: exit:
; CHECK-NEXT: ret void
; CHECK-LABEL: @test9(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[L1:%.*]]
; CHECK: L1.loopexit:
; CHECK-NEXT: br label [[L1_LOOPEXIT_SPLIT:%.*]]
; CHECK: L1.loopexit.split:
; CHECK-NEXT: unreachable
; CHECK: L1:
; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[L2_PREHEADER:%.*]]
; CHECK: L2.preheader:
; CHECK-NEXT: br label [[L3_PREHEADER:%.*]]
; CHECK: L3.preheader:
; CHECK-NEXT: [[Y_L2_LCSSA:%.*]] = phi i64 [ undef, [[L2_PREHEADER]] ]
; CHECK-NEXT: br label [[L3:%.*]]
; CHECK: L3:
; CHECK-NEXT: [[COND2:%.*]] = icmp slt i64 [[Y_L2_LCSSA]], [[N:%.*]]
; CHECK-NEXT: br i1 [[COND2]], label [[L3]], label [[L1_LOOPEXIT:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret void
;
; REMARKS-LABEL: Function: test9
; REMARKS: Loop deleted because it never executes
entry:
Expand Down

0 comments on commit 9e3de94

Please sign in to comment.