1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 19:23:23 +01:00

[InstCombine] Transform (A + B) - (A | B) to A & B (PR48604)

define i32 @src(i32 %x, i32 %y) {
%0:
  %a = add i32 %x, %y
  %o = or i32 %x, %y
  %r = sub i32 %a, %o
  ret i32 %r
}
=>
define i32 @tgt(i32 %x, i32 %y) {
%0:
  %b = and i32 %x, %y
  ret i32 %b
}
Transformation seems to be correct!

https://alive2.llvm.org/ce/z/aQRh2j
This commit is contained in:
Dávid Bolvanský 2020-12-31 14:02:44 +01:00
parent f838ba16ef
commit cd22ed8c6e
2 changed files with 12 additions and 12 deletions

View File

@ -1873,6 +1873,14 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
return BinaryOperator::CreateXor(A, B);
}
// (sub (add A, B) (or A, B)) --> (and A, B)
{
Value *A, *B;
if (match(Op0, m_Add(m_Value(A), m_Value(B))) &&
match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
return BinaryOperator::CreateAnd(A, B);
}
// (sub (and A, B) (or A, B)) --> neg (xor A, B)
{
Value *A, *B;

View File

@ -1221,9 +1221,7 @@ define <2 x i8> @flip_masked_bit_nonuniform(<2 x i8> %A) {
define i32 @and_test(i32 %x, i32 %y) {
; CHECK-LABEL: @and_test(
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[O:%.*]] = or i32 [[X]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[A]], [[O]]
; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = add i32 %x, %y
@ -1234,9 +1232,7 @@ define i32 @and_test(i32 %x, i32 %y) {
define i32 @and_test2(i32 %x, i32 %y) {
; CHECK-LABEL: @and_test2(
; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[O:%.*]] = or i32 [[Y]], [[X]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[A]], [[O]]
; CHECK-NEXT: [[R:%.*]] = and i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = add i32 %x, %y
@ -1247,9 +1243,7 @@ define i32 @and_test2(i32 %x, i32 %y) {
define i32 @and_test3(i32 %x, i32 %y) {
; CHECK-LABEL: @and_test3(
; CHECK-NEXT: [[A:%.*]] = add i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[O:%.*]] = or i32 [[X]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = sub i32 [[A]], [[O]]
; CHECK-NEXT: [[R:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret i32 [[R]]
;
%a = add i32 %y, %x
@ -1261,9 +1255,7 @@ define i32 @and_test3(i32 %x, i32 %y) {
define <2 x i8> @and_vec(<2 x i8> %X, <2 x i8> %Y) {
; CHECK-LABEL: @and_vec(
; CHECK-NEXT: [[A:%.*]] = add <2 x i8> [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[O:%.*]] = or <2 x i8> [[X]], [[Y]]
; CHECK-NEXT: [[R:%.*]] = sub <2 x i8> [[A]], [[O]]
; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: ret <2 x i8> [[R]]
;
%a = add <2 x i8> %X, %Y