Coverage Report

Created: 2018-08-20 19:24

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