2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
#include "polly/Support/SCEVValidator.h"
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
|
2011-11-08 23:41:28 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace SCEVType {
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief The type of a SCEV
|
|
|
|
///
|
|
|
|
/// To check for the validity of a SCEV we assign to each SCEV a type. The
|
|
|
|
/// possible types are INT, PARAM, IV and INVALID. The order of the types is
|
|
|
|
/// important. The subexpressions of SCEV with a type X can only have a type
|
|
|
|
/// that is smaller or equal than X.
|
|
|
|
enum TYPE {
|
|
|
|
// An integer value.
|
|
|
|
INT,
|
|
|
|
|
|
|
|
// An expression that is constant during the execution of the Scop,
|
|
|
|
// but that may depend on parameters unknown at compile time.
|
|
|
|
PARAM,
|
|
|
|
|
|
|
|
// An expression that may change during the execution of the SCoP.
|
|
|
|
IV,
|
|
|
|
|
|
|
|
// An invalid expression.
|
|
|
|
INVALID
|
|
|
|
};
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief The result the validator returns for a SCEV expression.
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult {
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief The type of the expression
|
2011-11-07 20:58:54 +08:00
|
|
|
SCEVType::TYPE type;
|
2011-11-17 20:56:17 +08:00
|
|
|
|
|
|
|
/// @brief The set of Parameters in the expression.
|
2011-11-08 23:41:28 +08:00
|
|
|
std::vector<const SCEV*> Parameters;
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
public:
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Create an invalid result.
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult() : type(SCEVType::INVALID) {};
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief The copy constructor
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult(const ValidatorResult &vres) {
|
|
|
|
type = vres.type;
|
2011-11-08 23:41:28 +08:00
|
|
|
Parameters = vres.Parameters;
|
2011-11-07 20:58:54 +08:00
|
|
|
};
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Construct a result with a certain type and no parameters.
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult(SCEVType::TYPE type) : type(type) {};
|
2011-11-17 20:56:17 +08:00
|
|
|
|
|
|
|
/// @brief Construct a result with a certain type and a single parameter.
|
2011-11-17 20:56:10 +08:00
|
|
|
ValidatorResult(SCEVType::TYPE type, const SCEV *Expr) : type(type) {
|
2011-11-08 23:41:28 +08:00
|
|
|
Parameters.push_back(Expr);
|
|
|
|
};
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV constant during the execution of the SCoP.
|
2011-11-07 20:58:54 +08:00
|
|
|
bool isConstant() {
|
|
|
|
return type == SCEVType::INT || type == SCEVType::PARAM;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV valid.
|
2011-11-07 20:58:54 +08:00
|
|
|
bool isValid() {
|
|
|
|
return type != SCEVType::INVALID;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV of type IV.
|
2011-11-07 20:58:54 +08:00
|
|
|
bool isIV() {
|
|
|
|
return type == SCEVType::IV;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV of type INT.
|
2011-11-07 20:58:54 +08:00
|
|
|
bool isINT() {
|
|
|
|
return type == SCEVType::INT;
|
|
|
|
}
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
/// @brief Get the parameters of this validator result.
|
|
|
|
std::vector<const SCEV*> getParameters() {
|
|
|
|
return Parameters;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Add the parameters of Source to this result.
|
2011-11-17 20:56:19 +08:00
|
|
|
void addParamsFrom(class ValidatorResult &Source) {
|
2011-11-17 20:56:17 +08:00
|
|
|
Parameters.insert(Parameters.end(),
|
|
|
|
Source.Parameters.begin(),
|
2011-11-08 23:41:28 +08:00
|
|
|
Source.Parameters.end());
|
|
|
|
}
|
2011-11-17 20:56:19 +08:00
|
|
|
|
|
|
|
/// @brief Merge a result.
|
|
|
|
///
|
|
|
|
/// This means to merge the parameters and to set the type to the most
|
|
|
|
/// specific type that matches both.
|
|
|
|
void merge(class ValidatorResult &ToMerge) {
|
|
|
|
type = std::max(type, ToMerge.type);
|
|
|
|
addParamsFrom(ToMerge);
|
|
|
|
}
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Check if a SCEV is valid in a SCoP.
|
|
|
|
struct SCEVValidator
|
2011-11-17 20:56:19 +08:00
|
|
|
: public SCEVVisitor<SCEVValidator, class ValidatorResult> {
|
2011-11-07 20:58:54 +08:00
|
|
|
private:
|
|
|
|
const Region *R;
|
|
|
|
ScalarEvolution &SE;
|
2011-11-10 20:45:03 +08:00
|
|
|
const Value *BaseAddress;
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
SCEVValidator(const Region *R, ScalarEvolution &SE,
|
2011-11-10 20:45:03 +08:00
|
|
|
const Value *BaseAddress) : R(R), SE(SE),
|
2011-11-07 20:58:54 +08:00
|
|
|
BaseAddress(BaseAddress) {};
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitConstant(const SCEVConstant *Constant) {
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INT);
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult Op = visit(Expr->getOperand());
|
|
|
|
|
|
|
|
// We currently do not represent a truncate expression as an affine
|
|
|
|
// expression. If it is constant during Scop execution, we treat it as a
|
|
|
|
// parameter, otherwise we bail out.
|
|
|
|
if (Op.isConstant())
|
2011-11-08 23:41:28 +08:00
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult Op = visit(Expr->getOperand());
|
|
|
|
|
|
|
|
// We currently do not represent a zero extend expression as an affine
|
|
|
|
// expression. If it is constant during Scop execution, we treat it as a
|
|
|
|
// parameter, otherwise we bail out.
|
|
|
|
if (Op.isConstant())
|
2011-11-17 20:56:19 +08:00
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
// We currently allow only signed SCEV expressions. In the case of a
|
|
|
|
// signed value, a sign extend is a noop.
|
|
|
|
//
|
|
|
|
// TODO: Reconsider this when we add support for unsigned values.
|
|
|
|
return visit(Expr->getOperand());
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult Return(SCEVType::INT);
|
|
|
|
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
|
|
|
|
ValidatorResult Op = visit(Expr->getOperand(i));
|
|
|
|
|
|
|
|
if (!Op.isValid())
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for NSW and NUW.
|
|
|
|
return Return;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult Return(SCEVType::INT);
|
|
|
|
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
|
|
|
|
ValidatorResult Op = visit(Expr->getOperand(i));
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
if (Op.isINT())
|
2011-11-07 20:58:54 +08:00
|
|
|
continue;
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
if (!Op.isValid() || !Return.isINT())
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Check for NSW and NUW.
|
|
|
|
return Return;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult LHS = visit(Expr->getLHS());
|
|
|
|
ValidatorResult RHS = visit(Expr->getRHS());
|
|
|
|
|
2011-11-17 20:56:12 +08:00
|
|
|
// We currently do not represent an unsigned devision as an affine
|
2011-11-07 20:58:54 +08:00
|
|
|
// expression. If the division is constant during Scop execution we treat it
|
|
|
|
// as a parameter, otherwise we bail out.
|
|
|
|
if (LHS.isConstant() && RHS.isConstant())
|
2011-11-08 23:41:28 +08:00
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
if (!Expr->isAffine())
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
|
|
|
ValidatorResult Start = visit(Expr->getStart());
|
|
|
|
ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE));
|
|
|
|
|
2011-11-17 20:56:14 +08:00
|
|
|
if (!Start.isValid() || !Recurrence.isConstant())
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
2011-11-17 20:56:14 +08:00
|
|
|
if (R->contains(Expr->getLoop())) {
|
|
|
|
if (Recurrence.isINT()) {
|
|
|
|
ValidatorResult Result(SCEVType::IV);
|
|
|
|
Result.addParamsFrom(Start);
|
|
|
|
return Result;
|
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:14 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:14 +08:00
|
|
|
if (Start.isConstant())
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:14 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
ValidatorResult Return(SCEVType::INT);
|
|
|
|
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
|
|
|
|
ValidatorResult Op = visit(Expr->getOperand(i));
|
|
|
|
|
|
|
|
if (!Op.isValid())
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Return;
|
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) {
|
2011-11-08 23:41:28 +08:00
|
|
|
ValidatorResult Return(SCEVType::PARAM);
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
// We do not support unsigned operations. If 'Expr' is constant during Scop
|
|
|
|
// execution we treat this as a parameter, otherwise we bail out.
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
|
|
|
|
ValidatorResult Op = visit(Expr->getOperand(i));
|
|
|
|
|
|
|
|
if (!Op.isConstant())
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-08 23:41:28 +08:00
|
|
|
return Return;
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:10 +08:00
|
|
|
ValidatorResult visitUnknown(const SCEVUnknown *Expr) {
|
2011-11-07 20:58:54 +08:00
|
|
|
Value *V = Expr->getValue();
|
|
|
|
|
|
|
|
if (isa<UndefValue>(V))
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
|
|
|
|
if (R->contains(I))
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
2011-11-10 20:45:03 +08:00
|
|
|
if (BaseAddress == V)
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace polly {
|
|
|
|
bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE,
|
2011-11-10 20:45:03 +08:00
|
|
|
const Value *BaseAddress) {
|
2011-11-07 20:58:54 +08:00
|
|
|
if (isa<SCEVCouldNotCompute>(Expr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SCEVValidator Validator(R, SE, BaseAddress);
|
|
|
|
ValidatorResult Result = Validator.visit(Expr);
|
|
|
|
|
|
|
|
return Result.isValid();
|
|
|
|
}
|
2011-11-08 23:41:28 +08:00
|
|
|
|
|
|
|
std::vector<const SCEV*> getParamsInAffineExpr(const Region *R,
|
|
|
|
const SCEV *Expr,
|
|
|
|
ScalarEvolution &SE,
|
2011-11-10 20:45:03 +08:00
|
|
|
const Value *BaseAddress) {
|
2011-11-08 23:41:28 +08:00
|
|
|
if (isa<SCEVCouldNotCompute>(Expr))
|
|
|
|
return std::vector<const SCEV*>();
|
|
|
|
|
|
|
|
SCEVValidator Validator(R, SE, BaseAddress);
|
|
|
|
ValidatorResult Result = Validator.visit(Expr);
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
return Result.getParameters();
|
2011-11-08 23:41:28 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|