SCEVAffinator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. //===--------- SCEVAffinator.cpp - Create Scops from LLVM IR -------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Create a polyhedral description for a SCEV value.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/Support/SCEVAffinator.h"
  13. #include "polly/Options.h"
  14. #include "polly/ScopInfo.h"
  15. #include "polly/Support/GICHelper.h"
  16. #include "polly/Support/SCEVValidator.h"
  17. #include "isl/aff.h"
  18. #include "isl/local_space.h"
  19. #include "isl/set.h"
  20. #include "isl/val.h"
  21. using namespace llvm;
  22. using namespace polly;
  23. static cl::opt<bool> IgnoreIntegerWrapping(
  24. "polly-ignore-integer-wrapping",
  25. cl::desc("Do not build run-time checks to proof absence of integer "
  26. "wrapping"),
  27. cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory));
  28. // The maximal number of basic sets we allow during the construction of a
  29. // piecewise affine function. More complex ones will result in very high
  30. // compile time.
  31. static int const MaxDisjunctionsInPwAff = 100;
  32. // The maximal number of bits for which a general expression is modeled
  33. // precisely.
  34. static unsigned const MaxSmallBitWidth = 7;
  35. /// Add the number of basic sets in @p Domain to @p User
  36. static isl_stat addNumBasicSets(__isl_take isl_set *Domain,
  37. __isl_take isl_aff *Aff, void *User) {
  38. auto *NumBasicSets = static_cast<unsigned *>(User);
  39. *NumBasicSets += isl_set_n_basic_set(Domain);
  40. isl_set_free(Domain);
  41. isl_aff_free(Aff);
  42. return isl_stat_ok;
  43. }
  44. /// Determine if @p PWAC is too complex to continue.
  45. static bool isTooComplex(PWACtx PWAC) {
  46. unsigned NumBasicSets = 0;
  47. isl_pw_aff_foreach_piece(PWAC.first.get(), addNumBasicSets, &NumBasicSets);
  48. if (NumBasicSets <= MaxDisjunctionsInPwAff)
  49. return false;
  50. return true;
  51. }
  52. /// Return the flag describing the possible wrapping of @p Expr.
  53. static SCEV::NoWrapFlags getNoWrapFlags(const SCEV *Expr) {
  54. if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
  55. return NAry->getNoWrapFlags();
  56. return SCEV::NoWrapMask;
  57. }
  58. static PWACtx combine(PWACtx PWAC0, PWACtx PWAC1,
  59. __isl_give isl_pw_aff *(Fn)(__isl_take isl_pw_aff *,
  60. __isl_take isl_pw_aff *)) {
  61. PWAC0.first = isl::manage(Fn(PWAC0.first.release(), PWAC1.first.release()));
  62. PWAC0.second = PWAC0.second.unite(PWAC1.second);
  63. return PWAC0;
  64. }
  65. static __isl_give isl_pw_aff *getWidthExpValOnDomain(unsigned Width,
  66. __isl_take isl_set *Dom) {
  67. auto *Ctx = isl_set_get_ctx(Dom);
  68. auto *WidthVal = isl_val_int_from_ui(Ctx, Width);
  69. auto *ExpVal = isl_val_2exp(WidthVal);
  70. return isl_pw_aff_val_on_domain(Dom, ExpVal);
  71. }
  72. SCEVAffinator::SCEVAffinator(Scop *S, LoopInfo &LI)
  73. : S(S), Ctx(S->getIslCtx().get()), SE(*S->getSE()), LI(LI),
  74. TD(S->getFunction().getParent()->getDataLayout()) {}
  75. Loop *SCEVAffinator::getScope() { return BB ? LI.getLoopFor(BB) : nullptr; }
  76. void SCEVAffinator::interpretAsUnsigned(PWACtx &PWAC, unsigned Width) {
  77. auto *NonNegDom = isl_pw_aff_nonneg_set(PWAC.first.copy());
  78. auto *NonNegPWA =
  79. isl_pw_aff_intersect_domain(PWAC.first.copy(), isl_set_copy(NonNegDom));
  80. auto *ExpPWA = getWidthExpValOnDomain(Width, isl_set_complement(NonNegDom));
  81. PWAC.first = isl::manage(isl_pw_aff_union_add(
  82. NonNegPWA, isl_pw_aff_add(PWAC.first.release(), ExpPWA)));
  83. }
  84. void SCEVAffinator::takeNonNegativeAssumption(
  85. PWACtx &PWAC, RecordedAssumptionsTy *RecordedAssumptions) {
  86. this->RecordedAssumptions = RecordedAssumptions;
  87. auto *NegPWA = isl_pw_aff_neg(PWAC.first.copy());
  88. auto *NegDom = isl_pw_aff_pos_set(NegPWA);
  89. PWAC.second =
  90. isl::manage(isl_set_union(PWAC.second.release(), isl_set_copy(NegDom)));
  91. auto *Restriction = BB ? NegDom : isl_set_params(NegDom);
  92. auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
  93. recordAssumption(RecordedAssumptions, UNSIGNED, isl::manage(Restriction), DL,
  94. AS_RESTRICTION, BB);
  95. }
  96. PWACtx SCEVAffinator::getPWACtxFromPWA(isl::pw_aff PWA) {
  97. return std::make_pair(PWA, isl::set::empty(isl::space(Ctx, 0, NumIterators)));
  98. }
  99. PWACtx SCEVAffinator::getPwAff(const SCEV *Expr, BasicBlock *BB,
  100. RecordedAssumptionsTy *RecordedAssumptions) {
  101. this->BB = BB;
  102. this->RecordedAssumptions = RecordedAssumptions;
  103. if (BB) {
  104. auto *DC = S->getDomainConditions(BB).release();
  105. NumIterators = isl_set_n_dim(DC);
  106. isl_set_free(DC);
  107. } else
  108. NumIterators = 0;
  109. return visit(Expr);
  110. }
  111. PWACtx SCEVAffinator::checkForWrapping(const SCEV *Expr, PWACtx PWAC) const {
  112. // If the SCEV flags do contain NSW (no signed wrap) then PWA already
  113. // represents Expr in modulo semantic (it is not allowed to overflow), thus we
  114. // are done. Otherwise, we will compute:
  115. // PWA = ((PWA + 2^(n-1)) mod (2 ^ n)) - 2^(n-1)
  116. // whereas n is the number of bits of the Expr, hence:
  117. // n = bitwidth(ExprType)
  118. if (IgnoreIntegerWrapping || (getNoWrapFlags(Expr) & SCEV::FlagNSW))
  119. return PWAC;
  120. isl::pw_aff PWAMod = addModuloSemantic(PWAC.first, Expr->getType());
  121. isl::set NotEqualSet = PWAC.first.ne_set(PWAMod);
  122. PWAC.second = PWAC.second.unite(NotEqualSet).coalesce();
  123. const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
  124. if (!BB)
  125. NotEqualSet = NotEqualSet.params();
  126. NotEqualSet = NotEqualSet.coalesce();
  127. if (!NotEqualSet.is_empty())
  128. recordAssumption(RecordedAssumptions, WRAPPING, NotEqualSet, Loc,
  129. AS_RESTRICTION, BB);
  130. return PWAC;
  131. }
  132. isl::pw_aff SCEVAffinator::addModuloSemantic(isl::pw_aff PWA,
  133. Type *ExprType) const {
  134. unsigned Width = TD.getTypeSizeInBits(ExprType);
  135. auto ModVal = isl::val::int_from_ui(Ctx, Width);
  136. ModVal = ModVal.pow2();
  137. isl::set Domain = PWA.domain();
  138. isl::pw_aff AddPW =
  139. isl::manage(getWidthExpValOnDomain(Width - 1, Domain.release()));
  140. return PWA.add(AddPW).mod(ModVal).sub(AddPW);
  141. }
  142. bool SCEVAffinator::hasNSWAddRecForLoop(Loop *L) const {
  143. for (const auto &CachedPair : CachedExpressions) {
  144. auto *AddRec = dyn_cast<SCEVAddRecExpr>(CachedPair.first.first);
  145. if (!AddRec)
  146. continue;
  147. if (AddRec->getLoop() != L)
  148. continue;
  149. if (AddRec->getNoWrapFlags() & SCEV::FlagNSW)
  150. return true;
  151. }
  152. return false;
  153. }
  154. bool SCEVAffinator::computeModuloForExpr(const SCEV *Expr) {
  155. unsigned Width = TD.getTypeSizeInBits(Expr->getType());
  156. // We assume nsw expressions never overflow.
  157. if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
  158. if (NAry->getNoWrapFlags() & SCEV::FlagNSW)
  159. return false;
  160. return Width <= MaxSmallBitWidth;
  161. }
  162. PWACtx SCEVAffinator::visit(const SCEV *Expr) {
  163. auto Key = std::make_pair(Expr, BB);
  164. PWACtx PWAC = CachedExpressions[Key];
  165. if (!PWAC.first.is_null())
  166. return PWAC;
  167. auto ConstantAndLeftOverPair = extractConstantFactor(Expr, SE);
  168. auto *Factor = ConstantAndLeftOverPair.first;
  169. Expr = ConstantAndLeftOverPair.second;
  170. auto *Scope = getScope();
  171. S->addParams(getParamsInAffineExpr(&S->getRegion(), Scope, Expr, SE));
  172. // In case the scev is a valid parameter, we do not further analyze this
  173. // expression, but create a new parameter in the isl_pw_aff. This allows us
  174. // to treat subexpressions that we cannot translate into an piecewise affine
  175. // expression, as constant parameters of the piecewise affine expression.
  176. if (isl_id *Id = S->getIdForParam(Expr).release()) {
  177. isl_space *Space = isl_space_set_alloc(Ctx.get(), 1, NumIterators);
  178. Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
  179. isl_set *Domain = isl_set_universe(isl_space_copy(Space));
  180. isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
  181. Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
  182. PWAC = getPWACtxFromPWA(isl::manage(isl_pw_aff_alloc(Domain, Affine)));
  183. } else {
  184. PWAC = SCEVVisitor<SCEVAffinator, PWACtx>::visit(Expr);
  185. if (computeModuloForExpr(Expr))
  186. PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
  187. else
  188. PWAC = checkForWrapping(Expr, PWAC);
  189. }
  190. if (!Factor->getType()->isIntegerTy(1)) {
  191. PWAC = combine(PWAC, visitConstant(Factor), isl_pw_aff_mul);
  192. if (computeModuloForExpr(Key.first))
  193. PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
  194. }
  195. // For compile time reasons we need to simplify the PWAC before we cache and
  196. // return it.
  197. PWAC.first = PWAC.first.coalesce();
  198. if (!computeModuloForExpr(Key.first))
  199. PWAC = checkForWrapping(Key.first, PWAC);
  200. CachedExpressions[Key] = PWAC;
  201. return PWAC;
  202. }
  203. PWACtx SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
  204. ConstantInt *Value = Expr->getValue();
  205. isl_val *v;
  206. // LLVM does not define if an integer value is interpreted as a signed or
  207. // unsigned value. Hence, without further information, it is unknown how
  208. // this value needs to be converted to GMP. At the moment, we only support
  209. // signed operations. So we just interpret it as signed. Later, there are
  210. // two options:
  211. //
  212. // 1. We always interpret any value as signed and convert the values on
  213. // demand.
  214. // 2. We pass down the signedness of the calculation and use it to interpret
  215. // this constant correctly.
  216. v = isl_valFromAPInt(Ctx.get(), Value->getValue(), /* isSigned */ true);
  217. isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators);
  218. isl_local_space *ls = isl_local_space_from_space(Space);
  219. return getPWACtxFromPWA(
  220. isl::manage(isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v))));
  221. }
  222. PWACtx SCEVAffinator::visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) {
  223. return visit(Expr->getOperand(0));
  224. }
  225. PWACtx SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
  226. // Truncate operations are basically modulo operations, thus we can
  227. // model them that way. However, for large types we assume the operand
  228. // to fit in the new type size instead of introducing a modulo with a very
  229. // large constant.
  230. auto *Op = Expr->getOperand();
  231. auto OpPWAC = visit(Op);
  232. unsigned Width = TD.getTypeSizeInBits(Expr->getType());
  233. if (computeModuloForExpr(Expr))
  234. return OpPWAC;
  235. auto *Dom = OpPWAC.first.domain().release();
  236. auto *ExpPWA = getWidthExpValOnDomain(Width - 1, Dom);
  237. auto *GreaterDom =
  238. isl_pw_aff_ge_set(OpPWAC.first.copy(), isl_pw_aff_copy(ExpPWA));
  239. auto *SmallerDom =
  240. isl_pw_aff_lt_set(OpPWAC.first.copy(), isl_pw_aff_neg(ExpPWA));
  241. auto *OutOfBoundsDom = isl_set_union(SmallerDom, GreaterDom);
  242. OpPWAC.second = OpPWAC.second.unite(isl::manage_copy(OutOfBoundsDom));
  243. if (!BB) {
  244. assert(isl_set_dim(OutOfBoundsDom, isl_dim_set) == 0 &&
  245. "Expected a zero dimensional set for non-basic-block domains");
  246. OutOfBoundsDom = isl_set_params(OutOfBoundsDom);
  247. }
  248. recordAssumption(RecordedAssumptions, UNSIGNED, isl::manage(OutOfBoundsDom),
  249. DebugLoc(), AS_RESTRICTION, BB);
  250. return OpPWAC;
  251. }
  252. PWACtx SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
  253. // A zero-extended value can be interpreted as a piecewise defined signed
  254. // value. If the value was non-negative it stays the same, otherwise it
  255. // is the sum of the original value and 2^n where n is the bit-width of
  256. // the original (or operand) type. Examples:
  257. // zext i8 127 to i32 -> { [127] }
  258. // zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] }
  259. // zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
  260. //
  261. // However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
  262. // truncate) to represent some forms of modulo computation. The left-hand side
  263. // of the condition in the code below would result in the SCEV
  264. // "zext i1 <false, +, true>for.body" which is just another description
  265. // of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
  266. //
  267. // for (i = 0; i < N; i++)
  268. // if (i & 1 != 0 /* == i % 2 */)
  269. // /* do something */
  270. //
  271. // If we do not make the modulo explicit but only use the mechanism described
  272. // above we will get the very restrictive assumption "N < 3", because for all
  273. // values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
  274. // Alternatively, we can make the modulo in the operand explicit in the
  275. // resulting piecewise function and thereby avoid the assumption on N. For the
  276. // example this would result in the following piecewise affine function:
  277. // { [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
  278. // [i0] -> [(0)] : 2*floor((i0)/2) = i0 }
  279. // To this end we can first determine if the (immediate) operand of the
  280. // zero-extend can wrap and, in case it might, we will use explicit modulo
  281. // semantic to compute the result instead of emitting non-wrapping
  282. // assumptions.
  283. //
  284. // Note that operands with large bit-widths are less likely to be negative
  285. // because it would result in a very large access offset or loop bound after
  286. // the zero-extend. To this end one can optimistically assume the operand to
  287. // be positive and avoid the piecewise definition if the bit-width is bigger
  288. // than some threshold (here MaxZextSmallBitWidth).
  289. //
  290. // We choose to go with a hybrid solution of all modeling techniques described
  291. // above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
  292. // wrapping explicitly and use a piecewise defined function. However, if the
  293. // bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
  294. // assumptions and assume the "former negative" piece will not exist.
  295. auto *Op = Expr->getOperand();
  296. auto OpPWAC = visit(Op);
  297. // If the width is to big we assume the negative part does not occur.
  298. if (!computeModuloForExpr(Op)) {
  299. takeNonNegativeAssumption(OpPWAC, RecordedAssumptions);
  300. return OpPWAC;
  301. }
  302. // If the width is small build the piece for the non-negative part and
  303. // the one for the negative part and unify them.
  304. unsigned Width = TD.getTypeSizeInBits(Op->getType());
  305. interpretAsUnsigned(OpPWAC, Width);
  306. return OpPWAC;
  307. }
  308. PWACtx SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
  309. // As all values are represented as signed, a sign extension is a noop.
  310. return visit(Expr->getOperand());
  311. }
  312. PWACtx SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
  313. PWACtx Sum = visit(Expr->getOperand(0));
  314. for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
  315. Sum = combine(Sum, visit(Expr->getOperand(i)), isl_pw_aff_add);
  316. if (isTooComplex(Sum))
  317. return complexityBailout();
  318. }
  319. return Sum;
  320. }
  321. PWACtx SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
  322. PWACtx Prod = visit(Expr->getOperand(0));
  323. for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
  324. Prod = combine(Prod, visit(Expr->getOperand(i)), isl_pw_aff_mul);
  325. if (isTooComplex(Prod))
  326. return complexityBailout();
  327. }
  328. return Prod;
  329. }
  330. PWACtx SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
  331. assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
  332. auto Flags = Expr->getNoWrapFlags();
  333. // Directly generate isl_pw_aff for Expr if 'start' is zero.
  334. if (Expr->getStart()->isZero()) {
  335. assert(S->contains(Expr->getLoop()) &&
  336. "Scop does not contain the loop referenced in this AddRec");
  337. PWACtx Step = visit(Expr->getOperand(1));
  338. isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators);
  339. isl_local_space *LocalSpace = isl_local_space_from_space(Space);
  340. unsigned loopDimension = S->getRelativeLoopDepth(Expr->getLoop());
  341. isl_aff *LAff = isl_aff_set_coefficient_si(
  342. isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
  343. isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
  344. Step.first = Step.first.mul(isl::manage(LPwAff));
  345. return Step;
  346. }
  347. // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
  348. // if 'start' is not zero.
  349. // TODO: Using the original SCEV no-wrap flags is not always safe, however
  350. // as our code generation is reordering the expression anyway it doesn't
  351. // really matter.
  352. const SCEV *ZeroStartExpr =
  353. SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
  354. Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
  355. PWACtx Result = visit(ZeroStartExpr);
  356. PWACtx Start = visit(Expr->getStart());
  357. Result = combine(Result, Start, isl_pw_aff_add);
  358. return Result;
  359. }
  360. PWACtx SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
  361. PWACtx Max = visit(Expr->getOperand(0));
  362. for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
  363. Max = combine(Max, visit(Expr->getOperand(i)), isl_pw_aff_max);
  364. if (isTooComplex(Max))
  365. return complexityBailout();
  366. }
  367. return Max;
  368. }
  369. PWACtx SCEVAffinator::visitSMinExpr(const SCEVSMinExpr *Expr) {
  370. PWACtx Min = visit(Expr->getOperand(0));
  371. for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
  372. Min = combine(Min, visit(Expr->getOperand(i)), isl_pw_aff_min);
  373. if (isTooComplex(Min))
  374. return complexityBailout();
  375. }
  376. return Min;
  377. }
  378. PWACtx SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
  379. llvm_unreachable("SCEVUMaxExpr not yet supported");
  380. }
  381. PWACtx SCEVAffinator::visitUMinExpr(const SCEVUMinExpr *Expr) {
  382. llvm_unreachable("SCEVUMinExpr not yet supported");
  383. }
  384. PWACtx
  385. SCEVAffinator::visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) {
  386. llvm_unreachable("SCEVSequentialUMinExpr not yet supported");
  387. }
  388. PWACtx SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
  389. // The handling of unsigned division is basically the same as for signed
  390. // division, except the interpretation of the operands. As the divisor
  391. // has to be constant in both cases we can simply interpret it as an
  392. // unsigned value without additional complexity in the representation.
  393. // For the dividend we could choose from the different representation
  394. // schemes introduced for zero-extend operations but for now we will
  395. // simply use an assumption.
  396. auto *Dividend = Expr->getLHS();
  397. auto *Divisor = Expr->getRHS();
  398. assert(isa<SCEVConstant>(Divisor) &&
  399. "UDiv is no parameter but has a non-constant RHS.");
  400. auto DividendPWAC = visit(Dividend);
  401. auto DivisorPWAC = visit(Divisor);
  402. if (SE.isKnownNegative(Divisor)) {
  403. // Interpret negative divisors unsigned. This is a special case of the
  404. // piece-wise defined value described for zero-extends as we already know
  405. // the actual value of the constant divisor.
  406. unsigned Width = TD.getTypeSizeInBits(Expr->getType());
  407. auto *DivisorDom = DivisorPWAC.first.domain().release();
  408. auto *WidthExpPWA = getWidthExpValOnDomain(Width, DivisorDom);
  409. DivisorPWAC.first = DivisorPWAC.first.add(isl::manage(WidthExpPWA));
  410. }
  411. // TODO: One can represent the dividend as piece-wise function to be more
  412. // precise but therefor a heuristic is needed.
  413. // Assume a non-negative dividend.
  414. takeNonNegativeAssumption(DividendPWAC, RecordedAssumptions);
  415. DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_div);
  416. DividendPWAC.first = DividendPWAC.first.floor();
  417. return DividendPWAC;
  418. }
  419. PWACtx SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
  420. assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
  421. auto *Scope = getScope();
  422. auto *Divisor = SDiv->getOperand(1);
  423. auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope);
  424. auto DivisorPWAC = visit(DivisorSCEV);
  425. assert(isa<SCEVConstant>(DivisorSCEV) &&
  426. "SDiv is no parameter but has a non-constant RHS.");
  427. auto *Dividend = SDiv->getOperand(0);
  428. auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope);
  429. auto DividendPWAC = visit(DividendSCEV);
  430. DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_q);
  431. return DividendPWAC;
  432. }
  433. PWACtx SCEVAffinator::visitSRemInstruction(Instruction *SRem) {
  434. assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!");
  435. auto *Scope = getScope();
  436. auto *Divisor = SRem->getOperand(1);
  437. auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope);
  438. auto DivisorPWAC = visit(DivisorSCEV);
  439. assert(isa<ConstantInt>(Divisor) &&
  440. "SRem is no parameter but has a non-constant RHS.");
  441. auto *Dividend = SRem->getOperand(0);
  442. auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope);
  443. auto DividendPWAC = visit(DividendSCEV);
  444. DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_r);
  445. return DividendPWAC;
  446. }
  447. PWACtx SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
  448. if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
  449. switch (I->getOpcode()) {
  450. case Instruction::IntToPtr:
  451. return visit(SE.getSCEVAtScope(I->getOperand(0), getScope()));
  452. case Instruction::SDiv:
  453. return visitSDivInstruction(I);
  454. case Instruction::SRem:
  455. return visitSRemInstruction(I);
  456. default:
  457. break; // Fall through.
  458. }
  459. }
  460. if (isa<ConstantPointerNull>(Expr->getValue())) {
  461. isl::val v{Ctx, 0};
  462. isl::space Space{Ctx, 0, NumIterators};
  463. isl::local_space ls{Space};
  464. return getPWACtxFromPWA(isl::aff(ls, v));
  465. }
  466. llvm_unreachable("Unknowns SCEV was neither a parameter, a constant nor a "
  467. "valid instruction.");
  468. }
  469. PWACtx SCEVAffinator::complexityBailout() {
  470. // We hit the complexity limit for affine expressions; invalidate the scop
  471. // and return a constant zero.
  472. const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc();
  473. S->invalidate(COMPLEXITY, Loc);
  474. return visit(SE.getZero(Type::getInt32Ty(S->getFunction().getContext())));
  475. }