diff --git a/llvm/include/llvm/Analysis/ConstraintSystem.h b/llvm/include/llvm/Analysis/ConstraintSystem.h index eab35b7a5923ef..2c83658b81dcc2 100644 --- a/llvm/include/llvm/Analysis/ConstraintSystem.h +++ b/llvm/include/llvm/Analysis/ConstraintSystem.h @@ -79,6 +79,7 @@ class ConstraintSystem { bool isConditionImplied(SmallVector R) const; + ArrayRef getLastConstraint() { return Constraints[0]; } void popLastConstraint() { Constraints.pop_back(); } void popLastNVariables(unsigned N) { for (auto &C : Constraints) { diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index 9a574fa1eb24bf..31c55b5244c997 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -52,17 +52,16 @@ class ConstraintInfo; struct StackEntry { unsigned NumIn; unsigned NumOut; - Instruction *Condition; bool IsNot; bool IsSigned = false; /// Variables that can be removed from the system once the stack entry gets /// removed. SmallVector ValuesToRelease; - StackEntry(unsigned NumIn, unsigned NumOut, CmpInst *Condition, bool IsNot, - bool IsSigned, SmallVector ValuesToRelease) - : NumIn(NumIn), NumOut(NumOut), Condition(Condition), IsNot(IsNot), - IsSigned(IsSigned), ValuesToRelease(ValuesToRelease) {} + StackEntry(unsigned NumIn, unsigned NumOut, bool IsNot, bool IsSigned, + SmallVector ValuesToRelease) + : NumIn(NumIn), NumOut(NumOut), IsNot(IsNot), IsSigned(IsSigned), + ValuesToRelease(ValuesToRelease) {} }; /// Struct to express a pre-condition of the form %Op0 Pred %Op1. @@ -435,16 +434,21 @@ struct State { } // namespace #ifndef NDEBUG -static void dumpWithNames(ConstraintTy &C, +static void dumpWithNames(const ConstraintSystem &CS, DenseMap &Value2Index) { SmallVector Names(Value2Index.size(), ""); for (auto &KV : Value2Index) { Names[KV.second - 1] = std::string("%") + KV.first->getName().str(); } - ConstraintSystem CS; - CS.addVariableRowFill(C.Coefficients); CS.dump(Names); } + +static void dumpWithNames(ArrayRef C, + DenseMap &Value2Index) { + ConstraintSystem CS; + CS.addVariableRowFill(C); + dumpWithNames(CS, Value2Index); +} #endif void State::addInfoFor(BasicBlock &BB) { @@ -553,10 +557,10 @@ void ConstraintInfo::addFact(CmpInst *Condition, bool IsNegated, unsigned NumIn, LLVM_DEBUG({ dbgs() << " constraint: "; - dumpWithNames(R, getValue2Index(R.IsSigned)); + dumpWithNames(R.Coefficients, getValue2Index(R.IsSigned)); }); - DFSInStack.emplace_back(NumIn, NumOut, Condition, IsNegated, R.IsSigned, + DFSInStack.emplace_back(NumIn, NumOut, IsNegated, R.IsSigned, ValuesToRelease); if (R.IsEq) { @@ -565,7 +569,7 @@ void ConstraintInfo::addFact(CmpInst *Condition, bool IsNegated, unsigned NumIn, Coeff *= -1; CSToUse.addVariableRowFill(R.Coefficients); - DFSInStack.emplace_back(NumIn, NumOut, Condition, IsNegated, R.IsSigned, + DFSInStack.emplace_back(NumIn, NumOut, IsNegated, R.IsSigned, SmallVector()); } } @@ -657,8 +661,13 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { assert(E.NumIn <= CB.NumIn); if (CB.NumOut <= E.NumOut) break; - LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot - << "\n"); + LLVM_DEBUG({ + dbgs() << "Removing "; + dumpWithNames(Info.getCS(E.IsSigned).getLastConstraint(), + Info.getValue2Index(E.IsSigned)); + dbgs() << "\n"; + }); + Info.popLastConstraint(E.IsSigned); // Remove variables in the system that went out of scope. auto &Mapping = Info.getValue2Index(E.IsSigned); @@ -700,11 +709,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { if (!DebugCounter::shouldExecute(EliminatedCounter)) continue; - LLVM_DEBUG(dbgs() << "Condition " << *Cmp - << " implied by dominating constraints\n"); LLVM_DEBUG({ - for (auto &E : reverse(DFSInStack)) - dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; + dbgs() << "Condition " << *Cmp + << " implied by dominating constraints\n"; + dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); }); Cmp->replaceUsesWithIf( ConstantInt::getTrue(F.getParent()->getContext()), [](Use &U) { @@ -721,11 +729,10 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) { if (!DebugCounter::shouldExecute(EliminatedCounter)) continue; - LLVM_DEBUG(dbgs() << "Condition !" << *Cmp - << " implied by dominating constraints\n"); LLVM_DEBUG({ - for (auto &E : reverse(DFSInStack)) - dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n"; + dbgs() << "Condition !" << *Cmp + << " implied by dominating constraints\n"; + dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); }); Cmp->replaceAllUsesWith( ConstantInt::getFalse(F.getParent()->getContext()));