Coverage Report

Created: 2019-02-20 07:29

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/Transform/DeLICM.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------ DeLICM.cpp -----------------------------------------*- C++ -*-===//
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
// Undo the effect of Loop Invariant Code Motion (LICM) and
10
// GVN Partial Redundancy Elimination (PRE) on SCoP-level.
11
//
12
// Namely, remove register/scalar dependencies by mapping them back to array
13
// elements.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "polly/DeLICM.h"
18
#include "polly/Options.h"
19
#include "polly/ScopInfo.h"
20
#include "polly/ScopPass.h"
21
#include "polly/Support/ISLOStream.h"
22
#include "polly/Support/ISLTools.h"
23
#include "polly/ZoneAlgo.h"
24
#include "llvm/ADT/Statistic.h"
25
30
#define DEBUG_TYPE "polly-delicm"
26
27
using namespace polly;
28
using namespace llvm;
29
30
namespace {
31
32
cl::opt<int>
33
    DelicmMaxOps("polly-delicm-max-ops",
34
                 cl::desc("Maximum number of isl operations to invest for "
35
                          "lifetime analysis; 0=no limit"),
36
                 cl::init(1000000), cl::cat(PollyCategory));
37
38
cl::opt<bool> DelicmOverapproximateWrites(
39
    "polly-delicm-overapproximate-writes",
40
    cl::desc(
41
        "Do more PHI writes than necessary in order to avoid partial accesses"),
42
    cl::init(false), cl::Hidden, cl::cat(PollyCategory));
43
44
cl::opt<bool> DelicmPartialWrites("polly-delicm-partial-writes",
45
                                  cl::desc("Allow partial writes"),
46
                                  cl::init(true), cl::Hidden,
47
                                  cl::cat(PollyCategory));
48
49
cl::opt<bool>
50
    DelicmComputeKnown("polly-delicm-compute-known",
51
                       cl::desc("Compute known content of array elements"),
52
                       cl::init(true), cl::Hidden, cl::cat(PollyCategory));
53
54
STATISTIC(DeLICMAnalyzed, "Number of successfully analyzed SCoPs");
55
STATISTIC(DeLICMOutOfQuota,
56
          "Analyses aborted because max_operations was reached");
57
STATISTIC(MappedValueScalars, "Number of mapped Value scalars");
58
STATISTIC(MappedPHIScalars, "Number of mapped PHI scalars");
59
STATISTIC(TargetsMapped, "Number of stores used for at least one mapping");
60
STATISTIC(DeLICMScopsModified, "Number of SCoPs optimized");
61
62
STATISTIC(NumValueWrites, "Number of scalar value writes after DeLICM");
63
STATISTIC(NumValueWritesInLoops,
64
          "Number of scalar value writes nested in affine loops after DeLICM");
65
STATISTIC(NumPHIWrites, "Number of scalar phi writes after DeLICM");
66
STATISTIC(NumPHIWritesInLoops,
67
          "Number of scalar phi writes nested in affine loops after DeLICM");
68
STATISTIC(NumSingletonWrites, "Number of singleton writes after DeLICM");
69
STATISTIC(NumSingletonWritesInLoops,
70
          "Number of singleton writes nested in affine loops after DeLICM");
71
72
isl::union_map computeReachingOverwrite(isl::union_map Schedule,
73
                                        isl::union_map Writes,
74
                                        bool InclPrevWrite,
75
40
                                        bool InclOverwrite) {
76
40
  return computeReachingWrite(Schedule, Writes, true, InclPrevWrite,
77
40
                              InclOverwrite);
78
40
}
79
80
/// Compute the next overwrite for a scalar.
81
///
82
/// @param Schedule      { DomainWrite[] -> Scatter[] }
83
///                      Schedule of (at least) all writes. Instances not in @p
84
///                      Writes are ignored.
85
/// @param Writes        { DomainWrite[] }
86
///                      The element instances that write to the scalar.
87
/// @param InclPrevWrite Whether to extend the timepoints to include
88
///                      the timepoint where the previous write happens.
89
/// @param InclOverwrite Whether the reaching overwrite includes the timepoint
90
///                      of the overwrite itself.
91
///
92
/// @return { Scatter[] -> DomainDef[] }
93
isl::union_map computeScalarReachingOverwrite(isl::union_map Schedule,
94
                                              isl::union_set Writes,
95
                                              bool InclPrevWrite,
96
40
                                              bool InclOverwrite) {
97
40
98
40
  // { DomainWrite[] }
99
40
  auto WritesMap = isl::union_map::from_domain(Writes);
100
40
101
40
  // { [Element[] -> Scatter[]] -> DomainWrite[] }
102
40
  auto Result = computeReachingOverwrite(
103
40
      std::move(Schedule), std::move(WritesMap), InclPrevWrite, InclOverwrite);
104
40
105
40
  return Result.domain_factor_range();
106
40
}
107
108
/// Overload of computeScalarReachingOverwrite, with only one writing statement.
109
/// Consequently, the result consists of only one map space.
110
///
111
/// @param Schedule      { DomainWrite[] -> Scatter[] }
112
/// @param Writes        { DomainWrite[] }
113
/// @param InclPrevWrite Include the previous write to result.
114
/// @param InclOverwrite Include the overwrite to the result.
115
///
116
/// @return { Scatter[] -> DomainWrite[] }
117
isl::map computeScalarReachingOverwrite(isl::union_map Schedule,
118
                                        isl::set Writes, bool InclPrevWrite,
119
40
                                        bool InclOverwrite) {
120
40
  isl::space ScatterSpace = getScatterSpace(Schedule);
121
40
  isl::space DomSpace = Writes.get_space();
122
40
123
40
  isl::union_map ReachOverwrite = computeScalarReachingOverwrite(
124
40
      Schedule, isl::union_set(Writes), InclPrevWrite, InclOverwrite);
125
40
126
40
  isl::space ResultSpace = ScatterSpace.map_from_domain_and_range(DomSpace);
127
40
  return singleton(std::move(ReachOverwrite), ResultSpace);
128
40
}
129
130
/// Try to find a 'natural' extension of a mapped to elements outside its
131
/// domain.
132
///
133
/// @param Relevant The map with mapping that may not be modified.
134
/// @param Universe The domain to which @p Relevant needs to be extended.
135
///
136
/// @return A map with that associates the domain elements of @p Relevant to the
137
///         same elements and in addition the elements of @p Universe to some
138
///         undefined elements. The function prefers to return simple maps.
139
11
isl::union_map expandMapping(isl::union_map Relevant, isl::union_set Universe) {
140
11
  Relevant = Relevant.coalesce();
141
11
  isl::union_set RelevantDomain = Relevant.domain();
142
11
  isl::union_map Simplified = Relevant.gist_domain(RelevantDomain);
143
11
  Simplified = Simplified.coalesce();
144
11
  return Simplified.intersect_domain(Universe);
145
11
}
146
147
/// Represent the knowledge of the contents of any array elements in any zone or
148
/// the knowledge we would add when mapping a scalar to an array element.
149
///
150
/// Every array element at every zone unit has one of two states:
151
///
152
/// - Unused: Not occupied by any value so a transformation can change it to
153
///   other values.
154
///
155
/// - Occupied: The element contains a value that is still needed.
156
///
157
/// The union of Unused and Unknown zones forms the universe, the set of all
158
/// elements at every timepoint. The universe can easily be derived from the
159
/// array elements that are accessed someway. Arrays that are never accessed
160
/// also never play a role in any computation and can hence be ignored. With a
161
/// given universe, only one of the sets needs to stored implicitly. Computing
162
/// the complement is also an expensive operation, hence this class has been
163
/// designed that only one of sets is needed while the other is assumed to be
164
/// implicit. It can still be given, but is mostly ignored.
165
///
166
/// There are two use cases for the Knowledge class:
167
///
168
/// 1) To represent the knowledge of the current state of ScopInfo. The unused
169
///    state means that an element is currently unused: there is no read of it
170
///    before the next overwrite. Also called 'Existing'.
171
///
172
/// 2) To represent the requirements for mapping a scalar to array elements. The
173
///    unused state means that there is no change/requirement. Also called
174
///    'Proposed'.
175
///
176
/// In addition to these states at unit zones, Knowledge needs to know when
177
/// values are written. This is because written values may have no lifetime (one
178
/// reason is that the value is never read). Such writes would therefore never
179
/// conflict, but overwrite values that might still be required. Another source
180
/// of problems are multiple writes to the same element at the same timepoint,
181
/// because their order is undefined.
182
class Knowledge {
183
private:
184
  /// { [Element[] -> Zone[]] }
185
  /// Set of array elements and when they are alive.
186
  /// Can contain a nullptr; in this case the set is implicitly defined as the
187
  /// complement of #Unused.
188
  ///
189
  /// The set of alive array elements is represented as zone, as the set of live
190
  /// values can differ depending on how the elements are interpreted.
191
  /// Assuming a value X is written at timestep [0] and read at timestep [1]
192
  /// without being used at any later point, then the value is alive in the
193
  /// interval ]0,1[. This interval cannot be represented by an integer set, as
194
  /// it does not contain any integer point. Zones allow us to represent this
195
  /// interval and can be converted to sets of timepoints when needed (e.g., in
196
  /// isConflicting when comparing to the write sets).
197
  /// @see convertZoneToTimepoints and this file's comment for more details.
198
  isl::union_set Occupied;
199
200
  /// { [Element[] -> Zone[]] }
201
  /// Set of array elements when they are not alive, i.e. their memory can be
202
  /// used for other purposed. Can contain a nullptr; in this case the set is
203
  /// implicitly defined as the complement of #Occupied.
204
  isl::union_set Unused;
205
206
  /// { [Element[] -> Zone[]] -> ValInst[] }
207
  /// Maps to the known content for each array element at any interval.
208
  ///
209
  /// Any element/interval can map to multiple known elements. This is due to
210
  /// multiple llvm::Value referring to the same content. Examples are
211
  ///
212
  /// - A value stored and loaded again. The LoadInst represents the same value
213
  /// as the StoreInst's value operand.
214
  ///
215
  /// - A PHINode is equal to any one of the incoming values. In case of
216
  /// LCSSA-form, it is always equal to its single incoming value.
217
  ///
218
  /// Two Knowledges are considered not conflicting if at least one of the known
219
  /// values match. Not known values are not stored as an unnamed tuple (as
220
  /// #Written does), but maps to nothing.
221
  ///
222
  ///  Known values are usually just defined for #Occupied elements. Knowing
223
  ///  #Unused contents has no advantage as it can be overwritten.
224
  isl::union_map Known;
225
226
  /// { [Element[] -> Scatter[]] -> ValInst[] }
227
  /// The write actions currently in the scop or that would be added when
228
  /// mapping a scalar. Maps to the value that is written.
229
  ///
230
  /// Written values that cannot be identified are represented by an unknown
231
  /// ValInst[] (an unnamed tuple of 0 dimension). It conflicts with itself.
232
  isl::union_map Written;
233
234
  /// Check whether this Knowledge object is well-formed.
235
685
  void checkConsistency() const {
236
#ifndef NDEBUG
237
    // Default-initialized object
238
    if (!Occupied && !Unused && !Known && !Written)
239
      return;
240
241
    assert(Occupied || Unused);
242
    assert(Known);
243
    assert(Written);
244
245
    // If not all fields are defined, we cannot derived the universe.
246
    if (!Occupied || !Unused)
247
      return;
248
249
    assert(Occupied.is_disjoint(Unused));
250
    auto Universe = Occupied.unite(Unused);
251
252
    assert(!Known.domain().is_subset(Universe).is_false());
253
    assert(!Written.domain().is_subset(Universe).is_false());
254
#endif
255
  }
256
257
public:
258
  /// Initialize a nullptr-Knowledge. This is only provided for convenience; do
259
  /// not use such an object.
260
102
  Knowledge() {}
261
262
  /// Create a new object with the given members.
263
  Knowledge(isl::union_set Occupied, isl::union_set Unused,
264
            isl::union_map Known, isl::union_map Written)
265
      : Occupied(std::move(Occupied)), Unused(std::move(Unused)),
266
601
        Known(std::move(Known)), Written(std::move(Written)) {
267
601
    checkConsistency();
268
601
  }
269
270
  /// Return whether this object was not default-constructed.
271
47
  bool isUsable() const { return (Occupied || Unused) && 
Known44
&&
Written44
; }
272
273
  /// Print the content of this object to @p OS.
274
0
  void print(llvm::raw_ostream &OS, unsigned Indent = 0) const {
275
0
    if (isUsable()) {
276
0
      if (Occupied)
277
0
        OS.indent(Indent) << "Occupied: " << Occupied << "\n";
278
0
      else
279
0
        OS.indent(Indent) << "Occupied: <Everything else not in Unused>\n";
280
0
      if (Unused)
281
0
        OS.indent(Indent) << "Unused:   " << Unused << "\n";
282
0
      else
283
0
        OS.indent(Indent) << "Unused:   <Everything else not in Occupied>\n";
284
0
      OS.indent(Indent) << "Known:    " << Known << "\n";
285
0
      OS.indent(Indent) << "Written : " << Written << '\n';
286
0
    } else {
287
0
      OS.indent(Indent) << "Invalid knowledge\n";
288
0
    }
289
0
  }
290
291
  /// Combine two knowledges, this and @p That.
292
84
  void learnFrom(Knowledge That) {
293
84
    assert(!isConflicting(*this, That));
294
84
    assert(Unused && That.Occupied);
295
84
    assert(
296
84
        !That.Unused &&
297
84
        "This function is only prepared to learn occupied elements from That");
298
84
    assert(!Occupied && "This function does not implement "
299
84
                        "`this->Occupied = "
300
84
                        "this->Occupied.unite(That.Occupied);`");
301
84
302
84
    Unused = Unused.subtract(That.Occupied);
303
84
    Known = Known.unite(That.Known);
304
84
    Written = Written.unite(That.Written);
305
84
306
84
    checkConsistency();
307
84
  }
308
309
  /// Determine whether two Knowledges conflict with each other.
310
  ///
311
  /// In theory @p Existing and @p Proposed are symmetric, but the
312
  /// implementation is constrained by the implicit interpretation. That is, @p
313
  /// Existing must have #Unused defined (use case 1) and @p Proposed must have
314
  /// #Occupied defined (use case 1).
315
  ///
316
  /// A conflict is defined as non-preserved semantics when they are merged. For
317
  /// instance, when for the same array and zone they assume different
318
  /// llvm::Values.
319
  ///
320
  /// @param Existing One of the knowledges with #Unused defined.
321
  /// @param Proposed One of the knowledges with #Occupied defined.
322
  /// @param OS       Dump the conflict reason to this output stream; use
323
  ///                 nullptr to not output anything.
324
  /// @param Indent   Indention for the conflict reason.
325
  ///
326
  /// @return True, iff the two knowledges are conflicting.
327
  static bool isConflicting(const Knowledge &Existing,
328
                            const Knowledge &Proposed,
329
                            llvm::raw_ostream *OS = nullptr,
330
321
                            unsigned Indent = 0) {
331
321
    assert(Existing.Unused);
332
321
    assert(Proposed.Occupied);
333
321
334
#ifndef NDEBUG
335
    if (Existing.Occupied && Proposed.Unused) {
336
      auto ExistingUniverse = Existing.Occupied.unite(Existing.Unused);
337
      auto ProposedUniverse = Proposed.Occupied.unite(Proposed.Unused);
338
      assert(ExistingUniverse.is_equal(ProposedUniverse) &&
339
             "Both inputs' Knowledges must be over the same universe");
340
    }
341
#endif
342
343
321
    // Do the Existing and Proposed lifetimes conflict?
344
321
    //
345
321
    // Lifetimes are described as the cross-product of array elements and zone
346
321
    // intervals in which they are alive (the space { [Element[] -> Zone[]] }).
347
321
    // In the following we call this "element/lifetime interval".
348
321
    //
349
321
    // In order to not conflict, one of the following conditions must apply for
350
321
    // each element/lifetime interval:
351
321
    //
352
321
    // 1. If occupied in one of the knowledges, it is unused in the other.
353
321
    //
354
321
    //   - or -
355
321
    //
356
321
    // 2. Both contain the same value.
357
321
    //
358
321
    // Instead of partitioning the element/lifetime intervals into a part that
359
321
    // both Knowledges occupy (which requires an expensive subtraction) and for
360
321
    // these to check whether they are known to be the same value, we check only
361
321
    // the second condition and ensure that it also applies when then first
362
321
    // condition is true. This is done by adding a wildcard value to
363
321
    // Proposed.Known and Existing.Unused such that they match as a common known
364
321
    // value. We use the "unknown ValInst" for this purpose. Every
365
321
    // Existing.Unused may match with an unknown Proposed.Occupied because these
366
321
    // never are in conflict with each other.
367
321
    auto ProposedOccupiedAnyVal = makeUnknownForDomain(Proposed.Occupied);
368
321
    auto ProposedValues = Proposed.Known.unite(ProposedOccupiedAnyVal);
369
321
370
321
    auto ExistingUnusedAnyVal = makeUnknownForDomain(Existing.Unused);
371
321
    auto ExistingValues = Existing.Known.unite(ExistingUnusedAnyVal);
372
321
373
321
    auto MatchingVals = ExistingValues.intersect(ProposedValues);
374
321
    auto Matches = MatchingVals.domain();
375
321
376
321
    // Any Proposed.Occupied must either have a match between the known values
377
321
    // of Existing and Occupied, or be in Existing.Unused. In the latter case,
378
321
    // the previously added "AnyVal" will match each other.
379
321
    if (!Proposed.Occupied.is_subset(Matches)) {
380
43
      if (OS) {
381
0
        auto Conflicting = Proposed.Occupied.subtract(Matches);
382
0
        auto ExistingConflictingKnown =
383
0
            Existing.Known.intersect_domain(Conflicting);
384
0
        auto ProposedConflictingKnown =
385
0
            Proposed.Known.intersect_domain(Conflicting);
386
0
387
0
        OS->indent(Indent) << "Proposed lifetime conflicting with Existing's\n";
388
0
        OS->indent(Indent) << "Conflicting occupied: " << Conflicting << "\n";
389
0
        if (!ExistingConflictingKnown.is_empty())
390
0
          OS->indent(Indent)
391
0
              << "Existing Known:       " << ExistingConflictingKnown << "\n";
392
0
        if (!ProposedConflictingKnown.is_empty())
393
0
          OS->indent(Indent)
394
0
              << "Proposed Known:       " << ProposedConflictingKnown << "\n";
395
0
      }
396
43
      return true;
397
43
    }
398
278
399
278
    // Do the writes in Existing conflict with occupied values in Proposed?
400
278
    //
401
278
    // In order to not conflict, it must either write to unused lifetime or
402
278
    // write the same value. To check, we remove the writes that write into
403
278
    // Proposed.Unused (they never conflict) and then see whether the written
404
278
    // value is already in Proposed.Known. If there are multiple known values
405
278
    // and a written value is known under different names, it is enough when one
406
278
    // of the written values (assuming that they are the same value under
407
278
    // different names, e.g. a PHINode and one of the incoming values) matches
408
278
    // one of the known names.
409
278
    //
410
278
    // We convert here the set of lifetimes to actual timepoints. A lifetime is
411
278
    // in conflict with a set of write timepoints, if either a live timepoint is
412
278
    // clearly within the lifetime or if a write happens at the beginning of the
413
278
    // lifetime (where it would conflict with the value that actually writes the
414
278
    // value alive). There is no conflict at the end of a lifetime, as the alive
415
278
    // value will always be read, before it is overwritten again. The last
416
278
    // property holds in Polly for all scalar values and we expect all users of
417
278
    // Knowledge to check this property also for accesses to MemoryKind::Array.
418
278
    auto ProposedFixedDefs =
419
278
        convertZoneToTimepoints(Proposed.Occupied, true, false);
420
278
    auto ProposedFixedKnown =
421
278
        convertZoneToTimepoints(Proposed.Known, isl::dim::in, true, false);
422
278
423
278
    auto ExistingConflictingWrites =
424
278
        Existing.Written.intersect_domain(ProposedFixedDefs);
425
278
    auto ExistingConflictingWritesDomain = ExistingConflictingWrites.domain();
426
278
427
278
    auto CommonWrittenVal =
428
278
        ProposedFixedKnown.intersect(ExistingConflictingWrites);
429
278
    auto CommonWrittenValDomain = CommonWrittenVal.domain();
430
278
431
278
    if (!ExistingConflictingWritesDomain.is_subset(CommonWrittenValDomain)) {
432
26
      if (OS) {
433
0
        auto ExistingConflictingWritten =
434
0
            ExistingConflictingWrites.subtract_domain(CommonWrittenValDomain);
435
0
        auto ProposedConflictingKnown = ProposedFixedKnown.subtract_domain(
436
0
            ExistingConflictingWritten.domain());
437
0
438
0
        OS->indent(Indent)
439
0
            << "Proposed a lifetime where there is an Existing write into it\n";
440
0
        OS->indent(Indent) << "Existing conflicting writes: "
441
0
                           << ExistingConflictingWritten << "\n";
442
0
        if (!ProposedConflictingKnown.is_empty())
443
0
          OS->indent(Indent)
444
0
              << "Proposed conflicting known:  " << ProposedConflictingKnown
445
0
              << "\n";
446
0
      }
447
26
      return true;
448
26
    }
449
252
450
252
    // Do the writes in Proposed conflict with occupied values in Existing?
451
252
    auto ExistingAvailableDefs =
452
252
        convertZoneToTimepoints(Existing.Unused, true, false);
453
252
    auto ExistingKnownDefs =
454
252
        convertZoneToTimepoints(Existing.Known, isl::dim::in, true, false);
455
252
456
252
    auto ProposedWrittenDomain = Proposed.Written.domain();
457
252
    auto KnownIdentical = ExistingKnownDefs.intersect(Proposed.Written);
458
252
    auto IdenticalOrUnused =
459
252
        ExistingAvailableDefs.unite(KnownIdentical.domain());
460
252
    if (!ProposedWrittenDomain.is_subset(IdenticalOrUnused)) {
461
24
      if (OS) {
462
0
        auto Conflicting = ProposedWrittenDomain.subtract(IdenticalOrUnused);
463
0
        auto ExistingConflictingKnown =
464
0
            ExistingKnownDefs.intersect_domain(Conflicting);
465
0
        auto ProposedConflictingWritten =
466
0
            Proposed.Written.intersect_domain(Conflicting);
467
0
468
0
        OS->indent(Indent) << "Proposed writes into range used by Existing\n";
469
0
        OS->indent(Indent) << "Proposed conflicting writes: "
470
0
                           << ProposedConflictingWritten << "\n";
471
0
        if (!ExistingConflictingKnown.is_empty())
472
0
          OS->indent(Indent)
473
0
              << "Existing conflicting known: " << ExistingConflictingKnown
474
0
              << "\n";
475
0
      }
476
24
      return true;
477
24
    }
478
228
479
228
    // Does Proposed write at the same time as Existing already does (order of
480
228
    // writes is undefined)? Writing the same value is permitted.
481
228
    auto ExistingWrittenDomain = Existing.Written.domain();
482
228
    auto BothWritten =
483
228
        Existing.Written.domain().intersect(Proposed.Written.domain());
484
228
    auto ExistingKnownWritten = filterKnownValInst(Existing.Written);
485
228
    auto ProposedKnownWritten = filterKnownValInst(Proposed.Written);
486
228
    auto CommonWritten =
487
228
        ExistingKnownWritten.intersect(ProposedKnownWritten).domain();
488
228
489
228
    if (!BothWritten.is_subset(CommonWritten)) {
490
24
      if (OS) {
491
0
        auto Conflicting = BothWritten.subtract(CommonWritten);
492
0
        auto ExistingConflictingWritten =
493
0
            Existing.Written.intersect_domain(Conflicting);
494
0
        auto ProposedConflictingWritten =
495
0
            Proposed.Written.intersect_domain(Conflicting);
496
0
497
0
        OS->indent(Indent) << "Proposed writes at the same time as an already "
498
0
                              "Existing write\n";
499
0
        OS->indent(Indent) << "Conflicting writes: " << Conflicting << "\n";
500
0
        if (!ExistingConflictingWritten.is_empty())
501
0
          OS->indent(Indent)
502
0
              << "Exiting write:      " << ExistingConflictingWritten << "\n";
503
0
        if (!ProposedConflictingWritten.is_empty())
504
0
          OS->indent(Indent)
505
0
              << "Proposed write:     " << ProposedConflictingWritten << "\n";
506
0
      }
507
24
      return true;
508
24
    }
509
204
510
204
    return false;
511
204
  }
512
};
513
514
/// Implementation of the DeLICM/DePRE transformation.
515
class DeLICMImpl : public ZoneAlgorithm {
516
private:
517
  /// Knowledge before any transformation took place.
518
  Knowledge OriginalZone;
519
520
  /// Current knowledge of the SCoP including all already applied
521
  /// transformations.
522
  Knowledge Zone;
523
524
  /// Number of StoreInsts something can be mapped to.
525
  int NumberOfCompatibleTargets = 0;
526
527
  /// The number of StoreInsts to which at least one value or PHI has been
528
  /// mapped to.
529
  int NumberOfTargetsMapped = 0;
530
531
  /// The number of llvm::Value mapped to some array element.
532
  int NumberOfMappedValueScalars = 0;
533
534
  /// The number of PHIs mapped to some array element.
535
  int NumberOfMappedPHIScalars = 0;
536
537
  /// Determine whether two knowledges are conflicting with each other.
538
  ///
539
  /// @see Knowledge::isConflicting
540
89
  bool isConflicting(const Knowledge &Proposed) {
541
89
    raw_ostream *OS = nullptr;
542
89
    LLVM_DEBUG(OS = &llvm::dbgs());
543
89
    return Knowledge::isConflicting(Zone, Proposed, OS, 4);
544
89
  }
545
546
  /// Determine whether @p SAI is a scalar that can be mapped to an array
547
  /// element.
548
97
  bool isMappable(const ScopArrayInfo *SAI) {
549
97
    assert(SAI);
550
97
551
97
    if (SAI->isValueKind()) {
552
63
      auto *MA = S->getValueDef(SAI);
553
63
      if (!MA) {
554
2
        LLVM_DEBUG(
555
2
            dbgs()
556
2
            << "    Reject because value is read-only within the scop\n");
557
2
        return false;
558
2
      }
559
61
560
61
      // Mapping if value is used after scop is not supported. The code
561
61
      // generator would need to reload the scalar after the scop, but it
562
61
      // does not have the information to where it is mapped to. Only the
563
61
      // MemoryAccesses have that information, not the ScopArrayInfo.
564
61
      auto Inst = MA->getAccessInstruction();
565
100
      for (auto User : Inst->users()) {
566
100
        if (!isa<Instruction>(User))
567
0
          return false;
568
100
        auto UserInst = cast<Instruction>(User);
569
100
570
100
        if (!S->contains(UserInst)) {
571
1
          LLVM_DEBUG(dbgs() << "    Reject because value is escaping\n");
572
1
          return false;
573
1
        }
574
100
      }
575
61
576
61
      
return true60
;
577
34
    }
578
34
579
34
    if (SAI->isPHIKind()) {
580
34
      auto *MA = S->getPHIRead(SAI);
581
34
      assert(MA);
582
34
583
34
      // Mapping of an incoming block from before the SCoP is not supported by
584
34
      // the code generator.
585
34
      auto PHI = cast<PHINode>(MA->getAccessInstruction());
586
68
      for (auto Incoming : PHI->blocks()) {
587
68
        if (!S->contains(Incoming)) {
588
0
          LLVM_DEBUG(dbgs()
589
0
                     << "    Reject because at least one incoming block is "
590
0
                        "not in the scop region\n");
591
0
          return false;
592
0
        }
593
68
      }
594
34
595
34
      return true;
596
0
    }
597
0
598
0
    LLVM_DEBUG(dbgs() << "    Reject ExitPHI or other non-value\n");
599
0
    return false;
600
0
  }
601
602
  /// Compute the uses of a MemoryKind::Value and its lifetime (from its
603
  /// definition to the last use).
604
  ///
605
  /// @param SAI The ScopArrayInfo representing the value's storage.
606
  ///
607
  /// @return { DomainDef[] -> DomainUse[] }, { DomainDef[] -> Zone[] }
608
  ///         First element is the set of uses for each definition.
609
  ///         The second is the lifetime of each definition.
610
  std::tuple<isl::union_map, isl::map>
611
56
  computeValueUses(const ScopArrayInfo *SAI) {
612
56
    assert(SAI->isValueKind());
613
56
614
56
    // { DomainRead[] }
615
56
    auto Reads = makeEmptyUnionSet();
616
56
617
56
    // Find all uses.
618
56
    for (auto *MA : S->getValueUses(SAI))
619
79
      Reads = Reads.add_set(getDomainFor(MA));
620
56
621
56
    // { DomainRead[] -> Scatter[] }
622
56
    auto ReadSchedule = getScatterFor(Reads);
623
56
624
56
    auto *DefMA = S->getValueDef(SAI);
625
56
    assert(DefMA);
626
56
627
56
    // { DomainDef[] }
628
56
    auto Writes = getDomainFor(DefMA);
629
56
630
56
    // { DomainDef[] -> Scatter[] }
631
56
    auto WriteScatter = getScatterFor(Writes);
632
56
633
56
    // { Scatter[] -> DomainDef[] }
634
56
    auto ReachDef = getScalarReachingDefinition(DefMA->getStatement());
635
56
636
56
    // { [DomainDef[] -> Scatter[]] -> DomainUse[] }
637
56
    auto Uses = isl::union_map(ReachDef.reverse().range_map())
638
56
                    .apply_range(ReadSchedule.reverse());
639
56
640
56
    // { DomainDef[] -> Scatter[] }
641
56
    auto UseScatter =
642
56
        singleton(Uses.domain().unwrap(),
643
56
                  Writes.get_space().map_from_domain_and_range(ScatterSpace));
644
56
645
56
    // { DomainDef[] -> Zone[] }
646
56
    auto Lifetime = betweenScatter(WriteScatter, UseScatter, false, true);
647
56
648
56
    // { DomainDef[] -> DomainRead[] }
649
56
    auto DefUses = Uses.domain_factor_domain();
650
56
651
56
    return std::make_pair(DefUses, Lifetime);
652
56
  }
653
654
  /// Try to map a MemoryKind::Value to a given array element.
655
  ///
656
  /// @param SAI       Representation of the scalar's memory to map.
657
  /// @param TargetElt { Scatter[] -> Element[] }
658
  ///                  Suggestion where to map a scalar to when at a timepoint.
659
  ///
660
  /// @return true if the scalar was successfully mapped.
661
59
  bool tryMapValue(const ScopArrayInfo *SAI, isl::map TargetElt) {
662
59
    assert(SAI->isValueKind());
663
59
664
59
    auto *DefMA = S->getValueDef(SAI);
665
59
    assert(DefMA->isValueKind());
666
59
    assert(DefMA->isMustWrite());
667
59
    auto *V = DefMA->getAccessValue();
668
59
    auto *DefInst = DefMA->getAccessInstruction();
669
59
670
59
    // Stop if the scalar has already been mapped.
671
59
    if (!DefMA->getLatestScopArrayInfo()->isValueKind())
672
1
      return false;
673
58
674
58
    // { DomainDef[] -> Scatter[] }
675
58
    auto DefSched = getScatterFor(DefMA);
676
58
677
58
    // Where each write is mapped to, according to the suggestion.
678
58
    // { DomainDef[] -> Element[] }
679
58
    auto DefTarget = TargetElt.apply_domain(DefSched.reverse());
680
58
    simplify(DefTarget);
681
58
    LLVM_DEBUG(dbgs() << "    Def Mapping: " << DefTarget << '\n');
682
58
683
58
    auto OrigDomain = getDomainFor(DefMA);
684
58
    auto MappedDomain = DefTarget.domain();
685
58
    if (!OrigDomain.is_subset(MappedDomain)) {
686
2
      LLVM_DEBUG(
687
2
          dbgs()
688
2
          << "    Reject because mapping does not encompass all instances\n");
689
2
      return false;
690
2
    }
691
56
692
56
    // { DomainDef[] -> Zone[] }
693
56
    isl::map Lifetime;
694
56
695
56
    // { DomainDef[] -> DomainUse[] }
696
56
    isl::union_map DefUses;
697
56
698
56
    std::tie(DefUses, Lifetime) = computeValueUses(SAI);
699
56
    LLVM_DEBUG(dbgs() << "    Lifetime: " << Lifetime << '\n');
700
56
701
56
    /// { [Element[] -> Zone[]] }
702
56
    auto EltZone = Lifetime.apply_domain(DefTarget).wrap();
703
56
    simplify(EltZone);
704
56
705
56
    // When known knowledge is disabled, just return the unknown value. It will
706
56
    // either get filtered out or conflict with itself.
707
56
    // { DomainDef[] -> ValInst[] }
708
56
    isl::map ValInst;
709
56
    if (DelicmComputeKnown)
710
56
      ValInst = makeValInst(V, DefMA->getStatement(),
711
56
                            LI->getLoopFor(DefInst->getParent()));
712
0
    else
713
0
      ValInst = makeUnknownForDomain(DefMA->getStatement());
714
56
715
56
    // { DomainDef[] -> [Element[] -> Zone[]] }
716
56
    auto EltKnownTranslator = DefTarget.range_product(Lifetime);
717
56
718
56
    // { [Element[] -> Zone[]] -> ValInst[] }
719
56
    auto EltKnown = ValInst.apply_domain(EltKnownTranslator);
720
56
    simplify(EltKnown);
721
56
722
56
    // { DomainDef[] -> [Element[] -> Scatter[]] }
723
56
    auto WrittenTranslator = DefTarget.range_product(DefSched);
724
56
725
56
    // { [Element[] -> Scatter[]] -> ValInst[] }
726
56
    auto DefEltSched = ValInst.apply_domain(WrittenTranslator);
727
56
    simplify(DefEltSched);
728
56
729
56
    Knowledge Proposed(EltZone, nullptr, filterKnownValInst(EltKnown),
730
56
                       DefEltSched);
731
56
    if (isConflicting(Proposed))
732
5
      return false;
733
51
734
51
    // { DomainUse[] -> Element[] }
735
51
    auto UseTarget = DefUses.reverse().apply_range(DefTarget);
736
51
737
51
    mapValue(SAI, std::move(DefTarget), std::move(UseTarget),
738
51
             std::move(Lifetime), std::move(Proposed));
739
51
    return true;
740
51
  }
741
742
  /// After a scalar has been mapped, update the global knowledge.
743
84
  void applyLifetime(Knowledge Proposed) {
744
84
    Zone.learnFrom(std::move(Proposed));
745
84
  }
746
747
  /// Map a MemoryKind::Value scalar to an array element.
748
  ///
749
  /// Callers must have ensured that the mapping is valid and not conflicting.
750
  ///
751
  /// @param SAI       The ScopArrayInfo representing the scalar's memory to
752
  ///                  map.
753
  /// @param DefTarget { DomainDef[] -> Element[] }
754
  ///                  The array element to map the scalar to.
755
  /// @param UseTarget { DomainUse[] -> Element[] }
756
  ///                  The array elements the uses are mapped to.
757
  /// @param Lifetime  { DomainDef[] -> Zone[] }
758
  ///                  The lifetime of each llvm::Value definition for
759
  ///                  reporting.
760
  /// @param Proposed  Mapping constraints for reporting.
761
  void mapValue(const ScopArrayInfo *SAI, isl::map DefTarget,
762
                isl::union_map UseTarget, isl::map Lifetime,
763
51
                Knowledge Proposed) {
764
51
    // Redirect the read accesses.
765
69
    for (auto *MA : S->getValueUses(SAI)) {
766
69
      // { DomainUse[] }
767
69
      auto Domain = getDomainFor(MA);
768
69
769
69
      // { DomainUse[] -> Element[] }
770
69
      auto NewAccRel = UseTarget.intersect_domain(Domain);
771
69
      simplify(NewAccRel);
772
69
773
69
      assert(isl_union_map_n_map(NewAccRel.get()) == 1);
774
69
      MA->setNewAccessRelation(isl::map::from_union_map(NewAccRel));
775
69
    }
776
51
777
51
    auto *WA = S->getValueDef(SAI);
778
51
    WA->setNewAccessRelation(DefTarget);
779
51
    applyLifetime(Proposed);
780
51
781
51
    MappedValueScalars++;
782
51
    NumberOfMappedValueScalars += 1;
783
51
  }
784
785
  isl::map makeValInst(Value *Val, ScopStmt *UserStmt, Loop *Scope,
786
122
                       bool IsCertain = true) {
787
122
    // When known knowledge is disabled, just return the unknown value. It will
788
122
    // either get filtered out or conflict with itself.
789
122
    if (!DelicmComputeKnown)
790
0
      return makeUnknownForDomain(UserStmt);
791
122
    return ZoneAlgorithm::makeValInst(Val, UserStmt, Scope, IsCertain);
792
122
  }
793
794
  /// Express the incoming values of a PHI for each incoming statement in an
795
  /// isl::union_map.
796
  ///
797
  /// @param SAI The PHI scalar represented by a ScopArrayInfo.
798
  ///
799
  /// @return { PHIWriteDomain[] -> ValInst[] }
800
33
  isl::union_map determinePHIWrittenValues(const ScopArrayInfo *SAI) {
801
33
    auto Result = makeEmptyUnionMap();
802
33
803
33
    // Collect the incoming values.
804
66
    for (auto *MA : S->getPHIIncomings(SAI)) {
805
66
      // { DomainWrite[] -> ValInst[] }
806
66
      isl::union_map ValInst;
807
66
      auto *WriteStmt = MA->getStatement();
808
66
809
66
      auto Incoming = MA->getIncoming();
810
66
      assert(!Incoming.empty());
811
66
      if (Incoming.size() == 1) {
812
66
        ValInst = makeValInst(Incoming[0].second, WriteStmt,
813
66
                              LI->getLoopFor(Incoming[0].first));
814
66
      } else {
815
0
        // If the PHI is in a subregion's exit node it can have multiple
816
0
        // incoming values (+ maybe another incoming edge from an unrelated
817
0
        // block). We cannot directly represent it as a single llvm::Value.
818
0
        // We currently model it as unknown value, but modeling as the PHIInst
819
0
        // itself could be OK, too.
820
0
        ValInst = makeUnknownForDomain(WriteStmt);
821
0
      }
822
66
823
66
      Result = Result.unite(ValInst);
824
66
    }
825
33
826
33
    assert(Result.is_single_valued() &&
827
33
           "Cannot have multiple incoming values for same incoming statement");
828
33
    return Result;
829
33
  }
830
831
  /// Try to map a MemoryKind::PHI scalar to a given array element.
832
  ///
833
  /// @param SAI       Representation of the scalar's memory to map.
834
  /// @param TargetElt { Scatter[] -> Element[] }
835
  ///                  Suggestion where to map the scalar to when at a
836
  ///                  timepoint.
837
  ///
838
  /// @return true if the PHI scalar has been mapped.
839
34
  bool tryMapPHI(const ScopArrayInfo *SAI, isl::map TargetElt) {
840
34
    auto *PHIRead = S->getPHIRead(SAI);
841
34
    assert(PHIRead->isPHIKind());
842
34
    assert(PHIRead->isRead());
843
34
844
34
    // Skip if already been mapped.
845
34
    if (!PHIRead->getLatestScopArrayInfo()->isPHIKind())
846
0
      return false;
847
34
848
34
    // { DomainRead[] -> Scatter[] }
849
34
    auto PHISched = getScatterFor(PHIRead);
850
34
851
34
    // { DomainRead[] -> Element[] }
852
34
    auto PHITarget = PHISched.apply_range(TargetElt);
853
34
    simplify(PHITarget);
854
34
    LLVM_DEBUG(dbgs() << "    Mapping: " << PHITarget << '\n');
855
34
856
34
    auto OrigDomain = getDomainFor(PHIRead);
857
34
    auto MappedDomain = PHITarget.domain();
858
34
    if (!OrigDomain.is_subset(MappedDomain)) {
859
0
      LLVM_DEBUG(
860
0
          dbgs()
861
0
          << "    Reject because mapping does not encompass all instances\n");
862
0
      return false;
863
0
    }
864
34
865
34
    // { DomainRead[] -> DomainWrite[] }
866
34
    auto PerPHIWrites = computePerPHI(SAI);
867
34
868
34
    // { DomainWrite[] -> Element[] }
869
34
    auto WritesTarget = PerPHIWrites.apply_domain(PHITarget).reverse();
870
34
    simplify(WritesTarget);
871
34
872
34
    // { DomainWrite[] }
873
34
    auto UniverseWritesDom = isl::union_set::empty(ParamSpace);
874
34
875
34
    for (auto *MA : S->getPHIIncomings(SAI))
876
68
      UniverseWritesDom = UniverseWritesDom.add_set(getDomainFor(MA));
877
34
878
34
    auto RelevantWritesTarget = WritesTarget;
879
34
    if (DelicmOverapproximateWrites)
880
11
      WritesTarget = expandMapping(WritesTarget, UniverseWritesDom);
881
34
882
34
    auto ExpandedWritesDom = WritesTarget.domain();
883
34
    if (!DelicmPartialWrites &&
884
34
        
!UniverseWritesDom.is_subset(ExpandedWritesDom)3
) {
885
1
      LLVM_DEBUG(
886
1
          dbgs() << "    Reject because did not find PHI write mapping for "
887
1
                    "all instances\n");
888
1
      if (DelicmOverapproximateWrites)
889
1
        LLVM_DEBUG(dbgs() << "      Relevant Mapping:    "
890
1
                          << RelevantWritesTarget << '\n');
891
1
      LLVM_DEBUG(dbgs() << "      Deduced Mapping:     " << WritesTarget
892
1
                        << '\n');
893
1
      LLVM_DEBUG(dbgs() << "      Missing instances:    "
894
1
                        << UniverseWritesDom.subtract(ExpandedWritesDom)
895
1
                        << '\n');
896
1
      return false;
897
1
    }
898
33
899
33
    //  { DomainRead[] -> Scatter[] }
900
33
    auto PerPHIWriteScatter =
901
33
        isl::map::from_union_map(PerPHIWrites.apply_range(Schedule));
902
33
903
33
    // { DomainRead[] -> Zone[] }
904
33
    auto Lifetime = betweenScatter(PerPHIWriteScatter, PHISched, false, true);
905
33
    simplify(Lifetime);
906
33
    LLVM_DEBUG(dbgs() << "    Lifetime: " << Lifetime << "\n");
907
33
908
33
    // { DomainWrite[] -> Zone[] }
909
33
    auto WriteLifetime = isl::union_map(Lifetime).apply_domain(PerPHIWrites);
910
33
911
33
    // { DomainWrite[] -> ValInst[] }
912
33
    auto WrittenValue = determinePHIWrittenValues(SAI);
913
33
914
33
    // { DomainWrite[] -> [Element[] -> Scatter[]] }
915
33
    auto WrittenTranslator = WritesTarget.range_product(Schedule);
916
33
917
33
    // { [Element[] -> Scatter[]] -> ValInst[] }
918
33
    auto Written = WrittenValue.apply_domain(WrittenTranslator);
919
33
    simplify(Written);
920
33
921
33
    // { DomainWrite[] -> [Element[] -> Zone[]] }
922
33
    auto LifetimeTranslator = WritesTarget.range_product(WriteLifetime);
923
33
924
33
    // { DomainWrite[] -> ValInst[] }
925
33
    auto WrittenKnownValue = filterKnownValInst(WrittenValue);
926
33
927
33
    // { [Element[] -> Zone[]] -> ValInst[] }
928
33
    auto EltLifetimeInst = WrittenKnownValue.apply_domain(LifetimeTranslator);
929
33
    simplify(EltLifetimeInst);
930
33
931
33
    // { [Element[] -> Zone[] }
932
33
    auto Occupied = LifetimeTranslator.range();
933
33
    simplify(Occupied);
934
33
935
33
    Knowledge Proposed(Occupied, nullptr, EltLifetimeInst, Written);
936
33
    if (isConflicting(Proposed))
937
0
      return false;
938
33
939
33
    mapPHI(SAI, std::move(PHITarget), std::move(WritesTarget),
940
33
           std::move(Lifetime), std::move(Proposed));
941
33
    return true;
942
33
  }
943
944
  /// Map a MemoryKind::PHI scalar to an array element.
945
  ///
946
  /// Callers must have ensured that the mapping is valid and not conflicting
947
  /// with the common knowledge.
948
  ///
949
  /// @param SAI         The ScopArrayInfo representing the scalar's memory to
950
  ///                    map.
951
  /// @param ReadTarget  { DomainRead[] -> Element[] }
952
  ///                    The array element to map the scalar to.
953
  /// @param WriteTarget { DomainWrite[] -> Element[] }
954
  ///                    New access target for each PHI incoming write.
955
  /// @param Lifetime    { DomainRead[] -> Zone[] }
956
  ///                    The lifetime of each PHI for reporting.
957
  /// @param Proposed    Mapping constraints for reporting.
958
  void mapPHI(const ScopArrayInfo *SAI, isl::map ReadTarget,
959
              isl::union_map WriteTarget, isl::map Lifetime,
960
33
              Knowledge Proposed) {
961
33
    // { Element[] }
962
33
    isl::space ElementSpace = ReadTarget.get_space().range();
963
33
964
33
    // Redirect the PHI incoming writes.
965
66
    for (auto *MA : S->getPHIIncomings(SAI)) {
966
66
      // { DomainWrite[] }
967
66
      auto Domain = getDomainFor(MA);
968
66
969
66
      // { DomainWrite[] -> Element[] }
970
66
      auto NewAccRel = WriteTarget.intersect_domain(Domain);
971
66
      simplify(NewAccRel);
972
66
973
66
      isl::space NewAccRelSpace =
974
66
          Domain.get_space().map_from_domain_and_range(ElementSpace);
975
66
      isl::map NewAccRelMap = singleton(NewAccRel, NewAccRelSpace);
976
66
      MA->setNewAccessRelation(NewAccRelMap);
977
66
    }
978
33
979
33
    // Redirect the PHI read.
980
33
    auto *PHIRead = S->getPHIRead(SAI);
981
33
    PHIRead->setNewAccessRelation(ReadTarget);
982
33
    applyLifetime(Proposed);
983
33
984
33
    MappedPHIScalars++;
985
33
    NumberOfMappedPHIScalars++;
986
33
  }
987
988
  /// Search and map scalars to memory overwritten by @p TargetStoreMA.
989
  ///
990
  /// Start trying to map scalars that are used in the same statement as the
991
  /// store. For every successful mapping, try to also map scalars of the
992
  /// statements where those are written. Repeat, until no more mapping
993
  /// opportunity is found.
994
  ///
995
  /// There is currently no preference in which order scalars are tried.
996
  /// Ideally, we would direct it towards a load instruction of the same array
997
  /// element.
998
40
  bool collapseScalarsToStore(MemoryAccess *TargetStoreMA) {
999
40
    assert(TargetStoreMA->isLatestArrayKind());
1000
40
    assert(TargetStoreMA->isMustWrite());
1001
40
1002
40
    auto TargetStmt = TargetStoreMA->getStatement();
1003
40
1004
40
    // { DomTarget[] }
1005
40
    auto TargetDom = getDomainFor(TargetStmt);
1006
40
1007
40
    // { DomTarget[] -> Element[] }
1008
40
    auto TargetAccRel = getAccessRelationFor(TargetStoreMA);
1009
40
1010
40
    // { Zone[] -> DomTarget[] }
1011
40
    // For each point in time, find the next target store instance.
1012
40
    auto Target =
1013
40
        computeScalarReachingOverwrite(Schedule, TargetDom, false, true);
1014
40
1015
40
    // { Zone[] -> Element[] }
1016
40
    // Use the target store's write location as a suggestion to map scalars to.
1017
40
    auto EltTarget = Target.apply_range(TargetAccRel);
1018
40
    simplify(EltTarget);
1019
40
    LLVM_DEBUG(dbgs() << "    Target mapping is " << EltTarget << '\n');
1020
40
1021
40
    // Stack of elements not yet processed.
1022
40
    SmallVector<MemoryAccess *, 16> Worklist;
1023
40
1024
40
    // Set of scalars already tested.
1025
40
    SmallPtrSet<const ScopArrayInfo *, 16> Closed;
1026
40
1027
40
    // Lambda to add all scalar reads to the work list.
1028
98
    auto ProcessAllIncoming = [&](ScopStmt *Stmt) {
1029
198
      for (auto *MA : *Stmt) {
1030
198
        if (!MA->isLatestScalarKind())
1031
137
          continue;
1032
61
        if (!MA->isRead())
1033
9
          continue;
1034
52
1035
52
        Worklist.push_back(MA);
1036
52
      }
1037
98
    };
1038
40
1039
40
    auto *WrittenVal = TargetStoreMA->getAccessInstruction()->getOperand(0);
1040
40
    if (auto *WrittenValInputMA = TargetStmt->lookupInputAccessOf(WrittenVal))
1041
29
      Worklist.push_back(WrittenValInputMA);
1042
11
    else
1043
11
      ProcessAllIncoming(TargetStmt);
1044
40
1045
40
    auto AnyMapped = false;
1046
40
    auto &DL = S->getRegion().getEntry()->getModule()->getDataLayout();
1047
40
    auto StoreSize =
1048
40
        DL.getTypeAllocSize(TargetStoreMA->getAccessValue()->getType());
1049
40
1050
151
    while (!Worklist.empty()) {
1051
111
      auto *MA = Worklist.pop_back_val();
1052
111
1053
111
      auto *SAI = MA->getScopArrayInfo();
1054
111
      if (Closed.count(SAI))
1055
14
        continue;
1056
97
      Closed.insert(SAI);
1057
97
      LLVM_DEBUG(dbgs() << "\n    Trying to map " << MA << " (SAI: " << SAI
1058
97
                        << ")\n");
1059
97
1060
97
      // Skip non-mappable scalars.
1061
97
      if (!isMappable(SAI))
1062
3
        continue;
1063
94
1064
94
      auto MASize = DL.getTypeAllocSize(MA->getAccessValue()->getType());
1065
94
      if (MASize > StoreSize) {
1066
1
        LLVM_DEBUG(
1067
1
            dbgs() << "    Reject because storage size is insufficient\n");
1068
1
        continue;
1069
1
      }
1070
93
1071
93
      // Try to map MemoryKind::Value scalars.
1072
93
      if (SAI->isValueKind()) {
1073
59
        if (!tryMapValue(SAI, EltTarget))
1074
8
          continue;
1075
51
1076
51
        auto *DefAcc = S->getValueDef(SAI);
1077
51
        ProcessAllIncoming(DefAcc->getStatement());
1078
51
1079
51
        AnyMapped = true;
1080
51
        continue;
1081
51
      }
1082
34
1083
34
      // Try to map MemoryKind::PHI scalars.
1084
34
      if (SAI->isPHIKind()) {
1085
34
        if (!tryMapPHI(SAI, EltTarget))
1086
1
          continue;
1087
33
        // Add inputs of all incoming statements to the worklist. Prefer the
1088
33
        // input accesses of the incoming blocks.
1089
66
        
for (auto *PHIWrite : S->getPHIIncomings(SAI))33
{
1090
66
          auto *PHIWriteStmt = PHIWrite->getStatement();
1091
66
          bool FoundAny = false;
1092
66
          for (auto Incoming : PHIWrite->getIncoming()) {
1093
66
            auto *IncomingInputMA =
1094
66
                PHIWriteStmt->lookupInputAccessOf(Incoming.second);
1095
66
            if (!IncomingInputMA)
1096
36
              continue;
1097
30
1098
30
            Worklist.push_back(IncomingInputMA);
1099
30
            FoundAny = true;
1100
30
          }
1101
66
1102
66
          if (!FoundAny)
1103
36
            ProcessAllIncoming(PHIWrite->getStatement());
1104
66
        }
1105
33
1106
33
        AnyMapped = true;
1107
33
        continue;
1108
33
      }
1109
34
    }
1110
40
1111
40
    if (AnyMapped) {
1112
30
      TargetsMapped++;
1113
30
      NumberOfTargetsMapped++;
1114
30
    }
1115
40
    return AnyMapped;
1116
40
  }
1117
1118
  /// Compute when an array element is unused.
1119
  ///
1120
  /// @return { [Element[] -> Zone[]] }
1121
51
  isl::union_set computeLifetime() const {
1122
51
    // { Element[] -> Zone[] }
1123
51
    auto ArrayUnused = computeArrayUnused(Schedule, AllMustWrites, AllReads,
1124
51
                                          false, false, true);
1125
51
1126
51
    auto Result = ArrayUnused.wrap();
1127
51
1128
51
    simplify(Result);
1129
51
    return Result;
1130
51
  }
1131
1132
  /// Determine when an array element is written to, and which value instance is
1133
  /// written.
1134
  ///
1135
  /// @return { [Element[] -> Scatter[]] -> ValInst[] }
1136
51
  isl::union_map computeWritten() const {
1137
51
    // { [Element[] -> Scatter[]] -> ValInst[] }
1138
51
    auto EltWritten = applyDomainRange(AllWriteValInst, Schedule);
1139
51
1140
51
    simplify(EltWritten);
1141
51
    return EltWritten;
1142
51
  }
1143
1144
  /// Determine whether an access touches at most one element.
1145
  ///
1146
  /// The accessed element could be a scalar or accessing an array with constant
1147
  /// subscript, such that all instances access only that element.
1148
  ///
1149
  /// @param MA The access to test.
1150
  ///
1151
  /// @return True, if zero or one elements are accessed; False if at least two
1152
  ///         different elements are accessed.
1153
58
  bool isScalarAccess(MemoryAccess *MA) {
1154
58
    auto Map = getAccessRelationFor(MA);
1155
58
    auto Set = Map.range();
1156
58
    return Set.is_singleton();
1157
58
  }
1158
1159
  /// Print mapping statistics to @p OS.
1160
44
  void printStatistics(llvm::raw_ostream &OS, int Indent = 0) const {
1161
44
    OS.indent(Indent) << "Statistics {\n";
1162
44
    OS.indent(Indent + 4) << "Compatible overwrites: "
1163
44
                          << NumberOfCompatibleTargets << "\n";
1164
44
    OS.indent(Indent + 4) << "Overwrites mapped to:  " << NumberOfTargetsMapped
1165
44
                          << '\n';
1166
44
    OS.indent(Indent + 4) << "Value scalars mapped:  "
1167
44
                          << NumberOfMappedValueScalars << '\n';
1168
44
    OS.indent(Indent + 4) << "PHI scalars mapped:    "
1169
44
                          << NumberOfMappedPHIScalars << '\n';
1170
44
    OS.indent(Indent) << "}\n";
1171
44
  }
1172
1173
  /// Return whether at least one transformation been applied.
1174
44
  bool isModified() const { return NumberOfTargetsMapped > 0; }
1175
1176
public:
1177
51
  DeLICMImpl(Scop *S, LoopInfo *LI) : ZoneAlgorithm("polly-delicm", S, LI) {}
1178
1179
  /// Calculate the lifetime (definition to last use) of every array element.
1180
  ///
1181
  /// @return True if the computed lifetimes (#Zone) is usable.
1182
51
  bool computeZone() {
1183
51
    // Check that nothing strange occurs.
1184
51
    collectCompatibleElts();
1185
51
1186
51
    isl::union_set EltUnused;
1187
51
    isl::union_map EltKnown, EltWritten;
1188
51
1189
51
    {
1190
51
      IslMaxOperationsGuard MaxOpGuard(IslCtx.get(), DelicmMaxOps);
1191
51
1192
51
      computeCommon();
1193
51
1194
51
      EltUnused = computeLifetime();
1195
51
      EltKnown = computeKnown(true, false);
1196
51
      EltWritten = computeWritten();
1197
51
    }
1198
51
    DeLICMAnalyzed++;
1199
51
1200
51
    if (!EltUnused || 
!EltKnown48
||
!EltWritten48
) {
1201
3
      assert(isl_ctx_last_error(IslCtx.get()) == isl_error_quota &&
1202
3
             "The only reason that these things have not been computed should "
1203
3
             "be if the max-operations limit hit");
1204
3
      DeLICMOutOfQuota++;
1205
3
      LLVM_DEBUG(dbgs() << "DeLICM analysis exceeded max_operations\n");
1206
3
      DebugLoc Begin, End;
1207
3
      getDebugLocations(getBBPairForRegion(&S->getRegion()), Begin, End);
1208
3
      OptimizationRemarkAnalysis R(DEBUG_TYPE, "OutOfQuota", Begin,
1209
3
                                   S->getEntry());
1210
3
      R << "maximal number of operations exceeded during zone analysis";
1211
3
      S->getFunction().getContext().diagnose(R);
1212
3
      return false;
1213
3
    }
1214
48
1215
48
    Zone = OriginalZone = Knowledge(nullptr, EltUnused, EltKnown, EltWritten);
1216
48
    LLVM_DEBUG(dbgs() << "Computed Zone:\n"; OriginalZone.print(dbgs(), 4));
1217
48
1218
48
    assert(Zone.isUsable() && OriginalZone.isUsable());
1219
48
    return true;
1220
48
  }
1221
1222
  /// Try to map as many scalars to unused array elements as possible.
1223
  ///
1224
  /// Multiple scalars might be mappable to intersecting unused array element
1225
  /// zones, but we can only chose one. This is a greedy algorithm, therefore
1226
  /// the first processed element claims it.
1227
48
  void greedyCollapse() {
1228
48
    bool Modified = false;
1229
48
1230
222
    for (auto &Stmt : *S) {
1231
436
      for (auto *MA : Stmt) {
1232
436
        if (!MA->isLatestArrayKind())
1233
326
          continue;
1234
110
        if (!MA->isWrite())
1235
43
          continue;
1236
67
1237
67
        if (MA->isMayWrite()) {
1238
4
          LLVM_DEBUG(dbgs() << "Access " << MA
1239
4
                            << " pruned because it is a MAY_WRITE\n");
1240
4
          OptimizationRemarkMissed R(DEBUG_TYPE, "TargetMayWrite",
1241
4
                                     MA->getAccessInstruction());
1242
4
          R << "Skipped possible mapping target because it is not an "
1243
4
               "unconditional overwrite";
1244
4
          S->getFunction().getContext().diagnose(R);
1245
4
          continue;
1246
4
        }
1247
63
1248
63
        if (Stmt.getNumIterators() == 0) {
1249
5
          LLVM_DEBUG(dbgs() << "Access " << MA
1250
5
                            << " pruned because it is not in a loop\n");
1251
5
          OptimizationRemarkMissed R(DEBUG_TYPE, "WriteNotInLoop",
1252
5
                                     MA->getAccessInstruction());
1253
5
          R << "skipped possible mapping target because it is not in a loop";
1254
5
          S->getFunction().getContext().diagnose(R);
1255
5
          continue;
1256
5
        }
1257
58
1258
58
        if (isScalarAccess(MA)) {
1259
5
          LLVM_DEBUG(dbgs()
1260
5
                     << "Access " << MA
1261
5
                     << " pruned because it writes only a single element\n");
1262
5
          OptimizationRemarkMissed R(DEBUG_TYPE, "ScalarWrite",
1263
5
                                     MA->getAccessInstruction());
1264
5
          R << "skipped possible mapping target because the memory location "
1265
5
               "written to does not depend on its outer loop";
1266
5
          S->getFunction().getContext().diagnose(R);
1267
5
          continue;
1268
5
        }
1269
53
1270
53
        if (!isa<StoreInst>(MA->getAccessInstruction())) {
1271
8
          LLVM_DEBUG(dbgs() << "Access " << MA
1272
8
                            << " pruned because it is not a StoreInst\n");
1273
8
          OptimizationRemarkMissed R(DEBUG_TYPE, "NotAStore",
1274
8
                                     MA->getAccessInstruction());
1275
8
          R << "skipped possible mapping target because non-store instructions "
1276
8
               "are not supported";
1277
8
          S->getFunction().getContext().diagnose(R);
1278
8
          continue;
1279
8
        }
1280
45
1281
45
        // Check for more than one element acces per statement instance.
1282
45
        // Currently we expect write accesses to be functional, eg. disallow
1283
45
        //
1284
45
        //   { Stmt[0] -> [i] : 0 <= i < 2 }
1285
45
        //
1286
45
        // This may occur when some accesses to the element write/read only
1287
45
        // parts of the element, eg. a single byte. Polly then divides each
1288
45
        // element into subelements of the smallest access length, normal access
1289
45
        // then touch multiple of such subelements. It is very common when the
1290
45
        // array is accesses with memset, memcpy or memmove which take i8*
1291
45
        // arguments.
1292
45
        isl::union_map AccRel = MA->getLatestAccessRelation();
1293
45
        if (!AccRel.is_single_valued().is_true()) {
1294
1
          LLVM_DEBUG(dbgs() << "Access " << MA
1295
1
                            << " is incompatible because it writes multiple "
1296
1
                               "elements per instance\n");
1297
1
          OptimizationRemarkMissed R(DEBUG_TYPE, "NonFunctionalAccRel",
1298
1
                                     MA->getAccessInstruction());
1299
1
          R << "skipped possible mapping target because it writes more than "
1300
1
               "one element";
1301
1
          S->getFunction().getContext().diagnose(R);
1302
1
          continue;
1303
1
        }
1304
44
1305
44
        isl::union_set TouchedElts = AccRel.range();
1306
44
        if (!TouchedElts.is_subset(CompatibleElts)) {
1307
4
          LLVM_DEBUG(
1308
4
              dbgs()
1309
4
              << "Access " << MA
1310
4
              << " is incompatible because it touches incompatible elements\n");
1311
4
          OptimizationRemarkMissed R(DEBUG_TYPE, "IncompatibleElts",
1312
4
                                     MA->getAccessInstruction());
1313
4
          R << "skipped possible mapping target because a target location "
1314
4
               "cannot be reliably analyzed";
1315
4
          S->getFunction().getContext().diagnose(R);
1316
4
          continue;
1317
4
        }
1318
40
1319
40
        assert(isCompatibleAccess(MA));
1320
40
        NumberOfCompatibleTargets++;
1321
40
        LLVM_DEBUG(dbgs() << "Analyzing target access " << MA << "\n");
1322
40
        if (collapseScalarsToStore(MA))
1323
30
          Modified = true;
1324
40
      }
1325
222
    }
1326
48
1327
48
    if (Modified)
1328
30
      DeLICMScopsModified++;
1329
48
  }
1330
1331
  /// Dump the internal information about a performed DeLICM to @p OS.
1332
47
  void print(llvm::raw_ostream &OS, int Indent = 0) {
1333
47
    if (!Zone.isUsable()) {
1334
3
      OS.indent(Indent) << "Zone not computed\n";
1335
3
      return;
1336
3
    }
1337
44
1338
44
    printStatistics(OS, Indent);
1339
44
    if (!isModified()) {
1340
16
      OS.indent(Indent) << "No modification has been made\n";
1341
16
      return;
1342
16
    }
1343
28
    printAccesses(OS, Indent);
1344
28
  }
1345
};
1346
1347
class DeLICM : public ScopPass {
1348
private:
1349
  DeLICM(const DeLICM &) = delete;
1350
  const DeLICM &operator=(const DeLICM &) = delete;
1351
1352
  /// The pass implementation, also holding per-scop data.
1353
  std::unique_ptr<DeLICMImpl> Impl;
1354
1355
51
  void collapseToUnused(Scop &S) {
1356
51
    auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1357
51
    Impl = make_unique<DeLICMImpl>(&S, &LI);
1358
51
1359
51
    if (!Impl->computeZone()) {
1360
3
      LLVM_DEBUG(dbgs() << "Abort because cannot reliably compute lifetimes\n");
1361
3
      return;
1362
3
    }
1363
48
1364
48
    LLVM_DEBUG(dbgs() << "Collapsing scalars to unused array elements...\n");
1365
48
    Impl->greedyCollapse();
1366
48
1367
48
    LLVM_DEBUG(dbgs() << "\nFinal Scop:\n");
1368
48
    LLVM_DEBUG(dbgs() << S);
1369
48
  }
1370
1371
public:
1372
  static char ID;
1373
51
  explicit DeLICM() : ScopPass(ID) {}
1374
1375
51
  virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
1376
51
    AU.addRequiredTransitive<ScopInfoRegionPass>();
1377
51
    AU.addRequired<LoopInfoWrapperPass>();
1378
51
    AU.setPreservesAll();
1379
51
  }
1380
1381
51
  virtual bool runOnScop(Scop &S) override {
1382
51
    // Free resources for previous scop's computation, if not yet done.
1383
51
    releaseMemory();
1384
51
1385
51
    collapseToUnused(S);
1386
51
1387
51
    auto ScopStats = S.getStatistics();
1388
51
    NumValueWrites += ScopStats.NumValueWrites;
1389
51
    NumValueWritesInLoops += ScopStats.NumValueWritesInLoops;
1390
51
    NumPHIWrites += ScopStats.NumPHIWrites;
1391
51
    NumPHIWritesInLoops += ScopStats.NumPHIWritesInLoops;
1392
51
    NumSingletonWrites += ScopStats.NumSingletonWrites;
1393
51
    NumSingletonWritesInLoops += ScopStats.NumSingletonWritesInLoops;
1394
51
1395
51
    return false;
1396
51
  }
1397
1398
47
  virtual void printScop(raw_ostream &OS, Scop &S) const override {
1399
47
    if (!Impl)
1400
0
      return;
1401
47
    assert(Impl->getScop() == &S);
1402
47
1403
47
    OS << "DeLICM result:\n";
1404
47
    Impl->print(OS);
1405
47
  }
1406
1407
279
  virtual void releaseMemory() override { Impl.reset(); }
1408
};
1409
1410
char DeLICM::ID;
1411
} // anonymous namespace
1412
1413
0
Pass *polly::createDeLICMPass() { return new DeLICM(); }
1414
1415
46.4k
INITIALIZE_PASS_BEGIN(DeLICM, "polly-delicm", "Polly - DeLICM/DePRE", false,
1416
46.4k
                      false)
1417
46.4k
INITIALIZE_PASS_DEPENDENCY(ScopInfoWrapperPass)
1418
46.4k
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
1419
46.4k
INITIALIZE_PASS_END(DeLICM, "polly-delicm", "Polly - DeLICM/DePRE", false,
1420
                    false)
1421
1422
bool polly::isConflicting(
1423
    isl::union_set ExistingOccupied, isl::union_set ExistingUnused,
1424
    isl::union_map ExistingKnown, isl::union_map ExistingWrites,
1425
    isl::union_set ProposedOccupied, isl::union_set ProposedUnused,
1426
    isl::union_map ProposedKnown, isl::union_map ProposedWrites,
1427
232
    llvm::raw_ostream *OS, unsigned Indent) {
1428
232
  Knowledge Existing(std::move(ExistingOccupied), std::move(ExistingUnused),
1429
232
                     std::move(ExistingKnown), std::move(ExistingWrites));
1430
232
  Knowledge Proposed(std::move(ProposedOccupied), std::move(ProposedUnused),
1431
232
                     std::move(ProposedKnown), std::move(ProposedWrites));
1432
232
1433
232
  return Knowledge::isConflicting(Existing, Proposed, OS, Indent);
1434
232
}