[llvm] 5c6b9e1 - [InstCombine] (~(a | b) & c) | ~(c | (a ^ b)) -> ~((a | b) & (c | (b ^ a)))
Stanislav Mekhanoshin via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 29 11:20:42 PST 2021
Author: Stanislav Mekhanoshin
Date: 2021-11-29T11:20:34-08:00
New Revision: 5c6b9e1622b10a543ea4210996d2732a6e5183da
URL: https://github.com/llvm/llvm-project/commit/5c6b9e1622b10a543ea4210996d2732a6e5183da
DIFF: https://github.com/llvm/llvm-project/commit/5c6b9e1622b10a543ea4210996d2732a6e5183da.diff
LOG: [InstCombine] (~(a | b) & c) | ~(c | (a ^ b)) -> ~((a | b) & (c | (b ^ a)))
```
----------------------------------------
define i3 @src(i3 %a, i3 %b, i3 %c) {
%0:
%or1 = or i3 %b, %c
%not1 = xor i3 %or1, 7
%and1 = and i3 %a, %not1
%xor1 = xor i3 %b, %c
%or2 = or i3 %xor1, %a
%not2 = xor i3 %or2, 7
%or3 = or i3 %and1, %not2
ret i3 %or3
}
=>
define i3 @tgt(i3 %a, i3 %b, i3 %c) {
%0:
%obc = or i3 %b, %c
%xbc = xor i3 %b, %c
%o = or i3 %a, %xbc
%and = and i3 %obc, %o
%r = xor i3 %and, 7
ret i3 %r
}
Transformation seems to be correct!
```
Differential Revision: https://reviews.llvm.org/D112955
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/test/Transforms/InstCombine/and-xor-or.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 15f1d8790a7fe..6d3b228e7d7f3 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -1727,16 +1727,18 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
(Opcode == Instruction::And) ? Instruction::Or : Instruction::And;
Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
- Value *A, *B, *C;
+ Value *A, *B, *C, *X, *Y;
// (~(A | B) & C) | ... --> ...
// (~(A & B) | C) & ... --> ...
// TODO: One use checks are conservative. We just need to check that a total
// number of multiple used values does not exceed reduction
// in operations.
- if (match(Op0, m_c_BinOp(FlippedOpcode,
- m_Not(m_BinOp(Opcode, m_Value(A), m_Value(B))),
- m_Value(C)))) {
+ if (match(Op0,
+ m_c_BinOp(FlippedOpcode,
+ m_CombineAnd(m_Value(X), m_Not(m_BinOp(Opcode, m_Value(A),
+ m_Value(B)))),
+ m_Value(C)))) {
// (~(A | B) & C) | (~(A | C) & B) --> (B ^ C) & ~A
// (~(A & B) | C) & (~(A & C) | B) --> ~((B ^ C) & A)
if (match(Op1,
@@ -1776,6 +1778,21 @@ static Instruction *foldComplexAndOrPatterns(BinaryOperator &I,
m_c_BinOp(Opcode, m_Specific(B), m_Specific(C)))))))
return BinaryOperator::CreateNot(Builder.CreateBinOp(
Opcode, Builder.CreateBinOp(FlippedOpcode, A, C), B));
+
+ // (~(A | B) & C) | ~(C | (A ^ B)) --> ~((A | B) & (C | (A ^ B)))
+ // Note, the pattern with swapped and/or is not handled because the
+ // result is more undefined than a source:
+ // (~(A & B) | C) & ~(C & (A ^ B)) --> (A ^ B ^ C) | ~(A | C) is invalid.
+ if (Opcode == Instruction::Or && Op0->hasOneUse() &&
+ match(Op1, m_OneUse(m_Not(m_CombineAnd(
+ m_Value(Y),
+ m_c_BinOp(Opcode, m_Specific(C),
+ m_c_Xor(m_Specific(A), m_Specific(B)))))))) {
+ // X = ~(A | B)
+ // Y = (C | (A ^ B)
+ Value *Or = cast<BinaryOperator>(X)->getOperand(0);
+ return BinaryOperator::CreateNot(Builder.CreateAnd(Or, Y));
+ }
}
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/and-xor-or.ll b/llvm/test/Transforms/InstCombine/and-xor-or.ll
index 2270fb10881dc..a07153835d895 100644
--- a/llvm/test/Transforms/InstCombine/and-xor-or.ll
+++ b/llvm/test/Transforms/InstCombine/and-xor-or.ll
@@ -2023,12 +2023,10 @@ define i32 @and_or_not_not_wrong_b(i32 %a, i32 %b, i32 %c, i32 %d) {
define i32 @and_not_or_or_not_or_xor(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %b, %c
@@ -2044,12 +2042,10 @@ define i32 @and_not_or_or_not_or_xor(i32 %a, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_commute1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_commute1(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[C:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %c, %b
@@ -2066,12 +2062,10 @@ define i32 @and_not_or_or_not_or_xor_commute2(i32 %a0, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_commute2(
; CHECK-NEXT: [[A:%.*]] = sdiv i32 42, [[A0:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A]], [[NOT1]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%a = sdiv i32 42, %a0 ; thwart complexity-based canonicalization
@@ -2088,12 +2082,10 @@ define i32 @and_not_or_or_not_or_xor_commute2(i32 %a0, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_commute3(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_commute3(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[C]], [[B]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %b, %c
@@ -2110,12 +2102,10 @@ define i32 @and_not_or_or_not_or_xor_commute4(i32 %a0, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_commute4(
; CHECK-NEXT: [[A:%.*]] = sdiv i32 42, [[A0:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A]], [[NOT1]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[A]], [[XOR1]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%a = sdiv i32 42, %a0 ; thwart complexity-based canonicalization
@@ -2132,12 +2122,10 @@ define i32 @and_not_or_or_not_or_xor_commute4(i32 %a0, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_commute5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_commute5(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: ret i32 [[OR3]]
;
%or1 = or i32 %b, %c
@@ -2153,12 +2141,10 @@ define i32 @and_not_or_or_not_or_xor_commute5(i32 %a, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_use1(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_use1(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: call void @use(i32 [[OR1]])
; CHECK-NEXT: ret i32 [[OR3]]
;
@@ -2177,11 +2163,10 @@ define i32 @and_not_or_or_not_or_xor_use2(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_use2(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: call void @use(i32 [[NOT1]])
; CHECK-NEXT: ret i32 [[OR3]]
;
@@ -2222,12 +2207,10 @@ define i32 @and_not_or_or_not_or_xor_use3(i32 %a, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_use4(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_use4(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: call void @use(i32 [[XOR1]])
; CHECK-NEXT: ret i32 [[OR3]]
;
@@ -2245,12 +2228,10 @@ define i32 @and_not_or_or_not_or_xor_use4(i32 %a, i32 %b, i32 %c) {
define i32 @and_not_or_or_not_or_xor_use5(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @and_not_or_or_not_or_xor_use5(
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[B:%.*]], [[C:%.*]]
-; CHECK-NEXT: [[NOT1:%.*]] = xor i32 [[OR1]], -1
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[NOT1]], [[A:%.*]]
; CHECK-NEXT: [[XOR1:%.*]] = xor i32 [[B]], [[C]]
-; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A]]
-; CHECK-NEXT: [[NOT2:%.*]] = xor i32 [[OR2]], -1
-; CHECK-NEXT: [[OR3:%.*]] = or i32 [[AND1]], [[NOT2]]
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[XOR1]], [[A:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[OR1]], [[OR2]]
+; CHECK-NEXT: [[OR3:%.*]] = xor i32 [[TMP1]], -1
; CHECK-NEXT: call void @use(i32 [[OR2]])
; CHECK-NEXT: ret i32 [[OR3]]
;
@@ -2289,6 +2270,8 @@ define i32 @and_not_or_or_not_or_xor_use6(i32 %a, i32 %b, i32 %c) {
}
; (a | ~(b & c)) & ~(a & (b ^ c)) --> ~(a | b) | (a ^ b ^ c)
+; This pattern is not handled because the result is more undefined than a source.
+; It is invalid as is, but feezing %a and %b will make it valid.
define i32 @or_not_and_and_not_and_xor(i32 %a, i32 %b, i32 %c) {
; CHECK-LABEL: @or_not_and_and_not_and_xor(
More information about the llvm-commits
mailing list