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