Index: llvm/lib/Transforms/Scalar/ConstraintElimination.cpp =================================================================== --- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -947,43 +947,57 @@ CSToUse.popLastConstraint(); }); - bool Changed = false; - if (CSToUse.isConditionImplied(R.Coefficients)) { + auto ReplaceCmpWithConstant = [&](CmpInst *Cmp, bool IsTrue) { if (!DebugCounter::shouldExecute(EliminatedCounter)) return false; LLVM_DEBUG({ - dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n"; + if (IsTrue) { + dbgs() << "Condition " << *Cmp; + } else { + auto InversePred = Cmp->getInversePredicate(); + dbgs() << "Condition " << CmpInst::getPredicateName(InversePred) << " " + << *A << ", " << *B; + } + dbgs() << " implied by dominating constraints\n"; CSToUse.dump(); }); + generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT); - Constant *TrueC = - ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType())); - Cmp->replaceUsesWithIf(TrueC, [](Use &U) { + Constant *ConstantC = + IsTrue + ? ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType())) + : ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType())); + Cmp->replaceUsesWithIf(ConstantC, [](Use &U) { // Conditions in an assume trivially simplify to true. Skip uses // in assume calls to not destroy the available information. auto *II = dyn_cast(U.getUser()); return !II || II->getIntrinsicID() != Intrinsic::assume; }); NumCondsRemoved++; - Changed = true; + return true; + }; + + bool Changed = false; + bool IsConditionImplied = CSToUse.isConditionImplied(R.Coefficients); + + if (IsConditionImplied) { + Changed = ReplaceCmpWithConstant(Cmp, true); + if (!Changed) + return false; } + + // Compute them separately. auto Negated = ConstraintSystem::negate(R.Coefficients); - if (!Negated.empty() && CSToUse.isConditionImplied(Negated)) { - if (!DebugCounter::shouldExecute(EliminatedCounter)) - return false; + auto IsNegatedImplied = + !Negated.empty() && CSToUse.isConditionImplied(Negated); - LLVM_DEBUG({ - dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n"; - CSToUse.dump(); - }); - generateReproducer(Cmp, ReproducerModule, ReproducerCondStack, Info, DT); - Constant *FalseC = - ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType())); - Cmp->replaceAllUsesWith(FalseC); - NumCondsRemoved++; - Changed = true; + if (IsNegatedImplied) { + Changed = ReplaceCmpWithConstant(Cmp, false); + if (!Changed) + return false; } + return Changed; }