Tighten the API for ScalarEvolutionNormalization

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@300331 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sanjoy Das 2017-04-14 15:49:59 +00:00
parent cfd7f99b33
commit fe33eb8710
5 changed files with 58 additions and 37 deletions

View File

@ -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<const Loop *, 2> PostIncLoopSet;
typedef function_ref<bool(const SCEVAddRecExpr *)> NormalizePredTy;
/// TransformForPostIncUse - Transform the given expression according to the
/// given transformation kind.
const SCEV *TransformForPostIncUse(TransformKind Kind, const SCEV *S,
Optional<NormalizePredTy> 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

View File

@ -270,17 +270,15 @@ bool IVUsers::AddUsersImpl(Instruction *I,
return Result;
};
ISE = TransformForPostIncUse(Normalize, ISE,
Optional<NormalizePredTy>(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<PostIncLoopSet &>(IU.getPostIncLoops()), *SE);
return normalizeForPostIncUse(getReplacementExpr(IU), IU.getPostIncLoops(),
*SE);
}
static const SCEVAddRecExpr *findAddRecForLoop(const SCEV *S, const Loop *L) {

View File

@ -1268,8 +1268,7 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
if (PostIncLoops.count(L)) {
PostIncLoopSet Loops;
Loops.insert(L);
Normalized = cast<SCEVAddRecExpr>(
TransformForPostIncUse(Normalize, S, None, Loops, SE));
Normalized = cast<SCEVAddRecExpr>(normalizeForPostIncUse(S, Loops, SE));
}
// Strip off any non-loop-dominating component from the addrec start.

View File

@ -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<NormalizePredTy> Pred;
PostIncLoopSet &Loops;
const PostIncLoopSet &Loops;
ScalarEvolution &SE;
DenseMap<const SCEV*, const SCEV*> Transformed;
public:
PostIncTransform(TransformKind kind, Optional<NormalizePredTy> 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<NormalizePredTy> Pred,
PostIncLoopSet &Loops,
ScalarEvolution &SE) {
static const SCEV *TransformForPostIncUse(TransformKind Kind, const SCEV *S,
Optional<NormalizePredTy> 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);
}

View File

@ -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<PostIncLoopSet &>(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<PostIncLoopSet &>(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.