Skip to content

Commit 7aed53e

Browse files
authored
[ScalarEvolution] Handle addrec incoming value in isImpliedViaMerge() (#126236)
The code already guards against values coming from a previous iteration using properlyDominates(). However, addrecs are considered to properly dominate the loop they are defined in. Handle this special case separately, by checking for expressions that have computable loop evolution (this should cover cases like a zext of an addrec as well). I considered changing the definition of properlyDominates() instead, but decided against it. The current definition is useful in other context, e.g. when deciding whether an expression is safe to expand in a given block. Fixes #126012.
1 parent 52a02b6 commit 7aed53e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12400,6 +12400,12 @@ bool ScalarEvolution::isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS,
1240012400
// iteration of a loop.
1240112401
if (!properlyDominates(L, LBB))
1240212402
return false;
12403+
// Addrecs are considered to properly dominate their loop, so are missed
12404+
// by the previous check. Discard any values that have computable
12405+
// evolution in this loop.
12406+
if (auto *Loop = LI.getLoopFor(LBB))
12407+
if (hasComputableLoopEvolution(L, Loop))
12408+
return false;
1240312409
if (!ProvedEasily(L, RHS))
1240412410
return false;
1240512411
}

llvm/test/Transforms/IndVarSimplify/pr126012.ll

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=indvars < %s | FileCheck %s
33

4-
; FIXME: This is a miscompile.
4+
; Do not infer that %cmp is true. The %indvar3 input of %indvar2 comes from
5+
; a previous iteration, so we should not compare it to a value from the current
6+
; iteration.
57
define i32 @test() {
68
; CHECK-LABEL: define i32 @test() {
79
; CHECK-NEXT: [[ENTRY:.*]]:
810
; CHECK-NEXT: br label %[[FOR_PREHEADER:.*]]
911
; CHECK: [[FOR_PREHEADER]]:
1012
; CHECK-NEXT: [[INDVAR1:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[PHI:%.*]], %[[FOR_INC:.*]] ]
11-
; CHECK-NEXT: [[INDVAR3:%.*]] = phi i32 [ 0, %[[ENTRY]] ], [ [[INC:%.*]], %[[FOR_INC]] ]
13+
; CHECK-NEXT: [[INDVAR2:%.*]] = phi i32 [ 1, %[[ENTRY]] ], [ [[INDVAR3:%.*]], %[[FOR_INC]] ]
14+
; CHECK-NEXT: [[INDVAR3]] = phi i32 [ 0, %[[ENTRY]] ], [ [[INC:%.*]], %[[FOR_INC]] ]
1215
; CHECK-NEXT: [[COND1:%.*]] = icmp eq i32 [[INDVAR3]], 0
1316
; CHECK-NEXT: br i1 [[COND1]], label %[[FOR_INC]], label %[[FOR_END:.*]]
1417
; CHECK: [[FOR_END]]:
15-
; CHECK-NEXT: [[EXT:%.*]] = zext i1 true to i32
18+
; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[INDVAR2]], 0
19+
; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[CMP]] to i32
1620
; CHECK-NEXT: br label %[[FOR_INC]]
1721
; CHECK: [[FOR_INC]]:
1822
; CHECK-NEXT: [[PHI]] = phi i32 [ [[EXT]], %[[FOR_END]] ], [ 0, %[[FOR_PREHEADER]] ]

0 commit comments

Comments
 (0)