1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=loop-vectorize -force-vector-width=4 -S | FileCheck %s
4 ; The function finds the smallest value from a float vector.
5 ; Check if vectorization is enabled by instruction flag `fcmp nnan`.
7 define float @minloop(ptr nocapture readonly %arg) {
8 ; CHECK-LABEL: @minloop(
10 ; CHECK-NEXT: [[T:%.*]] = load float, ptr [[ARG:%.*]], align 4
11 ; CHECK-NEXT: br label [[LOOP:%.*]]
13 ; CHECK-NEXT: [[T1:%.*]] = phi i64 [ [[T7:%.*]], [[LOOP]] ], [ 1, [[TOP:%.*]] ]
14 ; CHECK-NEXT: [[T2:%.*]] = phi float [ [[T6:%.*]], [[LOOP]] ], [ [[T]], [[TOP]] ]
15 ; CHECK-NEXT: [[T3:%.*]] = getelementptr float, ptr [[ARG]], i64 [[T1]]
16 ; CHECK-NEXT: [[T4:%.*]] = load float, ptr [[T3]], align 4
17 ; CHECK-NEXT: [[T5:%.*]] = fcmp nnan olt float [[T2]], [[T4]]
18 ; CHECK-NEXT: [[T6]] = select i1 [[T5]], float [[T2]], float [[T4]]
19 ; CHECK-NEXT: [[T7]] = add i64 [[T1]], 1
20 ; CHECK-NEXT: [[T8:%.*]] = icmp eq i64 [[T7]], 65537
21 ; CHECK-NEXT: br i1 [[T8]], label [[OUT:%.*]], label [[LOOP]]
23 ; CHECK-NEXT: [[T6_LCSSA:%.*]] = phi float [ [[T6]], [[LOOP]] ]
24 ; CHECK-NEXT: ret float [[T6_LCSSA]]
27 %t = load float, ptr %arg
30 loop: ; preds = %loop, %top
31 %t1 = phi i64 [ %t7, %loop ], [ 1, %top ]
32 %t2 = phi float [ %t6, %loop ], [ %t, %top ]
33 %t3 = getelementptr float, ptr %arg, i64 %t1
34 %t4 = load float, ptr %t3, align 4
35 %t5 = fcmp nnan olt float %t2, %t4
36 %t6 = select i1 %t5, float %t2, float %t4
38 %t8 = icmp eq i64 %t7, 65537
39 br i1 %t8, label %out, label %loop
45 ; Check if vectorization is still enabled by function attribute.
47 define float @minloopattr(ptr nocapture readonly %arg) #0 {
48 ; CHECK-LABEL: @minloopattr(
50 ; CHECK-NEXT: [[T:%.*]] = load float, ptr [[ARG:%.*]], align 4
51 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
53 ; CHECK-NEXT: [[MINMAX_IDENT_SPLATINSERT:%.*]] = insertelement <4 x float> poison, float [[T]], i64 0
54 ; CHECK-NEXT: [[MINMAX_IDENT_SPLAT:%.*]] = shufflevector <4 x float> [[MINMAX_IDENT_SPLATINSERT]], <4 x float> poison, <4 x i32> zeroinitializer
55 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
57 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
58 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x float> [ [[MINMAX_IDENT_SPLAT]], [[VECTOR_PH]] ], [ [[TMP4:%.*]], [[VECTOR_BODY]] ]
59 ; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i64 1, [[INDEX]]
60 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[OFFSET_IDX]], 0
61 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr float, ptr [[ARG]], i64 [[TMP0]]
62 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr float, ptr [[TMP1]], i32 0
63 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x float>, ptr [[TMP2]], align 4
64 ; CHECK-NEXT: [[TMP3:%.*]] = fcmp olt <4 x float> [[VEC_PHI]], [[WIDE_LOAD]]
65 ; CHECK-NEXT: [[TMP4]] = select <4 x i1> [[TMP3]], <4 x float> [[VEC_PHI]], <4 x float> [[WIDE_LOAD]]
66 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
67 ; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i64 [[INDEX_NEXT]], 65536
68 ; CHECK-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
69 ; CHECK: middle.block:
70 ; CHECK-NEXT: [[TMP6:%.*]] = call float @llvm.vector.reduce.fmin.v4f32(<4 x float> [[TMP4]])
71 ; CHECK-NEXT: br i1 true, label [[OUT:%.*]], label [[SCALAR_PH]]
73 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ 65537, [[MIDDLE_BLOCK]] ], [ 1, [[TOP:%.*]] ]
74 ; CHECK-NEXT: [[BC_MERGE_RDX:%.*]] = phi float [ [[T]], [[TOP]] ], [ [[TMP6]], [[MIDDLE_BLOCK]] ]
75 ; CHECK-NEXT: br label [[LOOP:%.*]]
77 ; CHECK-NEXT: [[T1:%.*]] = phi i64 [ [[T7:%.*]], [[LOOP]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
78 ; CHECK-NEXT: [[T2:%.*]] = phi float [ [[T6:%.*]], [[LOOP]] ], [ [[BC_MERGE_RDX]], [[SCALAR_PH]] ]
79 ; CHECK-NEXT: [[T3:%.*]] = getelementptr float, ptr [[ARG]], i64 [[T1]]
80 ; CHECK-NEXT: [[T4:%.*]] = load float, ptr [[T3]], align 4
81 ; CHECK-NEXT: [[T5:%.*]] = fcmp olt float [[T2]], [[T4]]
82 ; CHECK-NEXT: [[T6]] = select i1 [[T5]], float [[T2]], float [[T4]]
83 ; CHECK-NEXT: [[T7]] = add i64 [[T1]], 1
84 ; CHECK-NEXT: [[T8:%.*]] = icmp eq i64 [[T7]], 65537
85 ; CHECK-NEXT: br i1 [[T8]], label [[OUT]], label [[LOOP]], !llvm.loop [[LOOP2:![0-9]+]]
87 ; CHECK-NEXT: [[T6_LCSSA:%.*]] = phi float [ [[T6]], [[LOOP]] ], [ [[TMP6]], [[MIDDLE_BLOCK]] ]
88 ; CHECK-NEXT: ret float [[T6_LCSSA]]
91 %t = load float, ptr %arg
94 loop: ; preds = %loop, %top
95 %t1 = phi i64 [ %t7, %loop ], [ 1, %top ]
96 %t2 = phi float [ %t6, %loop ], [ %t, %top ]
97 %t3 = getelementptr float, ptr %arg, i64 %t1
98 %t4 = load float, ptr %t3, align 4
99 %t5 = fcmp olt float %t2, %t4
100 %t6 = select i1 %t5, float %t2, float %t4
102 %t8 = icmp eq i64 %t7, 65537
103 br i1 %t8, label %out, label %loop
109 ; Check if vectorization is prevented without the flag or attribute.
111 define float @minloopnovec(ptr nocapture readonly %arg) {
112 ; CHECK-LABEL: @minloopnovec(
114 ; CHECK-NEXT: [[T:%.*]] = load float, ptr [[ARG:%.*]], align 4
115 ; CHECK-NEXT: br label [[LOOP:%.*]]
117 ; CHECK-NEXT: [[T1:%.*]] = phi i64 [ [[T7:%.*]], [[LOOP]] ], [ 1, [[TOP:%.*]] ]
118 ; CHECK-NEXT: [[T2:%.*]] = phi float [ [[T6:%.*]], [[LOOP]] ], [ [[T]], [[TOP]] ]
119 ; CHECK-NEXT: [[T3:%.*]] = getelementptr float, ptr [[ARG]], i64 [[T1]]
120 ; CHECK-NEXT: [[T4:%.*]] = load float, ptr [[T3]], align 4
121 ; CHECK-NEXT: [[T5:%.*]] = fcmp olt float [[T2]], [[T4]]
122 ; CHECK-NEXT: [[T6]] = select i1 [[T5]], float [[T2]], float [[T4]]
123 ; CHECK-NEXT: [[T7]] = add i64 [[T1]], 1
124 ; CHECK-NEXT: [[T8:%.*]] = icmp eq i64 [[T7]], 65537
125 ; CHECK-NEXT: br i1 [[T8]], label [[OUT:%.*]], label [[LOOP]]
127 ; CHECK-NEXT: [[T6_LCSSA:%.*]] = phi float [ [[T6]], [[LOOP]] ]
128 ; CHECK-NEXT: ret float [[T6_LCSSA]]
131 %t = load float, ptr %arg
134 loop: ; preds = %loop, %top
135 %t1 = phi i64 [ %t7, %loop ], [ 1, %top ]
136 %t2 = phi float [ %t6, %loop ], [ %t, %top ]
137 %t3 = getelementptr float, ptr %arg, i64 %t1
138 %t4 = load float, ptr %t3, align 4
139 %t5 = fcmp olt float %t2, %t4
140 %t6 = select i1 %t5, float %t2, float %t4
142 %t8 = icmp eq i64 %t7, 65537
143 br i1 %t8, label %out, label %loop
149 ; This test is checking that we don't vectorize when only one of the required attributes is set.
150 ; Note that this test should not vectorize even after switching to IR-level FMF.
151 define float @minloopmissingnsz(ptr nocapture readonly %arg) #1 {
152 ; CHECK-LABEL: @minloopmissingnsz(
154 ; CHECK-NEXT: [[T:%.*]] = load float, ptr [[ARG:%.*]], align 4
155 ; CHECK-NEXT: br label [[LOOP:%.*]]
157 ; CHECK-NEXT: [[T1:%.*]] = phi i64 [ [[T7:%.*]], [[LOOP]] ], [ 1, [[TOP:%.*]] ]
158 ; CHECK-NEXT: [[T2:%.*]] = phi float [ [[T6:%.*]], [[LOOP]] ], [ [[T]], [[TOP]] ]
159 ; CHECK-NEXT: [[T3:%.*]] = getelementptr float, ptr [[ARG]], i64 [[T1]]
160 ; CHECK-NEXT: [[T4:%.*]] = load float, ptr [[T3]], align 4
161 ; CHECK-NEXT: [[T5:%.*]] = fcmp olt float [[T2]], [[T4]]
162 ; CHECK-NEXT: [[T6]] = select i1 [[T5]], float [[T2]], float [[T4]]
163 ; CHECK-NEXT: [[T7]] = add i64 [[T1]], 1
164 ; CHECK-NEXT: [[T8:%.*]] = icmp eq i64 [[T7]], 65537
165 ; CHECK-NEXT: br i1 [[T8]], label [[OUT:%.*]], label [[LOOP]]
167 ; CHECK-NEXT: [[T6_LCSSA:%.*]] = phi float [ [[T6]], [[LOOP]] ]
168 ; CHECK-NEXT: ret float [[T6_LCSSA]]
171 %t = load float, ptr %arg
174 loop: ; preds = %loop, %top
175 %t1 = phi i64 [ %t7, %loop ], [ 1, %top ]
176 %t2 = phi float [ %t6, %loop ], [ %t, %top ]
177 %t3 = getelementptr float, ptr %arg, i64 %t1
178 %t4 = load float, ptr %t3, align 4
179 %t5 = fcmp olt float %t2, %t4
180 %t6 = select i1 %t5, float %t2, float %t4
182 %t8 = icmp eq i64 %t7, 65537
183 br i1 %t8, label %out, label %loop
189 ; This would assert on FMF propagation.
191 define void @not_a_min_max() {
192 ; CHECK-LABEL: @not_a_min_max(
194 ; CHECK-NEXT: br label [[LOOP:%.*]]
196 ; CHECK-NEXT: [[F9_S0_V0:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[ADD:%.*]], [[LOOP]] ]
197 ; CHECK-NEXT: [[T14:%.*]] = icmp eq i32 [[F9_S0_V0]], 5
198 ; CHECK-NEXT: [[T15:%.*]] = select reassoc nnan ninf nsz contract afn i1 [[T14]], float 0x36A0000000000000, float 0.000000e+00
199 ; CHECK-NEXT: [[ADD]] = add nuw nsw i32 [[F9_S0_V0]], 1
200 ; CHECK-NEXT: br i1 false, label [[END:%.*]], label [[LOOP]]
202 ; CHECK-NEXT: ret void
208 %f9.s0.v0 = phi i32 [ 0, %entry ], [ %add, %loop ]
209 %t14 = icmp eq i32 %f9.s0.v0, 5
210 %t15 = select reassoc nnan ninf nsz contract afn i1 %t14, float 0x36A0000000000000, float 0.0
211 %add = add nuw nsw i32 %f9.s0.v0, 1
212 br i1 false, label %end, label %loop
218 attributes #0 = { "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" }
219 attributes #1 = { "no-nans-fp-math"="true" }