1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv32 -mattr=+m,+v < %s | FileCheck %s \
3 ; RUN: --check-prefixes=CHECK,RV32
4 ; RUN: llc -mtriple=riscv64 -mattr=+m,+v < %s | FileCheck %s \
5 ; RUN: --check-prefixes=CHECK,RV64
7 ; fold (add (umax X, C), -C) --> (usubsat X, C)
9 define <2 x i64> @add_umax_v2i64(<2 x i64> %a0) {
10 ; CHECK-LABEL: add_umax_v2i64:
12 ; CHECK-NEXT: li a0, 7
13 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
14 ; CHECK-NEXT: vssubu.vx v8, v8, a0
16 %v1 = call <2 x i64> @llvm.umax.v2i64(<2 x i64> %a0, <2 x i64> <i64 7, i64 7>)
17 %v2 = add <2 x i64> %v1, <i64 -7, i64 -7>
21 define <vscale x 2 x i64> @add_umax_nxv2i64(<vscale x 2 x i64> %a0) {
22 ; CHECK-LABEL: add_umax_nxv2i64:
24 ; CHECK-NEXT: li a0, 7
25 ; CHECK-NEXT: vsetvli a1, zero, e64, m2, ta, ma
26 ; CHECK-NEXT: vssubu.vx v8, v8, a0
28 %ins1 = insertelement <vscale x 2 x i64> poison, i64 7, i32 0
29 %splat1 = shufflevector <vscale x 2 x i64> %ins1, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
30 %ins2 = insertelement <vscale x 2 x i64> poison, i64 -7, i32 0
31 %splat2 = shufflevector <vscale x 2 x i64> %ins2, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
32 %v1 = call <vscale x 2 x i64> @llvm.umax.nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %splat1)
33 %v2 = add <vscale x 2 x i64> %v1, %splat2
34 ret <vscale x 2 x i64> %v2
37 ; Try to find umax(a,b) - b or a - umin(a,b) patterns
38 ; they may be converted to usubsat(a,b).
40 define <2 x i64> @sub_umax_v2i64(<2 x i64> %a0, <2 x i64> %a1) {
41 ; CHECK-LABEL: sub_umax_v2i64:
43 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
44 ; CHECK-NEXT: vssubu.vv v8, v8, v9
46 %v1 = call <2 x i64> @llvm.umax.v2i64(<2 x i64> %a0, <2 x i64> %a1)
47 %v2 = sub <2 x i64> %v1, %a1
51 define <vscale x 2 x i64> @sub_umax_nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1) {
52 ; CHECK-LABEL: sub_umax_nxv2i64:
54 ; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
55 ; CHECK-NEXT: vssubu.vv v8, v8, v10
57 %v1 = call <vscale x 2 x i64> @llvm.umax.nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1)
58 %v2 = sub <vscale x 2 x i64> %v1, %a1
59 ret <vscale x 2 x i64> %v2
62 define <2 x i64> @sub_umin_v2i64(<2 x i64> %a0, <2 x i64> %a1) {
63 ; CHECK-LABEL: sub_umin_v2i64:
65 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
66 ; CHECK-NEXT: vssubu.vv v8, v8, v9
68 %v1 = call <2 x i64> @llvm.umin.v2i64(<2 x i64> %a0, <2 x i64> %a1)
69 %v2 = sub <2 x i64> %a0, %v1
73 define <vscale x 2 x i64> @sub_umin_nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1) {
74 ; CHECK-LABEL: sub_umin_nxv2i64:
76 ; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
77 ; CHECK-NEXT: vssubu.vv v8, v8, v10
79 %v1 = call <vscale x 2 x i64> @llvm.umin.nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1)
80 %v2 = sub <vscale x 2 x i64> %a0, %v1
81 ret <vscale x 2 x i64> %v2
84 ; Match VSELECTs into sub with unsigned saturation.
86 ; x >= y ? x-y : 0 --> usubsat x, y
88 define <2 x i64> @vselect_sub_v2i64(<2 x i64> %a0, <2 x i64> %a1) {
89 ; CHECK-LABEL: vselect_sub_v2i64:
91 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
92 ; CHECK-NEXT: vssubu.vv v8, v8, v9
94 %cmp = icmp uge <2 x i64> %a0, %a1
95 %v1 = sub <2 x i64> %a0, %a1
96 %v2 = select <2 x i1> %cmp, <2 x i64> %v1, <2 x i64> zeroinitializer
100 define <vscale x 2 x i64> @vselect_sub_nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1) {
101 ; CHECK-LABEL: vselect_sub_nxv2i64:
103 ; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
104 ; CHECK-NEXT: vssubu.vv v8, v8, v10
106 %cmp = icmp uge <vscale x 2 x i64> %a0, %a1
107 %v1 = sub <vscale x 2 x i64> %a0, %a1
108 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i64> %v1, <vscale x 2 x i64> zeroinitializer
109 ret <vscale x 2 x i64> %v2
112 define <8 x i16> @vselect_sub_2_v8i16(<8 x i16> %x, i16 zeroext %w) nounwind {
113 ; CHECK-LABEL: vselect_sub_2_v8i16:
114 ; CHECK: # %bb.0: # %entry
115 ; CHECK-NEXT: vsetivli zero, 8, e16, m1, ta, ma
116 ; CHECK-NEXT: vssubu.vx v8, v8, a0
119 %0 = insertelement <8 x i16> poison, i16 %w, i32 0
120 %broadcast15 = shufflevector <8 x i16> %0, <8 x i16> poison, <8 x i32> zeroinitializer
121 %1 = icmp ult <8 x i16> %x, %broadcast15
122 %2 = sub <8 x i16> %x, %broadcast15
123 %res = select <8 x i1> %1, <8 x i16> zeroinitializer, <8 x i16> %2
127 define <vscale x 8 x i16> @vselect_sub_2_nxv8i16(<vscale x 8 x i16> %x, i16 zeroext %w) nounwind {
128 ; CHECK-LABEL: vselect_sub_2_nxv8i16:
129 ; CHECK: # %bb.0: # %entry
130 ; CHECK-NEXT: vsetvli a1, zero, e16, m2, ta, ma
131 ; CHECK-NEXT: vssubu.vx v8, v8, a0
134 %0 = insertelement <vscale x 8 x i16> poison, i16 %w, i32 0
135 %broadcast15 = shufflevector <vscale x 8 x i16> %0, <vscale x 8 x i16> poison, <vscale x 8 x i32> zeroinitializer
136 %1 = icmp ult <vscale x 8 x i16> %x, %broadcast15
137 %2 = sub <vscale x 8 x i16> %x, %broadcast15
138 %res = select <vscale x 8 x i1> %1, <vscale x 8 x i16> zeroinitializer, <vscale x 8 x i16> %2
139 ret <vscale x 8 x i16> %res
142 ; x > y ? x-y : 0 --> usubsat x, y
143 ; x > C-1 ? x+-C : 0 --> usubsat x, C
145 define <2 x i64> @vselect_add_const_v2i64(<2 x i64> %a0) {
146 ; CHECK-LABEL: vselect_add_const_v2i64:
148 ; CHECK-NEXT: li a0, 6
149 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
150 ; CHECK-NEXT: vssubu.vx v8, v8, a0
152 %v1 = add <2 x i64> %a0, <i64 -6, i64 -6>
153 %cmp = icmp ugt <2 x i64> %a0, <i64 5, i64 5>
154 %v2 = select <2 x i1> %cmp, <2 x i64> %v1, <2 x i64> zeroinitializer
158 define <vscale x 2 x i64> @vselect_add_const_nxv2i64(<vscale x 2 x i64> %a0) {
159 ; CHECK-LABEL: vselect_add_const_nxv2i64:
161 ; CHECK-NEXT: li a0, 6
162 ; CHECK-NEXT: vsetvli a1, zero, e64, m2, ta, ma
163 ; CHECK-NEXT: vssubu.vx v8, v8, a0
165 %cm1 = insertelement <vscale x 2 x i64> poison, i64 -6, i32 0
166 %splatcm1 = shufflevector <vscale x 2 x i64> %cm1, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
167 %nc = insertelement <vscale x 2 x i64> poison, i64 5, i32 0
168 %splatnc = shufflevector <vscale x 2 x i64> %nc, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
169 %v1 = add <vscale x 2 x i64> %a0, %splatcm1
170 %cmp = icmp ugt <vscale x 2 x i64> %a0, %splatnc
171 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i64> %v1, <vscale x 2 x i64> zeroinitializer
172 ret <vscale x 2 x i64> %v2
175 define <2 x i16> @vselect_add_const_signbit_v2i16(<2 x i16> %a0) {
176 ; CHECK-LABEL: vselect_add_const_signbit_v2i16:
178 ; CHECK-NEXT: lui a0, 8
179 ; CHECK-NEXT: addi a0, a0, -1
180 ; CHECK-NEXT: vsetivli zero, 2, e16, mf4, ta, ma
181 ; CHECK-NEXT: vssubu.vx v8, v8, a0
183 %cmp = icmp ugt <2 x i16> %a0, <i16 32766, i16 32766>
184 %v1 = add <2 x i16> %a0, <i16 -32767, i16 -32767>
185 %v2 = select <2 x i1> %cmp, <2 x i16> %v1, <2 x i16> zeroinitializer
189 define <vscale x 2 x i16> @vselect_add_const_signbit_nxv2i16(<vscale x 2 x i16> %a0) {
190 ; CHECK-LABEL: vselect_add_const_signbit_nxv2i16:
192 ; CHECK-NEXT: lui a0, 8
193 ; CHECK-NEXT: addi a0, a0, -1
194 ; CHECK-NEXT: vsetvli a1, zero, e16, mf2, ta, ma
195 ; CHECK-NEXT: vssubu.vx v8, v8, a0
197 %cm1 = insertelement <vscale x 2 x i16> poison, i16 32766, i32 0
198 %splatcm1 = shufflevector <vscale x 2 x i16> %cm1, <vscale x 2 x i16> poison, <vscale x 2 x i32> zeroinitializer
199 %nc = insertelement <vscale x 2 x i16> poison, i16 -32767, i32 0
200 %splatnc = shufflevector <vscale x 2 x i16> %nc, <vscale x 2 x i16> poison, <vscale x 2 x i32> zeroinitializer
201 %cmp = icmp ugt <vscale x 2 x i16> %a0, %splatcm1
202 %v1 = add <vscale x 2 x i16> %a0, %splatnc
203 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i16> %v1, <vscale x 2 x i16> zeroinitializer
204 ret <vscale x 2 x i16> %v2
207 ; x s< 0 ? x^C : 0 --> usubsat x, C
209 define <2 x i16> @vselect_xor_const_signbit_v2i16(<2 x i16> %a0) {
210 ; CHECK-LABEL: vselect_xor_const_signbit_v2i16:
212 ; CHECK-NEXT: lui a0, 8
213 ; CHECK-NEXT: vsetivli zero, 2, e16, mf4, ta, ma
214 ; CHECK-NEXT: vssubu.vx v8, v8, a0
216 %cmp = icmp slt <2 x i16> %a0, zeroinitializer
217 %v1 = xor <2 x i16> %a0, <i16 -32768, i16 -32768>
218 %v2 = select <2 x i1> %cmp, <2 x i16> %v1, <2 x i16> zeroinitializer
222 define <vscale x 2 x i16> @vselect_xor_const_signbit_nxv2i16(<vscale x 2 x i16> %a0) {
223 ; CHECK-LABEL: vselect_xor_const_signbit_nxv2i16:
225 ; CHECK-NEXT: lui a0, 8
226 ; CHECK-NEXT: vsetvli a1, zero, e16, mf2, ta, ma
227 ; CHECK-NEXT: vssubu.vx v8, v8, a0
229 %cmp = icmp slt <vscale x 2 x i16> %a0, zeroinitializer
230 %ins = insertelement <vscale x 2 x i16> poison, i16 -32768, i32 0
231 %splat = shufflevector <vscale x 2 x i16> %ins, <vscale x 2 x i16> poison, <vscale x 2 x i32> zeroinitializer
232 %v1 = xor <vscale x 2 x i16> %a0, %splat
233 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i16> %v1, <vscale x 2 x i16> zeroinitializer
234 ret <vscale x 2 x i16> %v2
237 ; Match VSELECTs into add with unsigned saturation.
239 ; x <= x+y ? x+y : ~0 --> uaddsat x, y
240 ; x+y >= x ? x+y : ~0 --> uaddsat x, y
242 define <2 x i64> @vselect_add_v2i64(<2 x i64> %a0, <2 x i64> %a1) {
243 ; CHECK-LABEL: vselect_add_v2i64:
245 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
246 ; CHECK-NEXT: vsaddu.vv v8, v8, v9
248 %v1 = add <2 x i64> %a0, %a1
249 %cmp = icmp ule <2 x i64> %a0, %v1
250 %v2 = select <2 x i1> %cmp, <2 x i64> %v1, <2 x i64> <i64 -1, i64 -1>
254 define <vscale x 2 x i64> @vselect_add_nxv2i64(<vscale x 2 x i64> %a0, <vscale x 2 x i64> %a1) {
255 ; CHECK-LABEL: vselect_add_nxv2i64:
257 ; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
258 ; CHECK-NEXT: vsaddu.vv v8, v8, v10
260 %v1 = add <vscale x 2 x i64> %a0, %a1
261 %cmp = icmp ule <vscale x 2 x i64> %a0, %v1
262 %allones = insertelement <vscale x 2 x i64> poison, i64 -1, i32 0
263 %splatallones = shufflevector <vscale x 2 x i64> %allones, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
264 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i64> %v1, <vscale x 2 x i64> %splatallones
265 ret <vscale x 2 x i64> %v2
268 ; if the rhs is a constant we have to reverse the const canonicalization.
269 ; x >= ~C ? x+C : ~0 --> uaddsat x, C
271 define <2 x i64> @vselect_add_const_2_v2i64(<2 x i64> %a0) {
272 ; CHECK-LABEL: vselect_add_const_2_v2i64:
274 ; CHECK-NEXT: vsetivli zero, 2, e64, m1, ta, ma
275 ; CHECK-NEXT: vsaddu.vi v8, v8, 6
277 %v1 = add <2 x i64> %a0, <i64 6, i64 6>
278 %cmp = icmp ule <2 x i64> %a0, <i64 -7, i64 -7>
279 %v2 = select <2 x i1> %cmp, <2 x i64> %v1, <2 x i64> <i64 -1, i64 -1>
283 define <vscale x 2 x i64> @vselect_add_const_2_nxv2i64(<vscale x 2 x i64> %a0) {
284 ; CHECK-LABEL: vselect_add_const_2_nxv2i64:
286 ; CHECK-NEXT: vsetvli a0, zero, e64, m2, ta, ma
287 ; CHECK-NEXT: vsaddu.vi v8, v8, 6
289 %cm1 = insertelement <vscale x 2 x i64> poison, i64 6, i32 0
290 %splatcm1 = shufflevector <vscale x 2 x i64> %cm1, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
291 %nc = insertelement <vscale x 2 x i64> poison, i64 -7, i32 0
292 %splatnc = shufflevector <vscale x 2 x i64> %nc, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
293 %v1 = add <vscale x 2 x i64> %a0, %splatcm1
294 %cmp = icmp ule <vscale x 2 x i64> %a0, %splatnc
295 %allones = insertelement <vscale x 2 x i64> poison, i64 -1, i32 0
296 %splatallones = shufflevector <vscale x 2 x i64> %allones, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
297 %v2 = select <vscale x 2 x i1> %cmp, <vscale x 2 x i64> %v1, <vscale x 2 x i64> %splatallones
298 ret <vscale x 2 x i64> %v2
301 declare <2 x i64> @llvm.umin.v2i64(<2 x i64>, <2 x i64>)
302 declare <2 x i64> @llvm.umax.v2i64(<2 x i64>, <2 x i64>)
303 declare <vscale x 2 x i64> @llvm.umin.nxv2i64(<vscale x 2 x i64>, <vscale x 2 x i64>)
304 declare <vscale x 2 x i64> @llvm.umax.nxv2i64(<vscale x 2 x i64>, <vscale x 2 x i64>)
305 ;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line: