Coverage Report

Created: 2018-04-23 18:20

/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
559
                                __isl_take isl_aff *Aff, void *User) {
46
559
  auto *NumBasicSets = static_cast<unsigned *>(User);
47
559
  *NumBasicSets += isl_set_n_basic_set(Domain);
48
559
  isl_set_free(Domain);
49
559
  isl_aff_free(Aff);
50
559
  return isl_stat_ok;
51
559
}
52
53
/// Determine if @p PWAC is too complex to continue.
54
331
static bool isTooComplex(PWACtx PWAC) {
55
331
  unsigned NumBasicSets = 0;
56
331
  isl_pw_aff_foreach_piece(PWAC.first.keep(), addNumBasicSets, &NumBasicSets);
57
331
  if (NumBasicSets <= MaxDisjunctionsInPwAff)
58
330
    return false;
59
1
  return true;
60
1
}
61
62
/// Return the flag describing the possible wrapping of @p Expr.
63
22.7k
static SCEV::NoWrapFlags getNoWrapFlags(const SCEV *Expr) {
64
22.7k
  if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
65
9.56k
    return NAry->getNoWrapFlags();
66
13.1k
  return SCEV::NoWrapMask;
67
13.1k
}
68
69
static PWACtx combine(PWACtx PWAC0, PWACtx PWAC1,
70
                      __isl_give isl_pw_aff *(Fn)(__isl_take isl_pw_aff *,
71
13.9k
                                                  __isl_take isl_pw_aff *)) {
72
13.9k
  PWAC0.first = isl::manage(Fn(PWAC0.first.take(), PWAC1.first.take()));
73
13.9k
  PWAC0.second = PWAC0.second.unite(PWAC1.second);
74
13.9k
  return PWAC0;
75
13.9k
}
76
77
static __isl_give isl_pw_aff *getWidthExpValOnDomain(unsigned Width,
78
3.07k
                                                     __isl_take isl_set *Dom) {
79
3.07k
  auto *Ctx = isl_set_get_ctx(Dom);
80
3.07k
  auto *WidthVal = isl_val_int_from_ui(Ctx, Width);
81
3.07k
  auto *ExpVal = isl_val_2exp(WidthVal);
82
3.07k
  return isl_pw_aff_val_on_domain(Dom, ExpVal);
83
3.07k
}
84
85
SCEVAffinator::SCEVAffinator(Scop *S, LoopInfo &LI)
86
    : S(S), Ctx(S->getIslCtx().get()), SE(*S->getSE()), LI(LI),
87
1.19k
      TD(S->getFunction().getParent()->getDataLayout()) {}
88
89
12.4k
Loop *SCEVAffinator::getScope() { return BB ? 
LI.getLoopFor(BB)12.0k
:
nullptr399
; }
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.take(), ExpPWA)));
98
45
}
99
100
139
void SCEVAffinator::takeNonNegativeAssumption(PWACtx &PWAC) {
101
139
  auto *NegPWA = isl_pw_aff_neg(PWAC.first.copy());
102
139
  auto *NegDom = isl_pw_aff_pos_set(NegPWA);
103
139
  PWAC.second =
104
139
      isl::manage(isl_set_union(PWAC.second.take(), isl_set_copy(NegDom)));
105
139
  auto *Restriction = BB ? 
NegDom131
:
isl_set_params(NegDom)8
;
106
139
  auto DL = BB ? 
BB->getTerminator()->getDebugLoc()131
:
DebugLoc()8
;
107
139
  S->recordAssumption(UNSIGNED, isl::manage(Restriction), DL, AS_RESTRICTION,
108
139
                      BB);
109
139
}
110
111
19.4k
PWACtx SCEVAffinator::getPWACtxFromPWA(isl::pw_aff PWA) {
112
19.4k
  return std::make_pair(PWA, isl::set::empty(isl::space(Ctx, 0, NumIterators)));
113
19.4k
}
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.66k
    auto *DC = S->getDomainConditions(BB).release();
120
9.66k
    NumIterators = isl_set_n_dim(DC);
121
9.66k
    isl_set_free(DC);
122
9.66k
  } else
123
530
    NumIterators = 0;
124
10.1k
125
10.1k
  return visit(Expr);
126
10.1k
}
127
128
22.7k
PWACtx SCEVAffinator::checkForWrapping(const SCEV *Expr, PWACtx PWAC) const {
129
22.7k
  // If the SCEV flags do contain NSW (no signed wrap) then PWA already
130
22.7k
  // represents Expr in modulo semantic (it is not allowed to overflow), thus we
131
22.7k
  // are done. Otherwise, we will compute:
132
22.7k
  //   PWA = ((PWA + 2^(n-1)) mod (2 ^ n)) - 2^(n-1)
133
22.7k
  // whereas n is the number of bits of the Expr, hence:
134
22.7k
  //   n = bitwidth(ExprType)
135
22.7k
136
22.7k
  if (IgnoreIntegerWrapping || (getNoWrapFlags(Expr) & SCEV::FlagNSW))
137
19.8k
    return PWAC;
138
2.92k
139
2.92k
  isl::pw_aff PWAMod = addModuloSemantic(PWAC.first, Expr->getType());
140
2.92k
141
2.92k
  isl::set NotEqualSet = PWAC.first.ne_set(PWAMod);
142
2.92k
  PWAC.second = PWAC.second.unite(NotEqualSet).coalesce();
143
2.92k
144
2.92k
  const DebugLoc &Loc = BB ? 
BB->getTerminator()->getDebugLoc()2.92k
:
DebugLoc()5
;
145
2.92k
  if (!BB)
146
5
    NotEqualSet = NotEqualSet.params();
147
2.92k
  NotEqualSet = NotEqualSet.coalesce();
148
2.92k
149
2.92k
  if (!NotEqualSet.is_empty())
150
2.88k
    S->recordAssumption(WRAPPING, NotEqualSet, Loc, AS_RESTRICTION, BB);
151
2.92k
152
2.92k
  return PWAC;
153
2.92k
}
154
155
isl::pw_aff SCEVAffinator::addModuloSemantic(isl::pw_aff PWA,
156
3.01k
                                             Type *ExprType) const {
157
3.01k
  unsigned Width = TD.getTypeSizeInBits(ExprType);
158
3.01k
159
3.01k
  auto ModVal = isl::val::int_from_ui(Ctx, Width);
160
3.01k
  ModVal = ModVal.two_exp();
161
3.01k
162
3.01k
  isl::set Domain = PWA.domain();
163
3.01k
  isl::pw_aff AddPW =
164
3.01k
      isl::manage(getWidthExpValOnDomain(Width - 1, Domain.take()));
165
3.01k
166
3.01k
  return PWA.add(AddPW).mod(ModVal).sub(AddPW);
167
3.01k
}
168
169
1.68k
bool SCEVAffinator::hasNSWAddRecForLoop(Loop *L) const {
170
10.1k
  for (const auto &CachedPair : CachedExpressions) {
171
10.1k
    auto *AddRec = dyn_cast<SCEVAddRecExpr>(CachedPair.first.first);
172
10.1k
    if (!AddRec)
173
6.04k
      continue;
174
4.10k
    if (AddRec->getLoop() != L)
175
2.23k
      continue;
176
1.87k
    if (AddRec->getNoWrapFlags() & SCEV::FlagNSW)
177
1.30k
      return true;
178
1.87k
  }
179
1.68k
180
1.68k
  
return false378
;
181
1.68k
}
182
183
35.3k
bool SCEVAffinator::computeModuloForExpr(const SCEV *Expr) {
184
35.3k
  unsigned Width = TD.getTypeSizeInBits(Expr->getType());
185
35.3k
  // We assume nsw expressions never overflow.
186
35.3k
  if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr))
187
14.6k
    if (NAry->getNoWrapFlags() & SCEV::FlagNSW)
188
9.98k
      return false;
189
25.3k
  return Width <= MaxSmallBitWidth;
190
25.3k
}
191
192
16.9k
PWACtx SCEVAffinator::visit(const SCEV *Expr) {
193
16.9k
194
16.9k
  auto Key = std::make_pair(Expr, BB);
195
16.9k
  PWACtx PWAC = CachedExpressions[Key];
196
16.9k
  if (PWAC.first)
197
4.63k
    return PWAC;
198
12.3k
199
12.3k
  auto ConstantAndLeftOverPair = extractConstantFactor(Expr, SE);
200
12.3k
  auto *Factor = ConstantAndLeftOverPair.first;
201
12.3k
  Expr = ConstantAndLeftOverPair.second;
202
12.3k
203
12.3k
  auto *Scope = getScope();
204
12.3k
  S->addParams(getParamsInAffineExpr(&S->getRegion(), Scope, Expr, SE));
205
12.3k
206
12.3k
  // In case the scev is a valid parameter, we do not further analyze this
207
12.3k
  // expression, but create a new parameter in the isl_pw_aff. This allows us
208
12.3k
  // to treat subexpressions that we cannot translate into an piecewise affine
209
12.3k
  // expression, as constant parameters of the piecewise affine expression.
210
12.3k
  if (isl_id *Id = S->getIdForParam(Expr).release()) {
211
1.83k
    isl_space *Space = isl_space_set_alloc(Ctx.get(), 1, NumIterators);
212
1.83k
    Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id);
213
1.83k
214
1.83k
    isl_set *Domain = isl_set_universe(isl_space_copy(Space));
215
1.83k
    isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space));
216
1.83k
    Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1);
217
1.83k
218
1.83k
    PWAC = getPWACtxFromPWA(isl::manage(isl_pw_aff_alloc(Domain, Affine)));
219
10.5k
  } else {
220
10.5k
    PWAC = SCEVVisitor<SCEVAffinator, PWACtx>::visit(Expr);
221
10.5k
    if (computeModuloForExpr(Expr))
222
63
      PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
223
10.4k
    else
224
10.4k
      PWAC = checkForWrapping(Expr, PWAC);
225
10.5k
  }
226
12.3k
227
12.3k
  if (!Factor->getType()->isIntegerTy(1)) {
228
12.3k
    PWAC = combine(PWAC, visitConstant(Factor), isl_pw_aff_mul);
229
12.3k
    if (computeModuloForExpr(Key.first))
230
21
      PWAC.first = addModuloSemantic(PWAC.first, Expr->getType());
231
12.3k
  }
232
12.3k
233
12.3k
  // For compile time reasons we need to simplify the PWAC before we cache and
234
12.3k
  // return it.
235
12.3k
  PWAC.first = PWAC.first.coalesce();
236
12.3k
  if (!computeModuloForExpr(Key.first))
237
12.2k
    PWAC = checkForWrapping(Key.first, PWAC);
238
12.3k
239
12.3k
  CachedExpressions[Key] = PWAC;
240
12.3k
  return PWAC;
241
12.3k
}
242
243
17.6k
PWACtx SCEVAffinator::visitConstant(const SCEVConstant *Expr) {
244
17.6k
  ConstantInt *Value = Expr->getValue();
245
17.6k
  isl_val *v;
246
17.6k
247
17.6k
  // LLVM does not define if an integer value is interpreted as a signed or
248
17.6k
  // unsigned value. Hence, without further information, it is unknown how
249
17.6k
  // this value needs to be converted to GMP. At the moment, we only support
250
17.6k
  // signed operations. So we just interpret it as signed. Later, there are
251
17.6k
  // two options:
252
17.6k
  //
253
17.6k
  // 1. We always interpret any value as signed and convert the values on
254
17.6k
  //    demand.
255
17.6k
  // 2. We pass down the signedness of the calculation and use it to interpret
256
17.6k
  //    this constant correctly.
257
17.6k
  v = isl_valFromAPInt(Ctx.get(), Value->getValue(), /* isSigned */ true);
258
17.6k
259
17.6k
  isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators);
260
17.6k
  isl_local_space *ls = isl_local_space_from_space(Space);
261
17.6k
  return getPWACtxFromPWA(
262
17.6k
      isl::manage(isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v))));
263
17.6k
}
264
265
50
PWACtx SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) {
266
50
  // Truncate operations are basically modulo operations, thus we can
267
50
  // model them that way. However, for large types we assume the operand
268
50
  // to fit in the new type size instead of introducing a modulo with a very
269
50
  // large constant.
270
50
271
50
  auto *Op = Expr->getOperand();
272
50
  auto OpPWAC = visit(Op);
273
50
274
50
  unsigned Width = TD.getTypeSizeInBits(Expr->getType());
275
50
276
50
  if (computeModuloForExpr(Expr))
277
36
    return OpPWAC;
278
14
279
14
  auto *Dom = OpPWAC.first.domain().take();
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
111
PWACtx SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
301
111
  // A zero-extended value can be interpreted as a piecewise defined signed
302
111
  // value. If the value was non-negative it stays the same, otherwise it
303
111
  // is the sum of the original value and 2^n where n is the bit-width of
304
111
  // the original (or operand) type. Examples:
305
111
  //   zext i8 127 to i32 -> { [127] }
306
111
  //   zext i8  -1 to i32 -> { [256 + (-1)] } = { [255] }
307
111
  //   zext i8  %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 }
308
111
  //
309
111
  // However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a
310
111
  // truncate) to represent some forms of modulo computation. The left-hand side
311
111
  // of the condition in the code below would result in the SCEV
312
111
  // "zext i1 <false, +, true>for.body" which is just another description
313
111
  // of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0".
314
111
  //
315
111
  //   for (i = 0; i < N; i++)
316
111
  //     if (i & 1 != 0 /* == i % 2 */)
317
111
  //       /* do something */
318
111
  //
319
111
  // If we do not make the modulo explicit but only use the mechanism described
320
111
  // above we will get the very restrictive assumption "N < 3", because for all
321
111
  // values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap.
322
111
  // Alternatively, we can make the modulo in the operand explicit in the
323
111
  // resulting piecewise function and thereby avoid the assumption on N. For the
324
111
  // example this would result in the following piecewise affine function:
325
111
  // { [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0;
326
111
  //   [i0] -> [(0)] : 2*floor((i0)/2) = i0 }
327
111
  // To this end we can first determine if the (immediate) operand of the
328
111
  // zero-extend can wrap and, in case it might, we will use explicit modulo
329
111
  // semantic to compute the result instead of emitting non-wrapping
330
111
  // assumptions.
331
111
  //
332
111
  // Note that operands with large bit-widths are less likely to be negative
333
111
  // because it would result in a very large access offset or loop bound after
334
111
  // the zero-extend. To this end one can optimistically assume the operand to
335
111
  // be positive and avoid the piecewise definition if the bit-width is bigger
336
111
  // than some threshold (here MaxZextSmallBitWidth).
337
111
  //
338
111
  // We choose to go with a hybrid solution of all modeling techniques described
339
111
  // above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the
340
111
  // wrapping explicitly and use a piecewise defined function. However, if the
341
111
  // bit-width is bigger than MaxZextSmallBitWidth we will employ overflow
342
111
  // assumptions and assume the "former negative" piece will not exist.
343
111
344
111
  auto *Op = Expr->getOperand();
345
111
  auto OpPWAC = visit(Op);
346
111
347
111
  // If the width is to big we assume the negative part does not occur.
348
111
  if (!computeModuloForExpr(Op)) {
349
66
    takeNonNegativeAssumption(OpPWAC);
350
66
    return OpPWAC;
351
66
  }
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
381
PWACtx SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
361
381
  // As all values are represented as signed, a sign extension is a noop.
362
381
  return visit(Expr->getOperand());
363
381
}
364
365
245
PWACtx SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) {
366
245
  PWACtx Sum = visit(Expr->getOperand(0));
367
245
368
518
  for (int i = 1, e = Expr->getNumOperands(); i < e; 
++i273
) {
369
273
    Sum = combine(Sum, visit(Expr->getOperand(i)), isl_pw_aff_add);
370
273
    if (isTooComplex(Sum))
371
0
      return std::make_pair(nullptr, nullptr);
372
273
  }
373
245
374
245
  return Sum;
375
245
}
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 std::make_pair(nullptr, nullptr);
384
1
  }
385
1
386
1
  return Prod;
387
1
}
388
389
4.23k
PWACtx SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) {
390
4.23k
  assert(Expr->isAffine() && "Only affine AddRecurrences allowed");
391
4.23k
392
4.23k
  auto Flags = Expr->getNoWrapFlags();
393
4.23k
394
4.23k
  // Directly generate isl_pw_aff for Expr if 'start' is zero.
395
4.23k
  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.15k
413
1.15k
  // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}'
414
1.15k
  // if 'start' is not zero.
415
1.15k
  // TODO: Using the original SCEV no-wrap flags is not always safe, however
416
1.15k
  //       as our code generation is reordering the expression anyway it doesn't
417
1.15k
  //       really matter.
418
1.15k
  const SCEV *ZeroStartExpr =
419
1.15k
      SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0),
420
1.15k
                       Expr->getStepRecurrence(SE), Expr->getLoop(), Flags);
421
1.15k
422
1.15k
  PWACtx Result = visit(ZeroStartExpr);
423
1.15k
  PWACtx Start = visit(Expr->getStart());
424
1.15k
  Result = combine(Result, Start, isl_pw_aff_add);
425
1.15k
  return Result;
426
1.15k
}
427
428
50
PWACtx SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) {
429
50
  PWACtx Max = visit(Expr->getOperand(0));
430
50
431
106
  for (int i = 1, e = Expr->getNumOperands(); i < e; 
++i56
) {
432
57
    Max = combine(Max, visit(Expr->getOperand(i)), isl_pw_aff_max);
433
57
    if (isTooComplex(Max))
434
1
      return std::make_pair(nullptr, nullptr);
435
57
  }
436
50
437
50
  
return Max49
;
438
50
}
439
440
0
PWACtx SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) {
441
0
  llvm_unreachable("SCEVUMaxExpr not yet supported");
442
0
}
443
444
41
PWACtx SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) {
445
41
  // The handling of unsigned division is basically the same as for signed
446
41
  // division, except the interpretation of the operands. As the divisor
447
41
  // has to be constant in both cases we can simply interpret it as an
448
41
  // unsigned value without additional complexity in the representation.
449
41
  // For the dividend we could choose from the different representation
450
41
  // schemes introduced for zero-extend operations but for now we will
451
41
  // simply use an assumption.
452
41
  auto *Dividend = Expr->getLHS();
453
41
  auto *Divisor = Expr->getRHS();
454
41
  assert(isa<SCEVConstant>(Divisor) &&
455
41
         "UDiv is no parameter but has a non-constant RHS.");
456
41
457
41
  auto DividendPWAC = visit(Dividend);
458
41
  auto DivisorPWAC = visit(Divisor);
459
41
460
41
  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().take();
466
2
    auto *WidthExpPWA = getWidthExpValOnDomain(Width, DivisorDom);
467
2
    DivisorPWAC.first = DivisorPWAC.first.add(isl::manage(WidthExpPWA));
468
2
  }
469
41
470
41
  // TODO: One can represent the dividend as piece-wise function to be more
471
41
  //       precise but therefor a heuristic is needed.
472
41
473
41
  // Assume a non-negative dividend.
474
41
  takeNonNegativeAssumption(DividendPWAC);
475
41
476
41
  DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_div);
477
41
  DividendPWAC.first = DividendPWAC.first.floor();
478
41
479
41
  return DividendPWAC;
480
41
}
481
482
39
PWACtx SCEVAffinator::visitSDivInstruction(Instruction *SDiv) {
483
39
  assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!");
484
39
485
39
  auto *Scope = getScope();
486
39
  auto *Divisor = SDiv->getOperand(1);
487
39
  auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope);
488
39
  auto DivisorPWAC = visit(DivisorSCEV);
489
39
  assert(isa<SCEVConstant>(DivisorSCEV) &&
490
39
         "SDiv is no parameter but has a non-constant RHS.");
491
39
492
39
  auto *Dividend = SDiv->getOperand(0);
493
39
  auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope);
494
39
  auto DividendPWAC = visit(DividendSCEV);
495
39
  DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_q);
496
39
  return DividendPWAC;
497
39
}
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
88
PWACtx SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) {
517
88
  if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) {
518
88
    switch (I->getOpcode()) {
519
88
    case Instruction::IntToPtr:
520
4
      return visit(SE.getSCEVAtScope(I->getOperand(0), getScope()));
521
88
    case Instruction::PtrToInt:
522
8
      return visit(SE.getSCEVAtScope(I->getOperand(0), getScope()));
523
88
    case Instruction::SDiv:
524
39
      return visitSDivInstruction(I);
525
88
    case Instruction::SRem:
526
37
      return visitSRemInstruction(I);
527
88
    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
}