1 ; RUN: opt -passes=loop-vectorize -scalable-vectorization=on -force-vector-width=4 -force-vector-interleave=1 -force-target-supports-scalable-vectors=true -S < %s | FileCheck %s --check-prefix=CHECK-VF4UF1
2 ; RUN: opt -passes=loop-vectorize -scalable-vectorization=on -force-vector-width=4 -force-vector-interleave=2 -force-target-supports-scalable-vectors=true -S < %s | FileCheck %s --check-prefix=CHECK-VF4UF2
4 ; void recurrence_1(int *a, int *b, int n) {
5 ; for(int i = 0; i < n; i++)
6 ; b[i] = a[i] + a[i - 1]
9 define i32 @recurrence_1(ptr nocapture readonly %a, ptr nocapture %b, i32 %n) {
10 ; CHECK-VF4UF1-LABEL: @recurrence_1
11 ; CHECK-VF4UF1: for.preheader
12 ; CHECK-VF4UF1: %[[SUB_1:.*]] = add i32 %n, -1
13 ; CHECK-VF4UF1: %[[ZEXT:.*]] = zext i32 %[[SUB_1]] to i64
14 ; CHECK-VF4UF1: %[[ADD:.*]] = add nuw nsw i64 %[[ZEXT]], 1
15 ; CHECK-VF4UF1: vector.ph:
16 ; CHECK-VF4UF1: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
17 ; CHECK-VF4UF1: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
18 ; CHECK-VF4UF1: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
19 ; CHECK-VF4UF1: %[[VEC_RECUR_INIT:.*]] = insertelement <vscale x 4 x i32> poison, i32 %pre_load, i32 %[[SUB1]]
20 ; CHECK-VF4UF1: vector.body:
21 ; CHECK-VF4UF1: %[[INDEX:.*]] = phi i64 [ 0, %vector.ph ], [ %[[NEXT_IDX:.*]], %vector.body ]
22 ; CHECK-VF4UF1: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i32> [ %[[VEC_RECUR_INIT]], %vector.ph ], [ %[[LOAD:.*]], %vector.body ]
23 ; CHECK-VF4UF1: %[[LOAD]] = load <vscale x 4 x i32>, ptr
24 ; CHECK-VF4UF1: %[[SPLICE:.*]] = call <vscale x 4 x i32> @llvm.vector.splice.nxv4i32(<vscale x 4 x i32> %[[VEC_RECUR]], <vscale x 4 x i32> %[[LOAD]], i32 -1)
25 ; CHECK-VF4UF1: middle.block:
26 ; CHECK-VF4UF1: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
27 ; CHECK-VF4UF1: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
28 ; CHECK-VF4UF1: %[[SUB3:.*]] = sub i32 %[[MUL2]], 2
29 ; CHECK-VF4UF1: %[[VEC_RECUR_FOR_PHI:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB3]]
30 ; CHECK-VF4UF1: %[[VSCALE3:.*]] = call i32 @llvm.vscale.i32()
31 ; CHECK-VF4UF1: %[[MUL3:.*]] = mul i32 %[[VSCALE3]], 4
32 ; CHECK-VF4UF1: %[[SUB3:.*]] = sub i32 %[[MUL3]], 1
33 ; CHECK-VF4UF1: %[[VEC_RECUR_EXT:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB3]]
35 br label %for.preheader
38 %pre_load = load i32, ptr %a
42 %0 = phi i32 [ %pre_load, %for.preheader ], [ %1, %scalar.body ]
43 %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
44 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
45 %arrayidx32 = getelementptr inbounds i32, ptr %a, i64 %indvars.iv.next
46 %1 = load i32, ptr %arrayidx32
47 %arrayidx34 = getelementptr inbounds i32, ptr %b, i64 %indvars.iv
48 %add35 = add i32 %1, %0
49 store i32 %add35, ptr %arrayidx34
50 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
51 %exitcond = icmp eq i32 %lftr.wideiv, %n
52 br i1 %exitcond, label %for.exit, label %scalar.body, !llvm.loop !0
58 ; int recurrence_2(int *a, int n) {
60 ; for (int i = 0; i < n; ++i)
61 ; minmax = min(minmax, max(a[i] - a[i-1], 0));
65 define i32 @recurrence_2(ptr nocapture readonly %a, i32 %n) {
66 ; CHECK-VF4UF1-LABEL: @recurrence_2
67 ; CHECK-VF4UF1: vector.ph:
68 ; CHECK-VF4UF1: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
69 ; CHECK-VF4UF1: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
70 ; CHECK-VF4UF1: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
71 ; CHECK-VF4UF1: %[[VEC_RECUR_INIT:.*]] = insertelement <vscale x 4 x i32> poison, i32 %.pre, i32 %[[SUB1]]
72 ; CHECK-VF4UF1: vector.body:
73 ; CHECK-VF4UF1: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i32> [ %[[VEC_RECUR_INIT]], %vector.ph ], [ %[[LOAD:.*]], %vector.body ]
74 ; CHECK-VF4UF1: %[[LOAD]] = load <vscale x 4 x i32>, ptr
75 ; CHECK-VF4UF1: %[[REVERSE:.*]] = call <vscale x 4 x i32> @llvm.vector.splice.nxv4i32(<vscale x 4 x i32> %[[VEC_RECUR]], <vscale x 4 x i32> %[[LOAD]], i32 -1)
76 ; CHECK-VF4UF1: middle.block:
77 ; CHECK-VF4UF1: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
78 ; CHECK-VF4UF1: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
79 ; CHECK-VF4UF1: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
80 ; CHECK-VF4UF1: %[[VEC_RECUR_EXT:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB2]]
82 %cmp27 = icmp sgt i32 %n, 0
83 br i1 %cmp27, label %for.preheader, label %for.cond.cleanup
86 %arrayidx2.phi.trans.insert = getelementptr inbounds i32, ptr %a, i64 -1
87 %.pre = load i32, ptr %arrayidx2.phi.trans.insert, align 4
90 for.cond.cleanup.loopexit:
91 %minmax.0.cond.lcssa = phi i32 [ %minmax.0.cond, %scalar.body ]
92 br label %for.cond.cleanup
95 %minmax.0.lcssa = phi i32 [ undef, %entry ], [ %minmax.0.cond.lcssa, %for.cond.cleanup.loopexit ]
96 ret i32 %minmax.0.lcssa
99 %0 = phi i32 [ %.pre, %for.preheader ], [ %1, %scalar.body ]
100 %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
101 %minmax.028 = phi i32 [ undef, %for.preheader ], [ %minmax.0.cond, %scalar.body ]
102 %arrayidx = getelementptr inbounds i32, ptr %a, i64 %indvars.iv
103 %1 = load i32, ptr %arrayidx, align 4
104 %sub3 = sub nsw i32 %1, %0
105 %cmp4 = icmp sgt i32 %sub3, 0
106 %cond = select i1 %cmp4, i32 %sub3, i32 0
107 %cmp5 = icmp slt i32 %minmax.028, %cond
108 %minmax.0.cond = select i1 %cmp5, i32 %minmax.028, i32 %cond
109 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
110 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
111 %exitcond = icmp eq i32 %lftr.wideiv, %n
112 br i1 %exitcond, label %for.cond.cleanup.loopexit, label %scalar.body, !llvm.loop !0
115 define void @recurrence_3(ptr nocapture readonly %a, ptr nocapture %b, i32 %n, float %f, i16 %p) {
116 ; CHECK-VF4UF1: vector.ph:
117 ; CHECK-VF4UF1: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
118 ; CHECK-VF4UF1: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
119 ; CHECK-VF4UF1: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
120 ; CHECK-VF4UF1: %vector.recur.init = insertelement <vscale x 4 x i16> poison, i16 %0, i32 %[[SUB1]]
121 ; CHECK-VF4UF1: vector.body:
122 ; CHECK-VF4UF1: %vector.recur = phi <vscale x 4 x i16> [ %vector.recur.init, %vector.ph ], [ %[[L1:.*]], %vector.body ]
123 ; CHECK-VF4UF1: %[[L1]] = load <vscale x 4 x i16>, ptr
124 ; CHECK-VF4UF1: %[[SPLICE:.*]] = call <vscale x 4 x i16> @llvm.vector.splice.nxv4i16(<vscale x 4 x i16> %vector.recur, <vscale x 4 x i16> %[[L1]], i32 -1)
125 ; Check also that the casts were not moved needlessly.
126 ; CHECK-VF4UF1: sitofp <vscale x 4 x i16> %[[L1]] to <vscale x 4 x double>
127 ; CHECK-VF4UF1: sitofp <vscale x 4 x i16> %[[SPLICE]] to <vscale x 4 x double>
128 ; CHECK-VF4UF1: middle.block:
129 ; CHECK-VF4UF1: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
130 ; CHECK-VF4UF1: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
131 ; CHECK-VF4UF1: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
132 ; CHECK-VF4UF1: %vector.recur.extract = extractelement <vscale x 4 x i16> %[[L1]], i32 %[[SUB2]]
134 %0 = load i16, ptr %a, align 2
135 %conv = sitofp i16 %0 to double
136 %conv1 = fpext float %f to double
137 %conv2 = sitofp i16 %p to double
138 %mul = fmul fast double %conv2, %conv1
139 %sub = fsub fast double %conv, %mul
140 store double %sub, ptr %b, align 8
141 %cmp25 = icmp sgt i32 %n, 1
142 br i1 %cmp25, label %for.preheader, label %for.end
145 br label %scalar.body
148 %1 = phi i16 [ %0, %for.preheader ], [ %2, %scalar.body ]
149 %iv = phi i64 [ %iv.next, %scalar.body ], [ 1, %for.preheader ]
150 %arrayidx5 = getelementptr inbounds i16, ptr %a, i64 %iv
151 %2 = load i16, ptr %arrayidx5, align 2
152 %conv6 = sitofp i16 %2 to double
153 %conv11 = sitofp i16 %1 to double
154 %mul12 = fmul fast double %conv11, %conv1
155 %sub13 = fsub fast double %conv6, %mul12
156 %arrayidx15 = getelementptr inbounds double, ptr %b, i64 %iv
157 store double %sub13, ptr %arrayidx15, align 8
158 %iv.next = add nuw nsw i64 %iv, 1
159 %lftr.wideiv = trunc i64 %iv.next to i32
160 %exitcond = icmp eq i32 %lftr.wideiv, %n
161 br i1 %exitcond, label %for.end.loopexit, label %scalar.body, !llvm.loop !0
170 define i64 @constant_folded_previous_value() {
171 ; CHECK-VF4UF2-LABEL: @constant_folded_previous_value
172 ; CHECK-VF4UF2: vector.body
173 ; CHECK-VF4UF2: %[[VECTOR_RECUR:.*]] = phi <vscale x 4 x i64> [ %vector.recur.init, %vector.ph ], [ splat (i64 1), %vector.body ]
174 ; CHECK-VF4UF2: br i1 {{.*}}, label %middle.block, label %vector.body
176 br label %scalar.body
179 %i = phi i64 [ 0, %entry ], [ %i.next, %scalar.body ]
180 %tmp2 = phi i64 [ 0, %entry ], [ %tmp3, %scalar.body ]
182 %i.next = add nuw nsw i64 %i, 1
183 %cond = icmp eq i64 %i.next, undef
184 br i1 %cond, label %for.end, label %scalar.body, !llvm.loop !0
190 ; We vectorize this first order recurrence, by generating two
191 ; extracts for the phi `val.phi` - one at the last index and
192 ; another at the second last index. We need these 2 extracts because
193 ; the first order recurrence phi is used outside the loop, so we require the phi
194 ; itself and not its update (addx).
195 define i32 @extract_second_last_iteration(ptr %cval, i32 %x) {
196 ; CHECK-VF4UF2-LABEL: @extract_second_last_iteration
197 ; CHECK-VF4UF2: vector.ph
198 ; CHECK-VF4UF2: call i32 @llvm.vscale.i32()
199 ; CHECK-VF4UF2: call i32 @llvm.vscale.i32()
200 ; CHECK-VF4UF2: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
201 ; CHECK-VF4UF2: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
202 ; CHECK-VF4UF2: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
203 ; CHECK-VF4UF2: %[[VEC_RECUR_INIT:.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 %[[SUB1]]
204 ; CHECK-VF4UF2: %[[SPLAT_INS1:.*]] = insertelement <vscale x 4 x i32> poison, i32 %x, i64 0
205 ; CHECK-VF4UF2: %[[SPLAT1:.*]] = shufflevector <vscale x 4 x i32> %[[SPLAT_INS1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
206 ; ; CHECK-VF4UF2: vector.body
207 ; CHECK-VF4UF2: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i32> [ %[[VEC_RECUR_INIT]], %vector.ph ], [ %[[ADD2:.*]], %vector.body ]
208 ; CHECK-VF4UF2: %[[ADD1:.*]] = add <vscale x 4 x i32> %{{.*}}, %[[SPLAT1]]
209 ; CHECK-VF4UF2: middle.block
210 ; CHECK-VF4UF2: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
211 ; CHECK-VF4UF2: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
212 ; CHECK-VF4UF2: %[[SUB3:.*]] = sub i32 %[[MUL2]], 2
213 ; CHECK-VF4UF2: %vector.recur.extract.for.phi = extractelement <vscale x 4 x i32> %[[ADD2]], i32 %[[SUB3]]
214 ; CHECK-VF4UF2: %[[VSCALE3:.*]] = call i32 @llvm.vscale.i32()
215 ; CHECK-VF4UF2: %[[MUL3:.*]] = mul i32 %[[VSCALE3]], 4
216 ; CHECK-VF4UF2: %[[SUB2:.*]] = sub i32 %[[MUL3]], 1
217 ; CHECK-VF4UF2: %vector.recur.extract = extractelement <vscale x 4 x i32> %[[ADD2]], i32 %[[SUB2]]
222 %inc.phi = phi i32 [ 0, %entry ], [ %inc, %for.body ]
223 %val.phi = phi i32 [ 0, %entry ], [ %addx, %for.body ]
224 %inc = add i32 %inc.phi, 1
225 %bc = zext i32 %inc.phi to i64
226 %addx = add i32 %inc.phi, %x
227 %cmp = icmp eq i32 %inc.phi, 95
228 br i1 %cmp, label %for.end, label %for.body, !llvm.loop !0
234 ; void sink_after(short *a, int n, int *b) {
235 ; for(int i = 0; i < n; i++)
236 ; b[i] = (aptr a[i + 1]);
239 ; Check that the sext sank after the load in the vector loop.
240 define void @sink_after(ptr %a, ptr %b, i64 %n) {
241 ; CHECK-VF4UF1-LABEL: @sink_after
242 ; CHECK-VF4UF1: vector.body
243 ; CHECK-VF4UF1: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i16> [ %vector.recur.init, %vector.ph ], [ %[[LOAD:.*]], %vector.body ]
244 ; CHECK-VF4UF1: %[[LOAD]] = load <vscale x 4 x i16>, ptr
245 ; CHECK-VF4UF1-NEXT: %[[SPLICE:.*]] = call <vscale x 4 x i16> @llvm.vector.splice.nxv4i16(<vscale x 4 x i16> %[[VEC_RECUR]], <vscale x 4 x i16> %[[LOAD]], i32 -1)
246 ; CHECK-VF4UF1-NEXT: sext <vscale x 4 x i16> %[[SPLICE]] to <vscale x 4 x i32>
247 ; CHECK-VF4UF1-NEXT: sext <vscale x 4 x i16> %[[LOAD]] to <vscale x 4 x i32>
249 %.pre = load i16, ptr %a
253 %0 = phi i16 [ %.pre, %entry ], [ %1, %for.body ]
254 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
255 %conv = sext i16 %0 to i32
256 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
257 %arrayidx2 = getelementptr inbounds i16, ptr %a, i64 %indvars.iv.next
258 %1 = load i16, ptr %arrayidx2
259 %conv3 = sext i16 %1 to i32
260 %mul = mul nsw i32 %conv3, %conv
261 %arrayidx5 = getelementptr inbounds i32, ptr %b, i64 %indvars.iv
262 store i32 %mul, ptr %arrayidx5
263 %exitcond = icmp eq i64 %indvars.iv.next, %n
264 br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !0
270 !0 = distinct !{!0, !1}
271 !1 = !{!"llvm.loop.vectorize.scalable.enable", i1 true}