1; RUN: opt -S  -loop-reroll   %s | FileCheck %s
2declare i32 @goo(i32, i32)
3
4@buf = external global i8*
5@aaa = global [16 x i8] c"\01\02\03\04\05\06\07\08\09\0A\0B\0C\0D\0E\0F\10", align 1
6
7define i32 @test1(i32 %len) {
8entry:
9  br label %while.body
10
11while.body:
12;CHECK-LABEL: while.body:
13;CHECK-NEXT:    %indvar = phi i32 [ %indvar.next, %while.body ], [ 0, %entry ]
14;CHECK-NEXT:    %buf.021 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @aaa, i64 0, i64 0), %entry ], [ %add.ptr, %while.body ]
15;CHECK-NEXT:    %sum44.020 = phi i64 [ 0, %entry ], [ %add, %while.body ]
16;CHECK-NEXT:    [[T2:%[0-9]+]] = load i8, i8* %buf.021, align 1
17;CHECK-NEXT:    %conv = zext i8 [[T2]] to i64
18;CHECK-NEXT:    %add = add i64 %conv, %sum44.020
19;CHECK-NEXT:    %add.ptr = getelementptr inbounds i8, i8* %buf.021, i64 1
20;CHECK-NEXT:    %indvar.next = add i32 %indvar, 1
21;CHECK-NEXT:    %exitcond = icmp eq i32 %indvar, 1
22;CHECK-NEXT:    br i1 %exitcond, label %while.end, label %while.body
23
24  %dec22 = phi i32 [ 4, %entry ], [ %dec, %while.body ]
25  %buf.021 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @aaa, i64 0, i64 0), %entry ], [ %add.ptr, %while.body ]
26  %sum44.020 = phi i64 [ 0, %entry ], [ %add9, %while.body ]
27  %0 = load i8, i8* %buf.021, align 1
28  %conv = zext i8 %0 to i64
29  %add = add i64 %conv, %sum44.020
30  %arrayidx1 = getelementptr inbounds i8, i8* %buf.021, i64 1
31  %1 = load i8, i8* %arrayidx1, align 1
32  %conv2 = zext i8 %1 to i64
33  %add3 = add i64 %add, %conv2
34  %arrayidx4 = getelementptr inbounds i8, i8* %buf.021, i64 2
35  %2 = load i8, i8* %arrayidx4, align 1
36  %conv5 = zext i8 %2 to i64
37  %add6 = add i64 %add3, %conv5
38  %arrayidx7 = getelementptr inbounds i8, i8* %buf.021, i64 3
39  %3 = load i8, i8* %arrayidx7, align 1
40  %conv8 = zext i8 %3 to i64
41  %add9 = add i64 %add6, %conv8
42  %add.ptr = getelementptr inbounds i8, i8* %buf.021, i64 4
43  %dec = add nsw i32 %dec22, -1
44  %tobool = icmp eq i32 %dec, 0
45  br i1 %tobool, label %while.end, label %while.body
46
47while.end:                                        ; preds = %while.body
48  %conv11 = trunc i64 %add9 to i32
49  %call = tail call i32 @goo(i32 0, i32 %conv11)
50  unreachable
51}
52
53define i32 @test2(i32 %N, i32* nocapture readonly %a, i32 %S) {
54entry:
55  %cmp.9 = icmp sgt i32 %N, 0
56  br i1 %cmp.9, label %for.body.lr.ph, label %for.cond.cleanup
57
58for.body.lr.ph:
59  br label %for.body
60
61for.cond.for.cond.cleanup_crit_edge:
62  br label %for.cond.cleanup
63
64for.cond.cleanup:
65  %S.addr.0.lcssa = phi i32 [ %add2, %for.cond.for.cond.cleanup_crit_edge ], [ %S, %entry ]
66  ret i32 %S.addr.0.lcssa
67
68for.body:
69;CHECK-LABEL: for.body:
70;CHECK-NEXT:    %indvar = phi i32 [ %indvar.next, %for.body ], [ 0, %for.body.lr.ph ]
71;CHECK-NEXT:    %S.addr.011 = phi i32 [ %S, %for.body.lr.ph ], [ %add, %for.body ]
72;CHECK-NEXT:    %a.addr.010 = phi i32* [ %a, %for.body.lr.ph ], [ %incdec.ptr1, %for.body ]
73;CHECK-NEXT:    %4 = load i32, i32* %a.addr.010, align 4
74;CHECK-NEXT:    %add = add nsw i32 %4, %S.addr.011
75;CHECK-NEXT:    %incdec.ptr1 = getelementptr inbounds i32, i32* %a.addr.010, i64 1
76;CHECK-NEXT:    %indvar.next = add i32 %indvar, 1
77;CHECK-NEXT:    %exitcond = icmp eq i32 %indvar, %3
78;CHECK-NEXT:    br i1 %exitcond, label %for.cond.for.cond.cleanup_crit_edge, label %for.body
79
80  %i.012 = phi i32 [ 0, %for.body.lr.ph ], [ %add3, %for.body ]
81  %S.addr.011 = phi i32 [ %S, %for.body.lr.ph ], [ %add2, %for.body ]
82  %a.addr.010 = phi i32* [ %a, %for.body.lr.ph ], [ %incdec.ptr1, %for.body ]
83  %incdec.ptr = getelementptr inbounds i32, i32* %a.addr.010, i64 1
84  %0 = load i32, i32* %a.addr.010, align 4
85  %add = add nsw i32 %0, %S.addr.011
86  %incdec.ptr1 = getelementptr inbounds i32, i32* %a.addr.010, i64 2
87  %1 = load i32, i32* %incdec.ptr, align 4
88  %add2 = add nsw i32 %add, %1
89  %add3 = add nsw i32 %i.012, 2
90  %cmp = icmp slt i32 %add3, %N
91  br i1 %cmp, label %for.body, label %for.cond.for.cond.cleanup_crit_edge
92}
93
94define i32 @test3(i32* nocapture readonly %buf, i32 %len) #0 {
95entry:
96  %cmp10 = icmp sgt i32 %len, 1
97  br i1 %cmp10, label %while.body.preheader, label %while.end
98
99while.body.preheader:                             ; preds = %entry
100  br label %while.body
101
102while.body:                                       ; preds = %while.body.preheader, %while.body
103;CHECK-LABEL: while.body:
104;CHECK-NEXT:  %indvar = phi i32 [ %indvar.next, %while.body ], [ 0, %while.body.preheader ]
105;CHECK-NEXT:  %S.012 = phi i32 [ %add, %while.body ], [ undef, %while.body.preheader ]
106;CHECK-NEXT:  %buf.addr.011 = phi i32* [ %add.ptr, %while.body ], [ %buf, %while.body.preheader ]
107;CHECK-NEXT:  %4 = load i32, i32* %buf.addr.011, align 4
108;CHECK-NEXT:  %add = add nsw i32 %4, %S.012
109;CHECK-NEXT:  %add.ptr = getelementptr inbounds i32, i32* %buf.addr.011, i64 -1
110;CHECK-NEXT:  %indvar.next = add i32 %indvar, 1
111;CHECK-NEXT:  %exitcond = icmp eq i32 %indvar, %3
112;CHECK-NEXT:  br i1 %exitcond, label %while.end.loopexit, label %while.body
113
114  %i.013 = phi i32 [ %sub, %while.body ], [ %len, %while.body.preheader ]
115  %S.012 = phi i32 [ %add2, %while.body ], [ undef, %while.body.preheader ]
116  %buf.addr.011 = phi i32* [ %add.ptr, %while.body ], [ %buf, %while.body.preheader ]
117  %0 = load i32, i32* %buf.addr.011, align 4
118  %add = add nsw i32 %0, %S.012
119  %arrayidx1 = getelementptr inbounds i32, i32* %buf.addr.011, i64 -1
120  %1 = load i32, i32* %arrayidx1, align 4
121  %add2 = add nsw i32 %add, %1
122  %add.ptr = getelementptr inbounds i32, i32* %buf.addr.011, i64 -2
123  %sub = add nsw i32 %i.013, -2
124  %cmp = icmp sgt i32 %sub, 1
125  br i1 %cmp, label %while.body, label %while.end.loopexit
126
127while.end.loopexit:                               ; preds = %while.body
128  br label %while.end
129
130while.end:                                        ; preds = %while.end.loopexit, %entry
131  %S.0.lcssa = phi i32 [ undef, %entry ], [ %add2, %while.end.loopexit ]
132  ret i32 %S.0.lcssa
133}
134
135