Skip to content

[InstCombine] Use known bits to simplify mask in foldSelectICmpAnd #128741

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 4 commits into from
Mar 14, 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
14 changes: 9 additions & 5 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
/// With some variations depending if FC is larger than TC, or the shift
/// isn't needed, or the bit widths don't match.
static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
InstCombiner::BuilderTy &Builder) {
InstCombiner::BuilderTy &Builder,
const SimplifyQuery &SQ) {
const APInt *SelTC, *SelFC;
if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
!match(Sel.getFalseValue(), m_APInt(SelFC)))
Expand Down Expand Up @@ -148,11 +149,14 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
} else if (auto Res = decomposeBitTestICmp(Cmp->getOperand(0),
Cmp->getOperand(1), Pred)) {
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
if (!Res->Mask.isPowerOf2())
AndMask = Res->Mask;
V = Res->X;
KnownBits Known =
computeKnownBits(V, /*Depth=*/0, SQ.getWithInstruction(&Sel));
AndMask &= Known.getMaxValue();
if (!AndMask.isPowerOf2())
return nullptr;

V = Res->X;
AndMask = Res->Mask;
Pred = Res->Pred;
CreateAnd = true;
} else {
Expand Down Expand Up @@ -1957,7 +1961,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
return NewSel;

if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder, SQ))
return replaceInstUsesWith(SI, V);

// NOTE: if we wanted to, this is where to detect integer MIN/MAX
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/Transforms/InstCombine/select-icmp-and.ll
Original file line number Diff line number Diff line change
Expand Up @@ -912,3 +912,35 @@ define i16 @select_trunc_nuw_bittest_or(i8 %x) {
%res = or i16 4, %select
ret i16 %res
}

define i16 @select_icmp_bittest_range(i16 range (i16 0, 512) %a) {
; CHECK-LABEL: @select_icmp_bittest_range(
; CHECK-NEXT: [[RES:%.*]] = and i16 [[A:%.*]], 256
; CHECK-NEXT: ret i16 [[RES]]
;
%cmp = icmp ult i16 %a, 256
%res = select i1 %cmp, i16 0, i16 256
ret i16 %res
}

define i16 @select_icmp_bittest_range_negative_test(i16 range (i16 0, 513) %a) {
; CHECK-LABEL: @select_icmp_bittest_range_negative_test(
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i16 [[A:%.*]], 256
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i16 0, i16 256
; CHECK-NEXT: ret i16 [[RES]]
;
%cmp = icmp ult i16 %a, 256
%res = select i1 %cmp, i16 0, i16 256
ret i16 %res
}

define i16 @select_icmp_bittest_range_negative_test2(i16 range (i16 0, 512) %a) {
; CHECK-LABEL: @select_icmp_bittest_range_negative_test2(
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i16 [[A:%.*]], 255
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i16 0, i16 255
; CHECK-NEXT: ret i16 [[RES]]
;
%cmp = icmp ult i16 %a, 255
%res = select i1 %cmp, i16 0, i16 255
ret i16 %res
}
Loading