Skip to content

Commit 0de5e6a

Browse files
committedJun 22, 2017
[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 llvm-svn: 306027
1 parent 93df2fb commit 0de5e6a

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -1230,9 +1230,10 @@ static Instruction *foldAndToXor(BinaryOperator &I,
12301230
// (A | ~B) & (B | ~A) --> ~(A ^ B)
12311231
// (~B | A) & (~A | B) --> ~(A ^ B)
12321232
// (~B | A) & (B | ~A) --> ~(A ^ B)
1233-
if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
1234-
match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B))))
1235-
return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1233+
if (Op0->hasOneUse() || Op1->hasOneUse())
1234+
if (match(Op0, m_c_Or(m_Value(A), m_Not(m_Value(B)))) &&
1235+
match(Op1, m_c_Or(m_Not(m_Specific(A)), m_Specific(B))))
1236+
return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
12361237

12371238
return nullptr;
12381239
}
@@ -1247,9 +1248,10 @@ static Instruction *foldOrToXor(BinaryOperator &I,
12471248
// Operand complexity canonicalization guarantees that the 'and' is Op0.
12481249
// (A & B) | ~(A | B) --> ~(A ^ B)
12491250
// (A & B) | ~(B | A) --> ~(A ^ B)
1250-
if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1251-
match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1252-
return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
1251+
if (Op0->hasOneUse() || Op1->hasOneUse())
1252+
if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
1253+
match(Op1, m_Not(m_c_Or(m_Specific(A), m_Specific(B)))))
1254+
return BinaryOperator::CreateNot(Builder.CreateXor(A, B));
12531255

12541256
// (A & ~B) | (~A & B) --> A ^ B
12551257
// (A & ~B) | (B & ~A) --> A ^ B

‎llvm/test/Transforms/InstCombine/and-or-not.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ define i32 @and_to_nxor_multiuse(float %fa, float %fb) {
527527
; CHECK-NEXT: [[NOTB:%.*]] = xor i32 [[B]], -1
528528
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[NOTA]], [[B]]
529529
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[NOTB]], [[A]]
530-
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[B]], [[A]]
531-
; CHECK-NEXT: [[AND:%.*]] = xor i32 [[TMP1]], -1
530+
; CHECK-NEXT: [[AND:%.*]] = and i32 [[OR1]], [[OR2]]
532531
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[OR1]], [[OR2]]
533532
; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL1]], [[AND]]
534533
; CHECK-NEXT: ret i32 [[MUL2]]
@@ -552,8 +551,7 @@ define i32 @or_to_nxor_multiuse(i32 %a, i32 %b) {
552551
; CHECK-NEXT: [[AND:%.*]] = and i32 [[A:%.*]], [[B:%.*]]
553552
; CHECK-NEXT: [[OR:%.*]] = or i32 [[A]], [[B]]
554553
; CHECK-NEXT: [[NOTOR:%.*]] = xor i32 [[OR]], -1
555-
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[A]], [[B]]
556-
; CHECK-NEXT: [[OR2:%.*]] = xor i32 [[TMP1]], -1
554+
; CHECK-NEXT: [[OR2:%.*]] = or i32 [[AND]], [[NOTOR]]
557555
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[AND]], [[NOTOR]]
558556
; CHECK-NEXT: [[MUL2:%.*]] = mul i32 [[MUL1]], [[OR2]]
559557
; CHECK-NEXT: ret i32 [[MUL2]]

0 commit comments

Comments
 (0)
Please sign in to comment.