Skip to content

[InstCombine] Pass disjoint in or combine #138800

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
May 8, 2025
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
13 changes: 12 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3894,12 +3894,23 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
// be simplified by a later pass either, so we try swapping the inner/outer
// ORs in the hopes that we'll be able to simplify it this way.
// (X|C) | V --> (X|V) | C
// Pass the disjoint flag in the following two patterns:
// 1. or-disjoint (or-disjoint X, C), V -->
// or-disjoint (or-disjoint X, V), C
//
// 2. or-disjoint (or X, C), V -->
// or (or-disjoint X, V), C
ConstantInt *CI;
if (Op0->hasOneUse() && !match(Op1, m_ConstantInt()) &&
match(Op0, m_Or(m_Value(A), m_ConstantInt(CI)))) {
bool IsDisjointOuter = cast<PossiblyDisjointInst>(I).isDisjoint();
bool IsDisjointInner = cast<PossiblyDisjointInst>(Op0)->isDisjoint();
Value *Inner = Builder.CreateOr(A, Op1);
cast<PossiblyDisjointInst>(Inner)->setIsDisjoint(IsDisjointOuter);
Inner->takeName(Op0);
return BinaryOperator::CreateOr(Inner, CI);
return IsDisjointOuter && IsDisjointInner
? BinaryOperator::CreateDisjointOr(Inner, CI)
: BinaryOperator::CreateOr(Inner, CI);
}

// Change (or (bool?A:B),(bool?C:D)) --> (bool?(or A,C):(or B,D))
Expand Down
48 changes: 48 additions & 0 deletions llvm/test/Transforms/InstCombine/or-or-combine.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -passes=instcombine < %s | FileCheck %s

; (X | C) | Y --> (X | Y) | C

define i32 @test1(i32 %x, i32 %y) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: [[INNER:%.*]] = or disjoint i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[OUTER:%.*]] = or disjoint i32 [[INNER]], 5
; CHECK-NEXT: ret i32 [[OUTER]]
;
%inner = or disjoint i32 %x, 5
%outer = or disjoint i32 %inner, %y
ret i32 %outer
}

define i32 @test2(i32 %x, i32 %y) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: [[INNER:%.*]] = or disjoint i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[OUTER:%.*]] = or i32 [[INNER]], 5
; CHECK-NEXT: ret i32 [[OUTER]]
;
%inner = or i32 %x, 5
%outer = or disjoint i32 %inner, %y
ret i32 %outer
}

define i32 @test3(i32 %x, i32 %y) {
; CHECK-LABEL: @test3(
; CHECK-NEXT: [[INNER:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[OUTER:%.*]] = or i32 [[INNER]], 5
; CHECK-NEXT: ret i32 [[OUTER]]
;
%inner = or disjoint i32 %x, 5
%outer = or i32 %inner, %y
ret i32 %outer
}

define i32 @test4(i32 %x, i32 %y) {
; CHECK-LABEL: @test4(
; CHECK-NEXT: [[INNER:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[OUTER:%.*]] = or i32 [[INNER]], 5
; CHECK-NEXT: ret i32 [[OUTER]]
;
%inner = or i32 %x, 5
%outer = or i32 %inner, %y
ret i32 %outer
}
Loading