1; PR23538
2; RUN: opt < %s -indvars -loop-deletion -S | FileCheck %s
3
4; Check IndVarSimplify should not replace exit value because or else
5; udiv will be introduced by expand and the cost will be high.
6
7declare void @_Z3mixRjj(i32* dereferenceable(4), i32)
8declare void @llvm.lifetime.start.p0i8(i64, i8* nocapture)
9declare void @llvm.lifetime.end.p0i8(i64, i8* nocapture)
10
11define i32 @_Z3fooPKcjj(i8* nocapture readonly %s, i32 %len, i32 %c) {
12; CHECK-LABEL: @_Z3fooPKcjj(
13; CHECK-NOT: udiv
14entry:
15  %a = alloca i32, align 4
16  %tmp = bitcast i32* %a to i8*
17  call void @llvm.lifetime.start.p0i8(i64 4, i8* %tmp)
18  store i32 -1640531527, i32* %a, align 4
19  %cmp8 = icmp ugt i32 %len, 11
20  br i1 %cmp8, label %while.body.lr.ph, label %while.end
21
22while.body.lr.ph:                                 ; preds = %entry
23  br label %while.body
24
25while.body:                                       ; preds = %while.body, %while.body.lr.ph
26  %keylen.010 = phi i32 [ %len, %while.body.lr.ph ], [ %sub, %while.body ]
27  %s.addr.09 = phi i8* [ %s, %while.body.lr.ph ], [ %add.ptr, %while.body ]
28  %tmp1 = bitcast i8* %s.addr.09 to i32*
29  %tmp2 = load i32, i32* %tmp1, align 4
30  %shl.i = shl i32 %tmp2, 1
31  %and.i = and i32 %shl.i, 16843008
32  %tmp3 = load i32, i32* %a, align 4
33  %sub.i = add i32 %tmp3, %tmp2
34  %add = sub i32 %sub.i, %and.i
35  store i32 %add, i32* %a, align 4
36  %add.ptr = getelementptr inbounds i8, i8* %s.addr.09, i64 12
37  %sub = add i32 %keylen.010, -12
38  %cmp = icmp ugt i32 %sub, 11
39  br i1 %cmp, label %while.body, label %while.cond.while.end_crit_edge
40
41while.cond.while.end_crit_edge:                   ; preds = %while.body
42  %sub.lcssa = phi i32 [ %sub, %while.body ]
43  br label %while.end
44
45while.end:                                        ; preds = %while.cond.while.end_crit_edge, %entry
46  %keylen.0.lcssa = phi i32 [ %sub.lcssa, %while.cond.while.end_crit_edge ], [ %len, %entry ]
47  call void @_Z3mixRjj(i32* dereferenceable(4) %a, i32 %keylen.0.lcssa)
48  %tmp4 = load i32, i32* %a, align 4
49  call void @llvm.lifetime.end.p0i8(i64 4, i8* %tmp)
50  ret i32 %tmp4
51}
52
53define i32 @zero_backedge_count_test(i32 %unknown_init, i32* %unknown_mem) {
54; CHECK-LABEL: @zero_backedge_count_test(
55entry:
56  br label %loop
57
58loop:
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
66leave:
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}
75