Coverage Report

Created: 2018-06-19 22:08

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