1; RUN: opt -mtriple=thumbv8.1m.main -mve-tail-predication -tail-predication=enabled -mattr=+mve %s -S -o - | FileCheck %s
2
3; CHECK-LABEL: reduction_i32
4; CHECK: phi i32 [ 0, %vector.ph ]
5; CHECK: phi <8 x i16> [ zeroinitializer, %vector.ph ]
6; CHECK: phi i32
7; CHECK: [[PHI:%[^ ]+]] = phi i32 [ %N, %vector.ph ], [ [[ELEMS:%[^ ]+]], %vector.body ]
8; CHECK: [[VCTP:%[^ ]+]] = call <8 x i1> @llvm.arm.mve.vctp16(i32 [[PHI]])
9; CHECK: [[ELEMS]] = sub i32 [[PHI]], 8
10; CHECK: call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp4, i32 4, <8 x i1> [[VCTP]], <8 x i16> undef)
11; CHECK: call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp6, i32 4, <8 x i1> [[VCTP]], <8 x i16> undef)
12define i16 @reduction_i32(i16* nocapture readonly %A, i16* nocapture readonly %B, i32 %N) {
13entry:
14  %cmp8 = icmp eq i32 %N, 0
15  br i1 %cmp8, label %for.cond.cleanup, label %vector.ph
16
17vector.ph:
18  %tmp = add i32 %N, -1
19  %n.rnd.up = add i32 %tmp, 8
20  %n.vec = and i32 %n.rnd.up, -8
21  %broadcast.splatinsert1 = insertelement <8 x i32> undef, i32 %tmp, i32 0
22  %broadcast.splat2 = shufflevector <8 x i32> %broadcast.splatinsert1, <8 x i32> undef, <8 x i32> zeroinitializer
23  %0 = add i32 %n.vec, -8
24  %1 = lshr i32 %0, 3
25  %2 = add i32 %1, 1
26  %start = call i32 @llvm.start.loop.iterations.i32(i32 %2)
27  br label %vector.body
28
29vector.body:                                      ; preds = %vector.body, %vector.ph
30  %index = phi i32 [ 0, %vector.ph], [ %index.next, %vector.body ]
31  %vec.phi = phi <8 x i16> [ zeroinitializer, %vector.ph], [ %tmp8, %vector.body ]
32  %3 = phi i32 [ %start, %vector.ph], [ %4, %vector.body ]
33  %broadcast.splatinsert = insertelement <8 x i32> undef, i32 %index, i32 0
34  %broadcast.splat = shufflevector <8 x i32> %broadcast.splatinsert, <8 x i32> undef, <8 x i32> zeroinitializer
35  %induction = add <8 x i32> %broadcast.splat, <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
36  %tmp2 = getelementptr inbounds i16, i16* %A, i32 %index
37
38  ; %tmp3 = icmp ule <8 x i32> %induction, %broadcast.splat2
39  %tmp3 = call <8 x i1> @llvm.get.active.lane.mask.v8i1.i32(i32 %index, i32 %N)
40
41  %tmp4 = bitcast i16* %tmp2 to <8 x i16>*
42  %wide.masked.load = call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp4, i32 4, <8 x i1> %tmp3, <8 x i16> undef)
43  %tmp5 = getelementptr inbounds i16, i16* %B, i32 %index
44  %tmp6 = bitcast i16* %tmp5 to <8 x i16>*
45  %wide.masked.load3 = call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp6, i32 4, <8 x i1> %tmp3, <8 x i16> undef)
46  %tmp7 = add <8 x i16> %wide.masked.load, %vec.phi
47  %tmp8 = add <8 x i16> %tmp7, %wide.masked.load3
48  %index.next = add i32 %index, 8
49  %4 = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %3, i32 1)
50  %5 = icmp ne i32 %4, 0
51  br i1 %5, label %vector.body, label %middle.block
52
53middle.block:                                     ; preds = %vector.body
54  %vec.phi.lcssa = phi <8 x i16> [ %vec.phi, %vector.body ]
55  %.lcssa3 = phi <8 x i1> [ %tmp3, %vector.body ]
56  %.lcssa = phi <8 x i16> [ %tmp8, %vector.body ]
57  %tmp10 = select <8 x i1> %.lcssa3, <8 x i16> %.lcssa, <8 x i16> %vec.phi.lcssa
58  %rdx.shuf = shufflevector <8 x i16> %tmp10, <8 x i16> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef>
59  %bin.rdx = add <8 x i16> %rdx.shuf, %tmp10
60  %rdx.shuf4 = shufflevector <8 x i16> %bin.rdx, <8 x i16> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
61  %bin.rdx5 = add <8 x i16> %rdx.shuf4, %bin.rdx
62  %rdx.shuf6 = shufflevector <8 x i16> %bin.rdx5, <8 x i16> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
63  %bin.rdx7 = add <8 x i16> %rdx.shuf6, %bin.rdx5
64  %tmp11 = extractelement <8 x i16> %bin.rdx7, i32 0
65  ret i16 %tmp11
66
67for.cond.cleanup:
68  %res.0 = phi i16 [ 0, %entry ]
69  ret i16 %res.0
70}
71
72; CHECK-LABEL: reduction_i32_with_scalar
73; CHECK: vector.body:
74; CHECK: %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
75; CHECK: %vec.phi = phi <8 x i16> [ zeroinitializer, %vector.ph ], [ %{{.*}}, %vector.body ]
76; CHECK: %{{.*}} = phi i32 [ %{{.*}}, %vector.ph ], [ %{{.*}}, %vector.body ]
77; CHECK: [[PHI:%[^ ]+]] = phi i32 [ %N, %vector.ph ], [ [[ELEMS:%[^ ]+]], %vector.body ]
78; CHECK: [[VCTP:%[^ ]+]] = call <8 x i1> @llvm.arm.mve.vctp16(i32 [[PHI]])
79; CHECK: [[ELEMS]] = sub i32 [[PHI]], 8
80; CHECK: call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp4, i32 4, <8 x i1> [[VCTP]], <8 x i16> undef)
81define i16 @reduction_i32_with_scalar(i16* nocapture readonly %A, i16 %B, i32 %N) local_unnamed_addr {
82entry:
83  %cmp8 = icmp eq i32 %N, 0
84  br i1 %cmp8, label %for.cond.cleanup, label %vector.ph
85
86vector.ph:
87  %tmp = add i32 %N, -1
88  %n.rnd.up = add nuw nsw i32 %tmp, 8
89  %n.vec = and i32 %n.rnd.up, -8
90  %broadcast.splatinsert1 = insertelement <8 x i32> undef, i32 %tmp, i32 0
91  %broadcast.splat2 = shufflevector <8 x i32> %broadcast.splatinsert1, <8 x i32> undef, <8 x i32> zeroinitializer
92  %broadcast.splatinsert3 = insertelement <8 x i16> undef, i16 %B, i32 0
93  %broadcast.splat4 = shufflevector <8 x i16> %broadcast.splatinsert3, <8 x i16> undef, <8 x i32> zeroinitializer
94  %0 = add i32 %n.vec, -8
95  %1 = lshr i32 %0, 3
96  %2 = add nuw nsw i32 %1, 1
97  %start = call i32 @llvm.start.loop.iterations.i32(i32 %2)
98  br label %vector.body
99
100vector.body:                                      ; preds = %vector.body, %vector.ph
101  %index = phi i32 [ 0, %vector.ph], [ %index.next, %vector.body ]
102  %vec.phi = phi <8 x i16> [ zeroinitializer, %vector.ph], [ %tmp6, %vector.body ]
103  %3 = phi i32 [ %start, %vector.ph], [ %4, %vector.body ]
104  %broadcast.splatinsert = insertelement <8 x i32> undef, i32 %index, i32 0
105  %broadcast.splat = shufflevector <8 x i32> %broadcast.splatinsert, <8 x i32> undef, <8 x i32> zeroinitializer
106  %induction = add <8 x i32> %broadcast.splat, <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
107  %tmp2 = getelementptr inbounds i16, i16* %A, i32 %index
108
109  ; %tmp3 = icmp ule <8 x i32> %induction, %broadcast.splat2
110  %tmp3 = call <8 x i1> @llvm.get.active.lane.mask.v8i1.i32(i32 %index, i32 %N)
111
112  %tmp4 = bitcast i16* %tmp2 to <8 x i16>*
113  %wide.masked.load = call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp4, i32 4, <8 x i1> %tmp3, <8 x i16> undef)
114  %tmp5 = add <8 x i16> %vec.phi, %broadcast.splat4
115  %tmp6 = add <8 x i16> %tmp5, %wide.masked.load
116  %index.next = add nuw nsw i32 %index, 8
117  %4 = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %3, i32 1)
118  %5 = icmp ne i32 %4, 0
119  br i1 %5, label %vector.body, label %middle.block
120
121middle.block:                                     ; preds = %vector.body
122  %tmp8 = select <8 x i1> %tmp3, <8 x i16> %tmp6, <8 x i16> %vec.phi
123  %rdx.shuf = shufflevector <8 x i16> %tmp8, <8 x i16> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef>
124  %bin.rdx = add <8 x i16> %rdx.shuf, %tmp8
125  %rdx.shuf5 = shufflevector <8 x i16> %bin.rdx, <8 x i16> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
126  %bin.rdx6 = add <8 x i16> %rdx.shuf5, %bin.rdx
127  %rdx.shuf7 = shufflevector <8 x i16> %bin.rdx6, <8 x i16> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
128  %bin.rdx8 = add <8 x i16> %rdx.shuf7, %bin.rdx6
129  %tmp9 = extractelement <8 x i16> %bin.rdx8, i32 0
130  ret i16 %tmp9
131
132for.cond.cleanup:
133  %res.0 = phi i16 [ 0, %entry ]
134  ret i16 %res.0
135}
136
137; The vector loop is not guarded with an entry check (N == 0). Check that
138; despite this we can still calculate a precise enough range so that the
139; the overflow checks for get.active.active.lane.mask don't reject
140; tail-predication.
141;
142; CHECK-LABEL: @reduction_not_guarded
143;
144; CHECK:     vector.body:
145; CHECK:     @llvm.arm.mve.vctp
146; CHECK-NOT: @llvm.get.active.lane.mask.v8i1.i32
147; CHECK:     ret
148;
149define i16 @reduction_not_guarded(i16* nocapture readonly %A, i16 %B, i32 %N) local_unnamed_addr {
150entry:
151  %tmp = add i32 %N, -1
152  %n.rnd.up = add nuw nsw i32 %tmp, 8
153  %n.vec = and i32 %n.rnd.up, -8
154  %broadcast.splatinsert1 = insertelement <8 x i32> undef, i32 %tmp, i32 0
155  %broadcast.splat2 = shufflevector <8 x i32> %broadcast.splatinsert1, <8 x i32> undef, <8 x i32> zeroinitializer
156  %broadcast.splatinsert3 = insertelement <8 x i16> undef, i16 %B, i32 0
157  %broadcast.splat4 = shufflevector <8 x i16> %broadcast.splatinsert3, <8 x i16> undef, <8 x i32> zeroinitializer
158  %0 = add i32 %n.vec, -8
159  %1 = lshr i32 %0, 3
160  %2 = add nuw nsw i32 %1, 1
161  %start = call i32 @llvm.start.loop.iterations.i32(i32 %2)
162  br label %vector.body
163
164vector.body:                                      ; preds = %vector.body, %vector.ph
165  %index = phi i32 [ 0, %entry], [ %index.next, %vector.body ]
166  %vec.phi = phi <8 x i16> [ zeroinitializer, %entry], [ %tmp6, %vector.body ]
167  %3 = phi i32 [ %start, %entry ], [ %4, %vector.body ]
168  %broadcast.splatinsert = insertelement <8 x i32> undef, i32 %index, i32 0
169  %broadcast.splat = shufflevector <8 x i32> %broadcast.splatinsert, <8 x i32> undef, <8 x i32> zeroinitializer
170  %induction = add <8 x i32> %broadcast.splat, <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
171  %tmp2 = getelementptr inbounds i16, i16* %A, i32 %index
172
173  ; %tmp3 = icmp ule <8 x i32> %induction, %broadcast.splat2
174  %tmp3 = call <8 x i1> @llvm.get.active.lane.mask.v8i1.i32(i32 %index, i32 %N)
175
176  %tmp4 = bitcast i16* %tmp2 to <8 x i16>*
177  %wide.masked.load = call <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>* %tmp4, i32 4, <8 x i1> %tmp3, <8 x i16> undef)
178  %tmp5 = add <8 x i16> %vec.phi, %broadcast.splat4
179  %tmp6 = add <8 x i16> %tmp5, %wide.masked.load
180  %index.next = add nuw nsw i32 %index, 8
181  %4 = call i32 @llvm.loop.decrement.reg.i32.i32.i32(i32 %3, i32 1)
182  %5 = icmp ne i32 %4, 0
183  br i1 %5, label %vector.body, label %middle.block
184
185middle.block:                                     ; preds = %vector.body
186  %tmp8 = select <8 x i1> %tmp3, <8 x i16> %tmp6, <8 x i16> %vec.phi
187  %rdx.shuf = shufflevector <8 x i16> %tmp8, <8 x i16> undef, <8 x i32> <i32 4, i32 5, i32 6, i32 7, i32 undef, i32 undef, i32 undef, i32 undef>
188  %bin.rdx = add <8 x i16> %rdx.shuf, %tmp8
189  %rdx.shuf5 = shufflevector <8 x i16> %bin.rdx, <8 x i16> undef, <8 x i32> <i32 2, i32 3, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
190  %bin.rdx6 = add <8 x i16> %rdx.shuf5, %bin.rdx
191  %rdx.shuf7 = shufflevector <8 x i16> %bin.rdx6, <8 x i16> undef, <8 x i32> <i32 1, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef, i32 undef>
192  %bin.rdx8 = add <8 x i16> %rdx.shuf7, %bin.rdx6
193  %tmp9 = extractelement <8 x i16> %bin.rdx8, i32 0
194  ret i16 %tmp9
195}
196
197; CHECK-LABEL: @Correlation
198; CHECK:       vector.body:
199; CHECK:       @llvm.arm.mve.vctp
200; CHECK-NOT:   %active.lane.mask = call <4 x i1> @llvm.get.active.lane.mask
201;
202define dso_local void @Correlation(i16* nocapture readonly %Input, i16* nocapture %Output, i16 signext %Size, i16 signext %N, i16 signext %Scale) local_unnamed_addr #0 {
203entry:
204  %conv = sext i16 %N to i32
205  %cmp36 = icmp sgt i16 %N, 0
206  br i1 %cmp36, label %for.body.lr.ph, label %for.end17
207
208for.body.lr.ph:
209  %conv2 = sext i16 %Size to i32
210  %conv1032 = zext i16 %Scale to i32
211  %0 = add i32 %conv2, 3
212  br label %for.body
213
214for.body:
215  %lsr.iv51 = phi i32 [ %lsr.iv.next, %for.end ], [ %0, %for.body.lr.ph ]
216  %lsr.iv46 = phi i16* [ %scevgep47, %for.end ], [ %Input, %for.body.lr.ph ]
217  %i.037 = phi i32 [ 0, %for.body.lr.ph ], [ %inc16, %for.end ]
218  %1 = mul nsw i32 %i.037, -1
219  %2 = add i32 %0, %1
220  %3 = lshr i32 %2, 2
221  %4 = shl nuw i32 %3, 2
222  %5 = add i32 %4, -4
223  %6 = lshr i32 %5, 2
224  %7 = add nuw nsw i32 %6, 1
225  %8 = sub i32 %conv2, %i.037
226  %cmp433 = icmp slt i32 %i.037, %conv2
227  br i1 %cmp433, label %vector.ph, label %for.end
228
229vector.ph:                                        ; preds = %for.body
230  %trip.count.minus.1 = add i32 %8, -1
231  %start = call i32 @llvm.start.loop.iterations.i32(i32 %7)
232  br label %vector.body
233
234vector.body:                                      ; preds = %vector.body, %vector.ph
235  %lsr.iv48 = phi i16* [ %scevgep49, %vector.body ], [ %lsr.iv46, %vector.ph ]
236  %lsr.iv = phi i16* [ %scevgep, %vector.body ], [ %Input, %vector.ph ]
237  %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
238  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %16, %vector.body ]
239  %9 = phi i32 [ %start, %vector.ph ], [ %17, %vector.body ]
240  %lsr.iv4850 = bitcast i16* %lsr.iv48 to <4 x i16>*
241  %lsr.iv45 = bitcast i16* %lsr.iv to <4 x i16>*
242  %active.lane.mask = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32 %index, i32 %8)
243  %wide.masked.load = call <4 x i16> @llvm.masked.load.v4i16.p0v4i16(<4 x i16>* %lsr.iv45, i32 2, <4 x i1> %active.lane.mask, <4 x i16> undef)
244  %10 = sext <4 x i16> %wide.masked.load to <4 x i32>
245  %wide.masked.load42 = call <4 x i16> @llvm.masked.load.v4i16.p0v4i16(<4 x i16>* %lsr.iv4850, i32 2, <4 x i1> %active.lane.mask, <4 x i16> undef)
246  %11 = sext <4 x i16> %wide.masked.load42 to <4 x i32>
247  %12 = mul nsw <4 x i32> %11, %10
248  %13 = insertelement <4 x i32> undef, i32 %conv1032, i32 0
249  %14 = shufflevector <4 x i32> %13, <4 x i32> undef, <4 x i32> zeroinitializer
250  %15 = ashr <4 x i32> %12, %14
251  %16 = add <4 x i32> %15, %vec.phi
252  %index.next = add i32 %index, 4
253  %scevgep = getelementptr i16, i16* %lsr.iv, i32 4
254  %scevgep49 = getelementptr i16, i16* %lsr.iv48, i32 4
255  %17 = call i32 @llvm.loop.decrement.reg.i32(i32 %9, i32 1)
256  %18 = icmp ne i32 %17, 0
257  br i1 %18, label %vector.body, label %middle.block
258
259middle.block:                                     ; preds = %vector.body
260  %19 = select <4 x i1> %active.lane.mask, <4 x i32> %16, <4 x i32> %vec.phi
261  %20 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %19)
262  br label %for.end
263
264for.end:                                          ; preds = %middle.block, %for.body
265  %Sum.0.lcssa = phi i32 [ 0, %for.body ], [ %20, %middle.block ]
266  %21 = lshr i32 %Sum.0.lcssa, 16
267  %conv13 = trunc i32 %21 to i16
268  %arrayidx14 = getelementptr inbounds i16, i16* %Output, i32 %i.037
269  store i16 %conv13, i16* %arrayidx14, align 2
270  %inc16 = add nuw nsw i32 %i.037, 1
271  %scevgep47 = getelementptr i16, i16* %lsr.iv46, i32 1
272  %lsr.iv.next = add i32 %lsr.iv51, -1
273  %exitcond39 = icmp eq i32 %inc16, %conv
274  br i1 %exitcond39, label %for.end17, label %for.body
275
276for.end17:                                        ; preds = %for.end, %entry
277  ret void
278}
279
280declare <8 x i16> @llvm.masked.load.v8i16.p0v8i16(<8 x i16>*, i32 immarg, <8 x i1>, <8 x i16>)
281declare i32 @llvm.start.loop.iterations.i32(i32)
282declare i32 @llvm.loop.decrement.reg.i32.i32.i32(i32, i32)
283declare <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32, i32)
284declare <8 x i1> @llvm.get.active.lane.mask.v8i1.i32(i32, i32)
285declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>)
286declare i32 @llvm.loop.decrement.reg.i32(i32, i32)
287declare <4 x i16> @llvm.masked.load.v4i16.p0v4i16(<4 x i16>*, i32 immarg, <4 x i1>, <4 x i16>)
288