diff --git a/include/llvm/Analysis/ScalarEvolutionNormalization.h b/include/llvm/Analysis/ScalarEvolutionNormalization.h index cc79aa9f60f..8c5470ceeaa 100644 --- a/include/llvm/Analysis/ScalarEvolutionNormalization.h +++ b/include/llvm/Analysis/ScalarEvolutionNormalization.h @@ -44,26 +44,24 @@ class Loop; class ScalarEvolution; class SCEV; -/// TransformKind - Different types of transformations that -/// TransformForPostIncUse can do. -enum TransformKind { - /// Normalize - Normalize according to the given loops. - Normalize, - /// Denormalize - Perform the inverse transform on the expression with the - /// given loop set. - Denormalize -}; - -/// PostIncLoopSet - A set of loops. typedef SmallPtrSet PostIncLoopSet; typedef function_ref NormalizePredTy; -/// TransformForPostIncUse - Transform the given expression according to the -/// given transformation kind. -const SCEV *TransformForPostIncUse(TransformKind Kind, const SCEV *S, - Optional Pred, - PostIncLoopSet &Loops, ScalarEvolution &SE); -} +/// Normalize \p S to be post-increment for all loops present in \p +/// Loops. +const SCEV *normalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, + ScalarEvolution &SE); + +/// Normalize \p S for all add recurrence sub-expressions for which \p +/// Pred returns true. +const SCEV *normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred, + ScalarEvolution &SE); + +/// Denormalize \p S to be post-increment for all loops present in \p +/// Loops. +const SCEV *denormalizeForPostIncUse(const SCEV *S, const PostIncLoopSet &Loops, + ScalarEvolution &SE); +} // namespace llvm #endif diff --git a/lib/Analysis/IVUsers.cpp b/lib/Analysis/IVUsers.cpp index de38cf8366c..fde805a5fde 100644 --- a/lib/Analysis/IVUsers.cpp +++ b/lib/Analysis/IVUsers.cpp @@ -270,17 +270,15 @@ bool IVUsers::AddUsersImpl(Instruction *I, return Result; }; - ISE = TransformForPostIncUse(Normalize, ISE, - Optional(NormalizePred), - NewUse.PostIncLoops, *SE); + ISE = normalizeForPostIncUseIf(ISE, NormalizePred, *SE); // PostIncNormalization effectively simplifies the expression under // pre-increment assumptions. Those assumptions (no wrapping) might not // hold for the post-inc value. Catch such cases by making sure the // transformation is invertible. if (OriginalISE != ISE) { - const SCEV *DenormalizedISE = TransformForPostIncUse( - Denormalize, ISE, None, NewUse.PostIncLoops, *SE); + const SCEV *DenormalizedISE = + denormalizeForPostIncUse(ISE, NewUse.PostIncLoops, *SE); // If we normalized the expression, but denormalization doesn't give the // original one, discard this user. @@ -398,9 +396,8 @@ const SCEV *IVUsers::getReplacementExpr(const IVStrideUse &IU) const { /// getExpr - Return the expression for the use. const SCEV *IVUsers::getExpr(const IVStrideUse &IU) const { - return TransformForPostIncUse( - Normalize, getReplacementExpr(IU), None, - const_cast(IU.getPostIncLoops()), *SE); + return normalizeForPostIncUse(getReplacementExpr(IU), IU.getPostIncLoops(), + *SE); } static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) { diff --git a/lib/Analysis/ScalarEvolutionExpander.cpp b/lib/Analysis/ScalarEvolutionExpander.cpp index 66df34dbe7f..6dd10441c4c 100644 --- a/lib/Analysis/ScalarEvolutionExpander.cpp +++ b/lib/Analysis/ScalarEvolutionExpander.cpp @@ -1268,8 +1268,7 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { if (PostIncLoops.count(L)) { PostIncLoopSet Loops; Loops.insert(L); - Normalized = cast( - TransformForPostIncUse(Normalize, S, None, Loops, SE)); + Normalized = cast(normalizeForPostIncUse(S, Loops, SE)); } // Strip off any non-loop-dominating component from the addrec start. diff --git a/lib/Analysis/ScalarEvolutionNormalization.cpp b/lib/Analysis/ScalarEvolutionNormalization.cpp index becc4bb6010..89237273d01 100644 --- a/lib/Analysis/ScalarEvolutionNormalization.cpp +++ b/lib/Analysis/ScalarEvolutionNormalization.cpp @@ -19,19 +19,29 @@ using namespace llvm; namespace { +/// TransformKind - Different types of transformations that +/// TransformForPostIncUse can do. +enum TransformKind { + /// Normalize - Normalize according to the given loops. + Normalize, + /// Denormalize - Perform the inverse transform on the expression with the + /// given loop set. + Denormalize +}; + /// Hold the state used during post-inc expression transformation, including a /// map of transformed expressions. class PostIncTransform { TransformKind Kind; Optional Pred; - PostIncLoopSet &Loops; + const PostIncLoopSet &Loops; ScalarEvolution &SE; DenseMap Transformed; public: PostIncTransform(TransformKind kind, Optional Pred, - PostIncLoopSet &loops, ScalarEvolution &se) + const PostIncLoopSet &loops, ScalarEvolution &se) : Kind(kind), Pred(Pred), Loops(loops), SE(se) {} const SCEV *TransformSubExpr(const SCEV *S); @@ -160,10 +170,28 @@ const SCEV *PostIncTransform::TransformSubExpr(const SCEV *S) { /// Top level driver for transforming an expression DAG into its requested /// post-inc form (either "Normalized" or "Denormalized"). -const SCEV *llvm::TransformForPostIncUse(TransformKind Kind, const SCEV *S, - Optional Pred, - PostIncLoopSet &Loops, - ScalarEvolution &SE) { +static const SCEV *TransformForPostIncUse(TransformKind Kind, const SCEV *S, + Optional Pred, + const PostIncLoopSet &Loops, + ScalarEvolution &SE) { PostIncTransform Transform(Kind, Pred, Loops, SE); return Transform.TransformSubExpr(S); } + +const SCEV *llvm::normalizeForPostIncUse(const SCEV *S, + const PostIncLoopSet &Loops, + ScalarEvolution &SE) { + return TransformForPostIncUse(Normalize, S, None, Loops, SE); +} + +const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred, + ScalarEvolution &SE) { + PostIncLoopSet Empty; + return TransformForPostIncUse(Normalize, S, Pred, Empty, SE); +} + +const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S, + const PostIncLoopSet &Loops, + ScalarEvolution &SE) { + return TransformForPostIncUse(Denormalize, S, None, Loops, SE); +} diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index b584e00b9f4..af137f6faa6 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -3160,7 +3160,7 @@ void LSRInstance::CollectFixupsAndInitialFormulae() { if (SE.isLoopInvariant(N, L) && isSafeToExpand(N, SE)) { // S is normalized, so normalize N before folding it into S // to keep the result normalized. - N = TransformForPostIncUse(Normalize, N, None, TmpPostIncLoops, SE); + N = normalizeForPostIncUse(N, TmpPostIncLoops, SE); Kind = LSRUse::ICmpZero; S = SE.getMinusSCEV(N, S); } @@ -4798,8 +4798,7 @@ Value *LSRInstance::Expand(const LSRUse &LU, assert(!Reg->isZero() && "Zero allocated in a base register!"); // If we're expanding for a post-inc user, make the post-inc adjustment. - PostIncLoopSet &Loops = const_cast(LF.PostIncLoops); - Reg = TransformForPostIncUse(Denormalize, Reg, None, Loops, SE); + Reg = denormalizeForPostIncUse(Reg, LF.PostIncLoops, SE); Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, nullptr))); } @@ -4810,7 +4809,7 @@ Value *LSRInstance::Expand(const LSRUse &LU, // If we're expanding for a post-inc user, make the post-inc adjustment. PostIncLoopSet &Loops = const_cast(LF.PostIncLoops); - ScaledS = TransformForPostIncUse(Denormalize, ScaledS, None, Loops, SE); + ScaledS = denormalizeForPostIncUse(ScaledS, Loops, SE); if (LU.Kind == LSRUse::ICmpZero) { // Expand ScaleReg as if it was part of the base regs.