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: and w8, w8, #0xffff
291 ; CHECK-NEXT: cmp w8, #256
292 ; CHECK-NEXT: cset w0, lo
294 %tmp0 = add i16 %x, %y
295 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
299 ; Comparing not with a constant
300 define i1 @add_ultcmp_bad_i16_i8_cmp(i16 %x, i16 %y) nounwind {
301 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_cmp:
303 ; CHECK-NEXT: add w8, w0, #128
304 ; CHECK-NEXT: and w8, w8, #0xffff
305 ; CHECK-NEXT: cmp w8, w1, uxth
306 ; CHECK-NEXT: cset w0, lo
308 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
309 %tmp1 = icmp ult i16 %tmp0, %y
313 ; Second constant is not larger than the first one
314 define i1 @add_ultcmp_bad_i8_i16(i16 %x) nounwind {
315 ; CHECK-LABEL: add_ultcmp_bad_i8_i16:
317 ; CHECK-NEXT: and w8, w0, #0xffff
318 ; CHECK-NEXT: add w8, w8, #128
319 ; CHECK-NEXT: lsr w0, w8, #16
321 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
322 %tmp1 = icmp ult i16 %tmp0, 128 ; 1U << (8-1)
326 ; First constant is not power of two
327 define i1 @add_ultcmp_bad_i16_i8_c0notpoweroftwo(i16 %x) nounwind {
328 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_c0notpoweroftwo:
330 ; CHECK-NEXT: add w8, w0, #192
331 ; CHECK-NEXT: and w8, w8, #0xffff
332 ; CHECK-NEXT: cmp w8, #256
333 ; CHECK-NEXT: cset w0, lo
335 %tmp0 = add i16 %x, 192 ; (1U << (8-1)) + (1U << (8-1-1))
336 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
340 ; Second constant is not power of two
341 define i1 @add_ultcmp_bad_i16_i8_c1notpoweroftwo(i16 %x) nounwind {
342 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_c1notpoweroftwo:
344 ; CHECK-NEXT: add w8, w0, #128
345 ; CHECK-NEXT: and w8, w8, #0xffff
346 ; CHECK-NEXT: cmp w8, #768
347 ; CHECK-NEXT: cset w0, lo
349 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
350 %tmp1 = icmp ult i16 %tmp0, 768 ; (1U << 8)) + (1U << (8+1))
354 ; Magic check fails, 64 << 1 != 256
355 define i1 @add_ultcmp_bad_i16_i8_magic(i16 %x) nounwind {
356 ; CHECK-LABEL: add_ultcmp_bad_i16_i8_magic:
358 ; CHECK-NEXT: add w8, w0, #64
359 ; CHECK-NEXT: and w8, w8, #0xffff
360 ; CHECK-NEXT: cmp w8, #256
361 ; CHECK-NEXT: cset w0, lo
363 %tmp0 = add i16 %x, 64 ; 1U << (8-1-1)
364 %tmp1 = icmp ult i16 %tmp0, 256 ; 1U << 8
368 ; Bad 'destination type'
369 define i1 @add_ultcmp_bad_i16_i4(i16 %x) nounwind {
370 ; CHECK-LABEL: add_ultcmp_bad_i16_i4:
372 ; CHECK-NEXT: add w8, w0, #8
373 ; CHECK-NEXT: and w8, w8, #0xffff
374 ; CHECK-NEXT: cmp w8, #16
375 ; CHECK-NEXT: cset w0, lo
377 %tmp0 = add i16 %x, 8 ; 1U << (4-1)
378 %tmp1 = icmp ult i16 %tmp0, 16 ; 1U << 4
383 define i1 @add_ultcmp_bad_i24_i8(i24 %x) nounwind {
384 ; CHECK-LABEL: add_ultcmp_bad_i24_i8:
386 ; CHECK-NEXT: add w8, w0, #128
387 ; CHECK-NEXT: and w8, w8, #0xffffff
388 ; CHECK-NEXT: cmp w8, #256
389 ; CHECK-NEXT: cset w0, lo
391 %tmp0 = add i24 %x, 128 ; 1U << (8-1)
392 %tmp1 = icmp ult i24 %tmp0, 256 ; 1U << 8
396 define i1 @add_ulecmp_bad_i16_i8(i16 %x) nounwind {
397 ; CHECK-LABEL: add_ulecmp_bad_i16_i8:
399 ; CHECK-NEXT: mov w0, #1 // =0x1
401 %tmp0 = add i16 %x, 128 ; 1U << (8-1)
402 %tmp1 = icmp ule i16 %tmp0, -1 ; when we +1 it, it will wrap to 0