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 @@ -66,7 +66,7 @@ } /// Returns true if there may be a solution for the constraints in the system. - bool mayHaveSolution(); + bool mayHaveSolution(SmallVector *Names = nullptr); static SmallVector negate(SmallVector R) { // The negated constraint R is obtained by multiplying by -1 and adding 1 to @@ -77,7 +77,8 @@ return R; } - bool isConditionImplied(SmallVector R) const; + bool isConditionImplied(SmallVector R, + SmallVector *Names = nullptr) const; ArrayRef getLastConstraint() { return Constraints[0]; } void popLastConstraint() { Constraints.pop_back(); } 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 @@ -134,14 +134,18 @@ dump(Names); } -bool ConstraintSystem::mayHaveSolution() { - LLVM_DEBUG(dump()); +bool ConstraintSystem::mayHaveSolution(SmallVector *Names) { + if (!Names) + LLVM_DEBUG(dump()); + else + LLVM_DEBUG(dump(ArrayRef(Names->begin(), Names->end()))); bool HasSolution = mayHaveSolutionImpl(); LLVM_DEBUG(dbgs() << (HasSolution ? "sat" : "unsat") << "\n"); return HasSolution; } -bool ConstraintSystem::isConditionImplied(SmallVector R) const { +bool ConstraintSystem::isConditionImplied( + SmallVector R, SmallVector *Names) const { // If all variable coefficients are 0, we have 'C >= 0'. If the constant is >= // 0, R is always true, regardless of the system. if (all_of(ArrayRef(R).drop_front(1), [](int64_t C) { return C == 0; })) @@ -153,5 +157,5 @@ auto NewSystem = *this; NewSystem.addVariableRow(R); - return !NewSystem.mayHaveSolution(); + return !NewSystem.mayHaveSolution(Names); } 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 @@ -65,6 +65,19 @@ return Result; } +static SmallVector getVarNamesList(DenseMap &Value2Index){ + SmallVector Names(Value2Index.size(), ""); + for (auto &KV : Value2Index) { + std::string OperandName; + if(KV.first->getName().empty()) + OperandName = KV.first->getNameOrAsOperand(); + else + OperandName = std::string("%") + KV.first->getName().str(); + Names[KV.second - 1] = OperandName; + } + return Names; +} + namespace { class ConstraintInfo; @@ -539,8 +552,10 @@ bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A, Value *B) const { auto R = getConstraintForSolving(Pred, A, B); + auto Value2Index = getValue2Index(R.IsSigned); + auto Names = getVarNamesList(Value2Index); return R.Preconditions.empty() && !R.empty() && - getCS(R.IsSigned).isConditionImplied(R.Coefficients); + getCS(R.IsSigned).isConditionImplied(R.Coefficients, &Names); } void ConstraintInfo::transferToOtherSystem( @@ -640,10 +655,7 @@ #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(); - } + SmallVector Names = getVarNamesList(Value2Index); CS.dump(Names); } @@ -756,6 +768,7 @@ Value *B = Cmp->getOperand(1); auto R = Info.getConstraintForSolving(Pred, A, B); + SmallVector Names = getVarNamesList(Info.getValue2Index(R.IsSigned)); if (R.empty() || !R.isValid(Info)){ LLVM_DEBUG(dbgs() << " failed to decompose condition\n"); return false; @@ -774,7 +787,7 @@ }); bool Changed = false; - if (CSToUse.isConditionImplied(R.Coefficients)) { + if (CSToUse.isConditionImplied(R.Coefficients, &Names)) { if (!DebugCounter::shouldExecute(EliminatedCounter)) return false; @@ -793,7 +806,7 @@ NumCondsRemoved++; Changed = true; } - if (CSToUse.isConditionImplied(ConstraintSystem::negate(R.Coefficients))) { + if (CSToUse.isConditionImplied(ConstraintSystem::negate(R.Coefficients), &Names)) { if (!DebugCounter::shouldExecute(EliminatedCounter)) return false; @@ -903,7 +916,9 @@ return false; auto &CSToUse = Info.getCS(R.IsSigned); - return CSToUse.isConditionImplied(R.Coefficients); + auto Value2Index = Info.getValue2Index(R.IsSigned); + auto Names = getVarNamesList(Value2Index); + return CSToUse.isConditionImplied(R.Coefficients, &Names); }; bool Changed = false;