1 ; RUN: opt < %s -S -loop-unroll -unroll-runtime=true | FileCheck %s
3 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"
5 ; Tests for unrolling loops with run-time trip counts
7 ; CHECK: %xtraiter = and i32 %n
8 ; CHECK: %lcmp.mod = icmp ne i32 %xtraiter, 0
9 ; CHECK: br i1 %lcmp.mod, label %for.body.prol, label %for.body.preheader.split
11 ; CHECK: for.body.prol:
12 ; CHECK: %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %for.body.prol ], [ 0, %for.body.preheader ]
13 ; CHECK: %prol.iter.sub = sub i32 %prol.iter, 1
14 ; CHECK: %prol.iter.cmp = icmp ne i32 %prol.iter.sub, 0
15 ; CHECK: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.preheader.split, !llvm.loop !0
17 define i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly {
19 %cmp1 = icmp eq i32 %n, 0
20 br i1 %cmp1, label %for.end, label %for.body
22 for.body: ; preds = %for.body, %entry
23 %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
24 %sum.02 = phi i32 [ %add, %for.body ], [ 0, %entry ]
25 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
26 %0 = load i32, i32* %arrayidx, align 4
27 %add = add nsw i32 %0, %sum.02
28 %indvars.iv.next = add i64 %indvars.iv, 1
29 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
30 %exitcond = icmp eq i32 %lftr.wideiv, %n
31 br i1 %exitcond, label %for.end, label %for.body
33 for.end: ; preds = %for.body, %entry
34 %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
39 ; Still try to completely unroll loops with compile-time trip counts
40 ; even if the -unroll-runtime is specified
43 ; CHECK-NOT: for.body.prol:
45 define i32 @test1(i32* nocapture %a) nounwind uwtable readonly {
49 for.body: ; preds = %for.body, %entry
50 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
51 %sum.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
52 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
53 %0 = load i32, i32* %arrayidx, align 4
54 %add = add nsw i32 %0, %sum.01
55 %indvars.iv.next = add i64 %indvars.iv, 1
56 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
57 %exitcond = icmp eq i32 %lftr.wideiv, 5
58 br i1 %exitcond, label %for.end, label %for.body
60 for.end: ; preds = %for.body
64 ; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
65 ; if the -unroll-runtime option is turned on
69 define void @foo(i32 %trips) {
71 br label %cond_true.outer
74 %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
78 %indvar.next2 = add i32 %indvar1.ph, 1
79 %exitcond3 = icmp eq i32 %indvar.next2, %trips
80 br i1 %exitcond3, label %cond_true138, label %cond_true.outer
87 ; Test run-time unrolling for a loop that counts down by -2.
89 ; CHECK: for.body.prol:
90 ; CHECK: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.preheader.split
92 define zeroext i16 @down(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
94 %cmp2 = icmp eq i32 %len, 0
95 br i1 %cmp2, label %for.end, label %for.body
97 for.body: ; preds = %for.body, %entry
98 %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
99 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
100 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
101 %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
102 %0 = load i16, i16* %p.addr.05, align 2
103 %conv = zext i16 %0 to i32
104 %add = add i32 %conv, %res.03
105 %sub = add nsw i32 %len.addr.04, -2
106 %cmp = icmp eq i32 %sub, 0
107 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
109 for.cond.for.end_crit_edge: ; preds = %for.body
110 %phitmp = trunc i32 %add to i16
113 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
114 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
118 ; Test run-time unrolling disable metadata.
120 ; CHECK-NOT: for.body.prol:
122 define zeroext i16 @test2(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
124 %cmp2 = icmp eq i32 %len, 0
125 br i1 %cmp2, label %for.end, label %for.body
127 for.body: ; preds = %for.body, %entry
128 %p.addr.05 = phi i16* [ %incdec.ptr, %for.body ], [ %p, %entry ]
129 %len.addr.04 = phi i32 [ %sub, %for.body ], [ %len, %entry ]
130 %res.03 = phi i32 [ %add, %for.body ], [ 0, %entry ]
131 %incdec.ptr = getelementptr inbounds i16, i16* %p.addr.05, i64 1
132 %0 = load i16, i16* %p.addr.05, align 2
133 %conv = zext i16 %0 to i32
134 %add = add i32 %conv, %res.03
135 %sub = add nsw i32 %len.addr.04, -2
136 %cmp = icmp eq i32 %sub, 0
137 br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body, !llvm.loop !0
139 for.cond.for.end_crit_edge: ; preds = %for.body
140 %phitmp = trunc i32 %add to i16
143 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
144 %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
148 !0 = distinct !{!0, !1}
149 !1 = !{!"llvm.loop.unroll.runtime.disable"}
151 ; CHECK: !0 = distinct !{!0, !1}
152 ; CHECK: !1 = !{!"llvm.loop.unroll.disable"}