@@ -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.
122122static 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
0 commit comments