Skip to content

Commit 6c99e63

Browse files
committed
[SCEV] By more careful when traversing phis in isImpliedViaMerge.
I think currently isImpliedViaMerge can incorrectly return true for phis in a loop/cycle, if the found condition involves the previous value of Consider the case in exit_cond_depends_on_inner_loop. At some point, we call (modulo simplifications) isImpliedViaMerge(<=, %x.lcssa, -1, %call, -1). The existing code tries to prove IncV <= -1 for all incoming values InvV using the found condition (%call <= -1). At the moment this succeeds, but only because it does not compare the same runtime value. The found condition checks the value of the last iteration, but the incoming value is from the *previous* iteration. Hence we incorrectly determine that the *previous* value was <= -1, which may not be true. I think we need to be more careful when looking at the incoming values here. In particular, we need to rule out that a found condition refers to any value that may refer to one of the previous iterations. I'm not sure there's a reliable way to do so (that also works of irreducible control flow). So for now this patch adds an additional requirement that the incoming value must properly dominate the phi block. This should ensure the values do not change in a cycle. I am not entirely sure if will catch all cases and I appreciate a through second look in that regard. Alternatively we could also unconditionally bail out in this case, instead of checking the incoming values Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D101829
1 parent 1e9c39a commit 6c99e63

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10785,6 +10785,10 @@ bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred,
1078510785
if (!dominates(RHS, IncBB))
1078610786
return false;
1078710787
const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
10788+
// Make sure L does not refer to a value from a potentially previous
10789+
// iteration of a loop.
10790+
if (!properlyDominates(L, IncBB))
10791+
return false;
1078810792
if (!ProvedEasily(L, RHS))
1078910793
return false;
1079010794
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,17 @@ 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.
213216
; Check that we can figure out that IV is non-negative via implication through
214217
; two Phi nodes, one being AddRec.
215218
define void @test_05(i32* %a, i32* %a_len_ptr, i1 %cond) {
216219

217220
; CHECK-LABEL: test_05
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:
221+
; CHECK: entry:
222+
; CHECK: br label %merge
223+
; CHECK-NOT: mainloop
223224

224225
entry:
225226
%len.a = load i32, i32* %a_len_ptr, !range !0

llvm/test/Transforms/IndVarSimplify/eliminate-exit.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,8 @@ define i32 @exit_cond_depends_on_inner_loop() {
453453
; CHECK-NEXT: br i1 [[INNER_COND]], label [[INNER]], label [[OUTER_EXITING_1:%.*]]
454454
; CHECK: outer.exiting.1:
455455
; CHECK-NEXT: [[X_LCSSA:%.*]] = phi i32 [ [[X]], [[INNER]] ]
456-
; CHECK-NEXT: br i1 false, label [[EXIT:%.*]], label [[OUTER_LATCH]]
456+
; CHECK-NEXT: [[OUTER_COND_1:%.*]] = icmp sgt i32 [[X_LCSSA]], -1
457+
; CHECK-NEXT: br i1 [[OUTER_COND_1]], label [[EXIT:%.*]], label [[OUTER_LATCH]]
457458
; CHECK: outer.latch:
458459
; CHECK-NEXT: [[IV_OUTER_NEXT]] = add nuw nsw i32 [[IV_OUTER]], 1
459460
; CHECK-NEXT: [[OUTER_COND_2:%.*]] = icmp ult i32 [[IV_OUTER]], 100

0 commit comments

Comments
 (0)