Coverage Report

Created: 2019-02-15 18:59

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