1 ; RUN: opt -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 -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 void @recurrence_1(i32* nocapture readonly %a, i32* 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>, <vscale x 4 x i32>*
24 ; CHECK-VF4UF1: %[[SPLICE:.*]] = call <vscale x 4 x i32> @llvm.experimental.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: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
29 ; CHECK-VF4UF1: %[[VEC_RECUR_EXT:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB2]]
30 ; CHECK-VF4UF1: %[[VSCALE3:.*]] = call i32 @llvm.vscale.i32()
31 ; CHECK-VF4UF1: %[[MUL3:.*]] = mul i32 %[[VSCALE3]], 4
32 ; CHECK-VF4UF1: %[[SUB3:.*]] = sub i32 %[[MUL3]], 2
33 ; CHECK-VF4UF1: %[[VEC_RECUR_FOR_PHI:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB3]]
35 br label %for.preheader
38 %arrayidx.phi.trans.insert = getelementptr inbounds i32, i32* %a, i64 0
39 %pre_load = load i32, i32* %arrayidx.phi.trans.insert
43 %0 = phi i32 [ %pre_load, %for.preheader ], [ %1, %scalar.body ]
44 %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
45 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
46 %arrayidx32 = getelementptr inbounds i32, i32* %a, i64 %indvars.iv.next
47 %1 = load i32, i32* %arrayidx32
48 %arrayidx34 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
49 %add35 = add i32 %1, %0
50 store i32 %add35, i32* %arrayidx34
51 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
52 %exitcond = icmp eq i32 %lftr.wideiv, %n
53 br i1 %exitcond, label %for.exit, label %scalar.body, !llvm.loop !0
59 ; int recurrence_2(int *a, int n) {
61 ; for (int i = 0; i < n; ++i)
62 ; minmax = min(minmax, max(a[i] - a[i-1], 0));
66 define i32 @recurrence_2(i32* nocapture readonly %a, i32 %n) {
67 ; CHECK-VF4UF1-LABEL: @recurrence_2
68 ; CHECK-VF4UF1: vector.ph:
69 ; CHECK-VF4UF1: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
70 ; CHECK-VF4UF1: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
71 ; CHECK-VF4UF1: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
72 ; CHECK-VF4UF1: %[[VEC_RECUR_INIT:.*]] = insertelement <vscale x 4 x i32> poison, i32 %.pre, i32 %[[SUB1]]
73 ; CHECK-VF4UF1: vector.body:
74 ; CHECK-VF4UF1: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i32> [ %[[VEC_RECUR_INIT]], %vector.ph ], [ %[[LOAD:.*]], %vector.body ]
75 ; CHECK-VF4UF1: %[[LOAD]] = load <vscale x 4 x i32>, <vscale x 4 x i32>*
76 ; CHECK-VF4UF1: %[[REVERSE:.*]] = call <vscale x 4 x i32> @llvm.experimental.vector.splice.nxv4i32(<vscale x 4 x i32> %[[VEC_RECUR]], <vscale x 4 x i32> %[[LOAD]], i32 -1)
77 ; CHECK-VF4UF1: middle.block:
78 ; CHECK-VF4UF1: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
79 ; CHECK-VF4UF1: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
80 ; CHECK-VF4UF1: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
81 ; CHECK-VF4UF1: %[[VEC_RECUR_EXT:.*]] = extractelement <vscale x 4 x i32> %[[LOAD]], i32 %[[SUB2]]
83 %cmp27 = icmp sgt i32 %n, 0
84 br i1 %cmp27, label %for.preheader, label %for.cond.cleanup
87 %arrayidx2.phi.trans.insert = getelementptr inbounds i32, i32* %a, i64 -1
88 %.pre = load i32, i32* %arrayidx2.phi.trans.insert, align 4
91 for.cond.cleanup.loopexit:
92 %minmax.0.cond.lcssa = phi i32 [ %minmax.0.cond, %scalar.body ]
93 br label %for.cond.cleanup
96 %minmax.0.lcssa = phi i32 [ undef, %entry ], [ %minmax.0.cond.lcssa, %for.cond.cleanup.loopexit ]
97 ret i32 %minmax.0.lcssa
100 %0 = phi i32 [ %.pre, %for.preheader ], [ %1, %scalar.body ]
101 %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
102 %minmax.028 = phi i32 [ undef, %for.preheader ], [ %minmax.0.cond, %scalar.body ]
103 %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
104 %1 = load i32, i32* %arrayidx, align 4
105 %sub3 = sub nsw i32 %1, %0
106 %cmp4 = icmp sgt i32 %sub3, 0
107 %cond = select i1 %cmp4, i32 %sub3, i32 0
108 %cmp5 = icmp slt i32 %minmax.028, %cond
109 %minmax.0.cond = select i1 %cmp5, i32 %minmax.028, i32 %cond
110 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
111 %lftr.wideiv = trunc i64 %indvars.iv.next to i32
112 %exitcond = icmp eq i32 %lftr.wideiv, %n
113 br i1 %exitcond, label %for.cond.cleanup.loopexit, label %scalar.body, !llvm.loop !0
116 define void @recurrence_3(i16* nocapture readonly %a, double* nocapture %b, i32 %n, float %f, i16 %p) {
117 ; CHECK-VF4UF1: vector.ph:
118 ; CHECK-VF4UF1: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
119 ; CHECK-VF4UF1: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
120 ; CHECK-VF4UF1: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
121 ; CHECK-VF4UF1: %vector.recur.init = insertelement <vscale x 4 x i16> poison, i16 %0, i32 %[[SUB1]]
122 ; CHECK-VF4UF1: vector.body:
123 ; CHECK-VF4UF1: %vector.recur = phi <vscale x 4 x i16> [ %vector.recur.init, %vector.ph ], [ %[[L1:.*]], %vector.body ]
124 ; CHECK-VF4UF1: %[[L1]] = load <vscale x 4 x i16>, <vscale x 4 x i16>*
125 ; CHECK-VF4UF1: %[[SPLICE:.*]] = call <vscale x 4 x i16> @llvm.experimental.vector.splice.nxv4i16(<vscale x 4 x i16> %vector.recur, <vscale x 4 x i16> %[[L1]], i32 -1)
126 ; Check also that the casts were not moved needlessly.
127 ; CHECK-VF4UF1: sitofp <vscale x 4 x i16> %[[L1]] to <vscale x 4 x double>
128 ; CHECK-VF4UF1: sitofp <vscale x 4 x i16> %[[SPLICE]] to <vscale x 4 x double>
129 ; CHECK-VF4UF1: middle.block:
130 ; CHECK-VF4UF1: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
131 ; CHECK-VF4UF1: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
132 ; CHECK-VF4UF1: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
133 ; CHECK-VF4UF1: %vector.recur.extract = extractelement <vscale x 4 x i16> %[[L1]], i32 %[[SUB2]]
135 %0 = load i16, i16* %a, align 2
136 %conv = sitofp i16 %0 to double
137 %conv1 = fpext float %f to double
138 %conv2 = sitofp i16 %p to double
139 %mul = fmul fast double %conv2, %conv1
140 %sub = fsub fast double %conv, %mul
141 store double %sub, double* %b, align 8
142 %cmp25 = icmp sgt i32 %n, 1
143 br i1 %cmp25, label %for.preheader, label %for.end
146 br label %scalar.body
149 %1 = phi i16 [ %0, %for.preheader ], [ %2, %scalar.body ]
150 %iv = phi i64 [ %iv.next, %scalar.body ], [ 1, %for.preheader ]
151 %arrayidx5 = getelementptr inbounds i16, i16* %a, i64 %iv
152 %2 = load i16, i16* %arrayidx5, align 2
153 %conv6 = sitofp i16 %2 to double
154 %conv11 = sitofp i16 %1 to double
155 %mul12 = fmul fast double %conv11, %conv1
156 %sub13 = fsub fast double %conv6, %mul12
157 %arrayidx15 = getelementptr inbounds double, double* %b, i64 %iv
158 store double %sub13, double* %arrayidx15, align 8
159 %iv.next = add nuw nsw i64 %iv, 1
160 %lftr.wideiv = trunc i64 %iv.next to i32
161 %exitcond = icmp eq i32 %lftr.wideiv, %n
162 br i1 %exitcond, label %for.end.loopexit, label %scalar.body, !llvm.loop !0
171 define void @constant_folded_previous_value() {
172 ; CHECK-VF4UF2-LABEL: @constant_folded_previous_value
173 ; CHECK-VF4UF2: vector.body
174 ; CHECK-VF4UF2: %[[VECTOR_RECUR:.*]] = phi <vscale x 4 x i64> [ %vector.recur.init, %vector.ph ], [ shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer), %vector.body ]
175 ; CHECK-VF4UF2: %[[SPLICE1:.*]] = call <vscale x 4 x i64> @llvm.experimental.vector.splice.nxv4i64(<vscale x 4 x i64> %vector.recur, <vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer), i32 -1)
176 ; CHECK-VF4UF2: %[[SPLICE2:.*]] = call <vscale x 4 x i64> @llvm.experimental.vector.splice.nxv4i64(<vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer), <vscale x 4 x i64> shufflevector (<vscale x 4 x i64> insertelement (<vscale x 4 x i64> poison, i64 1, i32 0), <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer), i32 -1)
177 ; CHECK-VF4UF2: br i1 {{.*}}, label %middle.block, label %vector.body
179 br label %scalar.body
182 %i = phi i64 [ 0, %entry ], [ %i.next, %scalar.body ]
183 %tmp2 = phi i64 [ 0, %entry ], [ %tmp3, %scalar.body ]
185 %i.next = add nuw nsw i64 %i, 1
186 %cond = icmp eq i64 %i.next, undef
187 br i1 %cond, label %for.end, label %scalar.body, !llvm.loop !0
193 ; We vectorize this first order recurrence, by generating two
194 ; extracts for the phi `val.phi` - one at the last index and
195 ; another at the second last index. We need these 2 extracts because
196 ; the first order recurrence phi is used outside the loop, so we require the phi
197 ; itself and not its update (addx).
198 define i32 @extract_second_last_iteration(i32* %cval, i32 %x) {
199 ; CHECK-VF4UF2-LABEL: @extract_second_last_iteration
200 ; CHECK-VF4UF2: vector.ph
201 ; CHECK-VF4UF2: call i32 @llvm.vscale.i32()
202 ; CHECK-VF4UF2: call i32 @llvm.vscale.i32()
203 ; CHECK-VF4UF2: %[[VSCALE1:.*]] = call i32 @llvm.vscale.i32()
204 ; CHECK-VF4UF2: %[[MUL1:.*]] = mul i32 %[[VSCALE1]], 4
205 ; CHECK-VF4UF2: %[[SUB1:.*]] = sub i32 %[[MUL1]], 1
206 ; CHECK-VF4UF2: %[[VEC_RECUR_INIT:.*]] = insertelement <vscale x 4 x i32> poison, i32 0, i32 %[[SUB1]]
207 ; CHECK-VF4UF2: %[[SPLAT_INS1:.*]] = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
208 ; CHECK-VF4UF2: %[[SPLAT1:.*]] = shufflevector <vscale x 4 x i32> %[[SPLAT_INS1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
209 ; CHECK-VF4UF2: %[[SPLAT_INS2:.*]] = insertelement <vscale x 4 x i32> poison, i32 %x, i32 0
210 ; CHECK-VF4UF2: %[[SPLAT2:.*]] = shufflevector <vscale x 4 x i32> %[[SPLAT_INS2]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
211 ; ; CHECK-VF4UF2: vector.body
212 ; CHECK-VF4UF2: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i32> [ %[[VEC_RECUR_INIT]], %vector.ph ], [ %[[ADD2:.*]], %vector.body ]
213 ; CHECK-VF4UF2: %[[ADD1:.*]] = add <vscale x 4 x i32> %{{.*}}, %[[SPLAT1]]
214 ; CHECK-VF4UF2: %[[ADD2]] = add <vscale x 4 x i32> %{{.*}}, %[[SPLAT2]]
215 ; CHECK-VF4UF2: middle.block
216 ; CHECK-VF4UF2: %[[VSCALE2:.*]] = call i32 @llvm.vscale.i32()
217 ; CHECK-VF4UF2: %[[MUL2:.*]] = mul i32 %[[VSCALE2]], 4
218 ; CHECK-VF4UF2: %[[SUB2:.*]] = sub i32 %[[MUL2]], 1
219 ; CHECK-VF4UF2: %vector.recur.extract = extractelement <vscale x 4 x i32> %[[ADD2]], i32 %[[SUB2]]
220 ; CHECK-VF4UF2: %[[VSCALE3:.*]] = call i32 @llvm.vscale.i32()
221 ; CHECK-VF4UF2: %[[MUL3:.*]] = mul i32 %[[VSCALE3]], 4
222 ; CHECK-VF4UF2: %[[SUB3:.*]] = sub i32 %[[MUL3]], 2
223 ; CHECK-VF4UF2: %vector.recur.extract.for.phi = extractelement <vscale x 4 x i32> %[[ADD2]], i32 %[[SUB3]]
228 %inc.phi = phi i32 [ 0, %entry ], [ %inc, %for.body ]
229 %val.phi = phi i32 [ 0, %entry ], [ %addx, %for.body ]
230 %inc = add i32 %inc.phi, 1
231 %bc = zext i32 %inc.phi to i64
232 %addx = add i32 %inc.phi, %x
233 %cmp = icmp eq i32 %inc.phi, 95
234 br i1 %cmp, label %for.end, label %for.body, !llvm.loop !0
240 ; void sink_after(short *a, int n, int *b) {
241 ; for(int i = 0; i < n; i++)
242 ; b[i] = (a[i] * a[i + 1]);
245 ; Check that the sext sank after the load in the vector loop.
246 define void @sink_after(i16* %a, i32* %b, i64 %n) {
247 ; CHECK-VF4UF1-LABEL: @sink_after
248 ; CHECK-VF4UF1: vector.body
249 ; CHECK-VF4UF1: %[[VEC_RECUR:.*]] = phi <vscale x 4 x i16> [ %vector.recur.init, %vector.ph ], [ %[[LOAD:.*]], %vector.body ]
250 ; CHECK-VF4UF1: %[[LOAD]] = load <vscale x 4 x i16>, <vscale x 4 x i16>*
251 ; CHECK-VF4UF1-NEXT: %[[SPLICE:.*]] = call <vscale x 4 x i16> @llvm.experimental.vector.splice.nxv4i16(<vscale x 4 x i16> %[[VEC_RECUR]], <vscale x 4 x i16> %[[LOAD]], i32 -1)
252 ; CHECK-VF4UF1-NEXT: sext <vscale x 4 x i16> %[[SPLICE]] to <vscale x 4 x i32>
253 ; CHECK-VF4UF1-NEXT: sext <vscale x 4 x i16> %[[LOAD]] to <vscale x 4 x i32>
255 %.pre = load i16, i16* %a
259 %0 = phi i16 [ %.pre, %entry ], [ %1, %for.body ]
260 %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
261 %conv = sext i16 %0 to i32
262 %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
263 %arrayidx2 = getelementptr inbounds i16, i16* %a, i64 %indvars.iv.next
264 %1 = load i16, i16* %arrayidx2
265 %conv3 = sext i16 %1 to i32
266 %mul = mul nsw i32 %conv3, %conv
267 %arrayidx5 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
268 store i32 %mul, i32* %arrayidx5
269 %exitcond = icmp eq i64 %indvars.iv.next, %n
270 br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !0
276 !0 = distinct !{!0, !1}
277 !1 = !{!"llvm.loop.vectorize.scalable.enable", i1 true}