Skip to content

Commit e4d1d0c

Browse files
committed
[SCEV] Fix isImpliedViaMerge() with values from previous iteration (PR56242)
When trying to prove an implied condition on a phi by proving it for all incoming values, we need to be careful about values coming from a backedge, as these may refer to a previous loop iteration. A variant of this issue was fixed in D101829, but the dominance condition used there isn't quite right: It checks that the value dominates the incoming block, which doesn't exclude backedges (values defined in a loop will usually dominate the loop latch, which is the incoming block of the backedge). Instead, we should be checking for domination of the phi block. Any values defined inside the loop will not dominate the loop header phi. Fixes #56242. Differential Revision: https://reviews.llvm.org/D128640
1 parent 6e2058e commit e4d1d0c

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11812,7 +11812,7 @@ bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred,
1181211812
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
1181311813
// Make sure L does not refer to a value from a potentially previous
1181411814
// iteration of a loop.
11815-
if (!properlyDominates(L, IncBB))
11815+
if (!properlyDominates(L, LBB))
1181611816
return false;
1181711817
if (!ProvedEasily(L, RHS))
1181811818
return false;

llvm/test/Transforms/IRCE/decrementing-loop.ll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,16 @@ exit:
210210
ret void
211211
}
212212

213-
; TODO: we need to be more careful when trying to look through phi nodes in
214-
; cycles, because the condition to prove may reference the previous value of
215-
; the phi. So we currently fail to optimize this case.
216213
; Check that we can figure out that IV is non-negative via implication through
217214
; two Phi nodes, one being AddRec.
218215
define void @test_05(i32* %a, i32* %a_len_ptr, i1 %cond) {
219216

220217
; CHECK-LABEL: test_05
221-
; CHECK: entry:
222-
; CHECK: br label %merge
223-
; CHECK-NOT: mainloop
218+
; CHECK: mainloop:
219+
; CHECK-NEXT: br label %loop
220+
; CHECK: loop:
221+
; CHECK: br i1 true, label %in.bounds, label %out.of.bounds
222+
; CHECK: loop.preloop:
224223

225224
entry:
226225
%len.a = load i32, i32* %a_len_ptr, !range !0

llvm/test/Transforms/IndVarSimplify/pr56242.ll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ define void @test(ptr %arr) {
99
; CHECK-NEXT: br label [[LOOP_HEADER:%.*]]
1010
; CHECK: loop.header:
1111
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[IV_INC:%.*]], [[LOOP_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
12+
; CHECK-NEXT: [[PREV:%.*]] = phi i32 [ [[V:%.*]], [[LOOP_LATCH]] ], [ 0, [[ENTRY]] ]
1213
; CHECK-NEXT: [[PTR:%.*]] = getelementptr inbounds i32, ptr [[ARR:%.*]], i64 [[IV]]
13-
; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[PTR]], align 4
14+
; CHECK-NEXT: [[V]] = load i32, ptr [[PTR]], align 4
1415
; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[V]], 0
1516
; CHECK-NEXT: br i1 [[CMP1]], label [[IF:%.*]], label [[LOOP_LATCH]]
1617
; CHECK: if:
17-
; CHECK-NEXT: call void @use(i1 false)
18+
; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i32 [[PREV]], 0
19+
; CHECK-NEXT: call void @use(i1 [[CMP2]])
1820
; CHECK-NEXT: br label [[LOOP_LATCH]]
1921
; CHECK: loop.latch:
2022
; CHECK-NEXT: [[IV_INC]] = add nuw nsw i64 [[IV]], 1

0 commit comments

Comments
 (0)