Skip to content

Commit ffd7298

Browse files
committed
[InstCombine] Use known bits to simplify mask in foldSelectICmpAnd
1 parent 7f78ed9 commit ffd7298

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ static Instruction *foldSelectBinOpIdentity(SelectInst &Sel,
120120
/// With some variations depending if FC is larger than TC, or the shift
121121
/// isn't needed, or the bit widths don't match.
122122
static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
123-
InstCombiner::BuilderTy &Builder) {
123+
InstCombiner::BuilderTy &Builder,
124+
SimplifyQuery &SQ) {
124125
const APInt *SelTC, *SelFC;
125126
if (!match(Sel.getTrueValue(), m_APInt(SelTC)) ||
126127
!match(Sel.getFalseValue(), m_APInt(SelFC)))
@@ -148,11 +149,13 @@ static Value *foldSelectICmpAnd(SelectInst &Sel, ICmpInst *Cmp,
148149
} else if (auto Res = decomposeBitTestICmp(Cmp->getOperand(0),
149150
Cmp->getOperand(1), Pred)) {
150151
assert(ICmpInst::isEquality(Res->Pred) && "Not equality test?");
151-
if (!Res->Mask.isPowerOf2())
152+
AndMask = Res->Mask;
153+
V = Res->X;
154+
KnownBits Known = computeKnownBits(V, 0, SQ.getWithInstruction(Cmp));
155+
AndMask &= Known.getMaxValue();
156+
if (!AndMask.isPowerOf2())
152157
return nullptr;
153158

154-
V = Res->X;
155-
AndMask = Res->Mask;
156159
Pred = Res->Pred;
157160
CreateAnd = true;
158161
} else {
@@ -1957,7 +1960,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19571960
tryToReuseConstantFromSelectInComparison(SI, *ICI, *this))
19581961
return NewSel;
19591962

1960-
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder))
1963+
if (Value *V = foldSelectICmpAnd(SI, ICI, Builder, SQ))
19611964
return replaceInstUsesWith(SI, V);
19621965

19631966
// NOTE: if we wanted to, this is where to detect integer MIN/MAX

llvm/test/Transforms/InstCombine/select-icmp-and.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,7 @@ define i16 @select_trunc_nuw_bittest_or(i8 %x) {
915915

916916
define i16 @select_icmp_bittest_range(i16 range (i16 0, 512) %a) {
917917
; CHECK-LABEL: @select_icmp_bittest_range(
918-
; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ult i16 [[A:%.*]], 256
919-
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP]], i16 0, i16 256
918+
; CHECK-NEXT: [[RES:%.*]] = and i16 [[A:%.*]], 256
920919
; CHECK-NEXT: ret i16 [[RES]]
921920
;
922921
%cmp = icmp ult i16 %a, 256

0 commit comments

Comments
 (0)