[llvm] 9d218b6 - [InstCombine] reduce or-xor-or patterns

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 13 06:59:25 PDT 2022


Author: Sanjay Patel
Date: 2022-08-13T09:52:01-04:00
New Revision: 9d218b61cc50256b0cd32ba4ccf9009215854762

URL: https://github.com/llvm/llvm-project/commit/9d218b61cc50256b0cd32ba4ccf9009215854762
DIFF: https://github.com/llvm/llvm-project/commit/9d218b61cc50256b0cd32ba4ccf9009215854762.diff

LOG: [InstCombine] reduce or-xor-or patterns

(A | ?) | (A ^ B) --> (A | ?) | B
https://alive2.llvm.org/ce/z/dbNQw4

This extends the existing transform to peek through
another 'or' instruction for the common operand.

This is the underlying missing fold that should allow
issue #56711 and issue #57120 to reduce even more.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
    llvm/test/Transforms/InstCombine/or-xor.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 843b4a4d8162..3c8414c8398f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2914,6 +2914,13 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
     if (Op0 == A || Op0 == B)
       return BinaryOperator::CreateOr(A, B);
 
+    // (A | ?) | (A ^ B) --> (A | ?) | B
+    // (B | ?) | (A ^ B) --> (B | ?) | A
+    if (match(Op0, m_c_Or(m_Specific(A), m_Value())))
+      return BinaryOperator::CreateOr(Op0, B);
+    if (match(Op0, m_c_Or(m_Specific(B), m_Value())))
+      return BinaryOperator::CreateOr(Op0, A);
+
     // (A & B) | (A ^ B) --> A | B
     // (B & A) | (A ^ B) --> A | B
     if (match(Op0, m_And(m_Specific(A), m_Specific(B))) ||

diff  --git a/llvm/test/Transforms/InstCombine/or-xor.ll b/llvm/test/Transforms/InstCombine/or-xor.ll
index a98775646aa6..f0e7f0f5245c 100644
--- a/llvm/test/Transforms/InstCombine/or-xor.ll
+++ b/llvm/test/Transforms/InstCombine/or-xor.ll
@@ -639,8 +639,7 @@ define i32 @PR45977_f2(i32 %a, i32 %b) {
 define i8 @or_xor_common_op_commute0(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute0(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[XOR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %x, %y
@@ -653,8 +652,7 @@ define i8 @or_xor_common_op_commute1(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute1(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
 ; CHECK-NEXT:    call void @use(i8 [[OR]])
-; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[XOR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %y, %x
@@ -669,7 +667,7 @@ define i8 @or_xor_common_op_commute2(i8 %x, i8 %y, i8 %z) {
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
 ; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[Z:%.*]], [[X]]
 ; CHECK-NEXT:    call void @use(i8 [[XOR]])
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[XOR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %x, %y
@@ -685,7 +683,7 @@ define i8 @or_xor_common_op_commute3(i8 %x, i8 %y, i8 %z) {
 ; CHECK-NEXT:    call void @use(i8 [[OR]])
 ; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[Z:%.*]], [[X]]
 ; CHECK-NEXT:    call void @use(i8 [[XOR]])
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[XOR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %y, %x
@@ -699,8 +697,7 @@ define i8 @or_xor_common_op_commute3(i8 %x, i8 %y, i8 %z) {
 define <2 x i8> @or_xor_common_op_commute4(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute4(
 ; CHECK-NEXT:    [[OR:%.*]] = or <2 x i8> [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor <2 x i8> [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = or <2 x i8> [[XOR]], [[OR]]
+; CHECK-NEXT:    [[R:%.*]] = or <2 x i8> [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret <2 x i8> [[R]]
 ;
   %or = or <2 x i8> %x, %y
@@ -712,8 +709,7 @@ define <2 x i8> @or_xor_common_op_commute4(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z
 define i8 @or_xor_common_op_commute5(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute5(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[X]], [[Z:%.*]]
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %y, %x
@@ -725,8 +721,7 @@ define i8 @or_xor_common_op_commute5(i8 %x, i8 %y, i8 %z) {
 define i8 @or_xor_common_op_commute6(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute6(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[Z:%.*]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %x, %y
@@ -738,8 +733,7 @@ define i8 @or_xor_common_op_commute6(i8 %x, i8 %y, i8 %z) {
 define i8 @or_xor_common_op_commute7(i8 %x, i8 %y, i8 %z) {
 ; CHECK-LABEL: @or_xor_common_op_commute7(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    [[XOR:%.*]] = xor i8 [[Z:%.*]], [[X]]
-; CHECK-NEXT:    [[R:%.*]] = or i8 [[XOR]], [[OR]]
+; CHECK-NEXT:    [[R:%.*]] = or i8 [[OR]], [[Z:%.*]]
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
   %or = or i8 %y, %x
@@ -748,6 +742,8 @@ define i8 @or_xor_common_op_commute7(i8 %x, i8 %y, i8 %z) {
   ret i8 %r
 }
 
+; negative test - need common operand
+
 define i8 @or_xor_notcommon_op(i8 %x, i8 %y, i8 %z, i8 %q) {
 ; CHECK-LABEL: @or_xor_notcommon_op(
 ; CHECK-NEXT:    [[OR:%.*]] = or i8 [[X:%.*]], [[Y:%.*]]


        


More information about the llvm-commits mailing list