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
|
|
|
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
// The maximal number of bits for which a general expression is modeled
|
|
|
|
// precisely.
|
|
|
|
static unsigned const MaxSmallBitWidth = 7;
|
2016-05-12 23:13:49 +08:00
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Add the number of basic sets in @p Domain to @p User
|
2016-09-08 22:31:31 +08:00
|
|
|
static isl_stat addNumBasicSets(__isl_take isl_set *Domain,
|
|
|
|
__isl_take isl_aff *Aff, void *User) {
|
2016-03-27 00:17:00 +08:00
|
|
|
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-09-02 14:33:33 +08:00
|
|
|
/// Determine if @p PWAC is too complex to continue.
|
2017-12-07 05:02:22 +08:00
|
|
|
static bool isTooComplex(PWACtx PWAC) {
|
2016-03-27 00:17:00 +08:00
|
|
|
unsigned NumBasicSets = 0;
|
2018-04-29 08:28:26 +08:00
|
|
|
isl_pw_aff_foreach_piece(PWAC.first.get(), addNumBasicSets, &NumBasicSets);
|
2016-05-02 20:25:18 +08:00
|
|
|
if (NumBasicSets <= MaxDisjunctionsInPwAff)
|
2016-03-27 00:17:00 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-02 14:33:33 +08:00
|
|
|
/// Return the flag describing the possible wrapping of @p Expr.
|
2016-04-12 17:33:47 +08:00
|
|
|
static SCEV::NoWrapFlags getNoWrapFlags(const SCEV *Expr) {
|
|
|
|
if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
|
|
|
|
return NAry->getNoWrapFlags();
|
|
|
|
return SCEV::NoWrapMask;
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
static PWACtx combine(PWACtx PWAC0, PWACtx PWAC1,
|
|
|
|
__isl_give isl_pw_aff *(Fn)(__isl_take isl_pw_aff *,
|
|
|
|
__isl_take isl_pw_aff *)) {
|
2018-04-29 08:57:38 +08:00
|
|
|
PWAC0.first = isl::manage(Fn(PWAC0.first.release(), PWAC1.first.release()));
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC0.second = PWAC0.second.unite(PWAC1.second);
|
|
|
|
return PWAC0;
|
2016-04-23 22:31:17 +08:00
|
|
|
}
|
|
|
|
|
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)
|
2017-11-20 06:13:34 +08:00
|
|
|
: S(S), Ctx(S->getIslCtx().get()), SE(*S->getSE()), LI(LI),
|
2016-05-23 20:38:05 +08:00
|
|
|
TD(S->getFunction().getParent()->getDataLayout()) {}
|
2015-08-12 18:19:50 +08:00
|
|
|
|
2016-06-06 18:07:40 +08:00
|
|
|
Loop *SCEVAffinator::getScope() { return BB ? LI.getLoopFor(BB) : nullptr; }
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
void SCEVAffinator::interpretAsUnsigned(PWACtx &PWAC, unsigned Width) {
|
|
|
|
auto *NonNegDom = isl_pw_aff_nonneg_set(PWAC.first.copy());
|
|
|
|
auto *NonNegPWA =
|
|
|
|
isl_pw_aff_intersect_domain(PWAC.first.copy(), isl_set_copy(NonNegDom));
|
2016-05-10 19:45:46 +08:00
|
|
|
auto *ExpPWA = getWidthExpValOnDomain(Width, isl_set_complement(NonNegDom));
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC.first = isl::manage(isl_pw_aff_union_add(
|
2018-04-29 08:57:38 +08:00
|
|
|
NonNegPWA, isl_pw_aff_add(PWAC.first.release(), ExpPWA)));
|
2016-05-10 19:45:46 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 18:44:41 +08:00
|
|
|
void SCEVAffinator::takeNonNegativeAssumption(PWACtx &PWAC) {
|
2017-12-07 05:02:22 +08:00
|
|
|
auto *NegPWA = isl_pw_aff_neg(PWAC.first.copy());
|
2016-04-29 18:44:41 +08:00
|
|
|
auto *NegDom = isl_pw_aff_pos_set(NegPWA);
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC.second =
|
2018-04-29 08:57:38 +08:00
|
|
|
isl::manage(isl_set_union(PWAC.second.release(), isl_set_copy(NegDom)));
|
2016-04-29 18:44:41 +08:00
|
|
|
auto *Restriction = BB ? NegDom : isl_set_params(NegDom);
|
|
|
|
auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
|
2017-11-22 03:25:26 +08:00
|
|
|
S->recordAssumption(UNSIGNED, isl::manage(Restriction), DL, AS_RESTRICTION,
|
|
|
|
BB);
|
2016-04-29 18:44:41 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::getPWACtxFromPWA(isl::pw_aff PWA) {
|
|
|
|
return std::make_pair(PWA, isl::set::empty(isl::space(Ctx, 0, NumIterators)));
|
2015-08-12 18:46:33 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::getPwAff(const SCEV *Expr, BasicBlock *BB) {
|
2015-09-16 06:49:04 +08:00
|
|
|
this->BB = BB;
|
|
|
|
|
|
|
|
if (BB) {
|
2017-08-07 05:42:38 +08:00
|
|
|
auto *DC = S->getDomainConditions(BB).release();
|
2015-09-16 06:49:04 +08:00
|
|
|
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
|
|
|
|
|
|
|
return visit(Expr);
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
isl::pw_aff PWAMod = addModuloSemantic(PWAC.first, Expr->getType());
|
|
|
|
|
|
|
|
isl::set NotEqualSet = PWAC.first.ne_set(PWAMod);
|
|
|
|
PWAC.second = PWAC.second.unite(NotEqualSet).coalesce();
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2016-04-12 21:28:39 +08:00
|
|
|
const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
|
2017-12-07 05:02:22 +08:00
|
|
|
if (!BB)
|
|
|
|
NotEqualSet = NotEqualSet.params();
|
|
|
|
NotEqualSet = NotEqualSet.coalesce();
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
if (!NotEqualSet.is_empty())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
isl::pw_aff SCEVAffinator::addModuloSemantic(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
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
auto ModVal = isl::val::int_from_ui(Ctx, Width);
|
|
|
|
ModVal = ModVal.two_exp();
|
2015-09-16 06:52:53 +08:00
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
isl::set Domain = PWA.domain();
|
|
|
|
isl::pw_aff AddPW =
|
2018-04-29 08:57:38 +08:00
|
|
|
isl::manage(getWidthExpValOnDomain(Width - 1, Domain.release()));
|
2018-03-11 02:07:03 +08:00
|
|
|
|
2018-03-11 03:15:23 +08:00
|
|
|
return PWA.add(AddPW).mod(ModVal).sub(AddPW);
|
2015-09-16 06:52:53 +08:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
bool SCEVAffinator::computeModuloForExpr(const SCEV *Expr) {
|
|
|
|
unsigned Width = TD.getTypeSizeInBits(Expr->getType());
|
|
|
|
// We assume nsw expressions never overflow.
|
|
|
|
if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
|
|
|
|
if (NAry->getNoWrapFlags() & SCEV::FlagNSW)
|
|
|
|
return false;
|
|
|
|
return Width <= MaxSmallBitWidth;
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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)
|
2017-12-07 05:02:22 +08:00
|
|
|
return 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;
|
|
|
|
|
2016-11-10 14:45:28 +08:00
|
|
|
auto *Scope = getScope();
|
|
|
|
S->addParams(getParamsInAffineExpr(&S->getRegion(), Scope, Expr, SE));
|
|
|
|
|
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.
|
2017-08-07 03:31:27 +08:00
|
|
|
if (isl_id *Id = S->getIdForParam(Expr).release()) {
|
2017-12-07 05:02:22 +08:00
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx.get(), 1, NumIterators);
|
2015-08-12 18:19:50 +08:00
|
|
|
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);
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC = getPWACtxFromPWA(isl::manage(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);
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (computeModuloForExpr(Expr))
|
|
|
|
PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
|
|
|
|
else
|
|
|
|
PWAC = checkForWrapping(Expr, PWAC);
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (!Factor->getType()->isIntegerTy(1)) {
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC = combine(PWAC, visitConstant(Factor), isl_pw_aff_mul);
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (computeModuloForExpr(Key.first))
|
|
|
|
PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
|
|
|
|
}
|
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.
|
2017-12-07 05:02:22 +08:00
|
|
|
PWAC.first = PWAC.first.coalesce();
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (!computeModuloForExpr(Key.first))
|
|
|
|
PWAC = checkForWrapping(Key.first, PWAC);
|
2016-04-12 21:28:39 +08:00
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
CachedExpressions[Key] = PWAC;
|
2016-04-23 22:31:17 +08:00
|
|
|
return PWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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.
|
2017-12-07 05:02:22 +08:00
|
|
|
v = isl_valFromAPInt(Ctx.get(), Value->getValue(), /* isSigned */ true);
|
2015-08-12 18:19:50 +08:00
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators);
|
2015-08-12 18:19:50 +08:00
|
|
|
isl_local_space *ls = isl_local_space_from_space(Space);
|
2017-12-07 05:02:22 +08:00
|
|
|
return getPWACtxFromPWA(
|
|
|
|
isl::manage(isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v))));
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 06:01:08 +08:00
|
|
|
PWACtx 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());
|
|
|
|
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (computeModuloForExpr(Expr))
|
2016-05-12 23:13:49 +08:00
|
|
|
return OpPWAC;
|
|
|
|
|
2018-04-29 08:57:38 +08:00
|
|
|
auto *Dom = OpPWAC.first.domain().release();
|
2016-05-12 23:13:49 +08:00
|
|
|
auto *ExpPWA = getWidthExpValOnDomain(Width - 1, Dom);
|
|
|
|
auto *GreaterDom =
|
2017-12-07 05:02:22 +08:00
|
|
|
isl_pw_aff_ge_set(OpPWAC.first.copy(), isl_pw_aff_copy(ExpPWA));
|
2016-05-12 23:13:49 +08:00
|
|
|
auto *SmallerDom =
|
2017-12-07 05:02:22 +08:00
|
|
|
isl_pw_aff_lt_set(OpPWAC.first.copy(), isl_pw_aff_neg(ExpPWA));
|
2016-05-12 23:13:49 +08:00
|
|
|
auto *OutOfBoundsDom = isl_set_union(SmallerDom, GreaterDom);
|
2018-02-20 15:26:58 +08:00
|
|
|
OpPWAC.second = OpPWAC.second.unite(isl::manage_copy(OutOfBoundsDom));
|
2016-11-10 19:44:10 +08:00
|
|
|
|
|
|
|
if (!BB) {
|
|
|
|
assert(isl_set_dim(OutOfBoundsDom, isl_dim_set) == 0 &&
|
|
|
|
"Expected a zero dimensional set for non-basic-block domains");
|
|
|
|
OutOfBoundsDom = isl_set_params(OutOfBoundsDom);
|
|
|
|
}
|
|
|
|
|
2017-11-22 03:25:26 +08:00
|
|
|
S->recordAssumption(UNSIGNED, isl::manage(OutOfBoundsDom), DebugLoc(),
|
|
|
|
AS_RESTRICTION, BB);
|
2016-05-12 23:13:49 +08:00
|
|
|
|
|
|
|
return OpPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 06:01:08 +08:00
|
|
|
PWACtx 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();
|
|
|
|
auto OpPWAC = visit(Op);
|
|
|
|
|
|
|
|
// If the width is to big we assume the negative part does not occur.
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
if (!computeModuloForExpr(Op)) {
|
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.
|
[SCEVAffinator] Make precise modular math more correct.
Integer math in LLVM IR is modular. Integer math in isl is
arbitrary-precision. Modeling LLVM IR math correctly in isl requires
either adding assumptions that math doesn't actually overflow, or
explicitly wrapping the math. However, expressions with the "nsw" flag
are special; we can pretend they're arbitrary-precision because it's
undefined behavior if the result wraps. SCEV expressions based on IR
instructions with an nsw flag also carry an nsw flag (roughly; actually,
the real rule is a bit more complicated, but the details don't matter
here).
Before this patch, SCEV flags were also overloaded with an additional
function: the ZExt code was mutating SCEV expressions as a hack to
indicate to checkForWrapping that we don't need to add assumptions to
the operand of a ZExt; it'll add explicit wrapping itself. This kind of
works... the problem is that if anything else ever touches that SCEV
expression, it'll get confused by the incorrect flags.
Instead, with this patch, we make the decision about whether to
explicitly wrap the math a bit earlier, basing the decision purely on
the SCEV expression itself, and not its users.
Differential Revision: https://reviews.llvm.org/D25287
llvm-svn: 284848
2016-10-22 02:08:02 +08:00
|
|
|
unsigned Width = TD.getTypeSizeInBits(Op->getType());
|
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
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx 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());
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx Sum = visit(Expr->getOperand(0));
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2017-12-07 05:02:22 +08:00
|
|
|
Sum = combine(Sum, visit(Expr->getOperand(i)), isl_pw_aff_add);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Sum))
|
2018-05-15 07:05:43 +08:00
|
|
|
return complexityBailout();
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Sum;
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx Prod = visit(Expr->getOperand(0));
|
2016-04-08 18:27:40 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2017-12-07 05:02:22 +08:00
|
|
|
Prod = combine(Prod, visit(Expr->getOperand(i)), isl_pw_aff_mul);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Prod))
|
2018-05-15 07:05:43 +08:00
|
|
|
return complexityBailout();
|
2016-04-08 18:27:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Prod;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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));
|
2017-12-07 05:02:22 +08:00
|
|
|
isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators);
|
2015-08-12 18:19:50 +08:00
|
|
|
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);
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
Step.first = Step.first.mul(isl::manage(LPwAff));
|
2016-04-23 22:31:17 +08:00
|
|
|
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());
|
2017-12-07 05:02:22 +08:00
|
|
|
Result = combine(Result, Start, isl_pw_aff_add);
|
2016-04-23 22:31:17 +08:00
|
|
|
return Result;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
2016-04-23 22:31:17 +08:00
|
|
|
PWACtx Max = visit(Expr->getOperand(0));
|
2015-08-12 18:19:50 +08:00
|
|
|
|
|
|
|
for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
|
2017-12-07 05:02:22 +08:00
|
|
|
Max = combine(Max, visit(Expr->getOperand(i)), isl_pw_aff_max);
|
2016-05-02 18:44:20 +08:00
|
|
|
if (isTooComplex(Max))
|
2018-05-15 07:05:43 +08:00
|
|
|
return complexityBailout();
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Max;
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
|
2015-08-12 18:19:50 +08:00
|
|
|
llvm_unreachable("SCEVUMaxExpr not yet supported");
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
PWACtx SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
|
2016-04-29 19:53:35 +08:00
|
|
|
// 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());
|
2018-04-29 08:57:38 +08:00
|
|
|
auto *DivisorDom = DivisorPWAC.first.domain().release();
|
2016-04-29 19:53:35 +08:00
|
|
|
auto *WidthExpPWA = getWidthExpValOnDomain(Width, DivisorDom);
|
2017-12-07 05:02:22 +08:00
|
|
|
DivisorPWAC.first = DivisorPWAC.first.add(isl::manage(WidthExpPWA));
|
2016-04-29 19:53:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_div);
|
|
|
|
DividendPWAC.first = DividendPWAC.first.floor();
|
2016-04-29 19:53:35 +08:00
|
|
|
|
|
|
|
return DividendPWAC;
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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);
|
2017-12-07 05:02:22 +08:00
|
|
|
DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_q);
|
2016-04-23 22:31:17 +08:00
|
|
|
return DividendPWAC;
|
2015-08-12 18:19:50 +08:00
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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);
|
2017-12-07 05:02:22 +08:00
|
|
|
DividendPWAC = 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
|
|
|
}
|
|
|
|
|
2017-12-07 05:02:22 +08:00
|
|
|
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.");
|
|
|
|
}
|
2018-05-15 07:05:43 +08:00
|
|
|
|
|
|
|
PWACtx SCEVAffinator::complexityBailout() {
|
|
|
|
// We hit the complexity limit for affine expressions; invalidate the scop
|
|
|
|
// and return a constant zero.
|
|
|
|
const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
|
|
|
|
S->invalidate(COMPLEXITY, Loc);
|
2018-05-19 05:57:44 +08:00
|
|
|
return visit(SE.getZero(Type::getInt32Ty(S->getFunction().getContext())));
|
2018-05-15 07:05:43 +08:00
|
|
|
}
|