-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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))) && | ||
TrueVal->getType()->isIntegerTy(1) && | ||
FalseVal->getType()->isIntegerTy(1) && | ||
CondVal->getType()->isIntegerTy(1) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These scalar type checks are not necessary. When given vector operands, the |
||
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; | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add your tests into |
||
; 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 | ||
} |
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 | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.