Skip to content

[InstCombine] Optimize 'xor-and/or-select' sequence to 'or' for bool #66394

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
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: 20 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3112,6 +3112,26 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
return SelectInst::Create(FalseVal, One, AndV);
}

// select (~b & a), a, b -> or a, b
// only for scalar types
if (match(CondVal, m_c_And(m_Not(m_Specific(FalseVal)), m_Specific(TrueVal))) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

This pattern is similar to, but more specific than, the one on line 3109. You should probably move this transform above that one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Correction: Both your patterns are more specific than the ones between lines 3089 and 3109. Move your code above those.

TrueVal->getType()->isIntegerTy(1) &&
FalseVal->getType()->isIntegerTy(1) &&
CondVal->getType()->isIntegerTy(1) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

These scalar type checks are not necessary. When given vector operands, the select instruction will perform the selection element by element.

CondVal->hasOneUse()) {
return BinaryOperator::CreateOr(TrueVal, FalseVal);
}

// select (~b | a), a, b -> or a, b
// only for scalar types
if (match(CondVal, m_c_Or(m_Not(m_Specific(FalseVal)), m_Specific(TrueVal))) &&
TrueVal->getType()->isIntegerTy(1) &&
FalseVal->getType()->isIntegerTy(1) &&
CondVal->getType()->isIntegerTy(1) &&
CondVal->hasOneUse()) {
return BinaryOperator::CreateOr(TrueVal, FalseVal);
}

if (match(FalseVal, m_Zero()) || match(TrueVal, m_One())) {
Use *Y = nullptr;
bool IsAnd = match(FalseVal, m_Zero()) ? true : false;
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/Transforms/InstCombine/fold-xor-and-select-i1.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
Copy link
Collaborator

@bryanpkc bryanpkc Sep 15, 2023

Choose a reason for hiding this comment

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

Add your tests into llvm/test/Transforms/InstCombine/select-and-or.ll, instead of creating new files.

; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i1 @max_if(i1 %a, i1 %b) {
; CHECK-LABEL: define i1 @max_if
; CHECK-SAME: (i1 [[A:%.*]], i1 [[B:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[A]], [[B]]
; CHECK-NEXT: ret i1 [[TMP1]]
;
%1 = xor i1 %b, true
%cmp = and i1 %1, %a
%2 = select i1 %cmp, i1 %a, i1 %b
ret i1 %2
}
14 changes: 14 additions & 0 deletions llvm/test/Transforms/InstCombine/fold-xor-or-select-i1.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

define i1 @max_if(i1 %a, i1 %b) {
; CHECK-LABEL: define i1 @max_if
; CHECK-SAME: (i1 [[A:%.*]], i1 [[B:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = or i1 [[A]], [[B]]
; CHECK-NEXT: ret i1 [[TMP1]]
;
%1 = xor i1 %b, true
%cmp = or i1 %1, %a
%2 = select i1 %cmp, i1 %a, i1 %b
ret i1 %2
}