[PATCH] D112108: [InstCombine] Fold `(a & ~b) & ~c` to `a & ~(b | c)`

Stanislav Mekhanoshin via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 19 16:46:25 PDT 2021


rampitec updated this revision to Diff 380823.
rampitec marked an inline comment as done.
rampitec added a comment.

Added vector test.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112108/new/

https://reviews.llvm.org/D112108

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
@@ -516,3 +516,65 @@
   %use2 = udiv i64 %or2, %use1
   ret i64 %use2
 }
+
+; (a & ~b) & ~c --> a & ~(b | c)
+
+define i32 @not_and_and_not(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: @not_and_and_not(
+; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], -1
+; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[TMP2]], [[A:%.*]]
+; CHECK-NEXT:    ret i32 [[AND2]]
+;
+  %not1 = xor i32 %b, -1
+  %not2 = xor i32 %c, -1
+  %and1 = and i32 %a, %not1
+  %and2 = and i32 %and1, %not2
+  ret i32 %and2
+}
+
+define <4 x i64> @not_and_and_not_4i64(<4 x i64> %a, <4 x i64> %b, <4 x i64> %c) {
+; CHECK-LABEL: @not_and_and_not_4i64(
+; CHECK-NEXT:    [[TMP1:%.*]] = or <4 x i64> [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = xor <4 x i64> [[TMP1]], <i64 -1, i64 -1, i64 -1, i64 -1>
+; CHECK-NEXT:    [[AND2:%.*]] = and <4 x i64> [[TMP2]], [[A:%.*]]
+; CHECK-NEXT:    ret <4 x i64> [[AND2]]
+;
+  %not1 = xor <4 x i64> %b, <i64 -1, i64 -1, i64 -1, i64 -1>
+  %not2 = xor <4 x i64> %c, <i64 -1, i64 -1, i64 -1, i64 -1>
+  %and1 = and <4 x i64> %a, %not1
+  %and2 = and <4 x i64> %and1, %not2
+  ret <4 x i64> %and2
+}
+
+; (~b & a) & ~c --> a & ~(b | c)
+
+define i32 @not_and_and_not_commute1(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: @not_and_and_not_commute1(
+; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], -1
+; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[TMP2]], [[A:%.*]]
+; CHECK-NEXT:    ret i32 [[AND2]]
+;
+  %not1 = xor i32 %b, -1
+  %not2 = xor i32 %c, -1
+  %and1 = and i32 %not1, %a
+  %and2 = and i32 %and1, %not2
+  ret i32 %and2
+}
+
+; ~c & (a & ~b) --> a & ~(b | c)
+
+define i32 @not_and_and_not_commute2(i32 %a, i32 %b, i32 %c) {
+; CHECK-LABEL: @not_and_and_not_commute2(
+; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT:    [[TMP2:%.*]] = xor i32 [[TMP1]], -1
+; CHECK-NEXT:    [[AND2:%.*]] = and i32 [[TMP2]], [[A:%.*]]
+; CHECK-NEXT:    ret i32 [[AND2]]
+;
+  %not1 = xor i32 %b, -1
+  %not2 = xor i32 %c, -1
+  %and1 = and i32 %a, %not1
+  %and2 = and i32 %not2, %and1
+  ret i32 %and2
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2012,6 +2012,22 @@
     if (match(Op0, m_c_Xor(m_Not(m_Value(A)), m_Value(B))) &&
         match(Op1, m_c_Or(m_Specific(A), m_Specific(B))))
       return BinaryOperator::CreateAnd(A, B);
+
+    // (A & ~B) & ~C -> A & ~(B | C)
+    // (~B & A) & ~C -> A & ~(B | C)
+    if (match(Op0, m_c_And(m_Value(A), m_Not(m_Value(B)))))
+      if (match(Op1, m_Not(m_Value(C))))
+        if (Op0->hasOneUse())
+          return BinaryOperator::CreateAnd(
+              A, Builder.CreateNot(Builder.CreateOr(B, C)));
+
+    // ~C & (A & ~B) -> A & ~(B | C)
+    // ~C & (~B & A) -> A & ~(B | C)
+    if (match(Op1, m_c_And(m_Value(A), m_Not(m_Value(B)))))
+      if (match(Op0, m_Not(m_Value(C))))
+        if (Op0->hasOneUse())
+          return BinaryOperator::CreateAnd(
+              A, Builder.CreateNot(Builder.CreateOr(B, C)));
   }
 
   {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112108.380823.patch
Type: text/x-patch
Size: 3464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211019/5574ae1e/attachment-0001.bin>


More information about the llvm-commits mailing list