1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
4 define i32 @test1(i32 %A) {
6 ; CHECK-NEXT: ret i32 [[A:%.*]]
12 define i32 @all_ones(i32 %A) {
13 ; CHECK-LABEL: @all_ones(
14 ; CHECK-NEXT: ret i32 -1
20 define <3 x i8> @all_ones_vec_with_poison_elt(<3 x i8> %A) {
21 ; CHECK-LABEL: @all_ones_vec_with_poison_elt(
22 ; CHECK-NEXT: ret <3 x i8> splat (i8 -1)
24 %B = or <3 x i8> %A, <i8 -1, i8 poison, i8 -1>
28 define i1 @test3(i1 %A) {
29 ; CHECK-LABEL: @test3(
30 ; CHECK-NEXT: ret i1 [[A:%.*]]
36 define i1 @test4(i1 %A) {
37 ; CHECK-LABEL: @test4(
38 ; CHECK-NEXT: ret i1 true
44 define i1 @test5(i1 %A) {
45 ; CHECK-LABEL: @test5(
46 ; CHECK-NEXT: ret i1 [[A:%.*]]
52 define i32 @test6(i32 %A) {
53 ; CHECK-LABEL: @test6(
54 ; CHECK-NEXT: ret i32 [[A:%.*]]
62 define i32 @or_not(i32 %A) {
63 ; CHECK-LABEL: @or_not(
64 ; CHECK-NEXT: ret i32 -1
66 %NotA = xor i32 %A, -1
71 define <2 x i4> @or_not_commute_vec_poison(<2 x i4> %A) {
72 ; CHECK-LABEL: @or_not_commute_vec_poison(
73 ; CHECK-NEXT: ret <2 x i4> splat (i4 -1)
75 %NotA = xor <2 x i4> %A, <i4 -1, i4 poison>
76 %B = or <2 x i4> %NotA, %A
80 define i8 @test8(i8 %A) {
81 ; CHECK-LABEL: @test8(
82 ; CHECK-NEXT: ret i8 -1
89 ; Test that (A|c1)|(B|c2) == (A|B)|(c1|c2)
90 define i8 @test9(i8 %A, i8 %B) {
91 ; CHECK-LABEL: @test9(
92 ; CHECK-NEXT: ret i8 -1
100 ; (X & C1) | C2 --> (X | C2) & (C1|C2)
101 define i8 @test10(i8 %A) {
102 ; CHECK-LABEL: @test10(
103 ; CHECK-NEXT: ret i8 -2
111 ; The following two cases only get folded by InstCombine,
112 ; see InstCombine/or-xor.ll.
114 ; (X ^ C1) | C2 --> (X | C2) ^ (C1&~C2)
115 define i8 @test11(i8 %A) {
116 ; CHECK-LABEL: @test11(
117 ; CHECK-NEXT: [[B:%.*]] = or i8 [[A:%.*]], -2
118 ; CHECK-NEXT: [[C:%.*]] = xor i8 [[B]], 13
119 ; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1
120 ; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12
121 ; CHECK-NEXT: ret i8 [[E]]
130 define i8 @test11v(<2 x i8> %A) {
131 ; CHECK-LABEL: @test11v(
132 ; CHECK-NEXT: [[B:%.*]] = or <2 x i8> [[A:%.*]], <i8 -2, i8 0>
133 ; CHECK-NEXT: [[CV:%.*]] = xor <2 x i8> [[B]], splat (i8 13)
134 ; CHECK-NEXT: [[C:%.*]] = extractelement <2 x i8> [[CV]], i32 0
135 ; CHECK-NEXT: [[D:%.*]] = or i8 [[C]], 1
136 ; CHECK-NEXT: [[E:%.*]] = xor i8 [[D]], 12
137 ; CHECK-NEXT: ret i8 [[E]]
139 %B = or <2 x i8> %A, <i8 -2, i8 0>
140 %CV = xor <2 x i8> %B, <i8 13, i8 13>
141 %C = extractelement <2 x i8> %CV, i32 0
147 ; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0.
148 ; If we have: ((V + N) & C1) | (V & C2)
149 ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
151 define i39 @test1_apint(i39 %V, i39 %M) {
152 ; CHECK-LABEL: @test1_apint(
153 ; CHECK-NEXT: [[N:%.*]] = and i39 [[M:%.*]], -274877906944
154 ; CHECK-NEXT: [[A:%.*]] = add i39 [[V:%.*]], [[N]]
155 ; CHECK-NEXT: ret i39 [[A]]
157 %C1 = xor i39 274877906943, -1 ;; C2 = 274877906943
158 %N = and i39 %M, 274877906944
161 %D = and i39 %V, 274877906943
166 define i7 @test2_apint(i7 %X) {
167 ; CHECK-LABEL: @test2_apint(
168 ; CHECK-NEXT: ret i7 [[X:%.*]]
174 define i17 @test3_apint(i17 %X) {
175 ; CHECK-LABEL: @test3_apint(
176 ; CHECK-NEXT: ret i17 -1
182 ; Test the case where Integer BitWidth > 64 && BitWidth <= 1024.
183 ; If we have: ((V + N) & C1) | (V & C2)
184 ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
186 define i399 @test4_apint(i399 %V, i399 %M) {
187 ; CHECK-LABEL: @test4_apint(
188 ; CHECK-NEXT: [[N:%.*]] = and i399 [[M:%.*]], 18446742974197923840
189 ; CHECK-NEXT: [[A:%.*]] = add i399 [[V:%.*]], [[N]]
190 ; CHECK-NEXT: ret i399 [[A]]
192 %C1 = xor i399 274877906943, -1 ;; C2 = 274877906943
193 %N = and i399 %M, 18446742974197923840
195 %B = and i399 %A, %C1
196 %D = and i399 %V, 274877906943
201 define i777 @test5_apint(i777 %X) {
202 ; CHECK-LABEL: @test5_apint(
203 ; CHECK-NEXT: ret i777 [[X:%.*]]
209 define i117 @test6_apint(i117 %X) {
210 ; CHECK-LABEL: @test6_apint(
211 ; CHECK-NEXT: ret i117 -1
217 ; Test the case where integer BitWidth <= 64 && BitWidth % 2 != 0.
218 ; Vector version of test1_apint with the add commuted
219 ; If we have: ((V + N) & C1) | (V & C2)
220 ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
222 define <2 x i39> @test7_apint(<2 x i39> %V, <2 x i39> %M) {
223 ; CHECK-LABEL: @test7_apint(
224 ; CHECK-NEXT: [[N:%.*]] = and <2 x i39> [[M:%.*]], splat (i39 -274877906944)
225 ; CHECK-NEXT: [[A:%.*]] = add <2 x i39> [[N]], [[V:%.*]]
226 ; CHECK-NEXT: ret <2 x i39> [[A]]
228 %C1 = xor <2 x i39> <i39 274877906943, i39 274877906943>, <i39 -1, i39 -1> ;; C2 = 274877906943
229 %N = and <2 x i39> %M, <i39 274877906944, i39 274877906944>
230 %A = add <2 x i39> %N, %V
231 %B = and <2 x i39> %A, %C1
232 %D = and <2 x i39> %V, <i39 274877906943, i39 274877906943>
233 %R = or <2 x i39> %B, %D
237 ; Test the case where Integer BitWidth > 64 && BitWidth <= 1024.
238 ; Vector version of test4_apint with the add and the or commuted
239 ; If we have: ((V + N) & C1) | (V & C2)
240 ; .. and C2 = ~C1 and C2 is 0+1+ and (N & C2) == 0
242 define <2 x i399> @test8_apint(<2 x i399> %V, <2 x i399> %M) {
243 ; CHECK-LABEL: @test8_apint(
244 ; CHECK-NEXT: [[N:%.*]] = and <2 x i399> [[M:%.*]], splat (i399 18446742974197923840)
245 ; CHECK-NEXT: [[A:%.*]] = add <2 x i399> [[N]], [[V:%.*]]
246 ; CHECK-NEXT: ret <2 x i399> [[A]]
248 %C1 = xor <2 x i399> <i399 274877906943, i399 274877906943>, <i399 -1, i399 -1> ;; C2 = 274877906943
249 %N = and <2 x i399> %M, <i399 18446742974197923840, i399 18446742974197923840>
250 %A = add <2 x i399> %N, %V
251 %B = and <2 x i399> %A, %C1
252 %D = and <2 x i399> %V, <i399 274877906943, i399 274877906943>
253 %R = or <2 x i399> %D, %B
259 define i8 @or_and_common_op_commute0(i8 %a, i8 %b) {
260 ; CHECK-LABEL: @or_and_common_op_commute0(
261 ; CHECK-NEXT: ret i8 [[A:%.*]]
268 define <2 x i8> @or_and_common_op_commute1(<2 x i8> %a, <2 x i8> %b) {
269 ; CHECK-LABEL: @or_and_common_op_commute1(
270 ; CHECK-NEXT: ret <2 x i8> [[A:%.*]]
272 %and = and <2 x i8> %b, %a
273 %or = or <2 x i8> %and, %a
277 define i8 @or_and_common_op_commute2(i8 %a, i8 %b) {
278 ; CHECK-LABEL: @or_and_common_op_commute2(
279 ; CHECK-NEXT: ret i8 [[A:%.*]]
286 define <2 x i8> @or_and_common_op_commute3(<2 x i8> %a, <2 x i8> %b) {
287 ; CHECK-LABEL: @or_and_common_op_commute3(
288 ; CHECK-NEXT: ret <2 x i8> [[A:%.*]]
290 %and = and <2 x i8> %b, %a
291 %or = or <2 x i8> %a, %and
297 define i1 @or_with_not_op_commute1(i1 %a, i1 %b) {
298 ; CHECK-LABEL: @or_with_not_op_commute1(
299 ; CHECK-NEXT: ret i1 true
302 %not = xor i1 %ab, -1
309 define i8 @or_with_not_op_commute2(i8 %a, i8 %b) {
310 ; CHECK-LABEL: @or_with_not_op_commute2(
311 ; CHECK-NEXT: ret i8 -1
314 %not = xor i8 %ab, -1
321 define <3 x i17> @or_with_not_op_commute3(<3 x i17> %a, <3 x i17> %b) {
322 ; CHECK-LABEL: @or_with_not_op_commute3(
323 ; CHECK-NEXT: ret <3 x i17> splat (i17 -1)
325 %ab = and <3 x i17> %a, %b
326 %not = xor <3 x i17> %ab, <i17 -1, i17 -1, i17 -1>
327 %r = or <3 x i17> %not, %a
333 define <2 x i1> @or_with_not_op_commute4(<2 x i1> %a, <2 x i1> %b) {
334 ; CHECK-LABEL: @or_with_not_op_commute4(
335 ; CHECK-NEXT: ret <2 x i1> splat (i1 true)
337 %ab = and <2 x i1> %b, %a
338 %not = xor <2 x i1> %ab, <i1 -1, i1 poison>
339 %r = or <2 x i1> %not, %a
343 define i32 @poison(i32 %x) {
344 ; CHECK-LABEL: @poison(
345 ; CHECK-NEXT: ret i32 poison
347 %v = or i32 %x, poison
351 ; (~A & B) | ~(A | B) --> ~A
353 define i4 @and_or_not_or_commute0(i4 %A, i4 %B) {
354 ; CHECK-LABEL: @and_or_not_or_commute0(
355 ; CHECK-NEXT: [[NOTA:%.*]] = xor i4 [[A:%.*]], -1
356 ; CHECK-NEXT: ret i4 [[NOTA]]
358 %nota = xor i4 %A, -1
359 %and = and i4 %nota, %B
361 %notab = xor i4 %or, -1
362 %r = or i4 %and, %notab
366 define i41 @and_or_not_or_commute1(i41 %A, i41 %B) {
367 ; CHECK-LABEL: @and_or_not_or_commute1(
368 ; CHECK-NEXT: [[NOTA:%.*]] = xor i41 [[A:%.*]], -1
369 ; CHECK-NEXT: ret i41 [[NOTA]]
371 %nota = xor i41 %A, -1
372 %and = and i41 %B, %nota
374 %notab = xor i41 %or, -1
375 %r = or i41 %and, %notab
379 define i8 @and_or_not_or_commute2(i8 %A, i8 %B) {
380 ; CHECK-LABEL: @and_or_not_or_commute2(
381 ; CHECK-NEXT: [[NOTA:%.*]] = xor i8 [[A:%.*]], -1
382 ; CHECK-NEXT: ret i8 [[NOTA]]
384 %nota = xor i8 %A, -1
385 %and = and i8 %nota, %B
387 %notab = xor i8 %or, -1
388 %r = or i8 %and, %notab
392 define <2 x i4> @and_or_not_or_commute3(<2 x i4> %A, <2 x i4> %B) {
393 ; CHECK-LABEL: @and_or_not_or_commute3(
394 ; CHECK-NEXT: [[NOTA:%.*]] = xor <2 x i4> [[A:%.*]], splat (i4 -1)
395 ; CHECK-NEXT: ret <2 x i4> [[NOTA]]
397 %nota = xor <2 x i4> %A, <i4 -1, i4 -1>
398 %and = and <2 x i4> %B, %nota
399 %or = or <2 x i4> %B, %A
400 %notab = xor <2 x i4> %or, <i4 -1, i4 -1>
401 %r = or <2 x i4> %and, %notab
405 define i4 @and_or_not_or_commute4(i4 %A, i4 %B) {
406 ; CHECK-LABEL: @and_or_not_or_commute4(
407 ; CHECK-NEXT: [[NOTA:%.*]] = xor i4 [[A:%.*]], -1
408 ; CHECK-NEXT: ret i4 [[NOTA]]
410 %nota = xor i4 %A, -1
411 %and = and i4 %nota, %B
413 %notab = xor i4 %or, -1
414 %r = or i4 %notab, %and
418 define i41 @and_or_not_or_commute5(i41 %A, i41 %B) {
419 ; CHECK-LABEL: @and_or_not_or_commute5(
420 ; CHECK-NEXT: [[NOTA:%.*]] = xor i41 [[A:%.*]], -1
421 ; CHECK-NEXT: ret i41 [[NOTA]]
423 %nota = xor i41 %A, -1
424 %and = and i41 %B, %nota
426 %notab = xor i41 %or, -1
427 %r = or i41 %notab, %and
431 define i8 @and_or_not_or_commute6(i8 %A, i8 %B) {
432 ; CHECK-LABEL: @and_or_not_or_commute6(
433 ; CHECK-NEXT: [[NOTA:%.*]] = xor i8 [[A:%.*]], -1
434 ; CHECK-NEXT: ret i8 [[NOTA]]
436 %nota = xor i8 %A, -1
437 %and = and i8 %nota, %B
439 %notab = xor i8 %or, -1
440 %r = or i8 %notab, %and
444 define <2 x i4> @and_or_not_or_commute7(<2 x i4> %A, <2 x i4> %B) {
445 ; CHECK-LABEL: @and_or_not_or_commute7(
446 ; CHECK-NEXT: [[NOTA:%.*]] = xor <2 x i4> [[A:%.*]], splat (i4 -1)
447 ; CHECK-NEXT: ret <2 x i4> [[NOTA]]
449 %nota = xor <2 x i4> %A, <i4 -1, i4 -1>
450 %and = and <2 x i4> %B, %nota
451 %or = or <2 x i4> %B, %A
452 %notab = xor <2 x i4> %or, <i4 -1, i4 -1>
453 %r = or <2 x i4> %notab, %and
457 ; (~A & B) | ~(A | B) --> ~A with logical and
458 define i1 @and_or_not_or_logical(i1 %A, i1 %B) {
459 ; CHECK-LABEL: @and_or_not_or_logical(
460 ; CHECK-NEXT: [[V:%.*]] = xor i1 [[A:%.*]], true
461 ; CHECK-NEXT: ret i1 [[V]]
464 %X = select i1 %V, i1 %B, i1 false
471 ; (~B & A) | ~(A | B) --> ~A with logical and
472 define i1 @and_or_not_or_logical_rev(i1 %A, i1 %B) {
473 ; CHECK-LABEL: @and_or_not_or_logical_rev(
474 ; CHECK-NEXT: [[V:%.*]] = xor i1 [[A:%.*]], true
475 ; CHECK-NEXT: ret i1 [[V]]
478 %X = select i1 %B, i1 %V, i1 false
485 ; (~A & B) | ~(A | B) --> ~A with logical And and logical Or
486 define i1 @and_or_not_logical_or_logical_rev(i1 %A, i1 %B) {
487 ; CHECK-LABEL: @and_or_not_logical_or_logical_rev(
488 ; CHECK-NEXT: [[V:%.*]] = xor i1 [[A:%.*]], true
489 ; CHECK-NEXT: ret i1 [[V]]
492 %X = select i1 %B, i1 %V, i1 false
493 %W = select i1 %B, i1 true, i1 %A
499 ; negative test - It is not safe to propagate an undef element from the 'not' op.
501 define <2 x i4> @and_or_not_or_commute7_undef_elt(<2 x i4> %A, <2 x i4> %B) {
502 ; CHECK-LABEL: @and_or_not_or_commute7_undef_elt(
503 ; CHECK-NEXT: [[NOTA:%.*]] = xor <2 x i4> [[A:%.*]], <i4 undef, i4 -1>
504 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[NOTA]]
505 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[B]], [[A]]
506 ; CHECK-NEXT: [[NOTAB:%.*]] = xor <2 x i4> [[OR]], splat (i4 -1)
507 ; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[NOTAB]], [[AND]]
508 ; CHECK-NEXT: ret <2 x i4> [[R]]
510 %nota = xor <2 x i4> %A, <i4 undef, i4 -1>
511 %and = and <2 x i4> %B, %nota
512 %or = or <2 x i4> %B, %A
513 %notab = xor <2 x i4> %or, <i4 -1, i4 -1>
514 %r = or <2 x i4> %notab, %and
518 ; doing the same with poison is safe.
520 define <2 x i4> @and_or_not_or_commute7_poison_elt(<2 x i4> %A, <2 x i4> %B) {
521 ; CHECK-LABEL: @and_or_not_or_commute7_poison_elt(
522 ; CHECK-NEXT: [[NOTA:%.*]] = xor <2 x i4> [[A:%.*]], <i4 poison, i4 -1>
523 ; CHECK-NEXT: ret <2 x i4> [[NOTA]]
525 %nota = xor <2 x i4> %A, <i4 poison, i4 -1>
526 %and = and <2 x i4> %B, %nota
527 %or = or <2 x i4> %B, %A
528 %notab = xor <2 x i4> %or, <i4 -1, i4 -1>
529 %r = or <2 x i4> %notab, %and
533 ; (A | B) | (A ^ B) --> A | B
535 define i69 @or_or_xor(i69 %A, i69 %B) {
536 ; CHECK-LABEL: @or_or_xor(
537 ; CHECK-NEXT: [[I1:%.*]] = or i69 [[A:%.*]], [[B:%.*]]
538 ; CHECK-NEXT: ret i69 [[I1]]
542 %i3 = or i69 %i1, %i2
546 ; (B | A) | (A ^ B) --> B | A
548 define i8 @or_or_xor_inner_or_commuted(i8 %A, i8 %B) {
549 ; CHECK-LABEL: @or_or_xor_inner_or_commuted(
550 ; CHECK-NEXT: [[I1:%.*]] = or i8 [[B:%.*]], [[A:%.*]]
551 ; CHECK-NEXT: ret i8 [[I1]]
559 ; (A ^ B) | (A | B) --> A | B
561 define <4 x i4> @or_or_xor_commuted(<4 x i4> %A, <4 x i4> %B) {
562 ; CHECK-LABEL: @or_or_xor_commuted(
563 ; CHECK-NEXT: [[I1:%.*]] = or <4 x i4> [[A:%.*]], [[B:%.*]]
564 ; CHECK-NEXT: ret <4 x i4> [[I1]]
566 %i1 = or <4 x i4> %A, %B
567 %i2 = xor <4 x i4> %A, %B
568 %i3 = or <4 x i4> %i2, %i1
572 ; (A ^ B) | (B | A) --> B | A
574 define i4 @or_or_xor_inner_or_outer_or_commuted(i4 %A, i4 %B) {
575 ; CHECK-LABEL: @or_or_xor_inner_or_outer_or_commuted(
576 ; CHECK-NEXT: [[I1:%.*]] = or i4 [[B:%.*]], [[A:%.*]]
577 ; CHECK-NEXT: ret i4 [[I1]]
585 define i32 @shifted_all_ones(i32 %shamt) {
586 ; CHECK-LABEL: @shifted_all_ones(
587 ; CHECK-NEXT: ret i32 -1
589 %r = lshr i32 -1, %shamt
590 %s = sub i32 32, %shamt
596 ; Sub from less than bitwidth is ok (overlapping ones).
598 define i32 @shifted_all_ones_commute(i32 %shamt) {
599 ; CHECK-LABEL: @shifted_all_ones_commute(
600 ; CHECK-NEXT: ret i32 -1
602 %r = lshr i32 -1, %shamt
603 %s = sub i32 31, %shamt
609 define <2 x i9> @shifted_all_ones_sub_on_lshr(<2 x i9> %shamt) {
610 ; CHECK-LABEL: @shifted_all_ones_sub_on_lshr(
611 ; CHECK-NEXT: ret <2 x i9> splat (i9 -1)
613 %l = shl <2 x i9> <i9 -1, i9 -1>, %shamt
614 %s = sub <2 x i9> <i9 5, i9 5>, %shamt
615 %r = lshr <2 x i9> <i9 -1, i9 -1>, %s
616 %o = or <2 x i9> %l, %r
620 define i8 @shifted_all_ones_sub_on_lshr_commute(i8 %shamt) {
621 ; CHECK-LABEL: @shifted_all_ones_sub_on_lshr_commute(
622 ; CHECK-NEXT: ret i8 -1
624 %l = shl i8 -1, %shamt
625 %s = sub i8 8, %shamt
631 ; negative test - need -1 in general case
633 define i32 @shifted_not_all_ones(i32 %shamt) {
634 ; CHECK-LABEL: @shifted_not_all_ones(
635 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -2, [[SHAMT:%.*]]
636 ; CHECK-NEXT: [[S:%.*]] = sub i32 31, [[SHAMT]]
637 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
638 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
639 ; CHECK-NEXT: ret i32 [[O]]
641 %r = lshr i32 -2, %shamt
642 %s = sub i32 31, %shamt
648 ; negative test - opposite shift amount may be too big
650 define i32 @shifted_all_ones_greater_than_bitwidth(i32 %shamt) {
651 ; CHECK-LABEL: @shifted_all_ones_greater_than_bitwidth(
652 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -1, [[SHAMT:%.*]]
653 ; CHECK-NEXT: [[S:%.*]] = sub i32 33, [[SHAMT]]
654 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
655 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
656 ; CHECK-NEXT: ret i32 [[O]]
658 %r = lshr i32 -1, %shamt
659 %s = sub i32 33, %shamt
665 ; negative test - shift amount must be derived from same base
667 define i32 @shifted_all_ones_not_same_amt(i32 %shamt, i32 %other) {
668 ; CHECK-LABEL: @shifted_all_ones_not_same_amt(
669 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -1, [[SHAMT:%.*]]
670 ; CHECK-NEXT: [[S:%.*]] = sub i32 32, [[OTHER:%.*]]
671 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
672 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
673 ; CHECK-NEXT: ret i32 [[O]]
675 %r = lshr i32 -1, %shamt
676 %s = sub i32 32, %other
682 ; (A & B) | ~(A ^ B) --> ~(A ^ B)
684 define i4 @or_nxor_and_commute0(i4 %a, i4 %b) {
685 ; CHECK-LABEL: @or_nxor_and_commute0(
686 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B:%.*]]
687 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
688 ; CHECK-NEXT: ret i4 [[NOT]]
692 %not = xor i4 %xor, -1
693 %r = or i4 %and, %not
697 define <2 x i4> @or_nxor_and_commute1(<2 x i4> %a, <2 x i4> %b) {
698 ; CHECK-LABEL: @or_nxor_and_commute1(
699 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A:%.*]], [[B:%.*]]
700 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], splat (i4 -1)
701 ; CHECK-NEXT: ret <2 x i4> [[NOT]]
703 %and = and <2 x i4> %a, %b
704 %xor = xor <2 x i4> %a, %b
705 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
706 %r = or <2 x i4> %not, %and
710 define i74 @or_nxor_and_commute2(i74 %a, i74 %b) {
711 ; CHECK-LABEL: @or_nxor_and_commute2(
712 ; CHECK-NEXT: [[XOR:%.*]] = xor i74 [[A:%.*]], [[B:%.*]]
713 ; CHECK-NEXT: [[NOT:%.*]] = xor i74 [[XOR]], -1
714 ; CHECK-NEXT: ret i74 [[NOT]]
716 %and = and i74 %b, %a
717 %xor = xor i74 %a, %b
718 %not = xor i74 %xor, -1
719 %r = or i74 %and, %not
723 define <2 x i4> @or_nxor_and_commute3(<2 x i4> %a, <2 x i4> %b) {
724 ; CHECK-LABEL: @or_nxor_and_commute3(
725 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A:%.*]], [[B:%.*]]
726 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], splat (i4 -1)
727 ; CHECK-NEXT: ret <2 x i4> [[NOT]]
729 %and = and <2 x i4> %b, %a
730 %xor = xor <2 x i4> %a, %b
731 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
732 %r = or <2 x i4> %not, %and
736 ; negative test - must have common operands
738 define i4 @or_nxor_and_wrong_val1(i4 %a, i4 %b, i4 %c) {
739 ; CHECK-LABEL: @or_nxor_and_wrong_val1(
740 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[A:%.*]], [[C:%.*]]
741 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A]], [[B:%.*]]
742 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
743 ; CHECK-NEXT: [[R:%.*]] = or i4 [[AND]], [[NOT]]
744 ; CHECK-NEXT: ret i4 [[R]]
748 %not = xor i4 %xor, -1
749 %r = or i4 %and, %not
753 ; negative test - must have common operands
755 define i4 @or_nxor_and_wrong_val2(i4 %a, i4 %b, i4 %c) {
756 ; CHECK-LABEL: @or_nxor_and_wrong_val2(
757 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[C:%.*]], [[B:%.*]]
758 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B]]
759 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
760 ; CHECK-NEXT: [[R:%.*]] = or i4 [[AND]], [[NOT]]
761 ; CHECK-NEXT: ret i4 [[R]]
765 %not = xor i4 %xor, -1
766 %r = or i4 %and, %not
770 ; negative test - undef in 'not' is allowed
772 define <2 x i4> @or_nxor_and_undef_elt(<2 x i4> %a, <2 x i4> %b) {
773 ; CHECK-LABEL: @or_nxor_and_undef_elt(
774 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[A:%.*]]
775 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A]], [[B]]
776 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 undef>
777 ; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[NOT]], [[AND]]
778 ; CHECK-NEXT: ret <2 x i4> [[R]]
780 %and = and <2 x i4> %b, %a
781 %xor = xor <2 x i4> %a, %b
782 %not = xor <2 x i4> %xor, <i4 -1, i4 undef>
783 %r = or <2 x i4> %not, %and
787 ; Same with poison is safe.
789 define <2 x i4> @or_nxor_and_poison_elt(<2 x i4> %a, <2 x i4> %b) {
790 ; CHECK-LABEL: @or_nxor_and_poison_elt(
791 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A:%.*]], [[B:%.*]]
792 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 poison>
793 ; CHECK-NEXT: ret <2 x i4> [[NOT]]
795 %and = and <2 x i4> %b, %a
796 %xor = xor <2 x i4> %a, %b
797 %not = xor <2 x i4> %xor, <i4 -1, i4 poison>
798 %r = or <2 x i4> %not, %and
802 ; ~(A ^ B) | (A | B) --> -1
804 define i4 @or_nxor_or_commute0(i4 %a, i4 %b) {
805 ; CHECK-LABEL: @or_nxor_or_commute0(
806 ; CHECK-NEXT: ret i4 -1
810 %not = xor i4 %xor, -1
815 define <2 x i4> @or_nxor_or_commute1(<2 x i4> %a, <2 x i4> %b) {
816 ; CHECK-LABEL: @or_nxor_or_commute1(
817 ; CHECK-NEXT: ret <2 x i4> splat (i4 -1)
819 %or = or <2 x i4> %a, %b
820 %xor = xor <2 x i4> %a, %b
821 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
822 %r = or <2 x i4> %or, %not
826 define i74 @or_nxor_or_commute2(i74 %a, i74 %b) {
827 ; CHECK-LABEL: @or_nxor_or_commute2(
828 ; CHECK-NEXT: ret i74 -1
831 %xor = xor i74 %a, %b
832 %not = xor i74 %xor, -1
833 %r = or i74 %not, %or
837 define <2 x i4> @or_nxor_or_commute3(<2 x i4> %a, <2 x i4> %b) {
838 ; CHECK-LABEL: @or_nxor_or_commute3(
839 ; CHECK-NEXT: ret <2 x i4> splat (i4 -1)
841 %or = or <2 x i4> %b, %a
842 %xor = xor <2 x i4> %a, %b
843 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
844 %r = or <2 x i4> %or, %not
848 ; negative test - must have common operands
850 define i4 @or_nxor_or_wrong_val1(i4 %a, i4 %b, i4 %c) {
851 ; CHECK-LABEL: @or_nxor_or_wrong_val1(
852 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[A:%.*]], [[C:%.*]]
853 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A]], [[B:%.*]]
854 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
855 ; CHECK-NEXT: [[R:%.*]] = or i4 [[NOT]], [[OR]]
856 ; CHECK-NEXT: ret i4 [[R]]
860 %not = xor i4 %xor, -1
865 ; negative test - must have common operands
867 define i4 @or_nxor_or_wrong_val2(i4 %a, i4 %b, i4 %c) {
868 ; CHECK-LABEL: @or_nxor_or_wrong_val2(
869 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[C:%.*]], [[B:%.*]]
870 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B]]
871 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
872 ; CHECK-NEXT: [[R:%.*]] = or i4 [[NOT]], [[OR]]
873 ; CHECK-NEXT: ret i4 [[R]]
877 %not = xor i4 %xor, -1
882 ; negative test - poison in 'not' is allowed
884 define <2 x i4> @or_nxor_or_poison_elt(<2 x i4> %a, <2 x i4> %b) {
885 ; CHECK-LABEL: @or_nxor_or_poison_elt(
886 ; CHECK-NEXT: ret <2 x i4> splat (i4 -1)
888 %or = or <2 x i4> %b, %a
889 %xor = xor <2 x i4> %a, %b
890 %not = xor <2 x i4> %xor, <i4 -1, i4 poison>
891 %r = or <2 x i4> %or, %not
895 ; (A ^ B) | (~A | B) --> -1
897 define i4 @or_xor_not_op_or(i4 %a, i4 %b){
898 ; CHECK-LABEL: @or_xor_not_op_or(
899 ; CHECK-NEXT: ret i4 -1
902 %nota = xor i4 %a, -1
903 %or = or i4 %nota, %b
908 ; (A ^ B) | (B | ~A) --> -1
910 define i71 @or_xor_not_op_or_commute1(i71 %a, i71 %b){
911 ; CHECK-LABEL: @or_xor_not_op_or_commute1(
912 ; CHECK-NEXT: ret i71 -1
914 %xor = xor i71 %a, %b
915 %nota = xor i71 %a, -1
916 %or = or i71 %b, %nota
917 %r = or i71 %xor, %or
921 ; (B ^ A) | (~A | B) --> -1
923 define i32 @or_xor_not_op_or_commute2(i32 %a, i32 %b){
924 ; CHECK-LABEL: @or_xor_not_op_or_commute2(
925 ; CHECK-NEXT: ret i32 -1
927 %xor = xor i32 %b, %a
928 %nota = xor i32 %a, -1
929 %or = or i32 %nota, %b
930 %r = or i32 %xor, %or
934 ; (B ^ A) | (B | ~A) --> -1
936 define i32 @or_xor_not_op_or_commute3(i32 %a, i32 %b){
937 ; CHECK-LABEL: @or_xor_not_op_or_commute3(
938 ; CHECK-NEXT: ret i32 -1
940 %xor = xor i32 %b, %a
941 %nota = xor i32 %a, -1
942 %or = or i32 %b, %nota
943 %r = or i32 %xor, %or
947 ; (~A | B) | (A ^ B) --> -1
949 define i32 @or_xor_not_op_or_commute4(i32 %a, i32 %b){
950 ; CHECK-LABEL: @or_xor_not_op_or_commute4(
951 ; CHECK-NEXT: ret i32 -1
953 %xor = xor i32 %a, %b
954 %nota = xor i32 %a, -1
955 %or = or i32 %nota, %b
956 %r = or i32 %or, %xor
960 ; (B | ~A) | (A ^ B) --> -1
962 define i32 @or_xor_not_op_or_commute5(i32 %a, i32 %b){
963 ; CHECK-LABEL: @or_xor_not_op_or_commute5(
964 ; CHECK-NEXT: ret i32 -1
966 %xor = xor i32 %a, %b
967 %nota = xor i32 %a, -1
968 %or = or i32 %b, %nota
969 %r = or i32 %or, %xor
973 ; (~A | B) | (B ^ A) --> -1
975 define i32 @or_xor_not_op_or_commute6(i32 %a, i32 %b){
976 ; CHECK-LABEL: @or_xor_not_op_or_commute6(
977 ; CHECK-NEXT: ret i32 -1
979 %xor = xor i32 %b, %a
980 %nota = xor i32 %a, -1
981 %or = or i32 %nota, %b
982 %r = or i32 %or, %xor
986 ; (B | ~A) | (B ^ A) --> -1
988 define i32 @or_xor_not_op_or_commute7(i32 %a, i32 %b){
989 ; CHECK-LABEL: @or_xor_not_op_or_commute7(
990 ; CHECK-NEXT: ret i32 -1
992 %xor = xor i32 %b, %a
993 %nota = xor i32 %a, -1
994 %or = or i32 %b, %nota
995 %r = or i32 %or, %xor
999 define <2 x i4> @or_xor_not_op_or_poison_elt(<2 x i4> %a, <2 x i4> %b) {
1000 ; CHECK-LABEL: @or_xor_not_op_or_poison_elt(
1001 ; CHECK-NEXT: ret <2 x i4> splat (i4 -1)
1003 %xor = xor <2 x i4> %a, %b
1004 %nota = xor <2 x i4> %a, <i4 -1, i4 poison>
1005 %or = or <2 x i4> %nota, %b
1006 %r = or <2 x i4> %xor, %or
1012 define i16 @or_xor_not_op_or_wrong_val(i16 %a, i16 %b, i16 %c) {
1013 ; CHECK-LABEL: @or_xor_not_op_or_wrong_val(
1014 ; CHECK-NEXT: [[XOR:%.*]] = xor i16 [[A:%.*]], [[C:%.*]]
1015 ; CHECK-NEXT: [[NOTA:%.*]] = xor i16 [[A]], -1
1016 ; CHECK-NEXT: [[OR:%.*]] = or i16 [[NOTA]], [[B:%.*]]
1017 ; CHECK-NEXT: [[R:%.*]] = or i16 [[XOR]], [[OR]]
1018 ; CHECK-NEXT: ret i16 [[R]]
1020 %xor = xor i16 %a, %c
1021 %nota = xor i16 %a, -1
1022 %or = or i16 %nota, %b
1023 %r = or i16 %xor, %or
1027 ; ~(x & y) | (x ^ y) --> ~(x & y)
1029 define i4 @or_nand_xor(i4 %x, i4 %y) {
1030 ; CHECK-LABEL: @or_nand_xor(
1031 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[X:%.*]], [[Y:%.*]]
1032 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1033 ; CHECK-NEXT: ret i4 [[NAND]]
1035 %and = and i4 %x, %y
1036 %xor = xor i4 %x, %y
1037 %nand = xor i4 %and, -1
1038 %or = or i4 %xor, %nand
1042 define <2 x i4> @or_nand_xor_commute1(<2 x i4> %x, <2 x i4> %y) {
1043 ; CHECK-LABEL: @or_nand_xor_commute1(
1044 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Y:%.*]], [[X:%.*]]
1045 ; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], splat (i4 -1)
1046 ; CHECK-NEXT: ret <2 x i4> [[NAND]]
1048 %and = and <2 x i4> %y, %x
1049 %xor = xor <2 x i4> %x, %y
1050 %nand = xor <2 x i4> %and, <i4 -1, i4 -1>
1051 %or = or <2 x i4> %xor, %nand
1055 define i71 @or_nand_xor_commute2(i71 %x, i71 %y) {
1056 ; CHECK-LABEL: @or_nand_xor_commute2(
1057 ; CHECK-NEXT: [[AND:%.*]] = and i71 [[X:%.*]], [[Y:%.*]]
1058 ; CHECK-NEXT: [[NAND:%.*]] = xor i71 [[AND]], -1
1059 ; CHECK-NEXT: ret i71 [[NAND]]
1061 %and = and i71 %x, %y
1062 %xor = xor i71 %x, %y
1063 %nand = xor i71 %and, -1
1064 %or = or i71 %nand, %xor
1068 define i4 @or_nand_xor_commute3(i4 %x, i4 %y) {
1069 ; CHECK-LABEL: @or_nand_xor_commute3(
1070 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[Y:%.*]], [[X:%.*]]
1071 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1072 ; CHECK-NEXT: ret i4 [[NAND]]
1074 %and = and i4 %y, %x
1075 %xor = xor i4 %x, %y
1076 %nand = xor i4 %and, -1
1077 %or = or i4 %nand, %xor
1081 ; negative test wrong operand
1083 define i4 @or_nand_xor_wrong_val(i4 %x, i4 %y, i4 %z) {
1084 ; CHECK-LABEL: @or_nand_xor_wrong_val(
1085 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[X:%.*]], [[Y:%.*]]
1086 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[X]], [[Z:%.*]]
1087 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1088 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[XOR]], [[NAND]]
1089 ; CHECK-NEXT: ret i4 [[OR]]
1091 %and = and i4 %x, %y
1092 %xor = xor i4 %x, %z
1093 %nand = xor i4 %and, -1
1094 %or = or i4 %xor, %nand
1098 ; negative test - undef element in 'not' is not allowed
1100 define <2 x i4> @or_nand_xor_undef_elt(<2 x i4> %x, <2 x i4> %y) {
1101 ; CHECK-LABEL: @or_nand_xor_undef_elt(
1102 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Y:%.*]], [[X:%.*]]
1103 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[X]], [[Y]]
1104 ; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], <i4 undef, i4 -1>
1105 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[XOR]], [[NAND]]
1106 ; CHECK-NEXT: ret <2 x i4> [[OR]]
1108 %and = and <2 x i4> %y, %x
1109 %xor = xor <2 x i4> %x, %y
1110 %nand = xor <2 x i4> %and, <i4 undef, i4 -1>
1111 %or = or <2 x i4> %xor, %nand
1115 ; Same with poison is safe.
1117 define <2 x i4> @or_nand_xor_poison_elt(<2 x i4> %x, <2 x i4> %y) {
1118 ; CHECK-LABEL: @or_nand_xor_poison_elt(
1119 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Y:%.*]], [[X:%.*]]
1120 ; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], <i4 poison, i4 -1>
1121 ; CHECK-NEXT: ret <2 x i4> [[NAND]]
1123 %and = and <2 x i4> %y, %x
1124 %xor = xor <2 x i4> %x, %y
1125 %nand = xor <2 x i4> %and, <i4 poison, i4 -1>
1126 %or = or <2 x i4> %xor, %nand
1130 declare i32 @llvm.fshl.i32 (i32, i32, i32)
1131 declare i32 @llvm.fshr.i32 (i32, i32, i32)
1133 define i32 @or_shl_fshl(i32 %x, i32 %y, i32 %s) {
1134 ; CHECK-LABEL: @or_shl_fshl(
1135 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[Y:%.*]], i32 [[X:%.*]], i32 [[S:%.*]])
1136 ; CHECK-NEXT: ret i32 [[FUN]]
1138 %shy = shl i32 %y, %s
1139 %fun = call i32 @llvm.fshl.i32(i32 %y, i32 %x, i32 %s)
1140 %or = or i32 %fun, %shy
1144 define i32 @or_shl_fshl_commute(i32 %x, i32 %y, i32 %s) {
1145 ; CHECK-LABEL: @or_shl_fshl_commute(
1146 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[Y:%.*]], i32 [[X:%.*]], i32 [[S:%.*]])
1147 ; CHECK-NEXT: ret i32 [[FUN]]
1149 %shy = shl i32 %y, %s
1150 %fun = call i32 @llvm.fshl.i32(i32 %y, i32 %x, i32 %s)
1151 %or = or i32 %shy, %fun
1155 ; negative test - fshl operands are not commutative
1157 define i32 @or_shl_fshl_wrong_order(i32 %x, i32 %y, i32 %s) {
1158 ; CHECK-LABEL: @or_shl_fshl_wrong_order(
1159 ; CHECK-NEXT: [[SHY:%.*]] = shl i32 [[Y:%.*]], [[S:%.*]]
1160 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y]], i32 [[S]])
1161 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[FUN]], [[SHY]]
1162 ; CHECK-NEXT: ret i32 [[OR]]
1164 %shy = shl i32 %y, %s
1165 %fun = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %s)
1166 %or = or i32 %fun, %shy
1170 define i32 @or_lshr_fshr(i32 %x, i32 %y, i32 %s) {
1171 ; CHECK-LABEL: @or_lshr_fshr(
1172 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[S:%.*]])
1173 ; CHECK-NEXT: ret i32 [[FUN]]
1175 %shy = lshr i32 %y, %s
1176 %fun = call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %s)
1177 %or = or i32 %fun, %shy
1181 define i32 @or_lshr_fshr_commute(i32 %x, i32 %y, i32 %s) {
1182 ; CHECK-LABEL: @or_lshr_fshr_commute(
1183 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[S:%.*]])
1184 ; CHECK-NEXT: ret i32 [[FUN]]
1186 %shy = lshr i32 %y, %s
1187 %fun = call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %s)
1188 %or = or i32 %shy, %fun
1192 ; negative test - fshr operands are not commutative
1194 define i32 @or_lshr_fshr_wrong_order(i32 %x, i32 %y, i32 %s) {
1195 ; CHECK-LABEL: @or_lshr_fshr_wrong_order(
1196 ; CHECK-NEXT: [[SHY:%.*]] = lshr i32 [[Y:%.*]], [[S:%.*]]
1197 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[Y]], i32 [[X:%.*]], i32 [[S]])
1198 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[FUN]], [[SHY]]
1199 ; CHECK-NEXT: ret i32 [[OR]]
1201 %shy = lshr i32 %y, %s
1202 %fun = call i32 @llvm.fshr.i32(i32 %y, i32 %x, i32 %s)
1203 %or = or i32 %fun, %shy