@@ -2485,9 +2485,8 @@ Instruction *InstCombinerImpl::foldICmpShlConstant(ICmpInst &Cmp,
24852485 // icmp ule i64 (shl X, 32), 8589934592 ->
24862486 // icmp ule i32 (trunc X, i32), 2 ->
24872487 // icmp ult i32 (trunc X, i32), 3
2488- if (auto FlippedStrictness =
2489- InstCombiner::getFlippedStrictnessPredicateAndConstant (
2490- Pred, ConstantInt::get (ShType->getContext (), C))) {
2488+ if (auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant (
2489+ Pred, ConstantInt::get (ShType->getContext (), C))) {
24912490 CmpPred = FlippedStrictness->first ;
24922491 RHSC = cast<ConstantInt>(FlippedStrictness->second )->getValue ();
24932492 }
@@ -3280,8 +3279,7 @@ bool InstCombinerImpl::matchThreeWayIntCompare(SelectInst *SI, Value *&LHS,
32803279 if (PredB == ICmpInst::ICMP_SGT && isa<Constant>(RHS2)) {
32813280 // x sgt C-1 <--> x sge C <--> not(x slt C)
32823281 auto FlippedStrictness =
3283- InstCombiner::getFlippedStrictnessPredicateAndConstant (
3284- PredB, cast<Constant>(RHS2));
3282+ getFlippedStrictnessPredicateAndConstant (PredB, cast<Constant>(RHS2));
32853283 if (!FlippedStrictness)
32863284 return false ;
32873285 assert (FlippedStrictness->first == ICmpInst::ICMP_SGE &&
@@ -6908,79 +6906,6 @@ Instruction *InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
69086906 return nullptr ;
69096907}
69106908
6911- std::optional<std::pair<CmpPredicate, Constant *>>
6912- InstCombiner::getFlippedStrictnessPredicateAndConstant (CmpPredicate Pred,
6913- Constant *C) {
6914- assert (ICmpInst::isRelational (Pred) && ICmpInst::isIntPredicate (Pred) &&
6915- " Only for relational integer predicates." );
6916-
6917- Type *Type = C->getType ();
6918- bool IsSigned = ICmpInst::isSigned (Pred);
6919-
6920- CmpInst::Predicate UnsignedPred = ICmpInst::getUnsignedPredicate (Pred);
6921- bool WillIncrement =
6922- UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
6923-
6924- // Check if the constant operand can be safely incremented/decremented
6925- // without overflowing/underflowing.
6926- auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
6927- return WillIncrement ? !C->isMaxValue (IsSigned) : !C->isMinValue (IsSigned);
6928- };
6929-
6930- Constant *SafeReplacementConstant = nullptr ;
6931- if (auto *CI = dyn_cast<ConstantInt>(C)) {
6932- // Bail out if the constant can't be safely incremented/decremented.
6933- if (!ConstantIsOk (CI))
6934- return std::nullopt ;
6935- } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
6936- unsigned NumElts = FVTy->getNumElements ();
6937- for (unsigned i = 0 ; i != NumElts; ++i) {
6938- Constant *Elt = C->getAggregateElement (i);
6939- if (!Elt)
6940- return std::nullopt ;
6941-
6942- if (isa<UndefValue>(Elt))
6943- continue ;
6944-
6945- // Bail out if we can't determine if this constant is min/max or if we
6946- // know that this constant is min/max.
6947- auto *CI = dyn_cast<ConstantInt>(Elt);
6948- if (!CI || !ConstantIsOk (CI))
6949- return std::nullopt ;
6950-
6951- if (!SafeReplacementConstant)
6952- SafeReplacementConstant = CI;
6953- }
6954- } else if (isa<VectorType>(C->getType ())) {
6955- // Handle scalable splat
6956- Value *SplatC = C->getSplatValue ();
6957- auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
6958- // Bail out if the constant can't be safely incremented/decremented.
6959- if (!CI || !ConstantIsOk (CI))
6960- return std::nullopt ;
6961- } else {
6962- // ConstantExpr?
6963- return std::nullopt ;
6964- }
6965-
6966- // It may not be safe to change a compare predicate in the presence of
6967- // undefined elements, so replace those elements with the first safe constant
6968- // that we found.
6969- // TODO: in case of poison, it is safe; let's replace undefs only.
6970- if (C->containsUndefOrPoisonElement ()) {
6971- assert (SafeReplacementConstant && " Replacement constant not set" );
6972- C = Constant::replaceUndefsWith (C, SafeReplacementConstant);
6973- }
6974-
6975- CmpInst::Predicate NewPred = CmpInst::getFlippedStrictnessPredicate (Pred);
6976-
6977- // Increment or decrement the constant.
6978- Constant *OneOrNegOne = ConstantInt::get (Type, WillIncrement ? 1 : -1 , true );
6979- Constant *NewC = ConstantExpr::getAdd (C, OneOrNegOne);
6980-
6981- return std::make_pair (NewPred, NewC);
6982- }
6983-
69846909// / If we have an icmp le or icmp ge instruction with a constant operand, turn
69856910// / it into the appropriate icmp lt or icmp gt instruction. This transform
69866911// / allows them to be folded in visitICmpInst.
@@ -6996,8 +6921,7 @@ static ICmpInst *canonicalizeCmpWithConstant(ICmpInst &I) {
69966921 if (!Op1C)
69976922 return nullptr ;
69986923
6999- auto FlippedStrictness =
7000- InstCombiner::getFlippedStrictnessPredicateAndConstant (Pred, Op1C);
6924+ auto FlippedStrictness = getFlippedStrictnessPredicateAndConstant (Pred, Op1C);
70016925 if (!FlippedStrictness)
70026926 return nullptr ;
70036927
0 commit comments