1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 declare i8 @llvm.abs.i8(i8, i1)
5 declare i32 @llvm.abs.i32(i32, i1)
6 declare <4 x i32> @llvm.abs.v4i32(<4 x i32>, i1)
7 declare <3 x i82> @llvm.abs.v3i82(<3 x i82>, i1)
8 declare void @llvm.assume(i1)
10 ; abs preserves trailing zeros so the second and is unneeded
11 define i32 @abs_trailing_zeros(i32 %x) {
12 ; CHECK-LABEL: @abs_trailing_zeros(
13 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -4
14 ; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[AND]], i1 false)
15 ; CHECK-NEXT: ret i32 [[ABS]]
18 %abs = call i32 @llvm.abs.i32(i32 %and, i1 false)
19 %and2 = and i32 %abs, -2
23 define <4 x i32> @abs_trailing_zeros_vec(<4 x i32> %x) {
24 ; CHECK-LABEL: @abs_trailing_zeros_vec(
25 ; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[X:%.*]], <i32 -4, i32 -8, i32 -16, i32 -32>
26 ; CHECK-NEXT: [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[AND]], i1 false)
27 ; CHECK-NEXT: ret <4 x i32> [[ABS]]
29 %and = and <4 x i32> %x, <i32 -4, i32 -8, i32 -16, i32 -32>
30 %abs = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %and, i1 false)
31 %and2 = and <4 x i32> %abs, <i32 -2, i32 -2, i32 -2, i32 -2>
35 ; negative test, can't remove the second and based on trailing zeroes.
36 ; FIXME: Could remove the first and using demanded bits.
37 define i32 @abs_trailing_zeros_negative(i32 %x) {
38 ; CHECK-LABEL: @abs_trailing_zeros_negative(
39 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], -2
40 ; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[AND]], i1 false)
41 ; CHECK-NEXT: [[AND2:%.*]] = and i32 [[ABS]], -4
42 ; CHECK-NEXT: ret i32 [[AND2]]
45 %abs = call i32 @llvm.abs.i32(i32 %and, i1 false)
46 %and2 = and i32 %abs, -4
50 define <4 x i32> @abs_trailing_zeros_negative_vec(<4 x i32> %x) {
51 ; CHECK-LABEL: @abs_trailing_zeros_negative_vec(
52 ; CHECK-NEXT: [[AND:%.*]] = and <4 x i32> [[X:%.*]], <i32 -2, i32 -2, i32 -2, i32 -2>
53 ; CHECK-NEXT: [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[AND]], i1 false)
54 ; CHECK-NEXT: [[AND2:%.*]] = and <4 x i32> [[ABS]], <i32 -4, i32 -4, i32 -4, i32 -4>
55 ; CHECK-NEXT: ret <4 x i32> [[AND2]]
57 %and = and <4 x i32> %x, <i32 -2, i32 -2, i32 -2, i32 -2>
58 %abs = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %and, i1 false)
59 %and2 = and <4 x i32> %abs, <i32 -4, i32 -4, i32 -4, i32 -4>
63 ; Make sure we infer this add doesn't overflow. The input to the abs has 3
64 ; sign bits, the abs reduces this to 2 sign bits.
65 define i32 @abs_signbits(i30 %x) {
66 ; CHECK-LABEL: @abs_signbits(
67 ; CHECK-NEXT: [[TMP1:%.*]] = call i30 @llvm.abs.i30(i30 [[X:%.*]], i1 false)
68 ; CHECK-NEXT: [[NARROW:%.*]] = add nuw i30 [[TMP1]], 1
69 ; CHECK-NEXT: [[ADD:%.*]] = zext i30 [[NARROW]] to i32
70 ; CHECK-NEXT: ret i32 [[ADD]]
72 %ext = sext i30 %x to i32
73 %abs = call i32 @llvm.abs.i32(i32 %ext, i1 false)
74 %add = add i32 %abs, 1
78 define <4 x i32> @abs_signbits_vec(<4 x i30> %x) {
79 ; CHECK-LABEL: @abs_signbits_vec(
80 ; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i30> @llvm.abs.v4i30(<4 x i30> [[X:%.*]], i1 false)
81 ; CHECK-NEXT: [[NARROW:%.*]] = add nuw <4 x i30> [[TMP1]], <i30 1, i30 1, i30 1, i30 1>
82 ; CHECK-NEXT: [[ADD:%.*]] = zext <4 x i30> [[NARROW]] to <4 x i32>
83 ; CHECK-NEXT: ret <4 x i32> [[ADD]]
85 %ext = sext <4 x i30> %x to <4 x i32>
86 %abs = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %ext, i1 false)
87 %add = add <4 x i32> %abs, <i32 1, i32 1, i32 1, i32 1>
91 define i32 @abs_of_neg(i32 %x) {
92 ; CHECK-LABEL: @abs_of_neg(
93 ; CHECK-NEXT: [[B:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
94 ; CHECK-NEXT: ret i32 [[B]]
97 %b = call i32 @llvm.abs.i32(i32 %a, i1 false)
101 define <4 x i32> @abs_of_neg_vec(<4 x i32> %x) {
102 ; CHECK-LABEL: @abs_of_neg_vec(
103 ; CHECK-NEXT: [[B:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[X:%.*]], i1 false)
104 ; CHECK-NEXT: ret <4 x i32> [[B]]
106 %a = sub nsw <4 x i32> zeroinitializer, %x
107 %b = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %a, i1 false)
111 define i32 @abs_of_select_neg_true_val(i1 %b, i32 %x) {
112 ; CHECK-LABEL: @abs_of_select_neg_true_val(
113 ; CHECK-NEXT: [[ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 true)
114 ; CHECK-NEXT: ret i32 [[ABS]]
117 %sel = select i1 %b, i32 %neg, i32 %x
118 %abs = call i32 @llvm.abs.i32(i32 %sel, i1 true)
122 define <4 x i32> @abs_of_select_neg_false_val(<4 x i1> %b, <4 x i32> %x) {
123 ; CHECK-LABEL: @abs_of_select_neg_false_val(
124 ; CHECK-NEXT: [[ABS:%.*]] = call <4 x i32> @llvm.abs.v4i32(<4 x i32> [[X:%.*]], i1 false)
125 ; CHECK-NEXT: ret <4 x i32> [[ABS]]
127 %neg = sub <4 x i32> zeroinitializer, %x
128 %sel = select <4 x i1> %b, <4 x i32> %x, <4 x i32> %neg
129 %abs = call <4 x i32> @llvm.abs.v4i32(<4 x i32> %sel, i1 false)
133 define i32 @abs_dom_cond_nopoison(i32 %x) {
134 ; CHECK-LABEL: @abs_dom_cond_nopoison(
135 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1
136 ; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
138 ; CHECK-NEXT: ret i32 [[X]]
140 ; CHECK-NEXT: [[A2:%.*]] = sub i32 0, [[X]]
141 ; CHECK-NEXT: ret i32 [[A2]]
143 %cmp = icmp sge i32 %x, 0
144 br i1 %cmp, label %true, label %false
147 %a1 = call i32 @llvm.abs.i32(i32 %x, i1 false)
151 %a2 = call i32 @llvm.abs.i32(i32 %x, i1 false)
155 define i32 @abs_dom_cond_poison(i32 %x) {
156 ; CHECK-LABEL: @abs_dom_cond_poison(
157 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], -1
158 ; CHECK-NEXT: br i1 [[CMP]], label [[TRUE:%.*]], label [[FALSE:%.*]]
160 ; CHECK-NEXT: ret i32 [[X]]
162 ; CHECK-NEXT: [[A2:%.*]] = sub nsw i32 0, [[X]]
163 ; CHECK-NEXT: ret i32 [[A2]]
165 %cmp = icmp sge i32 %x, 0
166 br i1 %cmp, label %true, label %false
169 %a1 = call i32 @llvm.abs.i32(i32 %x, i1 true)
173 %a2 = call i32 @llvm.abs.i32(i32 %x, i1 true)
177 ; Abs argument non-neg based on known bits.
179 define i32 @zext_abs(i31 %x) {
180 ; CHECK-LABEL: @zext_abs(
181 ; CHECK-NEXT: [[ZEXT:%.*]] = zext i31 [[X:%.*]] to i32
182 ; CHECK-NEXT: ret i32 [[ZEXT]]
184 %zext = zext i31 %x to i32
185 %abs = call i32 @llvm.abs.i32(i32 %zext, i1 false)
189 define <3 x i82> @lshr_abs(<3 x i82> %x) {
190 ; CHECK-LABEL: @lshr_abs(
191 ; CHECK-NEXT: [[LSHR:%.*]] = lshr <3 x i82> [[X:%.*]], <i82 1, i82 1, i82 1>
192 ; CHECK-NEXT: ret <3 x i82> [[LSHR]]
194 %lshr = lshr <3 x i82> %x, <i82 1, i82 1, i82 1>
195 %abs = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %lshr, i1 true)
199 define i32 @and_abs(i32 %x) {
200 ; CHECK-LABEL: @and_abs(
201 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[X:%.*]], 2147483644
202 ; CHECK-NEXT: ret i32 [[AND]]
204 %and = and i32 %x, 2147483644
205 %abs = call i32 @llvm.abs.i32(i32 %and, i1 true)
209 define <3 x i82> @select_abs(<3 x i1> %cond) {
210 ; CHECK-LABEL: @select_abs(
211 ; CHECK-NEXT: [[SEL:%.*]] = select <3 x i1> [[COND:%.*]], <3 x i82> zeroinitializer, <3 x i82> <i82 2147483647, i82 42, i82 1>
212 ; CHECK-NEXT: ret <3 x i82> [[SEL]]
214 %sel = select <3 x i1> %cond, <3 x i82> zeroinitializer, <3 x i82> <i82 2147483647, i82 42, i82 1>
215 %abs = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %sel, i1 false)
219 define i32 @assume_abs(i32 %x) {
220 ; CHECK-LABEL: @assume_abs(
221 ; CHECK-NEXT: [[ASSUME:%.*]] = icmp sgt i32 [[X:%.*]], -1
222 ; CHECK-NEXT: call void @llvm.assume(i1 [[ASSUME]])
223 ; CHECK-NEXT: ret i32 [[X]]
225 %assume = icmp sge i32 %x, 0
226 call void @llvm.assume(i1 %assume)
227 %abs = call i32 @llvm.abs.i32(i32 %x, i1 true)
231 ; Abs argument negative based on known bits.
233 define i32 @abs_assume_neg(i32 %x) {
234 ; CHECK-LABEL: @abs_assume_neg(
235 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X:%.*]], 0
236 ; CHECK-NEXT: call void @llvm.assume(i1 [[CMP]])
237 ; CHECK-NEXT: [[ABS:%.*]] = sub i32 0, [[X]]
238 ; CHECK-NEXT: ret i32 [[ABS]]
240 %cmp = icmp slt i32 %x, 0
241 call void @llvm.assume(i1 %cmp)
242 %abs = call i32 @llvm.abs.i32(i32 %x, i1 false)
246 define i32 @abs_known_neg(i16 %x) {
247 ; CHECK-LABEL: @abs_known_neg(
248 ; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[X:%.*]] to i32
249 ; CHECK-NEXT: [[NEG_NEG:%.*]] = add nuw nsw i32 [[EXT]], 1
250 ; CHECK-NEXT: ret i32 [[NEG_NEG]]
252 %ext = zext i16 %x to i32
253 %neg = sub nsw i32 -1, %ext
254 %abs = call i32 @llvm.abs.i32(i32 %neg, i1 false)
258 define i1 @abs_eq_int_min_poison(i8 %x) {
259 ; CHECK-LABEL: @abs_eq_int_min_poison(
260 ; CHECK-NEXT: ret i1 false
262 %abs = call i8 @llvm.abs.i8(i8 %x, i1 true)
263 %cmp = icmp eq i8 %abs, -128
267 define i1 @abs_ne_int_min_poison(i8 %x) {
268 ; CHECK-LABEL: @abs_ne_int_min_poison(
269 ; CHECK-NEXT: ret i1 true
271 %abs = call i8 @llvm.abs.i8(i8 %x, i1 true)
272 %cmp = icmp ne i8 %abs, -128
276 define i1 @abs_eq_int_min_nopoison(i8 %x) {
277 ; CHECK-LABEL: @abs_eq_int_min_nopoison(
278 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[X:%.*]], -128
279 ; CHECK-NEXT: ret i1 [[CMP]]
281 %abs = call i8 @llvm.abs.i8(i8 %x, i1 false)
282 %cmp = icmp eq i8 %abs, -128
286 define i1 @abs_ne_int_min_nopoison(i8 %x) {
287 ; CHECK-LABEL: @abs_ne_int_min_nopoison(
288 ; CHECK-NEXT: [[CMP:%.*]] = icmp ne i8 [[X:%.*]], -128
289 ; CHECK-NEXT: ret i1 [[CMP]]
291 %abs = call i8 @llvm.abs.i8(i8 %x, i1 false)
292 %cmp = icmp ne i8 %abs, -128
296 define i32 @abs_sext(i8 %x) {
297 ; CHECK-LABEL: @abs_sext(
298 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 false)
299 ; CHECK-NEXT: [[A:%.*]] = zext i8 [[TMP1]] to i32
300 ; CHECK-NEXT: ret i32 [[A]]
302 %s = sext i8 %x to i32
303 %a = call i32 @llvm.abs.i32(i32 %s, i1 0)
307 define <3 x i82> @abs_nsw_sext(<3 x i7> %x) {
308 ; CHECK-LABEL: @abs_nsw_sext(
309 ; CHECK-NEXT: [[TMP1:%.*]] = call <3 x i7> @llvm.abs.v3i7(<3 x i7> [[X:%.*]], i1 false)
310 ; CHECK-NEXT: [[A:%.*]] = zext <3 x i7> [[TMP1]] to <3 x i82>
311 ; CHECK-NEXT: ret <3 x i82> [[A]]
313 %s = sext <3 x i7> %x to <3 x i82>
314 %a = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %s, i1 1)
318 define i32 @abs_sext_extra_use(i8 %x, i32* %p) {
319 ; CHECK-LABEL: @abs_sext_extra_use(
320 ; CHECK-NEXT: [[S:%.*]] = sext i8 [[X:%.*]] to i32
321 ; CHECK-NEXT: store i32 [[S]], i32* [[P:%.*]], align 4
322 ; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[S]], i1 false)
323 ; CHECK-NEXT: ret i32 [[A]]
325 %s = sext i8 %x to i32
326 store i32 %s, i32* %p
327 %a = call i32 @llvm.abs.i32(i32 %s, i1 0)
333 define i8 @trunc_abs_sext(i8 %x) {
334 ; CHECK-LABEL: @trunc_abs_sext(
335 ; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.abs.i8(i8 [[X:%.*]], i1 false)
336 ; CHECK-NEXT: ret i8 [[TMP1]]
338 %s = sext i8 %x to i32
339 %a = tail call i32 @llvm.abs.i32(i32 %s, i1 true)
340 %t = trunc i32 %a to i8
344 define <4 x i8> @trunc_abs_sext_vec(<4 x i8> %x) {
345 ; CHECK-LABEL: @trunc_abs_sext_vec(
346 ; CHECK-NEXT: [[TMP1:%.*]] = call <4 x i8> @llvm.abs.v4i8(<4 x i8> [[X:%.*]], i1 false)
347 ; CHECK-NEXT: ret <4 x i8> [[TMP1]]
349 %s = sext <4 x i8> %x to <4 x i32>
350 %a = tail call <4 x i32> @llvm.abs.v4i32(<4 x i32> %s, i1 true)
351 %t = trunc <4 x i32> %a to <4 x i8>
355 ; abs() doesn't change the low bit.
357 define i32 @demand_low_bit(i32 %x) {
358 ; CHECK-LABEL: @demand_low_bit(
359 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], 1
360 ; CHECK-NEXT: ret i32 [[R]]
362 %a = call i32 @llvm.abs.i32(i32 %x, i1 false)
367 ; Int min behavior doesn't affect the transform.
369 define <3 x i82> @demand_low_bit_int_min_is_poison(<3 x i82> %x) {
370 ; CHECK-LABEL: @demand_low_bit_int_min_is_poison(
371 ; CHECK-NEXT: [[R:%.*]] = shl <3 x i82> [[X:%.*]], <i82 81, i82 81, i82 81>
372 ; CHECK-NEXT: ret <3 x i82> [[R]]
374 %a = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %x, i1 true)
375 %r = shl <3 x i82> %a, <i82 81, i82 81, i82 81>
379 ; Negative test - only low bit is allowed.
381 define i32 @demand_low_bits(i32 %x) {
382 ; CHECK-LABEL: @demand_low_bits(
383 ; CHECK-NEXT: [[A:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
384 ; CHECK-NEXT: [[R:%.*]] = and i32 [[A]], 3
385 ; CHECK-NEXT: ret i32 [[R]]
387 %a = call i32 @llvm.abs.i32(i32 %x, i1 false)
392 define i32 @srem_by_2_int_min_is_poison(i32 %x) {
393 ; CHECK-LABEL: @srem_by_2_int_min_is_poison(
394 ; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], 1
395 ; CHECK-NEXT: ret i32 [[R]]
398 %r = call i32 @llvm.abs.i32(i32 %s, i1 true)
402 define <3 x i82> @srem_by_2(<3 x i82> %x, <3 x i82>* %p) {
403 ; CHECK-LABEL: @srem_by_2(
404 ; CHECK-NEXT: [[S:%.*]] = srem <3 x i82> [[X:%.*]], <i82 2, i82 2, i82 2>
405 ; CHECK-NEXT: store <3 x i82> [[S]], <3 x i82>* [[P:%.*]], align 32
406 ; CHECK-NEXT: [[R:%.*]] = and <3 x i82> [[X]], <i82 1, i82 1, i82 1>
407 ; CHECK-NEXT: ret <3 x i82> [[R]]
409 %s = srem <3 x i82> %x, <i82 2, i82 2, i82 2>
410 store <3 x i82> %s, <3 x i82>* %p
411 %r = call <3 x i82> @llvm.abs.v3i82(<3 x i82> %s, i1 false)
415 ; TODO: A more general transform could sink the srem and turn it into urem.
417 define i32 @srem_by_3(i32 %x) {
418 ; CHECK-LABEL: @srem_by_3(
419 ; CHECK-NEXT: [[S:%.*]] = srem i32 [[X:%.*]], 3
420 ; CHECK-NEXT: [[R:%.*]] = call i32 @llvm.abs.i32(i32 [[S]], i1 true)
421 ; CHECK-NEXT: ret i32 [[R]]
424 %r = call i32 @llvm.abs.i32(i32 %s, i1 true)