[PATCH] D128640: [SCEV] Fix isImpliedViaMerge() with values from previous iteration (PR56242)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 27 06:15:23 PDT 2022


nikic created this revision.
nikic added reviewers: fhahn, reames, mkazantsev.
Herald added a subscriber: hiraditya.
Herald added a project: All.
nikic requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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 <https://reviews.llvm.org/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 header phi.

Fixes https://github.com/llvm/llvm-project/issues/56242.


https://reviews.llvm.org/D128640

Files:
  llvm/lib/Analysis/ScalarEvolution.cpp
  llvm/test/Transforms/IRCE/decrementing-loop.ll
  llvm/test/Transforms/IndVarSimplify/pr56242.ll


Index: llvm/test/Transforms/IndVarSimplify/pr56242.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/pr56242.ll
+++ llvm/test/Transforms/IndVarSimplify/pr56242.ll
@@ -9,12 +9,14 @@
 ; CHECK-NEXT:    br label [[LOOP_HEADER:%.*]]
 ; CHECK:       loop.header:
 ; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ [[IV_INC:%.*]], [[LOOP_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    [[PREV:%.*]] = phi i32 [ [[V:%.*]], [[LOOP_LATCH]] ], [ 0, [[ENTRY]] ]
 ; CHECK-NEXT:    [[PTR:%.*]] = getelementptr inbounds i32, ptr [[ARR:%.*]], i64 [[IV]]
-; CHECK-NEXT:    [[V:%.*]] = load i32, ptr [[PTR]], align 4
+; CHECK-NEXT:    [[V]] = load i32, ptr [[PTR]], align 4
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp sgt i32 [[V]], 0
 ; CHECK-NEXT:    br i1 [[CMP1]], label [[IF:%.*]], label [[LOOP_LATCH]]
 ; CHECK:       if:
-; CHECK-NEXT:    call void @use(i1 false)
+; CHECK-NEXT:    [[CMP2:%.*]] = icmp slt i32 [[PREV]], 0
+; CHECK-NEXT:    call void @use(i1 [[CMP2]])
 ; CHECK-NEXT:    br label [[LOOP_LATCH]]
 ; CHECK:       loop.latch:
 ; CHECK-NEXT:    [[IV_INC]] = add nuw nsw i64 [[IV]], 1
Index: llvm/test/Transforms/IRCE/decrementing-loop.ll
===================================================================
--- llvm/test/Transforms/IRCE/decrementing-loop.ll
+++ llvm/test/Transforms/IRCE/decrementing-loop.ll
@@ -210,17 +210,16 @@
   ret void
 }
 
-; TODO: we need to be more careful when trying to look through phi nodes in
-; cycles, because the condition to prove may reference the previous value of
-; the phi. So we currently fail to optimize this case.
 ; Check that we can figure out that IV is non-negative via implication through
 ; two Phi nodes, one being AddRec.
 define void @test_05(i32* %a, i32* %a_len_ptr, i1 %cond) {
 
 ; CHECK-LABEL: test_05
-; CHECK: entry:
-; CHECK:   br label %merge
-; CHECK-NOT: mainloop
+; CHECK:       mainloop:
+; CHECK-NEXT:    br label %loop
+; CHECK:       loop:
+; CHECK:         br i1 true, label %in.bounds, label %out.of.bounds
+; CHECK:       loop.preloop:
 
  entry:
   %len.a = load i32, i32* %a_len_ptr, !range !0
Index: llvm/lib/Analysis/ScalarEvolution.cpp
===================================================================
--- llvm/lib/Analysis/ScalarEvolution.cpp
+++ llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11647,7 +11647,7 @@
       const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
       // Make sure L does not refer to a value from a potentially previous
       // iteration of a loop.
-      if (!properlyDominates(L, IncBB))
+      if (!properlyDominates(L, LBB))
         return false;
       if (!ProvedEasily(L, RHS))
         return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128640.440191.patch
Type: text/x-patch
Size: 2678 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220627/11a8cbe1/attachment.bin>


More information about the llvm-commits mailing list