1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-p:64:64:64-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
6 define i32 @sub_constant(i32 %x) {
7 ; CHECK-LABEL: @sub_constant(
8 ; CHECK-NEXT: [[R:%.*]] = add i32 [[X:%.*]], -42
9 ; CHECK-NEXT: ret i32 [[R]]
17 define i32 @sub_constant_expression(i32 %x) {
18 ; CHECK-LABEL: @sub_constant_expression(
19 ; CHECK-NEXT: [[R:%.*]] = sub i32 [[X:%.*]], ptrtoint (i32* @g to i32)
20 ; CHECK-NEXT: ret i32 [[R]]
22 %r = sub i32 %x, ptrtoint (i32* @g to i32)
26 define <2 x i32> @sub_constant_vec(<2 x i32> %x) {
27 ; CHECK-LABEL: @sub_constant_vec(
28 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], <i32 -42, i32 12>
29 ; CHECK-NEXT: ret <2 x i32> [[R]]
31 %r = sub <2 x i32> %x, <i32 42, i32 -12>
35 define <3 x i33> @sub_constant_vec_weird_type(<3 x i33> %x) {
36 ; CHECK-LABEL: @sub_constant_vec_weird_type(
37 ; CHECK-NEXT: [[R:%.*]] = add <3 x i33> [[X:%.*]], <i33 42, i33 -42, i33 12>
38 ; CHECK-NEXT: ret <3 x i33> [[R]]
40 %r = sub <3 x i33> %x, <i33 -42, i33 42, i33 -12>
44 define <4 x i32> @sub_constant_expression_vec(<4 x i32> %x) {
45 ; CHECK-LABEL: @sub_constant_expression_vec(
46 ; CHECK-NEXT: [[R:%.*]] = sub <4 x i32> [[X:%.*]], bitcast (i128 ptrtoint (i32* @g to i128) to <4 x i32>)
47 ; CHECK-NEXT: ret <4 x i32> [[R]]
49 %r = sub <4 x i32> %x, bitcast (i128 ptrtoint (i32* @g to i128) to <4 x i32>)
53 define i32 @neg_sub(i32 %x, i32 %y) {
54 ; CHECK-LABEL: @neg_sub(
55 ; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
56 ; CHECK-NEXT: ret i32 [[R]]
63 define i32 @neg_nsw_sub(i32 %x, i32 %y) {
64 ; CHECK-LABEL: @neg_nsw_sub(
65 ; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
66 ; CHECK-NEXT: ret i32 [[R]]
68 %neg = sub nsw i32 0, %x
73 define i32 @neg_sub_nsw(i32 %x, i32 %y) {
74 ; CHECK-LABEL: @neg_sub_nsw(
75 ; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
76 ; CHECK-NEXT: ret i32 [[R]]
79 %r = sub nsw i32 %y, %neg
83 define i32 @neg_nsw_sub_nsw(i32 %x, i32 %y) {
84 ; CHECK-LABEL: @neg_nsw_sub_nsw(
85 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[Y:%.*]], [[X:%.*]]
86 ; CHECK-NEXT: ret i32 [[R]]
88 %neg = sub nsw i32 0, %x
89 %r = sub nsw i32 %y, %neg
93 define <2 x i32> @neg_sub_vec(<2 x i32> %x, <2 x i32> %y) {
94 ; CHECK-LABEL: @neg_sub_vec(
95 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
96 ; CHECK-NEXT: ret <2 x i32> [[R]]
98 %neg = sub <2 x i32> zeroinitializer, %x
99 %r = sub <2 x i32> %y, %neg
103 define <2 x i32> @neg_nsw_sub_vec(<2 x i32> %x, <2 x i32> %y) {
104 ; CHECK-LABEL: @neg_nsw_sub_vec(
105 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
106 ; CHECK-NEXT: ret <2 x i32> [[R]]
108 %neg = sub nsw <2 x i32> zeroinitializer, %x
109 %r = sub <2 x i32> %y, %neg
113 define <2 x i32> @neg_sub_nsw_vec(<2 x i32> %x, <2 x i32> %y) {
114 ; CHECK-LABEL: @neg_sub_nsw_vec(
115 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
116 ; CHECK-NEXT: ret <2 x i32> [[R]]
118 %neg = sub <2 x i32> zeroinitializer, %x
119 %r = sub nsw <2 x i32> %y, %neg
123 define <2 x i32> @neg_nsw_sub_nsw_vec(<2 x i32> %x, <2 x i32> %y) {
124 ; CHECK-LABEL: @neg_nsw_sub_nsw_vec(
125 ; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[Y:%.*]], [[X:%.*]]
126 ; CHECK-NEXT: ret <2 x i32> [[R]]
128 %neg = sub nsw <2 x i32> zeroinitializer, %x
129 %r = sub nsw <2 x i32> %y, %neg
133 define <2 x i32> @neg_sub_vec_undef(<2 x i32> %x, <2 x i32> %y) {
134 ; CHECK-LABEL: @neg_sub_vec_undef(
135 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
136 ; CHECK-NEXT: ret <2 x i32> [[R]]
138 %neg = sub <2 x i32> <i32 0, i32 undef>, %x
139 %r = sub <2 x i32> %y, %neg
143 define <2 x i32> @neg_nsw_sub_vec_undef(<2 x i32> %x, <2 x i32> %y) {
144 ; CHECK-LABEL: @neg_nsw_sub_vec_undef(
145 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
146 ; CHECK-NEXT: ret <2 x i32> [[R]]
148 %neg = sub nsw <2 x i32> <i32 undef, i32 0>, %x
149 %r = sub <2 x i32> %y, %neg
153 define <2 x i32> @neg_sub_nsw_vec_undef(<2 x i32> %x, <2 x i32> %y) {
154 ; CHECK-LABEL: @neg_sub_nsw_vec_undef(
155 ; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]]
156 ; CHECK-NEXT: ret <2 x i32> [[R]]
158 %neg = sub <2 x i32> <i32 undef, i32 0>, %x
159 %r = sub nsw <2 x i32> %y, %neg
163 ; This should not drop 'nsw'.
165 define <2 x i32> @neg_nsw_sub_nsw_vec_undef(<2 x i32> %x, <2 x i32> %y) {
166 ; CHECK-LABEL: @neg_nsw_sub_nsw_vec_undef(
167 ; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[Y:%.*]], [[X:%.*]]
168 ; CHECK-NEXT: ret <2 x i32> [[R]]
170 %neg = sub nsw <2 x i32> <i32 0, i32 undef>, %x
171 %r = sub nsw <2 x i32> %y, %neg
175 ; (~X) - (~Y) --> Y - X
176 ; Also, show that we can handle extra uses and vectors.
178 declare void @use8(i8)
180 define i8 @notnotsub(i8 %x, i8 %y) {
181 ; CHECK-LABEL: @notnotsub(
182 ; CHECK-NEXT: [[NX:%.*]] = xor i8 [[X:%.*]], -1
183 ; CHECK-NEXT: [[NY:%.*]] = xor i8 [[Y:%.*]], -1
184 ; CHECK-NEXT: [[SUB:%.*]] = sub i8 [[Y]], [[X]]
185 ; CHECK-NEXT: call void @use8(i8 [[NX]])
186 ; CHECK-NEXT: call void @use8(i8 [[NY]])
187 ; CHECK-NEXT: ret i8 [[SUB]]
191 %sub = sub i8 %nx, %ny
192 call void @use8(i8 %nx)
193 call void @use8(i8 %ny)
197 define <2 x i8> @notnotsub_vec(<2 x i8> %x, <2 x i8> %y) {
198 ; CHECK-LABEL: @notnotsub_vec(
199 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i8> [[Y:%.*]], [[X:%.*]]
200 ; CHECK-NEXT: ret <2 x i8> [[SUB]]
202 %nx = xor <2 x i8> %x, <i8 -1, i8 -1>
203 %ny = xor <2 x i8> %y, <i8 -1, i8 -1>
204 %sub = sub <2 x i8> %nx, %ny
208 define <2 x i8> @notnotsub_vec_undef_elts(<2 x i8> %x, <2 x i8> %y) {
209 ; CHECK-LABEL: @notnotsub_vec_undef_elts(
210 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i8> [[Y:%.*]], [[X:%.*]]
211 ; CHECK-NEXT: ret <2 x i8> [[SUB]]
213 %nx = xor <2 x i8> %x, <i8 undef, i8 -1>
214 %ny = xor <2 x i8> %y, <i8 -1, i8 undef>
215 %sub = sub <2 x i8> %nx, %ny
219 define i32 @test5(i32 %A, i32 %B, i32 %C) {
220 ; CHECK-LABEL: @test5(
221 ; CHECK-NEXT: [[D1:%.*]] = sub i32 [[C:%.*]], [[B:%.*]]
222 ; CHECK-NEXT: [[E:%.*]] = add i32 [[D1]], [[A:%.*]]
223 ; CHECK-NEXT: ret i32 [[E]]
230 define i32 @test6(i32 %A, i32 %B) {
231 ; CHECK-LABEL: @test6(
232 ; CHECK-NEXT: [[B_NOT:%.*]] = xor i32 [[B:%.*]], -1
233 ; CHECK-NEXT: [[D:%.*]] = and i32 [[B_NOT]], [[A:%.*]]
234 ; CHECK-NEXT: ret i32 [[D]]
241 define i32 @test6commuted(i32 %A, i32 %B) {
242 ; CHECK-LABEL: @test6commuted(
243 ; CHECK-NEXT: [[B_NOT:%.*]] = xor i32 [[B:%.*]], -1
244 ; CHECK-NEXT: [[D:%.*]] = and i32 [[B_NOT]], [[A:%.*]]
245 ; CHECK-NEXT: ret i32 [[D]]
252 define i32 @test7(i32 %A) {
253 ; CHECK-LABEL: @test7(
254 ; CHECK-NEXT: [[B:%.*]] = xor i32 [[A:%.*]], -1
255 ; CHECK-NEXT: ret i32 [[B]]
261 define i32 @test8(i32 %A) {
262 ; CHECK-LABEL: @test8(
263 ; CHECK-NEXT: [[C:%.*]] = shl i32 [[A:%.*]], 3
264 ; CHECK-NEXT: ret i32 [[C]]
271 define i32 @test9(i32 %A) {
272 ; CHECK-LABEL: @test9(
273 ; CHECK-NEXT: [[C:%.*]] = mul i32 [[A:%.*]], -2
274 ; CHECK-NEXT: ret i32 [[C]]
281 define i1 @test11(i8 %A, i8 %B) {
282 ; CHECK-LABEL: @test11(
283 ; CHECK-NEXT: [[D:%.*]] = icmp ne i8 [[A:%.*]], [[B:%.*]]
284 ; CHECK-NEXT: ret i1 [[D]]
287 %D = icmp ne i8 %C, 0
291 define <2 x i1> @test11vec(<2 x i8> %A, <2 x i8> %B) {
292 ; CHECK-LABEL: @test11vec(
293 ; CHECK-NEXT: [[D:%.*]] = icmp ne <2 x i8> [[A:%.*]], [[B:%.*]]
294 ; CHECK-NEXT: ret <2 x i1> [[D]]
296 %C = sub <2 x i8> %A, %B
297 %D = icmp ne <2 x i8> %C, zeroinitializer
301 define i32 @test12(i32 %A) {
302 ; CHECK-LABEL: @test12(
303 ; CHECK-NEXT: [[C:%.*]] = lshr i32 [[A:%.*]], 31
304 ; CHECK-NEXT: ret i32 [[C]]
311 define i32 @test13(i32 %A) {
312 ; CHECK-LABEL: @test13(
313 ; CHECK-NEXT: [[C:%.*]] = ashr i32 [[A:%.*]], 31
314 ; CHECK-NEXT: ret i32 [[C]]
321 define <2 x i32> @test12vec(<2 x i32> %A) {
322 ; CHECK-LABEL: @test12vec(
323 ; CHECK-NEXT: [[C:%.*]] = lshr <2 x i32> [[A:%.*]], <i32 31, i32 31>
324 ; CHECK-NEXT: ret <2 x i32> [[C]]
326 %B = ashr <2 x i32> %A, <i32 31, i32 31>
327 %C = sub <2 x i32> zeroinitializer, %B
331 define <2 x i32> @test13vec(<2 x i32> %A) {
332 ; CHECK-LABEL: @test13vec(
333 ; CHECK-NEXT: [[C:%.*]] = ashr <2 x i32> [[A:%.*]], <i32 31, i32 31>
334 ; CHECK-NEXT: ret <2 x i32> [[C]]
336 %B = lshr <2 x i32> %A, <i32 31, i32 31>
337 %C = sub <2 x i32> zeroinitializer, %B
341 define i32 @test15(i32 %A, i32 %B) {
342 ; CHECK-LABEL: @test15(
343 ; CHECK-NEXT: [[C:%.*]] = sub i32 0, [[A:%.*]]
344 ; CHECK-NEXT: [[D:%.*]] = srem i32 [[B:%.*]], [[C]]
345 ; CHECK-NEXT: ret i32 [[D]]
352 define i32 @test16(i32 %A) {
353 ; CHECK-LABEL: @test16(
354 ; CHECK-NEXT: [[Y:%.*]] = sdiv i32 [[A:%.*]], -1123
355 ; CHECK-NEXT: ret i32 [[Y]]
357 %X = sdiv i32 %A, 1123
362 ; Can't fold subtract here because negation it might oveflow.
364 define i32 @test17(i32 %A) {
365 ; CHECK-LABEL: @test17(
366 ; CHECK-NEXT: [[B:%.*]] = sub i32 0, [[A:%.*]]
367 ; CHECK-NEXT: [[C:%.*]] = sdiv i32 [[B]], 1234
368 ; CHECK-NEXT: ret i32 [[C]]
371 %C = sdiv i32 %B, 1234
375 define i64 @test18(i64 %Y) {
376 ; CHECK-LABEL: @test18(
377 ; CHECK-NEXT: ret i64 0
379 %tmp.4 = shl i64 %Y, 2
380 %tmp.12 = shl i64 %Y, 2
381 %tmp.8 = sub i64 %tmp.4, %tmp.12
385 define i1 @test20(i32 %g, i32 %h) {
386 ; CHECK-LABEL: @test20(
387 ; CHECK-NEXT: [[TMP_4:%.*]] = icmp ne i32 [[H:%.*]], 0
388 ; CHECK-NEXT: ret i1 [[TMP_4]]
390 %tmp.2 = sub i32 %g, %h
391 %tmp.4 = icmp ne i32 %tmp.2, %g
395 define i1 @test21(i32 %g, i32 %h) {
396 ; CHECK-LABEL: @test21(
397 ; CHECK-NEXT: [[TMP_4:%.*]] = icmp ne i32 [[H:%.*]], 0
398 ; CHECK-NEXT: ret i1 [[TMP_4]]
400 %tmp.2 = sub i32 %g, %h
401 %tmp.4 = icmp ne i32 %tmp.2, %g
406 define zeroext i1 @test22(i32 %a, i32 %b) nounwind {
407 ; CHECK-LABEL: @test22(
408 ; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i32 [[B:%.*]], [[A:%.*]]
409 ; CHECK-NEXT: ret i1 [[TMP5]]
411 %tmp2 = sub i32 0, %a
412 %tmp4 = sub i32 0, %b
413 %tmp5 = icmp eq i32 %tmp2, %tmp4
418 define i32 @test23(i8* %P, i64 %A){
419 ; CHECK-LABEL: @test23(
420 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[A:%.*]] to i32
421 ; CHECK-NEXT: ret i32 [[TMP1]]
423 %B = getelementptr inbounds i8, i8* %P, i64 %A
424 %C = ptrtoint i8* %B to i64
425 %D = trunc i64 %C to i32
426 %E = ptrtoint i8* %P to i64
427 %F = trunc i64 %E to i32
432 define i8 @test23_as1(i8 addrspace(1)* %P, i16 %A) {
433 ; CHECK-LABEL: @test23_as1(
434 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i16 [[A:%.*]] to i8
435 ; CHECK-NEXT: ret i8 [[TMP1]]
437 %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
438 %C = ptrtoint i8 addrspace(1)* %B to i16
439 %D = trunc i16 %C to i8
440 %E = ptrtoint i8 addrspace(1)* %P to i16
441 %F = trunc i16 %E to i8
446 define i64 @test24(i8* %P, i64 %A){
447 ; CHECK-LABEL: @test24(
448 ; CHECK-NEXT: ret i64 [[A:%.*]]
450 %B = getelementptr inbounds i8, i8* %P, i64 %A
451 %C = ptrtoint i8* %B to i64
452 %E = ptrtoint i8* %P to i64
457 define i16 @test24_as1(i8 addrspace(1)* %P, i16 %A) {
458 ; CHECK-LABEL: @test24_as1(
459 ; CHECK-NEXT: ret i16 [[A:%.*]]
461 %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
462 %C = ptrtoint i8 addrspace(1)* %B to i16
463 %E = ptrtoint i8 addrspace(1)* %P to i16
468 define i64 @test24a(i8* %P, i64 %A){
469 ; CHECK-LABEL: @test24a(
470 ; CHECK-NEXT: [[DIFF_NEG:%.*]] = sub i64 0, [[A:%.*]]
471 ; CHECK-NEXT: ret i64 [[DIFF_NEG]]
473 %B = getelementptr inbounds i8, i8* %P, i64 %A
474 %C = ptrtoint i8* %B to i64
475 %E = ptrtoint i8* %P to i64
480 define i16 @test24a_as1(i8 addrspace(1)* %P, i16 %A) {
481 ; CHECK-LABEL: @test24a_as1(
482 ; CHECK-NEXT: [[DIFF_NEG:%.*]] = sub i16 0, [[A:%.*]]
483 ; CHECK-NEXT: ret i16 [[DIFF_NEG]]
485 %B = getelementptr inbounds i8, i8 addrspace(1)* %P, i16 %A
486 %C = ptrtoint i8 addrspace(1)* %B to i16
487 %E = ptrtoint i8 addrspace(1)* %P to i16
493 @Arr = external global [42 x i16]
495 define i64 @test24b(i8* %P, i64 %A){
496 ; CHECK-LABEL: @test24b(
497 ; CHECK-NEXT: [[B_IDX:%.*]] = shl nuw i64 [[A:%.*]], 1
498 ; CHECK-NEXT: ret i64 [[B_IDX]]
500 %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A
501 %C = ptrtoint i16* %B to i64
502 %G = sub i64 %C, ptrtoint ([42 x i16]* @Arr to i64)
507 define i64 @test25(i8* %P, i64 %A){
508 ; CHECK-LABEL: @test25(
509 ; CHECK-NEXT: [[B_IDX:%.*]] = shl nuw i64 [[A:%.*]], 1
510 ; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[B_IDX]], -84
511 ; CHECK-NEXT: ret i64 [[TMP1]]
513 %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A
514 %C = ptrtoint i16* %B to i64
515 %G = sub i64 %C, ptrtoint (i16* getelementptr ([42 x i16], [42 x i16]* @Arr, i64 1, i64 0) to i64)
519 @Arr_as1 = external addrspace(1) global [42 x i16]
521 define i16 @test25_as1(i8 addrspace(1)* %P, i64 %A) {
522 ; CHECK-LABEL: @test25_as1(
523 ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[A:%.*]] to i16
524 ; CHECK-NEXT: [[B_IDX:%.*]] = shl nuw i16 [[TMP1]], 1
525 ; CHECK-NEXT: [[TMP2:%.*]] = add i16 [[B_IDX]], -84
526 ; CHECK-NEXT: ret i16 [[TMP2]]
528 %B = getelementptr inbounds [42 x i16], [42 x i16] addrspace(1)* @Arr_as1, i64 0, i64 %A
529 %C = ptrtoint i16 addrspace(1)* %B to i16
530 %G = sub i16 %C, ptrtoint (i16 addrspace(1)* getelementptr ([42 x i16], [42 x i16] addrspace(1)* @Arr_as1, i64 1, i64 0) to i16)
534 define i32 @test26(i32 %x) {
535 ; CHECK-LABEL: @test26(
536 ; CHECK-NEXT: [[NEG:%.*]] = shl i32 -3, [[X:%.*]]
537 ; CHECK-NEXT: ret i32 [[NEG]]
540 %neg = sub i32 0, %shl
544 define i32 @test27(i32 %x, i32 %y) {
545 ; CHECK-LABEL: @test27(
546 ; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[Y:%.*]], 3
547 ; CHECK-NEXT: [[SUB:%.*]] = add i32 [[TMP1]], [[X:%.*]]
548 ; CHECK-NEXT: ret i32 [[SUB]]
550 %mul = mul i32 %y, -8
551 %sub = sub i32 %x, %mul
555 define <2 x i32> @test27vec(<2 x i32> %x, <2 x i32> %y) {
556 ; CHECK-LABEL: @test27vec(
557 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[Y:%.*]], <i32 8, i32 6>
558 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
559 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
561 %mul = mul <2 x i32> %y, <i32 -8, i32 -6>
562 %sub = sub <2 x i32> %x, %mul
566 define <2 x i32> @test27vecsplat(<2 x i32> %x, <2 x i32> %y) {
567 ; CHECK-LABEL: @test27vecsplat(
568 ; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[Y:%.*]], <i32 3, i32 3>
569 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
570 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
572 %mul = mul <2 x i32> %y, <i32 -8, i32 -8>
573 %sub = sub <2 x i32> %x, %mul
577 define <2 x i32> @test27vecmixed(<2 x i32> %x, <2 x i32> %y) {
578 ; CHECK-LABEL: @test27vecmixed(
579 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[Y:%.*]], <i32 8, i32 -8>
580 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
581 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
583 %mul = mul <2 x i32> %y, <i32 -8, i32 8>
584 %sub = sub <2 x i32> %x, %mul
588 define i32 @test27commuted(i32 %x, i32 %y) {
589 ; CHECK-LABEL: @test27commuted(
590 ; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[Y:%.*]], 3
591 ; CHECK-NEXT: [[SUB:%.*]] = add i32 [[TMP1]], [[X:%.*]]
592 ; CHECK-NEXT: ret i32 [[SUB]]
594 %mul = mul i32 -8, %y
595 %sub = sub i32 %x, %mul
599 define <2 x i32> @test27commutedvec(<2 x i32> %x, <2 x i32> %y) {
600 ; CHECK-LABEL: @test27commutedvec(
601 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[Y:%.*]], <i32 8, i32 6>
602 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
603 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
605 %mul = mul <2 x i32> <i32 -8, i32 -6>, %y
606 %sub = sub <2 x i32> %x, %mul
610 define <2 x i32> @test27commutedvecsplat(<2 x i32> %x, <2 x i32> %y) {
611 ; CHECK-LABEL: @test27commutedvecsplat(
612 ; CHECK-NEXT: [[TMP1:%.*]] = shl <2 x i32> [[Y:%.*]], <i32 3, i32 3>
613 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
614 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
616 %mul = mul <2 x i32> <i32 -8, i32 -8>, %y
617 %sub = sub <2 x i32> %x, %mul
621 define <2 x i32> @test27commutedvecmixed(<2 x i32> %x, <2 x i32> %y) {
622 ; CHECK-LABEL: @test27commutedvecmixed(
623 ; CHECK-NEXT: [[TMP1:%.*]] = mul <2 x i32> [[Y:%.*]], <i32 8, i32 -8>
624 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i32> [[TMP1]], [[X:%.*]]
625 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
627 %mul = mul <2 x i32> <i32 -8, i32 8>, %y
628 %sub = sub <2 x i32> %x, %mul
632 define i32 @test28(i32 %x, i32 %y, i32 %z) {
633 ; CHECK-LABEL: @test28(
634 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[Z:%.*]], [[Y:%.*]]
635 ; CHECK-NEXT: [[SUB:%.*]] = add i32 [[TMP1]], [[X:%.*]]
636 ; CHECK-NEXT: ret i32 [[SUB]]
639 %mul = mul i32 %neg, %y
640 %sub = sub i32 %x, %mul
644 define i32 @test28commuted(i32 %x, i32 %y, i32 %z) {
645 ; CHECK-LABEL: @test28commuted(
646 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[Z:%.*]], [[Y:%.*]]
647 ; CHECK-NEXT: [[SUB:%.*]] = add i32 [[TMP1]], [[X:%.*]]
648 ; CHECK-NEXT: ret i32 [[SUB]]
651 %mul = mul i32 %y, %neg
652 %sub = sub i32 %x, %mul
656 define i64 @test29(i8* %foo, i64 %i, i64 %j) {
657 ; CHECK-LABEL: @test29(
658 ; CHECK-NEXT: [[TMP1:%.*]] = sub i64 [[I:%.*]], [[J:%.*]]
659 ; CHECK-NEXT: ret i64 [[TMP1]]
661 %gep1 = getelementptr inbounds i8, i8* %foo, i64 %i
662 %gep2 = getelementptr inbounds i8, i8* %foo, i64 %j
663 %cast1 = ptrtoint i8* %gep1 to i64
664 %cast2 = ptrtoint i8* %gep2 to i64
665 %sub = sub i64 %cast1, %cast2
669 define i64 @test30(i8* %foo, i64 %i, i64 %j) {
670 ; CHECK-LABEL: @test30(
671 ; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl nuw i64 [[I:%.*]], 2
672 ; CHECK-NEXT: [[TMP1:%.*]] = sub i64 [[GEP1_IDX]], [[J:%.*]]
673 ; CHECK-NEXT: ret i64 [[TMP1]]
675 %bit = bitcast i8* %foo to i32*
676 %gep1 = getelementptr inbounds i32, i32* %bit, i64 %i
677 %gep2 = getelementptr inbounds i8, i8* %foo, i64 %j
678 %cast1 = ptrtoint i32* %gep1 to i64
679 %cast2 = ptrtoint i8* %gep2 to i64
680 %sub = sub i64 %cast1, %cast2
684 define i16 @test30_as1(i8 addrspace(1)* %foo, i16 %i, i16 %j) {
685 ; CHECK-LABEL: @test30_as1(
686 ; CHECK-NEXT: [[GEP1_IDX:%.*]] = shl nuw i16 [[I:%.*]], 2
687 ; CHECK-NEXT: [[TMP1:%.*]] = sub i16 [[GEP1_IDX]], [[J:%.*]]
688 ; CHECK-NEXT: ret i16 [[TMP1]]
690 %bit = bitcast i8 addrspace(1)* %foo to i32 addrspace(1)*
691 %gep1 = getelementptr inbounds i32, i32 addrspace(1)* %bit, i16 %i
692 %gep2 = getelementptr inbounds i8, i8 addrspace(1)* %foo, i16 %j
693 %cast1 = ptrtoint i32 addrspace(1)* %gep1 to i16
694 %cast2 = ptrtoint i8 addrspace(1)* %gep2 to i16
695 %sub = sub i16 %cast1, %cast2
699 define <2 x i64> @test31(<2 x i64> %A) {
700 ; CHECK-LABEL: @test31(
701 ; CHECK-NEXT: [[SUB:%.*]] = add <2 x i64> [[A:%.*]], <i64 3, i64 4>
702 ; CHECK-NEXT: ret <2 x i64> [[SUB]]
704 %xor = xor <2 x i64> %A, <i64 -1, i64 -1>
705 %sub = sub <2 x i64> <i64 2, i64 3>, %xor
709 define <2 x i64> @test32(<2 x i64> %A) {
710 ; CHECK-LABEL: @test32(
711 ; CHECK-NEXT: [[SUB:%.*]] = sub <2 x i64> <i64 3, i64 4>, [[A:%.*]]
712 ; CHECK-NEXT: ret <2 x i64> [[SUB]]
714 %add = add <2 x i64> %A, <i64 -1, i64 -1>
715 %sub = sub <2 x i64> <i64 2, i64 3>, %add
719 define <2 x i64> @test35(<2 x i64> %A) {
720 ; CHECK-LABEL: @test35(
721 ; CHECK-NEXT: [[SUB:%.*]] = mul <2 x i64> [[A:%.*]], <i64 -2, i64 -3>
722 ; CHECK-NEXT: ret <2 x i64> [[SUB]]
724 %mul = mul <2 x i64> %A, <i64 3, i64 4>
725 %sub = sub <2 x i64> %A, %mul
729 define <2 x i64> @test36(<2 x i64> %A) {
730 ; CHECK-LABEL: @test36(
731 ; CHECK-NEXT: [[SUB:%.*]] = mul <2 x i64> [[A:%.*]], <i64 7, i64 15>
732 ; CHECK-NEXT: ret <2 x i64> [[SUB]]
734 %shl = shl <2 x i64> %A, <i64 3, i64 4>
735 %sub = sub <2 x i64> %shl, %A
739 define <2 x i32> @test37(<2 x i32> %A) {
740 ; CHECK-LABEL: @test37(
741 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[A:%.*]], <i32 -2147483648, i32 -2147483648>
742 ; CHECK-NEXT: [[SUB:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i32>
743 ; CHECK-NEXT: ret <2 x i32> [[SUB]]
745 %div = sdiv <2 x i32> %A, <i32 -2147483648, i32 -2147483648>
746 %sub = sub nsw <2 x i32> zeroinitializer, %div
750 define i32 @test38(i32 %A) {
751 ; CHECK-LABEL: @test38(
752 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[A:%.*]], -2147483648
753 ; CHECK-NEXT: [[SUB:%.*]] = sext i1 [[TMP1]] to i32
754 ; CHECK-NEXT: ret i32 [[SUB]]
756 %div = sdiv i32 %A, -2147483648
757 %sub = sub nsw i32 0, %div
761 define i16 @test40(i16 %a, i16 %b) {
762 ; CHECK-LABEL: @test40(
763 ; CHECK-NEXT: [[ASHR:%.*]] = ashr i16 [[A:%.*]], 1
764 ; CHECK-NEXT: [[ASHR1:%.*]] = ashr i16 [[B:%.*]], 1
765 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i16 [[ASHR]], [[ASHR1]]
766 ; CHECK-NEXT: ret i16 [[SUB]]
768 %ashr = ashr i16 %a, 1
769 %ashr1 = ashr i16 %b, 1
770 %sub = sub i16 %ashr, %ashr1
774 define i32 @test41(i16 %a, i16 %b) {
775 ; CHECK-LABEL: @test41(
776 ; CHECK-NEXT: [[CONV:%.*]] = sext i16 [[A:%.*]] to i32
777 ; CHECK-NEXT: [[CONV1:%.*]] = sext i16 [[B:%.*]] to i32
778 ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[CONV]], [[CONV1]]
779 ; CHECK-NEXT: ret i32 [[SUB]]
781 %conv = sext i16 %a to i32
782 %conv1 = sext i16 %b to i32
783 %sub = sub i32 %conv, %conv1
787 define i4 @test42(i4 %x, i4 %y) {
788 ; CHECK-LABEL: @test42(
789 ; CHECK-NEXT: [[A:%.*]] = and i4 [[Y:%.*]], 7
790 ; CHECK-NEXT: [[B:%.*]] = and i4 [[X:%.*]], 7
791 ; CHECK-NEXT: [[C:%.*]] = sub nsw i4 [[A]], [[B]]
792 ; CHECK-NEXT: ret i4 [[C]]
800 define i4 @test43(i4 %x, i4 %y) {
801 ; CHECK-LABEL: @test43(
802 ; CHECK-NEXT: [[A:%.*]] = or i4 [[X:%.*]], -8
803 ; CHECK-NEXT: [[B:%.*]] = and i4 [[Y:%.*]], 7
804 ; CHECK-NEXT: [[C:%.*]] = sub nuw i4 [[A]], [[B]]
805 ; CHECK-NEXT: ret i4 [[C]]
813 define i32 @test44(i32 %x) {
814 ; CHECK-LABEL: @test44(
815 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[X:%.*]], -32768
816 ; CHECK-NEXT: ret i32 [[SUB]]
818 %sub = sub nsw i32 %x, 32768
822 define i32 @test45(i32 %x, i32 %y) {
823 ; CHECK-LABEL: @test45(
824 ; CHECK-NEXT: [[SUB:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
825 ; CHECK-NEXT: ret i32 [[SUB]]
828 %xor = xor i32 %x, %y
829 %sub = sub i32 %or, %xor
833 define i32 @test45commuted(i32 %x, i32 %y) {
834 ; CHECK-LABEL: @test45commuted(
835 ; CHECK-NEXT: [[SUB:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
836 ; CHECK-NEXT: ret i32 [[SUB]]
839 %xor = xor i32 %y, %x
840 %sub = sub i32 %or, %xor
844 define i32 @test46(i32 %x, i32 %y) {
845 ; CHECK-LABEL: @test46(
846 ; CHECK-NEXT: [[X_NOT:%.*]] = xor i32 [[X:%.*]], -1
847 ; CHECK-NEXT: [[SUB:%.*]] = and i32 [[X_NOT]], [[Y:%.*]]
848 ; CHECK-NEXT: ret i32 [[SUB]]
851 %sub = sub i32 %or, %x
855 define i32 @test46commuted(i32 %x, i32 %y) {
856 ; CHECK-LABEL: @test46commuted(
857 ; CHECK-NEXT: [[X_NOT:%.*]] = xor i32 [[X:%.*]], -1
858 ; CHECK-NEXT: [[SUB:%.*]] = and i32 [[X_NOT]], [[Y:%.*]]
859 ; CHECK-NEXT: ret i32 [[SUB]]
862 %sub = sub i32 %or, %x
866 define i32 @test47(i1 %A, i32 %B, i32 %C, i32 %D) {
867 ; CHECK-LABEL: @test47(
868 ; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[D:%.*]], [[C:%.*]]
869 ; CHECK-NEXT: [[SUB:%.*]] = select i1 [[A:%.*]], i32 [[TMP1]], i32 0
870 ; CHECK-NEXT: ret i32 [[SUB]]
872 %sel0 = select i1 %A, i32 %D, i32 %B
873 %sel1 = select i1 %A, i32 %C, i32 %B
874 %sub = sub i32 %sel0, %sel1
878 define i32 @test48(i1 %A, i32 %B, i32 %C, i32 %D) {
879 ; CHECK-LABEL: @test48(
880 ; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[D:%.*]], [[C:%.*]]
881 ; CHECK-NEXT: [[SUB:%.*]] = select i1 [[A:%.*]], i32 0, i32 [[TMP1]]
882 ; CHECK-NEXT: ret i32 [[SUB]]
884 %sel0 = select i1 %A, i32 %B, i32 %D
885 %sel1 = select i1 %A, i32 %B, i32 %C
886 %sub = sub i32 %sel0, %sel1
890 define i32 @test49(i32 %X) {
891 ; CHECK-LABEL: @test49(
892 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 1, [[X:%.*]]
893 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[SUB]], 64
894 ; CHECK-NEXT: ret i32 [[RES]]
896 %sub = sub i32 129, %X
897 %res = and i32 %sub, 64
901 define i32 @test50(i32 %X) {
902 ; CHECK-LABEL: @test50(
903 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 1, [[X:%.*]]
904 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[SUB]], 127
905 ; CHECK-NEXT: ret i32 [[RES]]
907 %sub = sub i32 129, %X
908 %res = and i32 %sub, 127
912 define i32 @test51(i32 %X) {
913 ; CHECK-LABEL: @test51(
914 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 126, [[X:%.*]]
915 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[SUB]], 64
916 ; CHECK-NEXT: ret i32 [[RES]]
918 %sub = sub i32 254, %X
919 %res = and i32 %sub, 64
923 define i32 @test52(i32 %X) {
924 ; CHECK-LABEL: @test52(
925 ; CHECK-NEXT: [[SUB:%.*]] = sub i32 126, [[X:%.*]]
926 ; CHECK-NEXT: [[RES:%.*]] = and i32 [[SUB]], 127
927 ; CHECK-NEXT: ret i32 [[RES]]
929 %sub = sub i32 254, %X
930 %res = and i32 %sub, 127
934 define <2 x i1> @test53(<2 x i1> %A, <2 x i1> %B) {
935 ; CHECK-LABEL: @test53(
936 ; CHECK-NEXT: [[SUB:%.*]] = xor <2 x i1> [[A:%.*]], [[B:%.*]]
937 ; CHECK-NEXT: ret <2 x i1> [[SUB]]
939 %sub = sub <2 x i1> %A, %B
943 define i32 @test54(i1 %C) {
944 ; CHECK-LABEL: @test54(
945 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], i32 -877, i32 113
946 ; CHECK-NEXT: ret i32 [[V]]
948 %A = select i1 %C, i32 1000, i32 10
953 define <2 x i32> @test54vec(i1 %C) {
954 ; CHECK-LABEL: @test54vec(
955 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 -877, i32 -877>, <2 x i32> <i32 113, i32 113>
956 ; CHECK-NEXT: ret <2 x i32> [[V]]
958 %A = select i1 %C, <2 x i32> <i32 1000, i32 1000>, <2 x i32> <i32 10, i32 10>
959 %V = sub <2 x i32> <i32 123, i32 123>, %A
963 define <2 x i32> @test54vec2(i1 %C) {
964 ; CHECK-LABEL: @test54vec2(
965 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 -877, i32 -2167>, <2 x i32> <i32 113, i32 303>
966 ; CHECK-NEXT: ret <2 x i32> [[V]]
968 %A = select i1 %C, <2 x i32> <i32 1000, i32 2500>, <2 x i32> <i32 10, i32 30>
969 %V = sub <2 x i32> <i32 123, i32 333>, %A
973 define i32 @test55(i1 %which) {
974 ; CHECK-LABEL: @test55(
976 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
978 ; CHECK-NEXT: br label [[FINAL]]
980 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ -877, [[ENTRY:%.*]] ], [ 113, [[DELAY]] ]
981 ; CHECK-NEXT: ret i32 [[A]]
984 br i1 %which, label %final, label %delay
990 %A = phi i32 [ 1000, %entry ], [ 10, %delay ]
991 %value = sub i32 123, %A
995 define <2 x i32> @test55vec(i1 %which) {
996 ; CHECK-LABEL: @test55vec(
998 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
1000 ; CHECK-NEXT: br label [[FINAL]]
1002 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ <i32 -877, i32 -877>, [[ENTRY:%.*]] ], [ <i32 113, i32 113>, [[DELAY]] ]
1003 ; CHECK-NEXT: ret <2 x i32> [[A]]
1006 br i1 %which, label %final, label %delay
1012 %A = phi <2 x i32> [ <i32 1000, i32 1000>, %entry ], [ <i32 10, i32 10>, %delay ]
1013 %value = sub <2 x i32> <i32 123, i32 123>, %A
1014 ret <2 x i32> %value
1017 define <2 x i32> @test55vec2(i1 %which) {
1018 ; CHECK-LABEL: @test55vec2(
1019 ; CHECK-NEXT: entry:
1020 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
1022 ; CHECK-NEXT: br label [[FINAL]]
1024 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ <i32 -877, i32 -2167>, [[ENTRY:%.*]] ], [ <i32 113, i32 303>, [[DELAY]] ]
1025 ; CHECK-NEXT: ret <2 x i32> [[A]]
1028 br i1 %which, label %final, label %delay
1034 %A = phi <2 x i32> [ <i32 1000, i32 2500>, %entry ], [ <i32 10, i32 30>, %delay ]
1035 %value = sub <2 x i32> <i32 123, i32 333>, %A
1036 ret <2 x i32> %value
1039 define i32 @test56(i32 %A, i32 %B) {
1040 ; CHECK-LABEL: @test56(
1041 ; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]]
1042 ; CHECK-NEXT: ret i32 [[Y]]
1049 define i32 @test57(i32 %A, i32 %B) {
1050 ; CHECK-LABEL: @test57(
1051 ; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]]
1052 ; CHECK-NEXT: ret i32 [[Y]]
1059 @dummy_global1 = external global i8*
1060 @dummy_global2 = external global i8*
1062 define i64 @test58([100 x [100 x i8]]* %foo, i64 %i, i64 %j) {
1063 ; Note the reassociate pass and another instcombine pass will further optimize this to
1064 ; "%sub = i64 %i, %j, ret i64 %sub"
1065 ; gep1 and gep2 have only one use
1066 ; CHECK-LABEL: @test58(
1067 ; CHECK-NEXT: [[GEP2_OFFS:%.*]] = add i64 [[J:%.*]], 4200
1068 ; CHECK-NEXT: [[GEP1_OFFS:%.*]] = add i64 [[I:%.*]], 4200
1069 ; CHECK-NEXT: [[TMP1:%.*]] = sub i64 [[GEP1_OFFS]], [[GEP2_OFFS]]
1070 ; CHECK-NEXT: ret i64 [[TMP1]]
1072 %gep1 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 %i
1073 %gep2 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 %j
1074 %cast1 = ptrtoint i8* %gep1 to i64
1075 %cast2 = ptrtoint i8* %gep2 to i64
1076 %sub = sub i64 %cast1, %cast2
1080 define i64 @test59([100 x [100 x i8]]* %foo, i64 %i) {
1081 ; CHECK-LABEL: @test59(
1082 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO:%.*]], i64 0, i64 42, i64 [[I:%.*]]
1083 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO]], i64 0, i64 42, i64 0
1084 ; CHECK-NEXT: store i8* [[GEP1]], i8** @dummy_global1, align 8
1085 ; CHECK-NEXT: store i8* [[GEP2]], i8** @dummy_global2, align 8
1086 ; CHECK-NEXT: ret i64 [[I]]
1088 ; gep1 and gep2 have more than one uses
1089 %gep1 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 %i
1090 %gep2 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 0
1091 %cast1 = ptrtoint i8* %gep1 to i64
1092 %cast2 = ptrtoint i8* %gep2 to i64
1093 %sub = sub i64 %cast1, %cast2
1094 store i8* %gep1, i8** @dummy_global1
1095 store i8* %gep2, i8** @dummy_global2
1099 define i64 @test60([100 x [100 x i8]]* %foo, i64 %i, i64 %j) {
1100 ; CHECK-LABEL: @test60(
1101 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO:%.*]], i64 0, i64 [[J:%.*]], i64 [[I:%.*]]
1102 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO]], i64 0, i64 42, i64 0
1103 ; CHECK-NEXT: [[CAST1:%.*]] = ptrtoint i8* [[GEP1]] to i64
1104 ; CHECK-NEXT: [[CAST2:%.*]] = ptrtoint i8* [[GEP2]] to i64
1105 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[CAST1]], [[CAST2]]
1106 ; CHECK-NEXT: store i8* [[GEP1]], i8** @dummy_global1, align 8
1107 ; CHECK-NEXT: ret i64 [[SUB]]
1109 ; gep1 has a non-constant index and more than one uses. Shouldn't duplicate the arithmetic.
1110 %gep1 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 %j, i64 %i
1111 %gep2 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 0
1112 %cast1 = ptrtoint i8* %gep1 to i64
1113 %cast2 = ptrtoint i8* %gep2 to i64
1114 %sub = sub i64 %cast1, %cast2
1115 store i8* %gep1, i8** @dummy_global1
1119 define i64 @test61([100 x [100 x i8]]* %foo, i64 %i, i64 %j) {
1120 ; CHECK-LABEL: @test61(
1121 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO:%.*]], i64 0, i64 42, i64 0
1122 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* [[FOO]], i64 0, i64 [[J:%.*]], i64 [[I:%.*]]
1123 ; CHECK-NEXT: [[CAST1:%.*]] = ptrtoint i8* [[GEP1]] to i64
1124 ; CHECK-NEXT: [[CAST2:%.*]] = ptrtoint i8* [[GEP2]] to i64
1125 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[CAST1]], [[CAST2]]
1126 ; CHECK-NEXT: store i8* [[GEP2]], i8** @dummy_global2, align 8
1127 ; CHECK-NEXT: ret i64 [[SUB]]
1129 ; gep2 has a non-constant index and more than one uses. Shouldn't duplicate the arithmetic.
1130 %gep1 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 42, i64 0
1131 %gep2 = getelementptr inbounds [100 x [100 x i8]], [100 x [100 x i8]]* %foo, i64 0, i64 %j, i64 %i
1132 %cast1 = ptrtoint i8* %gep1 to i64
1133 %cast2 = ptrtoint i8* %gep2 to i64
1134 %sub = sub i64 %cast1, %cast2
1135 store i8* %gep2, i8** @dummy_global2
1139 define i32 @test62(i32 %A) {
1140 ; CHECK-LABEL: @test62(
1141 ; CHECK-NEXT: [[B:%.*]] = shl i32 [[A:%.*]], 1
1142 ; CHECK-NEXT: [[C:%.*]] = sub i32 2, [[B]]
1143 ; CHECK-NEXT: ret i32 [[C]]
1150 define <2 x i32> @test62vec(<2 x i32> %A) {
1151 ; CHECK-LABEL: @test62vec(
1152 ; CHECK-NEXT: [[B:%.*]] = shl <2 x i32> [[A:%.*]], <i32 1, i32 1>
1153 ; CHECK-NEXT: [[C:%.*]] = sub <2 x i32> <i32 2, i32 2>, [[B]]
1154 ; CHECK-NEXT: ret <2 x i32> [[C]]
1156 %B = sub <2 x i32> <i32 1, i32 1>, %A
1157 %C = shl <2 x i32> %B, <i32 1, i32 1>
1161 define i32 @test63(i32 %A) {
1162 ; CHECK-LABEL: @test63(
1163 ; CHECK-NEXT: [[B:%.*]] = shl i32 [[A:%.*]], 1
1164 ; CHECK-NEXT: ret i32 [[B]]
1172 define <2 x i32> @test63vec(<2 x i32> %A) {
1173 ; CHECK-LABEL: @test63vec(
1174 ; CHECK-NEXT: [[B:%.*]] = shl <2 x i32> [[A:%.*]], <i32 1, i32 1>
1175 ; CHECK-NEXT: ret <2 x i32> [[B]]
1177 %B = sub <2 x i32> <i32 1, i32 1>, %A
1178 %C = shl <2 x i32> %B, <i32 1, i32 1>
1179 %D = sub <2 x i32> <i32 2, i32 2>, %C
1183 ; FIXME: Transform (neg (max ~X, C)) -> ((min X, ~C) + 1). Same for min.
1184 define i32 @test64(i32 %x) {
1185 ; CHECK-LABEL: @test64(
1186 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 255
1187 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 255
1188 ; CHECK-NEXT: [[RES:%.*]] = add nsw i32 [[TMP2]], 1
1189 ; CHECK-NEXT: ret i32 [[RES]]
1192 %2 = icmp sgt i32 %1, -256
1193 %3 = select i1 %2, i32 %1, i32 -256
1194 %res = sub i32 0, %3
1198 define i32 @test65(i32 %x) {
1199 ; CHECK-LABEL: @test65(
1200 ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -256
1201 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -256
1202 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[TMP2]], 1
1203 ; CHECK-NEXT: ret i32 [[RES]]
1206 %2 = icmp slt i32 %1, 255
1207 %3 = select i1 %2, i32 %1, i32 255
1208 %res = sub i32 0, %3
1212 define i32 @test66(i32 %x) {
1213 ; CHECK-LABEL: @test66(
1214 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], -101
1215 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -101
1216 ; CHECK-NEXT: [[RES:%.*]] = add nuw i32 [[TMP2]], 1
1217 ; CHECK-NEXT: ret i32 [[RES]]
1220 %2 = icmp ugt i32 %1, 100
1221 %3 = select i1 %2, i32 %1, i32 100
1222 %res = sub i32 0, %3
1226 define i32 @test67(i32 %x) {
1227 ; CHECK-LABEL: @test67(
1228 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], 100
1229 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 100
1230 ; CHECK-NEXT: [[RES:%.*]] = add i32 [[TMP2]], 1
1231 ; CHECK-NEXT: ret i32 [[RES]]
1234 %2 = icmp ult i32 %1, -101
1235 %3 = select i1 %2, i32 %1, i32 -101
1236 %res = sub i32 0, %3
1240 ; Check splat vectors too
1241 define <2 x i32> @test68(<2 x i32> %x) {
1242 ; CHECK-LABEL: @test68(
1243 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 255, i32 255>
1244 ; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 255, i32 255>
1245 ; CHECK-NEXT: [[RES:%.*]] = add nsw <2 x i32> [[TMP2]], <i32 1, i32 1>
1246 ; CHECK-NEXT: ret <2 x i32> [[RES]]
1248 %1 = xor <2 x i32> %x, <i32 -1, i32 -1>
1249 %2 = icmp sgt <2 x i32> %1, <i32 -256, i32 -256>
1250 %3 = select <2 x i1> %2, <2 x i32> %1, <2 x i32> <i32 -256, i32 -256>
1251 %res = sub <2 x i32> zeroinitializer, %3
1255 ; And non-splat constant vectors.
1256 define <2 x i32> @test69(<2 x i32> %x) {
1257 ; CHECK-LABEL: @test69(
1258 ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], <i32 255, i32 127>
1259 ; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> <i32 255, i32 127>
1260 ; CHECK-NEXT: [[RES:%.*]] = add <2 x i32> [[TMP2]], <i32 1, i32 1>
1261 ; CHECK-NEXT: ret <2 x i32> [[RES]]
1263 %1 = xor <2 x i32> %x, <i32 -1, i32 -1>
1264 %2 = icmp sgt <2 x i32> %1, <i32 -256, i32 -128>
1265 %3 = select <2 x i1> %2, <2 x i32> %1, <2 x i32> <i32 -256, i32 -128>
1266 %res = sub <2 x i32> zeroinitializer, %3
1270 ; Check (X | Y) - Y --> X & ~Y when Y is a constant
1271 define i32 @test70(i32 %A) {
1272 ; CHECK-LABEL: @test70(
1273 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], -124
1274 ; CHECK-NEXT: ret i32 [[TMP1]]
1277 %C = sub i32 %B, 123
1281 ; Check (X | Y) - Y --> (X | Y) ^ Y doesn't happen where (X | Y) has multiple uses
1282 define i32 @test71(i32 %A, i32 %B) {
1283 ; CHECK-LABEL: @test71(
1284 ; CHECK-NEXT: [[C:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
1285 ; CHECK-NEXT: [[D:%.*]] = sub i32 [[C]], [[B]]
1286 ; CHECK-NEXT: [[E:%.*]] = mul i32 [[C]], [[D]]
1287 ; CHECK-NEXT: ret i32 [[E]]
1295 ; Check (X | Y) - Y --> X & ~Y where X and Y are vectors
1296 define <2 x i32> @test72(<2 x i32> %A, <2 x i32> %B) {
1297 ; CHECK-LABEL: @test72(
1298 ; CHECK-NEXT: [[B_NOT:%.*]] = xor <2 x i32> [[B:%.*]], <i32 -1, i32 -1>
1299 ; CHECK-NEXT: [[D:%.*]] = and <2 x i32> [[B_NOT]], [[A:%.*]]
1300 ; CHECK-NEXT: ret <2 x i32> [[D]]
1302 %C = or <2 x i32> %A, %B
1303 %D = sub <2 x i32> %C, %B
1307 ; Check reversing sub operands won't trigger (X | Y) - Y --> X & ~Y
1308 define i32 @test73(i32 %A, i32 %B) {
1309 ; CHECK-LABEL: @test73(
1310 ; CHECK-NEXT: [[C:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
1311 ; CHECK-NEXT: [[D:%.*]] = sub i32 [[B]], [[C]]
1312 ; CHECK-NEXT: ret i32 [[D]]
1319 define i32 @nsw_inference1(i32 %x, i32 %y) {
1320 ; CHECK-LABEL: @nsw_inference1(
1321 ; CHECK-NEXT: [[X2:%.*]] = or i32 [[X:%.*]], 1024
1322 ; CHECK-NEXT: [[Y2:%.*]] = and i32 [[Y:%.*]], 1
1323 ; CHECK-NEXT: [[Z:%.*]] = sub nuw nsw i32 [[X2]], [[Y2]]
1324 ; CHECK-NEXT: ret i32 [[Z]]
1326 %x2 = or i32 %x, 1024
1328 %z = sub i32 %x2, %y2
1332 define i32 @nsw_inference2(i32 %x, i32 %y) {
1333 ; CHECK-LABEL: @nsw_inference2(
1334 ; CHECK-NEXT: [[X2:%.*]] = and i32 [[X:%.*]], -1025
1335 ; CHECK-NEXT: [[Y2:%.*]] = or i32 [[Y:%.*]], -2
1336 ; CHECK-NEXT: [[Z:%.*]] = sub nsw i32 [[X2]], [[Y2]]
1337 ; CHECK-NEXT: ret i32 [[Z]]
1339 %x2 = and i32 %x, -1025
1341 %z = sub i32 %x2, %y2