1; RUN: opt < %s -S -loop-unroll -unroll-runtime=true | FileCheck %s
2
3target 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"
4
5; Tests for unrolling loops with run-time trip counts
6
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
10
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
16
17define i32 @test(i32* nocapture %a, i32 %n) nounwind uwtable readonly {
18entry:
19  %cmp1 = icmp eq i32 %n, 0
20  br i1 %cmp1, label %for.end, label %for.body
21
22for.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
32
33for.end:                                          ; preds = %for.body, %entry
34  %sum.0.lcssa = phi i32 [ 0, %entry ], [ %add, %for.body ]
35  ret i32 %sum.0.lcssa
36}
37
38
39; Still try to completely unroll loops with compile-time trip counts
40; even if the -unroll-runtime is specified
41
42; CHECK: for.body:
43; CHECK-NOT: for.body.prol:
44
45define i32 @test1(i32* nocapture %a) nounwind uwtable readonly {
46entry:
47  br label %for.body
48
49for.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
59
60for.end:                                          ; preds = %for.body
61  ret i32 %add
62}
63
64; This is test 2007-05-09-UnknownTripCount.ll which can be unrolled now
65; if the -unroll-runtime option is turned on
66
67; CHECK: bb72.2:
68
69define void @foo(i32 %trips) {
70entry:
71        br label %cond_true.outer
72
73cond_true.outer:
74        %indvar1.ph = phi i32 [ 0, %entry ], [ %indvar.next2, %bb72 ]
75        br label %bb72
76
77bb72:
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
81
82cond_true138:
83        ret void
84}
85
86
87; Test run-time unrolling for a loop that counts down by -2.
88
89; CHECK: for.body.prol:
90; CHECK: br i1 %prol.iter.cmp, label %for.body.prol, label %for.body.preheader.split
91
92define zeroext i16 @down(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
93entry:
94  %cmp2 = icmp eq i32 %len, 0
95  br i1 %cmp2, label %for.end, label %for.body
96
97for.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
108
109for.cond.for.end_crit_edge:                       ; preds = %for.body
110  %phitmp = trunc i32 %add to i16
111  br label %for.end
112
113for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
114  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
115  ret i16 %res.0.lcssa
116}
117
118; Test run-time unrolling disable metadata.
119; CHECK: for.body:
120; CHECK-NOT: for.body.prol:
121
122define zeroext i16 @test2(i16* nocapture %p, i32 %len) nounwind uwtable readonly {
123entry:
124  %cmp2 = icmp eq i32 %len, 0
125  br i1 %cmp2, label %for.end, label %for.body
126
127for.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
138
139for.cond.for.end_crit_edge:                       ; preds = %for.body
140  %phitmp = trunc i32 %add to i16
141  br label %for.end
142
143for.end:                                          ; preds = %for.cond.for.end_crit_edge, %entry
144  %res.0.lcssa = phi i16 [ %phitmp, %for.cond.for.end_crit_edge ], [ 0, %entry ]
145  ret i16 %res.0.lcssa
146}
147
148!0 = distinct !{!0, !1}
149!1 = !{!"llvm.loop.unroll.runtime.disable"}
150
151; CHECK: !0 = distinct !{!0, !1}
152; CHECK: !1 = !{!"llvm.loop.unroll.disable"}
153
154