1 ; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON
2 ; RUN: opt < %s -S -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>,unroll' -unroll-runtime=true -unroll-runtime-epilog=true | FileCheck %s -check-prefixes=EPILOG,COMMON
5 ; RUN: opt < %s -S -passes='require<opt-remark-emit>,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>,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>,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(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(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(
21 ; EPILOG: %xtraiter = and i32 %n
22 ; EPILOG: %lcmp.mod = icmp ne i32 %xtraiter, 0
23 ; EPILOG: br i1 %lcmp.mod, label %for.body.epil.preheader, label %for.end.loopexit
25 ; NOEPILOG-NOT: %xtraiter = and i32 %n
27 ; PROLOG: %xtraiter = and i32 %n
28 ; PROLOG: %lcmp.mod = icmp ne i32 %xtraiter, 0
29 ; PROLOG: br i1 %lcmp.mod, label %for.body.prol.preheader, label %for.body.prol.loopexit
31 ; NOPROLOG-NOT: %xtraiter = and i32 %n
33 ; EPILOG: for.body.epil:
34 ; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ], [ %indvars.iv.unr, %for.body.epil.preheader ]
35 ; EPILOG: %epil.iter.sub = sub i32 %epil.iter, 1
36 ; EPILOG: %epil.iter.cmp = icmp ne i32 %epil.iter.sub, 0
37 ; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop !0
40 ; NOEPILOG-NOT: for.body.epil:
42 ; PROLOG: for.body.prol:
43 ; PROLOG: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.prol.preheader ]
44 ; PROLOG: %prol.iter.sub = sub i32 %prol.iter, 1
45 ; PROLOG: %prol.iter.cmp = icmp ne i32 %prol.iter.sub, 0
46 ; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop !0
49 ; NOPROLOG-NOT: for.body.prol:
52 define i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly {
54 %cmp1 = icmp eq i32 %n, 0
55 br i1 %cmp1, label %for.end, label %for.body
57 for.body: ; preds = %for.body, %entry
58 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
59 %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ]
60 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
61 %0 = load i32, i32* %arrayidx, align 4
62 %add = add nsw i32 %0, %sum.02
63 %indvars.iv.next = add i64 %indvars.iv, 1
64 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
65 %exitcond = icmp eq i32 %lftr.wideiv, %n
66 br i1 %exitcond, label %for.end, label %for.body
68 for.end: ; preds = %for.body, %entry
69 %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
74 ; Still try to completely unroll loops with compile-time trip counts
75 ; even if the -unroll-runtime is specified
77 ; COMMON-LABEL: @test1(
79 ; COMMON-NOT: for.body.epil:
80 ; COMMON-NOT: for.body.prol:
82 define i32 @test1(i32* nocapture %a) nounwind uwtable readonly {
86 for.body: ; preds = %for.body, %entry
87 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
88 %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
89 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
90 %0 = load i32, i32* %arrayidx, align 4
91 %add = add nsw i32 %0, %sum.01
92 %indvars.iv.next = add i64 %indvars.iv, 1
93 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
94 %exitcond = icmp eq i32 %lftr.wideiv, 5
95 br i1 %exitcond, label %for.end, label %for.body
97 for.end: ; preds = %for.body
101 ; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
102 ; if the -unroll-runtime option is turned on
104 ; COMMON-LABEL: @foo(
107 ; NOEPILOG-NOT: bb72.2:
108 ; NOPROLOG-NOT: bb72.2:
110 define void @foo(i32 %trips) {
112 br label %cond_true.outer
115 %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
119 %indvar.next2 = add i32 %indvar1.ph, 1
120 %exitcond3 = icmp eq i32 %indvar.next2, %trips
121 br i1 %exitcond3, label %cond_true138, label %cond_true.outer
128 ; Test run-time unrolling for a loop that counts down by -2.
130 ; COMMON-LABEL: @down(
131 ; EPILOG: for.body.epil:
132 ; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.cond.for.end_crit_edge.epilog-lcssa
134 ; NOEPILOG: for.body:
135 ; NOEPILOG-NOT: for.body.epil:
137 ; PROLOG: for.body.prol:
138 ; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit
140 ; NOPROLOG: for.body:
141 ; NOPROLOG-NOT: for.body.prol:
143 define zeroext i16 @down(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
145 %cmp2 = icmp eq i32 %len, 0
146 br i1 %cmp2, label %for.end, label %for.body
148 for.body: ; preds = %for.body, %entry
149 %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
150 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
151 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
152 %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
153 %0 = load i16, i16* %p.addr.05, align 2
154 %conv = zext i16 %0 to i32
155 %add = add i32 %conv, %res.03
156 %sub = add nsw i32 %len.addr.04, -2
157 %cmp = icmp eq i32 %sub, 0
158 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
160 for.cond.for.end_crit_edge: ; preds = %for.body
161 %phitmp = trunc i32 %add to i16
164 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
165 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
169 ; Test run-time unrolling disable metadata.
170 ; COMMON-LABEL: @test2(
173 ; EPILOG-NOT: for.body.epil:
175 ; NOEPILOG: for.body:
176 ; NOEPILOG-NOT: for.body.epil:
179 ; PROLOG-NOT: for.body.prol:
181 ; NOPROLOG: for.body:
182 ; NOPROLOG-NOT: for.body.prol:
184 define zeroext i16 @test2(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
186 %cmp2 = icmp eq i32 %len, 0
187 br i1 %cmp2, label %for.end, label %for.body
189 for.body: ; preds = %for.body, %entry
190 %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
191 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
192 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
193 %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
194 %0 = load i16, i16* %p.addr.05, align 2
195 %conv = zext i16 %0 to i32
196 %add = add i32 %conv, %res.03
197 %sub = add nsw i32 %len.addr.04, -2
198 %cmp = icmp eq i32 %sub, 0
199 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0
201 for.cond.for.end_crit_edge: ; preds = %for.body
202 %phitmp = trunc i32 %add to i16
205 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
206 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
210 ; dont unroll loop with multiple exit/exiting blocks, unless
211 ; -runtime-unroll-multi-exit=true
212 ; single exit, multiple exiting blocks.
213 define void @unique_exit(i32 %arg) {
214 ; COMMON-LABEL: @unique_exit(
218 %tmp = icmp sgt i32 undef, %arg
219 br i1 %tmp, label %preheader, label %returnblock
221 preheader: ; preds = %entry
224 LoopExit: ; preds = %header, %latch
225 %tmp2.ph = phi i32 [ %tmp4, %header ], [ -1, %latch ]
226 br label %returnblock
228 returnblock: ; preds = %LoopExit, %entry
229 %tmp2 = phi i32 [ -1, %entry ], [ %tmp2.ph, %LoopExit ]
232 header: ; preds = %preheader, %latch
233 %tmp4 = phi i32 [ %inc, %latch ], [ %arg, %preheader ]
234 %inc = add nsw i32 %tmp4, 1
235 br i1 true, label %LoopExit, label %latch
237 latch: ; preds = %header
238 %cmp = icmp slt i32 %inc, undef
239 br i1 %cmp, label %header, label %LoopExit
242 ; multiple exit blocks. don't unroll
243 define void @multi_exit(i64 %trip, i1 %cond) {
244 ; COMMON-LABEL: @multi_exit(
248 br label %loop_header
251 %iv = phi i64 [ 0, %entry ], [ %iv_next, %loop_latch ]
252 br i1 %cond, label %loop_latch, label %loop_exiting_bb1
255 br i1 false, label %loop_exiting_bb2, label %exit1
258 br i1 false, label %loop_latch, label %exit3
264 %iv_next = add i64 %iv, 1
265 %cmp = icmp ne i64 %iv_next, %trip
266 br i1 %cmp, label %loop_header, label %exit2.loopexit
275 !0 = distinct !{!0, !1}
276 !1 = !{!"llvm.loop.unroll.runtime.disable"}
278 ; need to use LABEL here to separate function IR matching from metadata matching
279 ; COMMON-LABEL: {{^}}!0 =
281 ; EPILOG-SAME: distinct !{!0, !1}
282 ; EPILOG: !1 = !{!"llvm.loop.unroll.disable"}
284 ; PROLOG-SAME: distinct !{!0, !1}
285 ; PROLOG: !1 = !{!"llvm.loop.unroll.disable"}