2014-07-30 05:06:08 +08:00
|
|
|
//===------ IslExprBuilder.cpp ----- Code generate isl AST expressions ----===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/CodeGen/IslExprBuilder.h"
|
2016-05-12 23:12:43 +08:00
|
|
|
#include "polly/Options.h"
|
2014-10-05 19:32:18 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
2014-07-30 05:06:08 +08:00
|
|
|
#include "polly/Support/GICHelper.h"
|
2015-08-18 19:56:00 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2014-07-30 05:06:08 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-05 02:14:59 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2016-05-12 23:12:43 +08:00
|
|
|
/// @brief Different overflow tracking modes.
|
|
|
|
enum OverflowTrackingChoice {
|
|
|
|
OT_NEVER, ///< Never tack potential overflows.
|
|
|
|
OT_REQUEST, ///< Track potential overflows if requested.
|
|
|
|
OT_ALWAYS ///< Always track potential overflows.
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::opt<OverflowTrackingChoice> OTMode(
|
|
|
|
"polly-overflow-tracking",
|
|
|
|
cl::desc("Define where potential integer overflows in generated "
|
|
|
|
"expressions should be tracked."),
|
|
|
|
cl::values(clEnumValN(OT_NEVER, "never", "Never track the overflow bit."),
|
|
|
|
clEnumValN(OT_REQUEST, "request",
|
|
|
|
"Track the overflow bit if requested."),
|
|
|
|
clEnumValN(OT_ALWAYS, "always",
|
|
|
|
"Always track the overflow bit."),
|
|
|
|
clEnumValEnd),
|
|
|
|
cl::Hidden, cl::init(OT_REQUEST), cl::ZeroOrMore, cl::cat(PollyCategory));
|
|
|
|
|
2016-06-06 17:57:41 +08:00
|
|
|
// @TODO This should actually be derived from the DataLayout.
|
|
|
|
static cl::opt<unsigned> PollyMaxAllowedBitWidth(
|
|
|
|
"polly-max-expr-bit-width",
|
|
|
|
cl::desc("The maximal bit with for generated expressions."), cl::Hidden,
|
|
|
|
cl::ZeroOrMore, cl::init(64), cl::cat(PollyCategory));
|
|
|
|
|
2016-05-12 23:12:43 +08:00
|
|
|
IslExprBuilder::IslExprBuilder(Scop &S, PollyIRBuilder &Builder,
|
|
|
|
IDToValueTy &IDToValue, ValueMapT &GlobalMap,
|
|
|
|
const DataLayout &DL, ScalarEvolution &SE,
|
|
|
|
DominatorTree &DT, LoopInfo &LI)
|
|
|
|
: S(S), Builder(Builder), IDToValue(IDToValue), GlobalMap(GlobalMap),
|
|
|
|
DL(DL), SE(SE), DT(DT), LI(LI) {
|
|
|
|
OverflowState = (OTMode == OT_ALWAYS) ? Builder.getFalse() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IslExprBuilder::setTrackOverflow(bool Enable) {
|
|
|
|
// If potential overflows are tracked always or never we ignore requests
|
|
|
|
// to change the behaviour.
|
|
|
|
if (OTMode != OT_REQUEST)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Enable) {
|
|
|
|
// If tracking should be enabled initialize the OverflowState.
|
|
|
|
OverflowState = Builder.getFalse();
|
|
|
|
} else {
|
|
|
|
// If tracking should be disabled just unset the OverflowState.
|
|
|
|
OverflowState = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::getOverflowState() const {
|
|
|
|
// If the overflow tracking was requested but it is disabled we avoid the
|
|
|
|
// additional nullptr checks at the call sides but instead provide a
|
|
|
|
// meaningful result.
|
|
|
|
if (OTMode == OT_NEVER)
|
|
|
|
return Builder.getFalse();
|
|
|
|
return OverflowState;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createBinOp(BinaryOperator::BinaryOps Opc, Value *LHS,
|
|
|
|
Value *RHS, const Twine &Name) {
|
2016-06-06 17:57:41 +08:00
|
|
|
// Flag that is true if the computation cannot overflow.
|
|
|
|
bool IsSafeToCompute = false;
|
|
|
|
switch (Opc) {
|
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::Sub:
|
|
|
|
IsSafeToCompute = adjustTypesForSafeAddition(LHS, RHS);
|
|
|
|
break;
|
|
|
|
case Instruction::Mul:
|
|
|
|
IsSafeToCompute = adjustTypesForSafeMultiplication(LHS, RHS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown binary operator!");
|
|
|
|
}
|
2016-06-02 19:15:57 +08:00
|
|
|
|
2016-06-06 17:57:41 +08:00
|
|
|
// Handle the plain operation (without overflow tracking or a safe
|
|
|
|
// computation) first.
|
|
|
|
if (!OverflowState || (IsSafeToCompute && (OTMode != OT_ALWAYS))) {
|
2016-05-12 23:12:43 +08:00
|
|
|
switch (Opc) {
|
|
|
|
case Instruction::Add:
|
|
|
|
return Builder.CreateNSWAdd(LHS, RHS, Name);
|
|
|
|
case Instruction::Sub:
|
|
|
|
return Builder.CreateNSWSub(LHS, RHS, Name);
|
|
|
|
case Instruction::Mul:
|
|
|
|
return Builder.CreateNSWMul(LHS, RHS, Name);
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown binary operator!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Function *F = nullptr;
|
|
|
|
Module *M = Builder.GetInsertBlock()->getModule();
|
|
|
|
switch (Opc) {
|
|
|
|
case Instruction::Add:
|
|
|
|
F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow,
|
|
|
|
{LHS->getType()});
|
|
|
|
break;
|
|
|
|
case Instruction::Sub:
|
|
|
|
F = Intrinsic::getDeclaration(M, Intrinsic::ssub_with_overflow,
|
|
|
|
{LHS->getType()});
|
|
|
|
break;
|
|
|
|
case Instruction::Mul:
|
|
|
|
F = Intrinsic::getDeclaration(M, Intrinsic::smul_with_overflow,
|
|
|
|
{LHS->getType()});
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("No overflow intrinsic for binary operator found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *ResultStruct = Builder.CreateCall(F, {LHS, RHS}, Name);
|
|
|
|
assert(ResultStruct->getType()->isStructTy());
|
|
|
|
|
|
|
|
auto *OverflowFlag =
|
|
|
|
Builder.CreateExtractValue(ResultStruct, 1, Name + ".obit");
|
|
|
|
|
|
|
|
// If all overflows are tracked we do not combine the results as this could
|
|
|
|
// cause dominance problems. Instead we will always keep the last overflow
|
|
|
|
// flag as current state.
|
|
|
|
if (OTMode == OT_ALWAYS)
|
|
|
|
OverflowState = OverflowFlag;
|
|
|
|
else
|
|
|
|
OverflowState =
|
|
|
|
Builder.CreateOr(OverflowState, OverflowFlag, "polly.overflow.state");
|
|
|
|
|
|
|
|
return Builder.CreateExtractValue(ResultStruct, 0, Name + ".res");
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createAdd(Value *LHS, Value *RHS, const Twine &Name) {
|
|
|
|
return createBinOp(Instruction::Add, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createSub(Value *LHS, Value *RHS, const Twine &Name) {
|
|
|
|
return createBinOp(Instruction::Sub, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createMul(Value *LHS, Value *RHS, const Twine &Name) {
|
|
|
|
return createBinOp(Instruction::Mul, LHS, RHS, Name);
|
|
|
|
}
|
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
static Type *getWidestType(Type *T1, Type *T2) {
|
2014-07-30 05:06:08 +08:00
|
|
|
assert(isa<IntegerType>(T1) && isa<IntegerType>(T2));
|
|
|
|
|
|
|
|
if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits())
|
|
|
|
return T2;
|
|
|
|
else
|
|
|
|
return T1;
|
|
|
|
}
|
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
void IslExprBuilder::unifyTypes(Value *&V0, Value *&V1, Value *&V2) {
|
|
|
|
auto *T0 = V0->getType();
|
|
|
|
auto *T1 = V1->getType();
|
|
|
|
auto *T2 = V2->getType();
|
|
|
|
if (T0 == T1 && T1 == T2)
|
|
|
|
return;
|
|
|
|
auto *MaxT = getWidestType(T0, T1);
|
|
|
|
MaxT = getWidestType(MaxT, T2);
|
|
|
|
V0 = Builder.CreateSExt(V0, MaxT);
|
|
|
|
V1 = Builder.CreateSExt(V1, MaxT);
|
|
|
|
V2 = Builder.CreateSExt(V2, MaxT);
|
|
|
|
}
|
|
|
|
|
2016-06-06 17:57:41 +08:00
|
|
|
bool IslExprBuilder::adjustTypesForSafeComputation(Value *&LHS, Value *&RHS,
|
|
|
|
unsigned RequiredBitWidth) {
|
|
|
|
unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned MaxUsedBitWidth = std::max(LBitWidth, RBitWidth);
|
|
|
|
|
|
|
|
// @TODO For now use the maximal bit width if the required one is to large but
|
|
|
|
// note that this is not sound.
|
|
|
|
unsigned MaxAllowedBitWidth = PollyMaxAllowedBitWidth;
|
|
|
|
unsigned NewBitWidth =
|
|
|
|
std::max(MaxUsedBitWidth, std::min(MaxAllowedBitWidth, RequiredBitWidth));
|
|
|
|
|
|
|
|
Type *Ty = Builder.getIntNTy(NewBitWidth);
|
|
|
|
LHS = Builder.CreateSExt(LHS, Ty);
|
|
|
|
RHS = Builder.CreateSExt(RHS, Ty);
|
|
|
|
|
|
|
|
// If the new bit width is not large enough the computation is not sound.
|
|
|
|
return NewBitWidth == RequiredBitWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IslExprBuilder::adjustTypesForSafeAddition(Value *&LHS, Value *&RHS) {
|
|
|
|
unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
return adjustTypesForSafeComputation(LHS, RHS,
|
|
|
|
std::max(LBitWidth, RBitWidth) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IslExprBuilder::adjustTypesForSafeMultiplication(Value *&LHS,
|
|
|
|
Value *&RHS) {
|
|
|
|
unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits();
|
|
|
|
return adjustTypesForSafeComputation(LHS, RHS, LBitWidth + RBitWidth);
|
|
|
|
}
|
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus &&
|
|
|
|
"Unsupported unary operation");
|
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
auto *V = create(isl_ast_expr_get_op_arg(Expr, 0));
|
|
|
|
assert(V->getType()->isIntegerTy() &&
|
2015-02-02 23:25:09 +08:00
|
|
|
"Unary expressions can only be created for integer types");
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
2016-06-02 19:15:57 +08:00
|
|
|
return createSub(ConstantInt::getNullValue(V->getType()), V);
|
2014-07-30 05:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"isl ast expression not of type isl_ast_op");
|
|
|
|
assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 &&
|
|
|
|
"We need at least two operands in an n-ary operation");
|
2016-06-02 19:20:52 +08:00
|
|
|
assert((isl_ast_expr_get_op_type(Expr) == isl_ast_op_max ||
|
|
|
|
isl_ast_expr_get_op_type(Expr) == isl_ast_op_min) &&
|
|
|
|
"This is no n-ary isl ast expression");
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2016-06-02 19:20:52 +08:00
|
|
|
bool IsMax = isl_ast_expr_get_op_type(Expr) == isl_ast_op_max;
|
|
|
|
auto Pred = IsMax ? CmpInst::ICMP_SGT : CmpInst::ICMP_SLT;
|
|
|
|
auto *V = create(isl_ast_expr_get_op_arg(Expr, 0));
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2016-06-03 00:58:12 +08:00
|
|
|
for (int i = 1; i < isl_ast_expr_get_op_n_arg(Expr); ++i) {
|
2016-06-02 19:20:52 +08:00
|
|
|
auto *OpV = create(isl_ast_expr_get_op_arg(Expr, i));
|
2016-06-02 19:15:57 +08:00
|
|
|
unifyTypes(V, OpV);
|
2016-06-02 19:20:52 +08:00
|
|
|
V = Builder.CreateSelect(Builder.CreateICmp(Pred, V, OpV), V, OpV);
|
2014-07-30 05:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:14:30 +08:00
|
|
|
Value *IslExprBuilder::createAccessAddress(isl_ast_expr *Expr) {
|
2014-08-03 09:50:50 +08:00
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"isl ast expression not of type isl_ast_op");
|
|
|
|
assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_access &&
|
|
|
|
"not an access isl ast expression");
|
|
|
|
assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 &&
|
|
|
|
"We need at least two operands to create a member access.");
|
|
|
|
|
2014-10-05 19:32:18 +08:00
|
|
|
Value *Base, *IndexOp, *Access;
|
|
|
|
isl_ast_expr *BaseExpr;
|
|
|
|
isl_id *BaseId;
|
2014-08-03 09:51:59 +08:00
|
|
|
|
2014-10-05 19:32:18 +08:00
|
|
|
BaseExpr = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
BaseId = isl_ast_expr_get_id(BaseExpr);
|
|
|
|
isl_ast_expr_free(BaseExpr);
|
|
|
|
|
|
|
|
const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(BaseId);
|
|
|
|
Base = SAI->getBasePtr();
|
2015-09-05 18:32:56 +08:00
|
|
|
|
|
|
|
if (auto NewBase = GlobalMap.lookup(Base))
|
|
|
|
Base = NewBase;
|
|
|
|
|
2014-08-03 09:50:50 +08:00
|
|
|
assert(Base->getType()->isPointerTy() && "Access base should be a pointer");
|
2015-01-07 15:43:34 +08:00
|
|
|
StringRef BaseName = Base->getName();
|
2014-10-05 19:32:18 +08:00
|
|
|
|
2015-05-20 19:02:12 +08:00
|
|
|
auto PointerTy = PointerType::get(SAI->getElementType(),
|
|
|
|
Base->getType()->getPointerAddressSpace());
|
|
|
|
if (Base->getType() != PointerTy) {
|
|
|
|
Base =
|
|
|
|
Builder.CreateBitCast(Base, PointerTy, "polly.access.cast." + BaseName);
|
|
|
|
}
|
2014-08-03 09:50:50 +08:00
|
|
|
|
2014-10-05 19:33:59 +08:00
|
|
|
IndexOp = nullptr;
|
|
|
|
for (unsigned u = 1, e = isl_ast_expr_get_op_n_arg(Expr); u < e; u++) {
|
|
|
|
Value *NextIndex = create(isl_ast_expr_get_op_arg(Expr, u));
|
|
|
|
assert(NextIndex->getType()->isIntegerTy() &&
|
|
|
|
"Access index should be an integer");
|
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
IndexOp = !IndexOp ? NextIndex : createAdd(IndexOp, NextIndex,
|
|
|
|
"polly.access.add." + BaseName);
|
2014-10-05 19:33:59 +08:00
|
|
|
|
|
|
|
// For every but the last dimension multiply the size, for the last
|
|
|
|
// dimension we can exit the loop.
|
|
|
|
if (u + 1 >= e)
|
|
|
|
break;
|
|
|
|
|
2015-11-10 22:24:21 +08:00
|
|
|
const SCEV *DimSCEV = SAI->getDimensionSize(u);
|
2015-09-05 18:32:56 +08:00
|
|
|
|
2015-10-04 18:18:32 +08:00
|
|
|
llvm::ValueToValueMap Map(GlobalMap.begin(), GlobalMap.end());
|
|
|
|
DimSCEV = SCEVParameterRewriter::rewrite(DimSCEV, SE, Map);
|
2015-08-18 19:56:00 +08:00
|
|
|
Value *DimSize =
|
|
|
|
expandCodeFor(S, SE, DL, "polly", DimSCEV, DimSCEV->getType(),
|
2015-11-07 06:56:54 +08:00
|
|
|
&*Builder.GetInsertPoint());
|
2015-01-14 03:37:59 +08:00
|
|
|
|
2016-05-12 23:12:43 +08:00
|
|
|
IndexOp = createMul(IndexOp, DimSize, "polly.access.mul." + BaseName);
|
2014-10-05 19:33:59 +08:00
|
|
|
}
|
2014-08-03 09:50:50 +08:00
|
|
|
|
2014-10-05 19:32:18 +08:00
|
|
|
Access = Builder.CreateGEP(Base, IndexOp, "polly.access." + BaseName);
|
2014-08-03 09:50:50 +08:00
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return Access;
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:14:30 +08:00
|
|
|
Value *IslExprBuilder::createOpAccess(isl_ast_expr *Expr) {
|
|
|
|
Value *Addr = createAccessAddress(Expr);
|
|
|
|
assert(Addr && "Could not create op access address");
|
|
|
|
return Builder.CreateLoad(Addr, Addr->getName() + ".load");
|
|
|
|
}
|
|
|
|
|
2016-06-06 22:56:17 +08:00
|
|
|
Value *IslExprBuilder::createDiv(Value *LHS, Value *RHS, DivisionMode DM) {
|
|
|
|
auto *ConstRHS = dyn_cast<ConstantInt>(RHS);
|
|
|
|
unsigned UnusedBits = 0;
|
|
|
|
Value *Res = nullptr;
|
|
|
|
|
|
|
|
if (ConstRHS)
|
|
|
|
UnusedBits = ConstRHS->getValue().logBase2();
|
|
|
|
|
|
|
|
if (ConstRHS && ConstRHS->getValue().isPowerOf2() &&
|
|
|
|
ConstRHS->getValue().isNonNegative())
|
|
|
|
Res = Builder.CreateAShr(LHS, UnusedBits, "polly.div.shr");
|
|
|
|
else if (DM == DM_SIGNED)
|
|
|
|
Res = Builder.CreateSDiv(LHS, RHS, "pexp.div", true);
|
|
|
|
else if (DM == DM_UNSIGNED)
|
|
|
|
Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q");
|
|
|
|
else {
|
|
|
|
assert(DM == DM_FLOORED);
|
|
|
|
// TODO: Review code and check that this calculation does not yield
|
|
|
|
// incorrect overflow in some bordercases.
|
|
|
|
//
|
|
|
|
// floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
|
|
|
|
Value *Sum1 = createSub(LHS, RHS, "pexp.fdiv_q.0");
|
|
|
|
Value *One = ConstantInt::get(Sum1->getType(), 1);
|
|
|
|
Value *Sum2 = createAdd(Sum1, One, "pexp.fdiv_q.1");
|
|
|
|
Value *Zero = ConstantInt::get(LHS->getType(), 0);
|
|
|
|
Value *isNegative = Builder.CreateICmpSLT(LHS, Zero, "pexp.fdiv_q.2");
|
|
|
|
unifyTypes(LHS, Sum2);
|
|
|
|
Value *Dividend =
|
|
|
|
Builder.CreateSelect(isNegative, Sum2, LHS, "pexp.fdiv_q.3");
|
|
|
|
unifyTypes(Dividend, RHS);
|
|
|
|
Res = Builder.CreateSDiv(Dividend, RHS, "pexp.fdiv_q.4");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (UnusedBits) {
|
|
|
|
auto RequiredBits = Res->getType()->getPrimitiveSizeInBits() - UnusedBits;
|
|
|
|
Res = Builder.CreateTrunc(Res, Builder.getIntNTy(RequiredBits),
|
|
|
|
"polly.div.trunc");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
|
|
|
|
Value *LHS, *RHS, *Res;
|
|
|
|
isl_ast_op_type OpType;
|
|
|
|
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"isl ast expression not of type isl_ast_op");
|
|
|
|
assert(isl_ast_expr_get_op_n_arg(Expr) == 2 &&
|
|
|
|
"not a binary isl ast expression");
|
|
|
|
|
|
|
|
OpType = isl_ast_expr_get_op_type(Expr);
|
|
|
|
|
2016-04-10 17:50:10 +08:00
|
|
|
LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
|
|
|
|
RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
|
2015-02-02 23:25:09 +08:00
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
// For possibly overflowing operations we will later adjust types but
|
|
|
|
// for others we do it now as we will directly create the operations.
|
2014-07-30 05:06:08 +08:00
|
|
|
switch (OpType) {
|
|
|
|
case isl_ast_op_pdiv_q:
|
|
|
|
case isl_ast_op_pdiv_r:
|
|
|
|
case isl_ast_op_div:
|
|
|
|
case isl_ast_op_fdiv_q:
|
2014-12-08 00:04:29 +08:00
|
|
|
case isl_ast_op_zdiv_r:
|
2016-06-02 19:15:57 +08:00
|
|
|
unifyTypes(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
case isl_ast_op_add:
|
|
|
|
case isl_ast_op_sub:
|
|
|
|
case isl_ast_op_mul:
|
2016-06-02 19:15:57 +08:00
|
|
|
// Do nothing
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("This is no binary isl ast expression");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (OpType) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("This is no binary isl ast expression");
|
|
|
|
case isl_ast_op_add:
|
2016-05-12 23:12:43 +08:00
|
|
|
Res = createAdd(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
case isl_ast_op_sub:
|
2016-05-12 23:12:43 +08:00
|
|
|
Res = createSub(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
case isl_ast_op_mul:
|
2016-05-12 23:12:43 +08:00
|
|
|
Res = createMul(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
case isl_ast_op_div:
|
2016-06-06 22:56:17 +08:00
|
|
|
Res = createDiv(LHS, RHS, DM_SIGNED);
|
2015-05-30 01:08:19 +08:00
|
|
|
break;
|
2014-07-30 05:06:08 +08:00
|
|
|
case isl_ast_op_pdiv_q: // Dividend is non-negative
|
2016-06-06 22:56:17 +08:00
|
|
|
Res = createDiv(LHS, RHS, DM_UNSIGNED);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
2016-06-06 22:56:17 +08:00
|
|
|
case isl_ast_op_fdiv_q: // Round towards -infty
|
|
|
|
Res = createDiv(LHS, RHS, DM_FLOORED);
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
case isl_ast_op_pdiv_r: // Dividend is non-negative
|
2015-05-30 01:08:19 +08:00
|
|
|
Res = Builder.CreateURem(LHS, RHS, "pexp.pdiv_r");
|
|
|
|
break;
|
2014-12-08 00:04:29 +08:00
|
|
|
case isl_ast_op_zdiv_r: // Result only compared against zero
|
2016-06-04 02:51:48 +08:00
|
|
|
Res = Builder.CreateSRem(LHS, RHS, "pexp.zdiv_r");
|
2014-07-30 05:06:08 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select &&
|
|
|
|
"Unsupported unary isl ast expression");
|
|
|
|
Value *LHS, *RHS, *Cond;
|
|
|
|
|
|
|
|
Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
|
2014-10-07 22:37:59 +08:00
|
|
|
if (!Cond->getType()->isIntegerTy(1))
|
|
|
|
Cond = Builder.CreateIsNotNull(Cond);
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
|
|
|
|
RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
|
2016-06-02 19:15:57 +08:00
|
|
|
unifyTypes(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return Builder.CreateSelect(Cond, LHS, RHS);
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expected an isl_ast_expr_op expression");
|
|
|
|
|
|
|
|
Value *LHS, *RHS, *Res;
|
|
|
|
|
2016-04-10 17:50:10 +08:00
|
|
|
auto *Op0 = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
auto *Op1 = isl_ast_expr_get_op_arg(Expr, 1);
|
|
|
|
bool HasNonAddressOfOperand =
|
|
|
|
isl_ast_expr_get_type(Op0) != isl_ast_expr_op ||
|
|
|
|
isl_ast_expr_get_type(Op1) != isl_ast_expr_op ||
|
|
|
|
isl_ast_expr_get_op_type(Op0) != isl_ast_op_address_of ||
|
|
|
|
isl_ast_expr_get_op_type(Op1) != isl_ast_op_address_of;
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2016-04-10 17:50:10 +08:00
|
|
|
LHS = create(Op0);
|
|
|
|
RHS = create(Op1);
|
|
|
|
|
|
|
|
auto *LHSTy = LHS->getType();
|
|
|
|
auto *RHSTy = RHS->getType();
|
|
|
|
bool IsPtrType = LHSTy->isPointerTy() || RHSTy->isPointerTy();
|
|
|
|
bool UseUnsignedCmp = IsPtrType && !HasNonAddressOfOperand;
|
|
|
|
|
|
|
|
auto *PtrAsIntTy = Builder.getIntNTy(DL.getPointerSizeInBits());
|
|
|
|
if (LHSTy->isPointerTy())
|
|
|
|
LHS = Builder.CreatePtrToInt(LHS, PtrAsIntTy);
|
|
|
|
if (RHSTy->isPointerTy())
|
|
|
|
RHS = Builder.CreatePtrToInt(RHS, PtrAsIntTy);
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2016-06-02 19:15:57 +08:00
|
|
|
unifyTypes(LHS, RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2014-09-18 19:14:07 +08:00
|
|
|
isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr);
|
|
|
|
assert(OpType >= isl_ast_op_eq && OpType <= isl_ast_op_gt &&
|
|
|
|
"Unsupported ICmp isl ast expression");
|
|
|
|
assert(isl_ast_op_eq + 4 == isl_ast_op_gt &&
|
|
|
|
"Isl ast op type interface changed");
|
|
|
|
|
|
|
|
CmpInst::Predicate Predicates[5][2] = {
|
|
|
|
{CmpInst::ICMP_EQ, CmpInst::ICMP_EQ},
|
|
|
|
{CmpInst::ICMP_SLE, CmpInst::ICMP_ULE},
|
|
|
|
{CmpInst::ICMP_SLT, CmpInst::ICMP_ULT},
|
|
|
|
{CmpInst::ICMP_SGE, CmpInst::ICMP_UGE},
|
|
|
|
{CmpInst::ICMP_SGT, CmpInst::ICMP_UGT},
|
|
|
|
};
|
|
|
|
|
2016-04-10 17:50:10 +08:00
|
|
|
Res = Builder.CreateICmp(Predicates[OpType - isl_ast_op_eq][UseUnsignedCmp],
|
|
|
|
LHS, RHS);
|
2014-09-18 19:14:07 +08:00
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expected an isl_ast_expr_op expression");
|
|
|
|
|
|
|
|
Value *LHS, *RHS, *Res;
|
|
|
|
isl_ast_op_type OpType;
|
|
|
|
|
|
|
|
OpType = isl_ast_expr_get_op_type(Expr);
|
|
|
|
|
|
|
|
assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
|
|
|
|
"Unsupported isl_ast_op_type");
|
|
|
|
|
|
|
|
LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
|
|
|
|
RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
|
|
|
|
|
|
|
|
// Even though the isl pretty printer prints the expressions as 'exp && exp'
|
|
|
|
// or 'exp || exp', we actually code generate the bitwise expressions
|
|
|
|
// 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
|
|
|
|
// but it is, due to the use of i1 types, otherwise equivalent. The reason
|
|
|
|
// to go for bitwise operations is, that we assume the reduced control flow
|
|
|
|
// will outweight the overhead introduced by evaluating unneeded expressions.
|
|
|
|
// The isl code generation currently does not take advantage of the fact that
|
|
|
|
// the expression after an '||' or '&&' is in some cases not evaluated.
|
|
|
|
// Evaluating it anyways does not cause any undefined behaviour.
|
|
|
|
//
|
|
|
|
// TODO: Document in isl itself, that the unconditionally evaluating the
|
|
|
|
// second part of '||' or '&&' expressions is safe.
|
2014-09-18 19:17:17 +08:00
|
|
|
if (!LHS->getType()->isIntegerTy(1))
|
|
|
|
LHS = Builder.CreateIsNotNull(LHS);
|
|
|
|
if (!RHS->getType()->isIntegerTy(1))
|
|
|
|
RHS = Builder.CreateIsNotNull(RHS);
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
switch (OpType) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unsupported boolean expression");
|
|
|
|
case isl_ast_op_and:
|
|
|
|
Res = Builder.CreateAnd(LHS, RHS);
|
|
|
|
break;
|
|
|
|
case isl_ast_op_or:
|
|
|
|
Res = Builder.CreateOr(LHS, RHS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2015-03-05 02:14:59 +08:00
|
|
|
Value *
|
|
|
|
IslExprBuilder::createOpBooleanConditional(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expected an isl_ast_expr_op expression");
|
|
|
|
|
|
|
|
Value *LHS, *RHS;
|
|
|
|
isl_ast_op_type OpType;
|
|
|
|
|
|
|
|
Function *F = Builder.GetInsertBlock()->getParent();
|
|
|
|
LLVMContext &Context = F->getContext();
|
|
|
|
|
|
|
|
OpType = isl_ast_expr_get_op_type(Expr);
|
|
|
|
|
|
|
|
assert((OpType == isl_ast_op_and_then || OpType == isl_ast_op_or_else) &&
|
|
|
|
"Unsupported isl_ast_op_type");
|
|
|
|
|
|
|
|
auto InsertBB = Builder.GetInsertBlock();
|
|
|
|
auto InsertPoint = Builder.GetInsertPoint();
|
2015-11-07 06:56:54 +08:00
|
|
|
auto NextBB = SplitBlock(InsertBB, &*InsertPoint, &DT, &LI);
|
2015-03-05 02:14:59 +08:00
|
|
|
BasicBlock *CondBB = BasicBlock::Create(Context, "polly.cond", F);
|
|
|
|
LI.changeLoopFor(CondBB, LI.getLoopFor(InsertBB));
|
|
|
|
DT.addNewBlock(CondBB, InsertBB);
|
|
|
|
|
|
|
|
InsertBB->getTerminator()->eraseFromParent();
|
|
|
|
Builder.SetInsertPoint(InsertBB);
|
|
|
|
auto BR = Builder.CreateCondBr(Builder.getTrue(), NextBB, CondBB);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CondBB);
|
|
|
|
Builder.CreateBr(NextBB);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(InsertBB->getTerminator());
|
|
|
|
|
|
|
|
LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
|
|
|
|
if (!LHS->getType()->isIntegerTy(1))
|
|
|
|
LHS = Builder.CreateIsNotNull(LHS);
|
|
|
|
auto LeftBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
if (OpType == isl_ast_op_and || OpType == isl_ast_op_and_then)
|
|
|
|
BR->setCondition(Builder.CreateNeg(LHS));
|
|
|
|
else
|
|
|
|
BR->setCondition(LHS);
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(CondBB->getTerminator());
|
|
|
|
RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
|
|
|
|
if (!RHS->getType()->isIntegerTy(1))
|
|
|
|
RHS = Builder.CreateIsNotNull(RHS);
|
|
|
|
auto RightBB = Builder.GetInsertBlock();
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(NextBB->getTerminator());
|
|
|
|
auto PHI = Builder.CreatePHI(Builder.getInt1Ty(), 2);
|
|
|
|
PHI->addIncoming(OpType == isl_ast_op_and_then ? Builder.getFalse()
|
|
|
|
: Builder.getTrue(),
|
|
|
|
LeftBB);
|
|
|
|
PHI->addIncoming(RHS, RightBB);
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return PHI;
|
|
|
|
}
|
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expression not of type isl_ast_expr_op");
|
|
|
|
switch (isl_ast_expr_get_op_type(Expr)) {
|
|
|
|
case isl_ast_op_error:
|
|
|
|
case isl_ast_op_cond:
|
|
|
|
case isl_ast_op_call:
|
|
|
|
case isl_ast_op_member:
|
|
|
|
llvm_unreachable("Unsupported isl ast expression");
|
2014-08-03 09:50:50 +08:00
|
|
|
case isl_ast_op_access:
|
|
|
|
return createOpAccess(Expr);
|
2014-07-30 05:06:08 +08:00
|
|
|
case isl_ast_op_max:
|
|
|
|
case isl_ast_op_min:
|
|
|
|
return createOpNAry(Expr);
|
|
|
|
case isl_ast_op_add:
|
|
|
|
case isl_ast_op_sub:
|
|
|
|
case isl_ast_op_mul:
|
|
|
|
case isl_ast_op_div:
|
|
|
|
case isl_ast_op_fdiv_q: // Round towards -infty
|
|
|
|
case isl_ast_op_pdiv_q: // Dividend is non-negative
|
|
|
|
case isl_ast_op_pdiv_r: // Dividend is non-negative
|
2014-12-08 00:04:29 +08:00
|
|
|
case isl_ast_op_zdiv_r: // Result only compared against zero
|
2014-07-30 05:06:08 +08:00
|
|
|
return createOpBin(Expr);
|
|
|
|
case isl_ast_op_minus:
|
|
|
|
return createOpUnary(Expr);
|
|
|
|
case isl_ast_op_select:
|
|
|
|
return createOpSelect(Expr);
|
|
|
|
case isl_ast_op_and:
|
|
|
|
case isl_ast_op_or:
|
|
|
|
return createOpBoolean(Expr);
|
2015-03-05 02:14:59 +08:00
|
|
|
case isl_ast_op_and_then:
|
|
|
|
case isl_ast_op_or_else:
|
|
|
|
return createOpBooleanConditional(Expr);
|
2014-07-30 05:06:08 +08:00
|
|
|
case isl_ast_op_eq:
|
|
|
|
case isl_ast_op_le:
|
|
|
|
case isl_ast_op_lt:
|
|
|
|
case isl_ast_op_ge:
|
|
|
|
case isl_ast_op_gt:
|
|
|
|
return createOpICmp(Expr);
|
2014-09-18 19:14:30 +08:00
|
|
|
case isl_ast_op_address_of:
|
|
|
|
return createOpAddressOf(Expr);
|
2014-07-30 05:06:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unsupported isl_ast_expr_op kind.");
|
|
|
|
}
|
|
|
|
|
2014-09-18 19:14:30 +08:00
|
|
|
Value *IslExprBuilder::createOpAddressOf(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
|
|
|
|
"Expected an isl_ast_expr_op expression.");
|
|
|
|
assert(isl_ast_expr_get_op_n_arg(Expr) == 1 && "Address of should be unary.");
|
|
|
|
|
|
|
|
isl_ast_expr *Op = isl_ast_expr_get_op_arg(Expr, 0);
|
|
|
|
assert(isl_ast_expr_get_type(Op) == isl_ast_expr_op &&
|
|
|
|
"Expected address of operator to be an isl_ast_expr_op expression.");
|
|
|
|
assert(isl_ast_expr_get_op_type(Op) == isl_ast_op_access &&
|
|
|
|
"Expected address of operator to be an access expression.");
|
|
|
|
|
|
|
|
Value *V = createAccessAddress(Op);
|
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id &&
|
|
|
|
"Expression not of type isl_ast_expr_ident");
|
|
|
|
|
|
|
|
isl_id *Id;
|
|
|
|
Value *V;
|
|
|
|
|
|
|
|
Id = isl_ast_expr_get_id(Expr);
|
|
|
|
|
|
|
|
assert(IDToValue.count(Id) && "Identifier not found");
|
|
|
|
|
|
|
|
V = IDToValue[Id];
|
2015-11-08 03:46:04 +08:00
|
|
|
if (!V)
|
2016-06-06 17:57:41 +08:00
|
|
|
V = UndefValue::get(Builder.getInt1Ty());
|
2014-07-30 05:06:08 +08:00
|
|
|
|
2016-04-10 17:50:10 +08:00
|
|
|
if (V->getType()->isPointerTy())
|
|
|
|
V = Builder.CreatePtrToInt(V, Builder.getIntNTy(DL.getPointerSizeInBits()));
|
|
|
|
|
2015-03-11 06:35:43 +08:00
|
|
|
assert(V && "Unknown parameter id found");
|
|
|
|
|
2014-07-30 05:06:08 +08:00
|
|
|
isl_id_free(Id);
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
|
|
|
|
assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int &&
|
|
|
|
"Expression not of type isl_ast_expr_int");
|
2015-11-03 08:26:22 +08:00
|
|
|
|
2016-06-06 17:57:41 +08:00
|
|
|
auto *Val = isl_ast_expr_get_val(Expr);
|
|
|
|
auto *V = ConstantInt::get(Builder.getContext(), APIntFromVal(Val));
|
2014-07-30 05:06:08 +08:00
|
|
|
|
|
|
|
isl_ast_expr_free(Expr);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
|
|
|
|
switch (isl_ast_expr_get_type(Expr)) {
|
|
|
|
case isl_ast_expr_error:
|
|
|
|
llvm_unreachable("Code generation error");
|
|
|
|
case isl_ast_expr_op:
|
|
|
|
return createOp(Expr);
|
|
|
|
case isl_ast_expr_id:
|
|
|
|
return createId(Expr);
|
|
|
|
case isl_ast_expr_int:
|
|
|
|
return createInt(Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("Unexpected enum value");
|
|
|
|
}
|