Skip to content

Commit 523573e

Browse files
committed
[LoopDeletion] Revert 3af8a11 and add test coverage for breakage
This reverts 3af8a11 because I'd used an upper bound where an lower bound was required. The included reduced test case demonstrates the issue.
1 parent f40a579 commit 523573e

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

llvm/lib/Transforms/Scalar/LoopDeletion.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -407,25 +407,18 @@ breakBackedgeIfNotTaken(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
407407
if (!L->getLoopLatch())
408408
return LoopDeletionResult::Unmodified;
409409

410-
auto *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
411-
if (BTC->isZero()) {
412-
// SCEV knows this backedge isn't taken!
413-
breakLoopBackedge(L, DT, SE, LI, MSSA);
414-
++NumBackedgesBroken;
415-
return LoopDeletionResult::Deleted;
416-
}
417-
418-
// If SCEV leaves open the possibility of a zero trip count, see if
419-
// symbolically evaluating the first iteration lets us prove the backedge
420-
// unreachable.
421-
if (isa<SCEVCouldNotCompute>(BTC) || !SE.isKnownNonZero(BTC))
422-
if (canProveExitOnFirstIteration(L, DT, LI)) {
423-
breakLoopBackedge(L, DT, SE, LI, MSSA);
424-
++NumBackedgesBroken;
425-
return LoopDeletionResult::Deleted;
410+
auto *BTCMax = SE.getConstantMaxBackedgeTakenCount(L);
411+
if (!BTCMax->isZero()) {
412+
auto *BTC = SE.getBackedgeTakenCount(L);
413+
if (!BTC->isZero()) {
414+
if (!isa<SCEVCouldNotCompute>(BTC) && SE.isKnownNonZero(BTC))
415+
return LoopDeletionResult::Unmodified;
416+
if (!canProveExitOnFirstIteration(L, DT, LI))
417+
return LoopDeletionResult::Unmodified;
426418
}
427-
428-
return LoopDeletionResult::Unmodified;
419+
}
420+
breakLoopBackedge(L, DT, SE, LI, MSSA);
421+
return LoopDeletionResult::Deleted;
429422
}
430423

431424
/// Remove a loop if it is dead.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -loop-deletion -S < %s | FileCheck %s
3+
4+
@a = global i1 false, align 4
5+
6+
define i32 @main() {
7+
; CHECK-LABEL: @main(
8+
; CHECK-NEXT: entry:
9+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
10+
; CHECK: for.body:
11+
; CHECK-NEXT: [[D_015:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ]
12+
; CHECK-NEXT: call void @foo()
13+
; CHECK-NEXT: [[DOTB:%.*]] = load i1, i1* @a, align 4
14+
; CHECK-NEXT: [[CONV1:%.*]] = select i1 [[DOTB]], i32 -128, i32 1
15+
; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp ugt i32 [[CONV1]], [[D_015]]
16+
; CHECK-NEXT: br i1 [[CMP2_NOT]], label [[CLEANUP:%.*]], label [[FOR_INC8:%.*]]
17+
; CHECK: for.inc8:
18+
; CHECK-NEXT: [[INC9:%.*]] = add nuw nsw i32 [[D_015]], 1
19+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC9]], 19
20+
; CHECK-NEXT: br label [[CLEANUP]]
21+
; CHECK: cleanup:
22+
; CHECK-NEXT: store i1 true, i1* @a, align 4
23+
; CHECK-NEXT: ret i32 0
24+
;
25+
entry:
26+
br label %for.body
27+
28+
for.body:
29+
%d.015 = phi i32 [ 0, %entry ], [ %inc9, %for.inc8 ]
30+
call void @foo()
31+
%.b = load i1, i1* @a, align 4
32+
%conv1 = select i1 %.b, i32 -128, i32 1
33+
%cmp2.not = icmp ugt i32 %conv1, %d.015
34+
br i1 %cmp2.not, label %cleanup, label %for.inc8
35+
36+
for.inc8:
37+
%inc9 = add nuw nsw i32 %d.015, 1
38+
%exitcond.not = icmp eq i32 %inc9, 19
39+
br i1 %exitcond.not, label %cleanup, label %for.body
40+
41+
cleanup:
42+
store i1 true, i1* @a, align 4
43+
ret i32 0
44+
}
45+
46+
declare void @foo()
47+

0 commit comments

Comments
 (0)