[llvm] 5c759ed - [InstCombine] reduce another or-xor bitwise logic pattern
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 3 06:33:50 PDT 2022
Author: Sanjay Patel
Date: 2022-09-03T09:32:08-04:00
New Revision: 5c759edc57d61bf4713f7e9dbb490c7e0328136d
URL: https://github.com/llvm/llvm-project/commit/5c759edc57d61bf4713f7e9dbb490c7e0328136d
DIFF: https://github.com/llvm/llvm-project/commit/5c759edc57d61bf4713f7e9dbb490c7e0328136d.diff
LOG: [InstCombine] reduce another or-xor bitwise logic pattern
~(A & ?) | (A ^ B) --> ~((A & ?) & B)
https://alive2.llvm.org/ce/z/mxex6V
This is similar to 9d218b61cc50 where we peeked through
another logic op to find a common operand.
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 0fd3bdc1ef662..ac6780b51864f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3012,6 +3012,19 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
(match(Op0, m_Not(m_Specific(A))) || match(Op0, m_Not(m_Specific(B)))))
return BinaryOperator::CreateNot(Builder.CreateAnd(A, B));
+ // Same as above, but peek through an 'and' to the common operand:
+ // ~(A & ?) | (A ^ B) --> ~((A & ?) & B)
+ // ~(B & ?) | (A ^ B) --> ~((B & ?) & A)
+ Instruction *And;
+ if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
+ match(Op0, m_Not(m_CombineAnd(m_Instruction(And),
+ m_c_And(m_Specific(A), m_Value())))))
+ return BinaryOperator::CreateNot(Builder.CreateAnd(And, B));
+ if ((Op0->hasOneUse() || Op1->hasOneUse()) &&
+ match(Op0, m_Not(m_CombineAnd(m_Instruction(And),
+ m_c_And(m_Specific(B), m_Value())))))
+ return BinaryOperator::CreateNot(Builder.CreateAnd(And, A));
+
// (~A | C) | (A ^ B) --> ~(A & B) | C
// (~B | C) | (A ^ B) --> ~(A & B) | C
if (Op0->hasOneUse() && Op1->hasOneUse() &&
diff --git a/llvm/test/Transforms/InstCombine/or-xor.ll b/llvm/test/Transforms/InstCombine/or-xor.ll
index bc6e07a568672..7f2fe3f4dc7a2 100644
--- a/llvm/test/Transforms/InstCombine/or-xor.ll
+++ b/llvm/test/Transforms/InstCombine/or-xor.ll
@@ -960,9 +960,8 @@ define i8 @or_not_xor_common_op_use2(i8 %x, i8 %y, i8 %z) {
define i4 @or_nand_xor_common_op_commute0(i4 %x, i4 %y, i4 %z) {
; CHECK-LABEL: @or_nand_xor_common_op_commute0(
; CHECK-NEXT: [[AND:%.*]] = and i4 [[X:%.*]], [[Z:%.*]]
-; CHECK-NEXT: [[NAND:%.*]] = xor i4 [[AND]], -1
-; CHECK-NEXT: [[XOR:%.*]] = xor i4 [[X]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = or i4 [[XOR]], [[NAND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i4 [[AND]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = xor i4 [[TMP1]], -1
; CHECK-NEXT: ret i4 [[R]]
;
%and = and i4 %x, %z
@@ -975,9 +974,8 @@ define i4 @or_nand_xor_common_op_commute0(i4 %x, i4 %y, i4 %z) {
define <2 x i4> @or_nand_xor_common_op_commute1(<2 x i4> %x, <2 x i4> %y, <2 x i4> %z) {
; CHECK-LABEL: @or_nand_xor_common_op_commute1(
; CHECK-NEXT: [[AND:%.*]] = and <2 x i4> [[Z:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[NAND:%.*]] = xor <2 x i4> [[AND]], <i4 poison, i4 -1>
-; CHECK-NEXT: [[XOR:%.*]] = xor <2 x i4> [[X]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = or <2 x i4> [[XOR]], [[NAND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and <2 x i4> [[AND]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = xor <2 x i4> [[TMP1]], <i4 -1, i4 -1>
; CHECK-NEXT: ret <2 x i4> [[R]]
;
%and = and <2 x i4> %z, %x
@@ -991,9 +989,8 @@ define i8 @or_nand_xor_common_op_commute2(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @or_nand_xor_common_op_commute2(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[X:%.*]], [[Z:%.*]]
; CHECK-NEXT: call void @use(i8 [[AND]])
-; CHECK-NEXT: [[NAND:%.*]] = xor i8 [[AND]], -1
-; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[Y:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = or i8 [[XOR]], [[NAND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[AND]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[TMP1]], -1
; CHECK-NEXT: ret i8 [[R]]
;
%and = and i8 %x, %z
@@ -1009,8 +1006,8 @@ define i8 @or_nand_xor_common_op_commute3(i8 %x, i8 %y, i8 %z) {
; CHECK-NEXT: [[AND:%.*]] = and i8 [[Z:%.*]], [[X:%.*]]
; CHECK-NEXT: [[NAND:%.*]] = xor i8 [[AND]], -1
; CHECK-NEXT: call void @use(i8 [[NAND]])
-; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[Y:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = or i8 [[XOR]], [[NAND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[AND]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[TMP1]], -1
; CHECK-NEXT: ret i8 [[R]]
;
%and = and i8 %z, %x
@@ -1024,10 +1021,10 @@ define i8 @or_nand_xor_common_op_commute3(i8 %x, i8 %y, i8 %z) {
define i8 @or_nand_xor_common_op_commute3_use2(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @or_nand_xor_common_op_commute3_use2(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[Z:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[NAND:%.*]] = xor i8 [[AND]], -1
; CHECK-NEXT: [[XOR:%.*]] = xor i8 [[Y:%.*]], [[X]]
; CHECK-NEXT: call void @use(i8 [[XOR]])
-; CHECK-NEXT: [[R:%.*]] = or i8 [[XOR]], [[NAND]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[AND]], [[Y]]
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[TMP1]], -1
; CHECK-NEXT: ret i8 [[R]]
;
%and = and i8 %z, %x
@@ -1038,6 +1035,8 @@ define i8 @or_nand_xor_common_op_commute3_use2(i8 %x, i8 %y, i8 %z) {
ret i8 %r
}
+; negative test - too many extra uses
+
define i8 @or_nand_xor_common_op_commute3_use3(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @or_nand_xor_common_op_commute3_use3(
; CHECK-NEXT: [[AND:%.*]] = and i8 [[Z:%.*]], [[X:%.*]]
More information about the llvm-commits
mailing list