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/ScopInfo.h"
16 #include "polly/Support/GICHelper.h"
17 #include "polly/Support/SCEVValidator.h"
18 #include "polly/Support/ScopHelper.h"
19 #include "isl/aff.h"
20 #include "isl/local_space.h"
21 #include "isl/set.h"
22 #include "isl/val.h"
23 
24 using namespace llvm;
25 using namespace polly;
26 
27 SCEVAffinator::SCEVAffinator(Scop *S)
28     : S(S), Ctx(S->getIslCtx()), R(S->getRegion()), SE(*S->getSE()),
29       TD(R.getEntry()->getParent()->getParent()->getDataLayout()) {}
30 
31 SCEVAffinator::~SCEVAffinator() {
32   for (const auto &CachedPair : CachedExpressions)
33     isl_pw_aff_free(CachedPair.second);
34 }
35 
36 __isl_give isl_pw_aff *SCEVAffinator::getPwAff(const SCEV *Expr,
37                                                BasicBlock *BB) {
38   this->BB = BB;
39 
40   if (BB) {
41     auto *DC = S->getDomainConditions(BB);
42     NumIterators = isl_set_n_dim(DC);
43     isl_set_free(DC);
44   } else
45     NumIterators = 0;
46 
47   S->addParams(getParamsInAffineExpr(&R, Expr, SE));
48 
49   return visit(Expr);
50 }
51 
52 __isl_give isl_set *
53 SCEVAffinator::getWrappingContext(SCEV::NoWrapFlags Flags, Type *ExprType,
54                                   __isl_keep isl_pw_aff *PWA,
55                                   __isl_take isl_set *ExprDomain) const {
56   // If the SCEV flags do contain NSW (no signed wrap) then PWA already
57   // represents Expr in modulo semantic (it is not allowed to overflow), thus we
58   // are done. Otherwise, we will compute:
59   //   PWA = ((PWA + 2^(n-1)) mod (2 ^ n)) - 2^(n-1)
60   // whereas n is the number of bits of the Expr, hence:
61   //   n = bitwidth(ExprType)
62 
63   if (Flags & SCEV::FlagNSW)
64     return nullptr;
65 
66   isl_pw_aff *PWAMod = addModuloSemantic(isl_pw_aff_copy(PWA), ExprType);
67   if (isl_pw_aff_is_equal(PWA, PWAMod)) {
68     isl_pw_aff_free(PWAMod);
69     return nullptr;
70   }
71 
72   PWA = isl_pw_aff_copy(PWA);
73 
74   auto *NotEqualSet = isl_pw_aff_ne_set(PWA, PWAMod);
75   NotEqualSet = isl_set_intersect(NotEqualSet, isl_set_copy(ExprDomain));
76   NotEqualSet = isl_set_gist_params(NotEqualSet, S->getContext());
77   NotEqualSet = isl_set_params(NotEqualSet);
78   return NotEqualSet;
79 }
80 
81 __isl_give isl_set *SCEVAffinator::getWrappingContext() const {
82 
83   isl_set *WrappingCtx = isl_set_empty(S->getParamSpace());
84 
85   for (const auto &CachedPair : CachedExpressions) {
86     const SCEV *Expr = CachedPair.first.first;
87     SCEV::NoWrapFlags Flags;
88 
89     switch (Expr->getSCEVType()) {
90     case scAddExpr:
91       Flags = cast<SCEVAddExpr>(Expr)->getNoWrapFlags();
92       break;
93     case scMulExpr:
94       Flags = cast<SCEVMulExpr>(Expr)->getNoWrapFlags();
95       break;
96     case scAddRecExpr:
97       Flags = cast<SCEVAddRecExpr>(Expr)->getNoWrapFlags();
98       break;
99     default:
100       continue;
101     }
102 
103     isl_pw_aff *PWA = CachedPair.second;
104     BasicBlock *BB = CachedPair.first.second;
105     isl_set *ExprDomain = BB ? S->getDomainConditions(BB) : nullptr;
106 
107     isl_set *WPWACtx =
108         getWrappingContext(Flags, Expr->getType(), PWA, ExprDomain);
109     isl_set_free(ExprDomain);
110 
111     WrappingCtx = WPWACtx ? isl_set_union(WrappingCtx, WPWACtx) : WrappingCtx;
112   }
113 
114   return WrappingCtx;
115 }
116 
117 __isl_give isl_pw_aff *
118 SCEVAffinator::addModuloSemantic(__isl_take isl_pw_aff *PWA,
119                                  Type *ExprType) const {
120   unsigned Width = TD.getTypeStoreSizeInBits(ExprType);
121   isl_ctx *Ctx = isl_pw_aff_get_ctx(PWA);
122 
123   isl_val *ModVal = isl_val_int_from_ui(Ctx, Width);
124   ModVal = isl_val_2exp(ModVal);
125 
126   isl_val *AddVal = isl_val_int_from_ui(Ctx, Width - 1);
127   AddVal = isl_val_2exp(AddVal);
128 
129   isl_set *Domain = isl_pw_aff_domain(isl_pw_aff_copy(PWA));
130 
131   isl_pw_aff *AddPW = isl_pw_aff_val_on_domain(Domain, AddVal);
132 
133   PWA = isl_pw_aff_add(PWA, isl_pw_aff_copy(AddPW));
134   PWA = isl_pw_aff_mod_val(PWA, ModVal);
135   PWA = isl_pw_aff_sub(PWA, AddPW);
136 
137   return PWA;
138 }
139 
140 bool SCEVAffinator::hasNSWAddRecForLoop(Loop *L) const {
141   for (const auto &CachedPair : CachedExpressions) {
142     auto *AddRec = dyn_cast<SCEVAddRecExpr>(CachedPair.first.first);
143     if (!AddRec)
144       continue;
145     if (AddRec->getLoop() != L)
146       continue;
147     if (AddRec->getNoWrapFlags() & SCEV::FlagNSW)
148       return true;
149   }
150 
151   return false;
152 }
153 
154 __isl_give isl_pw_aff *SCEVAffinator::visit(const SCEV *Expr) {
155 
156   auto Key = std::make_pair(Expr, BB);
157   isl_pw_aff *PWA = CachedExpressions[Key];
158   if (PWA)
159     return isl_pw_aff_copy(PWA);
160 
161   // In case the scev is a valid parameter, we do not further analyze this
162   // expression, but create a new parameter in the isl_pw_aff. This allows us
163   // to treat subexpressions that we cannot translate into an piecewise affine
164   // expression, as constant parameters of the piecewise affine expression.
165   if (isl_id *Id = S->getIdForParam(Expr)) {
166     isl_space *Space = isl_space_set_alloc(Ctx, 1, NumIterators);
167     Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
168 
169     isl_set *Domain = isl_set_universe(isl_space_copy(Space));
170     isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
171     Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
172 
173     PWA = isl_pw_aff_alloc(Domain, Affine);
174     CachedExpressions[Key] = PWA;
175     return isl_pw_aff_copy(PWA);
176   }
177 
178   PWA = SCEVVisitor<SCEVAffinator, isl_pw_aff *>::visit(Expr);
179 
180   // For compile time reasons we need to simplify the PWA before we cache and
181   // return it.
182   PWA = isl_pw_aff_coalesce(PWA);
183 
184   CachedExpressions[Key] = PWA;
185   return isl_pw_aff_copy(PWA);
186 }
187 
188 __isl_give isl_pw_aff *SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
189   ConstantInt *Value = Expr->getValue();
190   isl_val *v;
191 
192   // LLVM does not define if an integer value is interpreted as a signed or
193   // unsigned value. Hence, without further information, it is unknown how
194   // this value needs to be converted to GMP. At the moment, we only support
195   // signed operations. So we just interpret it as signed. Later, there are
196   // two options:
197   //
198   // 1. We always interpret any value as signed and convert the values on
199   //    demand.
200   // 2. We pass down the signedness of the calculation and use it to interpret
201   //    this constant correctly.
202   v = isl_valFromAPInt(Ctx, Value->getValue(), /* isSigned */ true);
203 
204   isl_space *Space = isl_space_set_alloc(Ctx, 0, NumIterators);
205   isl_local_space *ls = isl_local_space_from_space(Space);
206   return isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v));
207 }
208 
209 __isl_give isl_pw_aff *
210 SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
211   llvm_unreachable("SCEVTruncateExpr not yet supported");
212 }
213 
214 __isl_give isl_pw_aff *
215 SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
216   llvm_unreachable("SCEVZeroExtendExpr not yet supported");
217 }
218 
219 __isl_give isl_pw_aff *
220 SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
221   // Assuming the value is signed, a sign extension is basically a noop.
222   // TODO: Reconsider this as soon as we support unsigned values.
223   return visit(Expr->getOperand());
224 }
225 
226 __isl_give isl_pw_aff *SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
227   isl_pw_aff *Sum = visit(Expr->getOperand(0));
228 
229   for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
230     isl_pw_aff *NextSummand = visit(Expr->getOperand(i));
231     Sum = isl_pw_aff_add(Sum, NextSummand);
232   }
233 
234   return Sum;
235 }
236 
237 __isl_give isl_pw_aff *SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) {
238   // Divide Expr into a constant part and the rest. Then visit both and multiply
239   // the result to obtain the representation for Expr. While the second part of
240   // ConstantAndLeftOverPair might still be a SCEVMulExpr we will not get to
241   // this point again. The reason is that if it is a multiplication it consists
242   // only of parameters and we will stop in the visit(const SCEV *) function and
243   // return the isl_pw_aff for that parameter.
244   auto ConstantAndLeftOverPair = extractConstantFactor(Expr, *S->getSE());
245   return isl_pw_aff_mul(visit(ConstantAndLeftOverPair.first),
246                         visit(ConstantAndLeftOverPair.second));
247 }
248 
249 __isl_give isl_pw_aff *SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
250   llvm_unreachable("SCEVUDivExpr not yet supported");
251 }
252 
253 __isl_give isl_pw_aff *
254 SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
255   assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
256 
257   auto Flags = Expr->getNoWrapFlags();
258 
259   // Directly generate isl_pw_aff for Expr if 'start' is zero.
260   if (Expr->getStart()->isZero()) {
261     assert(S->getRegion().contains(Expr->getLoop()) &&
262            "Scop does not contain the loop referenced in this AddRec");
263 
264     isl_pw_aff *Step = visit(Expr->getOperand(1));
265     isl_space *Space = isl_space_set_alloc(Ctx, 0, NumIterators);
266     isl_local_space *LocalSpace = isl_local_space_from_space(Space);
267 
268     unsigned loopDimension = S->getRelativeLoopDepth(Expr->getLoop());
269 
270     isl_aff *LAff = isl_aff_set_coefficient_si(
271         isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1);
272     isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff);
273 
274     return isl_pw_aff_mul(Step, LPwAff);
275   }
276 
277   // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
278   // if 'start' is not zero.
279   // TODO: Using the original SCEV no-wrap flags is not always safe, however
280   //       as our code generation is reordering the expression anyway it doesn't
281   //       really matter.
282   ScalarEvolution &SE = *S->getSE();
283   const SCEV *ZeroStartExpr =
284       SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
285                        Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
286 
287   isl_pw_aff *ZeroStartResult = visit(ZeroStartExpr);
288   isl_pw_aff *Start = visit(Expr->getStart());
289 
290   return isl_pw_aff_add(ZeroStartResult, Start);
291 }
292 
293 __isl_give isl_pw_aff *SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
294   isl_pw_aff *Max = visit(Expr->getOperand(0));
295 
296   for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) {
297     isl_pw_aff *NextOperand = visit(Expr->getOperand(i));
298     Max = isl_pw_aff_max(Max, NextOperand);
299   }
300 
301   return Max;
302 }
303 
304 __isl_give isl_pw_aff *SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
305   llvm_unreachable("SCEVUMaxExpr not yet supported");
306 }
307 
308 __isl_give isl_pw_aff *SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
309   assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
310   auto *SE = S->getSE();
311 
312   auto *Divisor = SDiv->getOperand(1);
313   auto *DivisorSCEV = SE->getSCEV(Divisor);
314   auto *DivisorPWA = visit(DivisorSCEV);
315   assert(isa<ConstantInt>(Divisor) &&
316          "SDiv is no parameter but has a non-constant RHS.");
317 
318   auto *Dividend = SDiv->getOperand(0);
319   auto *DividendSCEV = SE->getSCEV(Dividend);
320   auto *DividendPWA = visit(DividendSCEV);
321   return isl_pw_aff_tdiv_q(DividendPWA, DivisorPWA);
322 }
323 
324 __isl_give isl_pw_aff *SCEVAffinator::visitSRemInstruction(Instruction *SRem) {
325   assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!");
326   auto *SE = S->getSE();
327 
328   auto *Divisor = dyn_cast<ConstantInt>(SRem->getOperand(1));
329   assert(Divisor && "SRem is no parameter but has a non-constant RHS.");
330   auto *DivisorVal = isl_valFromAPInt(Ctx, Divisor->getValue(),
331                                       /* isSigned */ true);
332 
333   auto *Dividend = SRem->getOperand(0);
334   auto *DividendSCEV = SE->getSCEV(Dividend);
335   auto *DividendPWA = visit(DividendSCEV);
336 
337   return isl_pw_aff_mod_val(DividendPWA, isl_val_abs(DivisorVal));
338 }
339 
340 __isl_give isl_pw_aff *SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
341   if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
342     switch (I->getOpcode()) {
343     case Instruction::SDiv:
344       return visitSDivInstruction(I);
345     case Instruction::SRem:
346       return visitSRemInstruction(I);
347     default:
348       break; // Fall through.
349     }
350   }
351 
352   llvm_unreachable(
353       "Unknowns SCEV was neither parameter nor a valid instruction.");
354 }
355