1cee313d2SEric Christopher; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-runtime-epilog=true  | FileCheck %s -check-prefixes=EPILOG,COMMON
2cee313d2SEric Christopher; RUN: opt < %s -S -loop-unroll -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON
3cee313d2SEric Christopher;
4a95796a3SArthur Eubanks; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=true  | FileCheck %s -check-prefixes=EPILOG,COMMON
5a95796a3SArthur Eubanks; RUN: opt < %s -S -passes='require<opt-remark-emit>,loop-unroll' -unroll-runtime=true -unroll-runtime-epilog=false | FileCheck %s -check-prefixes=PROLOG,COMMON
6cee313d2SEric Christopher;
7cee313d2SEric Christopher; Restricted versions of unroll (unroll<peeling;noruntime>, unroll-full) should not be doing runtime unrolling
8cee313d2SEric Christopher; even if it is globally enabled through -unroll-runtime option
9cee313d2SEric Christopher;
10a95796a3SArthur Eubanks; 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
11a95796a3SArthur Eubanks; 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
12a95796a3SArthur Eubanks; 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
13a95796a3SArthur Eubanks; 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
14cee313d2SEric Christopher
15cee313d2SEric Christophertarget 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"
16cee313d2SEric Christopher
17cee313d2SEric Christopher; Tests for unrolling loops with run-time trip counts
18cee313d2SEric Christopher
19cee313d2SEric Christopher; COMMON-LABEL: @test(
20cee313d2SEric Christopher
21cee313d2SEric Christopher; EPILOG: %xtraiter = and i32 %n
22cee313d2SEric Christopher; EPILOG:  %lcmp.mod = icmp ne i32 %xtraiter, 0
23cee313d2SEric Christopher; EPILOG:  br i1 %lcmp.mod, label %for.body.epil.preheader, label %for.end.loopexit
24cee313d2SEric Christopher
25cee313d2SEric Christopher; NOEPILOG-NOT: %xtraiter = and i32 %n
26cee313d2SEric Christopher
27cee313d2SEric Christopher; PROLOG: %xtraiter = and i32 %n
28cee313d2SEric Christopher; PROLOG:  %lcmp.mod = icmp ne i32 %xtraiter, 0
29cee313d2SEric Christopher; PROLOG:  br i1 %lcmp.mod, label %for.body.prol.preheader, label %for.body.prol.loopexit
30cee313d2SEric Christopher
31cee313d2SEric Christopher; NOPROLOG-NOT: %xtraiter = and i32 %n
32cee313d2SEric Christopher
33cee313d2SEric Christopher; EPILOG: for.body.epil:
34cee313d2SEric Christopher; EPILOG: %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %for.body.epil ],  [ %indvars.iv.unr, %for.body.epil.preheader ]
35*37ead201SPhilip Reames; EPILOG:  %epil.iter.next = add i32 %epil.iter, 1
36*37ead201SPhilip Reames; EPILOG:  %epil.iter.cmp = icmp ne i32 %epil.iter.next, %xtraiter
37cee313d2SEric Christopher; EPILOG:  br i1 %epil.iter.cmp, label %for.body.epil, label %for.end.loopexit.epilog-lcssa, !llvm.loop !0
38cee313d2SEric Christopher
39cee313d2SEric Christopher; NOEPILOG: for.body:
40cee313d2SEric Christopher; NOEPILOG-NOT: for.body.epil:
41cee313d2SEric Christopher
42cee313d2SEric Christopher; PROLOG: for.body.prol:
43cee313d2SEric Christopher; PROLOG: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.prol.preheader ]
44*37ead201SPhilip Reames; PROLOG:  %prol.iter.next = add i32 %prol.iter, 1
45*37ead201SPhilip Reames; PROLOG:  %prol.iter.cmp = icmp ne i32 %prol.iter.next, %xtraiter
46cee313d2SEric Christopher; PROLOG:  br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit.unr-lcssa, !llvm.loop !0
47cee313d2SEric Christopher
48cee313d2SEric Christopher; NOPROLOG: for.body:
49cee313d2SEric Christopher; NOPROLOG-NOT: for.body.prol:
50cee313d2SEric Christopher
51cee313d2SEric Christopher
52cee313d2SEric Christopherdefine i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly {
53cee313d2SEric Christopherentry:
54cee313d2SEric Christopher  %cmp1 = icmp eq i32 %n, 0
55cee313d2SEric Christopher  br i1 %cmp1, label %for.end, label %for.body
56cee313d2SEric Christopher
57cee313d2SEric Christopherfor.body:                                         ; preds = %for.body, %entry
58cee313d2SEric Christopher  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
59cee313d2SEric Christopher  %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ]
60cee313d2SEric Christopher  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
61cee313d2SEric Christopher  %0 = load i32, i32* %arrayidx, align 4
62cee313d2SEric Christopher  %add = add nsw i32 %0, %sum.02
63cee313d2SEric Christopher  %indvars.iv.next = add i64 %indvars.iv, 1
64cee313d2SEric Christopher  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
65cee313d2SEric Christopher  %exitcond = icmp eq i32 %lftr.wideiv, %n
66cee313d2SEric Christopher  br i1 %exitcond, label %for.end, label %for.body
67cee313d2SEric Christopher
68cee313d2SEric Christopherfor.end:                                          ; preds = %for.body, %entry
69cee313d2SEric Christopher  %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
70cee313d2SEric Christopher  ret i32 %sum.0.lcssa
71cee313d2SEric Christopher}
72cee313d2SEric Christopher
73cee313d2SEric Christopher
74cee313d2SEric Christopher; Still try to completely unroll loops with compile-time trip counts
75cee313d2SEric Christopher; even if the -unroll-runtime is specified
76cee313d2SEric Christopher
77cee313d2SEric Christopher; COMMON-LABEL: @test1(
78cee313d2SEric Christopher; COMMON: for.body:
79cee313d2SEric Christopher; COMMON-NOT: for.body.epil:
80cee313d2SEric Christopher; COMMON-NOT: for.body.prol:
81cee313d2SEric Christopher
82cee313d2SEric Christopherdefine i32 @test1(i32* nocapture %a) nounwind uwtable readonly {
83cee313d2SEric Christopherentry:
84cee313d2SEric Christopher  br label %for.body
85cee313d2SEric Christopher
86cee313d2SEric Christopherfor.body:                                         ; preds = %for.body, %entry
87cee313d2SEric Christopher  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
88cee313d2SEric Christopher  %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
89cee313d2SEric Christopher  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
90cee313d2SEric Christopher  %0 = load i32, i32* %arrayidx, align 4
91cee313d2SEric Christopher  %add = add nsw i32 %0, %sum.01
92cee313d2SEric Christopher  %indvars.iv.next = add i64 %indvars.iv, 1
93cee313d2SEric Christopher  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
94cee313d2SEric Christopher  %exitcond = icmp eq i32 %lftr.wideiv, 5
95cee313d2SEric Christopher  br i1 %exitcond, label %for.end, label %for.body
96cee313d2SEric Christopher
97cee313d2SEric Christopherfor.end:                                          ; preds = %for.body
98cee313d2SEric Christopher  ret i32 %add
99cee313d2SEric Christopher}
100cee313d2SEric Christopher
101cee313d2SEric Christopher; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
102cee313d2SEric Christopher; if the -unroll-runtime option is turned on
103cee313d2SEric Christopher
104cee313d2SEric Christopher; COMMON-LABEL: @foo(
105cee313d2SEric Christopher; EPILOG: bb72.2:
106cee313d2SEric Christopher; PROLOG: bb72.2:
107cee313d2SEric Christopher; NOEPILOG-NOT: bb72.2:
108cee313d2SEric Christopher; NOPROLOG-NOT: bb72.2:
109cee313d2SEric Christopher
110cee313d2SEric Christopherdefine void @foo(i32 %trips) {
111cee313d2SEric Christopherentry:
112cee313d2SEric Christopher        br label %cond_true.outer
113cee313d2SEric Christopher
114cee313d2SEric Christophercond_true.outer:
115cee313d2SEric Christopher        %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
116cee313d2SEric Christopher        br label %bb72
117cee313d2SEric Christopher
118cee313d2SEric Christopherbb72:
119cee313d2SEric Christopher        %indvar.next2 = add i32 %indvar1.ph, 1
120cee313d2SEric Christopher        %exitcond3 = icmp eq i32 %indvar.next2, %trips
121cee313d2SEric Christopher        br i1 %exitcond3, label %cond_true138, label %cond_true.outer
122cee313d2SEric Christopher
123cee313d2SEric Christophercond_true138:
124cee313d2SEric Christopher        ret void
125cee313d2SEric Christopher}
126cee313d2SEric Christopher
127cee313d2SEric Christopher
128cee313d2SEric Christopher; Test run-time unrolling for a loop that counts down by -2.
129cee313d2SEric Christopher
130cee313d2SEric Christopher; COMMON-LABEL: @down(
131cee313d2SEric Christopher; EPILOG: for.body.epil:
132cee313d2SEric Christopher; EPILOG: br i1 %epil.iter.cmp, label %for.body.epil, label %for.cond.for.end_crit_edge.epilog-lcssa
133cee313d2SEric Christopher
134cee313d2SEric Christopher; NOEPILOG: for.body:
135cee313d2SEric Christopher; NOEPILOG-NOT: for.body.epil:
136cee313d2SEric Christopher
137cee313d2SEric Christopher; PROLOG: for.body.prol:
138cee313d2SEric Christopher; PROLOG: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.prol.loopexit
139cee313d2SEric Christopher
140cee313d2SEric Christopher; NOPROLOG: for.body:
141cee313d2SEric Christopher; NOPROLOG-NOT: for.body.prol:
142cee313d2SEric Christopher
143cee313d2SEric Christopherdefine zeroext i16 @down(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
144cee313d2SEric Christopherentry:
145cee313d2SEric Christopher  %cmp2 = icmp eq i32 %len, 0
146cee313d2SEric Christopher  br i1 %cmp2, label %for.end, label %for.body
147cee313d2SEric Christopher
148cee313d2SEric Christopherfor.body:                                         ; preds = %for.body, %entry
149cee313d2SEric Christopher  %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
150cee313d2SEric Christopher  %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
151cee313d2SEric Christopher  %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
152cee313d2SEric Christopher  %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
153cee313d2SEric Christopher  %0 = load i16, i16* %p.addr.05, align 2
154cee313d2SEric Christopher  %conv = zext i16 %0 to i32
155cee313d2SEric Christopher  %add = add i32 %conv, %res.03
156cee313d2SEric Christopher  %sub = add nsw i32 %len.addr.04, -2
157cee313d2SEric Christopher  %cmp = icmp eq i32 %sub, 0
158cee313d2SEric Christopher  br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
159cee313d2SEric Christopher
160cee313d2SEric Christopherfor.cond.for.end_crit_edge:                       ; preds = %for.body
161cee313d2SEric Christopher  %phitmp = trunc i32 %add to i16
162cee313d2SEric Christopher  br label %for.end
163cee313d2SEric Christopher
164cee313d2SEric Christopherfor.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
165cee313d2SEric Christopher  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
166cee313d2SEric Christopher  ret i16 %res.0.lcssa
167cee313d2SEric Christopher}
168cee313d2SEric Christopher
169cee313d2SEric Christopher; Test run-time unrolling disable metadata.
170cee313d2SEric Christopher; COMMON-LABEL: @test2(
171cee313d2SEric Christopher
172cee313d2SEric Christopher; EPILOG: for.body:
173cee313d2SEric Christopher; EPILOG-NOT: for.body.epil:
174cee313d2SEric Christopher
175cee313d2SEric Christopher; NOEPILOG: for.body:
176cee313d2SEric Christopher; NOEPILOG-NOT: for.body.epil:
177cee313d2SEric Christopher
178cee313d2SEric Christopher; PROLOG: for.body:
179cee313d2SEric Christopher; PROLOG-NOT: for.body.prol:
180cee313d2SEric Christopher
181cee313d2SEric Christopher; NOPROLOG: for.body:
182cee313d2SEric Christopher; NOPROLOG-NOT: for.body.prol:
183cee313d2SEric Christopher
184cee313d2SEric Christopherdefine zeroext i16 @test2(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
185cee313d2SEric Christopherentry:
186cee313d2SEric Christopher  %cmp2 = icmp eq i32 %len, 0
187cee313d2SEric Christopher  br i1 %cmp2, label %for.end, label %for.body
188cee313d2SEric Christopher
189cee313d2SEric Christopherfor.body:                                         ; preds = %for.body, %entry
190cee313d2SEric Christopher  %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
191cee313d2SEric Christopher  %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
192cee313d2SEric Christopher  %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
193cee313d2SEric Christopher  %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
194cee313d2SEric Christopher  %0 = load i16, i16* %p.addr.05, align 2
195cee313d2SEric Christopher  %conv = zext i16 %0 to i32
196cee313d2SEric Christopher  %add = add i32 %conv, %res.03
197cee313d2SEric Christopher  %sub = add nsw i32 %len.addr.04, -2
198cee313d2SEric Christopher  %cmp = icmp eq i32 %sub, 0
199cee313d2SEric Christopher  br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0
200cee313d2SEric Christopher
201cee313d2SEric Christopherfor.cond.for.end_crit_edge:                       ; preds = %for.body
202cee313d2SEric Christopher  %phitmp = trunc i32 %add to i16
203cee313d2SEric Christopher  br label %for.end
204cee313d2SEric Christopher
205cee313d2SEric Christopherfor.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
206cee313d2SEric Christopher  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
207cee313d2SEric Christopher  ret i16 %res.0.lcssa
208cee313d2SEric Christopher}
209cee313d2SEric Christopher
210cee313d2SEric Christopher; dont unroll loop with multiple exit/exiting blocks, unless
211cee313d2SEric Christopher; -runtime-unroll-multi-exit=true
212cee313d2SEric Christopher; single exit, multiple exiting blocks.
213cee313d2SEric Christopherdefine void @unique_exit(i32 %arg) {
214cee313d2SEric Christopher; COMMON-LABEL: @unique_exit(
215cee313d2SEric Christopher; COMMON-NOT: .unr
216cee313d2SEric Christopher
217cee313d2SEric Christopherentry:
218cee313d2SEric Christopher  %tmp = icmp sgt i32 undef, %arg
219cee313d2SEric Christopher  br i1 %tmp, label %preheader, label %returnblock
220cee313d2SEric Christopher
221cee313d2SEric Christopherpreheader:                                 ; preds = %entry
222cee313d2SEric Christopher  br label %header
223cee313d2SEric Christopher
224cee313d2SEric ChristopherLoopExit:                                ; preds = %header, %latch
225cee313d2SEric Christopher  %tmp2.ph = phi i32 [ %tmp4, %header ], [ -1, %latch ]
226cee313d2SEric Christopher  br label %returnblock
227cee313d2SEric Christopher
228cee313d2SEric Christopherreturnblock:                                         ; preds = %LoopExit, %entry
229cee313d2SEric Christopher  %tmp2 = phi i32 [ -1, %entry ], [ %tmp2.ph, %LoopExit ]
230cee313d2SEric Christopher  ret void
231cee313d2SEric Christopher
232cee313d2SEric Christopherheader:                                           ; preds = %preheader, %latch
233cee313d2SEric Christopher  %tmp4 = phi i32 [ %inc, %latch ], [ %arg, %preheader ]
234cee313d2SEric Christopher  %inc = add nsw i32 %tmp4, 1
235cee313d2SEric Christopher  br i1 true, label %LoopExit, label %latch
236cee313d2SEric Christopher
237cee313d2SEric Christopherlatch:                                            ; preds = %header
238cee313d2SEric Christopher  %cmp = icmp slt i32 %inc, undef
239cee313d2SEric Christopher  br i1 %cmp, label %header, label %LoopExit
240cee313d2SEric Christopher}
241cee313d2SEric Christopher
242cee313d2SEric Christopher; multiple exit blocks. don't unroll
243cee313d2SEric Christopherdefine void @multi_exit(i64 %trip, i1 %cond) {
244cee313d2SEric Christopher; COMMON-LABEL: @multi_exit(
245cee313d2SEric Christopher; COMMON-NOT: .unr
246cee313d2SEric Christopher
247cee313d2SEric Christopherentry:
248cee313d2SEric Christopher  br label %loop_header
249cee313d2SEric Christopher
250cee313d2SEric Christopherloop_header:
251cee313d2SEric Christopher  %iv = phi i64 [ 0, %entry ], [ %iv_next, %loop_latch ]
252cee313d2SEric Christopher  br i1 %cond, label %loop_latch, label %loop_exiting_bb1
253cee313d2SEric Christopher
254cee313d2SEric Christopherloop_exiting_bb1:
255cee313d2SEric Christopher  br i1 false, label %loop_exiting_bb2, label %exit1
256cee313d2SEric Christopher
257cee313d2SEric Christopherloop_exiting_bb2:
258cee313d2SEric Christopher  br i1 false, label %loop_latch, label %exit3
259cee313d2SEric Christopher
260cee313d2SEric Christopherexit3:
261cee313d2SEric Christopher  ret void
262cee313d2SEric Christopher
263cee313d2SEric Christopherloop_latch:
264cee313d2SEric Christopher  %iv_next = add i64 %iv, 1
265cee313d2SEric Christopher  %cmp = icmp ne i64 %iv_next, %trip
266cee313d2SEric Christopher  br i1 %cmp, label %loop_header, label %exit2.loopexit
267cee313d2SEric Christopher
268cee313d2SEric Christopherexit1:
269cee313d2SEric Christopher ret void
270cee313d2SEric Christopher
271cee313d2SEric Christopherexit2.loopexit:
272cee313d2SEric Christopher  ret void
273cee313d2SEric Christopher}
274cee313d2SEric Christopher
275cee313d2SEric Christopher!0 = distinct !{!0, !1}
276cee313d2SEric Christopher!1 = !{!"llvm.loop.unroll.runtime.disable"}
277cee313d2SEric Christopher
278cee313d2SEric Christopher; need to use LABEL here to separate function IR matching from metadata matching
279cee313d2SEric Christopher; COMMON-LABEL: {{^}}!0 =
280cee313d2SEric Christopher
281cee313d2SEric Christopher; EPILOG-SAME: distinct !{!0, !1}
282cee313d2SEric Christopher; EPILOG: !1 = !{!"llvm.loop.unroll.disable"}
283cee313d2SEric Christopher
284cee313d2SEric Christopher; PROLOG-SAME: distinct !{!0, !1}
285cee313d2SEric Christopher; PROLOG: !1 = !{!"llvm.loop.unroll.disable"}
286