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