Coverage Report

Created: 2017-10-03 07:32

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