From 5ce6d05ad6b500a6262211674e51b8d0598714d3 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 20 May 2010 15:17:54 +0000 Subject: [PATCH] Move the code for deleting BaseRegs and LSRUses into helper functions, and fix a bug that valgrind noticed where the code would std::swap an element with itself. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104225 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopStrengthReduce.cpp | 27 ++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 43c1d06b65d..bca5e4f595b 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -207,6 +207,8 @@ struct Formula { unsigned getNumRegs() const; const Type *getType() const; + void DeleteBaseReg(const SCEV *&S); + bool referencesReg(const SCEV *S) const; bool hasRegsUsedByUsesOtherThan(size_t LUIdx, const RegUseTracker &RegUses) const; @@ -310,6 +312,13 @@ const Type *Formula::getType() const { 0; } +/// DeleteBaseReg - Delete the given base reg from the BaseRegs list. +void Formula::DeleteBaseReg(const SCEV *&S) { + if (&S != &BaseRegs.back()) + std::swap(S, BaseRegs.back()); + BaseRegs.pop_back(); +} + /// referencesReg - Test if this formula references the given register. bool Formula::referencesReg(const SCEV *S) const { return S == ScaledReg || @@ -1013,7 +1022,8 @@ bool LSRUse::InsertFormula(const Formula &F) { /// DeleteFormula - Remove the given formula from this use's list. void LSRUse::DeleteFormula(Formula &F) { - std::swap(F, Formulae.back()); + if (&F != &Formulae.back()) + std::swap(F, Formulae.back()); Formulae.pop_back(); assert(!Formulae.empty() && "LSRUse has no formulae left!"); } @@ -1279,6 +1289,8 @@ class LSRInstance { LSRUse::KindType Kind, const Type *AccessTy); + void DeleteUse(LSRUse &LU); + LSRUse *FindUseWithSimilarFormula(const Formula &F, const LSRUse &OrigLU); public: @@ -1851,6 +1863,13 @@ LSRInstance::getUse(const SCEV *&Expr, return std::make_pair(LUIdx, Offset); } +/// DeleteUse - Delete the given use from the Uses list. +void LSRInstance::DeleteUse(LSRUse &LU) { + if (&LU != &Uses.back()) + std::swap(LU, Uses.back()); + Uses.pop_back(); +} + /// FindUseWithFormula - Look for a use distinct from OrigLU which is has /// a formula that has the same registers as the given formula. LSRUse * @@ -2423,8 +2442,7 @@ void LSRInstance::GenerateScales(LSRUse &LU, unsigned LUIdx, // TODO: This could be optimized to avoid all the copying. Formula F = Base; F.ScaledReg = Quotient; - std::swap(F.BaseRegs[i], F.BaseRegs.back()); - F.BaseRegs.pop_back(); + F.DeleteBaseReg(F.BaseRegs[i]); (void)InsertFormula(LU, LUIdx, F); } } @@ -2895,8 +2913,7 @@ void LSRInstance::NarrowSearchSpaceUsingHeuristics() { } // Delete the old use. - std::swap(LU, Uses.back()); - Uses.pop_back(); + DeleteUse(LU); --LUIdx; --NumUses; break;