Skip to content

[InstCombine] Fold (X & C1) | C2 into X & (C1 | C2) iff (X & C2) == C2 #76470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3872,6 +3872,14 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
}
}

// (X & C1) | C2 -> X & (C1 | C2) iff (X & C2) == C2
if (match(Op0, m_OneUse(m_And(m_Value(X), m_APInt(C1)))) &&
match(Op1, m_APInt(C2))) {
KnownBits KnownX = computeKnownBits(X, /*Depth*/ 0, &I);
if ((KnownX.One & *C2) == *C2)
return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *C1 | *C2));
}

return nullptr;
}

Expand Down
95 changes: 95 additions & 0 deletions llvm/test/Transforms/InstCombine/or.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1636,3 +1636,98 @@ define i32 @assoc_cast_assoc_disjoint(i16 %x) {
%c = or disjoint i32 %b, 65536
ret i32 %c
}

; (X & C1) | C2 -> X & (C1 | C2) iff (X & C2) == C2
define i32 @test_or_and_disjoint(i32 %a) {
; CHECK-LABEL: @test_or_and_disjoint(
; CHECK-NEXT: [[A0:%.*]] = and i32 [[A:%.*]], 24
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A0]], 8
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[A2:%.*]] = and i32 [[A]], 15
; CHECK-NEXT: ret i32 [[A2]]
; CHECK: if.else:
; CHECK-NEXT: ret i32 0
;
%a0 = and i32 %a, 24
%cmp = icmp eq i32 %a0, 8
br i1 %cmp, label %if.then, label %if.else
if.then:
%a1 = and i32 %a, 7
%a2 = or i32 %a1, 8
ret i32 %a2
if.else:
ret i32 0
}

define i32 @test_or_and_mixed(i32 %a) {
; CHECK-LABEL: @test_or_and_mixed(
; CHECK-NEXT: [[A0:%.*]] = and i32 [[A:%.*]], 27
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A0]], 11
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[A2:%.*]] = and i32 [[A]], 15
; CHECK-NEXT: ret i32 [[A2]]
; CHECK: if.else:
; CHECK-NEXT: ret i32 0
;
%a0 = and i32 %a, 27
%cmp = icmp eq i32 %a0, 11
br i1 %cmp, label %if.then, label %if.else
if.then:
%a1 = and i32 %a, 7
%a2 = or i32 %a1, 11
ret i32 %a2
if.else:
ret i32 0
}

; Negative tests

define i32 @test_or_and_disjoint_fail(i32 %a) {
; CHECK-LABEL: @test_or_and_disjoint_fail(
; CHECK-NEXT: [[A0:%.*]] = and i32 [[A:%.*]], 24
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A0]], 16
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[A1:%.*]] = and i32 [[A]], 7
; CHECK-NEXT: [[A2:%.*]] = or disjoint i32 [[A1]], 8
; CHECK-NEXT: ret i32 [[A2]]
; CHECK: if.else:
; CHECK-NEXT: ret i32 0
;
%a0 = and i32 %a, 24
%cmp = icmp eq i32 %a0, 16
br i1 %cmp, label %if.then, label %if.else
if.then:
%a1 = and i32 %a, 7
%a2 = or i32 %a1, 8
ret i32 %a2
if.else:
ret i32 0
}

define i32 @test_or_and_disjoint_multiuse(i32 %a) {
; CHECK-LABEL: @test_or_and_disjoint_multiuse(
; CHECK-NEXT: [[A0:%.*]] = and i32 [[A:%.*]], 24
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[A0]], 8
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[A1:%.*]] = and i32 [[A]], 7
; CHECK-NEXT: call void @use(i32 [[A1]])
; CHECK-NEXT: [[A2:%.*]] = or disjoint i32 [[A1]], 8
; CHECK-NEXT: ret i32 [[A2]]
; CHECK: if.else:
; CHECK-NEXT: ret i32 0
;
%a0 = and i32 %a, 24
%cmp = icmp eq i32 %a0, 8
br i1 %cmp, label %if.then, label %if.else
if.then:
%a1 = and i32 %a, 7
call void @use(i32 %a1)
%a2 = or i32 %a1, 8
ret i32 %a2
if.else:
ret i32 0
}