1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 define i32 @select_0_or_1_from_bool(i1 %x) {
5 ; CHECK-LABEL: @select_0_or_1_from_bool(
6 ; CHECK-NEXT: [[NOT_X:%.*]] = xor i1 [[X:%.*]], true
7 ; CHECK-NEXT: [[ADD:%.*]] = zext i1 [[NOT_X]] to i32
8 ; CHECK-NEXT: ret i32 [[ADD]]
10 %ext = sext i1 %x to i32
11 %add = add i32 %ext, 1
15 define <2 x i32> @select_0_or_1_from_bool_vec(<2 x i1> %x) {
16 ; CHECK-LABEL: @select_0_or_1_from_bool_vec(
17 ; CHECK-NEXT: [[NOT_X:%.*]] = xor <2 x i1> [[X:%.*]], <i1 true, i1 true>
18 ; CHECK-NEXT: [[ADD:%.*]] = zext <2 x i1> [[NOT_X]] to <2 x i32>
19 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
21 %ext = sext <2 x i1> %x to <2 x i32>
22 %add = add <2 x i32> %ext, <i32 1, i32 1>
26 define i32 @select_C_minus_1_or_C_from_bool(i1 %x) {
27 ; CHECK-LABEL: @select_C_minus_1_or_C_from_bool(
28 ; CHECK-NEXT: [[ADD:%.*]] = select i1 [[X:%.*]], i32 41, i32 42
29 ; CHECK-NEXT: ret i32 [[ADD]]
31 %ext = sext i1 %x to i32
32 %add = add i32 %ext, 42
36 define <2 x i32> @select_C_minus_1_or_C_from_bool_vec(<2 x i1> %x) {
37 ; CHECK-LABEL: @select_C_minus_1_or_C_from_bool_vec(
38 ; CHECK-NEXT: [[ADD:%.*]] = select <2 x i1> [[X:%.*]], <2 x i32> <i32 41, i32 42>, <2 x i32> <i32 42, i32 43>
39 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
41 %ext = sext <2 x i1> %x to <2 x i32>
42 %add = add <2 x i32> %ext, <i32 42, i32 43>
46 ; This is an 'andn' of the low bit.
48 define i32 @flip_and_mask(i32 %x) {
49 ; CHECK-LABEL: @flip_and_mask(
50 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], 1
51 ; CHECK-NEXT: [[INC:%.*]] = xor i32 [[TMP1]], 1
52 ; CHECK-NEXT: ret i32 [[INC]]
55 %shr = ashr i32 %shl, 31
56 %inc = add i32 %shr, 1
60 define <2 x i8> @flip_and_mask_splat(<2 x i8> %x) {
61 ; CHECK-LABEL: @flip_and_mask_splat(
62 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i8> [[X:%.*]], <i8 1, i8 1>
63 ; CHECK-NEXT: [[INC:%.*]] = xor <2 x i8> [[TMP1]], <i8 1, i8 1>
64 ; CHECK-NEXT: ret <2 x i8> [[INC]]
66 %shl = shl <2 x i8> %x, <i8 7, i8 7>
67 %shr = ashr <2 x i8> %shl, <i8 7, i8 7>
68 %inc = add <2 x i8> %shr, <i8 1, i8 1>
72 define i32 @test1(i32 %A) {
73 ; CHECK-LABEL: @test1(
74 ; CHECK-NEXT: ret i32 [[A:%.*]]
80 define i32 @test2(i32 %A) {
81 ; CHECK-LABEL: @test2(
82 ; CHECK-NEXT: ret i32 [[A:%.*]]
89 define i32 @test3(i32 %A) {
90 ; CHECK-LABEL: @test3(
91 ; CHECK-NEXT: ret i32 [[A:%.*]]
99 define i32 @test4(i32 %A, i32 %B) {
100 ; CHECK-LABEL: @test4(
101 ; CHECK-NEXT: [[D:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]
102 ; CHECK-NEXT: ret i32 [[D]]
110 define i32 @test5(i32 %A, i32 %B) {
111 ; CHECK-LABEL: @test5(
112 ; CHECK-NEXT: [[D:%.*]] = sub i32 [[B:%.*]], [[A:%.*]]
113 ; CHECK-NEXT: ret i32 [[D]]
120 define <2 x i8> @neg_op0_vec_undef_elt(<2 x i8> %a, <2 x i8> %b) {
121 ; CHECK-LABEL: @neg_op0_vec_undef_elt(
122 ; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[B:%.*]], [[A:%.*]]
123 ; CHECK-NEXT: ret <2 x i8> [[R]]
125 %nega = sub <2 x i8> <i8 0, i8 undef>, %a
126 %r = add <2 x i8> %nega, %b
130 define <2 x i8> @neg_neg_vec_undef_elt(<2 x i8> %a, <2 x i8> %b) {
131 ; CHECK-LABEL: @neg_neg_vec_undef_elt(
132 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[A:%.*]], [[B:%.*]]
133 ; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> zeroinitializer, [[TMP1]]
134 ; CHECK-NEXT: ret <2 x i8> [[R]]
136 %nega = sub <2 x i8> <i8 undef, i8 0>, %a
137 %negb = sub <2 x i8> <i8 undef, i8 0>, %b
138 %r = add <2 x i8> %nega, %negb
142 ; C = 7*A+A == 8*A == A << 3
143 define i32 @test6(i32 %A) {
144 ; CHECK-LABEL: @test6(
145 ; CHECK-NEXT: [[C:%.*]] = shl i32 [[A:%.*]], 3
146 ; CHECK-NEXT: ret i32 [[C]]
153 ; C = A+7*A == 8*A == A << 3
154 define i32 @test7(i32 %A) {
155 ; CHECK-LABEL: @test7(
156 ; CHECK-NEXT: [[C:%.*]] = shl i32 [[A:%.*]], 3
157 ; CHECK-NEXT: ret i32 [[C]]
164 ; (A & C1)+(B & C2) -> (A & C1)|(B & C2) iff C1&C2 == 0
165 define i32 @test8(i32 %A, i32 %B) {
166 ; CHECK-LABEL: @test8(
167 ; CHECK-NEXT: [[A1:%.*]] = and i32 [[A:%.*]], 7
168 ; CHECK-NEXT: [[B1:%.*]] = and i32 [[B:%.*]], 128
169 ; CHECK-NEXT: [[C:%.*]] = or i32 [[A1]], [[B1]]
170 ; CHECK-NEXT: ret i32 [[C]]
173 %B1 = and i32 %B, 128
174 %C = add i32 %A1, %B1
178 define i32 @test9(i32 %A) {
179 ; CHECK-LABEL: @test9(
180 ; CHECK-NEXT: [[C:%.*]] = shl i32 [[A:%.*]], 5
181 ; CHECK-NEXT: ret i32 [[C]]
189 define i1 @test10(i8 %a, i8 %b) {
190 ; CHECK-LABEL: @test10(
191 ; CHECK-NEXT: [[ADD:%.*]] = sub i8 0, [[B:%.*]]
192 ; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[ADD]], [[A:%.*]]
193 ; CHECK-NEXT: ret i1 [[C]]
196 %c = icmp ne i8 %add, 0
200 define <2 x i1> @test10vec(<2 x i8> %a, <2 x i8> %b) {
201 ; CHECK-LABEL: @test10vec(
202 ; CHECK-NEXT: [[C:%.*]] = sub <2 x i8> zeroinitializer, [[B:%.*]]
203 ; CHECK-NEXT: [[D:%.*]] = icmp ne <2 x i8> [[C]], [[A:%.*]]
204 ; CHECK-NEXT: ret <2 x i1> [[D]]
206 %c = add <2 x i8> %a, %b
207 %d = icmp ne <2 x i8> %c, zeroinitializer
211 define i1 @test11(i8 %A) {
212 ; CHECK-LABEL: @test11(
213 ; CHECK-NEXT: [[C:%.*]] = icmp ne i8 [[A:%.*]], 1
214 ; CHECK-NEXT: ret i1 [[C]]
217 %c = icmp ne i8 %B, 0
221 define <2 x i1> @test11vec(<2 x i8> %a) {
222 ; CHECK-LABEL: @test11vec(
223 ; CHECK-NEXT: [[C:%.*]] = icmp ne <2 x i8> [[A:%.*]], <i8 1, i8 1>
224 ; CHECK-NEXT: ret <2 x i1> [[C]]
226 %b = add <2 x i8> %a, <i8 -1, i8 -1>
227 %c = icmp ne <2 x i8> %b, zeroinitializer
231 define i8 @reassoc_shl1(i8 %x, i8 %y) {
232 ; CHECK-LABEL: @reassoc_shl1(
233 ; CHECK-NEXT: [[REASS_ADD:%.*]] = shl i8 [[X:%.*]], 1
234 ; CHECK-NEXT: [[R:%.*]] = add i8 [[REASS_ADD]], [[Y:%.*]]
235 ; CHECK-NEXT: ret i8 [[R]]
242 define <2 x i8> @reassoc_shl1_commute1(<2 x i8> %x, <2 x i8> %y) {
243 ; CHECK-LABEL: @reassoc_shl1_commute1(
244 ; CHECK-NEXT: [[REASS_ADD:%.*]] = shl <2 x i8> [[X:%.*]], <i8 1, i8 1>
245 ; CHECK-NEXT: [[R:%.*]] = add <2 x i8> [[REASS_ADD]], [[Y:%.*]]
246 ; CHECK-NEXT: ret <2 x i8> [[R]]
248 %a = add <2 x i8> %x, %y
249 %r = add <2 x i8> %a, %x
253 define i8 @reassoc_shl1_commute2(i8 %px, i8 %py) {
254 ; CHECK-LABEL: @reassoc_shl1_commute2(
255 ; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[PX:%.*]]
256 ; CHECK-NEXT: [[Y:%.*]] = sdiv i8 43, [[PY:%.*]]
257 ; CHECK-NEXT: [[REASS_ADD:%.*]] = shl i8 [[X]], 1
258 ; CHECK-NEXT: [[R:%.*]] = add i8 [[Y]], [[REASS_ADD]]
259 ; CHECK-NEXT: ret i8 [[R]]
261 %x = sdiv i8 42, %px ; thwart complexity-based canonicalization
262 %y = sdiv i8 43, %py ; thwart complexity-based canonicalization
268 define i8 @reassoc_shl1_commute3(i8 %px, i8 %py) {
269 ; CHECK-LABEL: @reassoc_shl1_commute3(
270 ; CHECK-NEXT: [[X:%.*]] = sdiv i8 42, [[PX:%.*]]
271 ; CHECK-NEXT: [[Y:%.*]] = sdiv i8 43, [[PY:%.*]]
272 ; CHECK-NEXT: [[REASS_ADD:%.*]] = shl i8 [[X]], 1
273 ; CHECK-NEXT: [[R:%.*]] = add i8 [[Y]], [[REASS_ADD]]
274 ; CHECK-NEXT: ret i8 [[R]]
276 %x = sdiv i8 42, %px ; thwart complexity-based canonicalization
277 %y = sdiv i8 43, %py ; thwart complexity-based canonicalization
283 declare void @use(i8)
285 define i8 @reassoc_shl1_extra_use(i8 %x, i8 %y) {
286 ; CHECK-LABEL: @reassoc_shl1_extra_use(
287 ; CHECK-NEXT: [[A:%.*]] = add i8 [[Y:%.*]], [[X:%.*]]
288 ; CHECK-NEXT: call void @use(i8 [[A]])
289 ; CHECK-NEXT: [[R:%.*]] = add i8 [[A]], [[X]]
290 ; CHECK-NEXT: ret i8 [[R]]
293 call void @use(i8 %a)
299 define i32 @test13(i32 %A, i32 %B, i32 %C) {
300 ; CHECK-LABEL: @test13(
301 ; CHECK-NEXT: [[D_OK:%.*]] = add i32 [[A:%.*]], [[B:%.*]]
302 ; CHECK-NEXT: [[E_OK:%.*]] = add i32 [[D_OK]], [[C:%.*]]
303 ; CHECK-NEXT: [[F:%.*]] = add i32 [[E_OK]], [[A]]
304 ; CHECK-NEXT: ret i32 [[F]]
306 %D_OK = add i32 %A, %B
307 %E_OK = add i32 %D_OK, %C
308 %F = add i32 %E_OK, %A
312 define i32 @test14(i32 %offset, i32 %difference) {
313 ; CHECK-LABEL: @test14(
314 ; CHECK-NEXT: [[TMP_2:%.*]] = and i32 [[DIFFERENCE:%.*]], 3
315 ; CHECK-NEXT: [[TMP_3_OK:%.*]] = add i32 [[TMP_2]], [[OFFSET:%.*]]
316 ; CHECK-NEXT: [[TMP_5_MASK:%.*]] = and i32 [[DIFFERENCE]], -4
317 ; CHECK-NEXT: [[TMP_8:%.*]] = add i32 [[TMP_3_OK]], [[TMP_5_MASK]]
318 ; CHECK-NEXT: ret i32 [[TMP_8]]
320 %tmp.2 = and i32 %difference, 3
321 %tmp.3_OK = add i32 %tmp.2, %offset
322 %tmp.5.mask = and i32 %difference, -4
323 ; == add %offset, %difference
324 %tmp.8 = add i32 %tmp.3_OK, %tmp.5.mask
329 define i8 @test15(i8 %A) {
330 ; CHECK-LABEL: @test15(
331 ; CHECK-NEXT: [[C:%.*]] = and i8 [[A:%.*]], 16
332 ; CHECK-NEXT: ret i8 [[C]]
339 define i32 @test17(i32 %A) {
340 ; CHECK-LABEL: @test17(
341 ; CHECK-NEXT: [[C:%.*]] = sub i32 0, [[A:%.*]]
342 ; CHECK-NEXT: ret i32 [[C]]
349 define i8 @test18(i8 %A) {
350 ; CHECK-LABEL: @test18(
351 ; CHECK-NEXT: [[C:%.*]] = sub i8 16, [[A:%.*]]
352 ; CHECK-NEXT: ret i8 [[C]]
359 define <2 x i64> @test18vec(<2 x i64> %A) {
360 ; CHECK-LABEL: @test18vec(
361 ; CHECK-NEXT: [[ADD:%.*]] = sub <2 x i64> <i64 1, i64 2>, [[A:%.*]]
362 ; CHECK-NEXT: ret <2 x i64> [[ADD]]
364 %xor = xor <2 x i64> %A, <i64 -1, i64 -1>
365 %add = add <2 x i64> %xor, <i64 2, i64 3>
369 define i32 @test19(i1 %C) {
370 ; CHECK-LABEL: @test19(
371 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], i32 1123, i32 133
372 ; CHECK-NEXT: ret i32 [[V]]
374 %A = select i1 %C, i32 1000, i32 10
379 define <2 x i32> @test19vec(i1 %C) {
380 ; CHECK-LABEL: @test19vec(
381 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 1123, i32 1123>, <2 x i32> <i32 133, i32 133>
382 ; CHECK-NEXT: ret <2 x i32> [[V]]
384 %A = select i1 %C, <2 x i32> <i32 1000, i32 1000>, <2 x i32> <i32 10, i32 10>
385 %V = add <2 x i32> %A, <i32 123, i32 123>
389 ; This is an InstSimplify fold, but test it here to make sure that
390 ; InstCombine does not prevent the fold.
391 ; With NSW, add of sign bit -> or of sign bit.
393 define i32 @test20(i32 %x) {
394 ; CHECK-LABEL: @test20(
395 ; CHECK-NEXT: ret i32 [[X:%.*]]
397 %y = xor i32 %x, -2147483648
398 %z = add nsw i32 %y, -2147483648
402 define i32 @xor_sign_bit(i32 %x) {
403 ; CHECK-LABEL: @xor_sign_bit(
404 ; CHECK-NEXT: [[ADD:%.*]] = add i32 [[X:%.*]], -2147483606
405 ; CHECK-NEXT: ret i32 [[ADD]]
407 %xor = xor i32 %x, 2147483648
408 %add = add i32 %xor, 42
412 define <2 x i32> @xor_sign_bit_vec_splat(<2 x i32> %x) {
413 ; CHECK-LABEL: @xor_sign_bit_vec_splat(
414 ; CHECK-NEXT: [[ADD:%.*]] = add <2 x i32> [[X:%.*]], <i32 -2147483606, i32 -2147483606>
415 ; CHECK-NEXT: ret <2 x i32> [[ADD]]
417 %xor = xor <2 x i32> %x, <i32 2147483648, i32 2147483648>
418 %add = add <2 x i32> %xor, <i32 42, i32 42>
422 ; No-wrap info allows converting the add to 'or'.
424 define i8 @add_nsw_signbit(i8 %x) {
425 ; CHECK-LABEL: @add_nsw_signbit(
426 ; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], -128
427 ; CHECK-NEXT: ret i8 [[Y]]
429 %y = add nsw i8 %x, -128
433 ; No-wrap info allows converting the add to 'or'.
435 define i8 @add_nuw_signbit(i8 %x) {
436 ; CHECK-LABEL: @add_nuw_signbit(
437 ; CHECK-NEXT: [[Y:%.*]] = or i8 [[X:%.*]], -128
438 ; CHECK-NEXT: ret i8 [[Y]]
440 %y = add nuw i8 %x, 128
444 define i32 @add_nsw_sext_add(i8 %x) {
445 ; CHECK-LABEL: @add_nsw_sext_add(
446 ; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X:%.*]] to i32
447 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[TMP1]], 398
448 ; CHECK-NEXT: ret i32 [[R]]
450 %add = add nsw i8 %x, 42
451 %ext = sext i8 %add to i32
452 %r = add i32 %ext, 356
456 ; Negative test - extra use of the sext means increase of instructions.
458 define i32 @add_nsw_sext_add_extra_use_1(i8 %x, i32* %p) {
459 ; CHECK-LABEL: @add_nsw_sext_add_extra_use_1(
460 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[X:%.*]], 42
461 ; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[ADD]] to i32
462 ; CHECK-NEXT: store i32 [[EXT]], i32* [[P:%.*]], align 4
463 ; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[EXT]], 356
464 ; CHECK-NEXT: ret i32 [[R]]
466 %add = add nsw i8 %x, 42
467 %ext = sext i8 %add to i32
468 store i32 %ext, i32* %p
469 %r = add i32 %ext, 356
473 define <2 x i32> @add_nsw_sext_add_vec_extra_use_2(<2 x i8> %x, <2 x i8>* %p) {
474 ; CHECK-LABEL: @add_nsw_sext_add_vec_extra_use_2(
475 ; CHECK-NEXT: [[ADD:%.*]] = add nsw <2 x i8> [[X:%.*]], <i8 42, i8 -5>
476 ; CHECK-NEXT: store <2 x i8> [[ADD]], <2 x i8>* [[P:%.*]], align 2
477 ; CHECK-NEXT: [[TMP1:%.*]] = sext <2 x i8> [[X]] to <2 x i32>
478 ; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[TMP1]], <i32 398, i32 7>
479 ; CHECK-NEXT: ret <2 x i32> [[R]]
481 %add = add nsw <2 x i8> %x, <i8 42, i8 -5>
482 store <2 x i8> %add, <2 x i8>* %p
483 %ext = sext <2 x i8> %add to <2 x i32>
484 %r = add <2 x i32> %ext, <i32 356, i32 12>
488 define <2 x i32> @add_nuw_zext_add_vec(<2 x i16> %x) {
489 ; CHECK-LABEL: @add_nuw_zext_add_vec(
490 ; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i32>
491 ; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[TMP1]], <i32 65850, i32 -7>
492 ; CHECK-NEXT: ret <2 x i32> [[R]]
494 %add = add nuw <2 x i16> %x, <i16 -42, i16 5>
495 %ext = zext <2 x i16> %add to <2 x i32>
496 %r = add <2 x i32> %ext, <i32 356, i32 -12>
500 ; Negative test - extra use of the zext means increase of instructions.
502 define i64 @add_nuw_zext_add_extra_use_1(i8 %x, i64* %p) {
503 ; CHECK-LABEL: @add_nuw_zext_add_extra_use_1(
504 ; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[X:%.*]], 42
505 ; CHECK-NEXT: [[EXT:%.*]] = zext i8 [[ADD]] to i64
506 ; CHECK-NEXT: store i64 [[EXT]], i64* [[P:%.*]], align 4
507 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw i64 [[EXT]], 356
508 ; CHECK-NEXT: ret i64 [[R]]
510 %add = add nuw i8 %x, 42
511 %ext = zext i8 %add to i64
512 store i64 %ext, i64* %p
513 %r = add i64 %ext, 356
517 define i64 @add_nuw_zext_add_extra_use_2(i8 %x, i8* %p) {
518 ; CHECK-LABEL: @add_nuw_zext_add_extra_use_2(
519 ; CHECK-NEXT: [[ADD:%.*]] = add nuw i8 [[X:%.*]], 42
520 ; CHECK-NEXT: store i8 [[ADD]], i8* [[P:%.*]], align 1
521 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[X]] to i64
522 ; CHECK-NEXT: [[R:%.*]] = add nuw nsw i64 [[TMP1]], -314
523 ; CHECK-NEXT: ret i64 [[R]]
525 %add = add nuw i8 %x, 42
526 store i8 %add, i8* %p
527 %ext = zext i8 %add to i64
528 %r = add i64 %ext, -356
532 define i1 @test21(i32 %x) {
533 ; CHECK-LABEL: @test21(
534 ; CHECK-NEXT: [[Y:%.*]] = icmp eq i32 [[X:%.*]], 119
535 ; CHECK-NEXT: ret i1 [[Y]]
538 %y = icmp eq i32 %t, 123
542 define <2 x i1> @test21vec(<2 x i32> %x) {
543 ; CHECK-LABEL: @test21vec(
544 ; CHECK-NEXT: [[Y:%.*]] = icmp eq <2 x i32> [[X:%.*]], <i32 119, i32 119>
545 ; CHECK-NEXT: ret <2 x i1> [[Y]]
547 %t = add <2 x i32> %x, <i32 4, i32 4>
548 %y = icmp eq <2 x i32> %t, <i32 123, i32 123>
552 define i32 @test22(i32 %V) {
553 ; CHECK-LABEL: @test22(
554 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[DEFAULT:%.*]] [
555 ; CHECK-NEXT: i32 10, label [[LAB1:%.*]]
556 ; CHECK-NEXT: i32 20, label [[LAB2:%.*]]
559 ; CHECK-NEXT: ret i32 123
561 ; CHECK-NEXT: ret i32 12312
563 ; CHECK-NEXT: ret i32 1231231
566 switch i32 %V2, label %Default [
571 Default: ; preds = %0
581 define i32 @test23(i1 %C, i32 %a) {
582 ; CHECK-LABEL: @test23(
584 ; CHECK-NEXT: br i1 [[C:%.*]], label [[ENDIF:%.*]], label [[ELSE:%.*]]
586 ; CHECK-NEXT: br label [[ENDIF]]
588 ; CHECK-NEXT: [[B_0:%.*]] = phi i32 [ 1, [[ENTRY:%.*]] ], [ 2, [[ELSE]] ]
589 ; CHECK-NEXT: ret i32 [[B_0]]
592 br i1 %C, label %endif, label %else
594 else: ; preds = %entry
597 endif: ; preds = %else, %entry
598 %b.0 = phi i32 [ 0, %entry ], [ 1, %else ]
599 %tmp.4 = add i32 %b.0, 1
603 define i32 @test24(i32 %A) {
604 ; CHECK-LABEL: @test24(
605 ; CHECK-NEXT: [[B:%.*]] = shl i32 [[A:%.*]], 1
606 ; CHECK-NEXT: ret i32 [[B]]
614 define i64 @test25(i64 %Y) {
615 ; CHECK-LABEL: @test25(
616 ; CHECK-NEXT: [[TMP_8:%.*]] = shl i64 [[Y:%.*]], 3
617 ; CHECK-NEXT: ret i64 [[TMP_8]]
619 %tmp.4 = shl i64 %Y, 2
620 %tmp.12 = shl i64 %Y, 2
621 %tmp.8 = add i64 %tmp.4, %tmp.12
625 define i32 @test26(i32 %A, i32 %B) {
626 ; CHECK-LABEL: @test26(
627 ; CHECK-NEXT: ret i32 [[A:%.*]]
634 ; Fold add through select.
635 define i32 @test27(i1 %C, i32 %X, i32 %Y) {
636 ; CHECK-LABEL: @test27(
637 ; CHECK-NEXT: [[C_UPGRD_1_V:%.*]] = select i1 [[C:%.*]], i32 [[X:%.*]], i32 123
638 ; CHECK-NEXT: ret i32 [[C_UPGRD_1_V]]
642 %C.upgrd.1 = select i1 %C, i32 %A, i32 %B
643 %D = sub i32 %C.upgrd.1, %Y
647 define i32 @test28(i32 %X) {
648 ; CHECK-LABEL: @test28(
649 ; CHECK-NEXT: [[Z:%.*]] = sub i32 -1192, [[X:%.*]]
650 ; CHECK-NEXT: ret i32 [[Z]]
652 %Y = add i32 %X, 1234
657 define i32 @test29(i32 %x, i32 %y) {
658 ; CHECK-LABEL: @test29(
659 ; CHECK-NEXT: [[TMP_2:%.*]] = sub i32 [[X:%.*]], [[Y:%.*]]
660 ; CHECK-NEXT: [[TMP_7:%.*]] = and i32 [[X]], 63
661 ; CHECK-NEXT: [[TMP_9:%.*]] = and i32 [[TMP_2]], -64
662 ; CHECK-NEXT: [[TMP_10:%.*]] = or i32 [[TMP_7]], [[TMP_9]]
663 ; CHECK-NEXT: ret i32 [[TMP_10]]
665 %tmp.2 = sub i32 %x, %y
666 %tmp.2.mask = and i32 %tmp.2, 63
667 %tmp.6 = add i32 %tmp.2.mask, %y
668 %tmp.7 = and i32 %tmp.6, 63
669 %tmp.9 = and i32 %tmp.2, -64
670 %tmp.10 = or i32 %tmp.7, %tmp.9
674 ; Add of sign bit -> xor of sign bit.
675 define i64 @test30(i64 %x) {
676 ; CHECK-LABEL: @test30(
677 ; CHECK-NEXT: ret i64 [[X:%.*]]
679 %tmp.2 = xor i64 %x, -9223372036854775808
680 %tmp.4 = add i64 %tmp.2, -9223372036854775808
684 define i32 @test31(i32 %A) {
685 ; CHECK-LABEL: @test31(
686 ; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[A:%.*]], 5
687 ; CHECK-NEXT: ret i32 [[TMP1]]
695 define i32 @test32(i32 %A) {
696 ; CHECK-LABEL: @test32(
697 ; CHECK-NEXT: [[B:%.*]] = shl i32 [[A:%.*]], 2
698 ; CHECK-NEXT: ret i32 [[B]]
706 define i8 @test33(i8 %A) {
707 ; CHECK-LABEL: @test33(
708 ; CHECK-NEXT: [[C:%.*]] = or i8 [[A:%.*]], 1
709 ; CHECK-NEXT: ret i8 [[C]]
716 define i8 @test34(i8 %A) {
717 ; CHECK-LABEL: @test34(
718 ; CHECK-NEXT: [[C:%.*]] = and i8 [[A:%.*]], 12
719 ; CHECK-NEXT: ret i8 [[C]]
726 ; If all bits affected by the add are included
727 ; in the mask, do the add before the mask op.
729 define i8 @masked_add(i8 %x) {
730 ; CHECK-LABEL: @masked_add(
731 ; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X:%.*]], 96
732 ; CHECK-NEXT: [[R:%.*]] = and i8 [[TMP1]], -16
733 ; CHECK-NEXT: ret i8 [[R]]
735 %and = and i8 %x, 240 ; 0xf0
736 %r = add i8 %and, 96 ; 0x60
740 define <2 x i8> @masked_add_splat(<2 x i8> %x) {
741 ; CHECK-LABEL: @masked_add_splat(
742 ; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], <i8 64, i8 64>
743 ; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[TMP1]], <i8 -64, i8 -64>
744 ; CHECK-NEXT: ret <2 x i8> [[R]]
746 %and = and <2 x i8> %x, <i8 192, i8 192> ; 0xc0
747 %r = add <2 x i8> %and, <i8 64, i8 64> ; 0x40
751 define i8 @not_masked_add(i8 %x) {
752 ; CHECK-LABEL: @not_masked_add(
753 ; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], 112
754 ; CHECK-NEXT: [[R:%.*]] = add nuw i8 [[AND]], 96
755 ; CHECK-NEXT: ret i8 [[R]]
757 %and = and i8 %x, 112 ; 0x70
758 %r = add i8 %and, 96 ; 0x60
762 define i32 @test35(i32 %a) {
763 ; CHECK-LABEL: @test35(
764 ; CHECK-NEXT: ret i32 -1
766 %tmpnot = xor i32 %a, -1
767 %tmp2 = add i32 %tmpnot, %a
771 define i32 @test36(i32 %a) {
772 ; CHECK-LABEL: @test36(
773 ; CHECK-NEXT: ret i32 0
776 %y = and i32 %a, -126
778 %q = and i32 %z, 1 ; always zero
782 define i1 @test37(i32 %a, i32 %b) {
783 ; CHECK-LABEL: @test37(
784 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
785 ; CHECK-NEXT: ret i1 [[CMP]]
787 %add = add i32 %a, %b
788 %cmp = icmp eq i32 %add, %a
792 define i1 @test38(i32 %a, i32 %b) {
793 ; CHECK-LABEL: @test38(
794 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
795 ; CHECK-NEXT: ret i1 [[CMP]]
797 %add = add i32 %a, %b
798 %cmp = icmp eq i32 %add, %b
802 define i1 @test39(i32 %a, i32 %b) {
803 ; CHECK-LABEL: @test39(
804 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[B:%.*]], 0
805 ; CHECK-NEXT: ret i1 [[CMP]]
807 %add = add i32 %b, %a
808 %cmp = icmp eq i32 %add, %a
812 define i1 @test40(i32 %a, i32 %b) {
813 ; CHECK-LABEL: @test40(
814 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
815 ; CHECK-NEXT: ret i1 [[CMP]]
817 %add = add i32 %b, %a
818 %cmp = icmp eq i32 %add, %b
822 ; (add (zext (add nuw X, C2)), C) --> (zext (add nuw X, C2 + C))
824 define i64 @test41(i32 %a) {
825 ; CHECK-LABEL: @test41(
826 ; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[A:%.*]], 15
827 ; CHECK-NEXT: [[SUB:%.*]] = zext i32 [[TMP1]] to i64
828 ; CHECK-NEXT: ret i64 [[SUB]]
830 %add = add nuw i32 %a, 16
831 %zext = zext i32 %add to i64
832 %sub = add i64 %zext, -1
836 ; (add (zext (add nuw X, C2)), C) --> (zext (add nuw X, C2 + C))
838 define <2 x i64> @test41vec(<2 x i32> %a) {
839 ; CHECK-LABEL: @test41vec(
840 ; CHECK-NEXT: [[TMP1:%.*]] = add nuw <2 x i32> [[A:%.*]], <i32 15, i32 15>
841 ; CHECK-NEXT: [[SUB:%.*]] = zext <2 x i32> [[TMP1]] to <2 x i64>
842 ; CHECK-NEXT: ret <2 x i64> [[SUB]]
844 %add = add nuw <2 x i32> %a, <i32 16, i32 16>
845 %zext = zext <2 x i32> %add to <2 x i64>
846 %sub = add <2 x i64> %zext, <i64 -1, i64 -1>
850 define <2 x i64> @test41vec_and_multiuse(<2 x i32> %a) {
851 ; CHECK-LABEL: @test41vec_and_multiuse(
852 ; CHECK-NEXT: [[ADD:%.*]] = add nuw <2 x i32> [[A:%.*]], <i32 16, i32 16>
853 ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i32> [[ADD]] to <2 x i64>
854 ; CHECK-NEXT: [[REASS_ADD:%.*]] = shl nuw nsw <2 x i64> [[ZEXT]], <i64 1, i64 1>
855 ; CHECK-NEXT: [[EXTRAUSE:%.*]] = add nsw <2 x i64> [[REASS_ADD]], <i64 -1, i64 -1>
856 ; CHECK-NEXT: ret <2 x i64> [[EXTRAUSE]]
858 %add = add nuw <2 x i32> %a, <i32 16, i32 16>
859 %zext = zext <2 x i32> %add to <2 x i64>
860 %sub = add <2 x i64> %zext, <i64 -1, i64 -1>
861 %extrause = add <2 x i64> %zext, %sub
862 ret <2 x i64> %extrause
865 define i32 @test42(i1 %C) {
866 ; CHECK-LABEL: @test42(
867 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], i32 1123, i32 133
868 ; CHECK-NEXT: ret i32 [[V]]
870 %A = select i1 %C, i32 1000, i32 10
875 define <2 x i32> @test42vec(i1 %C) {
876 ; CHECK-LABEL: @test42vec(
877 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 1123, i32 1123>, <2 x i32> <i32 133, i32 133>
878 ; CHECK-NEXT: ret <2 x i32> [[V]]
880 %A = select i1 %C, <2 x i32> <i32 1000, i32 1000>, <2 x i32> <i32 10, i32 10>
881 %V = add <2 x i32> <i32 123, i32 123>, %A
885 define <2 x i32> @test42vec2(i1 %C) {
886 ; CHECK-LABEL: @test42vec2(
887 ; CHECK-NEXT: [[V:%.*]] = select i1 [[C:%.*]], <2 x i32> <i32 1123, i32 2833>, <2 x i32> <i32 133, i32 363>
888 ; CHECK-NEXT: ret <2 x i32> [[V]]
890 %A = select i1 %C, <2 x i32> <i32 1000, i32 2500>, <2 x i32> <i32 10, i32 30>
891 %V = add <2 x i32> <i32 123, i32 333>, %A
895 define i32 @test55(i1 %which) {
896 ; CHECK-LABEL: @test55(
898 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
900 ; CHECK-NEXT: br label [[FINAL]]
902 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ 1123, [[ENTRY:%.*]] ], [ 133, [[DELAY]] ]
903 ; CHECK-NEXT: ret i32 [[A]]
906 br i1 %which, label %final, label %delay
912 %A = phi i32 [ 1000, %entry ], [ 10, %delay ]
913 %value = add i32 123, %A
917 define <2 x i32> @test43vec(i1 %which) {
918 ; CHECK-LABEL: @test43vec(
920 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
922 ; CHECK-NEXT: br label [[FINAL]]
924 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ <i32 1123, i32 1123>, [[ENTRY:%.*]] ], [ <i32 133, i32 133>, [[DELAY]] ]
925 ; CHECK-NEXT: ret <2 x i32> [[A]]
928 br i1 %which, label %final, label %delay
934 %A = phi <2 x i32> [ <i32 1000, i32 1000>, %entry ], [ <i32 10, i32 10>, %delay ]
935 %value = add <2 x i32> <i32 123, i32 123>, %A
939 define <2 x i32> @test43vec2(i1 %which) {
940 ; CHECK-LABEL: @test43vec2(
942 ; CHECK-NEXT: br i1 [[WHICH:%.*]], label [[FINAL:%.*]], label [[DELAY:%.*]]
944 ; CHECK-NEXT: br label [[FINAL]]
946 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ <i32 1123, i32 2833>, [[ENTRY:%.*]] ], [ <i32 133, i32 363>, [[DELAY]] ]
947 ; CHECK-NEXT: ret <2 x i32> [[A]]
950 br i1 %which, label %final, label %delay
956 %A = phi <2 x i32> [ <i32 1000, i32 2500>, %entry ], [ <i32 10, i32 30>, %delay ]
957 %value = add <2 x i32> <i32 123, i32 333>, %A
961 ; E = (A + 1) + ~B = A - B
962 define i32 @add_not_increment(i32 %A, i32 %B) {
963 ; CHECK-LABEL: @add_not_increment(
964 ; CHECK-NEXT: [[E:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
965 ; CHECK-NEXT: ret i32 [[E]]
973 ; E = (A + 1) + ~B = A - B
974 define <2 x i32> @add_not_increment_vec(<2 x i32> %A, <2 x i32> %B) {
975 ; CHECK-LABEL: @add_not_increment_vec(
976 ; CHECK-NEXT: [[E:%.*]] = sub <2 x i32> [[A:%.*]], [[B:%.*]]
977 ; CHECK-NEXT: ret <2 x i32> [[E]]
979 %C = xor <2 x i32> %B, <i32 -1, i32 -1>
980 %D = add <2 x i32> %A, <i32 1, i32 1>
981 %E = add <2 x i32> %D, %C
985 ; E = ~B + (1 + A) = A - B
986 define i32 @add_not_increment_commuted(i32 %A, i32 %B) {
987 ; CHECK-LABEL: @add_not_increment_commuted(
988 ; CHECK-NEXT: [[E:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
989 ; CHECK-NEXT: ret i32 [[E]]
997 ; E = (A + ~B) + 1 = A - B
998 define i32 @add_to_sub(i32 %M, i32 %B) {
999 ; CHECK-LABEL: @add_to_sub(
1000 ; CHECK-NEXT: [[A:%.*]] = mul i32 [[M:%.*]], 42
1001 ; CHECK-NEXT: [[E:%.*]] = sub i32 [[A]], [[B:%.*]]
1002 ; CHECK-NEXT: ret i32 [[E]]
1004 %A = mul i32 %M, 42 ; thwart complexity-based ordering
1011 ; E = (~B + A) + 1 = A - B
1012 define i32 @add_to_sub2(i32 %A, i32 %M) {
1013 ; CHECK-LABEL: @add_to_sub2(
1014 ; CHECK-NEXT: [[B_NEG:%.*]] = mul i32 [[M:%.*]], -42
1015 ; CHECK-NEXT: [[E:%.*]] = add i32 [[B_NEG]], [[A:%.*]]
1016 ; CHECK-NEXT: ret i32 [[E]]
1018 %B = mul i32 %M, 42 ; thwart complexity-based ordering
1025 ; (X | C1) + C2 --> (X | C1) ^ C1 iff (C1 == -C2)
1026 define i32 @test44(i32 %A) {
1027 ; CHECK-LABEL: @test44(
1028 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A:%.*]], -124
1029 ; CHECK-NEXT: ret i32 [[TMP1]]
1032 %C = add i32 %B, -123
1036 define i32 @test44_extra_use(i32 %A) {
1037 ; CHECK-LABEL: @test44_extra_use(
1038 ; CHECK-NEXT: [[B:%.*]] = or i32 [[A:%.*]], 123
1039 ; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[A]], -124
1040 ; CHECK-NEXT: [[D:%.*]] = mul i32 [[B]], [[TMP1]]
1041 ; CHECK-NEXT: ret i32 [[D]]
1044 %C = add i32 %B, -123
1049 define i32 @test44_non_matching(i32 %A) {
1050 ; CHECK-LABEL: @test44_non_matching(
1051 ; CHECK-NEXT: [[B:%.*]] = or i32 [[A:%.*]], 123
1052 ; CHECK-NEXT: [[C:%.*]] = add i32 [[B]], -321
1053 ; CHECK-NEXT: ret i32 [[C]]
1056 %C = add i32 %B, -321
1060 define <2 x i32> @test44_vec(<2 x i32> %A) {
1061 ; CHECK-LABEL: @test44_vec(
1062 ; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i32> [[A:%.*]], <i32 -124, i32 -124>
1063 ; CHECK-NEXT: ret <2 x i32> [[TMP1]]
1065 %B = or <2 x i32> %A, <i32 123, i32 123>
1066 %C = add <2 x i32> %B, <i32 -123, i32 -123>
1070 define <2 x i32> @test44_vec_non_matching(<2 x i32> %A) {
1071 ; CHECK-LABEL: @test44_vec_non_matching(
1072 ; CHECK-NEXT: [[B:%.*]] = or <2 x i32> [[A:%.*]], <i32 123, i32 123>
1073 ; CHECK-NEXT: [[C:%.*]] = add <2 x i32> [[B]], <i32 -321, i32 -321>
1074 ; CHECK-NEXT: ret <2 x i32> [[C]]
1076 %B = or <2 x i32> %A, <i32 123, i32 123>
1077 %C = add <2 x i32> %B, <i32 -321, i32 -321>
1081 define <2 x i32> @test44_vec_undef(<2 x i32> %A) {
1082 ; CHECK-LABEL: @test44_vec_undef(
1083 ; CHECK-NEXT: [[B:%.*]] = or <2 x i32> [[A:%.*]], <i32 123, i32 undef>
1084 ; CHECK-NEXT: [[C:%.*]] = add <2 x i32> [[B]], <i32 -123, i32 undef>
1085 ; CHECK-NEXT: ret <2 x i32> [[C]]
1087 %B = or <2 x i32> %A, <i32 123, i32 undef>
1088 %C = add <2 x i32> %B, <i32 -123, i32 undef>
1092 define <2 x i32> @test44_vec_non_splat(<2 x i32> %A) {
1093 ; CHECK-LABEL: @test44_vec_non_splat(
1094 ; CHECK-NEXT: [[B:%.*]] = or <2 x i32> [[A:%.*]], <i32 123, i32 456>
1095 ; CHECK-NEXT: [[C:%.*]] = add <2 x i32> [[B]], <i32 -123, i32 -456>
1096 ; CHECK-NEXT: ret <2 x i32> [[C]]
1098 %B = or <2 x i32> %A, <i32 123, i32 456>
1099 %C = add <2 x i32> %B, <i32 -123, i32 -456>
1103 define i32 @lshr_add(i1 %x, i1 %y) {
1104 ; CHECK-LABEL: @lshr_add(
1105 ; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X:%.*]], true
1106 ; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[Y:%.*]]
1107 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP2]] to i32
1108 ; CHECK-NEXT: ret i32 [[R]]
1110 %xz = zext i1 %x to i32
1111 %ys = sext i1 %y to i32
1112 %sub = add i32 %xz, %ys
1113 %r = lshr i32 %sub, 31
1117 define i5 @and_add(i1 %x, i1 %y) {
1118 ; CHECK-LABEL: @and_add(
1119 ; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X:%.*]], true
1120 ; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[Y:%.*]]
1121 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i5 -2, i5 0
1122 ; CHECK-NEXT: ret i5 [[R]]
1124 %xz = zext i1 %x to i5
1125 %ys = sext i1 %y to i5
1126 %sub = add i5 %xz, %ys
1127 %r = and i5 %sub, 30
1131 define <2 x i8> @ashr_add_commute(<2 x i1> %x, <2 x i1> %y) {
1132 ; CHECK-LABEL: @ashr_add_commute(
1133 ; CHECK-NEXT: [[TMP1:%.*]] = xor <2 x i1> [[X:%.*]], <i1 true, i1 true>
1134 ; CHECK-NEXT: [[TMP2:%.*]] = and <2 x i1> [[TMP1]], [[Y:%.*]]
1135 ; CHECK-NEXT: [[TMP3:%.*]] = sext <2 x i1> [[TMP2]] to <2 x i8>
1136 ; CHECK-NEXT: ret <2 x i8> [[TMP3]]
1138 %xz = zext <2 x i1> %x to <2 x i8>
1139 %ys = sext <2 x i1> %y to <2 x i8>
1140 %sub = add nsw <2 x i8> %ys, %xz
1141 %r = ashr <2 x i8> %sub, <i8 1, i8 1>
1145 define i32 @cmp_math(i32 %x, i32 %y) {
1146 ; CHECK-LABEL: @cmp_math(
1147 ; CHECK-NEXT: [[LT:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
1148 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[LT]] to i32
1149 ; CHECK-NEXT: ret i32 [[R]]
1151 %gt = icmp ugt i32 %x, %y
1152 %lt = icmp ult i32 %x, %y
1153 %xz = zext i1 %gt to i32
1154 %yz = zext i1 %lt to i32
1155 %s = sub i32 %xz, %yz
1156 %r = lshr i32 %s, 31
1160 ; Negative test - wrong type
1162 define i32 @lshr_add_nonbool(i2 %x, i1 %y) {
1163 ; CHECK-LABEL: @lshr_add_nonbool(
1164 ; CHECK-NEXT: [[XZ:%.*]] = zext i2 [[X:%.*]] to i32
1165 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1166 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XZ]], [[YS]]
1167 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1168 ; CHECK-NEXT: ret i32 [[R]]
1170 %xz = zext i2 %x to i32
1171 %ys = sext i1 %y to i32
1172 %sub = add i32 %xz, %ys
1173 %r = lshr i32 %sub, 31
1177 ; Negative test - wrong demand
1179 define i32 @and31_add(i1 %x, i1 %y) {
1180 ; CHECK-LABEL: @and31_add(
1181 ; CHECK-NEXT: [[XZ:%.*]] = zext i1 [[X:%.*]] to i32
1182 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1183 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XZ]], [[YS]]
1184 ; CHECK-NEXT: [[R:%.*]] = and i32 [[SUB]], 31
1185 ; CHECK-NEXT: ret i32 [[R]]
1187 %xz = zext i1 %x to i32
1188 %ys = sext i1 %y to i32
1189 %sub = add i32 %xz, %ys
1190 %r = and i32 %sub, 31
1194 ; Negative test - extra use
1196 define i32 @lshr_add_use(i1 %x, i1 %y, i32* %p) {
1197 ; CHECK-LABEL: @lshr_add_use(
1198 ; CHECK-NEXT: [[XZ:%.*]] = zext i1 [[X:%.*]] to i32
1199 ; CHECK-NEXT: store i32 [[XZ]], i32* [[P:%.*]], align 4
1200 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1201 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XZ]], [[YS]]
1202 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1203 ; CHECK-NEXT: ret i32 [[R]]
1205 %xz = zext i1 %x to i32
1206 store i32 %xz, i32* %p
1207 %ys = sext i1 %y to i32
1208 %sub = add i32 %xz, %ys
1209 %r = lshr i32 %sub, 31
1213 ; Negative test - extra use
1215 define i32 @lshr_add_use2(i1 %x, i1 %y, i32* %p) {
1216 ; CHECK-LABEL: @lshr_add_use2(
1217 ; CHECK-NEXT: [[XZ:%.*]] = zext i1 [[X:%.*]] to i32
1218 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1219 ; CHECK-NEXT: store i32 [[YS]], i32* [[P:%.*]], align 4
1220 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XZ]], [[YS]]
1221 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1222 ; CHECK-NEXT: ret i32 [[R]]
1224 %xz = zext i1 %x to i32
1225 %ys = sext i1 %y to i32
1226 store i32 %ys, i32* %p
1227 %sub = add i32 %xz, %ys
1228 %r = lshr i32 %sub, 31
1232 define i32 @lshr_add_sexts(i1 %x, i1 %y) {
1233 ; CHECK-LABEL: @lshr_add_sexts(
1234 ; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
1235 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
1236 ; CHECK-NEXT: ret i32 [[R]]
1238 %xs = sext i1 %x to i32
1239 %ys = sext i1 %y to i32
1240 %sub = add i32 %xs, %ys
1241 %r = lshr i32 %sub, 31
1245 define i5 @and_add_sexts(i1 %x, i1 %y) {
1246 ; CHECK-LABEL: @and_add_sexts(
1247 ; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[X:%.*]], [[Y:%.*]]
1248 ; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP1]], i5 -2, i5 0
1249 ; CHECK-NEXT: ret i5 [[R]]
1251 %xs = sext i1 %x to i5
1252 %ys = sext i1 %y to i5
1253 %sub = add i5 %xs, %ys
1254 %r = and i5 %sub, 30
1258 define <2 x i8> @ashr_add_sexts(<2 x i1> %x, <2 x i1> %y) {
1259 ; CHECK-LABEL: @ashr_add_sexts(
1260 ; CHECK-NEXT: [[TMP1:%.*]] = or <2 x i1> [[Y:%.*]], [[X:%.*]]
1261 ; CHECK-NEXT: [[TMP2:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i8>
1262 ; CHECK-NEXT: ret <2 x i8> [[TMP2]]
1264 %xs = sext <2 x i1> %x to <2 x i8>
1265 %ys = sext <2 x i1> %y to <2 x i8>
1266 %sub = add nsw <2 x i8> %ys, %xs
1267 %r = ashr <2 x i8> %sub, <i8 1, i8 1>
1271 define i32 @cmp_math_sexts(i32 %x, i32 %y) {
1272 ; CHECK-LABEL: @cmp_math_sexts(
1273 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i32 [[X:%.*]], [[Y:%.*]]
1274 ; CHECK-NEXT: [[R:%.*]] = zext i1 [[TMP1]] to i32
1275 ; CHECK-NEXT: ret i32 [[R]]
1277 %gt = icmp ugt i32 %x, %y
1278 %lt = icmp ult i32 %x, %y
1279 %xz = sext i1 %gt to i32
1280 %yz = zext i1 %lt to i32
1281 %s = sub i32 %xz, %yz
1282 %r = lshr i32 %s, 31
1286 ; Negative test - wrong type
1288 define i32 @lshr_add_nonbool_sexts(i2 %x, i1 %y) {
1289 ; CHECK-LABEL: @lshr_add_nonbool_sexts(
1290 ; CHECK-NEXT: [[XS:%.*]] = sext i2 [[X:%.*]] to i32
1291 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1292 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1293 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1294 ; CHECK-NEXT: ret i32 [[R]]
1296 %xs = sext i2 %x to i32
1297 %ys = sext i1 %y to i32
1298 %sub = add i32 %xs, %ys
1299 %r = lshr i32 %sub, 31
1303 ; Negative test - wrong demand
1305 define i32 @and31_add_sexts(i1 %x, i1 %y) {
1306 ; CHECK-LABEL: @and31_add_sexts(
1307 ; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1308 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1309 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1310 ; CHECK-NEXT: [[R:%.*]] = and i32 [[SUB]], 31
1311 ; CHECK-NEXT: ret i32 [[R]]
1313 %xs = sext i1 %x to i32
1314 %ys = sext i1 %y to i32
1315 %sub = add i32 %xs, %ys
1316 %r = and i32 %sub, 31
1320 ; Negative test - extra use
1322 define i32 @lshr_add_use_sexts(i1 %x, i1 %y, i32* %p) {
1323 ; CHECK-LABEL: @lshr_add_use_sexts(
1324 ; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1325 ; CHECK-NEXT: store i32 [[XS]], i32* [[P:%.*]], align 4
1326 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1327 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1328 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1329 ; CHECK-NEXT: ret i32 [[R]]
1331 %xs = sext i1 %x to i32
1332 store i32 %xs, i32* %p
1333 %ys = sext i1 %y to i32
1334 %sub = add i32 %xs, %ys
1335 %r = lshr i32 %sub, 31
1339 ; Negative test - extra use
1341 define i32 @lshr_add_use2_sexts(i1 %x, i1 %y, i32* %p) {
1342 ; CHECK-LABEL: @lshr_add_use2_sexts(
1343 ; CHECK-NEXT: [[XS:%.*]] = sext i1 [[X:%.*]] to i32
1344 ; CHECK-NEXT: [[YS:%.*]] = sext i1 [[Y:%.*]] to i32
1345 ; CHECK-NEXT: store i32 [[YS]], i32* [[P:%.*]], align 4
1346 ; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[XS]], [[YS]]
1347 ; CHECK-NEXT: [[R:%.*]] = lshr i32 [[SUB]], 31
1348 ; CHECK-NEXT: ret i32 [[R]]
1350 %xs = sext i1 %x to i32
1351 %ys = sext i1 %y to i32
1352 store i32 %ys, i32* %p
1353 %sub = add i32 %xs, %ys
1354 %r = lshr i32 %sub, 31
1358 define i8 @add_like_or_t0(i8 %x) {
1359 ; CHECK-LABEL: @add_like_or_t0(
1360 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 4
1361 ; CHECK-NEXT: [[R:%.*]] = add i8 [[I0]], 57
1362 ; CHECK-NEXT: ret i8 [[R]]
1365 %i1 = or i8 %i0, 15 ; no common bits
1369 define i8 @add_like_or_n1(i8 %x) {
1370 ; CHECK-LABEL: @add_like_or_n1(
1371 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 4
1372 ; CHECK-NEXT: [[I1:%.*]] = or i8 [[I0]], 31
1373 ; CHECK-NEXT: [[R:%.*]] = add i8 [[I1]], 42
1374 ; CHECK-NEXT: ret i8 [[R]]
1377 %i1 = or i8 %i0, 31 ; 4'th bit might be common-set
1381 define i8 @add_like_or_t2_extrause(i8 %x) {
1382 ; CHECK-LABEL: @add_like_or_t2_extrause(
1383 ; CHECK-NEXT: [[I0:%.*]] = shl i8 [[X:%.*]], 4
1384 ; CHECK-NEXT: [[I1:%.*]] = or i8 [[I0]], 15
1385 ; CHECK-NEXT: call void @use(i8 [[I1]])
1386 ; CHECK-NEXT: [[R:%.*]] = add i8 [[I0]], 57
1387 ; CHECK-NEXT: ret i8 [[R]]
1390 %i1 = or i8 %i0, 15 ; no common bits
1391 call void @use(i8 %i1) ; extra use