[llvm] [SimplifyIndVar] ICMP predicate conversion to EQ/NE (PR #144945)
Philip Reames via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 3 09:04:00 PST 2025
================
@@ -267,33 +390,43 @@ void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp,
// If the condition is always true or always false in the given context,
// replace it with a constant value.
SmallVector<Instruction *, 4> Users;
+ bool IsDead = false;
for (auto *U : ICmp->users())
Users.push_back(cast<Instruction>(U));
const Instruction *CtxI = findCommonDominator(Users, *DT);
if (auto Ev = SE->evaluatePredicateAt(Pred, S, X, CtxI)) {
SE->forgetValue(ICmp);
ICmp->replaceAllUsesWith(ConstantInt::getBool(ICmp->getContext(), *Ev));
DeadInsts.emplace_back(ICmp);
+ IsDead = true;
LLVM_DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
} else if (makeIVComparisonInvariant(ICmp, IVOperand)) {
- // fallthrough to end of function
- } else if (ICmpInst::isSigned(OriginalPred) &&
- SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
+ IsDead = true;
+ } else {
// If we were unable to make anything above, all we can is to canonicalize
// the comparison hoping that it will open the doors for other
- // optimizations. If we find out that we compare two non-negative values,
- // we turn the instruction's predicate to its unsigned version. Note that
- // we cannot rely on Pred here unless we check if we have swapped it.
- assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
- LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
- << '\n');
- ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
- ICmp->setSameSign();
- } else
- return;
+ // optimizations.
+ if (ICmpInst::isSigned(OriginalPred) && SE->isKnownNonNegative(S) &&
+ SE->isKnownNonNegative(X)) {
+ // If we find out that we compare two non-negative values,
+ // we turn the instruction's predicate to its unsigned version. Note that
+ // we cannot rely on Pred here unless we check if we have swapped it.
+ assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
+ LLVM_DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp
+ << '\n');
+ ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
+ ICmp->setSameSign();
+ Changed = true;
+ }
+ if (forceEqualityForICmp(ICmp, IVOperand)) {
+ Changed = true;
+ }
+ }
- ++NumElimCmp;
- Changed = true;
+ if (IsDead) {
----------------
preames wrote:
See https://github.com/llvm/llvm-project/pull/170514
https://github.com/llvm/llvm-project/pull/144945
More information about the llvm-commits
mailing list