1; RUN: opt < %s -loop-unroll -S | FileCheck %s
2;
3; Verify that the unrolling pass removes existing unroll count metadata
4; and adds a disable unrolling node after unrolling is complete.
5
6target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7target triple = "x86_64-unknown-linux-gnu"
8
9; #pragma clang loop  vectorize(enable) unroll_count(4) vectorize_width(8)
10;
11; Unroll count metadata should be replaced with unroll(disable).  Vectorize
12; metadata should be untouched.
13;
14; CHECK-LABEL: @unroll_count_4(
15; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
16define void @unroll_count_4(i32* nocapture %a) {
17entry:
18  br label %for.body
19
20for.body:                                         ; preds = %for.body, %entry
21  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
22  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
23  %0 = load i32, i32* %arrayidx, align 4
24  %inc = add nsw i32 %0, 1
25  store i32 %inc, i32* %arrayidx, align 4
26  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
27  %exitcond = icmp eq i64 %indvars.iv.next, 64
28  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !1
29
30for.end:                                          ; preds = %for.body
31  ret void
32}
33!1 = !{!1, !2, !3, !4}
34!2 = !{!"llvm.loop.vectorize.enable", i1 true}
35!3 = !{!"llvm.loop.unroll.count", i32 4}
36!4 = !{!"llvm.loop.vectorize.width", i32 8}
37
38; #pragma clang loop unroll(full)
39;
40; An unroll disable metadata node is only added for the unroll count case.
41; In this case, the loop has a full unroll metadata but can't be fully unrolled
42; because the trip count is dynamic.  The full unroll metadata should remain
43; after unrolling.
44;
45; CHECK-LABEL: @unroll_full(
46; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
47define void @unroll_full(i32* nocapture %a, i32 %b) {
48entry:
49  %cmp3 = icmp sgt i32 %b, 0
50  br i1 %cmp3, label %for.body, label %for.end, !llvm.loop !5
51
52for.body:                                         ; preds = %entry, %for.body
53  %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 0, %entry ]
54  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
55  %0 = load i32, i32* %arrayidx, align 4
56  %inc = add nsw i32 %0, 1
57  store i32 %inc, i32* %arrayidx, align 4
58  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
59  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
60  %exitcond = icmp eq i32 %lftr.wideiv, %b
61  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !5
62
63for.end:                                          ; preds = %for.body, %entry
64  ret void
65}
66!5 = !{!5, !6}
67!6 = !{!"llvm.loop.unroll.full"}
68
69; #pragma clang loop unroll(disable)
70;
71; Unroll metadata should not change.
72;
73; CHECK-LABEL: @unroll_disable(
74; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
75define void @unroll_disable(i32* nocapture %a) {
76entry:
77  br label %for.body
78
79for.body:                                         ; preds = %for.body, %entry
80  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
81  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
82  %0 = load i32, i32* %arrayidx, align 4
83  %inc = add nsw i32 %0, 1
84  store i32 %inc, i32* %arrayidx, align 4
85  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
86  %exitcond = icmp eq i64 %indvars.iv.next, 64
87  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !7
88
89for.end:                                          ; preds = %for.body
90  ret void
91}
92!7 = !{!7, !8}
93!8 = !{!"llvm.loop.unroll.disable"}
94
95; This function contains two loops which share the same llvm.loop metadata node
96; with an llvm.loop.unroll.count 2 hint.  Both loops should be unrolled.  This
97; verifies that adding disable metadata to a loop after unrolling doesn't affect
98; other loops which previously shared the same llvm.loop metadata.
99;
100; CHECK-LABEL: @shared_metadata(
101; CHECK: store i32
102; CHECK: store i32
103; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
104; CHECK: store i32
105; CHECK: store i32
106; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
107define void @shared_metadata(i32* nocapture %List) #0 {
108entry:
109  br label %for.body3
110
111for.body3:                                        ; preds = %for.body3, %entry
112  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body3 ]
113  %arrayidx = getelementptr inbounds i32, i32* %List, i64 %indvars.iv
114  %0 = load i32, i32* %arrayidx, align 4
115  %add4 = add nsw i32 %0, 10
116  store i32 %add4, i32* %arrayidx, align 4
117  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
118  %exitcond = icmp eq i64 %indvars.iv.next, 4
119  br i1 %exitcond, label %for.body3.1.preheader, label %for.body3, !llvm.loop !9
120
121for.body3.1.preheader:                            ; preds = %for.body3
122  br label %for.body3.1
123
124for.body3.1:                                      ; preds = %for.body3.1.preheader, %for.body3.1
125  %indvars.iv.1 = phi i64 [ %1, %for.body3.1 ], [ 0, %for.body3.1.preheader ]
126  %1 = add nsw i64 %indvars.iv.1, 1
127  %arrayidx.1 = getelementptr inbounds i32, i32* %List, i64 %1
128  %2 = load i32, i32* %arrayidx.1, align 4
129  %add4.1 = add nsw i32 %2, 10
130  store i32 %add4.1, i32* %arrayidx.1, align 4
131  %exitcond.1 = icmp eq i64 %1, 4
132  br i1 %exitcond.1, label %for.inc5.1, label %for.body3.1, !llvm.loop !9
133
134for.inc5.1:                                       ; preds = %for.body3.1
135  ret void
136}
137!9 = !{!9, !10}
138!10 = !{!"llvm.loop.unroll.count", i32 2}
139
140
141; CHECK: ![[LOOP_1]] = distinct !{![[LOOP_1]], ![[VEC_ENABLE:.*]], ![[WIDTH_8:.*]], ![[UNROLL_DISABLE:.*]]}
142; CHECK: ![[VEC_ENABLE]] = !{!"llvm.loop.vectorize.enable", i1 true}
143; CHECK: ![[WIDTH_8]] = !{!"llvm.loop.vectorize.width", i32 8}
144; CHECK: ![[UNROLL_DISABLE]] = !{!"llvm.loop.unroll.disable"}
145; CHECK: ![[LOOP_2]] = distinct !{![[LOOP_2]], ![[UNROLL_FULL:.*]]}
146; CHECK: ![[UNROLL_FULL]] = !{!"llvm.loop.unroll.full"}
147; CHECK: ![[LOOP_3]] = distinct !{![[LOOP_3]], ![[UNROLL_DISABLE:.*]]}
148; CHECK: ![[LOOP_4]] = distinct !{![[LOOP_4]], ![[UNROLL_DISABLE:.*]]}
149; CHECK: ![[LOOP_5]] = distinct !{![[LOOP_5]], ![[UNROLL_DISABLE:.*]]}
150