@@ -926,9 +926,11 @@ static Value *foldIsPowerOf2OrZero(ICmpInst *Cmp0, ICmpInst *Cmp1, bool IsAnd,
926926}
927927
928928// / Reduce a pair of compares that check if a value has exactly 1 bit set.
929- // / Also used for logical and/or, must be poison safe.
929+ // / Also used for logical and/or, must be poison safe if range attributes are
930+ // / dropped.
930931static Value *foldIsPowerOf2 (ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
931- InstCombiner::BuilderTy &Builder) {
932+ InstCombiner::BuilderTy &Builder,
933+ InstCombinerImpl &IC) {
932934 // Handle 'and' / 'or' commutation: make the equality check the first operand.
933935 if (JoinedByAnd && Cmp1->getPredicate () == ICmpInst::ICMP_NE)
934936 std::swap (Cmp0, Cmp1);
@@ -942,15 +944,21 @@ static Value *foldIsPowerOf2(ICmpInst *Cmp0, ICmpInst *Cmp1, bool JoinedByAnd,
942944 match (Cmp1, m_ICmp (Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific (X)),
943945 m_SpecificInt (2 ))) &&
944946 Pred0 == ICmpInst::ICMP_NE && Pred1 == ICmpInst::ICMP_ULT) {
945- Value *CtPop = Cmp1->getOperand (0 );
947+ auto *CtPop = cast<Instruction>(Cmp1->getOperand (0 ));
948+ // Drop range attributes and re-infer them in the next iteration.
949+ CtPop->dropPoisonGeneratingAnnotations ();
950+ IC.addToWorklist (CtPop);
946951 return Builder.CreateICmpEQ (CtPop, ConstantInt::get (CtPop->getType (), 1 ));
947952 }
948953 // (X == 0) || (ctpop(X) u> 1) --> ctpop(X) != 1
949954 if (!JoinedByAnd && match (Cmp0, m_ICmp (Pred0, m_Value (X), m_ZeroInt ())) &&
950955 match (Cmp1, m_ICmp (Pred1, m_Intrinsic<Intrinsic::ctpop>(m_Specific (X)),
951956 m_SpecificInt (1 ))) &&
952957 Pred0 == ICmpInst::ICMP_EQ && Pred1 == ICmpInst::ICMP_UGT) {
953- Value *CtPop = Cmp1->getOperand (0 );
958+ auto *CtPop = cast<Instruction>(Cmp1->getOperand (0 ));
959+ // Drop range attributes and re-infer them in the next iteration.
960+ CtPop->dropPoisonGeneratingAnnotations ();
961+ IC.addToWorklist (CtPop);
954962 return Builder.CreateICmpNE (CtPop, ConstantInt::get (CtPop->getType (), 1 ));
955963 }
956964 return nullptr ;
@@ -3347,7 +3355,7 @@ Value *InstCombinerImpl::foldAndOrOfICmps(ICmpInst *LHS, ICmpInst *RHS,
33473355 if (Value *V = foldSignedTruncationCheck (LHS, RHS, I, Builder))
33483356 return V;
33493357
3350- if (Value *V = foldIsPowerOf2 (LHS, RHS, IsAnd, Builder))
3358+ if (Value *V = foldIsPowerOf2 (LHS, RHS, IsAnd, Builder, * this ))
33513359 return V;
33523360
33533361 if (Value *V = foldPowerOf2AndShiftedMask (LHS, RHS, IsAnd, Builder))
0 commit comments