1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=aarch64-unknown-linux-gnu < %s | FileCheck %s
4 ; https://bugs.llvm.org/show_bug.cgi?id=38149
6 ; We are truncating from wider width, and then sign-extending
7 ; back to the original width. Then we inequality-comparing orig and src.
8 ; If they don't match, then we had signed truncation during truncation.
10 ; This can be expressed in a several ways in IR:
11 ; trunc + sext + icmp ne <- not canonical
12 ; shl + ashr + icmp ne
15 ; However only the simplest form (with two shifts) gets lowered best.
17 ; ---------------------------------------------------------------------------- ;
18 ; shl + ashr + icmp ne
19 ; ---------------------------------------------------------------------------- ;
21 define i1 @shifts_necmp_i16_i8(i16 %x) nounwind {
22 ; CHECK-LABEL: shifts_necmp_i16_i8:
24 ; CHECK-NEXT: sxtb w8, w0
25 ; CHECK-NEXT: and w8, w8, #0xffff
26 ; CHECK-NEXT: cmp w8, w0, uxth
27 ; CHECK-NEXT: cset w0, ne
29 %tmp0 = shl i16 %x, 8 ; 16-8
30 %tmp1 = ashr exact i16 %tmp0, 8 ; 16-8
31 %tmp2 = icmp ne i16 %tmp1, %x
35 define i1 @shifts_necmp_i32_i16(i32 %x) nounwind {
36 ; CHECK-LABEL: shifts_necmp_i32_i16:
38 ; CHECK-NEXT: cmp w0, w0, sxth
39 ; CHECK-NEXT: cset w0, ne
41 %tmp0 = shl i32 %x, 16 ; 32-16
42 %tmp1 = ashr exact i32 %tmp0, 16 ; 32-16
43 %tmp2 = icmp ne i32 %tmp1, %x
47 define i1 @shifts_necmp_i32_i8(i32 %x) nounwind {
48 ; CHECK-LABEL: shifts_necmp_i32_i8:
50 ; CHECK-NEXT: cmp w0, w0, sxtb
51 ; CHECK-NEXT: cset w0, ne
53 %tmp0 = shl i32 %x, 24 ; 32-8
54 %tmp1 = ashr exact i32 %tmp0, 24 ; 32-8
55 %tmp2 = icmp ne i32 %tmp1, %x
59 define i1 @shifts_necmp_i64_i32(i64 %x) nounwind {
60 ; CHECK-LABEL: shifts_necmp_i64_i32:
62 ; CHECK-NEXT: cmp x0, w0, sxtw
63 ; CHECK-NEXT: cset w0, ne
65 %tmp0 = shl i64 %x, 32 ; 64-32
66 %tmp1 = ashr exact i64 %tmp0, 32 ; 64-32
67 %tmp2 = icmp ne i64 %tmp1, %x
71 define i1 @shifts_necmp_i64_i16(i64 %x) nounwind {
72 ; CHECK-LABEL: shifts_necmp_i64_i16:
74 ; CHECK-NEXT: cmp x0, w0, sxth
75 ; CHECK-NEXT: cset w0, ne
77 %tmp0 = shl i64 %x, 48 ; 64-16
78 %tmp1 = ashr exact i64 %tmp0, 48 ; 64-16
79 %tmp2 = icmp ne i64 %tmp1, %x
83 define i1 @shifts_necmp_i64_i8(i64 %x) nounwind {
84 ; CHECK-LABEL: shifts_necmp_i64_i8:
86 ; CHECK-NEXT: cmp x0, w0, sxtb
87 ; CHECK-NEXT: cset w0, ne
89 %tmp0 = shl i64 %x, 56 ; 64-8
90 %tmp1 = ashr exact i64 %tmp0, 56 ; 64-8
91 %tmp2 = icmp ne i64 %tmp1, %x
95 ; ---------------------------------------------------------------------------- ;
97 ; ---------------------------------------------------------------------------- ;
99 define i1 @add_ultcmp_i16_i8(i16 %x) nounwind {
100 ; CHECK-LABEL: add_ultcmp_i16_i8:
102 ; CHECK-NEXT: and w8, w0, #0xffff
103 ; CHECK-NEXT: sub w8, w8, #128
104 ; CHECK-NEXT: lsr w8, w8, #8
105 ; CHECK-NEXT: cmp w8, #255
106 ; CHECK-NEXT: cset w0, lo
108 %tmp0 = add i16 %x, -128 ; ~0U << (8-1)
109 %tmp1 = icmp ult i16 %tmp0, -256 ; ~0U << 8
113 define i1 @add_ultcmp_i32_i16(i32 %x) nounwind {
114 ; CHECK-LABEL: add_ultcmp_i32_i16:
116 ; CHECK-NEXT: cmp w0, w0, sxth
117 ; CHECK-NEXT: cset w0, ne
119 %tmp0 = add i32 %x, -32768 ; ~0U << (16-1)
120 %tmp1 = icmp ult i32 %tmp0, -65536 ; ~0U << 16
124 define i1 @add_ultcmp_i32_i8(i32 %x) nounwind {
125 ; CHECK-LABEL: add_ultcmp_i32_i8:
127 ; CHECK-NEXT: cmp w0, w0, sxtb
128 ; CHECK-NEXT: cset w0, ne
130 %tmp0 = add i32 %x, -128 ; ~0U << (8-1)
131 %tmp1 = icmp ult i32 %tmp0, -256 ; ~0U << 8
135 define i1 @add_ultcmp_i64_i32(i64 %x) nounwind {
136 ; CHECK-LABEL: add_ultcmp_i64_i32:
138 ; CHECK-NEXT: cmp x0, w0, sxtw
139 ; CHECK-NEXT: cset w0, ne
141 %tmp0 = add i64 %x, -2147483648 ; ~0U << (32-1)
142 %tmp1 = icmp ult i64 %tmp0, -4294967296 ; ~0U << 32
146 define i1 @add_ultcmp_i64_i16(i64 %x) nounwind {
147 ; CHECK-LABEL: add_ultcmp_i64_i16:
149 ; CHECK-NEXT: cmp x0, w0, sxth
150 ; CHECK-NEXT: cset w0, ne
152 %tmp0 = add i64 %x, -32768 ; ~0U << (16-1)
153 %tmp1 = icmp ult i64 %tmp0, -65536 ; ~0U << 16
157 define i1 @add_ultcmp_i64_i8(i64 %x) nounwind {
158 ; CHECK-LABEL: add_ultcmp_i64_i8:
160 ; CHECK-NEXT: cmp x0, w0, sxtb
161 ; CHECK-NEXT: cset w0, ne
163 %tmp0 = add i64 %x, -128 ; ~0U << (8-1)
164 %tmp1 = icmp ult i64 %tmp0, -256 ; ~0U << 8
168 ; Slightly more canonical variant
169 define i1 @add_ulecmp_i16_i8(i16 %x) nounwind {
170 ; CHECK-LABEL: add_ulecmp_i16_i8:
172 ; CHECK-NEXT: and w8, w0, #0xffff
173 ; CHECK-NEXT: sub w8, w8, #128
174 ; CHECK-NEXT: lsr w8, w8, #8
175 ; CHECK-NEXT: cmp w8, #255
176 ; CHECK-NEXT: cset w0, lo
178 %tmp0 = add i16 %x, -128 ; ~0U << (8-1)
179 %tmp1 = icmp ule i16 %tmp0, -257 ; ~0U << 8 - 1
183 ; ---------------------------------------------------------------------------- ;
185 ; ---------------------------------------------------------------------------- ;
187 define i1 @add_ugecmp_i16_i8(i16 %x) nounwind {
188 ; CHECK-LABEL: add_ugecmp_i16_i8:
190 ; CHECK-NEXT: sxtb w8, w0
191 ; CHECK-NEXT: and w8, w8, #0xffff
192 ; CHECK-NEXT: cmp w8, w0, uxth
193 ; CHECK-NEXT: cset w0, ne
195 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
196 %tmp1 = icmp uge i16 %tmp0, 256 ; 1U << 8
200 define i1 @add_ugecmp_i32_i16(i32 %x) nounwind {
201 ; CHECK-LABEL: add_ugecmp_i32_i16:
203 ; CHECK-NEXT: cmp w0, w0, sxth
204 ; CHECK-NEXT: cset w0, ne
206 %tmp0 = add i32 %x, 32768 ; 1U << (16-1)
207 %tmp1 = icmp uge i32 %tmp0, 65536 ; 1U << 16
211 define i1 @add_ugecmp_i32_i8(i32 %x) nounwind {
212 ; CHECK-LABEL: add_ugecmp_i32_i8:
214 ; CHECK-NEXT: cmp w0, w0, sxtb
215 ; CHECK-NEXT: cset w0, ne
217 %tmp0 = add i32 %x, 128 ; 1U << (8-1)
218 %tmp1 = icmp uge i32 %tmp0, 256 ; 1U << 8
222 define i1 @add_ugecmp_i64_i32(i64 %x) nounwind {
223 ; CHECK-LABEL: add_ugecmp_i64_i32:
225 ; CHECK-NEXT: cmp x0, w0, sxtw
226 ; CHECK-NEXT: cset w0, ne
228 %tmp0 = add i64 %x, 2147483648 ; 1U << (32-1)
229 %tmp1 = icmp uge i64 %tmp0, 4294967296 ; 1U << 32
233 define i1 @add_ugecmp_i64_i16(i64 %x) nounwind {
234 ; CHECK-LABEL: add_ugecmp_i64_i16:
236 ; CHECK-NEXT: cmp x0, w0, sxth
237 ; CHECK-NEXT: cset w0, ne
239 %tmp0 = add i64 %x, 32768 ; 1U << (16-1)
240 %tmp1 = icmp uge i64 %tmp0, 65536 ; 1U << 16
244 define i1 @add_ugecmp_i64_i8(i64 %x) nounwind {
245 ; CHECK-LABEL: add_ugecmp_i64_i8:
247 ; CHECK-NEXT: cmp x0, w0, sxtb
248 ; CHECK-NEXT: cset w0, ne
250 %tmp0 = add i64 %x, 128 ; 1U << (8-1)
251 %tmp1 = icmp uge i64 %tmp0, 256 ; 1U << 8
255 ; Slightly more canonical variant
256 define i1 @add_ugtcmp_i16_i8(i16 %x) nounwind {
257 ; CHECK-LABEL: add_ugtcmp_i16_i8:
259 ; CHECK-NEXT: sxtb w8, w0
260 ; CHECK-NEXT: and w8, w8, #0xffff
261 ; CHECK-NEXT: cmp w8, w0, uxth
262 ; CHECK-NEXT: cset w0, ne
264 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
265 %tmp1 = icmp ugt i16 %tmp0, 255 ; (1U << 8) - 1
270 ; ---------------------------------------------------------------------------- ;
272 ; Adding not a constant
273 define i1 @add_ugecmp_bad_i16_i8_add(i16 %x, i16 %y) nounwind {
274 ; CHECK-LABEL: add_ugecmp_bad_i16_i8_add:
276 ; CHECK-NEXT: add w8, w0, w1
277 ; CHECK-NEXT: and w8, w8, #0xffff
278 ; CHECK-NEXT: cmp w8, #255
279 ; CHECK-NEXT: cset w0, hi
281 %tmp0 = add i16 %x, %y
282 %tmp1 = icmp uge i16 %tmp0, 256 ; 1U << 8
286 ; Comparing not with a constant
287 define i1 @add_ugecmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind {
288 ; CHECK-LABEL: add_ugecmp_bad_i16_i8_cmp:
290 ; CHECK-NEXT: add w8, w0, #128
291 ; CHECK-NEXT: and w8, w8, #0xffff
292 ; CHECK-NEXT: cmp w8, w1, uxth
293 ; CHECK-NEXT: cset w0, hs
295 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
296 %tmp1 = icmp uge i16 %tmp0, %y
300 ; Second constant is not larger than the first one
301 define i1 @add_ugecmp_bad_i8_i16(i16 %x) nounwind {
302 ; CHECK-LABEL: add_ugecmp_bad_i8_i16:
304 ; CHECK-NEXT: add w8, w0, #128
305 ; CHECK-NEXT: and w8, w8, #0xffff
306 ; CHECK-NEXT: cmp w8, #127
307 ; CHECK-NEXT: cset w0, hi
309 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
310 %tmp1 = icmp uge i16 %tmp0, 128 ; 1U << (8-1)
314 ; First constant is not power of two
315 define i1 @add_ugecmp_bad_i16_i8_c0notpoweroftwo(i16 %x) nounwind {
316 ; CHECK-LABEL: add_ugecmp_bad_i16_i8_c0notpoweroftwo:
318 ; CHECK-NEXT: add w8, w0, #192
319 ; CHECK-NEXT: and w8, w8, #0xffff
320 ; CHECK-NEXT: cmp w8, #255
321 ; CHECK-NEXT: cset w0, hi
323 %tmp0 = add i16 %x, 192 ; (1U << (8-1)) + (1U << (8-1-1))
324 %tmp1 = icmp uge i16 %tmp0, 256 ; 1U << 8
328 ; Second constant is not power of two
329 define i1 @add_ugecmp_bad_i16_i8_c1notpoweroftwo(i16 %x) nounwind {
330 ; CHECK-LABEL: add_ugecmp_bad_i16_i8_c1notpoweroftwo:
332 ; CHECK-NEXT: add w8, w0, #128
333 ; CHECK-NEXT: and w8, w8, #0xffff
334 ; CHECK-NEXT: cmp w8, #767
335 ; CHECK-NEXT: cset w0, hi
337 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
338 %tmp1 = icmp uge i16 %tmp0, 768 ; (1U << 8)) + (1U << (8+1))
342 ; Magic check fails, 64 << 1 != 256
343 define i1 @add_ugecmp_bad_i16_i8_magic(i16 %x) nounwind {
344 ; CHECK-LABEL: add_ugecmp_bad_i16_i8_magic:
346 ; CHECK-NEXT: add w8, w0, #64
347 ; CHECK-NEXT: and w8, w8, #0xffff
348 ; CHECK-NEXT: cmp w8, #255
349 ; CHECK-NEXT: cset w0, hi
351 %tmp0 = add i16 %x, 64 ; 1U << (8-1-1)
352 %tmp1 = icmp uge i16 %tmp0, 256 ; 1U << 8
356 ; Bad 'destination type'
357 define i1 @add_ugecmp_bad_i16_i4(i16 %x) nounwind {
358 ; CHECK-LABEL: add_ugecmp_bad_i16_i4:
360 ; CHECK-NEXT: add w8, w0, #8
361 ; CHECK-NEXT: and w8, w8, #0xffff
362 ; CHECK-NEXT: cmp w8, #15
363 ; CHECK-NEXT: cset w0, hi
365 %tmp0 = add i16 %x, 8 ; 1U << (4-1)
366 %tmp1 = icmp uge i16 %tmp0, 16 ; 1U << 4
371 define i1 @add_ugecmp_bad_i24_i8(i24 %x) nounwind {
372 ; CHECK-LABEL: add_ugecmp_bad_i24_i8:
374 ; CHECK-NEXT: add w8, w0, #128
375 ; CHECK-NEXT: and w8, w8, #0xffffff
376 ; CHECK-NEXT: cmp w8, #255
377 ; CHECK-NEXT: cset w0, hi
379 %tmp0 = add i24 %x, 128 ; 1U << (8-1)
380 %tmp1 = icmp uge i24 %tmp0, 256 ; 1U << 8
384 ; Slightly more canonical variant
385 define i1 @add_ugtcmp_bad_i16_i8(i16 %x) nounwind {
386 ; CHECK-LABEL: add_ugtcmp_bad_i16_i8:
388 ; CHECK-NEXT: mov w0, wzr
390 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
391 %tmp1 = icmp ugt i16 %tmp0, -1 ; when we +1 it, it will wrap to 0