Coverage Report

Created: 2017-03-28 09:59

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/Transform/DeLICM.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ DeLICM.cpp -----------------------------------------*- C++ -*-===//
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
// Undo the effect of Loop Invariant Code Motion (LICM) and
11
// GVN Partial Redundancy Elimination (PRE) on SCoP-level.
12
//
13
// Namely, remove register/scalar dependencies by mapping them back to array
14
// elements.
15
//
16
// The algorithms here work on the scatter space - the image space of the
17
// schedule returned by Scop::getSchedule(). We call an element in that space a
18
// "timepoint". Timepoints are lexicographically ordered such that we can
19
// defined ranges in the scatter space. We use two flavors of such ranges:
20
// Timepoint sets and zones. A timepoint set is simply a subset of the scatter
21
// space and is directly stored as isl_set.
22
//
23
// Zones are used to describe the space between timepoints as open sets, i.e.
24
// they do not contain the extrema. Using isl rational sets to express these
25
// would be overkill. We also cannot store them as the integer timepoints they
26
// contain; the (nonempty) zone between 1 and 2 would be empty and
27
// indistinguishable from e.g. the zone between 3 and 4. Also, we cannot store
28
// the integer set including the extrema; the set ]1,2[ + ]3,4[ could be
29
// coalesced to ]1,3[, although we defined the range [2,3] to be not in the set.
30
// Instead, we store the "half-open" integer extrema, including the lower bound,
31
// but excluding the upper bound. Examples:
32
//
33
// * The set { [i] : 1 <= i <= 3 } represents the zone ]0,3[ (which contains the
34
//   integer points 1 and 2, but not 0 or 3)
35
//
36
// * { [1] } represents the zone ]0,1[
37
//
38
// * { [i] : i = 1 or i = 3 } represents the zone ]0,1[ + ]2,3[
39
//
40
// Therefore, an integer i in the set represents the zone ]i-1,i[, i.e. strictly
41
// speaking the integer points never belong to the zone. However, depending an
42
// the interpretation, one might want to include them. Part of the
43
// interpretation may not be known when the zone is constructed.
44
//
45
// Reads are assumed to always take place before writes, hence we can think of
46
// reads taking place at the beginning of a timepoint and writes at the end.
47
//
48
// Let's assume that the zone represents the lifetime of a variable. That is,
49
// the zone begins with a write that defines the value during its lifetime and
50
// ends with the last read of that value. In the following we consider whether a
51
// read/write at the beginning/ending of the lifetime zone should be within the
52
// zone or outside of it.
53
//
54
// * A read at the timepoint that starts the live-range loads the previous
55
//   value. Hence, exclude the timepoint starting the zone.
56
//
57
// * A write at the timepoint that starts the live-range is not defined whether
58
//   it occurs before or after the write that starts the lifetime. We do not
59
//   allow this situation to occur. Hence, we include the timepoint starting the
60
//   zone to determine whether they are conflicting.
61
//
62
// * A read at the timepoint that ends the live-range reads the same variable.
63
//   We include the timepoint at the end of the zone to include that read into
64
//   the live-range. Doing otherwise would mean that the two reads access
65
//   different values, which would mean that the value they read are both alive
66
//   at the same time but occupy the same variable.
67
//
68
// * A write at the timepoint that ends the live-range starts a new live-range.
69
//   It must not be included in the live-range of the previous definition.
70
//
71
// All combinations of reads and writes at the endpoints are possible, but most
72
// of the time only the write->read (for instance, a live-range from definition
73
// to last use) and read->write (for instance, an unused range from last use to
74
// overwrite) and combinations are interesting (half-open ranges). write->write
75
// zones might be useful as well in some context to represent
76
// output-dependencies.
77
//
78
// @see convertZoneToTimepoints
79
//
80
//
81
// The code makes use of maps and sets in many different spaces. To not loose
82
// track in which space a set or map is expected to be in, variables holding an
83
// isl reference are usually annotated in the comments. They roughly follow isl
84
// syntax for spaces, but only the tuples, not the dimensions. The tuples have a
85
// meaning as follows:
86
//
87
// * Space[] - An unspecified tuple. Used for function parameters such that the
88
//             function caller can use it for anything they like.
89
//
90
// * Domain[] - A statement instance as returned by ScopStmt::getDomain()
91
//     isl_id_get_name: Stmt_<NameOfBasicBlock>
92
//     isl_id_get_user: Pointer to ScopStmt
93
//
94
// * Element[] - An array element as in the range part of
95
//               MemoryAccess::getAccessRelation()
96
//     isl_id_get_name: MemRef_<NameOfArrayVariable>
97
//     isl_id_get_user: Pointer to ScopArrayInfo
98
//
99
// * Scatter[] - Scatter space or space of timepoints
100
//     Has no tuple id
101
//
102
// * Zone[] - Range between timepoints as described above
103
//     Has no tuple id
104
//
105
// An annotation "{ Domain[] -> Scatter[] }" therefore means: A map from a
106
// statement instance to a timepoint, aka a schedule. There is only one scatter
107
// space, but most of the time multiple statements are processed in one set.
108
// This is why most of the time isl_union_map has to be used.
109
//
110
// The basic algorithm works as follows:
111
// At first we verify that the SCoP is compatible with this technique. For
112
// instance, two writes cannot write to the same location at the same statement
113
// instance because we cannot determine within the polyhedral model which one
114
// comes first. Once this was verified, we compute zones at which an array
115
// element is unused. This computation can fail if it takes too long. Then the
116
// main algorithm is executed. Because every store potentially trails an unused
117
// zone, we start at stores. We search for a scalar (MemoryKind::Value or
118
// MemoryKind::PHI) that we can map to the array element overwritten by the
119
// store, preferably one that is used by the store or at least the ScopStmt.
120
// When it does not conflict with the lifetime of the values in the array
121
// element, the map is applied and the unused zone updated as it is now used. We
122
// continue to try to map scalars to the array element until there are no more
123
// candidates to map. The algorithm is greedy in the sense that the first scalar
124
// not conflicting will be mapped. Other scalars processed later that could have
125
// fit the same unused zone will be rejected. As such the result depends on the
126
// processing order.
127
//
128
//===----------------------------------------------------------------------===//
129
130
#include "polly/DeLICM.h"
131
#include "polly/Options.h"
132
#include "polly/ScopInfo.h"
133
#include "polly/ScopPass.h"
134
#include "polly/Support/ISLTools.h"
135
#include "llvm/ADT/Statistic.h"
136
11
#define DEBUG_TYPE "polly-delicm"
137
138
using namespace polly;
139
using namespace llvm;
140
141
namespace {
142
143
cl::opt<int>
144
    DelicmMaxOps("polly-delicm-max-ops",
145
                 cl::desc("Maximum number of isl operations to invest for "
146
                          "lifetime analysis; 0=no limit"),
147
                 cl::init(1000000), cl::cat(PollyCategory));
148
149
cl::opt<bool> DelicmOverapproximateWrites(
150
    "polly-delicm-overapproximate-writes",
151
    cl::desc(
152
        "Do more PHI writes than necessary in order to avoid partial accesses"),
153
    cl::init(false), cl::Hidden, cl::cat(PollyCategory));
154
155
STATISTIC(DeLICMAnalyzed, "Number of successfully analyzed SCoPs");
156
STATISTIC(DeLICMOutOfQuota,
157
          "Analyses aborted because max_operations was reached");
158
STATISTIC(DeLICMIncompatible, "Number of SCoPs incompatible for analysis");
159
STATISTIC(MappedValueScalars, "Number of mapped Value scalars");
160
STATISTIC(MappedPHIScalars, "Number of mapped PHI scalars");
161
STATISTIC(TargetsMapped, "Number of stores used for at least one mapping");
162
STATISTIC(DeLICMScopsModified, "Number of SCoPs optimized");
163
164
/// Class for keeping track of scalar def-use chains in the polyhedral
165
/// representation.
166
///
167
/// MemoryKind::Value:
168
/// There is one definition per llvm::Value or zero (read-only values defined
169
/// before the SCoP) and an arbitrary number of reads.
170
///
171
/// MemoryKind::PHI, MemoryKind::ExitPHI:
172
/// There is at least one write (the incoming blocks/stmts) and one
173
/// (MemoryKind::PHI) or zero (MemoryKind::ExitPHI) reads per llvm::PHINode.
174
class ScalarDefUseChains {
175
private:
176
  /// The definitions (i.e. write MemoryAccess) of a MemoryKind::Value scalar.
177
  DenseMap<const ScopArrayInfo *, MemoryAccess *> ValueDefAccs;
178
179
  /// List of all uses (i.e. read MemoryAccesses) for a MemoryKind::Value
180
  /// scalar.
181
  DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>> ValueUseAccs;
182
183
  /// The receiving part (i.e. read MemoryAccess) of a MemoryKind::PHI scalar.
184
  DenseMap<const ScopArrayInfo *, MemoryAccess *> PHIReadAccs;
185
186
  /// List of all incoming values (write MemoryAccess) of a MemoryKind::PHI or
187
  /// MemoryKind::ExitPHI scalar.
188
  DenseMap<const ScopArrayInfo *, SmallVector<MemoryAccess *, 4>>
189
      PHIIncomingAccs;
190
191
public:
192
  /// Find the MemoryAccesses that access the ScopArrayInfo-represented memory.
193
  ///
194
  /// @param S The SCoP to analyze.
195
15
  void compute(Scop *S) {
196
15
    // Purge any previous result.
197
15
    reset();
198
15
199
74
    for (auto &Stmt : *S) {
200
143
      for (auto *MA : Stmt) {
201
143
        if (
MA->isOriginalValueKind() && 143
MA->isWrite()75
)
{31
202
31
          auto *SAI = MA->getScopArrayInfo();
203
31
          assert(!ValueDefAccs.count(SAI) &&
204
31
                 "There can be at most one "
205
31
                 "definition per MemoryKind::Value scalar");
206
31
          ValueDefAccs[SAI] = MA;
207
31
        }
208
143
209
143
        if (
MA->isOriginalValueKind() && 143
MA->isRead()75
)
210
44
          ValueUseAccs[MA->getScopArrayInfo()].push_back(MA);
211
143
212
143
        if (
MA->isOriginalAnyPHIKind() && 143
MA->isRead()50
)
{17
213
17
          auto *SAI = MA->getScopArrayInfo();
214
17
          assert(!PHIReadAccs.count(SAI) &&
215
17
                 "There must be exactly one read "
216
17
                 "per PHI (that's where the PHINode is)");
217
17
          PHIReadAccs[SAI] = MA;
218
17
        }
219
143
220
143
        if (
MA->isOriginalAnyPHIKind() && 143
MA->isWrite()50
)
221
33
          PHIIncomingAccs[MA->getScopArrayInfo()].push_back(MA);
222
143
      }
223
74
    }
224
15
  }
225
226
  /// Free all memory used by the analysis.
227
15
  void reset() {
228
15
    ValueDefAccs.clear();
229
15
    ValueUseAccs.clear();
230
15
    PHIReadAccs.clear();
231
15
    PHIIncomingAccs.clear();
232
15
  }
233
234
48
  MemoryAccess *getValueDef(const ScopArrayInfo *SAI) const {
235
48
    return ValueDefAccs.lookup(SAI);
236
48
  }
237
238
17
  ArrayRef<MemoryAccess *> getValueUses(const ScopArrayInfo *SAI) const {
239
17
    auto It = ValueUseAccs.find(SAI);
240
17
    if (It == ValueUseAccs.end())
241
0
      return {};
242
17
    return It->second;
243
17
  }
244
245
21
  MemoryAccess *getPHIRead(const ScopArrayInfo *SAI) const {
246
21
    return PHIReadAccs.lookup(SAI);
247
21
  }
248
249
18
  ArrayRef<MemoryAccess *> getPHIIncomings(const ScopArrayInfo *SAI) const {
250
18
    auto It = PHIIncomingAccs.find(SAI);
251
18
    if (It == PHIIncomingAccs.end())
252
0
      return {};
253
18
    return It->second;
254
18
  }
255
};
256
257
isl::union_map computeReachingDefinition(isl::union_map Schedule,
258
                                         isl::union_map Writes, bool InclDef,
259
9
                                         bool InclRedef) {
260
9
  return computeReachingWrite(Schedule, Writes, false, InclDef, InclRedef);
261
9
}
262
263
isl::union_map computeReachingOverwrite(isl::union_map Schedule,
264
                                        isl::union_map Writes,
265
                                        bool InclPrevWrite,
266
10
                                        bool InclOverwrite) {
267
10
  return computeReachingWrite(Schedule, Writes, true, InclPrevWrite,
268
10
                              InclOverwrite);
269
10
}
270
271
/// Compute the next overwrite for a scalar.
272
///
273
/// @param Schedule      { DomainWrite[] -> Scatter[] }
274
///                      Schedule of (at least) all writes. Instances not in @p
275
///                      Writes are ignored.
276
/// @param Writes        { DomainWrite[] }
277
///                      The element instances that write to the scalar.
278
/// @param InclPrevWrite Whether to extend the timepoints to include
279
///                      the timepoint where the previous write happens.
280
/// @param InclOverwrite Whether the reaching overwrite includes the timepoint
281
///                      of the overwrite itself.
282
///
283
/// @return { Scatter[] -> DomainDef[] }
284
isl::union_map computeScalarReachingOverwrite(isl::union_map Schedule,
285
                                              isl::union_set Writes,
286
                                              bool InclPrevWrite,
287
10
                                              bool InclOverwrite) {
288
10
289
10
  // { DomainWrite[] }
290
10
  auto WritesMap = give(isl_union_map_from_domain(Writes.take()));
291
10
292
10
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
293
10
  auto Result = computeReachingOverwrite(
294
10
      std::move(Schedule), std::move(WritesMap), InclPrevWrite, InclOverwrite);
295
10
296
10
  return give(isl_union_map_domain_factor_range(Result.take()));
297
10
}
298
299
/// Overload of computeScalarReachingOverwrite, with only one writing statement.
300
/// Consequently, the result consists of only one map space.
301
///
302
/// @param Schedule      { DomainWrite[] -> Scatter[] }
303
/// @param Writes        { DomainWrite[] }
304
/// @param InclPrevWrite Include the previous write to result.
305
/// @param InclOverwrite Include the overwrite to the result.
306
///
307
/// @return { Scatter[] -> DomainWrite[] }
308
isl::map computeScalarReachingOverwrite(isl::union_map Schedule,
309
                                        isl::set Writes, bool InclPrevWrite,
310
10
                                        bool InclOverwrite) {
311
10
  auto ScatterSpace = getScatterSpace(Schedule);
312
10
  auto DomSpace = give(isl_set_get_space(Writes.keep()));
313
10
314
10
  auto ReachOverwrite = computeScalarReachingOverwrite(
315
10
      Schedule, give(isl_union_set_from_set(Writes.take())), InclPrevWrite,
316
10
      InclOverwrite);
317
10
318
10
  auto ResultSpace = give(isl_space_map_from_domain_and_range(
319
10
      ScatterSpace.take(), DomSpace.take()));
320
10
  return singleton(std::move(ReachOverwrite), ResultSpace);
321
10
}
322
323
/// Compute the reaching definition of a scalar.
324
///
325
/// Compared to computeReachingDefinition, there is just one element which is
326
/// accessed and therefore only a set if instances that accesses that element is
327
/// required.
328
///
329
/// @param Schedule  { DomainWrite[] -> Scatter[] }
330
/// @param Writes    { DomainWrite[] }
331
/// @param InclDef   Include the timepoint of the definition to the result.
332
/// @param InclRedef Include the timepoint of the overwrite into the result.
333
///
334
/// @return { Scatter[] -> DomainWrite[] }
335
isl::union_map computeScalarReachingDefinition(isl::union_map Schedule,
336
                                               isl::union_set Writes,
337
9
                                               bool InclDef, bool InclRedef) {
338
9
339
9
  // { DomainWrite[] -> Element[] }
340
9
  auto Defs = give(isl_union_map_from_domain(Writes.take()));
341
9
342
9
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
343
9
  auto ReachDefs =
344
9
      computeReachingDefinition(Schedule, Defs, InclDef, InclRedef);
345
9
346
9
  // { Scatter[] -> DomainWrite[] }
347
9
  return give(isl_union_set_unwrap(
348
9
      isl_union_map_range(isl_union_map_curry(ReachDefs.take()))));
349
9
}
350
351
/// Compute the reaching definition of a scalar.
352
///
353
/// This overload accepts only a single writing statement as an isl_map,
354
/// consequently the result also is only a single isl_map.
355
///
356
/// @param Schedule  { DomainWrite[] -> Scatter[] }
357
/// @param Writes    { DomainWrite[] }
358
/// @param InclDef   Include the timepoint of the definition to the result.
359
/// @param InclRedef Include the timepoint of the overwrite into the result.
360
///
361
/// @return { Scatter[] -> DomainWrite[] }
362
isl::map computeScalarReachingDefinition( // { Domain[] -> Zone[] }
363
9
    isl::union_map Schedule, isl::set Writes, bool InclDef, bool InclRedef) {
364
9
  auto DomainSpace = give(isl_set_get_space(Writes.keep()));
365
9
  auto ScatterSpace = getScatterSpace(Schedule);
366
9
367
9
  //  { Scatter[] -> DomainWrite[] }
368
9
  auto UMap = computeScalarReachingDefinition(
369
9
      Schedule, give(isl_union_set_from_set(Writes.take())), InclDef,
370
9
      InclRedef);
371
9
372
9
  auto ResultSpace = give(isl_space_map_from_domain_and_range(
373
9
      ScatterSpace.take(), DomainSpace.take()));
374
9
  return singleton(UMap, ResultSpace);
375
9
}
376
377
/// If InputVal is not defined in the stmt itself, return the MemoryAccess that
378
/// reads the scalar. Return nullptr otherwise (if the value is defined in the
379
/// scop, or is synthesizable).
380
10
MemoryAccess *getInputAccessOf(Value *InputVal, ScopStmt *Stmt) {
381
20
  for (auto *MA : *Stmt) {
382
20
    if (!MA->isRead())
383
8
      continue;
384
12
    
if (12
!MA->isLatestScalarKind()12
)
385
1
      continue;
386
12
387
11
    assert(MA->getAccessValue() == MA->getBaseAddr());
388
11
    if (MA->getAccessValue() == InputVal)
389
8
      return MA;
390
11
  }
391
2
  return nullptr;
392
10
}
393
394
/// Try to find a 'natural' extension of a mapped to elements outside its
395
/// domain.
396
///
397
/// @param Relevant The map with mapping that may not be modified.
398
/// @param Universe The domain to which @p Relevant needs to be extended.
399
///
400
/// @return A map with that associates the domain elements of @p Relevant to the
401
///         same elements and in addition the elements of @p Universe to some
402
///         undefined elements. The function prefers to return simple maps.
403
2
isl::union_map expandMapping(isl::union_map Relevant, isl::union_set Universe) {
404
2
  Relevant = give(isl_union_map_coalesce(Relevant.take()));
405
2
  auto RelevantDomain = give(isl_union_map_domain(Relevant.copy()));
406
2
  auto Simplified =
407
2
      give(isl_union_map_gist_domain(Relevant.take(), RelevantDomain.take()));
408
2
  Simplified = give(isl_union_map_coalesce(Simplified.take()));
409
2
  return give(
410
2
      isl_union_map_intersect_domain(Simplified.take(), Universe.take()));
411
2
}
412
413
/// Represent the knowledge of the contents of any array elements in any zone or
414
/// the knowledge we would add when mapping a scalar to an array element.
415
///
416
/// Every array element at every zone unit has one of two states:
417
///
418
/// - Unused: Not occupied by any value so a transformation can change it to
419
///   other values.
420
///
421
/// - Occupied: The element contains a value that is still needed.
422
///
423
/// The union of Unused and Unknown zones forms the universe, the set of all
424
/// elements at every timepoint. The universe can easily be derived from the
425
/// array elements that are accessed someway. Arrays that are never accessed
426
/// also never play a role in any computation and can hence be ignored. With a
427
/// given universe, only one of the sets needs to stored implicitly. Computing
428
/// the complement is also an expensive operation, hence this class has been
429
/// designed that only one of sets is needed while the other is assumed to be
430
/// implicit. It can still be given, but is mostly ignored.
431
///
432
/// There are two use cases for the Knowledge class:
433
///
434
/// 1) To represent the knowledge of the current state of ScopInfo. The unused
435
///    state means that an element is currently unused: there is no read of it
436
///    before the next overwrite. Also called 'Existing'.
437
///
438
/// 2) To represent the requirements for mapping a scalar to array elements. The
439
///    unused state means that there is no change/requirement. Also called
440
///    'Proposed'.
441
///
442
/// In addition to these states at unit zones, Knowledge needs to know when
443
/// values are written. This is because written values may have no lifetime (one
444
/// reason is that the value is never read). Such writes would therefore never
445
/// conflict, but overwrite values that might still be required. Another source
446
/// of problems are multiple writes to the same element at the same timepoint,
447
/// because their order is undefined.
448
class Knowledge {
449
private:
450
  /// { [Element[] -> Zone[]] }
451
  /// Set of array elements and when they are alive.
452
  /// Can contain a nullptr; in this case the set is implicitly defined as the
453
  /// complement of #Unused.
454
  ///
455
  /// The set of alive array elements is represented as zone, as the set of live
456
  /// values can differ depending on how the elements are interpreted.
457
  /// Assuming a value X is written at timestep [0] and read at timestep [1]
458
  /// without being used at any later point, then the value is alive in the
459
  /// interval ]0,1[. This interval cannot be represented by an integer set, as
460
  /// it does not contain any integer point. Zones allow us to represent this
461
  /// interval and can be converted to sets of timepoints when needed (e.g., in
462
  /// isConflicting when comparing to the write sets).
463
  /// @see convertZoneToTimepoints and this file's comment for more details.
464
  isl::union_set Occupied;
465
466
  /// { [Element[] -> Zone[]] }
467
  /// Set of array elements when they are not alive, i.e. their memory can be
468
  /// used for other purposed. Can contain a nullptr; in this case the set is
469
  /// implicitly defined as the complement of #Occupied.
470
  isl::union_set Unused;
471
472
  /// { [Element[] -> Scatter[]] }
473
  /// The write actions currently in the scop or that would be added when
474
  /// mapping a scalar.
475
  isl::union_set Written;
476
477
  /// Check whether this Knowledge object is well-formed.
478
247
  void checkConsistency() const {
479
247
#ifndef NDEBUG
480
    // Default-initialized object
481
    if (!Occupied && !Unused && !Written)
482
      return;
483
484
    assert(Occupied || Unused);
485
    assert(Written);
486
487
    // If not all fields are defined, we cannot derived the universe.
488
    if (!Occupied || !Unused)
489
      return;
490
491
    assert(isl_union_set_is_disjoint(Occupied.keep(), Unused.keep()) ==
492
           isl_bool_true);
493
    auto Universe = give(isl_union_set_union(Occupied.copy(), Unused.copy()));
494
    assert(isl_union_set_is_subset(Written.keep(), Universe.keep()) ==
495
           isl_bool_true);
496
#endif
497
247
  }
498
499
public:
500
  /// Initialize a nullptr-Knowledge. This is only provided for convenience; do
501
  /// not use such an object.
502
38
  Knowledge() {}
503
504
  /// Create a new object with the given members.
505
  Knowledge(isl::union_set Occupied, isl::union_set Unused,
506
            isl::union_set Written)
507
      : Occupied(std::move(Occupied)), Unused(std::move(Unused)),
508
238
        Written(std::move(Written)) {
509
238
    checkConsistency();
510
238
  }
511
512
  /// Return whether this object was not default-constructed.
513
19
  bool isUsable() const 
{ return (Occupied || 19
Unused19
) &&
Written14
; }
514
515
  /// Print the content of this object to @p OS.
516
0
  void print(llvm::raw_ostream &OS, unsigned Indent = 0) const {
517
0
    if (isUsable()) {
518
0
      if (Occupied)
519
0
        OS.indent(Indent) << "Occupied: " << Occupied << "\n";
520
0
      else
521
0
        OS.indent(Indent) << "Occupied: <Everything else not in Unused>\n";
522
0
      if (Unused)
523
0
        OS.indent(Indent) << "Unused:   " << Unused << "\n";
524
0
      else
525
0
        OS.indent(Indent) << "Unused:   <Everything else not in Occupied>\n";
526
0
      OS.indent(Indent) << "Written : " << Written << '\n';
527
0
    } else {
528
0
      OS.indent(Indent) << "Invalid knowledge\n";
529
0
    }
530
0
  }
531
532
  /// Combine two knowledges, this and @p That.
533
9
  void learnFrom(Knowledge That) {
534
9
    assert(!isConflicting(*this, That));
535
9
    assert(Unused && That.Occupied);
536
9
    assert(
537
9
        !That.Unused &&
538
9
        "This function is only prepared to learn occupied elements from That");
539
9
    assert(!Occupied && "This function does not implement "
540
9
                        "`this->Occupied = "
541
9
                        "give(isl_union_set_union(this->Occupied.take(), "
542
9
                        "That.Occupied.copy()));`");
543
9
544
9
    Unused = give(isl_union_set_subtract(Unused.take(), That.Occupied.copy()));
545
9
    Written = give(isl_union_set_union(Written.take(), That.Written.take()));
546
9
547
9
    checkConsistency();
548
9
  }
549
550
  /// Determine whether two Knowledges conflict with each other.
551
  ///
552
  /// In theory @p Existing and @p Proposed are symmetric, but the
553
  /// implementation is constrained by the implicit interpretation. That is, @p
554
  /// Existing must have #Unused defined (use case 1) and @p Proposed must have
555
  /// #Occupied defined (use case 1).
556
  ///
557
  /// A conflict is defined as non-preserved semantics when they are merged. For
558
  /// instance, when for the same array and zone they assume different
559
  /// llvm::Values.
560
  ///
561
  /// @param Existing One of the knowledges with #Unused defined.
562
  /// @param Proposed One of the knowledges with #Occupied defined.
563
  /// @param OS       Dump the conflict reason to this output stream; use
564
  ///                 nullptr to not output anything.
565
  /// @param Indent   Indention for the conflict reason.
566
  ///
567
  /// @return True, iff the two knowledges are conflicting.
568
  static bool isConflicting(const Knowledge &Existing,
569
                            const Knowledge &Proposed,
570
                            llvm::raw_ostream *OS = nullptr,
571
120
                            unsigned Indent = 0) {
572
120
    assert(Existing.Unused);
573
120
    assert(Proposed.Occupied);
574
120
575
120
#ifndef NDEBUG
576
    if (Existing.Occupied && Proposed.Unused) {
577
      auto ExistingUniverse = give(isl_union_set_union(Existing.Occupied.copy(),
578
                                                       Existing.Unused.copy()));
579
      auto ProposedUniverse = give(isl_union_set_union(Proposed.Occupied.copy(),
580
                                                       Proposed.Unused.copy()));
581
      assert(isl_union_set_is_equal(ExistingUniverse.keep(),
582
                                    ProposedUniverse.keep()) == isl_bool_true &&
583
             "Both inputs' Knowledges must be over the same universe");
584
    }
585
#endif
586
120
587
120
    // Are the new lifetimes required for Proposed unused in Existing?
588
120
    if (isl_union_set_is_subset(Proposed.Occupied.keep(),
589
20
                                Existing.Unused.keep()) != isl_bool_true) {
590
20
      if (
OS20
)
{0
591
0
        auto ConflictingLifetimes = give(isl_union_set_subtract(
592
0
            Proposed.Occupied.copy(), Existing.Unused.copy()));
593
0
        OS->indent(Indent) << "Proposed lifetimes are not unused in existing\n";
594
0
        OS->indent(Indent) << "Conflicting lifetimes: " << ConflictingLifetimes
595
0
                           << "\n";
596
0
      }
597
20
      return true;
598
20
    }
599
120
600
120
    // Do the writes in Existing only overwrite unused values in Proposed?
601
120
    // We convert here the set of lifetimes to actual timepoints. A lifetime is
602
120
    // in conflict with a set of write timepoints, if either a live timepoint is
603
120
    // clearly within the lifetime or if a write happens at the beginning of the
604
120
    // lifetime (where it would conflict with the value that actually writes the
605
120
    // value alive). There is no conflict at the end of a lifetime, as the alive
606
120
    // value will always be read, before it is overwritten again. The last
607
120
    // property holds in Polly for all scalar values and we expect all users of
608
120
    // Knowledge to check this property also for accesses to MemoryKind::Array.
609
100
    auto ProposedFixedDefs =
610
100
        convertZoneToTimepoints(Proposed.Occupied, true, false);
611
100
    if (isl_union_set_is_disjoint(Existing.Written.keep(),
612
15
                                  ProposedFixedDefs.keep()) != isl_bool_true) {
613
15
      if (
OS15
)
{0
614
0
        auto ConflictingWrites = give(isl_union_set_intersect(
615
0
            Existing.Written.copy(), ProposedFixedDefs.copy()));
616
0
        OS->indent(Indent) << "Proposed writes into range used by existing\n";
617
0
        OS->indent(Indent) << "Conflicting writes: " << ConflictingWrites
618
0
                           << "\n";
619
0
      }
620
15
      return true;
621
15
    }
622
100
623
100
    // Do the new writes in Proposed only overwrite unused values in Existing?
624
85
    auto ExistingAvailableDefs =
625
85
        convertZoneToTimepoints(Existing.Unused, true, false);
626
85
    if (isl_union_set_is_subset(Proposed.Written.keep(),
627
85
                                ExistingAvailableDefs.keep()) !=
628
12
        isl_bool_true) {
629
12
      if (
OS12
)
{0
630
0
        auto ConflictingWrites = give(isl_union_set_subtract(
631
0
            Proposed.Written.copy(), ExistingAvailableDefs.copy()));
632
0
        OS->indent(Indent)
633
0
            << "Proposed a lifetime where there is an Existing write into it\n";
634
0
        OS->indent(Indent) << "Conflicting writes: " << ConflictingWrites
635
0
                           << "\n";
636
0
      }
637
12
      return true;
638
12
    }
639
85
640
85
    // Does Proposed write at the same time as Existing already does (order of
641
85
    // writes is undefined)?
642
73
    
if (73
isl_union_set_is_disjoint(Existing.Written.keep(),73
643
8
                                  Proposed.Written.keep()) != isl_bool_true) {
644
8
      if (
OS8
)
{0
645
0
        auto ConflictingWrites = give(isl_union_set_intersect(
646
0
            Existing.Written.copy(), Proposed.Written.copy()));
647
0
        OS->indent(Indent) << "Proposed writes at the same time as an already "
648
0
                              "Existing write\n";
649
0
        OS->indent(Indent) << "Conflicting writes: " << ConflictingWrites
650
0
                           << "\n";
651
0
      }
652
8
      return true;
653
8
    }
654
73
655
65
    return false;
656
73
  }
657
};
658
659
1
std::string printIntruction(Instruction *Instr, bool IsForDebug = false) {
660
1
  std::string Result;
661
1
  raw_string_ostream OS(Result);
662
1
  Instr->print(OS, IsForDebug);
663
1
  OS.flush();
664
1
  size_t i = 0;
665
3
  while (
i < Result.size() && 3
Result[i] == ' '3
)
666
2
    i += 1;
667
1
  return Result.substr(i);
668
1
}
669
670
/// Base class for algorithms based on zones, like DeLICM.
671
class ZoneAlgorithm {
672
protected:
673
  /// Hold a reference to the isl_ctx to avoid it being freed before we released
674
  /// all of the isl objects.
675
  ///
676
  /// This must be declared before any other member that holds an isl object.
677
  /// This guarantees that the shared_ptr and its isl_ctx is destructed last,
678
  /// after all other members free'd the isl objects they were holding.
679
  std::shared_ptr<isl_ctx> IslCtx;
680
681
  /// Cached reaching definitions for each ScopStmt.
682
  ///
683
  /// Use getScalarReachingDefinition() to get its contents.
684
  DenseMap<ScopStmt *, isl::map> ScalarReachDefZone;
685
686
  /// The analyzed Scop.
687
  Scop *S;
688
689
  /// Parameter space that does not need realignment.
690
  isl::space ParamSpace;
691
692
  /// Space the schedule maps to.
693
  isl::space ScatterSpace;
694
695
  /// Cached version of the schedule and domains.
696
  isl::union_map Schedule;
697
698
  /// Combined access relations of all MemoryKind::Array READ accesses.
699
  /// { DomainRead[] -> Element[] }
700
  isl::union_map AllReads;
701
702
  /// Combined access relations of all MemoryKind::Array, MAY_WRITE accesses.
703
  /// { DomainMayWrite[] -> Element[] }
704
  isl::union_map AllMayWrites;
705
706
  /// Combined access relations of all MemoryKind::Array, MUST_WRITE accesses.
707
  /// { DomainMustWrite[] -> Element[] }
708
  isl::union_map AllMustWrites;
709
710
  /// Prepare the object before computing the zones of @p S.
711
  ZoneAlgorithm(Scop *S)
712
19
      : IslCtx(S->getSharedIslCtx()), S(S), Schedule(give(S->getSchedule())) {
713
19
714
19
    auto Domains = give(S->getDomains());
715
19
716
19
    Schedule =
717
19
        give(isl_union_map_intersect_domain(Schedule.take(), Domains.take()));
718
19
    ParamSpace = give(isl_union_map_get_space(Schedule.keep()));
719
19
    ScatterSpace = getScatterSpace(Schedule);
720
19
  }
721
722
private:
723
  /// Check whether @p Stmt can be accurately analyzed by zones.
724
  ///
725
  /// What violates our assumptions:
726
  /// - A load after a write of the same location; we assume that all reads
727
  ///   occur before the writes.
728
  /// - Two writes to the same location; we cannot model the order in which
729
  ///   these occur.
730
  ///
731
  /// Scalar reads implicitly always occur before other accesses therefore never
732
  /// violate the first condition. There is also at most one write to a scalar,
733
  /// satisfying the second condition.
734
88
  bool isCompatibleStmt(ScopStmt *Stmt) {
735
88
    auto Stores = makeEmptyUnionMap();
736
88
    auto Loads = makeEmptyUnionMap();
737
88
738
88
    // This assumes that the MemoryKind::Array MemoryAccesses are iterated in
739
88
    // order.
740
170
    for (auto *MA : *Stmt) {
741
170
      if (!MA->isLatestArrayKind())
742
145
        continue;
743
170
744
25
      auto AccRel =
745
25
          give(isl_union_map_from_map(getAccessRelationFor(MA).take()));
746
25
747
25
      if (
MA->isRead()25
)
{3
748
3
        // Reject load after store to same location.
749
3
        if (
!isl_union_map_is_disjoint(Stores.keep(), AccRel.keep())3
)
{1
750
1
          OptimizationRemarkMissed R(DEBUG_TYPE, "LoadAfterStore",
751
1
                                     MA->getAccessInstruction());
752
1
          R << "load after store of same element in same statement";
753
1
          R << " (previous stores: " << Stores;
754
1
          R << ", loading: " << AccRel << ")";
755
1
          S->getFunction().getContext().diagnose(R);
756
1
          return false;
757
1
        }
758
3
759
2
        Loads = give(isl_union_map_union(Loads.take(), AccRel.take()));
760
2
761
2
        continue;
762
3
      }
763
25
764
22
      
if (22
!isa<StoreInst>(MA->getAccessInstruction())22
)
{1
765
1
        DEBUG(dbgs() << "WRITE that is not a StoreInst not supported\n");
766
1
        OptimizationRemarkMissed R(DEBUG_TYPE, "UnusualStore",
767
1
                                   MA->getAccessInstruction());
768
1
        R << "encountered write that is not a StoreInst: "
769
1
          << printIntruction(MA->getAccessInstruction());
770
1
        S->getFunction().getContext().diagnose(R);
771
1
        return false;
772
1
      }
773
22
774
22
      // In region statements the order is less clear, eg. the load and store
775
22
      // might be in a boxed loop.
776
21
      
if (21
Stmt->isRegionStmt() &&21
777
4
          
!isl_union_map_is_disjoint(Loads.keep(), AccRel.keep())4
)
{1
778
1
        OptimizationRemarkMissed R(DEBUG_TYPE, "StoreInSubregion",
779
1
                                   MA->getAccessInstruction());
780
1
        R << "store is in a non-affine subregion";
781
1
        S->getFunction().getContext().diagnose(R);
782
1
        return false;
783
1
      }
784
21
785
21
      // Do not allow more than one store to the same location.
786
20
      
if (20
!isl_union_map_is_disjoint(Stores.keep(), AccRel.keep())20
)
{1
787
1
        OptimizationRemarkMissed R(DEBUG_TYPE, "StoreAfterStore",
788
1
                                   MA->getAccessInstruction());
789
1
        R << "store after store of same element in same statement";
790
1
        R << " (previous stores: " << Stores;
791
1
        R << ", storing: " << AccRel << ")";
792
1
        S->getFunction().getContext().diagnose(R);
793
1
        return false;
794
1
      }
795
20
796
19
      Stores = give(isl_union_map_union(Stores.take(), AccRel.take()));
797
19
    }
798
88
799
84
    return true;
800
88
  }
801
802
1
  void addArrayReadAccess(MemoryAccess *MA) {
803
1
    assert(MA->isLatestArrayKind());
804
1
    assert(MA->isRead());
805
1
806
1
    // { DomainRead[] -> Element[] }
807
1
    auto AccRel = getAccessRelationFor(MA);
808
1
    AllReads = give(isl_union_map_add_map(AllReads.take(), AccRel.copy()));
809
1
  }
810
811
17
  void addArrayWriteAccess(MemoryAccess *MA) {
812
17
    assert(MA->isLatestArrayKind());
813
17
    assert(MA->isWrite());
814
17
815
17
    // { Domain[] -> Element[] }
816
17
    auto AccRel = getAccessRelationFor(MA);
817
17
818
17
    if (MA->isMustWrite())
819
14
      AllMustWrites =
820
14
          give(isl_union_map_add_map(AllMustWrites.take(), AccRel.copy()));
821
17
822
17
    if (MA->isMayWrite())
823
3
      AllMayWrites =
824
3
          give(isl_union_map_add_map(AllMayWrites.take(), AccRel.copy()));
825
17
  }
826
827
protected:
828
11
  isl::union_set makeEmptyUnionSet() const {
829
11
    return give(isl_union_set_empty(ParamSpace.copy()));
830
11
  }
831
832
227
  isl::union_map makeEmptyUnionMap() const {
833
227
    return give(isl_union_map_empty(ParamSpace.copy()));
834
227
  }
835
836
  /// Check whether @p S can be accurately analyzed by zones.
837
19
  bool isCompatibleScop() {
838
88
    for (auto &Stmt : *S) {
839
88
      if (!isCompatibleStmt(&Stmt))
840
4
        return false;
841
88
    }
842
15
    return true;
843
19
  }
844
845
  /// Get the schedule for @p Stmt.
846
  ///
847
  /// The domain of the result is as narrow as possible.
848
35
  isl::map getScatterFor(ScopStmt *Stmt) const {
849
35
    auto ResultSpace = give(isl_space_map_from_domain_and_range(
850
35
        Stmt->getDomainSpace(), ScatterSpace.copy()));
851
35
    return give(isl_union_map_extract_map(Schedule.keep(), ResultSpace.take()));
852
35
  }
853
854
  /// Get the schedule of @p MA's parent statement.
855
35
  isl::map getScatterFor(MemoryAccess *MA) const {
856
35
    return getScatterFor(MA->getStatement());
857
35
  }
858
859
  /// Get the schedule for the statement instances of @p Domain.
860
22
  isl::union_map getScatterFor(isl::union_set Domain) const {
861
22
    return give(isl_union_map_intersect_domain(Schedule.copy(), Domain.take()));
862
22
  }
863
864
  /// Get the schedule for the statement instances of @p Domain.
865
11
  isl::map getScatterFor(isl::set Domain) const {
866
11
    auto ResultSpace = give(isl_space_map_from_domain_and_range(
867
11
        isl_set_get_space(Domain.keep()), ScatterSpace.copy()));
868
11
    auto UDomain = give(isl_union_set_from_set(Domain.copy()));
869
11
    auto UResult = getScatterFor(std::move(UDomain));
870
11
    auto Result = singleton(std::move(UResult), std::move(ResultSpace));
871
11
    assert(isl_set_is_equal(give(isl_map_domain(Result.copy())).keep(),
872
11
                            Domain.keep()) == isl_bool_true);
873
11
    return Result;
874
11
  }
875
876
  /// Get the domain of @p Stmt.
877
156
  isl::set getDomainFor(ScopStmt *Stmt) const {
878
156
    return give(Stmt->getDomain());
879
156
  }
880
881
  /// Get the domain @p MA's parent statement.
882
137
  isl::set getDomainFor(MemoryAccess *MA) const {
883
137
    return getDomainFor(MA->getStatement());
884
137
  }
885
886
  /// Get the access relation of @p MA.
887
  ///
888
  /// The domain of the result is as narrow as possible.
889
65
  isl::map getAccessRelationFor(MemoryAccess *MA) const {
890
65
    auto Domain = getDomainFor(MA);
891
65
    auto AccRel = give(MA->getLatestAccessRelation());
892
65
    return give(isl_map_intersect_domain(AccRel.take(), Domain.take()));
893
65
  }
894
895
  /// Get the reaching definition of a scalar defined in @p Stmt.
896
  ///
897
  /// Note that this does not depend on the llvm::Instruction, only on the
898
  /// statement it is defined in. Therefore the same computation can be reused.
899
  ///
900
  /// @param Stmt The statement in which a scalar is defined.
901
  ///
902
  /// @return { Scatter[] -> DomainDef[] }
903
11
  isl::map getScalarReachingDefinition(ScopStmt *Stmt) {
904
11
    auto &Result = ScalarReachDefZone[Stmt];
905
11
    if (Result)
906
2
      return Result;
907
11
908
9
    auto Domain = getDomainFor(Stmt);
909
9
    Result = computeScalarReachingDefinition(Schedule, Domain, false, true);
910
9
    simplify(Result);
911
9
912
9
    assert(Result);
913
9
    return Result;
914
11
  }
915
916
  /// Compute the different zones.
917
15
  void computeCommon() {
918
15
    AllReads = makeEmptyUnionMap();
919
15
    AllMayWrites = makeEmptyUnionMap();
920
15
    AllMustWrites = makeEmptyUnionMap();
921
15
922
74
    for (auto &Stmt : *S) {
923
143
      for (auto *MA : Stmt) {
924
143
        if (!MA->isLatestArrayKind())
925
125
          continue;
926
143
927
18
        
if (18
MA->isRead()18
)
928
1
          addArrayReadAccess(MA);
929
18
930
18
        if (MA->isWrite())
931
17
          addArrayWriteAccess(MA);
932
18
      }
933
74
    }
934
15
935
15
    // { DomainWrite[] -> Element[] }
936
15
    auto AllWrites =
937
15
        give(isl_union_map_union(AllMustWrites.copy(), AllMayWrites.copy()));
938
15
  }
939
940
  /// Print the current state of all MemoryAccesses to @p.
941
3
  void printAccesses(llvm::raw_ostream &OS, int Indent = 0) const {
942
3
    OS.indent(Indent) << "After accesses {\n";
943
16
    for (auto &Stmt : *S) {
944
16
      OS.indent(Indent + 4) << Stmt.getBaseName() << "\n";
945
16
      for (auto *MA : Stmt)
946
37
        MA->print(OS);
947
16
    }
948
3
    OS.indent(Indent) << "}\n";
949
3
  }
950
951
public:
952
  /// Return the SCoP this object is analyzing.
953
0
  Scop *getScop() const { return S; }
954
};
955
956
/// Implementation of the DeLICM/DePRE transformation.
957
class DeLICMImpl : public ZoneAlgorithm {
958
private:
959
  /// Knowledge before any transformation took place.
960
  Knowledge OriginalZone;
961
962
  /// Current knowledge of the SCoP including all already applied
963
  /// transformations.
964
  Knowledge Zone;
965
966
  /// For getting the MemoryAccesses that write or read a given scalar.
967
  ScalarDefUseChains DefUse;
968
969
  /// Number of StoreInsts something can be mapped to.
970
  int NumberOfCompatibleTargets = 0;
971
972
  /// The number of StoreInsts to which at least one value or PHI has been
973
  /// mapped to.
974
  int NumberOfTargetsMapped = 0;
975
976
  /// The number of llvm::Value mapped to some array element.
977
  int NumberOfMappedValueScalars = 0;
978
979
  /// The number of PHIs mapped to some array element.
980
  int NumberOfMappedPHIScalars = 0;
981
982
  /// Determine whether two knowledges are conflicting with each other.
983
  ///
984
  /// @see Knowledge::isConflicting
985
16
  bool isConflicting(const Knowledge &Proposed) {
986
16
    raw_ostream *OS = nullptr;
987
16
    DEBUG(OS = &llvm::dbgs());
988
16
    return Knowledge::isConflicting(Zone, Proposed, OS, 4);
989
16
  }
990
991
  /// Determine whether @p SAI is a scalar that can be mapped to an array
992
  /// element.
993
20
  bool isMappable(const ScopArrayInfo *SAI) {
994
20
    assert(SAI);
995
20
996
20
    if (
SAI->isValueKind()20
)
{14
997
14
      auto *MA = DefUse.getValueDef(SAI);
998
14
      if (
!MA14
)
{1
999
1
        DEBUG(dbgs()
1000
1
              << "    Reject because value is read-only within the scop\n");
1001
1
        return false;
1002
1
      }
1003
14
1004
14
      // Mapping if value is used after scop is not supported. The code
1005
14
      // generator would need to reload the scalar after the scop, but it
1006
14
      // does not have the information to where it is mapped to. Only the
1007
14
      // MemoryAccesses have that information, not the ScopArrayInfo.
1008
13
      auto Inst = MA->getAccessInstruction();
1009
23
      for (auto User : Inst->users()) {
1010
23
        if (!isa<Instruction>(User))
1011
0
          return false;
1012
23
        auto UserInst = cast<Instruction>(User);
1013
23
1014
23
        if (
!S->contains(UserInst)23
)
{1
1015
1
          DEBUG(dbgs() << "    Reject because value is escaping\n");
1016
1
          return false;
1017
1
        }
1018
23
      }
1019
13
1020
12
      return true;
1021
13
    }
1022
20
1023
6
    
if (6
SAI->isPHIKind()6
)
{6
1024
6
      auto *MA = DefUse.getPHIRead(SAI);
1025
6
      assert(MA);
1026
6
1027
6
      // Mapping of an incoming block from before the SCoP is not supported by
1028
6
      // the code generator.
1029
6
      auto PHI = cast<PHINode>(MA->getAccessInstruction());
1030
12
      for (auto Incoming : PHI->blocks()) {
1031
12
        if (
!S->contains(Incoming)12
)
{0
1032
0
          DEBUG(dbgs() << "    Reject because at least one incoming block is "
1033
0
                          "not in the scop region\n");
1034
0
          return false;
1035
0
        }
1036
12
      }
1037
6
1038
6
      return true;
1039
6
    }
1040
6
1041
0
    
DEBUG0
(dbgs() << " Reject ExitPHI or other non-value\n");0
1042
0
    return false;
1043
6
  }
1044
1045
  /// Compute the uses of a MemoryKind::Value and its lifetime (from its
1046
  /// definition to the last use).
1047
  ///
1048
  /// @param SAI The ScopArrayInfo representing the value's storage.
1049
  ///
1050
  /// @return { DomainDef[] -> DomainUse[] }, { DomainDef[] -> Zone[] }
1051
  ///         First element is the set of uses for each definition.
1052
  ///         The second is the lifetime of each definition.
1053
  std::tuple<isl::union_map, isl::map>
1054
11
  computeValueUses(const ScopArrayInfo *SAI) {
1055
11
    assert(SAI->isValueKind());
1056
11
1057
11
    // { DomainRead[] }
1058
11
    auto Reads = makeEmptyUnionSet();
1059
11
1060
11
    // Find all uses.
1061
11
    for (auto *MA : DefUse.getValueUses(SAI))
1062
18
      Reads =
1063
18
          give(isl_union_set_add_set(Reads.take(), getDomainFor(MA).take()));
1064
11
1065
11
    // { DomainRead[] -> Scatter[] }
1066
11
    auto ReadSchedule = getScatterFor(Reads);
1067
11
1068
11
    auto *DefMA = DefUse.getValueDef(SAI);
1069
11
    assert(DefMA);
1070
11
1071
11
    // { DomainDef[] }
1072
11
    auto Writes = getDomainFor(DefMA);
1073
11
1074
11
    // { DomainDef[] -> Scatter[] }
1075
11
    auto WriteScatter = getScatterFor(Writes);
1076
11
1077
11
    // { Scatter[] -> DomainDef[] }
1078
11
    auto ReachDef = getScalarReachingDefinition(DefMA->getStatement());
1079
11
1080
11
    // { [DomainDef[] -> Scatter[]] -> DomainUse[] }
1081
11
    auto Uses = give(
1082
11
        isl_union_map_apply_range(isl_union_map_from_map(isl_map_range_map(
1083
11
                                      isl_map_reverse(ReachDef.take()))),
1084
11
                                  isl_union_map_reverse(ReadSchedule.take())));
1085
11
1086
11
    // { DomainDef[] -> Scatter[] }
1087
11
    auto UseScatter =
1088
11
        singleton(give(isl_union_set_unwrap(isl_union_map_domain(Uses.copy()))),
1089
11
                  give(isl_space_map_from_domain_and_range(
1090
11
                      isl_set_get_space(Writes.keep()), ScatterSpace.copy())));
1091
11
1092
11
    // { DomainDef[] -> Zone[] }
1093
11
    auto Lifetime = betweenScatter(WriteScatter, UseScatter, false, true);
1094
11
1095
11
    // { DomainDef[] -> DomainRead[] }
1096
11
    auto DefUses = give(isl_union_map_domain_factor_domain(Uses.take()));
1097
11
1098
11
    return std::make_pair(DefUses, Lifetime);
1099
11
  }
1100
1101
  /// For each 'execution' of a PHINode, get the incoming block that was
1102
  /// executed before.
1103
  ///
1104
  /// For each PHI instance we can directly determine which was the incoming
1105
  /// block, and hence derive which value the PHI has.
1106
  ///
1107
  /// @param SAI The ScopArrayInfo representing the PHI's storage.
1108
  ///
1109
  /// @return { DomainPHIRead[] -> DomainPHIWrite[] }
1110
6
  isl::union_map computePerPHI(const ScopArrayInfo *SAI) {
1111
6
    assert(SAI->isPHIKind());
1112
6
1113
6
    // { DomainPHIWrite[] -> Scatter[] }
1114
6
    auto PHIWriteScatter = makeEmptyUnionMap();
1115
6
1116
6
    // Collect all incoming block timepoint.
1117
12
    for (auto *MA : DefUse.getPHIIncomings(SAI)) {
1118
12
      auto Scatter = getScatterFor(MA);
1119
12
      PHIWriteScatter =
1120
12
          give(isl_union_map_add_map(PHIWriteScatter.take(), Scatter.take()));
1121
12
    }
1122
6
1123
6
    // { DomainPHIRead[] -> Scatter[] }
1124
6
    auto PHIReadScatter = getScatterFor(DefUse.getPHIRead(SAI));
1125
6
1126
6
    // { DomainPHIRead[] -> Scatter[] }
1127
6
    auto BeforeRead = beforeScatter(PHIReadScatter, true);
1128
6
1129
6
    // { Scatter[] }
1130
6
    auto WriteTimes = singleton(
1131
6
        give(isl_union_map_range(PHIWriteScatter.copy())), ScatterSpace);
1132
6
1133
6
    // { DomainPHIRead[] -> Scatter[] }
1134
6
    auto PHIWriteTimes =
1135
6
        give(isl_map_intersect_range(BeforeRead.take(), WriteTimes.take()));
1136
6
    auto LastPerPHIWrites = give(isl_map_lexmax(PHIWriteTimes.take()));
1137
6
1138
6
    // { DomainPHIRead[] -> DomainPHIWrite[] }
1139
6
    auto Result = give(isl_union_map_apply_range(
1140
6
        isl_union_map_from_map(LastPerPHIWrites.take()),
1141
6
        isl_union_map_reverse(PHIWriteScatter.take())));
1142
6
    assert(isl_union_map_is_single_valued(Result.keep()) == isl_bool_true);
1143
6
    assert(isl_union_map_is_injective(Result.keep()) == isl_bool_true);
1144
6
    return Result;
1145
6
  }
1146
1147
  /// Try to map a MemoryKind::Value to a given array element.
1148
  ///
1149
  /// @param SAI       Representation of the scalar's memory to map.
1150
  /// @param TargetElt { Scatter[] -> Element[] }
1151
  ///                  Suggestion where to map a scalar to when at a timepoint.
1152
  ///
1153
  /// @return true if the scalar was successfully mapped.
1154
11
  bool tryMapValue(const ScopArrayInfo *SAI, isl::map TargetElt) {
1155
11
    assert(SAI->isValueKind());
1156
11
1157
11
    auto *DefMA = DefUse.getValueDef(SAI);
1158
11
    assert(DefMA->isValueKind());
1159
11
    assert(DefMA->isMustWrite());
1160
11
1161
11
    // Stop if the scalar has already been mapped.
1162
11
    if (!DefMA->getLatestScopArrayInfo()->isValueKind())
1163
0
      return false;
1164
11
1165
11
    // { DomainDef[] -> Scatter[] }
1166
11
    auto DefSched = getScatterFor(DefMA);
1167
11
1168
11
    // Where each write is mapped to, according to the suggestion.
1169
11
    // { DomainDef[] -> Element[] }
1170
11
    auto DefTarget = give(isl_map_apply_domain(
1171
11
        TargetElt.copy(), isl_map_reverse(DefSched.copy())));
1172
11
    simplify(DefTarget);
1173
11
    DEBUG(dbgs() << "    Def Mapping: " << DefTarget << '\n');
1174
11
1175
11
    auto OrigDomain = getDomainFor(DefMA);
1176
11
    auto MappedDomain = give(isl_map_domain(DefTarget.copy()));
1177
11
    if (
!isl_set_is_subset(OrigDomain.keep(), MappedDomain.keep())11
)
{0
1178
0
      DEBUG(dbgs()
1179
0
            << "    Reject because mapping does not encompass all instances\n");
1180
0
      return false;
1181
0
    }
1182
11
1183
11
    // { DomainDef[] -> Zone[] }
1184
11
    isl::map Lifetime;
1185
11
1186
11
    // { DomainDef[] -> DomainUse[] }
1187
11
    isl::union_map DefUses;
1188
11
1189
11
    std::tie(DefUses, Lifetime) = computeValueUses(SAI);
1190
11
    DEBUG(dbgs() << "    Lifetime: " << Lifetime << '\n');
1191
11
1192
11
    /// { [Element[] -> Zone[]] }
1193
11
    auto EltZone = give(
1194
11
        isl_map_wrap(isl_map_apply_domain(Lifetime.copy(), DefTarget.copy())));
1195
11
    simplify(EltZone);
1196
11
1197
11
    // { [Element[] -> Scatter[]] }
1198
11
    auto DefEltSched = give(isl_map_wrap(isl_map_reverse(
1199
11
        isl_map_apply_domain(DefTarget.copy(), DefSched.copy()))));
1200
11
    simplify(DefEltSched);
1201
11
1202
11
    Knowledge Proposed(EltZone, nullptr, DefEltSched);
1203
11
    if (isConflicting(Proposed))
1204
5
      return false;
1205
11
1206
11
    // { DomainUse[] -> Element[] }
1207
6
    auto UseTarget = give(
1208
6
        isl_union_map_apply_range(isl_union_map_reverse(DefUses.take()),
1209
6
                                  isl_union_map_from_map(DefTarget.copy())));
1210
6
1211
6
    mapValue(SAI, std::move(DefTarget), std::move(UseTarget),
1212
6
             std::move(Lifetime), std::move(Proposed));
1213
6
    return true;
1214
11
  }
1215
1216
  /// After a scalar has been mapped, update the global knowledge.
1217
9
  void applyLifetime(Knowledge Proposed) {
1218
9
    Zone.learnFrom(std::move(Proposed));
1219
9
  }
1220
1221
  /// Map a MemoryKind::Value scalar to an array element.
1222
  ///
1223
  /// Callers must have ensured that the mapping is valid and not conflicting.
1224
  ///
1225
  /// @param SAI       The ScopArrayInfo representing the scalar's memory to
1226
  ///                  map.
1227
  /// @param DefTarget { DomainDef[] -> Element[] }
1228
  ///                  The array element to map the scalar to.
1229
  /// @param UseTarget { DomainUse[] -> Element[] }
1230
  ///                  The array elements the uses are mapped to.
1231
  /// @param Lifetime  { DomainDef[] -> Zone[] }
1232
  ///                  The lifetime of each llvm::Value definition for
1233
  ///                  reporting.
1234
  /// @param Proposed  Mapping constraints for reporting.
1235
  void mapValue(const ScopArrayInfo *SAI, isl::map DefTarget,
1236
                isl::union_map UseTarget, isl::map Lifetime,
1237
6
                Knowledge Proposed) {
1238
6
    // Redirect the read accesses.
1239
8
    for (auto *MA : DefUse.getValueUses(SAI)) {
1240
8
      // { DomainUse[] }
1241
8
      auto Domain = getDomainFor(MA);
1242
8
1243
8
      // { DomainUse[] -> Element[] }
1244
8
      auto NewAccRel = give(isl_union_map_intersect_domain(
1245
8
          UseTarget.copy(), isl_union_set_from_set(Domain.take())));
1246
8
      simplify(NewAccRel);
1247
8
1248
8
      assert(isl_union_map_n_map(NewAccRel.keep()) == 1);
1249
8
      MA->setNewAccessRelation(isl_map_from_union_map(NewAccRel.take()));
1250
8
    }
1251
6
1252
6
    auto *WA = DefUse.getValueDef(SAI);
1253
6
    WA->setNewAccessRelation(DefTarget.copy());
1254
6
    applyLifetime(Proposed);
1255
6
1256
6
    MappedValueScalars++;
1257
6
    NumberOfMappedValueScalars += 1;
1258
6
  }
1259
1260
  /// Try to map a MemoryKind::PHI scalar to a given array element.
1261
  ///
1262
  /// @param SAI       Representation of the scalar's memory to map.
1263
  /// @param TargetElt { Scatter[] -> Element[] }
1264
  ///                  Suggestion where to map the scalar to when at a
1265
  ///                  timepoint.
1266
  ///
1267
  /// @return true if the PHI scalar has been mapped.
1268
6
  bool tryMapPHI(const ScopArrayInfo *SAI, isl::map TargetElt) {
1269
6
    auto *PHIRead = DefUse.getPHIRead(SAI);
1270
6
    assert(PHIRead->isPHIKind());
1271
6
    assert(PHIRead->isRead());
1272
6
1273
6
    // Skip if already been mapped.
1274
6
    if (!PHIRead->getLatestScopArrayInfo()->isPHIKind())
1275
0
      return false;
1276
6
1277
6
    // { DomainRead[] -> Scatter[] }
1278
6
    auto PHISched = getScatterFor(PHIRead);
1279
6
1280
6
    // { DomainRead[] -> Element[] }
1281
6
    auto PHITarget =
1282
6
        give(isl_map_apply_range(PHISched.copy(), TargetElt.copy()));
1283
6
    simplify(PHITarget);
1284
6
    DEBUG(dbgs() << "    Mapping: " << PHITarget << '\n');
1285
6
1286
6
    auto OrigDomain = getDomainFor(PHIRead);
1287
6
    auto MappedDomain = give(isl_map_domain(PHITarget.copy()));
1288
6
    if (
!isl_set_is_subset(OrigDomain.keep(), MappedDomain.keep())6
)
{0
1289
0
      DEBUG(dbgs()
1290
0
            << "    Reject because mapping does not encompass all instances\n");
1291
0
      return false;
1292
0
    }
1293
6
1294
6
    // { DomainRead[] -> DomainWrite[] }
1295
6
    auto PerPHIWrites = computePerPHI(SAI);
1296
6
1297
6
    // { DomainWrite[] -> Element[] }
1298
6
    auto WritesTarget = give(isl_union_map_reverse(isl_union_map_apply_domain(
1299
6
        PerPHIWrites.copy(), isl_union_map_from_map(PHITarget.copy()))));
1300
6
    simplify(WritesTarget);
1301
6
1302
6
    // { DomainWrite[] }
1303
6
    auto UniverseWritesDom = give(isl_union_set_empty(ParamSpace.copy()));
1304
6
1305
6
    for (auto *MA : DefUse.getPHIIncomings(SAI))
1306
12
      UniverseWritesDom = give(isl_union_set_add_set(UniverseWritesDom.take(),
1307
12
                                                     getDomainFor(MA).take()));
1308
6
1309
6
    auto RelevantWritesTarget = WritesTarget;
1310
6
    if (DelicmOverapproximateWrites)
1311
2
      WritesTarget = expandMapping(WritesTarget, UniverseWritesDom);
1312
6
1313
6
    auto ExpandedWritesDom = give(isl_union_map_domain(WritesTarget.copy()));
1314
6
    if (!isl_union_set_is_subset(UniverseWritesDom.keep(),
1315
1
                                 ExpandedWritesDom.keep())) {
1316
1
      DEBUG(dbgs() << "    Reject because did not find PHI write mapping for "
1317
1
                      "all instances\n");
1318
1
      if (DelicmOverapproximateWrites)
1319
1
        DEBUG(dbgs() << "      Relevant Mapping:    " << RelevantWritesTarget
1320
1
                     << '\n');
1321
1
      DEBUG(dbgs() << "      Deduced Mapping:     " << WritesTarget << '\n');
1322
1
      DEBUG(dbgs() << "      Missing instances:    "
1323
1
                   << give(isl_union_set_subtract(UniverseWritesDom.copy(),
1324
1
                                                  ExpandedWritesDom.copy()))
1325
1
                   << '\n');
1326
1
      return false;
1327
1
    }
1328
6
1329
6
    //  { DomainRead[] -> Scatter[] }
1330
5
    auto PerPHIWriteScatter = give(isl_map_from_union_map(
1331
5
        isl_union_map_apply_range(PerPHIWrites.copy(), Schedule.copy())));
1332
5
1333
5
    // { DomainRead[] -> Zone[] }
1334
5
    auto Lifetime = betweenScatter(PerPHIWriteScatter, PHISched, false, true);
1335
5
    simplify(Lifetime);
1336
5
    DEBUG(dbgs() << "    Lifetime: " << Lifetime << "\n");
1337
5
1338
5
    // { DomainWrite[] -> Zone[] }
1339
5
    auto WriteLifetime = give(isl_union_map_apply_domain(
1340
5
        isl_union_map_from_map(Lifetime.copy()), PerPHIWrites.copy()));
1341
5
1342
5
    // { DomainWrite[] -> [Element[] -> Scatter[]] }
1343
5
    auto WrittenTranslator =
1344
5
        give(isl_union_map_range_product(WritesTarget.copy(), Schedule.copy()));
1345
5
1346
5
    // { [Element[] -> Scatter[]] }
1347
5
    auto Written = give(isl_union_map_range(WrittenTranslator.copy()));
1348
5
    simplify(Written);
1349
5
1350
5
    // { DomainWrite[] -> [Element[] -> Zone[]] }
1351
5
    auto LifetimeTranslator = give(
1352
5
        isl_union_map_range_product(WritesTarget.copy(), WriteLifetime.take()));
1353
5
1354
5
    // { [Element[] -> Zone[] }
1355
5
    auto Occupied = give(isl_union_map_range(LifetimeTranslator.copy()));
1356
5
    simplify(Occupied);
1357
5
1358
5
    Knowledge Proposed(Occupied, nullptr, Written);
1359
5
    if (isConflicting(Proposed))
1360
2
      return false;
1361
5
1362
3
    mapPHI(SAI, std::move(PHITarget), std::move(WritesTarget),
1363
3
           std::move(Lifetime), std::move(Proposed));
1364
3
    return true;
1365
5
  }
1366
1367
  /// Map a MemoryKind::PHI scalar to an array element.
1368
  ///
1369
  /// Callers must have ensured that the mapping is valid and not conflicting
1370
  /// with the common knowledge.
1371
  ///
1372
  /// @param SAI         The ScopArrayInfo representing the scalar's memory to
1373
  ///                    map.
1374
  /// @param ReadTarget  { DomainRead[] -> Element[] }
1375
  ///                    The array element to map the scalar to.
1376
  /// @param WriteTarget { DomainWrite[] -> Element[] }
1377
  ///                    New access target for each PHI incoming write.
1378
  /// @param Lifetime    { DomainRead[] -> Zone[] }
1379
  ///                    The lifetime of each PHI for reporting.
1380
  /// @param Proposed    Mapping constraints for reporting.
1381
  void mapPHI(const ScopArrayInfo *SAI, isl::map ReadTarget,
1382
              isl::union_map WriteTarget, isl::map Lifetime,
1383
3
              Knowledge Proposed) {
1384
3
    // Redirect the PHI incoming writes.
1385
6
    for (auto *MA : DefUse.getPHIIncomings(SAI)) {
1386
6
      // { DomainWrite[] }
1387
6
      auto Domain = getDomainFor(MA);
1388
6
1389
6
      // { DomainWrite[] -> Element[] }
1390
6
      auto NewAccRel = give(isl_union_map_intersect_domain(
1391
6
          WriteTarget.copy(), isl_union_set_from_set(Domain.take())));
1392
6
      simplify(NewAccRel);
1393
6
1394
6
      assert(isl_union_map_n_map(NewAccRel.keep()) == 1);
1395
6
      MA->setNewAccessRelation(isl_map_from_union_map(NewAccRel.take()));
1396
6
    }
1397
3
1398
3
    // Redirect the PHI read.
1399
3
    auto *PHIRead = DefUse.getPHIRead(SAI);
1400
3
    PHIRead->setNewAccessRelation(ReadTarget.copy());
1401
3
    applyLifetime(Proposed);
1402
3
1403
3
    MappedPHIScalars++;
1404
3
    NumberOfMappedPHIScalars++;
1405
3
  }
1406
1407
  /// Search and map scalars to memory overwritten by @p TargetStoreMA.
1408
  ///
1409
  /// Start trying to map scalars that are used in the same statement as the
1410
  /// store. For every successful mapping, try to also map scalars of the
1411
  /// statements where those are written. Repeat, until no more mapping
1412
  /// opportunity is found.
1413
  ///
1414
  /// There is currently no preference in which order scalars are tried.
1415
  /// Ideally, we would direct it towards a load instruction of the same array
1416
  /// element.
1417
10
  bool collapseScalarsToStore(MemoryAccess *TargetStoreMA) {
1418
10
    assert(TargetStoreMA->isLatestArrayKind());
1419
10
    assert(TargetStoreMA->isMustWrite());
1420
10
1421
10
    auto TargetStmt = TargetStoreMA->getStatement();
1422
10
1423
10
    // { DomTarget[] }
1424
10
    auto TargetDom = getDomainFor(TargetStmt);
1425
10
1426
10
    // { DomTarget[] -> Element[] }
1427
10
    auto TargetAccRel = getAccessRelationFor(TargetStoreMA);
1428
10
1429
10
    // { Zone[] -> DomTarget[] }
1430
10
    // For each point in time, find the next target store instance.
1431
10
    auto Target =
1432
10
        computeScalarReachingOverwrite(Schedule, TargetDom, false, true);
1433
10
1434
10
    // { Zone[] -> Element[] }
1435
10
    // Use the target store's write location as a suggestion to map scalars to.
1436
10
    auto EltTarget =
1437
10
        give(isl_map_apply_range(Target.take(), TargetAccRel.take()));
1438
10
    simplify(EltTarget);
1439
10
    DEBUG(dbgs() << "    Target mapping is " << EltTarget << '\n');
1440
10
1441
10
    // Stack of elements not yet processed.
1442
10
    SmallVector<MemoryAccess *, 16> Worklist;
1443
10
1444
10
    // Set of scalars already tested.
1445
10
    SmallPtrSet<const ScopArrayInfo *, 16> Closed;
1446
10
1447
10
    // Lambda to add all scalar reads to the work list.
1448
14
    auto ProcessAllIncoming = [&](ScopStmt *Stmt) {
1449
34
      for (auto *MA : *Stmt) {
1450
34
        if (!MA->isLatestScalarKind())
1451
16
          continue;
1452
18
        
if (18
!MA->isRead()18
)
1453
5
          continue;
1454
18
1455
13
        Worklist.push_back(MA);
1456
13
      }
1457
14
    };
1458
10
1459
10
    // Add initial scalar. Either the value written by the store, or all inputs
1460
10
    // of its statement.
1461
10
    auto WrittenVal = TargetStoreMA->getAccessValue();
1462
10
    if (auto InputAcc = getInputAccessOf(WrittenVal, TargetStmt))
1463
8
      Worklist.push_back(InputAcc);
1464
10
    else
1465
2
      ProcessAllIncoming(TargetStmt);
1466
10
1467
10
    auto AnyMapped = false;
1468
10
    auto &DL =
1469
10
        S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
1470
10
    auto StoreSize =
1471
10
        DL.getTypeAllocSize(TargetStoreMA->getAccessValue()->getType());
1472
10
1473
31
    while (
!Worklist.empty()31
)
{21
1474
21
      auto *MA = Worklist.pop_back_val();
1475
21
1476
21
      auto *SAI = MA->getScopArrayInfo();
1477
21
      if (Closed.count(SAI))
1478
1
        continue;
1479
20
      Closed.insert(SAI);
1480
20
      DEBUG(dbgs() << "\n    Trying to map " << MA << " (SAI: " << SAI
1481
20
                   << ")\n");
1482
20
1483
20
      // Skip non-mappable scalars.
1484
20
      if (!isMappable(SAI))
1485
2
        continue;
1486
20
1487
18
      auto MASize = DL.getTypeAllocSize(MA->getAccessValue()->getType());
1488
18
      if (
MASize > StoreSize18
)
{1
1489
1
        DEBUG(dbgs() << "    Reject because storage size is insufficient\n");
1490
1
        continue;
1491
1
      }
1492
18
1493
18
      // Try to map MemoryKind::Value scalars.
1494
17
      
if (17
SAI->isValueKind()17
)
{11
1495
11
        if (!tryMapValue(SAI, EltTarget))
1496
5
          continue;
1497
11
1498
6
        auto *DefAcc = DefUse.getValueDef(SAI);
1499
6
        ProcessAllIncoming(DefAcc->getStatement());
1500
6
1501
6
        AnyMapped = true;
1502
6
        continue;
1503
11
      }
1504
17
1505
17
      // Try to map MemoryKind::PHI scalars.
1506
6
      
if (6
SAI->isPHIKind()6
)
{6
1507
6
        if (!tryMapPHI(SAI, EltTarget))
1508
3
          continue;
1509
6
        // Add inputs of all incoming statements to the worklist.
1510
3
        for (auto *PHIWrite : DefUse.getPHIIncomings(SAI))
1511
6
          ProcessAllIncoming(PHIWrite->getStatement());
1512
3
1513
3
        AnyMapped = true;
1514
3
        continue;
1515
6
      }
1516
6
    }
1517
10
1518
10
    if (
AnyMapped10
)
{3
1519
3
      TargetsMapped++;
1520
3
      NumberOfTargetsMapped++;
1521
3
    }
1522
10
    return AnyMapped;
1523
10
  }
1524
1525
  /// Compute when an array element is unused.
1526
  ///
1527
  /// @return { [Element[] -> Zone[]] }
1528
15
  isl::union_set computeLifetime() const {
1529
15
    // { Element[] -> Zone[] }
1530
15
    auto ArrayUnused = computeArrayUnused(Schedule, AllMustWrites, AllReads,
1531
15
                                          false, false, true);
1532
15
1533
15
    auto Result = give(isl_union_map_wrap(ArrayUnused.copy()));
1534
15
1535
15
    simplify(Result);
1536
15
    return Result;
1537
15
  }
1538
1539
  /// Determine when an array element is written to.
1540
  ///
1541
  /// @return { [Element[] -> Scatter[]] }
1542
15
  isl::union_set computeWritten() const {
1543
15
    // { WriteDomain[] -> Element[] }
1544
15
    auto AllWrites =
1545
15
        give(isl_union_map_union(AllMustWrites.copy(), AllMayWrites.copy()));
1546
15
1547
15
    // { Scatter[] -> Element[] }
1548
15
    auto WriteTimepoints =
1549
15
        give(isl_union_map_apply_domain(AllWrites.copy(), Schedule.copy()));
1550
15
1551
15
    auto Result =
1552
15
        give(isl_union_map_wrap(isl_union_map_reverse(WriteTimepoints.copy())));
1553
15
1554
15
    simplify(Result);
1555
15
    return Result;
1556
15
  }
1557
1558
  /// Determine whether an access touches at most one element.
1559
  ///
1560
  /// The accessed element could be a scalar or accessing an array with constant
1561
  /// subscript, such that all instances access only that element.
1562
  ///
1563
  /// @param MA The access to test.
1564
  ///
1565
  /// @return True, if zero or one elements are accessed; False if at least two
1566
  ///         different elements are accessed.
1567
12
  bool isScalarAccess(MemoryAccess *MA) {
1568
12
    auto Map = getAccessRelationFor(MA);
1569
12
    auto Set = give(isl_map_range(Map.take()));
1570
12
    return isl_set_is_singleton(Set.keep()) == isl_bool_true;
1571
12
  }
1572
1573
  /// Print mapping statistics to @p OS.
1574
14
  void printStatistics(llvm::raw_ostream &OS, int Indent = 0) const {
1575
14
    OS.indent(Indent) << "Statistics {\n";
1576
14
    OS.indent(Indent + 4) << "Compatible overwrites: "
1577
14
                          << NumberOfCompatibleTargets << "\n";
1578
14
    OS.indent(Indent + 4) << "Overwrites mapped to:  " << NumberOfTargetsMapped
1579
14
                          << '\n';
1580
14
    OS.indent(Indent + 4) << "Value scalars mapped:  "
1581
14
                          << NumberOfMappedValueScalars << '\n';
1582
14
    OS.indent(Indent + 4) << "PHI scalars mapped:    "
1583
14
                          << NumberOfMappedPHIScalars << '\n';
1584
14
    OS.indent(Indent) << "}\n";
1585
14
  }
1586
1587
  /// Return whether at least one transformation been applied.
1588
14
  bool isModified() const { return NumberOfTargetsMapped > 0; }
1589
1590
public:
1591
19
  DeLICMImpl(Scop *S) : ZoneAlgorithm(S) {}
1592
1593
  /// Calculate the lifetime (definition to last use) of every array element.
1594
  ///
1595
  /// @return True if the computed lifetimes (#Zone) is usable.
1596
19
  bool computeZone() {
1597
19
    // Check that nothing strange occurs.
1598
19
    if (
!isCompatibleScop()19
)
{4
1599
4
      DeLICMIncompatible++;
1600
4
      return false;
1601
4
    }
1602
19
1603
15
    DefUse.compute(S);
1604
15
    isl::union_set EltUnused, EltWritten;
1605
15
1606
15
    {
1607
15
      IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), DelicmMaxOps);
1608
15
1609
15
      computeCommon();
1610
15
1611
15
      EltUnused = computeLifetime();
1612
15
      EltWritten = computeWritten();
1613
15
    }
1614
15
    DeLICMAnalyzed++;
1615
15
1616
15
    if (
!EltUnused || 15
!EltWritten14
)
{1
1617
1
      assert(isl_ctx_last_error(IslCtx.get()) == isl_error_quota &&
1618
1
             "The only reason that these things have not been computed should "
1619
1
             "be if the max-operations limit hit");
1620
1
      DeLICMOutOfQuota++;
1621
1
      DEBUG(dbgs() << "DeLICM analysis exceeded max_operations\n");
1622
1
      DebugLoc Begin, End;
1623
1
      getDebugLocations(getBBPairForRegion(&S->getRegion()), Begin, End);
1624
1
      OptimizationRemarkAnalysis R(DEBUG_TYPE, "OutOfQuota", Begin,
1625
1
                                   S->getEntry());
1626
1
      R << "maximal number of operations exceeded during zone analysis";
1627
1
      S->getFunction().getContext().diagnose(R);
1628
1
      return false;
1629
1
    }
1630
15
1631
14
    Zone = OriginalZone = Knowledge(nullptr, EltUnused, EltWritten);
1632
14
    DEBUG(dbgs() << "Computed Zone:\n"; OriginalZone.print(dbgs(), 4));
1633
14
1634
14
    assert(Zone.isUsable() && OriginalZone.isUsable());
1635
14
    return true;
1636
15
  }
1637
1638
  /// Try to map as many scalars to unused array elements as possible.
1639
  ///
1640
  /// Multiple scalars might be mappable to intersecting unused array element
1641
  /// zones, but we can only chose one. This is a greedy algorithm, therefore
1642
  /// the first processed element claims it.
1643
14
  void greedyCollapse() {
1644
14
    bool Modified = false;
1645
14
1646
69
    for (auto &Stmt : *S) {
1647
134
      for (auto *MA : Stmt) {
1648
134
        if (!MA->isLatestArrayKind())
1649
116
          continue;
1650
18
        
if (18
!MA->isWrite()18
)
1651
2
          continue;
1652
18
1653
16
        
if (16
MA->isMayWrite()16
)
{3
1654
3
          DEBUG(dbgs() << "Access " << MA
1655
3
                       << " pruned because it is a MAY_WRITE\n");
1656
3
          OptimizationRemarkMissed R(DEBUG_TYPE, "TargetMayWrite",
1657
3
                                     MA->getAccessInstruction());
1658
3
          R << "Skipped possible mapping target because it is not an "
1659
3
               "unconditional overwrite";
1660
3
          S->getFunction().getContext().diagnose(R);
1661
3
          continue;
1662
3
        }
1663
16
1664
13
        
if (13
Stmt.getNumIterators() == 013
)
{1
1665
1
          DEBUG(dbgs() << "Access " << MA
1666
1
                       << " pruned because it is not in a loop\n");
1667
1
          OptimizationRemarkMissed R(DEBUG_TYPE, "WriteNotInLoop",
1668
1
                                     MA->getAccessInstruction());
1669
1
          R << "skipped possible mapping target because it is not in a loop";
1670
1
          S->getFunction().getContext().diagnose(R);
1671
1
          continue;
1672
1
        }
1673
13
1674
12
        
if (12
isScalarAccess(MA)12
)
{2
1675
2
          DEBUG(dbgs() << "Access " << MA
1676
2
                       << " pruned because it writes only a single element\n");
1677
2
          OptimizationRemarkMissed R(DEBUG_TYPE, "ScalarWrite",
1678
2
                                     MA->getAccessInstruction());
1679
2
          R << "skipped possible mapping target because the memory location "
1680
2
               "written to does not depend on its outer loop";
1681
2
          S->getFunction().getContext().diagnose(R);
1682
2
          continue;
1683
2
        }
1684
12
1685
10
        NumberOfCompatibleTargets++;
1686
10
        DEBUG(dbgs() << "Analyzing target access " << MA << "\n");
1687
10
        if (collapseScalarsToStore(MA))
1688
3
          Modified = true;
1689
10
      }
1690
69
    }
1691
14
1692
14
    if (Modified)
1693
3
      DeLICMScopsModified++;
1694
14
  }
1695
1696
  /// Dump the internal information about a performed DeLICM to @p OS.
1697
19
  void print(llvm::raw_ostream &OS, int Indent = 0) {
1698
19
    if (
!Zone.isUsable()19
)
{5
1699
5
      OS.indent(Indent) << "Zone not computed\n";
1700
5
      return;
1701
5
    }
1702
19
1703
14
    printStatistics(OS, Indent);
1704
14
    if (
!isModified()14
)
{11
1705
11
      OS.indent(Indent) << "No modification has been made\n";
1706
11
      return;
1707
11
    }
1708
3
    printAccesses(OS, Indent);
1709
3
  }
1710
};
1711
1712
class DeLICM : public ScopPass {
1713
private:
1714
  DeLICM(const DeLICM &) = delete;
1715
  const DeLICM &operator=(const DeLICM &) = delete;
1716
1717
  /// The pass implementation, also holding per-scop data.
1718
  std::unique_ptr<DeLICMImpl> Impl;
1719
1720
19
  void collapseToUnused(Scop &S) {
1721
19
    Impl = make_unique<DeLICMImpl>(&S);
1722
19
1723
19
    if (
!Impl->computeZone()19
)
{5
1724
5
      DEBUG(dbgs() << "Abort because cannot reliably compute lifetimes\n");
1725
5
      return;
1726
5
    }
1727
19
1728
14
    
DEBUG14
(dbgs() << "Collapsing scalars to unused array elements...\n");14
1729
14
    Impl->greedyCollapse();
1730
14
1731
14
    DEBUG(dbgs() << "\nFinal Scop:\n");
1732
14
    DEBUG(S.print(dbgs()));
1733
14
  }
1734
1735
public:
1736
  static char ID;
1737
19
  explicit DeLICM() : ScopPass(ID) {}
1738
1739
19
  virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
1740
19
    AU.addRequiredTransitive<ScopInfoRegionPass>();
1741
19
    AU.setPreservesAll();
1742
19
  }
1743
1744
19
  virtual bool runOnScop(Scop &S) override {
1745
19
    // Free resources for previous scop's computation, if not yet done.
1746
19
    releaseMemory();
1747
19
1748
19
    collapseToUnused(S);
1749
19
1750
19
    return false;
1751
19
  }
1752
1753
19
  virtual void printScop(raw_ostream &OS, Scop &S) const override {
1754
19
    if (!Impl)
1755
0
      return;
1756
19
    assert(Impl->getScop() == &S);
1757
19
1758
19
    OS << "DeLICM result:\n";
1759
19
    Impl->print(OS);
1760
19
  }
1761
1762
99
  virtual void releaseMemory() override { Impl.reset(); }
1763
};
1764
1765
char DeLICM::ID;
1766
} // anonymous namespace
1767
1768
0
Pass *polly::createDeLICMPass() { return new DeLICM(); }
1769
1770
39.2k
INITIALIZE_PASS_BEGIN39.2k
(DeLICM, "polly-delicm", "Polly - DeLICM/DePRE", false,39.2k
1771
39.2k
                      false)
1772
39.2k
INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass)
1773
39.2k
INITIALIZE_PASS_END(DeLICM, "polly-delicm", "Polly - DeLICM/DePRE", false,
1774
                    false)
1775
1776
bool polly::isConflicting(isl::union_set ExistingOccupied,
1777
                          isl::union_set ExistingUnused,
1778
                          isl::union_set ExistingWrites,
1779
                          isl::union_set ProposedOccupied,
1780
                          isl::union_set ProposedUnused,
1781
                          isl::union_set ProposedWrites, llvm::raw_ostream *OS,
1782
104
                          unsigned Indent) {
1783
104
  Knowledge Existing(std::move(ExistingOccupied), std::move(ExistingUnused),
1784
104
                     std::move(ExistingWrites));
1785
104
  Knowledge Proposed(std::move(ProposedOccupied), std::move(ProposedUnused),
1786
104
                     std::move(ProposedWrites));
1787
104
1788
104
  return Knowledge::isConflicting(Existing, Proposed, OS, Indent);
1789
104
}