2016-04-24 19:55:41 +02:00
|
|
|
//===- ScalarEvolutionNormalization.cpp - See below -----------------------===//
|
2010-04-08 00:27:08 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-04-08 00:27:08 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements utilities for working with "normalized" expressions.
|
|
|
|
// See the comments at the top of ScalarEvolutionNormalization.h for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
|
2010-04-08 00:27:08 +02:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-04-14 17:49:59 +02:00
|
|
|
/// 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
|
|
|
|
};
|
|
|
|
|
2017-04-14 19:42:10 +02:00
|
|
|
namespace {
|
|
|
|
struct NormalizeDenormalizeRewriter
|
|
|
|
: public SCEVRewriteVisitor<NormalizeDenormalizeRewriter> {
|
|
|
|
const TransformKind Kind;
|
|
|
|
|
|
|
|
// NB! Pred is a function_ref. Storing it here is okay only because
|
|
|
|
// we're careful about the lifetime of NormalizeDenormalizeRewriter.
|
|
|
|
const NormalizePredTy Pred;
|
|
|
|
|
|
|
|
NormalizeDenormalizeRewriter(TransformKind Kind, NormalizePredTy Pred,
|
|
|
|
ScalarEvolution &SE)
|
|
|
|
: SCEVRewriteVisitor<NormalizeDenormalizeRewriter>(SE), Kind(Kind),
|
|
|
|
Pred(Pred) {}
|
|
|
|
const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr);
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const SCEV *
|
|
|
|
NormalizeDenormalizeRewriter::visitAddRecExpr(const SCEVAddRecExpr *AR) {
|
|
|
|
SmallVector<const SCEV *, 8> Operands;
|
|
|
|
|
|
|
|
transform(AR->operands(), std::back_inserter(Operands),
|
|
|
|
[&](const SCEV *Op) { return visit(Op); });
|
|
|
|
|
2017-04-25 02:09:19 +02:00
|
|
|
if (!Pred(AR))
|
|
|
|
return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
|
|
|
|
|
|
|
|
// Normalization and denormalization are fancy names for decrementing and
|
|
|
|
// incrementing a SCEV expression with respect to a set of loops. Since
|
|
|
|
// Pred(AR) has returned true, we know we need to normalize or denormalize AR
|
|
|
|
// with respect to its loop.
|
|
|
|
|
|
|
|
if (Kind == Denormalize) {
|
|
|
|
// Denormalization / "partial increment" is essentially the same as \c
|
|
|
|
// SCEVAddRecExpr::getPostIncExpr. Here we use an explicit loop to make the
|
|
|
|
// symmetry with Normalization clear.
|
|
|
|
for (int i = 0, e = Operands.size() - 1; i < e; i++)
|
|
|
|
Operands[i] = SE.getAddExpr(Operands[i], Operands[i + 1]);
|
|
|
|
} else {
|
|
|
|
assert(Kind == Normalize && "Only two possibilities!");
|
|
|
|
|
|
|
|
// Normalization / "partial decrement" is a bit more subtle. Since
|
|
|
|
// incrementing a SCEV expression (in general) changes the step of the SCEV
|
|
|
|
// expression as well, we cannot use the step of the current expression.
|
|
|
|
// Instead, we have to use the step of the very expression we're trying to
|
|
|
|
// compute!
|
|
|
|
//
|
|
|
|
// We solve the issue by recursively building up the result, starting from
|
|
|
|
// the "least significant" operand in the add recurrence:
|
2017-04-14 19:42:10 +02:00
|
|
|
//
|
2017-04-25 02:09:19 +02:00
|
|
|
// Base case:
|
|
|
|
// Single operand add recurrence. It's its own normalization.
|
|
|
|
//
|
|
|
|
// N-operand case:
|
|
|
|
// {S_{N-1},+,S_{N-2},+,...,+,S_0} = S
|
|
|
|
//
|
|
|
|
// Since the step recurrence of S is {S_{N-2},+,...,+,S_0}, we know its
|
|
|
|
// normalization by induction. We subtract the normalized step
|
|
|
|
// recurrence from S_{N-1} to get the normalization of S.
|
|
|
|
|
|
|
|
for (int i = Operands.size() - 2; i >= 0; i--)
|
|
|
|
Operands[i] = SE.getMinusSCEV(Operands[i], Operands[i + 1]);
|
2010-04-08 00:27:08 +02:00
|
|
|
}
|
2017-04-25 02:09:19 +02:00
|
|
|
|
|
|
|
return SE.getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagAnyWrap);
|
2011-10-13 19:21:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 17:49:59 +02:00
|
|
|
const SCEV *llvm::normalizeForPostIncUse(const SCEV *S,
|
|
|
|
const PostIncLoopSet &Loops,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-14 17:50:07 +02:00
|
|
|
auto Pred = [&](const SCEVAddRecExpr *AR) {
|
|
|
|
return Loops.count(AR->getLoop());
|
|
|
|
};
|
2017-04-14 19:42:10 +02:00
|
|
|
return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
|
2017-04-14 17:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *llvm::normalizeForPostIncUseIf(const SCEV *S, NormalizePredTy Pred,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-14 19:42:10 +02:00
|
|
|
return NormalizeDenormalizeRewriter(Normalize, Pred, SE).visit(S);
|
2017-04-14 17:49:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *llvm::denormalizeForPostIncUse(const SCEV *S,
|
|
|
|
const PostIncLoopSet &Loops,
|
|
|
|
ScalarEvolution &SE) {
|
2017-04-14 17:50:07 +02:00
|
|
|
auto Pred = [&](const SCEVAddRecExpr *AR) {
|
|
|
|
return Loops.count(AR->getLoop());
|
|
|
|
};
|
2017-04-14 19:42:10 +02:00
|
|
|
return NormalizeDenormalizeRewriter(Denormalize, Pred, SE).visit(S);
|
2017-04-14 17:49:59 +02:00
|
|
|
}
|