[PATCH] D124710: [InstCombine] Fold ((A&B)^C)|B

Alexander Shaposhnikov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 29 18:55:43 PDT 2022


alexander-shaposhnikov created this revision.
alexander-shaposhnikov added reviewers: fhahn, nikic, craig.topper.
alexander-shaposhnikov created this object with visibility "All Users".
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
alexander-shaposhnikov requested review of this revision.
Herald added a project: LLVM.

Fold ((A&B)^C)|B into C | B.

https://alive2.llvm.org/ce/z/zSGSor

(https://github.com/llvm/llvm-project/issues/55169)

Test plan: ninja check-llvm


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D124710

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


Index: llvm/test/Transforms/InstCombine/and-xor-or.ll
===================================================================
--- llvm/test/Transforms/InstCombine/and-xor-or.ll
+++ llvm/test/Transforms/InstCombine/and-xor-or.ll
@@ -172,10 +172,8 @@
 
 define i64 @and_xor_or1(i64 %x, i64 %y, i64 %z) {
 ; CHECK-LABEL: @and_xor_or1(
-; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = xor i64 [[TMP1]], [[Z:%.*]]
-; CHECK-NEXT:    [[TMP3:%.*]] = or i64 [[TMP2]], [[Y]]
-; CHECK-NEXT:    ret i64 [[TMP3]]
+; CHECK-NEXT:    [[TMP1:%.*]] = or i64 [[Z:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i64 [[TMP1]]
 ;
   %1 = and i64 %x, %y
   %2 = xor i64 %1, %z
@@ -187,10 +185,8 @@
 
 define i64 @and_xor_or2(i64 %x, i64 %y, i64 %z) {
 ; CHECK-LABEL: @and_xor_or2(
-; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    [[TMP2:%.*]] = xor i64 [[TMP1]], [[Z:%.*]]
-; CHECK-NEXT:    [[TMP3:%.*]] = or i64 [[TMP2]], [[Y]]
-; CHECK-NEXT:    ret i64 [[TMP3]]
+; CHECK-NEXT:    [[TMP1:%.*]] = or i64 [[Z:%.*]], [[Y:%.*]]
+; CHECK-NEXT:    ret i64 [[TMP1]]
 ;
   %1 = and i64 %x, %y
   %2 = xor i64 %1, %z
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2688,6 +2688,16 @@
     if (match(Op1, m_Xor(m_Specific(B), m_Specific(A))))
       return BinaryOperator::CreateOr(Op1, C);
 
+  // ((A & B) ^ C) | B -> C | B
+  if (match(Op0, m_Xor(m_And(m_Value(A), m_Value(B)), m_Value(C))))
+    if (match(Op1, m_Specific(B)))
+      return BinaryOperator::CreateOr(C, B);
+
+  // B | ((A & B) ^ C) -> B | C
+  if (match(Op1, m_Xor(m_And(m_Value(A), m_Value(B)), m_Value(C))))
+    if (match(Op0, m_Specific(B)))
+      return BinaryOperator::CreateOr(B, C);
+
   // ((B | C) & A) | B -> B | (A & C)
   if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
     return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124710.426203.patch
Type: text/x-patch
Size: 2084 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220430/57f0e2d1/attachment.bin>


More information about the llvm-commits mailing list