Skip to content

Commit

Permalink
[InstCombine] Clean up bitwise folds without one-use check
Browse files Browse the repository at this point in the history
  • Loading branch information
dtcxzyw committed Feb 7, 2024
1 parent e2897e4 commit 1cebf05
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 deletions.
19 changes: 0 additions & 19 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3610,14 +3610,6 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
if (match(Op1, m_c_Xor(m_c_And(m_Value(A), m_Specific(Op0)), m_Value(C))))
return BinaryOperator::CreateOr(Op0, C);

// ((B | C) & A) | B -> B | (A & C)
if (match(Op0, m_c_And(m_c_Or(m_Specific(Op1), m_Value(C)), m_Value(A))))
return BinaryOperator::CreateOr(Op1, Builder.CreateAnd(A, C));

// B | ((B | C) & A) -> B | (A & C)
if (match(Op1, m_c_And(m_c_Or(m_Specific(Op0), m_Value(C)), m_Value(A))))
return BinaryOperator::CreateOr(Op0, Builder.CreateAnd(A, C));

if (Instruction *DeMorgan = matchDeMorgansLaws(I, *this))
return DeMorgan;

Expand Down Expand Up @@ -3670,17 +3662,6 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
Value *Nand = Builder.CreateNot(Builder.CreateAnd(A, B), "nand");
return BinaryOperator::CreateOr(Nand, C);
}

// A | (~A ^ B) --> ~B | A
// B | (A ^ ~B) --> ~A | B
if (Op1->hasOneUse() && match(A, m_Not(m_Specific(Op0)))) {
Value *NotB = Builder.CreateNot(B, B->getName() + ".not");
return BinaryOperator::CreateOr(NotB, Op0);
}
if (Op1->hasOneUse() && match(B, m_Not(m_Specific(Op0)))) {
Value *NotA = Builder.CreateNot(A, A->getName() + ".not");
return BinaryOperator::CreateOr(NotA, Op0);
}
}

// A | ~(A | B) -> A | ~B
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/InstCombine/or-xor.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ define i32 @or_xor_not_uses1(i32 %x, i32 %y) {
; CHECK-LABEL: @or_xor_not_uses1(
; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[Y:%.*]], -1
; CHECK-NEXT: call void @use(i32 [[NOT]])
; CHECK-NEXT: [[X_NOT:%.*]] = xor i32 [[X:%.*]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[X_NOT]], [[Y]]
; CHECK-NEXT: [[TMP1:%.*]] = xor i32 [[X:%.*]], -1
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[Y]]
; CHECK-NEXT: ret i32 [[OR1]]
;
%not = xor i32 %y, -1
Expand Down
13 changes: 6 additions & 7 deletions llvm/test/Transforms/InstCombine/or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ define i32 @test40d(i32 %a, i32 %b) {

define i32 @test45(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @test45(
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[Z:%.*]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[Y:%.*]]
; CHECK-NEXT: ret i32 [[OR1]]
;
Expand All @@ -757,7 +757,7 @@ define i32 @test45_uses1(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @test45_uses1(
; CHECK-NEXT: [[OR:%.*]] = or i32 [[Y:%.*]], [[Z:%.*]]
; CHECK-NEXT: call void @use(i32 [[OR]])
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], [[Z]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[Z]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[Y]]
; CHECK-NEXT: ret i32 [[OR1]]
;
Expand All @@ -773,8 +773,7 @@ define i32 @test45_uses2(i32 %x, i32 %y, i32 %z) {
; CHECK-NEXT: [[OR:%.*]] = or i32 [[Y:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i32 [[OR]], [[X:%.*]]
; CHECK-NEXT: call void @use(i32 [[AND]])
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[Z]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[Y]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[AND]], [[Y]]
; CHECK-NEXT: ret i32 [[OR1]]
;
%or = or i32 %y, %z
Expand All @@ -787,7 +786,7 @@ define i32 @test45_uses2(i32 %x, i32 %y, i32 %z) {
define i32 @test45_commuted1(i32 %x, i32 %y, i32 %z) {
; CHECK-LABEL: @test45_commuted1(
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X:%.*]], [[Z:%.*]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[Z:%.*]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
; CHECK-NEXT: ret i32 [[OR1]]
;
Expand All @@ -803,7 +802,7 @@ define i32 @test45_commuted2(i32 %x, i32 %y, i32 %z) {
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
; CHECK-NEXT: [[XX:%.*]] = mul i32 [[X:%.*]], [[X]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[XX]], [[Z:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[YY]]
; CHECK-NEXT: ret i32 [[OR1]]
;
%yy = mul i32 %y, %y ; thwart complexity-based ordering
Expand All @@ -819,7 +818,7 @@ define i32 @test45_commuted3(i32 %x, i32 %y, i32 %z) {
; CHECK-NEXT: [[YY:%.*]] = mul i32 [[Y:%.*]], [[Y]]
; CHECK-NEXT: [[ZZ:%.*]] = mul i32 [[Z:%.*]], [[Z]]
; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[ZZ]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[YY]], [[TMP1]]
; CHECK-NEXT: [[OR1:%.*]] = or i32 [[TMP1]], [[YY]]
; CHECK-NEXT: ret i32 [[OR1]]
;
%yy = mul i32 %y, %y ; thwart complexity-based ordering
Expand Down

0 comments on commit 1cebf05

Please sign in to comment.