xref: /llvm-project/llvm/lib/Transforms/Scalar/LoopDistribute.cpp (revision 61399ac424373662b72515e1c47b799c8204536f)
1 //===- LoopDistribute.cpp - Loop Distribution Pass ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the Loop Distribution Pass.  Its main focus is to
11 // distribute loops that cannot be vectorized due to dependence cycles.  It
12 // tries to isolate the offending dependences into a new loop allowing
13 // vectorization of the remaining parts.
14 //
15 // For dependence analysis, the pass uses the LoopVectorizer's
16 // LoopAccessAnalysis.  Because this analysis presumes no change in the order of
17 // memory operations, special care is taken to preserve the lexical order of
18 // these operations.
19 //
20 // Similarly to the Vectorizer, the pass also supports loop versioning to
21 // run-time disambiguate potentially overlapping arrays.
22 //
23 //===----------------------------------------------------------------------===//
24 
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/ADT/EquivalenceClasses.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Analysis/LoopAccessAnalysis.h"
30 #include "llvm/Analysis/LoopInfo.h"
31 #include "llvm/IR/Dominators.h"
32 #include "llvm/Pass.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
36 #include "llvm/Transforms/Utils/Cloning.h"
37 #include "llvm/Transforms/Utils/LoopUtils.h"
38 #include "llvm/Transforms/Utils/LoopVersioning.h"
39 #include <list>
40 
41 #define LDIST_NAME "loop-distribute"
42 #define DEBUG_TYPE LDIST_NAME
43 
44 using namespace llvm;
45 
46 static cl::opt<bool>
47     LDistVerify("loop-distribute-verify", cl::Hidden,
48                 cl::desc("Turn on DominatorTree and LoopInfo verification "
49                          "after Loop Distribution"),
50                 cl::init(false));
51 
52 static cl::opt<bool> DistributeNonIfConvertible(
53     "loop-distribute-non-if-convertible", cl::Hidden,
54     cl::desc("Whether to distribute into a loop that may not be "
55              "if-convertible by the loop vectorizer"),
56     cl::init(false));
57 
58 static cl::opt<unsigned> DistributeSCEVCheckThreshold(
59     "loop-distribute-scev-check-threshold", cl::init(8), cl::Hidden,
60     cl::desc("The maximum number of SCEV checks allowed for Loop "
61              "Distribution"));
62 
63 STATISTIC(NumLoopsDistributed, "Number of loops distributed");
64 
65 namespace {
66 /// \brief Maintains the set of instructions of the loop for a partition before
67 /// cloning.  After cloning, it hosts the new loop.
68 class InstPartition {
69   typedef SmallPtrSet<Instruction *, 8> InstructionSet;
70 
71 public:
72   InstPartition(Instruction *I, Loop *L, bool DepCycle = false)
73       : DepCycle(DepCycle), OrigLoop(L), ClonedLoop(nullptr) {
74     Set.insert(I);
75   }
76 
77   /// \brief Returns whether this partition contains a dependence cycle.
78   bool hasDepCycle() const { return DepCycle; }
79 
80   /// \brief Adds an instruction to this partition.
81   void add(Instruction *I) { Set.insert(I); }
82 
83   /// \brief Collection accessors.
84   InstructionSet::iterator begin() { return Set.begin(); }
85   InstructionSet::iterator end() { return Set.end(); }
86   InstructionSet::const_iterator begin() const { return Set.begin(); }
87   InstructionSet::const_iterator end() const { return Set.end(); }
88   bool empty() const { return Set.empty(); }
89 
90   /// \brief Moves this partition into \p Other.  This partition becomes empty
91   /// after this.
92   void moveTo(InstPartition &Other) {
93     Other.Set.insert(Set.begin(), Set.end());
94     Set.clear();
95     Other.DepCycle |= DepCycle;
96   }
97 
98   /// \brief Populates the partition with a transitive closure of all the
99   /// instructions that the seeded instructions dependent on.
100   void populateUsedSet() {
101     // FIXME: We currently don't use control-dependence but simply include all
102     // blocks (possibly empty at the end) and let simplifycfg mostly clean this
103     // up.
104     for (auto *B : OrigLoop->getBlocks())
105       Set.insert(B->getTerminator());
106 
107     // Follow the use-def chains to form a transitive closure of all the
108     // instructions that the originally seeded instructions depend on.
109     SmallVector<Instruction *, 8> Worklist(Set.begin(), Set.end());
110     while (!Worklist.empty()) {
111       Instruction *I = Worklist.pop_back_val();
112       // Insert instructions from the loop that we depend on.
113       for (Value *V : I->operand_values()) {
114         auto *I = dyn_cast<Instruction>(V);
115         if (I && OrigLoop->contains(I->getParent()) && Set.insert(I).second)
116           Worklist.push_back(I);
117       }
118     }
119   }
120 
121   /// \brief Clones the original loop.
122   ///
123   /// Updates LoopInfo and DominatorTree using the information that block \p
124   /// LoopDomBB dominates the loop.
125   Loop *cloneLoopWithPreheader(BasicBlock *InsertBefore, BasicBlock *LoopDomBB,
126                                unsigned Index, LoopInfo *LI,
127                                DominatorTree *DT) {
128     ClonedLoop = ::cloneLoopWithPreheader(InsertBefore, LoopDomBB, OrigLoop,
129                                           VMap, Twine(".ldist") + Twine(Index),
130                                           LI, DT, ClonedLoopBlocks);
131     return ClonedLoop;
132   }
133 
134   /// \brief The cloned loop.  If this partition is mapped to the original loop,
135   /// this is null.
136   const Loop *getClonedLoop() const { return ClonedLoop; }
137 
138   /// \brief Returns the loop where this partition ends up after distribution.
139   /// If this partition is mapped to the original loop then use the block from
140   /// the loop.
141   const Loop *getDistributedLoop() const {
142     return ClonedLoop ? ClonedLoop : OrigLoop;
143   }
144 
145   /// \brief The VMap that is populated by cloning and then used in
146   /// remapinstruction to remap the cloned instructions.
147   ValueToValueMapTy &getVMap() { return VMap; }
148 
149   /// \brief Remaps the cloned instructions using VMap.
150   void remapInstructions() {
151     remapInstructionsInBlocks(ClonedLoopBlocks, VMap);
152   }
153 
154   /// \brief Based on the set of instructions selected for this partition,
155   /// removes the unnecessary ones.
156   void removeUnusedInsts() {
157     SmallVector<Instruction *, 8> Unused;
158 
159     for (auto *Block : OrigLoop->getBlocks())
160       for (auto &Inst : *Block)
161         if (!Set.count(&Inst)) {
162           Instruction *NewInst = &Inst;
163           if (!VMap.empty())
164             NewInst = cast<Instruction>(VMap[NewInst]);
165 
166           assert(!isa<BranchInst>(NewInst) &&
167                  "Branches are marked used early on");
168           Unused.push_back(NewInst);
169         }
170 
171     // Delete the instructions backwards, as it has a reduced likelihood of
172     // having to update as many def-use and use-def chains.
173     for (auto *Inst : make_range(Unused.rbegin(), Unused.rend())) {
174       if (!Inst->use_empty())
175         Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
176       Inst->eraseFromParent();
177     }
178   }
179 
180   void print() const {
181     if (DepCycle)
182       dbgs() << "  (cycle)\n";
183     for (auto *I : Set)
184       // Prefix with the block name.
185       dbgs() << "  " << I->getParent()->getName() << ":" << *I << "\n";
186   }
187 
188   void printBlocks() const {
189     for (auto *BB : getDistributedLoop()->getBlocks())
190       dbgs() << *BB;
191   }
192 
193 private:
194   /// \brief Instructions from OrigLoop selected for this partition.
195   InstructionSet Set;
196 
197   /// \brief Whether this partition contains a dependence cycle.
198   bool DepCycle;
199 
200   /// \brief The original loop.
201   Loop *OrigLoop;
202 
203   /// \brief The cloned loop.  If this partition is mapped to the original loop,
204   /// this is null.
205   Loop *ClonedLoop;
206 
207   /// \brief The blocks of ClonedLoop including the preheader.  If this
208   /// partition is mapped to the original loop, this is empty.
209   SmallVector<BasicBlock *, 8> ClonedLoopBlocks;
210 
211   /// \brief These gets populated once the set of instructions have been
212   /// finalized. If this partition is mapped to the original loop, these are not
213   /// set.
214   ValueToValueMapTy VMap;
215 };
216 
217 /// \brief Holds the set of Partitions.  It populates them, merges them and then
218 /// clones the loops.
219 class InstPartitionContainer {
220   typedef DenseMap<Instruction *, int> InstToPartitionIdT;
221 
222 public:
223   InstPartitionContainer(Loop *L, LoopInfo *LI, DominatorTree *DT)
224       : L(L), LI(LI), DT(DT) {}
225 
226   /// \brief Returns the number of partitions.
227   unsigned getSize() const { return PartitionContainer.size(); }
228 
229   /// \brief Adds \p Inst into the current partition if that is marked to
230   /// contain cycles.  Otherwise start a new partition for it.
231   void addToCyclicPartition(Instruction *Inst) {
232     // If the current partition is non-cyclic.  Start a new one.
233     if (PartitionContainer.empty() || !PartitionContainer.back().hasDepCycle())
234       PartitionContainer.emplace_back(Inst, L, /*DepCycle=*/true);
235     else
236       PartitionContainer.back().add(Inst);
237   }
238 
239   /// \brief Adds \p Inst into a partition that is not marked to contain
240   /// dependence cycles.
241   ///
242   //  Initially we isolate memory instructions into as many partitions as
243   //  possible, then later we may merge them back together.
244   void addToNewNonCyclicPartition(Instruction *Inst) {
245     PartitionContainer.emplace_back(Inst, L);
246   }
247 
248   /// \brief Merges adjacent non-cyclic partitions.
249   ///
250   /// The idea is that we currently only want to isolate the non-vectorizable
251   /// partition.  We could later allow more distribution among these partition
252   /// too.
253   void mergeAdjacentNonCyclic() {
254     mergeAdjacentPartitionsIf(
255         [](const InstPartition *P) { return !P->hasDepCycle(); });
256   }
257 
258   /// \brief If a partition contains only conditional stores, we won't vectorize
259   /// it.  Try to merge it with a previous cyclic partition.
260   void mergeNonIfConvertible() {
261     mergeAdjacentPartitionsIf([&](const InstPartition *Partition) {
262       if (Partition->hasDepCycle())
263         return true;
264 
265       // Now, check if all stores are conditional in this partition.
266       bool seenStore = false;
267 
268       for (auto *Inst : *Partition)
269         if (isa<StoreInst>(Inst)) {
270           seenStore = true;
271           if (!LoopAccessInfo::blockNeedsPredication(Inst->getParent(), L, DT))
272             return false;
273         }
274       return seenStore;
275     });
276   }
277 
278   /// \brief Merges the partitions according to various heuristics.
279   void mergeBeforePopulating() {
280     mergeAdjacentNonCyclic();
281     if (!DistributeNonIfConvertible)
282       mergeNonIfConvertible();
283   }
284 
285   /// \brief Merges partitions in order to ensure that no loads are duplicated.
286   ///
287   /// We can't duplicate loads because that could potentially reorder them.
288   /// LoopAccessAnalysis provides dependency information with the context that
289   /// the order of memory operation is preserved.
290   ///
291   /// Return if any partitions were merged.
292   bool mergeToAvoidDuplicatedLoads() {
293     typedef DenseMap<Instruction *, InstPartition *> LoadToPartitionT;
294     typedef EquivalenceClasses<InstPartition *> ToBeMergedT;
295 
296     LoadToPartitionT LoadToPartition;
297     ToBeMergedT ToBeMerged;
298 
299     // Step through the partitions and create equivalence between partitions
300     // that contain the same load.  Also put partitions in between them in the
301     // same equivalence class to avoid reordering of memory operations.
302     for (PartitionContainerT::iterator I = PartitionContainer.begin(),
303                                        E = PartitionContainer.end();
304          I != E; ++I) {
305       auto *PartI = &*I;
306 
307       // If a load occurs in two partitions PartI and PartJ, merge all
308       // partitions (PartI, PartJ] into PartI.
309       for (Instruction *Inst : *PartI)
310         if (isa<LoadInst>(Inst)) {
311           bool NewElt;
312           LoadToPartitionT::iterator LoadToPart;
313 
314           std::tie(LoadToPart, NewElt) =
315               LoadToPartition.insert(std::make_pair(Inst, PartI));
316           if (!NewElt) {
317             DEBUG(dbgs() << "Merging partitions due to this load in multiple "
318                          << "partitions: " << PartI << ", "
319                          << LoadToPart->second << "\n" << *Inst << "\n");
320 
321             auto PartJ = I;
322             do {
323               --PartJ;
324               ToBeMerged.unionSets(PartI, &*PartJ);
325             } while (&*PartJ != LoadToPart->second);
326           }
327         }
328     }
329     if (ToBeMerged.empty())
330       return false;
331 
332     // Merge the member of an equivalence class into its class leader.  This
333     // makes the members empty.
334     for (ToBeMergedT::iterator I = ToBeMerged.begin(), E = ToBeMerged.end();
335          I != E; ++I) {
336       if (!I->isLeader())
337         continue;
338 
339       auto PartI = I->getData();
340       for (auto PartJ : make_range(std::next(ToBeMerged.member_begin(I)),
341                                    ToBeMerged.member_end())) {
342         PartJ->moveTo(*PartI);
343       }
344     }
345 
346     // Remove the empty partitions.
347     PartitionContainer.remove_if(
348         [](const InstPartition &P) { return P.empty(); });
349 
350     return true;
351   }
352 
353   /// \brief Sets up the mapping between instructions to partitions.  If the
354   /// instruction is duplicated across multiple partitions, set the entry to -1.
355   void setupPartitionIdOnInstructions() {
356     int PartitionID = 0;
357     for (const auto &Partition : PartitionContainer) {
358       for (Instruction *Inst : Partition) {
359         bool NewElt;
360         InstToPartitionIdT::iterator Iter;
361 
362         std::tie(Iter, NewElt) =
363             InstToPartitionId.insert(std::make_pair(Inst, PartitionID));
364         if (!NewElt)
365           Iter->second = -1;
366       }
367       ++PartitionID;
368     }
369   }
370 
371   /// \brief Populates the partition with everything that the seeding
372   /// instructions require.
373   void populateUsedSet() {
374     for (auto &P : PartitionContainer)
375       P.populateUsedSet();
376   }
377 
378   /// \brief This performs the main chunk of the work of cloning the loops for
379   /// the partitions.
380   void cloneLoops() {
381     BasicBlock *OrigPH = L->getLoopPreheader();
382     // At this point the predecessor of the preheader is either the memcheck
383     // block or the top part of the original preheader.
384     BasicBlock *Pred = OrigPH->getSinglePredecessor();
385     assert(Pred && "Preheader does not have a single predecessor");
386     BasicBlock *ExitBlock = L->getExitBlock();
387     assert(ExitBlock && "No single exit block");
388     Loop *NewLoop;
389 
390     assert(!PartitionContainer.empty() && "at least two partitions expected");
391     // We're cloning the preheader along with the loop so we already made sure
392     // it was empty.
393     assert(&*OrigPH->begin() == OrigPH->getTerminator() &&
394            "preheader not empty");
395 
396     // Create a loop for each partition except the last.  Clone the original
397     // loop before PH along with adding a preheader for the cloned loop.  Then
398     // update PH to point to the newly added preheader.
399     BasicBlock *TopPH = OrigPH;
400     unsigned Index = getSize() - 1;
401     for (auto I = std::next(PartitionContainer.rbegin()),
402               E = PartitionContainer.rend();
403          I != E; ++I, --Index, TopPH = NewLoop->getLoopPreheader()) {
404       auto *Part = &*I;
405 
406       NewLoop = Part->cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT);
407 
408       Part->getVMap()[ExitBlock] = TopPH;
409       Part->remapInstructions();
410     }
411     Pred->getTerminator()->replaceUsesOfWith(OrigPH, TopPH);
412 
413     // Now go in forward order and update the immediate dominator for the
414     // preheaders with the exiting block of the previous loop.  Dominance
415     // within the loop is updated in cloneLoopWithPreheader.
416     for (auto Curr = PartitionContainer.cbegin(),
417               Next = std::next(PartitionContainer.cbegin()),
418               E = PartitionContainer.cend();
419          Next != E; ++Curr, ++Next)
420       DT->changeImmediateDominator(
421           Next->getDistributedLoop()->getLoopPreheader(),
422           Curr->getDistributedLoop()->getExitingBlock());
423   }
424 
425   /// \brief Removes the dead instructions from the cloned loops.
426   void removeUnusedInsts() {
427     for (auto &Partition : PartitionContainer)
428       Partition.removeUnusedInsts();
429   }
430 
431   /// \brief For each memory pointer, it computes the partitionId the pointer is
432   /// used in.
433   ///
434   /// This returns an array of int where the I-th entry corresponds to I-th
435   /// entry in LAI.getRuntimePointerCheck().  If the pointer is used in multiple
436   /// partitions its entry is set to -1.
437   SmallVector<int, 8>
438   computePartitionSetForPointers(const LoopAccessInfo &LAI) {
439     const RuntimePointerChecking *RtPtrCheck = LAI.getRuntimePointerChecking();
440 
441     unsigned N = RtPtrCheck->Pointers.size();
442     SmallVector<int, 8> PtrToPartitions(N);
443     for (unsigned I = 0; I < N; ++I) {
444       Value *Ptr = RtPtrCheck->Pointers[I].PointerValue;
445       auto Instructions =
446           LAI.getInstructionsForAccess(Ptr, RtPtrCheck->Pointers[I].IsWritePtr);
447 
448       int &Partition = PtrToPartitions[I];
449       // First set it to uninitialized.
450       Partition = -2;
451       for (Instruction *Inst : Instructions) {
452         // Note that this could be -1 if Inst is duplicated across multiple
453         // partitions.
454         int ThisPartition = this->InstToPartitionId[Inst];
455         if (Partition == -2)
456           Partition = ThisPartition;
457         // -1 means belonging to multiple partitions.
458         else if (Partition == -1)
459           break;
460         else if (Partition != (int)ThisPartition)
461           Partition = -1;
462       }
463       assert(Partition != -2 && "Pointer not belonging to any partition");
464     }
465 
466     return PtrToPartitions;
467   }
468 
469   void print(raw_ostream &OS) const {
470     unsigned Index = 0;
471     for (const auto &P : PartitionContainer) {
472       OS << "Partition " << Index++ << " (" << &P << "):\n";
473       P.print();
474     }
475   }
476 
477   void dump() const { print(dbgs()); }
478 
479 #ifndef NDEBUG
480   friend raw_ostream &operator<<(raw_ostream &OS,
481                                  const InstPartitionContainer &Partitions) {
482     Partitions.print(OS);
483     return OS;
484   }
485 #endif
486 
487   void printBlocks() const {
488     unsigned Index = 0;
489     for (const auto &P : PartitionContainer) {
490       dbgs() << "\nPartition " << Index++ << " (" << &P << "):\n";
491       P.printBlocks();
492     }
493   }
494 
495 private:
496   typedef std::list<InstPartition> PartitionContainerT;
497 
498   /// \brief List of partitions.
499   PartitionContainerT PartitionContainer;
500 
501   /// \brief Mapping from Instruction to partition Id.  If the instruction
502   /// belongs to multiple partitions the entry contains -1.
503   InstToPartitionIdT InstToPartitionId;
504 
505   Loop *L;
506   LoopInfo *LI;
507   DominatorTree *DT;
508 
509   /// \brief The control structure to merge adjacent partitions if both satisfy
510   /// the \p Predicate.
511   template <class UnaryPredicate>
512   void mergeAdjacentPartitionsIf(UnaryPredicate Predicate) {
513     InstPartition *PrevMatch = nullptr;
514     for (auto I = PartitionContainer.begin(); I != PartitionContainer.end();) {
515       auto DoesMatch = Predicate(&*I);
516       if (PrevMatch == nullptr && DoesMatch) {
517         PrevMatch = &*I;
518         ++I;
519       } else if (PrevMatch != nullptr && DoesMatch) {
520         I->moveTo(*PrevMatch);
521         I = PartitionContainer.erase(I);
522       } else {
523         PrevMatch = nullptr;
524         ++I;
525       }
526     }
527   }
528 };
529 
530 /// \brief For each memory instruction, this class maintains difference of the
531 /// number of unsafe dependences that start out from this instruction minus
532 /// those that end here.
533 ///
534 /// By traversing the memory instructions in program order and accumulating this
535 /// number, we know whether any unsafe dependence crosses over a program point.
536 class MemoryInstructionDependences {
537   typedef MemoryDepChecker::Dependence Dependence;
538 
539 public:
540   struct Entry {
541     Instruction *Inst;
542     unsigned NumUnsafeDependencesStartOrEnd;
543 
544     Entry(Instruction *Inst) : Inst(Inst), NumUnsafeDependencesStartOrEnd(0) {}
545   };
546 
547   typedef SmallVector<Entry, 8> AccessesType;
548 
549   AccessesType::const_iterator begin() const { return Accesses.begin(); }
550   AccessesType::const_iterator end() const { return Accesses.end(); }
551 
552   MemoryInstructionDependences(
553       const SmallVectorImpl<Instruction *> &Instructions,
554       const SmallVectorImpl<Dependence> &Dependences) {
555     Accesses.append(Instructions.begin(), Instructions.end());
556 
557     DEBUG(dbgs() << "Backward dependences:\n");
558     for (auto &Dep : Dependences)
559       if (Dep.isPossiblyBackward()) {
560         // Note that the designations source and destination follow the program
561         // order, i.e. source is always first.  (The direction is given by the
562         // DepType.)
563         ++Accesses[Dep.Source].NumUnsafeDependencesStartOrEnd;
564         --Accesses[Dep.Destination].NumUnsafeDependencesStartOrEnd;
565 
566         DEBUG(Dep.print(dbgs(), 2, Instructions));
567       }
568   }
569 
570 private:
571   AccessesType Accesses;
572 };
573 
574 /// \brief The actual class performing the per-loop work.
575 class LoopDistributeForLoop {
576 public:
577   LoopDistributeForLoop(Loop *L, LoopInfo *LI, const LoopAccessInfo &LAI,
578                         DominatorTree *DT, ScalarEvolution *SE)
579       : L(L), LI(LI), LAI(LAI), DT(DT), SE(SE) {}
580 
581   /// \brief Try to distribute an inner-most loop.
582   bool processLoop() {
583     assert(L->empty() && "Only process inner loops.");
584 
585     DEBUG(dbgs() << "\nLDist: In \"" << L->getHeader()->getParent()->getName()
586                  << "\" checking " << *L << "\n");
587 
588     BasicBlock *PH = L->getLoopPreheader();
589     if (!PH) {
590       DEBUG(dbgs() << "Skipping; no preheader");
591       return false;
592     }
593     if (!L->getExitBlock()) {
594       DEBUG(dbgs() << "Skipping; multiple exit blocks");
595       return false;
596     }
597     // LAA will check that we only have a single exiting block.
598 
599     // Currently, we only distribute to isolate the part of the loop with
600     // dependence cycles to enable partial vectorization.
601     if (LAI.canVectorizeMemory()) {
602       DEBUG(dbgs() << "Skipping; memory operations are safe for vectorization");
603       return false;
604     }
605     auto *Dependences = LAI.getDepChecker().getDependences();
606     if (!Dependences || Dependences->empty()) {
607       DEBUG(dbgs() << "Skipping; No unsafe dependences to isolate");
608       return false;
609     }
610 
611     InstPartitionContainer Partitions(L, LI, DT);
612 
613     // First, go through each memory operation and assign them to consecutive
614     // partitions (the order of partitions follows program order).  Put those
615     // with unsafe dependences into "cyclic" partition otherwise put each store
616     // in its own "non-cyclic" partition (we'll merge these later).
617     //
618     // Note that a memory operation (e.g. Load2 below) at a program point that
619     // has an unsafe dependence (Store3->Load1) spanning over it must be
620     // included in the same cyclic partition as the dependent operations.  This
621     // is to preserve the original program order after distribution.  E.g.:
622     //
623     //                NumUnsafeDependencesStartOrEnd  NumUnsafeDependencesActive
624     //  Load1   -.                     1                       0->1
625     //  Load2    | /Unsafe/            0                       1
626     //  Store3  -'                    -1                       1->0
627     //  Load4                          0                       0
628     //
629     // NumUnsafeDependencesActive > 0 indicates this situation and in this case
630     // we just keep assigning to the same cyclic partition until
631     // NumUnsafeDependencesActive reaches 0.
632     const MemoryDepChecker &DepChecker = LAI.getDepChecker();
633     MemoryInstructionDependences MID(DepChecker.getMemoryInstructions(),
634                                      *Dependences);
635 
636     int NumUnsafeDependencesActive = 0;
637     for (auto &InstDep : MID) {
638       Instruction *I = InstDep.Inst;
639       // We update NumUnsafeDependencesActive post-instruction, catch the
640       // start of a dependence directly via NumUnsafeDependencesStartOrEnd.
641       if (NumUnsafeDependencesActive ||
642           InstDep.NumUnsafeDependencesStartOrEnd > 0)
643         Partitions.addToCyclicPartition(I);
644       else
645         Partitions.addToNewNonCyclicPartition(I);
646       NumUnsafeDependencesActive += InstDep.NumUnsafeDependencesStartOrEnd;
647       assert(NumUnsafeDependencesActive >= 0 &&
648              "Negative number of dependences active");
649     }
650 
651     // Add partitions for values used outside.  These partitions can be out of
652     // order from the original program order.  This is OK because if the
653     // partition uses a load we will merge this partition with the original
654     // partition of the load that we set up in the previous loop (see
655     // mergeToAvoidDuplicatedLoads).
656     auto DefsUsedOutside = findDefsUsedOutsideOfLoop(L);
657     for (auto *Inst : DefsUsedOutside)
658       Partitions.addToNewNonCyclicPartition(Inst);
659 
660     DEBUG(dbgs() << "Seeded partitions:\n" << Partitions);
661     if (Partitions.getSize() < 2)
662       return false;
663 
664     // Run the merge heuristics: Merge non-cyclic adjacent partitions since we
665     // should be able to vectorize these together.
666     Partitions.mergeBeforePopulating();
667     DEBUG(dbgs() << "\nMerged partitions:\n" << Partitions);
668     if (Partitions.getSize() < 2)
669       return false;
670 
671     // Now, populate the partitions with non-memory operations.
672     Partitions.populateUsedSet();
673     DEBUG(dbgs() << "\nPopulated partitions:\n" << Partitions);
674 
675     // In order to preserve original lexical order for loads, keep them in the
676     // partition that we set up in the MemoryInstructionDependences loop.
677     if (Partitions.mergeToAvoidDuplicatedLoads()) {
678       DEBUG(dbgs() << "\nPartitions merged to ensure unique loads:\n"
679                    << Partitions);
680       if (Partitions.getSize() < 2)
681         return false;
682     }
683 
684     // Don't distribute the loop if we need too many SCEV run-time checks.
685     const SCEVUnionPredicate &Pred = LAI.PSE.getUnionPredicate();
686     if (Pred.getComplexity() > DistributeSCEVCheckThreshold) {
687       DEBUG(dbgs() << "Too many SCEV run-time checks needed.\n");
688       return false;
689     }
690 
691     DEBUG(dbgs() << "\nDistributing loop: " << *L << "\n");
692     // We're done forming the partitions set up the reverse mapping from
693     // instructions to partitions.
694     Partitions.setupPartitionIdOnInstructions();
695 
696     // To keep things simple have an empty preheader before we version or clone
697     // the loop.  (Also split if this has no predecessor, i.e. entry, because we
698     // rely on PH having a predecessor.)
699     if (!PH->getSinglePredecessor() || &*PH->begin() != PH->getTerminator())
700       SplitBlock(PH, PH->getTerminator(), DT, LI);
701 
702     // If we need run-time checks, version the loop now.
703     auto PtrToPartition = Partitions.computePartitionSetForPointers(LAI);
704     const auto *RtPtrChecking = LAI.getRuntimePointerChecking();
705     const auto &AllChecks = RtPtrChecking->getChecks();
706     auto Checks = includeOnlyCrossPartitionChecks(AllChecks, PtrToPartition,
707                                                   RtPtrChecking);
708 
709     if (!Pred.isAlwaysTrue() || !Checks.empty()) {
710       DEBUG(dbgs() << "\nPointers:\n");
711       DEBUG(LAI.getRuntimePointerChecking()->printChecks(dbgs(), Checks));
712       LoopVersioning LVer(LAI, L, LI, DT, SE, false);
713       LVer.setAliasChecks(std::move(Checks));
714       LVer.setSCEVChecks(LAI.PSE.getUnionPredicate());
715       LVer.versionLoop(DefsUsedOutside);
716       LVer.annotateLoopWithNoAlias();
717     }
718 
719     // Create identical copies of the original loop for each partition and hook
720     // them up sequentially.
721     Partitions.cloneLoops();
722 
723     // Now, we remove the instruction from each loop that don't belong to that
724     // partition.
725     Partitions.removeUnusedInsts();
726     DEBUG(dbgs() << "\nAfter removing unused Instrs:\n");
727     DEBUG(Partitions.printBlocks());
728 
729     if (LDistVerify) {
730       LI->verify();
731       DT->verifyDomTree();
732     }
733 
734     ++NumLoopsDistributed;
735     return true;
736   }
737 
738 private:
739   /// \brief Filter out checks between pointers from the same partition.
740   ///
741   /// \p PtrToPartition contains the partition number for pointers.  Partition
742   /// number -1 means that the pointer is used in multiple partitions.  In this
743   /// case we can't safely omit the check.
744   SmallVector<RuntimePointerChecking::PointerCheck, 4>
745   includeOnlyCrossPartitionChecks(
746       const SmallVectorImpl<RuntimePointerChecking::PointerCheck> &AllChecks,
747       const SmallVectorImpl<int> &PtrToPartition,
748       const RuntimePointerChecking *RtPtrChecking) {
749     SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks;
750 
751     std::copy_if(AllChecks.begin(), AllChecks.end(), std::back_inserter(Checks),
752                  [&](const RuntimePointerChecking::PointerCheck &Check) {
753                    for (unsigned PtrIdx1 : Check.first->Members)
754                      for (unsigned PtrIdx2 : Check.second->Members)
755                        // Only include this check if there is a pair of pointers
756                        // that require checking and the pointers fall into
757                        // separate partitions.
758                        //
759                        // (Note that we already know at this point that the two
760                        // pointer groups need checking but it doesn't follow
761                        // that each pair of pointers within the two groups need
762                        // checking as well.
763                        //
764                        // In other words we don't want to include a check just
765                        // because there is a pair of pointers between the two
766                        // pointer groups that require checks and a different
767                        // pair whose pointers fall into different partitions.)
768                        if (RtPtrChecking->needsChecking(PtrIdx1, PtrIdx2) &&
769                            !RuntimePointerChecking::arePointersInSamePartition(
770                                PtrToPartition, PtrIdx1, PtrIdx2))
771                          return true;
772                    return false;
773                  });
774 
775     return Checks;
776   }
777 
778   // Analyses used.
779   Loop *L;
780   LoopInfo *LI;
781   const LoopAccessInfo &LAI;
782   DominatorTree *DT;
783   ScalarEvolution *SE;
784 };
785 
786 /// \brief The pass class.
787 class LoopDistribute : public FunctionPass {
788 public:
789   LoopDistribute() : FunctionPass(ID) {
790     initializeLoopDistributePass(*PassRegistry::getPassRegistry());
791   }
792 
793   bool runOnFunction(Function &F) override {
794     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
795     auto *LAA = &getAnalysis<LoopAccessAnalysis>();
796     auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
797     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
798 
799     // Build up a worklist of inner-loops to vectorize. This is necessary as the
800     // act of distributing a loop creates new loops and can invalidate iterators
801     // across the loops.
802     SmallVector<Loop *, 8> Worklist;
803 
804     for (Loop *TopLevelLoop : *LI)
805       for (Loop *L : depth_first(TopLevelLoop))
806         // We only handle inner-most loops.
807         if (L->empty())
808           Worklist.push_back(L);
809 
810     // Now walk the identified inner loops.
811     bool Changed = false;
812     for (Loop *L : Worklist) {
813       const LoopAccessInfo &LAI = LAA->getInfo(L, ValueToValueMap());
814       LoopDistributeForLoop LDL(L, LI, LAI, DT, SE);
815       Changed |= LDL.processLoop();
816     }
817 
818     // Process each loop nest in the function.
819     return Changed;
820   }
821 
822   void getAnalysisUsage(AnalysisUsage &AU) const override {
823     AU.addRequired<ScalarEvolutionWrapperPass>();
824     AU.addRequired<LoopInfoWrapperPass>();
825     AU.addPreserved<LoopInfoWrapperPass>();
826     AU.addRequired<LoopAccessAnalysis>();
827     AU.addRequired<DominatorTreeWrapperPass>();
828     AU.addPreserved<DominatorTreeWrapperPass>();
829   }
830 
831   static char ID;
832 };
833 } // anonymous namespace
834 
835 char LoopDistribute::ID;
836 static const char ldist_name[] = "Loop Distribition";
837 
838 INITIALIZE_PASS_BEGIN(LoopDistribute, LDIST_NAME, ldist_name, false, false)
839 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
840 INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis)
841 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
842 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
843 INITIALIZE_PASS_END(LoopDistribute, LDIST_NAME, ldist_name, false, false)
844 
845 namespace llvm {
846 FunctionPass *createLoopDistributePass() { return new LoopDistribute(); }
847 }
848