From 69a0438a0595913449f0a13af6dcebade2a01446 Mon Sep 17 00:00:00 2001 From: Sanjoy Das Date: Fri, 14 Apr 2017 16:47:12 +0000 Subject: [PATCH] Delete some unnecessary boilerplate The PostIncTransform class was not pulling its weight, so delete it and use free functions instead. This also makes the use of `function_ref` more idiomatic. We were storing an instance of function_ref in the PostIncTransform class before, which was fine in that specific case, but the usage after this change is more obviously okay. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300338 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Analysis/ScalarEvolutionNormalization.cpp | 76 ++++++++++----------------- 1 file changed, 29 insertions(+), 47 deletions(-) diff --git a/lib/Analysis/ScalarEvolutionNormalization.cpp b/lib/Analysis/ScalarEvolutionNormalization.cpp index d700eacc584..6a1a1feaf11 100644 --- a/lib/Analysis/ScalarEvolutionNormalization.cpp +++ b/lib/Analysis/ScalarEvolutionNormalization.cpp @@ -17,8 +17,6 @@ #include "llvm/Analysis/ScalarEvolutionNormalization.h" using namespace llvm; -namespace { - /// TransformKind - Different types of transformations that /// TransformForPostIncUse can do. enum TransformKind { @@ -29,33 +27,19 @@ enum TransformKind { Denormalize }; -/// Hold the state used during post-inc expression transformation, including a -/// map of transformed expressions. -class PostIncTransform { - TransformKind Kind; - NormalizePredTy Pred; - ScalarEvolution &SE; - - DenseMap Transformed; - -public: - PostIncTransform(TransformKind kind, NormalizePredTy Pred, - ScalarEvolution &se) - : Kind(kind), Pred(Pred), SE(se) {} - - const SCEV *TransformSubExpr(const SCEV *S); +typedef DenseMap NormalizedCacheTy; -protected: - const SCEV *TransformImpl(const SCEV *S); -}; - -} // namespace +static const SCEV *transformSubExpr(const TransformKind Kind, + NormalizePredTy Pred, ScalarEvolution &SE, + NormalizedCacheTy &Cache, const SCEV *S); /// Implement post-inc transformation for all valid expression types. -const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { +static const SCEV *transformImpl(const TransformKind Kind, NormalizePredTy Pred, + ScalarEvolution &SE, NormalizedCacheTy &Cache, + const SCEV *S) { if (const SCEVCastExpr *X = dyn_cast(S)) { const SCEV *O = X->getOperand(); - const SCEV *N = TransformSubExpr(O); + const SCEV *N = transformSubExpr(Kind, Pred, SE, Cache, O); if (O != N) switch (S->getSCEVType()) { case scZeroExtend: return SE.getZeroExtendExpr(N, S->getType()); @@ -71,7 +55,9 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { SmallVector Operands; transform(AR->operands(), std::back_inserter(Operands), - [&](const SCEV *Op) { return TransformSubExpr(Op); }); + [&](const SCEV *Op) { + return transformSubExpr(Kind, Pred, SE, Cache, Op); + }); // Conservatively use AnyWrap until/unless we need FlagNW. const SCEV *Result = @@ -94,12 +80,12 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { // denormalization, which isn't correct. if (Pred(AR)) { const SCEV *TransformedStep = - TransformSubExpr(AR->getStepRecurrence(SE)); + transformSubExpr(Kind, Pred, SE, Cache, AR->getStepRecurrence(SE)); Result = SE.getMinusSCEV(Result, TransformedStep); } #if 0 // See the comment on the assert above. - assert(S == TransformSubExpr(Result, User, OperandValToReplace) && + assert(S == transformSubExpr(Result, User, OperandValToReplace) && "SCEV normalization is not invertible!"); #endif break; @@ -108,7 +94,7 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { // stated above. if (Pred(AR)) { const SCEV *TransformedStep = - TransformSubExpr(AR->getStepRecurrence(SE)); + transformSubExpr(Kind, Pred, SE, Cache, AR->getStepRecurrence(SE)); Result = SE.getAddExpr(Result, TransformedStep); } break; @@ -121,7 +107,7 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { bool Changed = false; // Transform each operand. for (auto *O : X->operands()) { - const SCEV *N = TransformSubExpr(O); + const SCEV *N = transformSubExpr(Kind, Pred, SE, Cache, O); Changed |= N != O; Operands.push_back(N); } @@ -140,8 +126,8 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { if (const SCEVUDivExpr *X = dyn_cast(S)) { const SCEV *LO = X->getLHS(); const SCEV *RO = X->getRHS(); - const SCEV *LN = TransformSubExpr(LO); - const SCEV *RN = TransformSubExpr(RO); + const SCEV *LN = transformSubExpr(Kind, Pred, SE, Cache, LO); + const SCEV *RN = transformSubExpr(Kind, Pred, SE, Cache, RO); if (LO != LN || RO != RN) return SE.getUDivExpr(LN, RN); return S; @@ -152,40 +138,35 @@ const SCEV *PostIncTransform::TransformImpl(const SCEV *S) { /// Manage recursive transformation across an expression DAG. Revisiting /// expressions would lead to exponential recursion. -const SCEV *PostIncTransform::TransformSubExpr(const SCEV *S) { +static const SCEV *transformSubExpr(const TransformKind Kind, + NormalizePredTy Pred, ScalarEvolution &SE, + NormalizedCacheTy &Cache, const SCEV *S) { if (isa(S) || isa(S)) return S; - const SCEV *Result = Transformed.lookup(S); + const SCEV *Result = Cache.lookup(S); if (Result) return Result; - Result = TransformImpl(S); - Transformed[S] = Result; + Result = transformImpl(Kind, Pred, SE, Cache, S); + Cache[S] = Result; return Result; } -/// Top level driver for transforming an expression DAG into its requested -/// post-inc form (either "Normalized" or "Denormalized"). -static const SCEV *TransformForPostIncUse(TransformKind Kind, const SCEV *S, - NormalizePredTy Pred, - ScalarEvolution &SE) { - PostIncTransform Transform(Kind, Pred, SE); - return Transform.TransformSubExpr(S); -} - const SCEV *llvm::normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, ScalarEvolution &SE) { auto Pred = [&](const SCEVAddRecExpr *AR) { return Loops.count(AR->getLoop()); }; - return TransformForPostIncUse(Normalize, S, Pred, SE); + NormalizedCacheTy Cache; + return transformSubExpr(Normalize, Pred, SE, Cache, S); } const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred, ScalarEvolution &SE) { - return TransformForPostIncUse(Normalize, S, Pred, SE); + NormalizedCacheTy Cache; + return transformSubExpr(Normalize, Pred, SE, Cache, S); } const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S, @@ -194,5 +175,6 @@ const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S, auto Pred = [&](const SCEVAddRecExpr *AR) { return Loops.count(AR->getLoop()); }; - return TransformForPostIncUse(Denormalize, S, Pred, SE); + NormalizedCacheTy Cache; + return transformSubExpr(Denormalize, Pred, SE, Cache, S); } -- 2.11.0