Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit aab7f85

Browse files
committed
Merging r309758:
------------------------------------------------------------------------ r309758 | sanjoy | 2017-08-01 15:37:58 -0700 (Tue, 01 Aug 2017) | 6 lines [SCEV/IndVars] Always compute loop exiting values if the backedge count is 0 If SCEV can prove that the backedge taken count for a loop is zero, it does not need to "understand" a recursive PHI to compute its exiting value. This should fix PR33885. ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_50@310538 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 2a35d3b commit aab7f85

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

lib/Analysis/ScalarEvolution.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7582,6 +7582,25 @@ const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
75827582
const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
75837583
if (const SCEVConstant *BTCC =
75847584
dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
7585+
7586+
// This trivial case can show up in some degenerate cases where
7587+
// the incoming IR has not yet been fully simplified.
7588+
if (BTCC->getValue()->isZero()) {
7589+
Value *InitValue = nullptr;
7590+
bool MultipleInitValues = false;
7591+
for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
7592+
if (!LI->contains(PN->getIncomingBlock(i))) {
7593+
if (!InitValue)
7594+
InitValue = PN->getIncomingValue(i);
7595+
else if (InitValue != PN->getIncomingValue(i)) {
7596+
MultipleInitValues = true;
7597+
break;
7598+
}
7599+
}
7600+
if (!MultipleInitValues && InitValue)
7601+
return getSCEV(InitValue);
7602+
}
7603+
}
75857604
// Okay, we know how many times the containing loop executes. If
75867605
// this is a constant evolving PHI node, get the final value at
75877606
// the specified iteration number.

test/Transforms/IndVarSimplify/exit_value_test2.ll

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
; Check IndVarSimplify should not replace exit value because or else
55
; udiv will be introduced by expand and the cost will be high.
6-
;
7-
; CHECK-LABEL: @_Z3fooPKcjj(
8-
; CHECK-NOT: udiv
96

107
declare void @_Z3mixRjj(i32* dereferenceable(4), i32)
118
declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
129
declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
1310

1411
define i32 @_Z3fooPKcjj(i8* nocapture readonly %s, i32 %len, i32 %c) {
12+
; CHECK-LABEL: @_Z3fooPKcjj(
13+
; CHECK-NOT: udiv
1514
entry:
1615
%a = alloca i32, align 4
1716
%tmp = bitcast i32* %a to i8*
@@ -50,3 +49,26 @@ while.end: ; preds = %while.cond.while.en
5049
call void @llvm.lifetime.end.p0i8(i64 4, i8* %tmp)
5150
ret i32 %tmp4
5251
}
52+
53+
define i32 @zero_backedge_count_test(i32 %unknown_init, i32* %unknown_mem) {
54+
; CHECK-LABEL: @zero_backedge_count_test(
55+
entry:
56+
br label %loop
57+
58+
loop:
59+
%iv = phi i32 [ 0, %entry], [ %iv.inc, %loop ]
60+
%unknown_phi = phi i32 [ %unknown_init, %entry ], [ %unknown_next, %loop ]
61+
%iv.inc = add i32 %iv, 1
62+
%be_taken = icmp ne i32 %iv.inc, 1
63+
%unknown_next = load volatile i32, i32* %unknown_mem
64+
br i1 %be_taken, label %loop, label %leave
65+
66+
leave:
67+
; We can fold %unknown_phi even though the backedge value for it is completely
68+
; unknown, since we can prove that the loop's backedge taken count is 0.
69+
70+
; CHECK: leave:
71+
; CHECK: ret i32 %unknown_init
72+
%exit_val = phi i32 [ %unknown_phi, %loop ]
73+
ret i32 %exit_val
74+
}

0 commit comments

Comments
 (0)