[llvm] r306027 - [InstCombine] Add one use checks to or/and->xnor folding
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 22 09:12:02 PDT 2017
Author: ctopper
Date: Thu Jun 22 11:12:02 2017
New Revision: 306027
URL: http://llvm.org/viewvc/llvm-project?rev=306027&view=rev
Log:
[InstCombine] Add one use checks to or/and->xnor folding
If the components of the and/or had multiple uses, this transform created an additional instruction.
This patch makes sure we remove one of the components.
Differential Revision: https://reviews.llvm.org/D34498
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
llvm/trunk/test/Transforms/InstCombine/and-or-not.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp?rev=306027&r1=306026&r2=306027&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp Thu Jun 22 11:12:02 2017
@@ -1230,9 +1230,10 @@ static Instruction *foldAndToXor(BinaryO
// (A | ~B) & (B | ~A) --> ~(A ^ B)
// (~B | A) & (~A | B) --> ~(A ^ B)
// (~B | A) & (B | ~A) --> ~(A ^ B)
- if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
- match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B))))
- return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
+ if (Op0->hasOneUse() || Op1->hasOneUse())
+ if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
+ match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B))))
+ return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
return nullptr;
}
@@ -1247,9 +1248,10 @@ static Instruction *foldOrToXor(BinaryOp
// Operand complexity canonicalization guarantees that the 'and' is Op0.
// (A & B) | ~(A | B) --> ~(A ^ B)
// (A & B) | ~(B | A) --> ~(A ^ B)
- if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
- match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
- return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
+ if (Op0->hasOneUse() || Op1->hasOneUse())
+ if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
+ match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
+ return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
// (A & ~B) | (~A & B) --> A ^ B
// (A & ~B) | (B & ~A) --> A ^ B
Modified: llvm/trunk/test/Transforms/InstCombine/and-or-not.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/and-or-not.ll?rev=306027&r1=306026&r2=306027&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/and-or-not.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/and-or-not.ll Thu Jun 22 11:12:02 2017
@@ -527,8 +527,7 @@ define i32 @and_to_nxor_multiuse(float %
; CHECK-NEXT: [[NOTB:%.*]] = xor i32 [[B]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOTA]], [[B]]
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOTB]], [[A]]
-; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], [[A]]
-; CHECK-NEXT: [[AND:%.*]] = xor i32 [[TMP1]], -1
+; CHECK-NEXT: [[AND:%.*]] = and i32 [[OR1]], [[OR2]]
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[OR1]], [[OR2]]
; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL1]], [[AND]]
; CHECK-NEXT: ret i32 [[MUL2]]
@@ -552,8 +551,7 @@ define i32 @or_to_nxor_multiuse(i32 %a,
; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: [[OR:%.*]] = or i32 [[A]], [[B]]
; CHECK-NEXT: [[NOTOR:%.*]] = xor i32 [[OR]], -1
-; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A]], [[B]]
-; CHECK-NEXT: [[OR2:%.*]] = xor i32 [[TMP1]], -1
+; CHECK-NEXT: [[OR2:%.*]] = or i32 [[AND]], [[NOTOR]]
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[AND]], [[NOTOR]]
; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL1]], [[OR2]]
; CHECK-NEXT: ret i32 [[MUL2]]
More information about the llvm-commits
mailing list