Skip to content

InstCombine: fold(select C, (X | A), X) | B into X | select C, (A | B), B. (#154246) #154267

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
70 changes: 70 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,6 +2695,40 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
return FoldedLogic;

// Factor a common operand across select feeding an 'or':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for duplicating the code in visitAnd?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
while working on the optimization for the OR operation , I rewrite the expression from (select C, (X | A), X) | B to select C, (A | B),B. The same OR optimization can be used to rewrite AND operations as well. Instead of overengineering , I thought of duplicating it for AND operations.

if not required for AND operations, the code can be discarded.

// (select Cond, (X | A), X) | B -> X | select Cond, (A | B), B
// (select Cond, X, (X | A)) | B -> X | select Cond, B, (A | B)
// and commuted variants swapping the operands of the outer 'or'.
auto TryFactorSelectOr = [&](Value *SelOp, Value *OtherOp) -> Instruction * {
auto *SI = dyn_cast<SelectInst>(SelOp);
if (!SI)
return nullptr;

Value *Cond = SI->getCondition();
Value *T = SI->getTrueValue();
Value *F = SI->getFalseValue();
Value *X, *A;

// Match: select Cond, (X|A), X
if (match(T, m_c_Or(m_Value(X), m_Value(A))) && F == X) {
Value *AorB = Builder.CreateOr(A, OtherOp);
Value *InnerSel = Builder.CreateSelect(Cond, AorB, OtherOp);
return BinaryOperator::CreateOr(X, InnerSel);
}
// Match: select Cond, X, (X|A)
if (match(F, m_c_Or(m_Value(X), m_Value(A))) && T == X) {
Value *AorB = Builder.CreateOr(A, OtherOp);
Value *InnerSel = Builder.CreateSelect(Cond, OtherOp, AorB);
return BinaryOperator::CreateOr(X, InnerSel);
}
return nullptr;
};

if (Instruction *R = TryFactorSelectOr(Op0, Op1))
return R;
if (Instruction *R = TryFactorSelectOr(Op1, Op0))
return R;

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

Expand Down Expand Up @@ -4022,6 +4056,42 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
}
}

// Factor a common operand across select feeding an 'or' before pushing the
// constant into the select arms:
// (select Cond, (X | A), X) | B -> X | select Cond, (A | B), B
// (select Cond, X, (X | A)) | B -> X | select Cond, B, (A | B)
// and commuted variants swapping the operands of the outer 'or'.
auto TryFactorSelectOrEarly = [&](Value *SelOp,
Value *OtherOp) -> Instruction * {
auto *SI = dyn_cast<SelectInst>(SelOp);
if (!SI)
return nullptr;

Value *Cond = SI->getCondition();
Value *T = SI->getTrueValue();
Value *F = SI->getFalseValue();
Value *X, *A;

// Match: select Cond, (X|A), X
if (match(T, m_c_Or(m_Value(X), m_Value(A))) && F == X) {
Value *AorB = Builder.CreateOr(A, OtherOp);
Value *InnerSel = Builder.CreateSelect(Cond, AorB, OtherOp);
return BinaryOperator::CreateOr(X, InnerSel);
}
// Match: select Cond, X, (X|A)
if (match(F, m_c_Or(m_Value(X), m_Value(A))) && T == X) {
Value *AorB = Builder.CreateOr(A, OtherOp);
Value *InnerSel = Builder.CreateSelect(Cond, OtherOp, AorB);
return BinaryOperator::CreateOr(X, InnerSel);
}
return nullptr;
};

if (Instruction *R = TryFactorSelectOrEarly(Op0, Op1))
return R;
if (Instruction *R = TryFactorSelectOrEarly(Op1, Op0))
return R;

if (Instruction *FoldedLogic = foldBinOpIntoSelectOrPhi(I))
return FoldedLogic;

Expand Down
33 changes: 33 additions & 0 deletions llvm/test/Transforms/InstCombine/or-select-factor.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Use update_test_checks.py to generate test files.
  2. Add some negative tests.
  3. Add some multi-use tests.
  4. Do not add the tgt function. It should be generated by the script above.

Please read https://llvm.org/docs/InstCombineContributorGuide.html#tests for more information.


; Fold: (select C, (x | a), x) | b -> x | select C, (a | b), b

define i8 @src(i8 %x, i8 %y) {
; CHECK-LABEL: @src(
; CHECK-NEXT: [[V0:%.*]] = icmp eq i8 [[Y:%.*]], -1
; CHECK-NEXT: [[V1:%.*]] = or i8 [[X:%.*]], 4
; CHECK-NEXT: [[V2:%.*]] = select i1 [[V0]], i8 [[V1]], i8 [[X]]
; CHECK-NEXT: [[V3:%.*]] = or i8 [[V2]], 1
; CHECK-NEXT: ret i8 [[V3]]
;
%v0 = icmp eq i8 %y, -1
%v1 = or i8 %x, 4
%v2 = select i1 %v0, i8 %v1, i8 %x
%v3 = or i8 %v2, 1
ret i8 %v3
}

define i8 @tgt(i8 %x, i8 %y) {
; CHECK-LABEL: @tgt(
; CHECK-NEXT: [[V0:%.*]] = icmp eq i8 [[Y:%.*]], -1
; CHECK-NEXT: [[V1:%.*]] = select i1 [[V0]], i8 5, i8 1
; CHECK-NEXT: [[V2:%.*]] = or i8 [[X:%.*]], [[V1]]
; CHECK-NEXT: ret i8 [[V2]]
;
%v0 = icmp eq i8 %y, -1
%v1 = select i1 %v0, i8 5, i8 1
%v2 = or i8 %x, %v1
ret i8 %v2
}


Loading