Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 8 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,
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,13 @@ 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, 0, SQ.getWithInstruction(Cmp));
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 +1960,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
10 changes: 10 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,13 @@ 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
}