2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2013-03-19 04:21:13 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
#define DEBUG_TYPE "polly-scev-validator"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2011-11-07 20:58:54 +08:00
|
|
|
#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 {
|
2013-02-22 16:21:52 +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-17 20:56:21 +08:00
|
|
|
SCEVType::TYPE Type;
|
2011-11-17 20:56:17 +08:00
|
|
|
|
|
|
|
/// @brief The set of Parameters in the expression.
|
2013-02-22 16:21:52 +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 The copy constructor
|
2011-11-17 20:56:21 +08:00
|
|
|
ValidatorResult(const ValidatorResult &Source) {
|
|
|
|
Type = Source.Type;
|
|
|
|
Parameters = Source.Parameters;
|
2013-02-22 16:21:52 +08:00
|
|
|
}
|
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.
|
2012-03-16 18:16:28 +08:00
|
|
|
ValidatorResult(SCEVType::TYPE Type) : Type(Type) {
|
|
|
|
assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter");
|
2013-02-22 16:21:52 +08:00
|
|
|
}
|
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:21 +08:00
|
|
|
ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) {
|
2011-11-08 23:41:28 +08:00
|
|
|
Parameters.push_back(Expr);
|
2013-02-22 16:21:52 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
/// @brief Get the type of the ValidatorResult.
|
2013-02-22 16:21:52 +08:00
|
|
|
SCEVType::TYPE getType() { return Type; }
|
2012-09-08 22:00:37 +08:00
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV constant during the execution of the SCoP.
|
2013-02-22 16:21:52 +08:00
|
|
|
bool isConstant() { return Type == SCEVType::INT || Type == SCEVType::PARAM; }
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:17 +08:00
|
|
|
/// @brief Is the analyzed SCEV valid.
|
2013-02-22 16:21:52 +08:00
|
|
|
bool isValid() { return Type != SCEVType::INVALID; }
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:21 +08:00
|
|
|
/// @brief Is the analyzed SCEV of Type IV.
|
2013-02-22 16:21:52 +08:00
|
|
|
bool isIV() { return Type == SCEVType::IV; }
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:21 +08:00
|
|
|
/// @brief Is the analyzed SCEV of Type INT.
|
2013-02-22 16:21:52 +08:00
|
|
|
bool isINT() { return Type == SCEVType::INT; }
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
/// @brief Is the analyzed SCEV of Type PARAM.
|
2013-02-22 16:21:52 +08:00
|
|
|
bool isPARAM() { return Type == SCEVType::PARAM; }
|
2012-09-08 22:00:37 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
/// @brief Get the parameters of this validator result.
|
2013-02-22 16:21:52 +08:00
|
|
|
std::vector<const SCEV *> getParameters() { return Parameters; }
|
2011-11-17 20:56:19 +08:00
|
|
|
|
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) {
|
2013-02-22 16:21:52 +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.
|
|
|
|
///
|
2011-11-17 20:56:21 +08:00
|
|
|
/// This means to merge the parameters and to set the Type to the most
|
|
|
|
/// specific Type that matches both.
|
2011-11-17 20:56:19 +08:00
|
|
|
void merge(class ValidatorResult &ToMerge) {
|
2011-11-17 20:56:21 +08:00
|
|
|
Type = std::max(Type, ToMerge.Type);
|
2011-11-17 20:56:19 +08:00
|
|
|
addParamsFrom(ToMerge);
|
|
|
|
}
|
2012-03-16 18:12:37 +08:00
|
|
|
|
|
|
|
void print(raw_ostream &OS) {
|
|
|
|
switch (Type) {
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::INT:
|
|
|
|
OS << "SCEVType::INT";
|
2012-03-16 18:12:37 +08:00
|
|
|
break;
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::PARAM:
|
|
|
|
OS << "SCEVType::PARAM";
|
2012-03-16 18:12:37 +08:00
|
|
|
break;
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::IV:
|
|
|
|
OS << "SCEVType::IV";
|
2012-03-16 18:12:37 +08:00
|
|
|
break;
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::INVALID:
|
|
|
|
OS << "SCEVType::INVALID";
|
2012-03-16 18:12:37 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
};
|
|
|
|
|
2012-03-16 18:12:37 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) {
|
|
|
|
VR.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
/// Check if a SCEV is valid in a SCoP.
|
2013-02-22 16:21:52 +08:00
|
|
|
struct SCEVValidator :
|
|
|
|
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:
|
2013-02-22 16:21:52 +08:00
|
|
|
SCEVValidator(const Region *R, ScalarEvolution &SE, const Value *BaseAddress)
|
|
|
|
: R(R), SE(SE), BaseAddress(BaseAddress) {}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
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());
|
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
switch (Op.getType()) {
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::INT:
|
|
|
|
case SCEVType::PARAM:
|
|
|
|
// 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.
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
|
|
|
case SCEVType::IV:
|
|
|
|
DEBUG(dbgs() << "INVALID: Truncation of SCEVType::IV expression");
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
case SCEVType::INVALID:
|
|
|
|
return Op;
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
llvm_unreachable("Unknown SCEVType");
|
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());
|
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
switch (Op.getType()) {
|
2013-02-22 16:21:52 +08:00
|
|
|
case SCEVType::INT:
|
|
|
|
case SCEVType::PARAM:
|
|
|
|
// 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.
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
|
|
|
case SCEVType::IV:
|
|
|
|
DEBUG(dbgs() << "INVALID: ZeroExtend of SCEVType::IV expression");
|
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
|
|
|
case SCEVType::INVALID:
|
|
|
|
return Op;
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
llvm_unreachable("Unknown SCEVType");
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
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));
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2012-09-08 22:00:37 +08:00
|
|
|
|
|
|
|
// Early exit.
|
|
|
|
if (!Return.isValid())
|
|
|
|
break;
|
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);
|
|
|
|
|
2013-04-14 21:15:59 +08:00
|
|
|
bool HasMultipleParams = false;
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
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;
|
|
|
|
|
2013-04-10 15:42:28 +08:00
|
|
|
if (Op.isPARAM() && Return.isPARAM()) {
|
2013-05-07 15:30:56 +08:00
|
|
|
HasMultipleParams = true;
|
2013-04-10 15:42:28 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) {
|
2012-09-08 22:00:37 +08:00
|
|
|
DEBUG(dbgs() << "INVALID: More than one non-int operand in MulExpr\n"
|
|
|
|
<< "\tExpr: " << *Expr << "\n"
|
|
|
|
<< "\tPrevious expression type: " << Return << "\n"
|
2013-02-22 16:21:52 +08:00
|
|
|
<< "\tNext operand (" << Op
|
|
|
|
<< "): " << *Expr->getOperand(i) << "\n");
|
2012-09-08 22:00:37 +08:00
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
Return.merge(Op);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2013-04-14 21:15:59 +08:00
|
|
|
if (HasMultipleParams)
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
|
|
|
|
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());
|
|
|
|
|
2012-04-10 03:46:05 +08:00
|
|
|
// We currently do not represent an unsigned division 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
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
DEBUG(dbgs() << "INVALID: unsigned division of non-constant expressions");
|
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) {
|
2012-09-08 22:00:37 +08:00
|
|
|
if (!Expr->isAffine()) {
|
|
|
|
DEBUG(dbgs() << "INVALID: AddRec is not affine");
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
ValidatorResult Start = visit(Expr->getStart());
|
|
|
|
ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE));
|
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
if (!Start.isValid())
|
|
|
|
return Start;
|
|
|
|
|
|
|
|
if (!Recurrence.isValid())
|
|
|
|
return Recurrence;
|
2011-11-07 20:58:54 +08:00
|
|
|
|
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
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
DEBUG(dbgs() << "INVALID: AddRec within scop has non-int"
|
|
|
|
"recurrence part");
|
2011-11-17 20:56:14 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
assert(Start.isConstant() && Recurrence.isConstant() &&
|
|
|
|
"Expected 'Start' and 'Recurrence' to be constant");
|
2012-09-08 22:00:37 +08:00
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 20:56:19 +08:00
|
|
|
class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
2012-03-16 18:16:28 +08:00
|
|
|
ValidatorResult Return(SCEVType::INT, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) {
|
|
|
|
ValidatorResult Op = visit(Expr->getOperand(i));
|
|
|
|
|
|
|
|
if (!Op.isValid())
|
2012-09-08 22:00:37 +08:00
|
|
|
return Op;
|
2011-11-07 20:58:54 +08:00
|
|
|
|
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-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));
|
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
if (!Op.isConstant()) {
|
|
|
|
DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand");
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
|
2012-03-16 18:16:28 +08:00
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
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();
|
|
|
|
|
2012-03-17 00:36:47 +08:00
|
|
|
// We currently only support integer types. It may be useful to support
|
|
|
|
// pointer types, e.g. to support code like:
|
|
|
|
//
|
|
|
|
// if (A)
|
|
|
|
// A[i] = 1;
|
|
|
|
//
|
|
|
|
// See test/CodeGen/20120316-InvalidCast.ll
|
2012-09-08 22:00:37 +08:00
|
|
|
if (!Expr->getType()->isIntegerTy()) {
|
|
|
|
DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer type");
|
2012-03-17 00:36:47 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2012-03-17 00:36:47 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
if (isa<UndefValue>(V)) {
|
|
|
|
DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value");
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Expr->getValue()))
|
2012-09-08 22:00:37 +08:00
|
|
|
if (R->contains(I)) {
|
|
|
|
DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction "
|
|
|
|
"within the region\n");
|
2011-11-07 20:58:54 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
|
2012-09-08 22:00:37 +08:00
|
|
|
if (BaseAddress == V) {
|
|
|
|
DEBUG(dbgs() << "INVALID: UnknownExpr references BaseAddress\n");
|
2011-11-10 20:45:03 +08:00
|
|
|
return ValidatorResult(SCEVType::INVALID);
|
2012-09-08 22:00:37 +08:00
|
|
|
}
|
2011-11-10 20:45:03 +08:00
|
|
|
|
|
|
|
return ValidatorResult(SCEVType::PARAM, Expr);
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-19 04:21:13 +08:00
|
|
|
/// @brief Check whether a SCEV refers to an SSA name defined inside a region.
|
|
|
|
///
|
|
|
|
struct SCEVInRegionDependences :
|
|
|
|
public SCEVVisitor<SCEVInRegionDependences, bool> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Returns true when the SCEV has SSA names defined in region R.
|
|
|
|
static bool hasDependences(const SCEV *S, const Region *R) {
|
|
|
|
SCEVInRegionDependences Ignore(R);
|
|
|
|
return Ignore.visit(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
SCEVInRegionDependences(const Region *R) : R(R) {}
|
|
|
|
|
|
|
|
bool visit(const SCEV *Expr) {
|
|
|
|
return SCEVVisitor<SCEVInRegionDependences, bool>::visit(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitConstant(const SCEVConstant *Constant) { return false; }
|
|
|
|
|
|
|
|
bool visitTruncateExpr(const SCEVTruncateExpr *Expr) {
|
|
|
|
return visit(Expr->getOperand());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
|
|
|
|
return visit(Expr->getOperand());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
|
|
|
|
return visit(Expr->getOperand());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitAddExpr(const SCEVAddExpr *Expr) {
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
if (visit(Expr->getOperand(i)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitMulExpr(const SCEVMulExpr *Expr) {
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
if (visit(Expr->getOperand(i)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitUDivExpr(const SCEVUDivExpr *Expr) {
|
|
|
|
if (visit(Expr->getLHS()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (visit(Expr->getRHS()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
|
|
|
if (visit(Expr->getStart()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < Expr->getNumOperands(); ++i)
|
|
|
|
if (visit(Expr->getOperand(i)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
|
|
|
for (size_t i = 0; i < Expr->getNumOperands(); ++i)
|
|
|
|
if (visit(Expr->getOperand(i)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitUMaxExpr(const SCEVUMaxExpr *Expr) {
|
|
|
|
for (size_t i = 0; i < Expr->getNumOperands(); ++i)
|
|
|
|
if (visit(Expr->getOperand(i)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool visitUnknown(const SCEVUnknown *Expr) {
|
|
|
|
Instruction *Inst = dyn_cast<Instruction>(Expr->getValue());
|
|
|
|
|
|
|
|
// Return true when Inst is defined inside the region R.
|
|
|
|
if (Inst && R->contains(Inst))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Region *R;
|
|
|
|
};
|
|
|
|
|
2011-11-07 20:58:54 +08:00
|
|
|
namespace polly {
|
2013-03-23 09:05:07 +08:00
|
|
|
bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R) {
|
|
|
|
return SCEVInRegionDependences::hasDependences(Expr, R);
|
|
|
|
}
|
2013-03-19 04:21:13 +08:00
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
bool isAffineExpr(const Region *R, const SCEV *Expr, ScalarEvolution &SE,
|
|
|
|
const Value *BaseAddress) {
|
|
|
|
if (isa<SCEVCouldNotCompute>(Expr))
|
2013-02-22 16:21:52 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
SCEVValidator Validator(R, SE, BaseAddress);
|
2013-03-23 09:05:07 +08:00
|
|
|
DEBUG(dbgs() << "\n"; dbgs() << "Expr: " << *Expr << "\n";
|
|
|
|
dbgs() << "Region: " << R->getNameStr() << "\n"; dbgs() << " -> ");
|
2013-02-22 16:21:52 +08:00
|
|
|
|
|
|
|
ValidatorResult Result = Validator.visit(Expr);
|
|
|
|
|
2013-03-23 09:05:07 +08:00
|
|
|
DEBUG(if (Result.isValid()) dbgs() << "VALID\n"; dbgs() << "\n";);
|
2013-02-22 16:21:52 +08:00
|
|
|
|
|
|
|
return Result.isValid();
|
|
|
|
}
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2013-05-07 15:30:56 +08:00
|
|
|
std::vector<const SCEV *> getParamsInAffineExpr(const Region *R,
|
|
|
|
const SCEV *Expr,
|
|
|
|
ScalarEvolution &SE,
|
|
|
|
const Value *BaseAddress) {
|
2013-02-22 16:21:52 +08:00
|
|
|
if (isa<SCEVCouldNotCompute>(Expr))
|
|
|
|
return std::vector<const SCEV *>();
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
SCEVValidator Validator(R, SE, BaseAddress);
|
|
|
|
ValidatorResult Result = Validator.visit(Expr);
|
2011-11-08 23:41:28 +08:00
|
|
|
|
2013-02-22 16:21:52 +08:00
|
|
|
return Result.getParameters();
|
|
|
|
}
|
2011-11-07 20:58:54 +08:00
|
|
|
}
|