Skip to content

[InstCombine] Bail out on inner disjoint or in foldSelectICmpEq #121635

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

Closed
wants to merge 4 commits into from
Closed
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
20 changes: 9 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1850,25 +1850,23 @@ static Instruction *foldSelectICmpEq(SelectInst &SI, ICmpInst *ICI,
return nullptr;

const unsigned AndOps = Instruction::And, OrOps = Instruction::Or,
XorOps = Instruction::Xor, NoOps = 0;
XorOps = Instruction::Xor;
enum NotMask { None = 0, NotInner, NotRHS };

// We cannot refine TrueVal to FalseVal when the inner instruction contains
// disjoint or.
auto matchFalseVal = [&](unsigned OuterOpc, unsigned InnerOpc,
unsigned NotMask) {
auto matchInner = m_c_BinOp(InnerOpc, m_Specific(X), m_Specific(Y));
if (OuterOpc == NoOps)
return match(CmpRHS, m_Zero()) && match(FalseVal, matchInner);

if (NotMask == NotInner) {
auto matchInner =
m_CombineAnd(m_c_BinOp(InnerOpc, m_Specific(X), m_Specific(Y)),
m_Unless(m_DisjointOr(m_Value(), m_Value())));
if (NotMask == NotInner)
return match(FalseVal, m_c_BinOp(OuterOpc, m_NotForbidPoison(matchInner),
m_Specific(CmpRHS)));
} else if (NotMask == NotRHS) {
if (NotMask == NotRHS)
return match(FalseVal, m_c_BinOp(OuterOpc, matchInner,
m_NotForbidPoison(m_Specific(CmpRHS))));
} else {
return match(FalseVal,
m_c_BinOp(OuterOpc, matchInner, m_Specific(CmpRHS)));
}
return match(FalseVal, m_c_BinOp(OuterOpc, matchInner, m_Specific(CmpRHS)));
};

// (X&Y)==C ? X|Y : X^Y -> (X^Y)|C : X^Y or (X^Y)^ C : X^Y
Expand Down
78 changes: 78 additions & 0 deletions llvm/test/Transforms/InstCombine/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4564,6 +4564,84 @@ define i32 @src_no_trans_select_xor_eq0_or_xor(i32 %x, i32 %y) {
ret i32 %cond
}

define i32 @src_no_trans_select_xor_eqc_and_disjoint_or_and_notc(i32 %x, i32 %y, i32 %c) {
; CHECK-LABEL: @src_no_trans_select_xor_eqc_and_disjoint_or_and_notc(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[XOR]], [[C:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X]], [[Y]]
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[Y]], [[X]]
; CHECK-NEXT: [[NOT:%.*]] = xor i32 [[C]], -1
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[OR]], [[NOT]]
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[AND]], i32 [[AND1]]
; CHECK-NEXT: ret i32 [[COND]]
;
entry:
%xor = xor i32 %y, %x
%cmp = icmp eq i32 %xor, %c
%and = and i32 %x, %y
%or = or disjoint i32 %y, %x
%not = xor i32 %c, -1
%and1 = and i32 %or, %not
%cond = select i1 %cmp, i32 %and, i32 %and1
ret i32 %cond
}

define i32 @negative_inner_disjoint_or2(i32 %x, i32 %y, i32 %c) {
; CHECK-LABEL: @negative_inner_disjoint_or2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[XOR]], [[C:%.*]]
; CHECK-NEXT: [[AND:%.*]] = and i32 [[X]], [[Y]]
; CHECK-NEXT: [[OR:%.*]] = or disjoint i32 [[Y]], [[X]]
; CHECK-NEXT: [[AND1:%.*]] = xor i32 [[OR]], [[C]]
; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP]], i32 [[AND]], i32 [[AND1]]
; CHECK-NEXT: ret i32 [[COND]]
;
entry:
%xor = xor i32 %y, %x
%cmp = icmp eq i32 %xor, %c
%and = and i32 %x, %y
%or = or disjoint i32 %y, %x
%and1 = xor i32 %or, %c
%cond = select i1 %cmp, i32 %and, i32 %and1
ret i32 %cond
}

define i32 @positive_outer_disjoint_or1(i32 %x, i32 %y, i32 %c) {
; CHECK-LABEL: @positive_outer_disjoint_or1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[AND:%.*]] = xor i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or disjoint i32 [[AND]], [[C:%.*]]
; CHECK-NEXT: ret i32 [[OR1]]
;
entry:
%xor = and i32 %y, %x
%cmp = icmp eq i32 %xor, %c
%or = or i32 %y, %x
%and = xor i32 %y, %x
%or1 = or disjoint i32 %and, %c
%cond = select i1 %cmp, i32 %or, i32 %or1
ret i32 %cond
}

define i32 @positive_outer_disjoint_or2(i32 %x, i32 %y, i32 %c) {
; CHECK-LABEL: @positive_outer_disjoint_or2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[AND:%.*]] = and i32 [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: [[OR1:%.*]] = or disjoint i32 [[AND]], [[C:%.*]]
; CHECK-NEXT: ret i32 [[OR1]]
;
entry:
%xor = xor i32 %y, %x
%cmp = icmp eq i32 %xor, %c
%or = or i32 %y, %x
%and = and i32 %y, %x
%or1 = or disjoint i32 %and, %c
%cond = select i1 %cmp, i32 %or, i32 %or1
ret i32 %cond
}

; (X == C) ? X : Y -> (X == C) ? C : Y
; Fixed #77553
define i32 @src_select_xxory_eq0_xorxy_y(i32 %x, i32 %y) {
Expand Down
Loading