Coverage Report

Created: 2018-12-13 20:48

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Analysis/DependenceInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DependenceInfo.cpp - Calculate dependency information for a Scop. --===//
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
// Calculate the data dependency relations for a Scop using ISL.
11
//
12
// The integer set library (ISL) from Sven, has a integrated dependency analysis
13
// to calculate data dependences. This pass takes advantage of this and
14
// calculate those dependences a Scop.
15
//
16
// The dependences in this pass are exact in terms that for a specific read
17
// statement instance only the last write statement instance is returned. In
18
// case of may writes a set of possible write instances is returned. This
19
// analysis will never produce redundant dependences.
20
//
21
//===----------------------------------------------------------------------===//
22
//
23
#include "polly/DependenceInfo.h"
24
#include "polly/LinkAllPasses.h"
25
#include "polly/Options.h"
26
#include "polly/ScopInfo.h"
27
#include "polly/Support/GICHelper.h"
28
#include "polly/Support/ISLTools.h"
29
#include "llvm/Support/Debug.h"
30
#include <isl/aff.h>
31
#include <isl/ctx.h>
32
#include <isl/flow.h>
33
#include <isl/map.h>
34
#include <isl/options.h>
35
#include <isl/schedule.h>
36
#include <isl/set.h>
37
#include <isl/union_map.h>
38
#include <isl/union_set.h>
39
40
using namespace polly;
41
using namespace llvm;
42
43
#define DEBUG_TYPE "polly-dependence"
44
45
static cl::opt<int> OptComputeOut(
46
    "polly-dependences-computeout",
47
    cl::desc("Bound the dependence analysis by a maximal amount of "
48
             "computational steps (0 means no bound)"),
49
    cl::Hidden, cl::init(500000), cl::ZeroOrMore, cl::cat(PollyCategory));
50
51
static cl::opt<bool> LegalityCheckDisabled(
52
    "disable-polly-legality", cl::desc("Disable polly legality check"),
53
    cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
54
55
static cl::opt<bool>
56
    UseReductions("polly-dependences-use-reductions",
57
                  cl::desc("Exploit reductions in dependence analysis"),
58
                  cl::Hidden, cl::init(true), cl::ZeroOrMore,
59
                  cl::cat(PollyCategory));
60
61
enum AnalysisType { VALUE_BASED_ANALYSIS, MEMORY_BASED_ANALYSIS };
62
63
static cl::opt<enum AnalysisType> OptAnalysisType(
64
    "polly-dependences-analysis-type",
65
    cl::desc("The kind of dependence analysis to use"),
66
    cl::values(clEnumValN(VALUE_BASED_ANALYSIS, "value-based",
67
                          "Exact dependences without transitive dependences"),
68
               clEnumValN(MEMORY_BASED_ANALYSIS, "memory-based",
69
                          "Overapproximation of dependences")),
70
    cl::Hidden, cl::init(VALUE_BASED_ANALYSIS), cl::ZeroOrMore,
71
    cl::cat(PollyCategory));
72
73
static cl::opt<Dependences::AnalysisLevel> OptAnalysisLevel(
74
    "polly-dependences-analysis-level",
75
    cl::desc("The level of dependence analysis"),
76
    cl::values(clEnumValN(Dependences::AL_Statement, "statement-wise",
77
                          "Statement-level analysis"),
78
               clEnumValN(Dependences::AL_Reference, "reference-wise",
79
                          "Memory reference level analysis that distinguish"
80
                          " accessed references in the same statement"),
81
               clEnumValN(Dependences::AL_Access, "access-wise",
82
                          "Memory reference level analysis that distinguish"
83
                          " access instructions in the same statement")),
84
    cl::Hidden, cl::init(Dependences::AL_Statement), cl::ZeroOrMore,
85
    cl::cat(PollyCategory));
86
87
//===----------------------------------------------------------------------===//
88
89
/// Tag the @p Relation domain with @p TagId
90
static __isl_give isl_map *tag(__isl_take isl_map *Relation,
91
368
                               __isl_take isl_id *TagId) {
92
368
  isl_space *Space = isl_map_get_space(Relation);
93
368
  Space = isl_space_drop_dims(Space, isl_dim_out, 0,
94
368
                              isl_map_dim(Relation, isl_dim_out));
95
368
  Space = isl_space_set_tuple_id(Space, isl_dim_out, TagId);
96
368
  isl_multi_aff *Tag = isl_multi_aff_domain_map(Space);
97
368
  Relation = isl_map_preimage_domain_multi_aff(Relation, Tag);
98
368
  return Relation;
99
368
}
100
101
/// Tag the @p Relation domain with either MA->getArrayId() or
102
///        MA->getId() based on @p TagLevel
103
static __isl_give isl_map *tag(__isl_take isl_map *Relation, MemoryAccess *MA,
104
1.68k
                               Dependences::AnalysisLevel TagLevel) {
105
1.68k
  if (TagLevel == Dependences::AL_Reference)
106
252
    return tag(Relation, MA->getArrayId().release());
107
1.42k
108
1.42k
  if (TagLevel == Dependences::AL_Access)
109
116
    return tag(Relation, MA->getId().release());
110
1.31k
111
1.31k
  // No need to tag at the statement level.
112
1.31k
  return Relation;
113
1.31k
}
114
115
/// Collect information about the SCoP @p S.
116
static void collectInfo(Scop &S, isl_union_map *&Read,
117
                        isl_union_map *&MustWrite, isl_union_map *&MayWrite,
118
                        isl_union_map *&ReductionTagMap,
119
                        isl_union_set *&TaggedStmtDomain,
120
604
                        Dependences::AnalysisLevel Level) {
121
604
  isl_space *Space = S.getParamSpace().release();
122
604
  Read = isl_union_map_empty(isl_space_copy(Space));
123
604
  MustWrite = isl_union_map_empty(isl_space_copy(Space));
124
604
  MayWrite = isl_union_map_empty(isl_space_copy(Space));
125
604
  ReductionTagMap = isl_union_map_empty(isl_space_copy(Space));
126
604
  isl_union_map *StmtSchedule = isl_union_map_empty(Space);
127
604
128
604
  SmallPtrSet<const ScopArrayInfo *, 8> ReductionArrays;
129
604
  if (UseReductions)
130
604
    for (ScopStmt &Stmt : S)
131
1.02k
      for (MemoryAccess *MA : Stmt)
132
1.87k
        if (MA->isReductionLike())
133
334
          ReductionArrays.insert(MA->getScopArrayInfo());
134
604
135
1.02k
  for (ScopStmt &Stmt : S) {
136
1.87k
    for (MemoryAccess *MA : Stmt) {
137
1.87k
      isl_set *domcp = Stmt.getDomain().release();
138
1.87k
      isl_map *accdom = MA->getAccessRelation().release();
139
1.87k
140
1.87k
      accdom = isl_map_intersect_domain(accdom, domcp);
141
1.87k
142
1.87k
      if (ReductionArrays.count(MA->getScopArrayInfo())) {
143
383
        // Wrap the access domain and adjust the schedule accordingly.
144
383
        //
145
383
        // An access domain like
146
383
        //   Stmt[i0, i1] -> MemAcc_A[i0 + i1]
147
383
        // will be transformed into
148
383
        //   [Stmt[i0, i1] -> MemAcc_A[i0 + i1]] -> MemAcc_A[i0 + i1]
149
383
        //
150
383
        // We collect all the access domains in the ReductionTagMap.
151
383
        // This is used in Dependences::calculateDependences to create
152
383
        // a tagged Schedule tree.
153
383
154
383
        ReductionTagMap =
155
383
            isl_union_map_add_map(ReductionTagMap, isl_map_copy(accdom));
156
383
        accdom = isl_map_range_map(accdom);
157
1.49k
      } else {
158
1.49k
        accdom = tag(accdom, MA, Level);
159
1.49k
        if (Level > Dependences::AL_Statement) {
160
184
          isl_map *StmtScheduleMap = Stmt.getSchedule().release();
161
184
          assert(StmtScheduleMap &&
162
184
                 "Schedules that contain extension nodes require special "
163
184
                 "handling.");
164
184
          isl_map *Schedule = tag(StmtScheduleMap, MA, Level);
165
184
          StmtSchedule = isl_union_map_add_map(StmtSchedule, Schedule);
166
184
        }
167
1.49k
      }
168
1.87k
169
1.87k
      if (MA->isRead())
170
756
        Read = isl_union_map_add_map(Read, accdom);
171
1.12k
      else if (MA->isMayWrite())
172
34
        MayWrite = isl_union_map_add_map(MayWrite, accdom);
173
1.08k
      else
174
1.08k
        MustWrite = isl_union_map_add_map(MustWrite, accdom);
175
1.87k
    }
176
1.02k
177
1.02k
    if (!ReductionArrays.empty() && 
Level == Dependences::AL_Statement207
)
178
191
      StmtSchedule =
179
191
          isl_union_map_add_map(StmtSchedule, Stmt.getSchedule().release());
180
1.02k
  }
181
604
182
604
  StmtSchedule = isl_union_map_intersect_params(
183
604
      StmtSchedule, S.getAssumedContext().release());
184
604
  TaggedStmtDomain = isl_union_map_domain(StmtSchedule);
185
604
186
604
  ReductionTagMap = isl_union_map_coalesce(ReductionTagMap);
187
604
  Read = isl_union_map_coalesce(Read);
188
604
  MustWrite = isl_union_map_coalesce(MustWrite);
189
604
  MayWrite = isl_union_map_coalesce(MayWrite);
190
604
}
191
192
/// Fix all dimension of @p Zero to 0 and add it to @p user
193
93
static void fixSetToZero(isl::set Zero, isl::union_set *User) {
194
351
  for (unsigned i = 0; i < Zero.dim(isl::dim::set); 
i++258
)
195
258
    Zero = Zero.fix_si(isl::dim::set, i, 0);
196
93
  *User = User->add_set(Zero);
197
93
}
198
199
/// Compute the privatization dependences for a given dependency @p Map
200
///
201
/// Privatization dependences are widened original dependences which originate
202
/// or end in a reduction access. To compute them we apply the transitive close
203
/// of the reduction dependences (which maps each iteration of a reduction
204
/// statement to all following ones) on the RAW/WAR/WAW dependences. The
205
/// dependences which start or end at a reduction statement will be extended to
206
/// depend on all following reduction statement iterations as well.
207
/// Note: "Following" here means according to the reduction dependences.
208
///
209
/// For the input:
210
///
211
///  S0:   *sum = 0;
212
///        for (int i = 0; i < 1024; i++)
213
///  S1:     *sum += i;
214
///  S2:   *sum = *sum * 3;
215
///
216
/// we have the following dependences before we add privatization dependences:
217
///
218
///   RAW:
219
///     { S0[] -> S1[0]; S1[1023] -> S2[] }
220
///   WAR:
221
///     {  }
222
///   WAW:
223
///     { S0[] -> S1[0]; S1[1024] -> S2[] }
224
///   RED:
225
///     { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
226
///
227
/// and afterwards:
228
///
229
///   RAW:
230
///     { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
231
///       S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
232
///   WAR:
233
///     {  }
234
///   WAW:
235
///     { S0[] -> S1[i0] : i0 >= 0 and i0 <= 1023;
236
///       S1[i0] -> S2[] : i0 >= 0 and i0 <= 1023}
237
///   RED:
238
///     { S1[i0] -> S1[1 + i0] : i0 >= 0 and i0 <= 1022 }
239
///
240
/// Note: This function also computes the (reverse) transitive closure of the
241
///       reduction dependences.
242
73
void Dependences::addPrivatizationDependences() {
243
73
  isl_union_map *PrivRAW, *PrivWAW, *PrivWAR;
244
73
245
73
  // The transitive closure might be over approximated, thus could lead to
246
73
  // dependency cycles in the privatization dependences. To make sure this
247
73
  // will not happen we remove all negative dependences after we computed
248
73
  // the transitive closure.
249
73
  TC_RED = isl_union_map_transitive_closure(isl_union_map_copy(RED), nullptr);
250
73
251
73
  // FIXME: Apply the current schedule instead of assuming the identity schedule
252
73
  //        here. The current approach is only valid as long as we compute the
253
73
  //        dependences only with the initial (identity schedule). Any other
254
73
  //        schedule could change "the direction of the backward dependences" we
255
73
  //        want to eliminate here.
256
73
  isl_union_set *UDeltas = isl_union_map_deltas(isl_union_map_copy(TC_RED));
257
73
  isl_union_set *Universe = isl_union_set_universe(isl_union_set_copy(UDeltas));
258
73
  isl::union_set Zero =
259
73
      isl::manage(isl_union_set_empty(isl_union_set_get_space(Universe)));
260
73
261
73
  for (isl::set Set : isl::manage_copy(Universe).get_set_list())
262
93
    fixSetToZero(Set, &Zero);
263
73
264
73
  isl_union_map *NonPositive =
265
73
      isl_union_set_lex_le_union_set(UDeltas, Zero.release());
266
73
267
73
  TC_RED = isl_union_map_subtract(TC_RED, NonPositive);
268
73
269
73
  TC_RED = isl_union_map_union(
270
73
      TC_RED, isl_union_map_reverse(isl_union_map_copy(TC_RED)));
271
73
  TC_RED = isl_union_map_coalesce(TC_RED);
272
73
273
73
  isl_union_map **Maps[] = {&RAW, &WAW, &WAR};
274
73
  isl_union_map **PrivMaps[] = {&PrivRAW, &PrivWAW, &PrivWAR};
275
292
  for (unsigned u = 0; u < 3; 
u++219
) {
276
219
    isl_union_map **Map = Maps[u], **PrivMap = PrivMaps[u];
277
219
278
219
    *PrivMap = isl_union_map_apply_range(isl_union_map_copy(*Map),
279
219
                                         isl_union_map_copy(TC_RED));
280
219
    *PrivMap = isl_union_map_union(
281
219
        *PrivMap, isl_union_map_apply_range(isl_union_map_copy(TC_RED),
282
219
                                            isl_union_map_copy(*Map)));
283
219
284
219
    *Map = isl_union_map_union(*Map, *PrivMap);
285
219
  }
286
73
287
73
  isl_union_set_free(Universe);
288
73
}
289
290
static __isl_give isl_union_flow *buildFlow(__isl_keep isl_union_map *Snk,
291
                                            __isl_keep isl_union_map *Src,
292
                                            __isl_keep isl_union_map *MaySrc,
293
2.41k
                                            __isl_keep isl_schedule *Schedule) {
294
2.41k
  isl_union_access_info *AI;
295
2.41k
296
2.41k
  AI = isl_union_access_info_from_sink(isl_union_map_copy(Snk));
297
2.41k
  if (MaySrc)
298
2.41k
    AI = isl_union_access_info_set_may_source(AI, isl_union_map_copy(MaySrc));
299
2.41k
  if (Src)
300
2.39k
    AI = isl_union_access_info_set_must_source(AI, isl_union_map_copy(Src));
301
2.41k
  AI = isl_union_access_info_set_schedule(AI, isl_schedule_copy(Schedule));
302
2.41k
  auto Flow = isl_union_access_info_compute_flow(AI);
303
2.41k
  LLVM_DEBUG(if (!Flow) dbgs()
304
2.41k
                 << "last error: "
305
2.41k
                 << isl_ctx_last_error(isl_schedule_get_ctx(Schedule))
306
2.41k
                 << '\n';);
307
2.41k
  return Flow;
308
2.41k
}
309
310
/// Compute exact WAR dependences
311
/// We need exact WAR dependences. That is, if there are
312
/// dependences of the form:
313
/// must-W2 (sink) <- must-W1 (sink) <- R (source)
314
/// We wish to generate *ONLY*:
315
/// { R -> W1 },
316
/// NOT:
317
/// { R -> W2, R -> W1 }
318
///
319
/// However, in the case of may-writes, we do *not* wish to allow
320
/// may-writes to block must-writes. This makes sense, since perhaps the
321
/// may-write will not happen. In that case, the exact dependence will
322
/// be the (read -> must-write).
323
/// Example:
324
/// must-W2 (sink) <- may-W1 (sink) <- R (source)
325
/// We wish to generate:
326
/// { R-> W1, R -> W2 }
327
///
328
/// We use the fact that may dependences are not allowed to flow
329
/// through a must source. That way, reads will be stopped by intermediate
330
/// must-writes.
331
/// However, may-sources may not interfere with one another. Hence, reads
332
/// will not block each other from generating dependences.
333
///
334
/// Write (Sink) <- MustWrite (Must-Source) <- Read (MaySource) is
335
/// present, then the dependence
336
///    { Write <- Read }
337
/// is not tracked.
338
///
339
/// We would like to specify the Must-Write as kills, source as Read
340
/// and sink as Write.
341
/// ISL does not have the functionality currently to support "kills".
342
/// Use the Must-Source as a way to specify "kills".
343
/// The drawback is that we will have both
344
///   { Write <- MustWrite, Write <- Read }
345
///
346
/// We need to filter this to track only { Write <- Read }.
347
///
348
/// Filtering { Write <- Read } from WAROverestimated:
349
/// --------------------------------------------------
350
/// isl_union_flow_get_full_may_dependence gives us dependences of the form
351
///   WAROverestimated = { Read+MustWrite -> [Write -> MemoryAccess]}
352
///
353
///  We need to intersect the domain with Read to get only
354
///  Read dependences.
355
///    Read = { Read -> MemoryAccess }
356
///
357
///
358
/// 1. Construct:
359
///   WARMemAccesses = { Read+Write -> [Read+Write -> MemoryAccess] }
360
/// This takes a Read+Write from WAROverestimated and maps it to the
361
/// corresponding wrapped memory access from WAROverestimated.
362
///
363
/// 2. Apply WARMemAcesses to the domain of WAR Overestimated to give:
364
///   WAR = { [Read+Write -> MemoryAccess] -> [Write -> MemoryAccess] }
365
///
366
/// WAR is in a state where we can intersect with Read, since they
367
/// have the same structure.
368
///
369
/// 3. Intersect this with a wrapped Read. Read is wrapped
370
/// to ensure the domains look the same.
371
///   WAR = WAR \intersect (wrapped Read)
372
///   WAR = { [Read -> MemoryAccesss] -> [Write -> MemoryAccess] }
373
///
374
///  4. Project out the memory access in the domain to get
375
///  WAR = { Read -> Write }
376
static isl_union_map *buildWAR(isl_union_map *Write, isl_union_map *MustWrite,
377
598
                               isl_union_map *Read, isl_schedule *Schedule) {
378
598
  isl_union_flow *Flow = buildFlow(Write, MustWrite, Read, Schedule);
379
598
  auto *WAROverestimated = isl_union_flow_get_full_may_dependence(Flow);
380
598
381
598
  // 1. Constructing WARMemAccesses
382
598
  // WarMemAccesses = { Read+Write -> [Write -> MemAccess] }
383
598
  // Range factor of range product
384
598
  //     { Read+Write -> MemAcesss }
385
598
  // Domain projection
386
598
  //     { [Read+Write -> MemAccess] -> Read+Write }
387
598
  // Reverse
388
598
  //     { Read+Write -> [Read+Write -> MemAccess] }
389
598
  auto WARMemAccesses = isl_union_map_copy(WAROverestimated);
390
598
  WARMemAccesses = isl_union_map_range_factor_range(WAROverestimated);
391
598
  WARMemAccesses = isl_union_map_domain_map(WARMemAccesses);
392
598
  WARMemAccesses = isl_union_map_reverse(WARMemAccesses);
393
598
394
598
  // 2. Apply to get domain tagged with memory accesses
395
598
  isl_union_map *WAR =
396
598
      isl_union_map_apply_domain(WAROverestimated, WARMemAccesses);
397
598
398
598
  // 3. Intersect with Read to extract only reads
399
598
  auto ReadWrapped = isl_union_map_wrap(isl_union_map_copy(Read));
400
598
  WAR = isl_union_map_intersect_domain(WAR, ReadWrapped);
401
598
402
598
  // 4. Project out memory accesses to get usual style dependences
403
598
  WAR = isl_union_map_range_factor_domain(WAR);
404
598
  WAR = isl_union_map_domain_factor_domain(WAR);
405
598
406
598
  isl_union_flow_free(Flow);
407
598
  return WAR;
408
598
}
409
410
604
void Dependences::calculateDependences(Scop &S) {
411
604
  isl_union_map *Read, *MustWrite, *MayWrite, *ReductionTagMap;
412
604
  isl_schedule *Schedule;
413
604
  isl_union_set *TaggedStmtDomain;
414
604
415
604
  LLVM_DEBUG(dbgs() << "Scop: \n" << S << "\n");
416
604
417
604
  collectInfo(S, Read, MustWrite, MayWrite, ReductionTagMap, TaggedStmtDomain,
418
604
              Level);
419
604
420
604
  bool HasReductions = !isl_union_map_is_empty(ReductionTagMap);
421
604
422
604
  LLVM_DEBUG(dbgs() << "Read: " << Read << '\n';
423
604
             dbgs() << "MustWrite: " << MustWrite << '\n';
424
604
             dbgs() << "MayWrite: " << MayWrite << '\n';
425
604
             dbgs() << "ReductionTagMap: " << ReductionTagMap << '\n';
426
604
             dbgs() << "TaggedStmtDomain: " << TaggedStmtDomain << '\n';);
427
604
428
604
  Schedule = S.getScheduleTree().release();
429
604
430
604
  if (!HasReductions) {
431
497
    isl_union_map_free(ReductionTagMap);
432
497
    // Tag the schedule tree if we want fine-grain dependence info
433
497
    if (Level > AL_Statement) {
434
40
      auto TaggedMap =
435
40
          isl_union_set_unwrap(isl_union_set_copy(TaggedStmtDomain));
436
40
      auto Tags = isl_union_map_domain_map_union_pw_multi_aff(TaggedMap);
437
40
      Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
438
40
    }
439
497
  } else {
440
107
    isl_union_map *IdentityMap;
441
107
    isl_union_pw_multi_aff *ReductionTags, *IdentityTags, *Tags;
442
107
443
107
    // Extract Reduction tags from the combined access domains in the given
444
107
    // SCoP. The result is a map that maps each tagged element in the domain to
445
107
    // the memory location it accesses. ReductionTags = {[Stmt[i] ->
446
107
    // Array[f(i)]] -> Stmt[i] }
447
107
    ReductionTags =
448
107
        isl_union_map_domain_map_union_pw_multi_aff(ReductionTagMap);
449
107
450
107
    // Compute an identity map from each statement in domain to itself.
451
107
    // IdentityTags = { [Stmt[i] -> Stmt[i] }
452
107
    IdentityMap = isl_union_set_identity(isl_union_set_copy(TaggedStmtDomain));
453
107
    IdentityTags = isl_union_pw_multi_aff_from_union_map(IdentityMap);
454
107
455
107
    Tags = isl_union_pw_multi_aff_union_add(ReductionTags, IdentityTags);
456
107
457
107
    // By pulling back Tags from Schedule, we have a schedule tree that can
458
107
    // be used to compute normal dependences, as well as 'tagged' reduction
459
107
    // dependences.
460
107
    Schedule = isl_schedule_pullback_union_pw_multi_aff(Schedule, Tags);
461
107
  }
462
604
463
604
  LLVM_DEBUG(dbgs() << "Read: " << Read << "\n";
464
604
             dbgs() << "MustWrite: " << MustWrite << "\n";
465
604
             dbgs() << "MayWrite: " << MayWrite << "\n";
466
604
             dbgs() << "Schedule: " << Schedule << "\n");
467
604
468
604
  isl_union_map *StrictWAW = nullptr;
469
604
  {
470
604
    IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), OptComputeOut);
471
604
472
604
    RAW = WAW = WAR = RED = nullptr;
473
604
    isl_union_map *Write = isl_union_map_union(isl_union_map_copy(MustWrite),
474
604
                                               isl_union_map_copy(MayWrite));
475
604
476
604
    // We are interested in detecting reductions that do not have intermediate
477
604
    // computations that are captured by other statements.
478
604
    //
479
604
    // Example:
480
604
    // void f(int *A, int *B) {
481
604
    //     for(int i = 0; i <= 100; i++) {
482
604
    //
483
604
    //            *-WAR (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
484
604
    //            |                                                   |
485
604
    //            *-WAW (S0[i] -> S0[i + 1] 0 <= i <= 100)------------*
486
604
    //            |                                                   |
487
604
    //            v                                                   |
488
604
    //     S0:    *A += i; >------------------*-----------------------*
489
604
    //                                        |
490
604
    //         if (i >= 98) {          WAR (S0[i] -> S1[i]) 98 <= i <= 100
491
604
    //                                        |
492
604
    //     S1:        *B = *A; <--------------*
493
604
    //         }
494
604
    //     }
495
604
    // }
496
604
    //
497
604
    // S0[0 <= i <= 100] has a reduction. However, the values in
498
604
    // S0[98 <= i <= 100] is captured in S1[98 <= i <= 100].
499
604
    // Since we allow free reordering on our reduction dependences, we need to
500
604
    // remove all instances of a reduction statement that have data dependences
501
604
    // originating from them.
502
604
    // In the case of the example, we need to remove S0[98 <= i <= 100] from
503
604
    // our reduction dependences.
504
604
    //
505
604
    // When we build up the WAW dependences that are used to detect reductions,
506
604
    // we consider only **Writes that have no intermediate Reads**.
507
604
    //
508
604
    // `isl_union_flow_get_must_dependence` gives us dependences of the form:
509
604
    // (sink <- must_source).
510
604
    //
511
604
    // It *will not give* dependences of the form:
512
604
    // 1. (sink <- ... <- may_source <- ... <- must_source)
513
604
    // 2. (sink <- ... <- must_source <- ... <- must_source)
514
604
    //
515
604
    // For a detailed reference on ISL's flow analysis, see:
516
604
    // "Presburger Formulas and Polyhedral Compilation" - Approximate Dataflow
517
604
    //  Analysis.
518
604
    //
519
604
    // Since we set "Write" as a must-source, "Read" as a may-source, and ask
520
604
    // for must dependences, we get all Writes to Writes that **do not flow
521
604
    // through a Read**.
522
604
    //
523
604
    // ScopInfo::checkForReductions makes sure that if something captures
524
604
    // the reduction variable in the same basic block, then it is rejected
525
604
    // before it is even handed here. This makes sure that there is exactly
526
604
    // one read and one write to a reduction variable in a Statement.
527
604
    // Example:
528
604
    //     void f(int *sum, int A[N], int B[N]) {
529
604
    //       for (int i = 0; i < N; i++) {
530
604
    //         *sum += A[i]; < the store and the load is not tagged as a
531
604
    //         B[i] = *sum;  < reduction-like access due to the overlap.
532
604
    //       }
533
604
    //     }
534
604
535
604
    isl_union_flow *Flow = buildFlow(Write, Write, Read, Schedule);
536
604
    StrictWAW = isl_union_flow_get_must_dependence(Flow);
537
604
    isl_union_flow_free(Flow);
538
604
539
604
    if (OptAnalysisType == VALUE_BASED_ANALYSIS) {
540
598
      Flow = buildFlow(Read, MustWrite, MayWrite, Schedule);
541
598
      RAW = isl_union_flow_get_may_dependence(Flow);
542
598
      isl_union_flow_free(Flow);
543
598
544
598
      Flow = buildFlow(Write, MustWrite, MayWrite, Schedule);
545
598
      WAW = isl_union_flow_get_may_dependence(Flow);
546
598
      isl_union_flow_free(Flow);
547
598
548
598
      WAR = buildWAR(Write, MustWrite, Read, Schedule);
549
598
      isl_union_map_free(Write);
550
598
      isl_schedule_free(Schedule);
551
598
    } else {
552
6
      isl_union_flow *Flow;
553
6
554
6
      Flow = buildFlow(Read, nullptr, Write, Schedule);
555
6
      RAW = isl_union_flow_get_may_dependence(Flow);
556
6
      isl_union_flow_free(Flow);
557
6
558
6
      Flow = buildFlow(Write, nullptr, Read, Schedule);
559
6
      WAR = isl_union_flow_get_may_dependence(Flow);
560
6
      isl_union_flow_free(Flow);
561
6
562
6
      Flow = buildFlow(Write, nullptr, Write, Schedule);
563
6
      WAW = isl_union_flow_get_may_dependence(Flow);
564
6
      isl_union_flow_free(Flow);
565
6
566
6
      isl_union_map_free(Write);
567
6
      isl_schedule_free(Schedule);
568
6
    }
569
604
570
604
    isl_union_map_free(MustWrite);
571
604
    isl_union_map_free(MayWrite);
572
604
    isl_union_map_free(Read);
573
604
574
604
    RAW = isl_union_map_coalesce(RAW);
575
604
    WAW = isl_union_map_coalesce(WAW);
576
604
    WAR = isl_union_map_coalesce(WAR);
577
604
578
604
    // End of max_operations scope.
579
604
  }
580
604
581
604
  if (isl_ctx_last_error(IslCtx.get()) == isl_error_quota) {
582
6
    isl_union_map_free(RAW);
583
6
    isl_union_map_free(WAW);
584
6
    isl_union_map_free(WAR);
585
6
    isl_union_map_free(StrictWAW);
586
6
    RAW = WAW = WAR = StrictWAW = nullptr;
587
6
    isl_ctx_reset_error(IslCtx.get());
588
6
  }
589
604
590
604
  // Drop out early, as the remaining computations are only needed for
591
604
  // reduction dependences or dependences that are finer than statement
592
604
  // level dependences.
593
604
  if (!HasReductions && 
Level == AL_Statement497
) {
594
457
    RED = isl_union_map_empty(isl_union_map_get_space(RAW));
595
457
    TC_RED = isl_union_map_empty(isl_union_set_get_space(TaggedStmtDomain));
596
457
    isl_union_set_free(TaggedStmtDomain);
597
457
    isl_union_map_free(StrictWAW);
598
457
    return;
599
457
  }
600
147
601
147
  isl_union_map *STMT_RAW, *STMT_WAW, *STMT_WAR;
602
147
  STMT_RAW = isl_union_map_intersect_domain(
603
147
      isl_union_map_copy(RAW), isl_union_set_copy(TaggedStmtDomain));
604
147
  STMT_WAW = isl_union_map_intersect_domain(
605
147
      isl_union_map_copy(WAW), isl_union_set_copy(TaggedStmtDomain));
606
147
  STMT_WAR =
607
147
      isl_union_map_intersect_domain(isl_union_map_copy(WAR), TaggedStmtDomain);
608
147
  LLVM_DEBUG({
609
147
    dbgs() << "Wrapped Dependences:\n";
610
147
    dump();
611
147
    dbgs() << "\n";
612
147
  });
613
147
614
147
  // To handle reduction dependences we proceed as follows:
615
147
  // 1) Aggregate all possible reduction dependences, namely all self
616
147
  //    dependences on reduction like statements.
617
147
  // 2) Intersect them with the actual RAW & WAW dependences to the get the
618
147
  //    actual reduction dependences. This will ensure the load/store memory
619
147
  //    addresses were __identical__ in the two iterations of the statement.
620
147
  // 3) Relax the original RAW, WAW and WAR dependences by subtracting the
621
147
  //    actual reduction dependences. Binary reductions (sum += A[i]) cause
622
147
  //    the same, RAW, WAW and WAR dependences.
623
147
  // 4) Add the privatization dependences which are widened versions of
624
147
  //    already present dependences. They model the effect of manual
625
147
  //    privatization at the outermost possible place (namely after the last
626
147
  //    write and before the first access to a reduction location).
627
147
628
147
  // Step 1)
629
147
  RED = isl_union_map_empty(isl_union_map_get_space(RAW));
630
294
  for (ScopStmt &Stmt : S) {
631
612
    for (MemoryAccess *MA : Stmt) {
632
612
      if (!MA->isReductionLike())
633
278
        continue;
634
334
      isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
635
334
      isl_map *Identity =
636
334
          isl_map_from_domain_and_range(isl_set_copy(AccDomW), AccDomW);
637
334
      RED = isl_union_map_add_map(RED, Identity);
638
334
    }
639
294
  }
640
147
641
147
  // Step 2)
642
147
  RED = isl_union_map_intersect(RED, isl_union_map_copy(RAW));
643
147
  RED = isl_union_map_intersect(RED, StrictWAW);
644
147
645
147
  if (!isl_union_map_is_empty(RED)) {
646
73
647
73
    // Step 3)
648
73
    RAW = isl_union_map_subtract(RAW, isl_union_map_copy(RED));
649
73
    WAW = isl_union_map_subtract(WAW, isl_union_map_copy(RED));
650
73
    WAR = isl_union_map_subtract(WAR, isl_union_map_copy(RED));
651
73
652
73
    // Step 4)
653
73
    addPrivatizationDependences();
654
73
  } else
655
74
    TC_RED = isl_union_map_empty(isl_union_map_get_space(RED));
656
147
657
147
  LLVM_DEBUG({
658
147
    dbgs() << "Final Wrapped Dependences:\n";
659
147
    dump();
660
147
    dbgs() << "\n";
661
147
  });
662
147
663
147
  // RED_SIN is used to collect all reduction dependences again after we
664
147
  // split them according to the causing memory accesses. The current assumption
665
147
  // is that our method of splitting will not have any leftovers. In the end
666
147
  // we validate this assumption until we have more confidence in this method.
667
147
  isl_union_map *RED_SIN = isl_union_map_empty(isl_union_map_get_space(RAW));
668
147
669
147
  // For each reduction like memory access, check if there are reduction
670
147
  // dependences with the access relation of the memory access as a domain
671
147
  // (wrapped space!). If so these dependences are caused by this memory access.
672
147
  // We then move this portion of reduction dependences back to the statement ->
673
147
  // statement space and add a mapping from the memory access to these
674
147
  // dependences.
675
294
  for (ScopStmt &Stmt : S) {
676
612
    for (MemoryAccess *MA : Stmt) {
677
612
      if (!MA->isReductionLike())
678
278
        continue;
679
334
680
334
      isl_set *AccDomW = isl_map_wrap(MA->getAccessRelation().release());
681
334
      isl_union_map *AccRedDepU = isl_union_map_intersect_domain(
682
334
          isl_union_map_copy(TC_RED), isl_union_set_from_set(AccDomW));
683
334
      if (isl_union_map_is_empty(AccRedDepU)) {
684
148
        isl_union_map_free(AccRedDepU);
685
148
        continue;
686
148
      }
687
186
688
186
      isl_map *AccRedDep = isl_map_from_union_map(AccRedDepU);
689
186
      RED_SIN = isl_union_map_add_map(RED_SIN, isl_map_copy(AccRedDep));
690
186
      AccRedDep = isl_map_zip(AccRedDep);
691
186
      AccRedDep = isl_set_unwrap(isl_map_domain(AccRedDep));
692
186
      setReductionDependences(MA, AccRedDep);
693
186
    }
694
294
  }
695
147
696
147
  assert(isl_union_map_is_equal(RED_SIN, TC_RED) &&
697
147
         "Intersecting the reduction dependence domain with the wrapped access "
698
147
         "relation is not enough, we need to loosen the access relation also");
699
147
  isl_union_map_free(RED_SIN);
700
147
701
147
  RAW = isl_union_map_zip(RAW);
702
147
  WAW = isl_union_map_zip(WAW);
703
147
  WAR = isl_union_map_zip(WAR);
704
147
  RED = isl_union_map_zip(RED);
705
147
  TC_RED = isl_union_map_zip(TC_RED);
706
147
707
147
  LLVM_DEBUG({
708
147
    dbgs() << "Zipped Dependences:\n";
709
147
    dump();
710
147
    dbgs() << "\n";
711
147
  });
712
147
713
147
  RAW = isl_union_set_unwrap(isl_union_map_domain(RAW));
714
147
  WAW = isl_union_set_unwrap(isl_union_map_domain(WAW));
715
147
  WAR = isl_union_set_unwrap(isl_union_map_domain(WAR));
716
147
  RED = isl_union_set_unwrap(isl_union_map_domain(RED));
717
147
  TC_RED = isl_union_set_unwrap(isl_union_map_domain(TC_RED));
718
147
719
147
  LLVM_DEBUG({
720
147
    dbgs() << "Unwrapped Dependences:\n";
721
147
    dump();
722
147
    dbgs() << "\n";
723
147
  });
724
147
725
147
  RAW = isl_union_map_union(RAW, STMT_RAW);
726
147
  WAW = isl_union_map_union(WAW, STMT_WAW);
727
147
  WAR = isl_union_map_union(WAR, STMT_WAR);
728
147
729
147
  RAW = isl_union_map_coalesce(RAW);
730
147
  WAW = isl_union_map_coalesce(WAW);
731
147
  WAR = isl_union_map_coalesce(WAR);
732
147
  RED = isl_union_map_coalesce(RED);
733
147
  TC_RED = isl_union_map_coalesce(TC_RED);
734
147
735
147
  LLVM_DEBUG(dump());
736
147
}
737
738
bool Dependences::isValidSchedule(
739
87
    Scop &S, const StatementToIslMapTy &NewSchedule) const {
740
87
  if (LegalityCheckDisabled)
741
0
    return true;
742
87
743
87
  isl::union_map Dependences = getDependences(TYPE_RAW | TYPE_WAW | TYPE_WAR);
744
87
  isl::space Space = S.getParamSpace();
745
87
  isl::union_map Schedule = isl::union_map::empty(Space);
746
87
747
87
  isl::space ScheduleSpace;
748
87
749
135
  for (ScopStmt &Stmt : S) {
750
135
    isl::map StmtScat;
751
135
752
135
    auto Lookup = NewSchedule.find(&Stmt);
753
135
    if (Lookup == NewSchedule.end())
754
0
      StmtScat = Stmt.getSchedule();
755
135
    else
756
135
      StmtScat = Lookup->second;
757
135
    assert(!StmtScat.is_null() &&
758
135
           "Schedules that contain extension nodes require special handling.");
759
135
760
135
    if (!ScheduleSpace)
761
87
      ScheduleSpace = StmtScat.get_space().range();
762
135
763
135
    Schedule = Schedule.add_map(StmtScat);
764
135
  }
765
87
766
87
  Dependences = Dependences.apply_domain(Schedule);
767
87
  Dependences = Dependences.apply_range(Schedule);
768
87
769
87
  isl::set Zero = isl::set::universe(ScheduleSpace);
770
307
  for (unsigned i = 0; i < Zero.dim(isl::dim::set); 
i++220
)
771
220
    Zero = Zero.fix_si(isl::dim::set, i, 0);
772
87
773
87
  isl::union_set UDeltas = Dependences.deltas();
774
87
  isl::set Deltas = singleton(UDeltas, ScheduleSpace);
775
87
776
87
  isl::map NonPositive = Deltas.lex_le_set(Zero);
777
87
  return NonPositive.is_empty();
778
87
}
779
780
// Check if the current scheduling dimension is parallel.
781
//
782
// We check for parallelism by verifying that the loop does not carry any
783
// dependences.
784
//
785
// Parallelism test: if the distance is zero in all outer dimensions, then it
786
// has to be zero in the current dimension as well.
787
//
788
// Implementation: first, translate dependences into time space, then force
789
// outer dimensions to be equal. If the distance is zero in the current
790
// dimension, then the loop is parallel. The distance is zero in the current
791
// dimension if it is a subset of a map with equal values for the current
792
// dimension.
793
bool Dependences::isParallel(isl_union_map *Schedule, isl_union_map *Deps,
794
609
                             isl_pw_aff **MinDistancePtr) const {
795
609
  isl_set *Deltas, *Distance;
796
609
  isl_map *ScheduleDeps;
797
609
  unsigned Dimension;
798
609
  bool IsParallel;
799
609
800
609
  Deps = isl_union_map_apply_range(Deps, isl_union_map_copy(Schedule));
801
609
  Deps = isl_union_map_apply_domain(Deps, isl_union_map_copy(Schedule));
802
609
803
609
  if (isl_union_map_is_empty(Deps)) {
804
329
    isl_union_map_free(Deps);
805
329
    return true;
806
329
  }
807
280
808
280
  ScheduleDeps = isl_map_from_union_map(Deps);
809
280
  Dimension = isl_map_dim(ScheduleDeps, isl_dim_out) - 1;
810
280
811
620
  for (unsigned i = 0; i < Dimension; 
i++340
)
812
340
    ScheduleDeps = isl_map_equate(ScheduleDeps, isl_dim_out, i, isl_dim_in, i);
813
280
814
280
  Deltas = isl_map_deltas(ScheduleDeps);
815
280
  Distance = isl_set_universe(isl_set_get_space(Deltas));
816
280
817
280
  // [0, ..., 0, +] - All zeros and last dimension larger than zero
818
620
  for (unsigned i = 0; i < Dimension; 
i++340
)
819
340
    Distance = isl_set_fix_si(Distance, isl_dim_set, i, 0);
820
280
821
280
  Distance = isl_set_lower_bound_si(Distance, isl_dim_set, Dimension, 1);
822
280
  Distance = isl_set_intersect(Distance, Deltas);
823
280
824
280
  IsParallel = isl_set_is_empty(Distance);
825
280
  if (IsParallel || 
!MinDistancePtr210
) {
826
227
    isl_set_free(Distance);
827
227
    return IsParallel;
828
227
  }
829
53
830
53
  Distance = isl_set_project_out(Distance, isl_dim_set, 0, Dimension);
831
53
  Distance = isl_set_coalesce(Distance);
832
53
833
53
  // This last step will compute a expression for the minimal value in the
834
53
  // distance polyhedron Distance with regards to the first (outer most)
835
53
  // dimension.
836
53
  *MinDistancePtr = isl_pw_aff_coalesce(isl_set_dim_min(Distance, 0));
837
53
838
53
  return false;
839
53
}
840
841
270
static void printDependencyMap(raw_ostream &OS, __isl_keep isl_union_map *DM) {
842
270
  if (DM)
843
261
    OS << DM << "\n";
844
9
  else
845
9
    OS << "n/a\n";
846
270
}
847
848
54
void Dependences::print(raw_ostream &OS) const {
849
54
  OS << "\tRAW dependences:\n\t\t";
850
54
  printDependencyMap(OS, RAW);
851
54
  OS << "\tWAR dependences:\n\t\t";
852
54
  printDependencyMap(OS, WAR);
853
54
  OS << "\tWAW dependences:\n\t\t";
854
54
  printDependencyMap(OS, WAW);
855
54
  OS << "\tReduction dependences:\n\t\t";
856
54
  printDependencyMap(OS, RED);
857
54
  OS << "\tTransitive closure of reduction dependences:\n\t\t";
858
54
  printDependencyMap(OS, TC_RED);
859
54
}
860
861
0
void Dependences::dump() const { print(dbgs()); }
862
863
582
void Dependences::releaseMemory() {
864
582
  isl_union_map_free(RAW);
865
582
  isl_union_map_free(WAR);
866
582
  isl_union_map_free(WAW);
867
582
  isl_union_map_free(RED);
868
582
  isl_union_map_free(TC_RED);
869
582
870
582
  RED = RAW = WAR = WAW = TC_RED = nullptr;
871
582
872
582
  for (auto &ReductionDeps : ReductionDependences)
873
154
    isl_map_free(ReductionDeps.second);
874
582
  ReductionDependences.clear();
875
582
}
876
877
768
isl::union_map Dependences::getDependences(int Kinds) const {
878
768
  assert(hasValidDependences() && "No valid dependences available");
879
768
  isl::space Space = isl::manage_copy(RAW).get_space();
880
768
  isl::union_map Deps = Deps.empty(Space);
881
768
882
768
  if (Kinds & TYPE_RAW)
883
549
    Deps = Deps.unite(isl::manage_copy(RAW));
884
768
885
768
  if (Kinds & TYPE_WAR)
886
511
    Deps = Deps.unite(isl::manage_copy(WAR));
887
768
888
768
  if (Kinds & TYPE_WAW)
889
511
    Deps = Deps.unite(isl::manage_copy(WAW));
890
768
891
768
  if (Kinds & TYPE_RED)
892
54
    Deps = Deps.unite(isl::manage_copy(RED));
893
768
894
768
  if (Kinds & TYPE_TC_RED)
895
258
    Deps = Deps.unite(isl::manage_copy(TC_RED));
896
768
897
768
  Deps = Deps.coalesce();
898
768
  Deps = Deps.detect_equalities();
899
768
  return Deps;
900
768
}
901
902
341
bool Dependences::hasValidDependences() const {
903
341
  return (RAW != nullptr) && 
(WAR != nullptr)338
&&
(WAW != nullptr)338
;
904
341
}
905
906
__isl_give isl_map *
907
0
Dependences::getReductionDependences(MemoryAccess *MA) const {
908
0
  return isl_map_copy(ReductionDependences.lookup(MA));
909
0
}
910
911
186
void Dependences::setReductionDependences(MemoryAccess *MA, isl_map *D) {
912
186
  assert(ReductionDependences.count(MA) == 0 &&
913
186
         "Reduction dependences set twice!");
914
186
  ReductionDependences[MA] = D;
915
186
}
916
917
const Dependences &
918
0
DependenceAnalysis::Result::getDependences(Dependences::AnalysisLevel Level) {
919
0
  if (Dependences *d = D[Level].get())
920
0
    return *d;
921
0
922
0
  return recomputeDependences(Level);
923
0
}
924
925
const Dependences &DependenceAnalysis::Result::recomputeDependences(
926
0
    Dependences::AnalysisLevel Level) {
927
0
  D[Level].reset(new Dependences(S.getSharedIslCtx(), Level));
928
0
  D[Level]->calculateDependences(S);
929
0
  return *D[Level];
930
0
}
931
932
DependenceAnalysis::Result
933
DependenceAnalysis::run(Scop &S, ScopAnalysisManager &SAM,
934
0
                        ScopStandardAnalysisResults &SAR) {
935
0
  return {S, {}};
936
0
}
937
938
AnalysisKey DependenceAnalysis::Key;
939
940
PreservedAnalyses
941
DependenceInfoPrinterPass::run(Scop &S, ScopAnalysisManager &SAM,
942
                               ScopStandardAnalysisResults &SAR,
943
0
                               SPMUpdater &U) {
944
0
  auto &DI = SAM.getResult<DependenceAnalysis>(S, SAR);
945
0
946
0
  if (auto d = DI.D[OptAnalysisLevel].get()) {
947
0
    d->print(OS);
948
0
    return PreservedAnalyses::all();
949
0
  }
950
0
951
0
  // Otherwise create the dependences on-the-fly and print them
952
0
  Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
953
0
  D.calculateDependences(S);
954
0
  D.print(OS);
955
0
956
0
  return PreservedAnalyses::all();
957
0
}
958
959
const Dependences &
960
609
DependenceInfo::getDependences(Dependences::AnalysisLevel Level) {
961
609
  if (Dependences *d = D[Level].get())
962
83
    return *d;
963
526
964
526
  return recomputeDependences(Level);
965
526
}
966
967
const Dependences &
968
531
DependenceInfo::recomputeDependences(Dependences::AnalysisLevel Level) {
969
531
  D[Level].reset(new Dependences(S->getSharedIslCtx(), Level));
970
531
  D[Level]->calculateDependences(*S);
971
531
  return *D[Level];
972
531
}
973
974
574
bool DependenceInfo::runOnScop(Scop &ScopVar) {
975
574
  S = &ScopVar;
976
574
  return false;
977
574
}
978
979
/// Print the dependences for the given SCoP to @p OS.
980
981
48
void polly::DependenceInfo::printScop(raw_ostream &OS, Scop &S) const {
982
48
  if (auto d = D[OptAnalysisLevel].get()) {
983
0
    d->print(OS);
984
0
    return;
985
0
  }
986
48
987
48
  // Otherwise create the dependences on-the-fly and print it
988
48
  Dependences D(S.getSharedIslCtx(), OptAnalysisLevel);
989
48
  D.calculateDependences(S);
990
48
  D.print(OS);
991
48
}
992
993
587
void DependenceInfo::getAnalysisUsage(AnalysisUsage &AU) const {
994
587
  AU.addRequiredTransitive<ScopInfoRegionPass>();
995
587
  AU.setPreservesAll();
996
587
}
997
998
char DependenceInfo::ID = 0;
999
1000
0
Pass *polly::createDependenceInfoPass() { return new DependenceInfo(); }
1001
1002
45.6k
INITIALIZE_PASS_BEGIN(DependenceInfo, "polly-dependences",
1003
45.6k
                      "Polly - Calculate dependences", false, false);
1004
45.6k
INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass);
1005
45.6k
INITIALIZE_PASS_END(DependenceInfo, "polly-dependences",
1006
                    "Polly - Calculate dependences", false, false)
1007
1008
//===----------------------------------------------------------------------===//
1009
const Dependences &
1010
DependenceInfoWrapperPass::getDependences(Scop *S,
1011
33
                                          Dependences::AnalysisLevel Level) {
1012
33
  auto It = ScopToDepsMap.find(S);
1013
33
  if (It != ScopToDepsMap.end())
1014
33
    if (It->second) {
1015
33
      if (It->second->getDependenceLevel() == Level)
1016
33
        return *It->second.get();
1017
0
    }
1018
0
  return recomputeDependences(S, Level);
1019
0
}
1020
1021
const Dependences &DependenceInfoWrapperPass::recomputeDependences(
1022
25
    Scop *S, Dependences::AnalysisLevel Level) {
1023
25
  std::unique_ptr<Dependences> D(new Dependences(S->getSharedIslCtx(), Level));
1024
25
  D->calculateDependences(*S);
1025
25
  auto Inserted = ScopToDepsMap.insert(std::make_pair(S, std::move(D)));
1026
25
  return *Inserted.first->second;
1027
25
}
1028
1029
26
bool DependenceInfoWrapperPass::runOnFunction(Function &F) {
1030
26
  auto &SI = *getAnalysis<ScopInfoWrapperPass>().getSI();
1031
26
  for (auto &It : SI) {
1032
25
    assert(It.second && "Invalid SCoP object!");
1033
25
    recomputeDependences(It.second.get(), Dependences::AL_Access);
1034
25
  }
1035
26
  return false;
1036
26
}
1037
1038
7
void DependenceInfoWrapperPass::print(raw_ostream &OS, const Module *M) const {
1039
7
  for (auto &It : ScopToDepsMap) {
1040
6
    assert((It.first && It.second) && "Invalid Scop or Dependence object!\n");
1041
6
    It.second->print(OS);
1042
6
  }
1043
7
}
1044
1045
26
void DependenceInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
1046
26
  AU.addRequiredTransitive<ScopInfoWrapperPass>();
1047
26
  AU.setPreservesAll();
1048
26
}
1049
1050
char DependenceInfoWrapperPass::ID = 0;
1051
1052
0
Pass *polly::createDependenceInfoWrapperPassPass() {
1053
0
  return new DependenceInfoWrapperPass();
1054
0
}
1055
1056
45.6k
INITIALIZE_PASS_BEGIN(
1057
45.6k
    DependenceInfoWrapperPass, "polly-function-dependences",
1058
45.6k
    "Polly - Calculate dependences for all the SCoPs of a function", false,
1059
45.6k
    false)
1060
45.6k
INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass);
1061
45.6k
INITIALIZE_PASS_END(
1062
    DependenceInfoWrapperPass, "polly-function-dependences",
1063
    "Polly - Calculate dependences for all the SCoPs of a function", false,
1064
    false)