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 equality-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 eq <- not canonical
12 ; shl + ashr + icmp eq
15 ; However only the simplest form (with two shifts) gets lowered best.
17 ; ---------------------------------------------------------------------------- ;
18 ; shl + ashr + icmp eq
19 ; ---------------------------------------------------------------------------- ;
21 define i1 @shifts_eqcmp_i16_i8(i16 %x) nounwind {
22 ; CHECK-LABEL: shifts_eqcmp_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, eq
29 %tmp0 = shl i16 %x, 8 ; 16-8
30 %tmp1 = ashr exact i16 %tmp0, 8 ; 16-8
31 %tmp2 = icmp eq i16 %tmp1, %x
35 define i1 @shifts_eqcmp_i32_i16(i32 %x) nounwind {
36 ; CHECK-LABEL: shifts_eqcmp_i32_i16:
38 ; CHECK-NEXT: cmp w0, w0, sxth
39 ; CHECK-NEXT: cset w0, eq
41 %tmp0 = shl i32 %x, 16 ; 32-16
42 %tmp1 = ashr exact i32 %tmp0, 16 ; 32-16
43 %tmp2 = icmp eq i32 %tmp1, %x
47 define i1 @shifts_eqcmp_i32_i8(i32 %x) nounwind {
48 ; CHECK-LABEL: shifts_eqcmp_i32_i8:
50 ; CHECK-NEXT: cmp w0, w0, sxtb
51 ; CHECK-NEXT: cset w0, eq
53 %tmp0 = shl i32 %x, 24 ; 32-8
54 %tmp1 = ashr exact i32 %tmp0, 24 ; 32-8
55 %tmp2 = icmp eq i32 %tmp1, %x
59 define i1 @shifts_eqcmp_i64_i32(i64 %x) nounwind {
60 ; CHECK-LABEL: shifts_eqcmp_i64_i32:
62 ; CHECK-NEXT: cmp x0, w0, sxtw
63 ; CHECK-NEXT: cset w0, eq
65 %tmp0 = shl i64 %x, 32 ; 64-32
66 %tmp1 = ashr exact i64 %tmp0, 32 ; 64-32
67 %tmp2 = icmp eq i64 %tmp1, %x
71 define i1 @shifts_eqcmp_i64_i16(i64 %x) nounwind {
72 ; CHECK-LABEL: shifts_eqcmp_i64_i16:
74 ; CHECK-NEXT: cmp x0, w0, sxth
75 ; CHECK-NEXT: cset w0, eq
77 %tmp0 = shl i64 %x, 48 ; 64-16
78 %tmp1 = ashr exact i64 %tmp0, 48 ; 64-16
79 %tmp2 = icmp eq i64 %tmp1, %x
83 define i1 @shifts_eqcmp_i64_i8(i64 %x) nounwind {
84 ; CHECK-LABEL: shifts_eqcmp_i64_i8:
86 ; CHECK-NEXT: cmp x0, w0, sxtb
87 ; CHECK-NEXT: cset w0, eq
89 %tmp0 = shl i64 %x, 56 ; 64-8
90 %tmp1 = ashr exact i64 %tmp0, 56 ; 64-8
91 %tmp2 = icmp eq i64 %tmp1, %x
95 ; ---------------------------------------------------------------------------- ;
97 ; ---------------------------------------------------------------------------- ;
99 define i1 @add_ugecmp_i16_i8(i16 %x) nounwind {
100 ; CHECK-LABEL: add_ugecmp_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, #254
106 ; CHECK-NEXT: cset w0, hi
108 %tmp0 = add i16 %x, -128 ; ~0U << (8-1)
109 %tmp1 = icmp uge i16 %tmp0, -256 ; ~0U << 8
113 define i1 @add_ugecmp_i32_i16_i8(i16 %xx) nounwind {
114 ; CHECK-LABEL: add_ugecmp_i32_i16_i8:
116 ; CHECK-NEXT: and w8, w0, #0xffff
117 ; CHECK-NEXT: cmp w8, w8, sxtb
118 ; CHECK-NEXT: cset w0, eq
120 %x = zext i16 %xx to i32
121 %tmp0 = add i32 %x, -128 ; ~0U << (8-1)
122 %tmp1 = icmp uge i32 %tmp0, -256 ; ~0U << 8
126 define i1 @add_ugecmp_i32_i16(i32 %x) nounwind {
127 ; CHECK-LABEL: add_ugecmp_i32_i16:
129 ; CHECK-NEXT: cmp w0, w0, sxth
130 ; CHECK-NEXT: cset w0, eq
132 %tmp0 = add i32 %x, -32768 ; ~0U << (16-1)
133 %tmp1 = icmp uge i32 %tmp0, -65536 ; ~0U << 16
137 define i1 @add_ugecmp_i32_i8(i32 %x) nounwind {
138 ; CHECK-LABEL: add_ugecmp_i32_i8:
140 ; CHECK-NEXT: cmp w0, w0, sxtb
141 ; CHECK-NEXT: cset w0, eq
143 %tmp0 = add i32 %x, -128 ; ~0U << (8-1)
144 %tmp1 = icmp uge i32 %tmp0, -256 ; ~0U << 8
148 define i1 @add_ugecmp_i64_i32(i64 %x) nounwind {
149 ; CHECK-LABEL: add_ugecmp_i64_i32:
151 ; CHECK-NEXT: cmp x0, w0, sxtw
152 ; CHECK-NEXT: cset w0, eq
154 %tmp0 = add i64 %x, -2147483648 ; ~0U << (32-1)
155 %tmp1 = icmp uge i64 %tmp0, -4294967296 ; ~0U << 32
159 define i1 @add_ugecmp_i64_i16(i64 %x) nounwind {
160 ; CHECK-LABEL: add_ugecmp_i64_i16:
162 ; CHECK-NEXT: cmp x0, w0, sxth
163 ; CHECK-NEXT: cset w0, eq
165 %tmp0 = add i64 %x, -32768 ; ~0U << (16-1)
166 %tmp1 = icmp uge i64 %tmp0, -65536 ; ~0U << 16
170 define i1 @add_ugecmp_i64_i8(i64 %x) nounwind {
171 ; CHECK-LABEL: add_ugecmp_i64_i8:
173 ; CHECK-NEXT: cmp x0, w0, sxtb
174 ; CHECK-NEXT: cset w0, eq
176 %tmp0 = add i64 %x, -128 ; ~0U << (8-1)
177 %tmp1 = icmp uge i64 %tmp0, -256 ; ~0U << 8
181 ; Slightly more canonical variant
182 define i1 @add_ugtcmp_i16_i8(i16 %x) nounwind {
183 ; CHECK-LABEL: add_ugtcmp_i16_i8:
185 ; CHECK-NEXT: and w8, w0, #0xffff
186 ; CHECK-NEXT: sub w8, w8, #128
187 ; CHECK-NEXT: lsr w8, w8, #8
188 ; CHECK-NEXT: cmp w8, #254
189 ; CHECK-NEXT: cset w0, hi
191 %tmp0 = add i16 %x, -128 ; ~0U << (8-1)
192 %tmp1 = icmp ugt i16 %tmp0, -257 ; ~0U << 8 - 1
196 ; ---------------------------------------------------------------------------- ;
198 ; ---------------------------------------------------------------------------- ;
200 define i1 @add_ultcmp_i16_i8(i16 %x) nounwind {
201 ; CHECK-LABEL: add_ultcmp_i16_i8:
203 ; CHECK-NEXT: sxtb w8, w0
204 ; CHECK-NEXT: and w8, w8, #0xffff
205 ; CHECK-NEXT: cmp w8, w0, uxth
206 ; CHECK-NEXT: cset w0, eq
208 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
209 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
213 define i1 @add_ultcmp_i32_i16(i32 %x) nounwind {
214 ; CHECK-LABEL: add_ultcmp_i32_i16:
216 ; CHECK-NEXT: cmp w0, w0, sxth
217 ; CHECK-NEXT: cset w0, eq
219 %tmp0 = add i32 %x, 32768 ; 1U << (16-1)
220 %tmp1 = icmp ult i32 %tmp0, 65536 ; 1U << 16
224 define i1 @add_ultcmp_i32_i8(i32 %x) nounwind {
225 ; CHECK-LABEL: add_ultcmp_i32_i8:
227 ; CHECK-NEXT: cmp w0, w0, sxtb
228 ; CHECK-NEXT: cset w0, eq
230 %tmp0 = add i32 %x, 128 ; 1U << (8-1)
231 %tmp1 = icmp ult i32 %tmp0, 256 ; 1U << 8
235 define i1 @add_ultcmp_i64_i32(i64 %x) nounwind {
236 ; CHECK-LABEL: add_ultcmp_i64_i32:
238 ; CHECK-NEXT: cmp x0, w0, sxtw
239 ; CHECK-NEXT: cset w0, eq
241 %tmp0 = add i64 %x, 2147483648 ; 1U << (32-1)
242 %tmp1 = icmp ult i64 %tmp0, 4294967296 ; 1U << 32
246 define i1 @add_ultcmp_i64_i16(i64 %x) nounwind {
247 ; CHECK-LABEL: add_ultcmp_i64_i16:
249 ; CHECK-NEXT: cmp x0, w0, sxth
250 ; CHECK-NEXT: cset w0, eq
252 %tmp0 = add i64 %x, 32768 ; 1U << (16-1)
253 %tmp1 = icmp ult i64 %tmp0, 65536 ; 1U << 16
257 define i1 @add_ultcmp_i64_i8(i64 %x) nounwind {
258 ; CHECK-LABEL: add_ultcmp_i64_i8:
260 ; CHECK-NEXT: cmp x0, w0, sxtb
261 ; CHECK-NEXT: cset w0, eq
263 %tmp0 = add i64 %x, 128 ; 1U << (8-1)
264 %tmp1 = icmp ult i64 %tmp0, 256 ; 1U << 8
268 ; Slightly more canonical variant
269 define i1 @add_ulecmp_i16_i8(i16 %x) nounwind {
270 ; CHECK-LABEL: add_ulecmp_i16_i8:
272 ; CHECK-NEXT: sxtb w8, w0
273 ; CHECK-NEXT: and w8, w8, #0xffff
274 ; CHECK-NEXT: cmp w8, w0, uxth
275 ; CHECK-NEXT: cset w0, eq
277 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
278 %tmp1 = icmp ule i16 %tmp0, 255 ; (1U << 8) - 1
283 ; ---------------------------------------------------------------------------- ;
285 ; Adding not a constant
286 define i1 @add_ultcmp_bad_i16_i8_add(i16 %x, i16 %y) nounwind {
287 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_add:
289 ; CHECK-NEXT: add w8, w0, w1
290 ; CHECK-NEXT: tst w8, #0xff00
291 ; CHECK-NEXT: cset w0, eq
293 %tmp0 = add i16 %x, %y
294 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
298 ; Comparing not with a constant
299 define i1 @add_ultcmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind {
300 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_cmp:
302 ; CHECK-NEXT: add w8, w0, #128
303 ; CHECK-NEXT: and w8, w8, #0xffff
304 ; CHECK-NEXT: cmp w8, w1, uxth
305 ; CHECK-NEXT: cset w0, lo
307 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
308 %tmp1 = icmp ult i16 %tmp0, %y
312 ; Second constant is not larger than the first one
313 define i1 @add_ultcmp_bad_i8_i16(i16 %x) nounwind {
314 ; CHECK-LABEL: add_ultcmp_bad_i8_i16:
316 ; CHECK-NEXT: and w8, w0, #0xffff
317 ; CHECK-NEXT: add w8, w8, #128
318 ; CHECK-NEXT: lsr w0, w8, #16
320 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
321 %tmp1 = icmp ult i16 %tmp0, 128 ; 1U << (8-1)
325 ; First constant is not power of two
326 define i1 @add_ultcmp_bad_i16_i8_c0notpoweroftwo(i16 %x) nounwind {
327 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_c0notpoweroftwo:
329 ; CHECK-NEXT: add w8, w0, #192
330 ; CHECK-NEXT: tst w8, #0xff00
331 ; CHECK-NEXT: cset w0, eq
333 %tmp0 = add i16 %x, 192 ; (1U << (8-1)) + (1U << (8-1-1))
334 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
338 ; Second constant is not power of two
339 define i1 @add_ultcmp_bad_i16_i8_c1notpoweroftwo(i16 %x) nounwind {
340 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_c1notpoweroftwo:
342 ; CHECK-NEXT: add w8, w0, #128
343 ; CHECK-NEXT: and w8, w8, #0xffff
344 ; CHECK-NEXT: cmp w8, #768
345 ; CHECK-NEXT: cset w0, lo
347 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
348 %tmp1 = icmp ult i16 %tmp0, 768 ; (1U << 8)) + (1U << (8+1))
352 ; Magic check fails, 64 << 1 != 256
353 define i1 @add_ultcmp_bad_i16_i8_magic(i16 %x) nounwind {
354 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_magic:
356 ; CHECK-NEXT: add w8, w0, #64
357 ; CHECK-NEXT: tst w8, #0xff00
358 ; CHECK-NEXT: cset w0, eq
360 %tmp0 = add i16 %x, 64 ; 1U << (8-1-1)
361 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
365 ; Bad 'destination type'
366 define i1 @add_ultcmp_bad_i16_i4(i16 %x) nounwind {
367 ; CHECK-LABEL: add_ultcmp_bad_i16_i4:
369 ; CHECK-NEXT: add w8, w0, #8
370 ; CHECK-NEXT: tst w8, #0xfff0
371 ; CHECK-NEXT: cset w0, eq
373 %tmp0 = add i16 %x, 8 ; 1U << (4-1)
374 %tmp1 = icmp ult i16 %tmp0, 16 ; 1U << 4
379 define i1 @add_ultcmp_bad_i24_i8(i24 %x) nounwind {
380 ; CHECK-LABEL: add_ultcmp_bad_i24_i8:
382 ; CHECK-NEXT: add w8, w0, #128
383 ; CHECK-NEXT: tst w8, #0xffff00
384 ; CHECK-NEXT: cset w0, eq
386 %tmp0 = add i24 %x, 128 ; 1U << (8-1)
387 %tmp1 = icmp ult i24 %tmp0, 256 ; 1U << 8
391 define i1 @add_ulecmp_bad_i16_i8(i16 %x) nounwind {
392 ; CHECK-LABEL: add_ulecmp_bad_i16_i8:
394 ; CHECK-NEXT: mov w0, #1 // =0x1
396 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
397 %tmp1 = icmp ule i16 %tmp0, -1 ; when we +1 it, it will wrap to 0