llvm.org GIT mirror llvm / 2d38e17
[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/trunk@309758 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
2 changed file(s) with 44 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
75967596 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
75977597 if (const SCEVConstant *BTCC =
75987598 dyn_cast(BackedgeTakenCount)) {
7599
7600 // This trivial case can show up in some degenerate cases where
7601 // the incoming IR has not yet been fully simplified.
7602 if (BTCC->getValue()->isZero()) {
7603 Value *InitValue = nullptr;
7604 bool MultipleInitValues = false;
7605 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
7606 if (!LI->contains(PN->getIncomingBlock(i))) {
7607 if (!InitValue)
7608 InitValue = PN->getIncomingValue(i);
7609 else if (InitValue != PN->getIncomingValue(i)) {
7610 MultipleInitValues = true;
7611 break;
7612 }
7613 }
7614 if (!MultipleInitValues && InitValue)
7615 return getSCEV(InitValue);
7616 }
7617 }
75997618 // Okay, we know how many times the containing loop executes. If
76007619 // this is a constant evolving PHI node, get the final value at
76017620 // the specified iteration number.
22
33 ; Check IndVarSimplify should not replace exit value because or else
44 ; udiv will be introduced by expand and the cost will be high.
5 ;
6 ; CHECK-LABEL: @_Z3fooPKcjj(
7 ; CHECK-NOT: udiv
85
96 declare void @_Z3mixRjj(i32* dereferenceable(4), i32)
107 declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
118 declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
129
1310 define i32 @_Z3fooPKcjj(i8* nocapture readonly %s, i32 %len, i32 %c) {
11 ; CHECK-LABEL: @_Z3fooPKcjj(
12 ; CHECK-NOT: udiv
1413 entry:
1514 %a = alloca i32, align 4
1615 %tmp = bitcast i32* %a to i8*
4948 call void @llvm.lifetime.end.p0i8(i64 4, i8* %tmp)
5049 ret i32 %tmp4
5150 }
51
52 define i32 @zero_backedge_count_test(i32 %unknown_init, i32* %unknown_mem) {
53 ; CHECK-LABEL: @zero_backedge_count_test(
54 entry:
55 br label %loop
56
57 loop:
58 %iv = phi i32 [ 0, %entry], [ %iv.inc, %loop ]
59 %unknown_phi = phi i32 [ %unknown_init, %entry ], [ %unknown_next, %loop ]
60 %iv.inc = add i32 %iv, 1
61 %be_taken = icmp ne i32 %iv.inc, 1
62 %unknown_next = load volatile i32, i32* %unknown_mem
63 br i1 %be_taken, label %loop, label %leave
64
65 leave:
66 ; We can fold %unknown_phi even though the backedge value for it is completely
67 ; unknown, since we can prove that the loop's backedge taken count is 0.
68
69 ; CHECK: leave:
70 ; CHECK: ret i32 %unknown_init
71 %exit_val = phi i32 [ %unknown_phi, %loop ]
72 ret i32 %exit_val
73 }