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_undef_elt(<3 x i8> %A) {
21 ; CHECK-LABEL: @all_ones_vec_with_undef_elt(
22 ; CHECK-NEXT: ret <3 x i8> <i8 -1, i8 -1, i8 -1>
24 %B = or <3 x i8> %A, <i8 -1, i8 undef, 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_undef(<2 x i4> %A) {
72 ; CHECK-LABEL: @or_not_commute_vec_undef(
73 ; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
75 %NotA = xor <2 x i4> %A, <i4 -1, i4 undef>
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]], <i8 13, 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:%.*]], <i39 -274877906944, 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:%.*]], <i399 18446742974197923840, 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> <i17 -1, i17 -1, 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> <i1 true, i1 true>
337 %ab = and <2 x i1> %b, %a
338 %not = xor <2 x i1> %ab, <i1 -1, i1 undef>
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:%.*]], <i4 -1, 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:%.*]], <i4 -1, 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]], <i4 -1, 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 ; (A | B) | (A ^ B) --> A | B
520 define i69 @or_or_xor(i69 %A, i69 %B) {
521 ; CHECK-LABEL: @or_or_xor(
522 ; CHECK-NEXT: [[I1:%.*]] = or i69 [[A:%.*]], [[B:%.*]]
523 ; CHECK-NEXT: ret i69 [[I1]]
527 %i3 = or i69 %i1, %i2
531 ; (B | A) | (A ^ B) --> B | A
533 define i8 @or_or_xor_inner_or_commuted(i8 %A, i8 %B) {
534 ; CHECK-LABEL: @or_or_xor_inner_or_commuted(
535 ; CHECK-NEXT: [[I1:%.*]] = or i8 [[B:%.*]], [[A:%.*]]
536 ; CHECK-NEXT: ret i8 [[I1]]
544 ; (A ^ B) | (A | B) --> A | B
546 define <4 x i4> @or_or_xor_commuted(<4 x i4> %A, <4 x i4> %B) {
547 ; CHECK-LABEL: @or_or_xor_commuted(
548 ; CHECK-NEXT: [[I1:%.*]] = or <4 x i4> [[A:%.*]], [[B:%.*]]
549 ; CHECK-NEXT: ret <4 x i4> [[I1]]
551 %i1 = or <4 x i4> %A, %B
552 %i2 = xor <4 x i4> %A, %B
553 %i3 = or <4 x i4> %i2, %i1
557 ; (A ^ B) | (B | A) --> B | A
559 define i4 @or_or_xor_inner_or_outer_or_commuted(i4 %A, i4 %B) {
560 ; CHECK-LABEL: @or_or_xor_inner_or_outer_or_commuted(
561 ; CHECK-NEXT: [[I1:%.*]] = or i4 [[B:%.*]], [[A:%.*]]
562 ; CHECK-NEXT: ret i4 [[I1]]
570 define i32 @shifted_all_ones(i32 %shamt) {
571 ; CHECK-LABEL: @shifted_all_ones(
572 ; CHECK-NEXT: ret i32 -1
574 %r = lshr i32 -1, %shamt
575 %s = sub i32 32, %shamt
581 ; Sub from less than bitwidth is ok (overlapping ones).
583 define i32 @shifted_all_ones_commute(i32 %shamt) {
584 ; CHECK-LABEL: @shifted_all_ones_commute(
585 ; CHECK-NEXT: ret i32 -1
587 %r = lshr i32 -1, %shamt
588 %s = sub i32 31, %shamt
594 define <2 x i9> @shifted_all_ones_sub_on_lshr(<2 x i9> %shamt) {
595 ; CHECK-LABEL: @shifted_all_ones_sub_on_lshr(
596 ; CHECK-NEXT: ret <2 x i9> <i9 -1, i9 -1>
598 %l = shl <2 x i9> <i9 -1, i9 -1>, %shamt
599 %s = sub <2 x i9> <i9 5, i9 5>, %shamt
600 %r = lshr <2 x i9> <i9 -1, i9 -1>, %s
601 %o = or <2 x i9> %l, %r
605 define i8 @shifted_all_ones_sub_on_lshr_commute(i8 %shamt) {
606 ; CHECK-LABEL: @shifted_all_ones_sub_on_lshr_commute(
607 ; CHECK-NEXT: ret i8 -1
609 %l = shl i8 -1, %shamt
610 %s = sub i8 8, %shamt
616 ; negative test - need -1 in general case
618 define i32 @shifted_not_all_ones(i32 %shamt) {
619 ; CHECK-LABEL: @shifted_not_all_ones(
620 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -2, [[SHAMT:%.*]]
621 ; CHECK-NEXT: [[S:%.*]] = sub i32 31, [[SHAMT]]
622 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
623 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
624 ; CHECK-NEXT: ret i32 [[O]]
626 %r = lshr i32 -2, %shamt
627 %s = sub i32 31, %shamt
633 ; negative test - opposite shift amount may be too big
635 define i32 @shifted_all_ones_greater_than_bitwidth(i32 %shamt) {
636 ; CHECK-LABEL: @shifted_all_ones_greater_than_bitwidth(
637 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -1, [[SHAMT:%.*]]
638 ; CHECK-NEXT: [[S:%.*]] = sub i32 33, [[SHAMT]]
639 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
640 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
641 ; CHECK-NEXT: ret i32 [[O]]
643 %r = lshr i32 -1, %shamt
644 %s = sub i32 33, %shamt
650 ; negative test - shift amount must be derived from same base
652 define i32 @shifted_all_ones_not_same_amt(i32 %shamt, i32 %other) {
653 ; CHECK-LABEL: @shifted_all_ones_not_same_amt(
654 ; CHECK-NEXT: [[R:%.*]] = lshr i32 -1, [[SHAMT:%.*]]
655 ; CHECK-NEXT: [[S:%.*]] = sub i32 32, [[OTHER:%.*]]
656 ; CHECK-NEXT: [[L:%.*]] = shl i32 -1, [[S]]
657 ; CHECK-NEXT: [[O:%.*]] = or i32 [[R]], [[L]]
658 ; CHECK-NEXT: ret i32 [[O]]
660 %r = lshr i32 -1, %shamt
661 %s = sub i32 32, %other
667 ; (A & B) | ~(A ^ B) --> ~(A ^ B)
669 define i4 @or_nxor_and_commute0(i4 %a, i4 %b) {
670 ; CHECK-LABEL: @or_nxor_and_commute0(
671 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B:%.*]]
672 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
673 ; CHECK-NEXT: ret i4 [[NOT]]
677 %not = xor i4 %xor, -1
678 %r = or i4 %and, %not
682 define <2 x i4> @or_nxor_and_commute1(<2 x i4> %a, <2 x i4> %b) {
683 ; CHECK-LABEL: @or_nxor_and_commute1(
684 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A:%.*]], [[B:%.*]]
685 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 -1>
686 ; CHECK-NEXT: ret <2 x i4> [[NOT]]
688 %and = and <2 x i4> %a, %b
689 %xor = xor <2 x i4> %a, %b
690 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
691 %r = or <2 x i4> %not, %and
695 define i74 @or_nxor_and_commute2(i74 %a, i74 %b) {
696 ; CHECK-LABEL: @or_nxor_and_commute2(
697 ; CHECK-NEXT: [[XOR:%.*]] = xor i74 [[A:%.*]], [[B:%.*]]
698 ; CHECK-NEXT: [[NOT:%.*]] = xor i74 [[XOR]], -1
699 ; CHECK-NEXT: ret i74 [[NOT]]
701 %and = and i74 %b, %a
702 %xor = xor i74 %a, %b
703 %not = xor i74 %xor, -1
704 %r = or i74 %and, %not
708 define <2 x i4> @or_nxor_and_commute3(<2 x i4> %a, <2 x i4> %b) {
709 ; CHECK-LABEL: @or_nxor_and_commute3(
710 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A:%.*]], [[B:%.*]]
711 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 -1>
712 ; CHECK-NEXT: ret <2 x i4> [[NOT]]
714 %and = and <2 x i4> %b, %a
715 %xor = xor <2 x i4> %a, %b
716 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
717 %r = or <2 x i4> %not, %and
721 ; negative test - must have common operands
723 define i4 @or_nxor_and_wrong_val1(i4 %a, i4 %b, i4 %c) {
724 ; CHECK-LABEL: @or_nxor_and_wrong_val1(
725 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[A:%.*]], [[C:%.*]]
726 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A]], [[B:%.*]]
727 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
728 ; CHECK-NEXT: [[R:%.*]] = or i4 [[AND]], [[NOT]]
729 ; CHECK-NEXT: ret i4 [[R]]
733 %not = xor i4 %xor, -1
734 %r = or i4 %and, %not
738 ; negative test - must have common operands
740 define i4 @or_nxor_and_wrong_val2(i4 %a, i4 %b, i4 %c) {
741 ; CHECK-LABEL: @or_nxor_and_wrong_val2(
742 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[C:%.*]], [[B:%.*]]
743 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B]]
744 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
745 ; CHECK-NEXT: [[R:%.*]] = or i4 [[AND]], [[NOT]]
746 ; CHECK-NEXT: ret i4 [[R]]
750 %not = xor i4 %xor, -1
751 %r = or i4 %and, %not
755 ; negative test - undef in 'not' is allowed
757 define <2 x i4> @or_nxor_and_undef_elt(<2 x i4> %a, <2 x i4> %b) {
758 ; CHECK-LABEL: @or_nxor_and_undef_elt(
759 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[B:%.*]], [[A:%.*]]
760 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[A]], [[B]]
761 ; CHECK-NEXT: [[NOT:%.*]] = xor <2 x i4> [[XOR]], <i4 -1, i4 undef>
762 ; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[NOT]], [[AND]]
763 ; CHECK-NEXT: ret <2 x i4> [[R]]
765 %and = and <2 x i4> %b, %a
766 %xor = xor <2 x i4> %a, %b
767 %not = xor <2 x i4> %xor, <i4 -1, i4 undef>
768 %r = or <2 x i4> %not, %and
772 ; ~(A ^ B) | (A | B) --> -1
774 define i4 @or_nxor_or_commute0(i4 %a, i4 %b) {
775 ; CHECK-LABEL: @or_nxor_or_commute0(
776 ; CHECK-NEXT: ret i4 -1
780 %not = xor i4 %xor, -1
785 define <2 x i4> @or_nxor_or_commute1(<2 x i4> %a, <2 x i4> %b) {
786 ; CHECK-LABEL: @or_nxor_or_commute1(
787 ; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
789 %or = or <2 x i4> %a, %b
790 %xor = xor <2 x i4> %a, %b
791 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
792 %r = or <2 x i4> %or, %not
796 define i74 @or_nxor_or_commute2(i74 %a, i74 %b) {
797 ; CHECK-LABEL: @or_nxor_or_commute2(
798 ; CHECK-NEXT: ret i74 -1
801 %xor = xor i74 %a, %b
802 %not = xor i74 %xor, -1
803 %r = or i74 %not, %or
807 define <2 x i4> @or_nxor_or_commute3(<2 x i4> %a, <2 x i4> %b) {
808 ; CHECK-LABEL: @or_nxor_or_commute3(
809 ; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
811 %or = or <2 x i4> %b, %a
812 %xor = xor <2 x i4> %a, %b
813 %not = xor <2 x i4> %xor, <i4 -1, i4 -1>
814 %r = or <2 x i4> %or, %not
818 ; negative test - must have common operands
820 define i4 @or_nxor_or_wrong_val1(i4 %a, i4 %b, i4 %c) {
821 ; CHECK-LABEL: @or_nxor_or_wrong_val1(
822 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[A:%.*]], [[C:%.*]]
823 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A]], [[B:%.*]]
824 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
825 ; CHECK-NEXT: [[R:%.*]] = or i4 [[NOT]], [[OR]]
826 ; CHECK-NEXT: ret i4 [[R]]
830 %not = xor i4 %xor, -1
835 ; negative test - must have common operands
837 define i4 @or_nxor_or_wrong_val2(i4 %a, i4 %b, i4 %c) {
838 ; CHECK-LABEL: @or_nxor_or_wrong_val2(
839 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[C:%.*]], [[B:%.*]]
840 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[A:%.*]], [[B]]
841 ; CHECK-NEXT: [[NOT:%.*]] = xor i4 [[XOR]], -1
842 ; CHECK-NEXT: [[R:%.*]] = or i4 [[NOT]], [[OR]]
843 ; CHECK-NEXT: ret i4 [[R]]
847 %not = xor i4 %xor, -1
852 ; negative test - undef in 'not' is allowed
854 define <2 x i4> @or_nxor_or_undef_elt(<2 x i4> %a, <2 x i4> %b) {
855 ; CHECK-LABEL: @or_nxor_or_undef_elt(
856 ; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
858 %or = or <2 x i4> %b, %a
859 %xor = xor <2 x i4> %a, %b
860 %not = xor <2 x i4> %xor, <i4 -1, i4 undef>
861 %r = or <2 x i4> %or, %not
865 ; (A ^ B) | (~A | B) --> -1
867 define i4 @or_xor_not_op_or(i4 %a, i4 %b){
868 ; CHECK-LABEL: @or_xor_not_op_or(
869 ; CHECK-NEXT: ret i4 -1
872 %nota = xor i4 %a, -1
873 %or = or i4 %nota, %b
878 ; (A ^ B) | (B | ~A) --> -1
880 define i71 @or_xor_not_op_or_commute1(i71 %a, i71 %b){
881 ; CHECK-LABEL: @or_xor_not_op_or_commute1(
882 ; CHECK-NEXT: ret i71 -1
884 %xor = xor i71 %a, %b
885 %nota = xor i71 %a, -1
886 %or = or i71 %b, %nota
887 %r = or i71 %xor, %or
891 ; (B ^ A) | (~A | B) --> -1
893 define i32 @or_xor_not_op_or_commute2(i32 %a, i32 %b){
894 ; CHECK-LABEL: @or_xor_not_op_or_commute2(
895 ; CHECK-NEXT: ret i32 -1
897 %xor = xor i32 %b, %a
898 %nota = xor i32 %a, -1
899 %or = or i32 %nota, %b
900 %r = or i32 %xor, %or
904 ; (B ^ A) | (B | ~A) --> -1
906 define i32 @or_xor_not_op_or_commute3(i32 %a, i32 %b){
907 ; CHECK-LABEL: @or_xor_not_op_or_commute3(
908 ; CHECK-NEXT: ret i32 -1
910 %xor = xor i32 %b, %a
911 %nota = xor i32 %a, -1
912 %or = or i32 %b, %nota
913 %r = or i32 %xor, %or
917 ; (~A | B) | (A ^ B) --> -1
919 define i32 @or_xor_not_op_or_commute4(i32 %a, i32 %b){
920 ; CHECK-LABEL: @or_xor_not_op_or_commute4(
921 ; CHECK-NEXT: ret i32 -1
923 %xor = xor i32 %a, %b
924 %nota = xor i32 %a, -1
925 %or = or i32 %nota, %b
926 %r = or i32 %or, %xor
930 ; (B | ~A) | (A ^ B) --> -1
932 define i32 @or_xor_not_op_or_commute5(i32 %a, i32 %b){
933 ; CHECK-LABEL: @or_xor_not_op_or_commute5(
934 ; CHECK-NEXT: ret i32 -1
936 %xor = xor i32 %a, %b
937 %nota = xor i32 %a, -1
938 %or = or i32 %b, %nota
939 %r = or i32 %or, %xor
943 ; (~A | B) | (B ^ A) --> -1
945 define i32 @or_xor_not_op_or_commute6(i32 %a, i32 %b){
946 ; CHECK-LABEL: @or_xor_not_op_or_commute6(
947 ; CHECK-NEXT: ret i32 -1
949 %xor = xor i32 %b, %a
950 %nota = xor i32 %a, -1
951 %or = or i32 %nota, %b
952 %r = or i32 %or, %xor
956 ; (B | ~A) | (B ^ A) --> -1
958 define i32 @or_xor_not_op_or_commute7(i32 %a, i32 %b){
959 ; CHECK-LABEL: @or_xor_not_op_or_commute7(
960 ; CHECK-NEXT: ret i32 -1
962 %xor = xor i32 %b, %a
963 %nota = xor i32 %a, -1
964 %or = or i32 %b, %nota
965 %r = or i32 %or, %xor
969 define <2 x i4> @or_xor_not_op_or_undef_elt(<2 x i4> %a, <2 x i4> %b) {
970 ; CHECK-LABEL: @or_xor_not_op_or_undef_elt(
971 ; CHECK-NEXT: ret <2 x i4> <i4 -1, i4 -1>
973 %xor = xor <2 x i4> %a, %b
974 %nota = xor <2 x i4> %a, <i4 -1, i4 undef>
975 %or = or <2 x i4> %nota, %b
976 %r = or <2 x i4> %xor, %or
982 define i16 @or_xor_not_op_or_wrong_val(i16 %a, i16 %b, i16 %c) {
983 ; CHECK-LABEL: @or_xor_not_op_or_wrong_val(
984 ; CHECK-NEXT: [[XOR:%.*]] = xor i16 [[A:%.*]], [[C:%.*]]
985 ; CHECK-NEXT: [[NOTA:%.*]] = xor i16 [[A]], -1
986 ; CHECK-NEXT: [[OR:%.*]] = or i16 [[NOTA]], [[B:%.*]]
987 ; CHECK-NEXT: [[R:%.*]] = or i16 [[XOR]], [[OR]]
988 ; CHECK-NEXT: ret i16 [[R]]
990 %xor = xor i16 %a, %c
991 %nota = xor i16 %a, -1
992 %or = or i16 %nota, %b
993 %r = or i16 %xor, %or
997 ; ~(x & y) | (x ^ y) --> ~(x & y)
999 define i4 @or_nand_xor(i4 %x, i4 %y) {
1000 ; CHECK-LABEL: @or_nand_xor(
1001 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[X:%.*]], [[Y:%.*]]
1002 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1003 ; CHECK-NEXT: ret i4 [[NAND]]
1005 %and = and i4 %x, %y
1006 %xor = xor i4 %x, %y
1007 %nand = xor i4 %and, -1
1008 %or = or i4 %xor, %nand
1012 define <2 x i4> @or_nand_xor_commute1(<2 x i4> %x, <2 x i4> %y) {
1013 ; CHECK-LABEL: @or_nand_xor_commute1(
1014 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Y:%.*]], [[X:%.*]]
1015 ; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], <i4 -1, i4 -1>
1016 ; CHECK-NEXT: ret <2 x i4> [[NAND]]
1018 %and = and <2 x i4> %y, %x
1019 %xor = xor <2 x i4> %x, %y
1020 %nand = xor <2 x i4> %and, <i4 -1, i4 -1>
1021 %or = or <2 x i4> %xor, %nand
1025 define i71 @or_nand_xor_commute2(i71 %x, i71 %y) {
1026 ; CHECK-LABEL: @or_nand_xor_commute2(
1027 ; CHECK-NEXT: [[AND:%.*]] = and i71 [[X:%.*]], [[Y:%.*]]
1028 ; CHECK-NEXT: [[NAND:%.*]] = xor i71 [[AND]], -1
1029 ; CHECK-NEXT: ret i71 [[NAND]]
1031 %and = and i71 %x, %y
1032 %xor = xor i71 %x, %y
1033 %nand = xor i71 %and, -1
1034 %or = or i71 %nand, %xor
1038 define i4 @or_nand_xor_commute3(i4 %x, i4 %y) {
1039 ; CHECK-LABEL: @or_nand_xor_commute3(
1040 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[Y:%.*]], [[X:%.*]]
1041 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1042 ; CHECK-NEXT: ret i4 [[NAND]]
1044 %and = and i4 %y, %x
1045 %xor = xor i4 %x, %y
1046 %nand = xor i4 %and, -1
1047 %or = or i4 %nand, %xor
1051 ; negative test wrong operand
1053 define i4 @or_nand_xor_wrong_val(i4 %x, i4 %y, i4 %z) {
1054 ; CHECK-LABEL: @or_nand_xor_wrong_val(
1055 ; CHECK-NEXT: [[AND:%.*]] = and i4 [[X:%.*]], [[Y:%.*]]
1056 ; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[X]], [[Z:%.*]]
1057 ; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
1058 ; CHECK-NEXT: [[OR:%.*]] = or i4 [[XOR]], [[NAND]]
1059 ; CHECK-NEXT: ret i4 [[OR]]
1061 %and = and i4 %x, %y
1062 %xor = xor i4 %x, %z
1063 %nand = xor i4 %and, -1
1064 %or = or i4 %xor, %nand
1068 ; negative test - undef element in 'not' is not allowed
1070 define <2 x i4> @or_nand_xor_undef_elt(<2 x i4> %x, <2 x i4> %y) {
1071 ; CHECK-LABEL: @or_nand_xor_undef_elt(
1072 ; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Y:%.*]], [[X:%.*]]
1073 ; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[X]], [[Y]]
1074 ; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], <i4 undef, i4 -1>
1075 ; CHECK-NEXT: [[OR:%.*]] = or <2 x i4> [[XOR]], [[NAND]]
1076 ; CHECK-NEXT: ret <2 x i4> [[OR]]
1078 %and = and <2 x i4> %y, %x
1079 %xor = xor <2 x i4> %x, %y
1080 %nand = xor <2 x i4> %and, <i4 undef, i4 -1>
1081 %or = or <2 x i4> %xor, %nand
1085 declare i32 @llvm.fshl.i32 (i32, i32, i32)
1086 declare i32 @llvm.fshr.i32 (i32, i32, i32)
1088 define i32 @or_shl_fshl(i32 %x, i32 %y, i32 %s) {
1089 ; CHECK-LABEL: @or_shl_fshl(
1090 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[Y:%.*]], i32 [[X:%.*]], i32 [[S:%.*]])
1091 ; CHECK-NEXT: ret i32 [[FUN]]
1093 %shy = shl i32 %y, %s
1094 %fun = call i32 @llvm.fshl.i32(i32 %y, i32 %x, i32 %s)
1095 %or = or i32 %fun, %shy
1099 define i32 @or_shl_fshl_commute(i32 %x, i32 %y, i32 %s) {
1100 ; CHECK-LABEL: @or_shl_fshl_commute(
1101 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[Y:%.*]], i32 [[X:%.*]], i32 [[S:%.*]])
1102 ; CHECK-NEXT: ret i32 [[FUN]]
1104 %shy = shl i32 %y, %s
1105 %fun = call i32 @llvm.fshl.i32(i32 %y, i32 %x, i32 %s)
1106 %or = or i32 %shy, %fun
1110 ; negative test - fshl operands are not commutative
1112 define i32 @or_shl_fshl_wrong_order(i32 %x, i32 %y, i32 %s) {
1113 ; CHECK-LABEL: @or_shl_fshl_wrong_order(
1114 ; CHECK-NEXT: [[SHY:%.*]] = shl i32 [[Y:%.*]], [[S:%.*]]
1115 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshl.i32(i32 [[X:%.*]], i32 [[Y]], i32 [[S]])
1116 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[FUN]], [[SHY]]
1117 ; CHECK-NEXT: ret i32 [[OR]]
1119 %shy = shl i32 %y, %s
1120 %fun = call i32 @llvm.fshl.i32(i32 %x, i32 %y, i32 %s)
1121 %or = or i32 %fun, %shy
1125 define i32 @or_lshr_fshr(i32 %x, i32 %y, i32 %s) {
1126 ; CHECK-LABEL: @or_lshr_fshr(
1127 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[S:%.*]])
1128 ; CHECK-NEXT: ret i32 [[FUN]]
1130 %shy = lshr i32 %y, %s
1131 %fun = call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %s)
1132 %or = or i32 %fun, %shy
1136 define i32 @or_lshr_fshr_commute(i32 %x, i32 %y, i32 %s) {
1137 ; CHECK-LABEL: @or_lshr_fshr_commute(
1138 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[S:%.*]])
1139 ; CHECK-NEXT: ret i32 [[FUN]]
1141 %shy = lshr i32 %y, %s
1142 %fun = call i32 @llvm.fshr.i32(i32 %x, i32 %y, i32 %s)
1143 %or = or i32 %shy, %fun
1147 ; negative test - fshr operands are not commutative
1149 define i32 @or_lshr_fshr_wrong_order(i32 %x, i32 %y, i32 %s) {
1150 ; CHECK-LABEL: @or_lshr_fshr_wrong_order(
1151 ; CHECK-NEXT: [[SHY:%.*]] = lshr i32 [[Y:%.*]], [[S:%.*]]
1152 ; CHECK-NEXT: [[FUN:%.*]] = call i32 @llvm.fshr.i32(i32 [[Y]], i32 [[X:%.*]], i32 [[S]])
1153 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[FUN]], [[SHY]]
1154 ; CHECK-NEXT: ret i32 [[OR]]
1156 %shy = lshr i32 %y, %s
1157 %fun = call i32 @llvm.fshr.i32(i32 %y, i32 %x, i32 %s)
1158 %or = or i32 %fun, %shy