1 ; RUN: opt < %s -loop-unroll -S | FileCheck %s
3 ; Verify that the unrolling pass removes existing unroll count metadata
4 ; and adds a disable unrolling node after unrolling is complete.
6 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 target triple = "x86_64-unknown-linux-gnu"
9 ; #pragma clang loop vectorize(enable) unroll_count(4) vectorize_width(8)
11 ; Unroll count metadata should be replaced with unroll(disable). Vectorize
12 ; metadata should be untouched.
14 ; CHECK-LABEL: @unroll_count_4(
15 ; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_1:.*]]
16 define void @unroll_count_4(i32* nocapture %a) {
20 for.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
30 for.end: ; preds = %for.body
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}
38 ; #pragma clang loop unroll(full)
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
45 ; CHECK-LABEL: @unroll_full(
46 ; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_2:.*]]
47 define void @unroll_full(i32* nocapture %a, i32 %b) {
49 %cmp3 = icmp sgt i32 %b, 0
50 br i1 %cmp3, label %for.body, label %for.end, !llvm.loop !5
52 for.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
63 for.end: ; preds = %for.body, %entry
67 !6 = !{!"llvm.loop.unroll.full"}
69 ; #pragma clang loop unroll(disable)
71 ; Unroll metadata should not change.
73 ; CHECK-LABEL: @unroll_disable(
74 ; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_3:.*]]
75 define void @unroll_disable(i32* nocapture %a) {
79 for.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
89 for.end: ; preds = %for.body
93 !8 = !{!"llvm.loop.unroll.disable"}
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.
100 ; CHECK-LABEL: @shared_metadata(
103 ; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_4:.*]]
106 ; CHECK: br i1 {{.*}}, label {{.*}}, label {{.*}}, !llvm.loop ![[LOOP_5:.*]]
107 define void @shared_metadata(i32* nocapture %List) #0 {
111 for.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
121 for.body3.1.preheader: ; preds = %for.body3
122 br label %for.body3.1
124 for.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
134 for.inc5.1: ; preds = %for.body3.1
138 !10 = !{!"llvm.loop.unroll.count", i32 2}
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:.*]]}