2015-08-12 18:19:50 +08:00
|
|
|
//===--------- SCEVAffinator.cpp - Create Scops from LLVM IR -------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Create a polyhedral description for a SCEV value.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/Support/SCEVAffinator.h"
|
2016-04-12 21:28:39 +08:00
|
|
|
#include "polly/Options.h"
|
2015-08-12 18:19:50 +08:00
|
|
|
#include "polly/ScopInfo.h"
|
|
|
|
#include "polly/Support/GICHelper.h"
|
|
|
|
#include "polly/Support/SCEVValidator.h"
|
2015-09-25 17:49:19 +08:00
|
|
|
#include "polly/Support/ScopHelper.h"
|
2015-08-12 18:19:50 +08:00
|
|
|
#include "isl/aff.h"
|
2015-09-25 17:49:19 +08:00
|
|
|
#include "isl/local_space.h"
|
2015-08-12 18:19:50 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/val.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
2016-04-12 21:28:39 +08:00
|
|
|
static cl::opt<bool> IgnoreIntegerWrapping(
|
|
|
|
"polly-ignore-integer-wrapping",
|
|
|
|
cl::desc("Do not build run-time checks to proof absence of integer "
|
|
|
|
"wrapping"),
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory));
|
|
|
|
|
2016-03-27 00:17:00 +08:00
|
|
|
// The maximal number of basic sets we allow during the construction of a
|
|
|
|
// piecewise affine function. More complex ones will result in very high
|
|
|
|
// compile time.
|
2016-05-02 20:25:18 +08:00
|
|
|
static int const MaxDisjunctionsInPwAff = 100;
|
2016-03-27 00:17:00 +08:00
|
|
|
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
// The maximal number of bits for which a zero-extend is modeled precisely.
|
|
|
|
static unsigned const MaxZextSmallBitWidth = 7;
|
|
|
|
|
2016-05-12 23:13:49 +08:00
|
|
|
// The maximal number of bits for which a truncate is modeled precisely.
|
|
|
|
static unsigned const MaxTruncateSmallBitWidth = 31;
|
|
|
|
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
/// @brief Return true if a zero-extend from @p Width bits is precisely modeled.
|
|
|
|
static bool isPreciseZeroExtend(unsigned Width) {
|
|
|
|
return Width <= MaxZextSmallBitWidth;
|
|
|
|
}
|
|
|
|
|
2016-05-12 23:13:49 +08:00
|
|
|
/// @brief Return true if a truncate from @p Width bits is precisely modeled.
|
|
|
|
static bool isPreciseTruncate(unsigned Width) {
|
|
|
|
return Width <= MaxTruncateSmallBitWidth;
|
|
|
|
}
|
|
|
|
|
2016-03-27 00:17:00 +08:00
|
|
|
/// @brief Add the number of basic sets in @p Domain to @p User
|
|
|
|
static isl_stat addNumBasicSets(isl_set *Domain, isl_aff *Aff, void *User) {
|
|
|
|
auto *NumBasicSets = static_cast<unsigned *>(User);
|
|
|
|
*NumBasicSets += isl_set_n_basic_set(Domain);
|
|
|
|
isl_set_free(Domain);
|
|
|
|
isl_aff_free(Aff);
|
|
|
|
return isl_stat_ok;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
/// @brief Helper to free a PWACtx object.
|
|
|
|
static void freePWACtx(__isl_take PWACtx &PWAC) {
|
|
|
|
isl_pw_aff_free(PWAC.first);
|
|
|
|
isl_set_free(PWAC.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @brief Helper to copy a PWACtx object.
|
|
|
|
static __isl_give PWACtx copyPWACtx(const __isl_keep PWACtx &PWAC) {
|
|
|
|
return std::make_pair(isl_pw_aff_copy(PWAC.first), isl_set_copy(PWAC.second));
|
|
|
|
}
|
|
|
|
|
2016-05-02 18:44:20 +08:00
|
|
|
/// @brief Determine if @p PWAC is too complex to continue.
|
2016-03-27 00:17:00 +08:00
|
|
|
///
|
2016-04-23 22:31:17 +08:00
|
|
|
/// Note that @p PWAC will be "free" (deallocated) if this function returns
|
|
|
|
/// true, but not if this function returns false.
|
2016-05-02 18:44:20 +08:00
|
|
|
static bool isTooComplex(PWACtx &PWAC) {
|
2016-03-27 00:17:00 +08:00
|
|
|
unsigned NumBasicSets = 0;
|
2016-04-23 22:31:17 +08:00
|
|
|
isl_pw_aff_foreach_piece(PWAC.first, addNumBasicSets, &NumBasicSets);
|
2016-05-02 20:25:18 +08:00
|
|
|
if (NumBasicSets <= MaxDisjunctionsInPwAff)
|
2016-03-27 00:17:00 +08:00
|
|
|
return false;
|
2016-04-23 22:31:17 +08:00
|
|
|
freePWACtx(PWAC);
|
2016-03-27 00:17:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-12 17:33:47 +08:00
|
|
|
/// @brief Return the flag describing the possible wrapping of @p Expr.
|
|
|
|
static SCEV::NoWrapFlags getNoWrapFlags(const SCEV *Expr) {
|
|
|
|
if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
|
|
|
|
return NAry->getNoWrapFlags();
|
|
|
|
return SCEV::NoWrapMask;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
static void combine(__isl_keep PWACtx &PWAC0, const __isl_take PWACtx &PWAC1,
|
|
|
|
isl_pw_aff *(Fn)(isl_pw_aff *, isl_pw_aff *)) {
|
|
|
|
PWAC0.first = Fn(PWAC0.first, PWAC1.first);
|
|
|
|
PWAC0.second = isl_set_union(PWAC0.second, PWAC1.second);
|
|
|
|
}
|
|
|
|
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
/// @brief Set the possible wrapping of @p Expr to @p Flags.
|
|
|
|
static const SCEV *setNoWrapFlags(ScalarEvolution &SE, const SCEV *Expr,
|
|
|
|
SCEV::NoWrapFlags Flags) {
|
|
|
|
auto *NAry = dyn_cast<SCEVNAryExpr>(Expr);
|
|
|
|
if (!NAry)
|
|
|
|
return Expr;
|
|
|
|
|
|
|
|
SmallVector<const SCEV *, 8> Ops(NAry->op_begin(), NAry->op_end());
|
|
|
|
switch (Expr->getSCEVType()) {
|
|
|
|
case scAddExpr:
|
|
|
|
return SE.getAddExpr(Ops, Flags);
|
|
|
|
case scMulExpr:
|
|
|
|
return SE.getMulExpr(Ops, Flags);
|
|
|
|
case scAddRecExpr:
|
|
|
|
return SE.getAddRecExpr(Ops, cast<SCEVAddRecExpr>(Expr)->getLoop(), Flags);
|
|
|
|
default:
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:52:30 +08:00
|
|
|
static __isl_give isl_pw_aff *getWidthExpValOnDomain(unsigned Width,
|
|
|
|
__isl_take isl_set *Dom) {
|
|
|
|
auto *Ctx = isl_set_get_ctx(Dom);
|
|
|
|
auto *WidthVal = isl_val_int_from_ui(Ctx, Width);
|
|
|
|
auto *ExpVal = isl_val_2exp(WidthVal);
|
|
|
|
return isl_pw_aff_val_on_domain(Dom, ExpVal);
|
|
|
|
}
|
|
|
|
|
2016-03-04 06:10:47 +08:00
|
|
|
SCEVAffinator::SCEVAffinator(Scop *S, LoopInfo &LI)
|
2016-05-23 20:38:05 +08:00
|
|
|
: S(S), Ctx(S->getIslCtx()), SE(*S->getSE()), LI(LI),
|
|
|
|
TD(S->getFunction().getParent()->getDataLayout()) {}
|
2015-08-12 18:19:50 +08:00
|
|
|
|
2015-08-12 18:46:33 +08:00
|
|
|
SCEVAffinator::~SCEVAffinator() {
|
2016-04-23 22:31:17 +08:00
|
|
|
for (auto &CachedPair : CachedExpressions)
|
|
|
|
freePWACtx(CachedPair.second);
|
|
|
|
}
|
|
|
|
|
2016-06-06 18:07:40 +08:00
|
|
|
Loop *SCEVAffinator::getScope() { return BB ? LI.getLoopFor(BB) : nullptr; }
|
|
|
|
|
2016-05-10 19:45:46 +08:00
|
|
|
void SCEVAffinator::interpretAsUnsigned(__isl_keep PWACtx &PWAC,
|
|
|
|
unsigned Width) {
|
|
|
|
auto *PWA = PWAC.first;
|
|
|
|
auto *NonNegDom = isl_pw_aff_nonneg_set(isl_pw_aff_copy(PWA));
|
|
|
|
auto *NonNegPWA = isl_pw_aff_intersect_domain(isl_pw_aff_copy(PWA),
|
|
|
|
isl_set_copy(NonNegDom));
|
|
|
|
auto *ExpPWA = getWidthExpValOnDomain(Width, isl_set_complement(NonNegDom));
|
|
|
|
PWAC.first = isl_pw_aff_union_add(NonNegPWA, isl_pw_aff_add(PWA, ExpPWA));
|
|
|
|
}
|
|
|
|
|
2016-04-29 18:44:41 +08:00
|
|
|
void SCEVAffinator::takeNonNegativeAssumption(PWACtx &PWAC) {
|
|
|
|
auto *NegPWA = isl_pw_aff_neg(isl_pw_aff_copy(PWAC.first));
|
|
|
|
auto *NegDom = isl_pw_aff_pos_set(NegPWA);
|
|
|
|
PWAC.second = isl_set_union(PWAC.second, isl_set_copy(NegDom));
|
|
|
|
auto *Restriction = BB ? NegDom : isl_set_params(NegDom);
|
|
|
|
auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
|
|
|
|
S->recordAssumption(UNSIGNED, Restriction, DL, AS_RESTRICTION, BB);
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::getPWACtxFromPWA(__isl_take isl_pw_aff *PWA) {
|
|
|
|
return std::make_pair(
|
|
|
|
PWA, isl_set_empty(isl_space_set_alloc(Ctx, 0, NumIterators)));
|
2015-08-12 18:46:33 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::getPwAff(const SCEV *Expr, BasicBlock *BB) {
|
2015-09-16 06:49:04 +08:00
|
|
|
this->BB = BB;
|
|
|
|
|
|
|
|
if (BB) {
|
|
|
|
auto *DC = S->getDomainConditions(BB);
|
|
|
|
NumIterators = isl_set_n_dim(DC);
|
|
|
|
isl_set_free(DC);
|
|
|
|
} else
|
2015-08-12 23:27:16 +08:00
|
|
|
NumIterators = 0;
|
2015-08-12 18:19:50 +08:00
|
|
|
|
2016-06-06 18:07:40 +08:00
|
|
|
auto *Scope = getScope();
|
2016-05-23 20:38:05 +08:00
|
|
|
S->addParams(getParamsInAffineExpr(&S->getRegion(), Scope, Expr, SE));
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
return visit(Expr);
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::checkForWrapping(const SCEV *Expr,
|
|
|
|
PWACtx PWAC) const {
|
2015-09-16 06:52:53 +08:00
|
|
|
// If the SCEV flags do contain NSW (no signed wrap) then PWA already
|
|
|
|
// represents Expr in modulo semantic (it is not allowed to overflow), thus we
|
|
|
|
// are done. Otherwise, we will compute:
|
|
|
|
// PWA = ((PWA + 2^(n-1)) mod (2 ^ n)) - 2^(n-1)
|
|
|
|
// whereas n is the number of bits of the Expr, hence:
|
|
|
|
// n = bitwidth(ExprType)
|
|
|
|
|
2016-04-12 21:28:39 +08:00
|
|
|
if (IgnoreIntegerWrapping || (getNoWrapFlags(Expr) & SCEV::FlagNSW))
|
2016-04-23 22:31:17 +08:00
|
|
|
return PWAC;
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
auto *PWA = PWAC.first;
|
2016-04-12 21:28:39 +08:00
|
|
|
auto *PWAMod = addModuloSemantic(isl_pw_aff_copy(PWA), Expr->getType());
|
|
|
|
auto *NotEqualSet = isl_pw_aff_ne_set(isl_pw_aff_copy(PWA), PWAMod);
|
2016-04-23 22:31:17 +08:00
|
|
|
PWAC.second = isl_set_union(PWAC.second, isl_set_copy(NotEqualSet));
|
2016-06-06 18:06:07 +08:00
|
|
|
PWAC.second = isl_set_coalesce(PWAC.second);
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2016-04-12 21:28:39 +08:00
|
|
|
const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
|
|
|
|
NotEqualSet = BB ? NotEqualSet : isl_set_params(NotEqualSet);
|
2016-06-06 18:06:07 +08:00
|
|
|
NotEqualSet = isl_set_coalesce(NotEqualSet);
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2016-04-12 21:28:39 +08:00
|
|
|
if (isl_set_is_empty(NotEqualSet))
|
|
|
|
isl_set_free(NotEqualSet);
|
|
|
|
else
|
|
|
|
S->recordAssumption(WRAPPING, NotEqualSet, Loc, AS_RESTRICTION, BB);
|
2016-04-23 22:31:17 +08:00
|
|
|
|
|
|
|
return PWAC;
|
2015-09-16 06:52:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__isl_give isl_pw_aff *
|
|
|
|
SCEVAffinator::addModuloSemantic(__isl_take isl_pw_aff *PWA,
|
|
|
|
Type *ExprType) const {
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
unsigned Width = TD.getTypeSizeInBits(ExprType);
|
2015-09-16 06:52:53 +08:00
|
|
|
isl_ctx *Ctx = isl_pw_aff_get_ctx(PWA);
|
|
|
|
|
|
|
|
isl_val *ModVal = isl_val_int_from_ui(Ctx, Width);
|
|
|
|
ModVal = isl_val_2exp(ModVal);
|
|
|
|
|
|
|
|
isl_set *Domain = isl_pw_aff_domain(isl_pw_aff_copy(PWA));
|
2016-04-29 19:52:30 +08:00
|
|
|
isl_pw_aff *AddPW = getWidthExpValOnDomain(Width - 1, Domain);
|
2015-09-16 06:52:53 +08:00
|
|
|
|
|
|
|
PWA = isl_pw_aff_add(PWA, isl_pw_aff_copy(AddPW));
|
|
|
|
PWA = isl_pw_aff_mod_val(PWA, ModVal);
|
|
|
|
PWA = isl_pw_aff_sub(PWA, AddPW);
|
|
|
|
|
|
|
|
return PWA;
|
|
|
|
}
|
|
|
|
|
2015-09-21 00:59:23 +08:00
|
|
|
bool SCEVAffinator::hasNSWAddRecForLoop(Loop *L) const {
|
|
|
|
for (const auto &CachedPair : CachedExpressions) {
|
|
|
|
auto *AddRec = dyn_cast<SCEVAddRecExpr>(CachedPair.first.first);
|
|
|
|
if (!AddRec)
|
|
|
|
continue;
|
|
|
|
if (AddRec->getLoop() != L)
|
|
|
|
continue;
|
|
|
|
if (AddRec->getNoWrapFlags() & SCEV::FlagNSW)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visit(const SCEV *Expr) {
|
2015-08-12 18:46:33 +08:00
|
|
|
|
2015-09-16 06:49:04 +08:00
|
|
|
auto Key = std::make_pair(Expr, BB);
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx PWAC = CachedExpressions[Key];
|
|
|
|
if (PWAC.first)
|
|
|
|
return copyPWACtx(PWAC);
|
2015-08-12 18:46:33 +08:00
|
|
|
|
2016-06-06 18:06:53 +08:00
|
|
|
auto ConstantAndLeftOverPair = extractConstantFactor(Expr, SE);
|
2016-02-15 06:30:56 +08:00
|
|
|
auto *Factor = ConstantAndLeftOverPair.first;
|
|
|
|
Expr = ConstantAndLeftOverPair.second;
|
|
|
|
|
2015-08-12 18:19:50 +08:00
|
|
|
// In case the scev is a valid parameter, we do not further analyze this
|
|
|
|
// expression, but create a new parameter in the isl_pw_aff. This allows us
|
|
|
|
// to treat subexpressions that we cannot translate into an piecewise affine
|
|
|
|
// expression, as constant parameters of the piecewise affine expression.
|
|
|
|
if (isl_id *Id = S->getIdForParam(Expr)) {
|
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx, 1, NumIterators);
|
|
|
|
Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
|
|
|
|
|
|
|
|
isl_set *Domain = isl_set_universe(isl_space_copy(Space));
|
|
|
|
isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
|
|
|
|
Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
PWAC = getPWACtxFromPWA(isl_pw_aff_alloc(Domain, Affine));
|
2016-02-15 06:30:56 +08:00
|
|
|
} else {
|
2016-04-23 22:31:17 +08:00
|
|
|
PWAC = SCEVVisitor<SCEVAffinator, PWACtx>::visit(Expr);
|
|
|
|
PWAC = checkForWrapping(Expr, PWAC);
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-06-03 00:57:12 +08:00
|
|
|
if (!Factor->getType()->isIntegerTy(1))
|
|
|
|
combine(PWAC, visitConstant(Factor), isl_pw_aff_mul);
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
// For compile time reasons we need to simplify the PWAC before we cache and
|
2015-09-16 06:52:53 +08:00
|
|
|
// return it.
|
2016-04-23 22:31:17 +08:00
|
|
|
PWAC.first = isl_pw_aff_coalesce(PWAC.first);
|
|
|
|
PWAC = checkForWrapping(Key.first, PWAC);
|
2016-04-12 21:28:39 +08:00
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
CachedExpressions[Key] = copyPWACtx(PWAC);
|
|
|
|
return PWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
|
2015-08-12 18:19:50 +08:00
|
|
|
ConstantInt *Value = Expr->getValue();
|
|
|
|
isl_val *v;
|
|
|
|
|
|
|
|
// LLVM does not define if an integer value is interpreted as a signed or
|
|
|
|
// unsigned value. Hence, without further information, it is unknown how
|
|
|
|
// this value needs to be converted to GMP. At the moment, we only support
|
|
|
|
// signed operations. So we just interpret it as signed. Later, there are
|
|
|
|
// two options:
|
|
|
|
//
|
|
|
|
// 1. We always interpret any value as signed and convert the values on
|
|
|
|
// demand.
|
|
|
|
// 2. We pass down the signedness of the calculation and use it to interpret
|
|
|
|
// this constant correctly.
|
|
|
|
v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
|
|
|
|
|
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx, 0, NumIterators);
|
|
|
|
isl_local_space *ls = isl_local_space_from_space(Space);
|
2016-04-23 22:31:17 +08:00
|
|
|
return getPWACtxFromPWA(isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v)));
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx
|
2015-08-12 18:19:50 +08:00
|
|
|
SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
|
2016-05-12 23:13:49 +08:00
|
|
|
// Truncate operations are basically modulo operations, thus we can
|
|
|
|
// model them that way. However, for large types we assume the operand
|
|
|
|
// to fit in the new type size instead of introducing a modulo with a very
|
|
|
|
// large constant.
|
|
|
|
|
|
|
|
auto *Op = Expr->getOperand();
|
|
|
|
auto OpPWAC = visit(Op);
|
|
|
|
|
|
|
|
unsigned Width = TD.getTypeSizeInBits(Expr->getType());
|
|
|
|
bool Precise = isPreciseTruncate(Width);
|
|
|
|
|
|
|
|
if (Precise) {
|
|
|
|
OpPWAC.first = addModuloSemantic(OpPWAC.first, Expr->getType());
|
|
|
|
return OpPWAC;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto *Dom = isl_pw_aff_domain(isl_pw_aff_copy(OpPWAC.first));
|
|
|
|
auto *ExpPWA = getWidthExpValOnDomain(Width - 1, Dom);
|
|
|
|
auto *GreaterDom =
|
|
|
|
isl_pw_aff_ge_set(isl_pw_aff_copy(OpPWAC.first), isl_pw_aff_copy(ExpPWA));
|
|
|
|
auto *SmallerDom =
|
|
|
|
isl_pw_aff_lt_set(isl_pw_aff_copy(OpPWAC.first), isl_pw_aff_neg(ExpPWA));
|
|
|
|
auto *OutOfBoundsDom = isl_set_union(SmallerDom, GreaterDom);
|
|
|
|
OpPWAC.second = isl_set_union(OpPWAC.second, isl_set_copy(OutOfBoundsDom));
|
|
|
|
S->recordAssumption(UNSIGNED, OutOfBoundsDom, DebugLoc(), AS_RESTRICTION, BB);
|
|
|
|
|
|
|
|
return OpPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx
|
2015-08-12 18:19:50 +08:00
|
|
|
SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
// A zero-extended value can be interpreted as a piecewise defined signed
|
|
|
|
// value. If the value was non-negative it stays the same, otherwise it
|
|
|
|
// is the sum of the original value and 2^n where n is the bit-width of
|
|
|
|
// the original (or operand) type. Examples:
|
|
|
|
// zext i8 127 to i32 -> { [127] }
|
|
|
|
// zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
|
|
|
|
// zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
|
|
|
|
//
|
|
|
|
// However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
|
|
|
|
// truncate) to represent some forms of modulo computation. The left-hand side
|
|
|
|
// of the condition in the code below would result in the SCEV
|
|
|
|
// "zext i1 <false, +, true>for.body" which is just another description
|
|
|
|
// of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
|
|
|
|
//
|
|
|
|
// for (i = 0; i < N; i++)
|
|
|
|
// if (i & 1 != 0 /* == i % 2 */)
|
|
|
|
// /* do something */
|
|
|
|
//
|
|
|
|
// If we do not make the modulo explicit but only use the mechanism described
|
|
|
|
// above we will get the very restrictive assumption "N < 3", because for all
|
|
|
|
// values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
|
|
|
|
// Alternatively, we can make the modulo in the operand explicit in the
|
|
|
|
// resulting piecewise function and thereby avoid the assumption on N. For the
|
|
|
|
// example this would result in the following piecewise affine function:
|
|
|
|
// { [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
|
|
|
|
// [i0] -> [(0)] : 2*floor((i0)/2) = i0 }
|
|
|
|
// To this end we can first determine if the (immediate) operand of the
|
|
|
|
// zero-extend can wrap and, in case it might, we will use explicit modulo
|
|
|
|
// semantic to compute the result instead of emitting non-wrapping
|
|
|
|
// assumptions.
|
|
|
|
//
|
|
|
|
// Note that operands with large bit-widths are less likely to be negative
|
|
|
|
// because it would result in a very large access offset or loop bound after
|
|
|
|
// the zero-extend. To this end one can optimistically assume the operand to
|
|
|
|
// be positive and avoid the piecewise definition if the bit-width is bigger
|
|
|
|
// than some threshold (here MaxZextSmallBitWidth).
|
|
|
|
//
|
|
|
|
// We choose to go with a hybrid solution of all modeling techniques described
|
|
|
|
// above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
|
|
|
|
// wrapping explicitly and use a piecewise defined function. However, if the
|
|
|
|
// bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
|
|
|
|
// assumptions and assume the "former negative" piece will not exist.
|
|
|
|
|
|
|
|
auto *Op = Expr->getOperand();
|
|
|
|
unsigned Width = TD.getTypeSizeInBits(Op->getType());
|
|
|
|
|
|
|
|
bool Precise = isPreciseZeroExtend(Width);
|
|
|
|
|
|
|
|
auto Flags = getNoWrapFlags(Op);
|
|
|
|
auto NoWrapFlags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
|
|
|
|
bool OpCanWrap = Precise && !(Flags & SCEV::FlagNSW);
|
|
|
|
if (OpCanWrap)
|
|
|
|
Op = setNoWrapFlags(SE, Op, NoWrapFlags);
|
|
|
|
|
|
|
|
auto OpPWAC = visit(Op);
|
|
|
|
if (OpCanWrap)
|
2016-05-12 23:13:49 +08:00
|
|
|
OpPWAC.first = addModuloSemantic(OpPWAC.first, Op->getType());
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
|
|
|
|
// If the width is to big we assume the negative part does not occur.
|
|
|
|
if (!Precise) {
|
2016-04-29 18:44:41 +08:00
|
|
|
takeNonNegativeAssumption(OpPWAC);
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
return OpPWAC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the width is small build the piece for the non-negative part and
|
|
|
|
// the one for the negative part and unify them.
|
2016-05-10 19:45:46 +08:00
|
|
|
interpretAsUnsigned(OpPWAC, Width);
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
return OpPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx
|
2015-08-12 18:19:50 +08:00
|
|
|
SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
|
Model zext-extend instructions
A zero-extended value can be interpreted as a piecewise defined signed
value. If the value was non-negative it stays the same, otherwise it
is the sum of the original value and 2^n where n is the bit-width of
the original (or operand) type. Examples:
zext i8 127 to i32 -> { [127] }
zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
truncate) to represent some forms of modulo computation. The left-hand side
of the condition in the code below would result in the SCEV
"zext i1 <false, +, true>for.body" which is just another description
of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
for (i = 0; i < N; i++)
if (i & 1 != 0 /* == i % 2 */)
/* do something */
If we do not make the modulo explicit but only use the mechanism described
above we will get the very restrictive assumption "N < 3", because for all
values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
Alternatively, we can make the modulo in the operand explicit in the
resulting piecewise function and thereby avoid the assumption on N. For the
example this would result in the following piecewise affine function:
{ [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
[i0] -> [(0)] : 2*floor((i0)/2) = i0 }
To this end we can first determine if the (immediate) operand of the
zero-extend can wrap and, in case it might, we will use explicit modulo
semantic to compute the result instead of emitting non-wrapping assumptions.
Note that operands with large bit-widths are less likely to be negative
because it would result in a very large access offset or loop bound after the
zero-extend. To this end one can optimistically assume the operand to be
positive and avoid the piecewise definition if the bit-width is bigger than
some threshold (here MaxZextSmallBitWidth).
We choose to go with a hybrid solution of all modeling techniques described
above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
wrapping explicitly and use a piecewise defined function. However, if the
bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
assumptions and assume the "former negative" piece will not exist.
llvm-svn: 267408
2016-04-25 22:01:36 +08:00
|
|
|
// As all values are represented as signed, a sign extension is a noop.
|
2015-08-12 18:19:50 +08:00
|
|
|
return visit(Expr->getOperand());
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
|
|
|
|
PWACtx Sum = visit(Expr->getOperand(0));
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2016-04-23 22:31:17 +08:00
|
|
|
combine(Sum, visit(Expr->getOperand(i)), isl_pw_aff_add);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Sum))
|
2016-04-23 22:31:17 +08:00
|
|
|
return std::make_pair(nullptr, nullptr);
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Sum;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
|
|
|
|
PWACtx Prod = visit(Expr->getOperand(0));
|
2016-04-08 18:27:40 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2016-04-23 22:31:17 +08:00
|
|
|
combine(Prod, visit(Expr->getOperand(i)), isl_pw_aff_mul);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Prod))
|
2016-04-23 22:31:17 +08:00
|
|
|
return std::make_pair(nullptr, nullptr);
|
2016-04-08 18:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Prod;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
2015-08-12 18:19:50 +08:00
|
|
|
assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
|
|
|
|
|
|
|
|
auto Flags = Expr->getNoWrapFlags();
|
|
|
|
|
|
|
|
// Directly generate isl_pw_aff for Expr if 'start' is zero.
|
|
|
|
if (Expr->getStart()->isZero()) {
|
2016-05-23 20:40:48 +08:00
|
|
|
assert(S->contains(Expr->getLoop()) &&
|
2015-08-12 18:19:50 +08:00
|
|
|
"Scop does not contain the loop referenced in this AddRec");
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx Step = visit(Expr->getOperand(1));
|
2015-08-12 18:19:50 +08:00
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx, 0, NumIterators);
|
|
|
|
isl_local_space *LocalSpace = isl_local_space_from_space(Space);
|
|
|
|
|
2015-08-27 14:53:52 +08:00
|
|
|
unsigned loopDimension = S->getRelativeLoopDepth(Expr->getLoop());
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
isl_aff *LAff = isl_aff_set_coefficient_si(
|
|
|
|
isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
|
|
|
|
isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
Step.first = isl_pw_aff_mul(Step.first, LPwAff);
|
|
|
|
return Step;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
|
|
|
|
// if 'start' is not zero.
|
|
|
|
// TODO: Using the original SCEV no-wrap flags is not always safe, however
|
|
|
|
// as our code generation is reordering the expression anyway it doesn't
|
|
|
|
// really matter.
|
|
|
|
const SCEV *ZeroStartExpr =
|
|
|
|
SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
|
|
|
|
Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx Result = visit(ZeroStartExpr);
|
|
|
|
PWACtx Start = visit(Expr->getStart());
|
|
|
|
combine(Result, Start, isl_pw_aff_add);
|
|
|
|
return Result;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
|
|
|
PWACtx Max = visit(Expr->getOperand(0));
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2016-04-23 22:31:17 +08:00
|
|
|
combine(Max, visit(Expr->getOperand(i)), isl_pw_aff_max);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Max))
|
2016-04-23 22:31:17 +08:00
|
|
|
return std::make_pair(nullptr, nullptr);
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
|
2015-08-12 18:19:50 +08:00
|
|
|
llvm_unreachable("SCEVUMaxExpr not yet supported");
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:53:35 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
|
|
|
|
// The handling of unsigned division is basically the same as for signed
|
|
|
|
// division, except the interpretation of the operands. As the divisor
|
|
|
|
// has to be constant in both cases we can simply interpret it as an
|
|
|
|
// unsigned value without additional complexity in the representation.
|
|
|
|
// For the dividend we could choose from the different representation
|
|
|
|
// schemes introduced for zero-extend operations but for now we will
|
|
|
|
// simply use an assumption.
|
|
|
|
auto *Dividend = Expr->getLHS();
|
|
|
|
auto *Divisor = Expr->getRHS();
|
|
|
|
assert(isa<SCEVConstant>(Divisor) &&
|
|
|
|
"UDiv is no parameter but has a non-constant RHS.");
|
|
|
|
|
|
|
|
auto DividendPWAC = visit(Dividend);
|
|
|
|
auto DivisorPWAC = visit(Divisor);
|
|
|
|
|
|
|
|
if (SE.isKnownNegative(Divisor)) {
|
|
|
|
// Interpret negative divisors unsigned. This is a special case of the
|
|
|
|
// piece-wise defined value described for zero-extends as we already know
|
|
|
|
// the actual value of the constant divisor.
|
|
|
|
unsigned Width = TD.getTypeSizeInBits(Expr->getType());
|
|
|
|
auto *DivisorDom = isl_pw_aff_domain(isl_pw_aff_copy(DivisorPWAC.first));
|
|
|
|
auto *WidthExpPWA = getWidthExpValOnDomain(Width, DivisorDom);
|
|
|
|
DivisorPWAC.first = isl_pw_aff_add(DivisorPWAC.first, WidthExpPWA);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: One can represent the dividend as piece-wise function to be more
|
|
|
|
// precise but therefor a heuristic is needed.
|
|
|
|
|
|
|
|
// Assume a non-negative dividend.
|
|
|
|
takeNonNegativeAssumption(DividendPWAC);
|
|
|
|
|
|
|
|
combine(DividendPWAC, DivisorPWAC, isl_pw_aff_div);
|
|
|
|
DividendPWAC.first = isl_pw_aff_floor(DividendPWAC.first);
|
|
|
|
|
|
|
|
return DividendPWAC;
|
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
|
2015-08-12 18:19:50 +08:00
|
|
|
assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
|
|
|
|
|
2016-06-06 18:07:40 +08:00
|
|
|
auto *Scope = getScope();
|
2015-08-12 18:19:50 +08:00
|
|
|
auto *Divisor = SDiv->getOperand(1);
|
2016-06-06 18:07:40 +08:00
|
|
|
auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope);
|
2016-04-23 22:31:17 +08:00
|
|
|
auto DivisorPWAC = visit(DivisorSCEV);
|
2016-07-12 23:08:47 +08:00
|
|
|
assert(isa<SCEVConstant>(DivisorSCEV) &&
|
2015-08-12 18:19:50 +08:00
|
|
|
"SDiv is no parameter but has a non-constant RHS.");
|
|
|
|
|
|
|
|
auto *Dividend = SDiv->getOperand(0);
|
2016-06-06 18:07:40 +08:00
|
|
|
auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope);
|
2016-04-23 22:31:17 +08:00
|
|
|
auto DividendPWAC = visit(DividendSCEV);
|
|
|
|
combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_q);
|
|
|
|
return DividendPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitSRemInstruction(Instruction *SRem) {
|
2015-08-12 18:19:50 +08:00
|
|
|
assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!");
|
|
|
|
|
2016-06-07 20:00:37 +08:00
|
|
|
auto *Scope = getScope();
|
|
|
|
auto *Divisor = SRem->getOperand(1);
|
|
|
|
auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope);
|
|
|
|
auto DivisorPWAC = visit(DivisorSCEV);
|
|
|
|
assert(isa<ConstantInt>(Divisor) &&
|
|
|
|
"SRem is no parameter but has a non-constant RHS.");
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
auto *Dividend = SRem->getOperand(0);
|
2016-06-07 20:00:37 +08:00
|
|
|
auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope);
|
2016-04-23 22:31:17 +08:00
|
|
|
auto DividendPWAC = visit(DividendSCEV);
|
2016-06-07 20:00:37 +08:00
|
|
|
combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_r);
|
2016-04-23 22:31:17 +08:00
|
|
|
return DividendPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-23 22:31:17 +08:00
|
|
|
__isl_give PWACtx SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
|
2015-08-12 18:19:50 +08:00
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
|
|
|
|
switch (I->getOpcode()) {
|
2016-06-12 03:26:08 +08:00
|
|
|
case Instruction::IntToPtr:
|
|
|
|
return visit(SE.getSCEVAtScope(I->getOperand(0), getScope()));
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
return visit(SE.getSCEVAtScope(I->getOperand(0), getScope()));
|
2015-08-12 18:19:50 +08:00
|
|
|
case Instruction::SDiv:
|
|
|
|
return visitSDivInstruction(I);
|
|
|
|
case Instruction::SRem:
|
|
|
|
return visitSRemInstruction(I);
|
|
|
|
default:
|
|
|
|
break; // Fall through.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable(
|
|
|
|
"Unknowns SCEV was neither parameter nor a valid instruction.");
|
|
|
|
}
|