1; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true  | FileCheck %s -check-prefix=EPILOG
2; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefix=PROLOG
3
4; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(unroll)' -unroll-runtime=true -unroll-runtime-epilog=true  | FileCheck %s -check-prefix=EPILOG
5; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(unroll)' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefix=PROLOG
6
7target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
8
9; Tests for unrolling loops with run-time trip counts
10
11; EPILOG: %xtraiter = and i32 %n
12; EPILOG:  %lcmp.mod = icmp ne i32 %xtraiter, 0
13; EPILOG:  br i1 %lcmp.mod, label %for.body.epil.preheader, label %for.end.loopexit
14
15; PROLOG: %xtraiter = and i32 %n
16; PROLOG:  %lcmp.mod = icmp ne i32 %xtraiter, 0
17; PROLOG:  br i1 %lcmp.mod, label %for.body.prol.preheader, label %for.body.prol.loopexit
18
19; EPILOG: for.body.epil:
20; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ],  [ %indvars.iv.unr, %for.body.epil.preheader ]
21; EPILOG:  %epil.iter.sub = sub i32 %epil.iter, 1
22; EPILOG:  %epil.iter.cmp = icmp ne i32 %epil.iter.sub, 0
23; EPILOG:  br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop !0
24
25; PROLOG: for.body.prol:
26; PROLOG: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.prol.preheader ]
27; PROLOG:  %prol.iter.sub = sub i32 %prol.iter, 1
28; PROLOG:  %prol.iter.cmp = icmp ne i32 %prol.iter.sub, 0
29; PROLOG:  br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop !0
30
31
32define i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly {
33entry:
34  %cmp1 = icmp eq i32 %n, 0
35  br i1 %cmp1, label %for.end, label %for.body
36
37for.body:                                         ; preds = %for.body, %entry
38  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
39  %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ]
40  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
41  %0 = load i32, i32* %arrayidx, align 4
42  %add = add nsw i32 %0, %sum.02
43  %indvars.iv.next = add i64 %indvars.iv, 1
44  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
45  %exitcond = icmp eq i32 %lftr.wideiv, %n
46  br i1 %exitcond, label %for.end, label %for.body
47
48for.end:                                          ; preds = %for.body, %entry
49  %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
50  ret i32 %sum.0.lcssa
51}
52
53
54; Still try to completely unroll loops with compile-time trip counts
55; even if the -unroll-runtime is specified
56
57; EPILOG: for.body:
58; EPILOG-NOT: for.body.epil:
59
60; PROLOG: for.body:
61; PROLOG-NOT: for.body.prol:
62
63define i32 @test1(i32* nocapture %a) nounwind uwtable readonly {
64entry:
65  br label %for.body
66
67for.body:                                         ; preds = %for.body, %entry
68  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
69  %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
70  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
71  %0 = load i32, i32* %arrayidx, align 4
72  %add = add nsw i32 %0, %sum.01
73  %indvars.iv.next = add i64 %indvars.iv, 1
74  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
75  %exitcond = icmp eq i32 %lftr.wideiv, 5
76  br i1 %exitcond, label %for.end, label %for.body
77
78for.end:                                          ; preds = %for.body
79  ret i32 %add
80}
81
82; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
83; if the -unroll-runtime option is turned on
84
85; EPILOG: bb72.2:
86; PROLOG: bb72.2:
87
88define void @foo(i32 %trips) {
89entry:
90        br label %cond_true.outer
91
92cond_true.outer:
93        %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
94        br label %bb72
95
96bb72:
97        %indvar.next2 = add i32 %indvar1.ph, 1
98        %exitcond3 = icmp eq i32 %indvar.next2, %trips
99        br i1 %exitcond3, label %cond_true138, label %cond_true.outer
100
101cond_true138:
102        ret void
103}
104
105
106; Test run-time unrolling for a loop that counts down by -2.
107
108; EPILOG: for.body.epil:
109; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.cond.for.end_crit_edge.epilog-lcssa
110
111; PROLOG: for.body.prol:
112; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit
113
114define zeroext i16 @down(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
115entry:
116  %cmp2 = icmp eq i32 %len, 0
117  br i1 %cmp2, label %for.end, label %for.body
118
119for.body:                                         ; preds = %for.body, %entry
120  %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
121  %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
122  %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
123  %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
124  %0 = load i16, i16* %p.addr.05, align 2
125  %conv = zext i16 %0 to i32
126  %add = add i32 %conv, %res.03
127  %sub = add nsw i32 %len.addr.04, -2
128  %cmp = icmp eq i32 %sub, 0
129  br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
130
131for.cond.for.end_crit_edge:                       ; preds = %for.body
132  %phitmp = trunc i32 %add to i16
133  br label %for.end
134
135for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
136  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
137  ret i16 %res.0.lcssa
138}
139
140; Test run-time unrolling disable metadata.
141; EPILOG: for.body:
142; EPILOG-NOT: for.body.epil:
143
144; PROLOG: for.body:
145; PROLOG-NOT: for.body.prol:
146
147define zeroext i16 @test2(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
148entry:
149  %cmp2 = icmp eq i32 %len, 0
150  br i1 %cmp2, label %for.end, label %for.body
151
152for.body:                                         ; preds = %for.body, %entry
153  %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
154  %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
155  %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
156  %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
157  %0 = load i16, i16* %p.addr.05, align 2
158  %conv = zext i16 %0 to i32
159  %add = add i32 %conv, %res.03
160  %sub = add nsw i32 %len.addr.04, -2
161  %cmp = icmp eq i32 %sub, 0
162  br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0
163
164for.cond.for.end_crit_edge:                       ; preds = %for.body
165  %phitmp = trunc i32 %add to i16
166  br label %for.end
167
168for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
169  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
170  ret i16 %res.0.lcssa
171}
172
173!0 = distinct !{!0, !1}
174!1 = !{!"llvm.loop.unroll.runtime.disable"}
175
176; EPILOG: !0 = distinct !{!0, !1}
177; EPILOG: !1 = !{!"llvm.loop.unroll.disable"}
178
179; PROLOG: !0 = distinct !{!0, !1}
180; PROLOG: !1 = !{!"llvm.loop.unroll.disable"}
181