1 ; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON
2 ; RUN: opt < %s -S -passes=loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON
4 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON
5 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON
7 ; Restricted versions of unroll (unroll<peeling;noruntime>, unroll-full) should not be doing runtime unrolling
8 ; even if it is globally enabled through -unroll-runtime option
10 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll<peeling;no-runtime>' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=NOEPILOG,COMMON
11 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll<peeling;no-runtime>' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=NOPROLOG,COMMON
12 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(loop-unroll-full)' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=NOEPILOG,COMMON
13 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop(loop-unroll-full)' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=NOPROLOG,COMMON
15 target 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"
17 ; Tests for unrolling loops with run-time trip counts
19 ; COMMON-LABEL: @test(
22 ; EPILOG: br i1 %cmp1, label %for.end, label %for.body.preheader, !prof [[EPILOG_PROF_0:![0-9]+]]
23 ; EPILOG: for.body.preheader:
24 ; EPILOG: %xtraiter = and i32 %n
25 ; EPILOG: br i1 %1, label %for.end.loopexit.unr-lcssa, label %for.body.preheader.new, !prof [[EPILOG_PROF_1:![0-9]+]]
27 ; EPILOG: for.end.loopexit.unr-lcssa:
28 ; EPILOG: %lcmp.mod = icmp ne i32 %xtraiter, 0
29 ; EPILOG: br i1 %lcmp.mod, label %for.body.epil.preheader, label %for.end.loopexit, !prof [[EPILOG_PROF_2:![0-9]+]]
31 ; NOEPILOG-NOT: %xtraiter = and i32 %n
34 ; PROLOG: br i1 %cmp1, label %for.end, label %for.body.preheader, !prof [[PROLOG_PROF_0:![0-9]+]]
36 ; PROLOG: for.body.preheader:
37 ; PROLOG: %xtraiter = and i32 %n
38 ; PROLOG: %lcmp.mod = icmp ne i32 %xtraiter, 0
39 ; PROLOG: br i1 %lcmp.mod, label %for.body.prol.preheader, label %for.body.prol.loopexit, !prof [[PROLOG_PROF_1:![0-9]+]]
41 ; NOPROLOG-NOT: %xtraiter = and i32 %n
43 ; EPILOG: for.body.epil:
44 ; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ], [ %indvars.iv.unr, %for.body.epil.preheader ]
45 ; EPILOG: %epil.iter.next = add i32 %epil.iter, 1
46 ; EPILOG: %epil.iter.cmp = icmp ne i32 %epil.iter.next, %xtraiter
47 ; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !prof [[EPILOG_PROF_3:![0-9]+]], !llvm.loop [[EPILOG_LOOP:![0-9]+]]
50 ; NOEPILOG-NOT: for.body.epil:
52 ; PROLOG: for.body.prol:
53 ; PROLOG: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.prol.preheader ]
54 ; PROLOG: %prol.iter.next = add i32 %prol.iter, 1
55 ; PROLOG: %prol.iter.cmp = icmp ne i32 %prol.iter.next, %xtraiter
56 ; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !prof [[PROLOG_PROF_2:![0-9]+]], !llvm.loop [[PROLOG_LOOP:![0-9]+]]
58 ; PROLOG: for.body.prol.loopexit:
59 ; PROLOG: br i1 %2, label %for.end.loopexit, label %for.body.preheader.new, !prof [[PROLOG_PROF_1:![0-9]+]]
62 ; NOPROLOG-NOT: for.body.prol:
65 define i32 @test(ptr nocapture %a, i32 %n) nounwind uwtable readonly !prof !2 {
67 %cmp1 = icmp eq i32 %n, 0
68 br i1 %cmp1, label %for.end, label %for.body, !prof !3
70 for.body: ; preds = %for.body, %entry
71 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
72 %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ]
73 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv
74 %0 = load i32, ptr %arrayidx, align 4
75 %add = add nsw i32 %0, %sum.02
76 %indvars.iv.next = add i64 %indvars.iv, 1
77 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
78 %exitcond = icmp eq i32 %lftr.wideiv, %n
79 br i1 %exitcond, label %for.end, label %for.body, !prof !4
81 for.end: ; preds = %for.body, %entry
82 %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
87 ; Still try to completely unroll loops with compile-time trip counts
88 ; even if the -unroll-runtime is specified
90 ; COMMON-LABEL: @test1(
92 ; COMMON-NOT: for.body.epil:
93 ; COMMON-NOT: for.body.prol:
95 define i32 @test1(ptr nocapture %a) nounwind uwtable readonly {
99 for.body: ; preds = %for.body, %entry
100 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
101 %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
102 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv
103 %0 = load i32, ptr %arrayidx, align 4
104 %add = add nsw i32 %0, %sum.01
105 %indvars.iv.next = add i64 %indvars.iv, 1
106 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
107 %exitcond = icmp eq i32 %lftr.wideiv, 5
108 br i1 %exitcond, label %for.end, label %for.body
110 for.end: ; preds = %for.body
114 ; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
115 ; if the -unroll-runtime option is turned on
117 ; COMMON-LABEL: @foo(
120 ; NOEPILOG-NOT: bb72.2:
121 ; NOPROLOG-NOT: bb72.2:
123 define void @foo(i32 %trips) {
125 br label %cond_true.outer
128 %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
132 %indvar.next2 = add i32 %indvar1.ph, 1
133 %exitcond3 = icmp eq i32 %indvar.next2, %trips
134 br i1 %exitcond3, label %cond_true138, label %cond_true.outer
141 ; Test run-time unrolling for a loop that counts down by -2.
143 ; COMMON-LABEL: @down(
144 ; EPILOG: for.body.epil:
145 ; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.cond.for.end_crit_edge.epilog-lcssa
147 ; NOEPILOG: for.body:
148 ; NOEPILOG-NOT: for.body.epil:
150 ; PROLOG: for.body.prol:
151 ; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit
153 ; NOPROLOG: for.body:
154 ; NOPROLOG-NOT: for.body.prol:
156 define zeroext i16 @down(ptr nocapture %p, i32 %len) nounwind uwtable readonly {
158 %cmp2 = icmp eq i32 %len, 0
159 br i1 %cmp2, label %for.end, label %for.body
161 for.body: ; preds = %for.body, %entry
162 %p.addr.05 = phi ptr [ %incdec.ptr, %for.body ], [ %p, %entry ]
163 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
164 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
165 %incdec.ptr = getelementptr inbounds i16, ptr %p.addr.05, i64 1
166 %0 = load i16, ptr %p.addr.05, align 2
167 %conv = zext i16 %0 to i32
168 %add = add i32 %conv, %res.03
169 %sub = add nsw i32 %len.addr.04, -2
170 %cmp = icmp eq i32 %sub, 0
171 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
173 for.cond.for.end_crit_edge: ; preds = %for.body
174 %phitmp = trunc i32 %add to i16
177 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
178 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
182 ; Test run-time unrolling disable metadata.
183 ; COMMON-LABEL: @test2(
186 ; EPILOG-NOT: for.body.epil:
188 ; NOEPILOG: for.body:
189 ; NOEPILOG-NOT: for.body.epil:
192 ; PROLOG-NOT: for.body.prol:
194 ; NOPROLOG: for.body:
195 ; NOPROLOG-NOT: for.body.prol:
197 define zeroext i16 @test2(ptr nocapture %p, i32 %len) nounwind uwtable readonly {
199 %cmp2 = icmp eq i32 %len, 0
200 br i1 %cmp2, label %for.end, label %for.body
202 for.body: ; preds = %for.body, %entry
203 %p.addr.05 = phi ptr [ %incdec.ptr, %for.body ], [ %p, %entry ]
204 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
205 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
206 %incdec.ptr = getelementptr inbounds i16, ptr %p.addr.05, i64 1
207 %0 = load i16, ptr %p.addr.05, align 2
208 %conv = zext i16 %0 to i32
209 %add = add i32 %conv, %res.03
210 %sub = add nsw i32 %len.addr.04, -2
211 %cmp = icmp eq i32 %sub, 0
212 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0
214 for.cond.for.end_crit_edge: ; preds = %for.body
215 %phitmp = trunc i32 %add to i16
218 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
219 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
223 ; dont unroll loop with multiple exit/exiting blocks, unless
224 ; -runtime-unroll-multi-exit=true
225 ; single exit, multiple exiting blocks.
226 define void @unique_exit(i32 %arg) {
227 ; COMMON-LABEL: @unique_exit(
231 %tmp = icmp sgt i32 undef, %arg
232 br i1 %tmp, label %preheader, label %returnblock
234 preheader: ; preds = %entry
237 LoopExit: ; preds = %header, %latch
238 %tmp2.ph = phi i32 [ %tmp4, %header ], [ -1, %latch ]
239 br label %returnblock
241 returnblock: ; preds = %LoopExit, %entry
242 %tmp2 = phi i32 [ -1, %entry ], [ %tmp2.ph, %LoopExit ]
245 header: ; preds = %preheader, %latch
246 %tmp4 = phi i32 [ %inc, %latch ], [ %arg, %preheader ]
247 %inc = add nsw i32 %tmp4, 1
248 br i1 true, label %LoopExit, label %latch
250 latch: ; preds = %header
251 %cmp = icmp slt i32 %inc, undef
252 br i1 %cmp, label %header, label %LoopExit
255 ; multiple exit blocks. don't unroll
256 define void @multi_exit(i64 %trip, i1 %cond) {
257 ; COMMON-LABEL: @multi_exit(
261 br label %loop_header
264 %iv = phi i64 [ 0, %entry ], [ %iv_next, %loop_latch ]
265 br i1 %cond, label %loop_latch, label %loop_exiting_bb1
268 br i1 false, label %loop_exiting_bb2, label %exit1
271 br i1 false, label %loop_latch, label %exit3
277 %iv_next = add i64 %iv, 1
278 %cmp = icmp ne i64 %iv_next, %trip
279 br i1 %cmp, label %loop_header, label %exit2.loopexit
288 !0 = distinct !{!0, !1}
289 !1 = !{!"llvm.loop.unroll.runtime.disable"}
290 !2 = !{!"function_entry_count", i64 1}
291 !3 = !{!"branch_weights", i32 1, i32 11}
292 !4 = !{!"branch_weights", i32 1, i32 42}
294 ; need to use LABEL here to separate function IR matching from metadata matching
295 ; COMMON-LABEL: {{^}}!0 =
297 ; EPILOG: [[EPILOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11}
298 ; EPILOG: [[EPILOG_PROF_1]] = !{!"branch_weights", i32 1, i32 127}
299 ; EPILOG: [[EPILOG_PROF_2]] = !{!"branch_weights", i32 1, i32 7}
300 ; EPILOG: [[EPILOG_PROF_3]] = !{!"branch_weights", i32 3, i32 1}
302 ; EPILOG: [[EPILOG_LOOP]] = distinct !{[[EPILOG_LOOP]], [[EPILOG_LOOP_1:![0-9]+]]}
303 ; EPILOG: [[EPILOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"}
305 ; PROLOG: [[PROLOG_PROF_0]] = !{!"branch_weights", i32 1, i32 11}
306 ; PROLOG: [[PROLOG_PROF_1]] = !{!"branch_weights", i32 1, i32 127}
307 ; PROLOG: [[PROLOG_PROF_2]] = !{!"branch_weights", i32 3, i32 1}
309 ; PROLOG: distinct !{[[PROLOG_LOOP]], [[PROLOG_LOOP_1:![0-9]+]]}
310 ; PROLOG: [[PROLOG_LOOP_1]] = !{!"llvm.loop.unroll.disable"}