[PATCH] D112276: [InstCombine] Fold `(c & ~(a | b)) | (b & ~(a | c))` to `~a & (b ^ c)`
Stanislav Mekhanoshin via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 21 15:20:06 PDT 2021
rampitec created this revision.
rampitec added a reviewer: spatel.
Herald added a subscriber: hiraditya.
rampitec requested review of this revision.
Herald added a project: LLVM.
----------------------------------------
define i4 @src(i4 %a, i4 %b, i4 %c) {
%0:
%or1 = or i4 %a, %b
%not1 = xor i4 %or1, 15
%and1 = and i4 %not1, %c
%or2 = or i4 %a, %c
%not2 = xor i4 %or2, 15
%and2 = and i4 %not2, %b
%or3 = or i4 %and1, %and2
ret i4 %or3
}
=>
define i4 @tgt(i4 %a, i4 %b, i4 %c) {
%0:
%xor = xor i4 %b, %c
%not = xor i4 %a, 15
%or3 = and i4 %xor, %not
ret i4 %or3
}
Transformation seems to be correct!
https://reviews.llvm.org/D112276
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
@@ -703,13 +703,9 @@
define i32 @or_not_and(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and(
-; CHECK-NEXT: [[OR1:%.*]] = or i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[C:%.*]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[NOT2]], [[B]]
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[AND2]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[A:%.*]], -1
+; CHECK-NEXT: [[OR3:%.*]] = and i32 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %a, %b
@@ -725,13 +721,9 @@
define i32 @or_not_and_commute1(i32 %a, i32 %b0, i32 %c) {
; CHECK-LABEL: @or_not_and_commute1(
; CHECK-NEXT: [[B:%.*]] = sdiv i32 42, [[B0:%.*]]
-; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[A:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[C:%.*]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[B]], [[NOT2]]
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[AND2]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], [[C:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[A:%.*]], -1
+; CHECK-NEXT: [[OR3:%.*]] = and i32 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i32 [[OR3]]
;
%b = sdiv i32 42, %b0 ; thwart complexity-based canonicalization
@@ -748,13 +740,9 @@
define i32 @or_not_and_commute2(i32 %a, i32 %b0, i32 %c) {
; CHECK-LABEL: @or_not_and_commute2(
; CHECK-NEXT: [[B:%.*]] = sdiv i32 42, [[B0:%.*]]
-; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B]], [[A:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[C:%.*]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[C]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[B]], [[NOT2]]
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND2]], [[AND1]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], [[C:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[A:%.*]], -1
+; CHECK-NEXT: [[OR3:%.*]] = and i32 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i32 [[OR3]]
;
%b = sdiv i32 42, %b0 ; thwart complexity-based canonicalization
@@ -770,13 +758,9 @@
define i32 @or_not_and_commute3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_commute3(
-; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[A:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[C:%.*]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[C]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[AND2:%.*]] = and i32 [[NOT2]], [[B]]
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[AND2]]
+; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B:%.*]], [[C:%.*]]
+; CHECK-NEXT: [[TMP2:%.*]] = xor i32 [[A:%.*]], -1
+; CHECK-NEXT: [[OR3:%.*]] = and i32 [[TMP1]], [[TMP2]]
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %b, %a
Index: llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -2796,6 +2796,22 @@
if (match(Op0, m_And(m_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));
+ // (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
+ if (match(Op0, m_OneUse(m_c_And(m_OneUse(m_Not(m_Or(m_Value(A), m_Value(B)))),
+ m_Value(C))))) {
+ if (match(Op1, m_OneUse(m_c_And(
+ m_OneUse(m_Not(m_c_Or(m_Specific(A), m_Specific(C)))),
+ m_Specific(B)))))
+ return BinaryOperator::CreateAnd(Builder.CreateXor(B, C),
+ Builder.CreateNot(A));
+
+ if (match(Op1, m_OneUse(m_c_And(
+ m_OneUse(m_Not(m_c_Or(m_Specific(B), m_Specific(C)))),
+ m_Specific(A)))))
+ return BinaryOperator::CreateAnd(Builder.CreateXor(A, C),
+ Builder.CreateNot(B));
+ }
+
if (Instruction *DeMorgan = matchDeMorgansLaws(I, Builder))
return DeMorgan;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112276.381410.patch
Type: text/x-patch
Size: 4664 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211021/b44dd40b/attachment-0001.bin>
More information about the llvm-commits
mailing list