diff --git a/llvm/include/llvm/Analysis/ConstraintSystem.h b/llvm/include/llvm/Analysis/ConstraintSystem.h --- a/llvm/include/llvm/Analysis/ConstraintSystem.h +++ b/llvm/include/llvm/Analysis/ConstraintSystem.h @@ -31,13 +31,17 @@ // Eliminate constraints from the system using Fourier–Motzkin elimination. bool eliminateUsingFM(); - /// Print the constraints in the system, using x0...xn as variable names. - void dump() const; - /// Returns true if there may be a solution for the constraints in the system. bool mayHaveSolutionImpl(); + /// Get list of variable names from Value2Index map. + SmallVector getVarNamesList() const; + public: + ConstraintSystem() {} + ConstraintSystem(const DenseMap &Value2Index) + : Value2Index(Value2Index) {} + bool addVariableRow(ArrayRef R) { assert(Constraints.empty() || R.size() == Constraints.back().size()); // If all variable coefficients are 0, the constraint does not provide any @@ -97,8 +101,8 @@ /// Returns the number of rows in the constraint system. unsigned size() const { return Constraints.size(); } - /// Print the constraints in the system, using \p Names as variable names. - void dump(ArrayRef Names) const; + /// Print the constraints in the system. + void dump() const; }; } // namespace llvm diff --git a/llvm/lib/Analysis/ConstraintSystem.cpp b/llvm/lib/Analysis/ConstraintSystem.cpp --- a/llvm/lib/Analysis/ConstraintSystem.cpp +++ b/llvm/lib/Analysis/ConstraintSystem.cpp @@ -106,10 +106,22 @@ return all_of(Constraints, [](auto &R) { return R[0] >= 0; }); } -void ConstraintSystem::dump(ArrayRef Names) const { +SmallVector ConstraintSystem::getVarNamesList() const { + SmallVector Names(Value2Index.size(), ""); + for (auto &[V, Index] : Value2Index) { + std::string OperandName; + if (V->getName().empty()) + OperandName = V->getNameOrAsOperand(); + else + OperandName = std::string("%") + V->getName().str(); + Names[Index - 1] = OperandName; + } + return Names; +} +void ConstraintSystem::dump() const { if (Constraints.empty()) return; - + auto Names = getVarNamesList(); for (const auto &Row : Constraints) { SmallVector Parts; for (unsigned I = 1, S = Row.size(); I < S; ++I) { @@ -126,14 +138,6 @@ } } -void ConstraintSystem::dump() const { - SmallVector Names; - for (unsigned i = 1; i < Constraints.back().size(); ++i) - Names.push_back("x" + std::to_string(i)); - LLVM_DEBUG(dbgs() << "---\n"); - dump(Names); -} - bool ConstraintSystem::mayHaveSolution() { LLVM_DEBUG(dump()); bool HasSolution = mayHaveSolutionImpl(); diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -636,20 +636,12 @@ } // namespace #ifndef NDEBUG -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(); - } - CS.dump(Names); -} -static void dumpWithNames(ArrayRef C, - DenseMap &Value2Index) { - ConstraintSystem CS; +static void dumpConstraint(ArrayRef C, + const DenseMap &Value2Index) { + ConstraintSystem CS(Value2Index); CS.addVariableRowFill(C); - dumpWithNames(CS, Value2Index); + CS.dump(); } #endif @@ -778,7 +770,7 @@ LLVM_DEBUG({ dbgs() << "Condition " << *Cmp << " implied by dominating constraints\n"; - dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); + CSToUse.dump(); }); Constant *TrueC = ConstantInt::getTrue(CmpInst::makeCmpResultType(Cmp->getType())); @@ -797,7 +789,7 @@ LLVM_DEBUG({ dbgs() << "Condition !" << *Cmp << " implied by dominating constraints\n"; - dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned)); + CSToUse.dump(); }); Constant *FalseC = ConstantInt::getFalse(CmpInst::makeCmpResultType(Cmp->getType())); @@ -840,7 +832,7 @@ LLVM_DEBUG({ dbgs() << " constraint: "; - dumpWithNames(R.Coefficients, getValue2Index(R.IsSigned)); + dumpConstraint(R.Coefficients, getValue2Index(R.IsSigned)); dbgs() << "\n"; }); @@ -981,8 +973,8 @@ break; LLVM_DEBUG({ dbgs() << "Removing "; - dumpWithNames(Info.getCS(E.IsSigned).getLastConstraint(), - Info.getValue2Index(E.IsSigned)); + dumpConstraint(Info.getCS(E.IsSigned).getLastConstraint(), + Info.getValue2Index(E.IsSigned)); dbgs() << "\n"; }); diff --git a/llvm/test/Transforms/ConstraintElimination/debug.ll b/llvm/test/Transforms/ConstraintElimination/debug.ll --- a/llvm/test/Transforms/ConstraintElimination/debug.ll +++ b/llvm/test/Transforms/ConstraintElimination/debug.ll @@ -14,7 +14,17 @@ ; CHECK-NEXT: constraint: %y + -1 * %z <= 0 ; CHECK: Checking %t.1 = icmp ule i4 %x, %z +; CHECK: %x + -1 * %y <= 0 +; CHECK: %y + -1 * %z <= 0 +; CHECK: -1 * %x + %z <= -1 +; CHECK: unsat ; CHECK: Condition %t.1 = icmp ule i4 %x, %z implied by dominating constraints +; CHECK: %x + -1 * %y <= 0 +; CHECK: %y + -1 * %z <= 0 +; CHECK: %x + -1 * %y <= 0 +; CHECK: %y + -1 * %z <= 0 +; CHECK: %x + -1 * %z <= 0 +; CHECK: sat entry: %c.1 = icmp ule i4 %x, %y