Skip to content

Commit af970cd

Browse files
nikictstellar
authored andcommitted
[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. (cherry picked from commit 7aed53e)
1 parent 9bbf3a9 commit af970cd

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
@@ -12402,6 +12402,12 @@ bool ScalarEvolution::isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS,
1240212402
// iteration of a loop.
1240312403
if (!properlyDominates(L, LBB))
1240412404
return false;
12405+
// Addrecs are considered to properly dominate their loop, so are missed
12406+
// by the previous check. Discard any values that have computable
12407+
// evolution in this loop.
12408+
if (auto *Loop = LI.getLoopFor(LBB))
12409+
if (hasComputableLoopEvolution(L, Loop))
12410+
return false;
1240512411
if (!ProvedEasily(L, RHS))
1240612412
return false;
1240712413
}

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)