blob: 3624bba10345073d03af1bcae5dd18a11f3872f7 [file] [log] [blame]
Adam Nemet938d3d62015-05-14 12:05:18 +00001//===- 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
Adam Nemetb2593f72016-07-18 16:29:27 +000025#include "llvm/Transforms/Scalar/LoopDistribute.h"
Adam Nemet938d3d62015-05-14 12:05:18 +000026#include "llvm/ADT/DepthFirstIterator.h"
27#include "llvm/ADT/EquivalenceClasses.h"
28#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/Statistic.h"
Adam Nemetaad81602016-07-15 17:23:20 +000030#include "llvm/Analysis/BlockFrequencyInfo.h"
Eli Friedman66fdba82016-09-16 18:01:48 +000031#include "llvm/Analysis/GlobalsModRef.h"
Adam Nemet938d3d62015-05-14 12:05:18 +000032#include "llvm/Analysis/LoopAccessAnalysis.h"
33#include "llvm/Analysis/LoopInfo.h"
Adam Nemetaad81602016-07-15 17:23:20 +000034#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Adam Nemet0ba164b2016-04-28 23:08:32 +000035#include "llvm/IR/DiagnosticInfo.h"
Adam Nemet938d3d62015-05-14 12:05:18 +000036#include "llvm/IR/Dominators.h"
37#include "llvm/Pass.h"
38#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000040#include "llvm/Transforms/Scalar/LoopPassManager.h"
Adam Nemet938d3d62015-05-14 12:05:18 +000041#include "llvm/Transforms/Utils/BasicBlockUtils.h"
42#include "llvm/Transforms/Utils/Cloning.h"
Ashutosh Nemac5b7b552015-08-19 05:40:42 +000043#include "llvm/Transforms/Utils/LoopUtils.h"
Adam Nemet215746b2015-07-10 18:55:13 +000044#include "llvm/Transforms/Utils/LoopVersioning.h"
Adam Nemet938d3d62015-05-14 12:05:18 +000045#include <list>
46
47#define LDIST_NAME "loop-distribute"
48#define DEBUG_TYPE LDIST_NAME
49
50using namespace llvm;
51
52static cl::opt<bool>
53 LDistVerify("loop-distribute-verify", cl::Hidden,
54 cl::desc("Turn on DominatorTree and LoopInfo verification "
55 "after Loop Distribution"),
56 cl::init(false));
57
58static cl::opt<bool> DistributeNonIfConvertible(
59 "loop-distribute-non-if-convertible", cl::Hidden,
60 cl::desc("Whether to distribute into a loop that may not be "
61 "if-convertible by the loop vectorizer"),
62 cl::init(false));
63
Silviu Baranga2910a4f2015-11-09 13:26:09 +000064static cl::opt<unsigned> DistributeSCEVCheckThreshold(
65 "loop-distribute-scev-check-threshold", cl::init(8), cl::Hidden,
66 cl::desc("The maximum number of SCEV checks allowed for Loop "
67 "Distribution"));
68
Adam Nemetd2fa4142016-04-27 05:28:18 +000069static cl::opt<unsigned> PragmaDistributeSCEVCheckThreshold(
70 "loop-distribute-scev-check-threshold-with-pragma", cl::init(128),
71 cl::Hidden,
72 cl::desc(
73 "The maximum number of SCEV checks allowed for Loop "
74 "Distribution for loop marked with #pragma loop distribute(enable)"));
75
Adam Nemetd2fa4142016-04-27 05:28:18 +000076static cl::opt<bool> EnableLoopDistribute(
77 "enable-loop-distribute", cl::Hidden,
Adam Nemet32e6a342016-12-21 04:07:40 +000078 cl::desc("Enable the new, experimental LoopDistribution Pass"),
79 cl::init(false));
Adam Nemetd2fa4142016-04-27 05:28:18 +000080
Adam Nemet938d3d62015-05-14 12:05:18 +000081STATISTIC(NumLoopsDistributed, "Number of loops distributed");
82
Adam Nemet2f85b732015-05-14 12:33:32 +000083namespace {
Adam Nemet938d3d62015-05-14 12:05:18 +000084/// \brief Maintains the set of instructions of the loop for a partition before
85/// cloning. After cloning, it hosts the new loop.
86class InstPartition {
87 typedef SmallPtrSet<Instruction *, 8> InstructionSet;
88
89public:
90 InstPartition(Instruction *I, Loop *L, bool DepCycle = false)
91 : DepCycle(DepCycle), OrigLoop(L), ClonedLoop(nullptr) {
92 Set.insert(I);
93 }
94
95 /// \brief Returns whether this partition contains a dependence cycle.
96 bool hasDepCycle() const { return DepCycle; }
97
98 /// \brief Adds an instruction to this partition.
99 void add(Instruction *I) { Set.insert(I); }
100
101 /// \brief Collection accessors.
102 InstructionSet::iterator begin() { return Set.begin(); }
103 InstructionSet::iterator end() { return Set.end(); }
104 InstructionSet::const_iterator begin() const { return Set.begin(); }
105 InstructionSet::const_iterator end() const { return Set.end(); }
106 bool empty() const { return Set.empty(); }
107
108 /// \brief Moves this partition into \p Other. This partition becomes empty
109 /// after this.
110 void moveTo(InstPartition &Other) {
111 Other.Set.insert(Set.begin(), Set.end());
112 Set.clear();
113 Other.DepCycle |= DepCycle;
114 }
115
116 /// \brief Populates the partition with a transitive closure of all the
117 /// instructions that the seeded instructions dependent on.
118 void populateUsedSet() {
119 // FIXME: We currently don't use control-dependence but simply include all
120 // blocks (possibly empty at the end) and let simplifycfg mostly clean this
121 // up.
122 for (auto *B : OrigLoop->getBlocks())
123 Set.insert(B->getTerminator());
124
125 // Follow the use-def chains to form a transitive closure of all the
126 // instructions that the originally seeded instructions depend on.
127 SmallVector<Instruction *, 8> Worklist(Set.begin(), Set.end());
128 while (!Worklist.empty()) {
129 Instruction *I = Worklist.pop_back_val();
130 // Insert instructions from the loop that we depend on.
131 for (Value *V : I->operand_values()) {
132 auto *I = dyn_cast<Instruction>(V);
133 if (I && OrigLoop->contains(I->getParent()) && Set.insert(I).second)
134 Worklist.push_back(I);
135 }
136 }
137 }
138
139 /// \brief Clones the original loop.
140 ///
141 /// Updates LoopInfo and DominatorTree using the information that block \p
142 /// LoopDomBB dominates the loop.
143 Loop *cloneLoopWithPreheader(BasicBlock *InsertBefore, BasicBlock *LoopDomBB,
144 unsigned Index, LoopInfo *LI,
145 DominatorTree *DT) {
146 ClonedLoop = ::cloneLoopWithPreheader(InsertBefore, LoopDomBB, OrigLoop,
147 VMap, Twine(".ldist") + Twine(Index),
148 LI, DT, ClonedLoopBlocks);
149 return ClonedLoop;
150 }
151
152 /// \brief The cloned loop. If this partition is mapped to the original loop,
153 /// this is null.
154 const Loop *getClonedLoop() const { return ClonedLoop; }
155
156 /// \brief Returns the loop where this partition ends up after distribution.
157 /// If this partition is mapped to the original loop then use the block from
158 /// the loop.
159 const Loop *getDistributedLoop() const {
160 return ClonedLoop ? ClonedLoop : OrigLoop;
161 }
162
163 /// \brief The VMap that is populated by cloning and then used in
164 /// remapinstruction to remap the cloned instructions.
165 ValueToValueMapTy &getVMap() { return VMap; }
166
167 /// \brief Remaps the cloned instructions using VMap.
Adam Nemet1a689182015-07-10 18:55:09 +0000168 void remapInstructions() {
169 remapInstructionsInBlocks(ClonedLoopBlocks, VMap);
170 }
Adam Nemet938d3d62015-05-14 12:05:18 +0000171
172 /// \brief Based on the set of instructions selected for this partition,
173 /// removes the unnecessary ones.
174 void removeUnusedInsts() {
175 SmallVector<Instruction *, 8> Unused;
176
177 for (auto *Block : OrigLoop->getBlocks())
178 for (auto &Inst : *Block)
179 if (!Set.count(&Inst)) {
180 Instruction *NewInst = &Inst;
181 if (!VMap.empty())
182 NewInst = cast<Instruction>(VMap[NewInst]);
183
184 assert(!isa<BranchInst>(NewInst) &&
185 "Branches are marked used early on");
186 Unused.push_back(NewInst);
187 }
188
189 // Delete the instructions backwards, as it has a reduced likelihood of
190 // having to update as many def-use and use-def chains.
David Majnemerd7708772016-06-24 04:05:21 +0000191 for (auto *Inst : reverse(Unused)) {
Adam Nemet938d3d62015-05-14 12:05:18 +0000192 if (!Inst->use_empty())
193 Inst->replaceAllUsesWith(UndefValue::get(Inst->getType()));
194 Inst->eraseFromParent();
195 }
196 }
197
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000198 void print() const {
Adam Nemet938d3d62015-05-14 12:05:18 +0000199 if (DepCycle)
200 dbgs() << " (cycle)\n";
201 for (auto *I : Set)
202 // Prefix with the block name.
203 dbgs() << " " << I->getParent()->getName() << ":" << *I << "\n";
204 }
205
206 void printBlocks() const {
207 for (auto *BB : getDistributedLoop()->getBlocks())
208 dbgs() << *BB;
209 }
210
211private:
212 /// \brief Instructions from OrigLoop selected for this partition.
213 InstructionSet Set;
214
215 /// \brief Whether this partition contains a dependence cycle.
216 bool DepCycle;
217
218 /// \brief The original loop.
219 Loop *OrigLoop;
220
221 /// \brief The cloned loop. If this partition is mapped to the original loop,
222 /// this is null.
223 Loop *ClonedLoop;
224
225 /// \brief The blocks of ClonedLoop including the preheader. If this
226 /// partition is mapped to the original loop, this is empty.
227 SmallVector<BasicBlock *, 8> ClonedLoopBlocks;
228
229 /// \brief These gets populated once the set of instructions have been
230 /// finalized. If this partition is mapped to the original loop, these are not
231 /// set.
232 ValueToValueMapTy VMap;
233};
234
235/// \brief Holds the set of Partitions. It populates them, merges them and then
236/// clones the loops.
237class InstPartitionContainer {
238 typedef DenseMap<Instruction *, int> InstToPartitionIdT;
239
240public:
241 InstPartitionContainer(Loop *L, LoopInfo *LI, DominatorTree *DT)
242 : L(L), LI(LI), DT(DT) {}
243
244 /// \brief Returns the number of partitions.
245 unsigned getSize() const { return PartitionContainer.size(); }
246
247 /// \brief Adds \p Inst into the current partition if that is marked to
248 /// contain cycles. Otherwise start a new partition for it.
249 void addToCyclicPartition(Instruction *Inst) {
250 // If the current partition is non-cyclic. Start a new one.
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000251 if (PartitionContainer.empty() || !PartitionContainer.back().hasDepCycle())
252 PartitionContainer.emplace_back(Inst, L, /*DepCycle=*/true);
Adam Nemet938d3d62015-05-14 12:05:18 +0000253 else
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000254 PartitionContainer.back().add(Inst);
Adam Nemet938d3d62015-05-14 12:05:18 +0000255 }
256
257 /// \brief Adds \p Inst into a partition that is not marked to contain
258 /// dependence cycles.
259 ///
260 // Initially we isolate memory instructions into as many partitions as
261 // possible, then later we may merge them back together.
262 void addToNewNonCyclicPartition(Instruction *Inst) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000263 PartitionContainer.emplace_back(Inst, L);
Adam Nemet938d3d62015-05-14 12:05:18 +0000264 }
265
266 /// \brief Merges adjacent non-cyclic partitions.
267 ///
268 /// The idea is that we currently only want to isolate the non-vectorizable
269 /// partition. We could later allow more distribution among these partition
270 /// too.
271 void mergeAdjacentNonCyclic() {
272 mergeAdjacentPartitionsIf(
273 [](const InstPartition *P) { return !P->hasDepCycle(); });
274 }
275
276 /// \brief If a partition contains only conditional stores, we won't vectorize
277 /// it. Try to merge it with a previous cyclic partition.
278 void mergeNonIfConvertible() {
279 mergeAdjacentPartitionsIf([&](const InstPartition *Partition) {
280 if (Partition->hasDepCycle())
281 return true;
282
283 // Now, check if all stores are conditional in this partition.
284 bool seenStore = false;
285
286 for (auto *Inst : *Partition)
287 if (isa<StoreInst>(Inst)) {
288 seenStore = true;
289 if (!LoopAccessInfo::blockNeedsPredication(Inst->getParent(), L, DT))
290 return false;
291 }
292 return seenStore;
293 });
294 }
295
296 /// \brief Merges the partitions according to various heuristics.
297 void mergeBeforePopulating() {
298 mergeAdjacentNonCyclic();
299 if (!DistributeNonIfConvertible)
300 mergeNonIfConvertible();
301 }
302
303 /// \brief Merges partitions in order to ensure that no loads are duplicated.
304 ///
305 /// We can't duplicate loads because that could potentially reorder them.
306 /// LoopAccessAnalysis provides dependency information with the context that
307 /// the order of memory operation is preserved.
308 ///
309 /// Return if any partitions were merged.
310 bool mergeToAvoidDuplicatedLoads() {
311 typedef DenseMap<Instruction *, InstPartition *> LoadToPartitionT;
312 typedef EquivalenceClasses<InstPartition *> ToBeMergedT;
313
314 LoadToPartitionT LoadToPartition;
315 ToBeMergedT ToBeMerged;
316
317 // Step through the partitions and create equivalence between partitions
318 // that contain the same load. Also put partitions in between them in the
319 // same equivalence class to avoid reordering of memory operations.
320 for (PartitionContainerT::iterator I = PartitionContainer.begin(),
321 E = PartitionContainer.end();
322 I != E; ++I) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000323 auto *PartI = &*I;
Adam Nemet938d3d62015-05-14 12:05:18 +0000324
325 // If a load occurs in two partitions PartI and PartJ, merge all
326 // partitions (PartI, PartJ] into PartI.
327 for (Instruction *Inst : *PartI)
328 if (isa<LoadInst>(Inst)) {
329 bool NewElt;
330 LoadToPartitionT::iterator LoadToPart;
331
332 std::tie(LoadToPart, NewElt) =
333 LoadToPartition.insert(std::make_pair(Inst, PartI));
334 if (!NewElt) {
335 DEBUG(dbgs() << "Merging partitions due to this load in multiple "
336 << "partitions: " << PartI << ", "
337 << LoadToPart->second << "\n" << *Inst << "\n");
338
339 auto PartJ = I;
340 do {
341 --PartJ;
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000342 ToBeMerged.unionSets(PartI, &*PartJ);
343 } while (&*PartJ != LoadToPart->second);
Adam Nemet938d3d62015-05-14 12:05:18 +0000344 }
345 }
346 }
347 if (ToBeMerged.empty())
348 return false;
349
350 // Merge the member of an equivalence class into its class leader. This
351 // makes the members empty.
352 for (ToBeMergedT::iterator I = ToBeMerged.begin(), E = ToBeMerged.end();
353 I != E; ++I) {
354 if (!I->isLeader())
355 continue;
356
357 auto PartI = I->getData();
358 for (auto PartJ : make_range(std::next(ToBeMerged.member_begin(I)),
359 ToBeMerged.member_end())) {
360 PartJ->moveTo(*PartI);
361 }
362 }
363
364 // Remove the empty partitions.
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000365 PartitionContainer.remove_if(
366 [](const InstPartition &P) { return P.empty(); });
Adam Nemet938d3d62015-05-14 12:05:18 +0000367
368 return true;
369 }
370
371 /// \brief Sets up the mapping between instructions to partitions. If the
372 /// instruction is duplicated across multiple partitions, set the entry to -1.
373 void setupPartitionIdOnInstructions() {
374 int PartitionID = 0;
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000375 for (const auto &Partition : PartitionContainer) {
376 for (Instruction *Inst : Partition) {
Adam Nemet938d3d62015-05-14 12:05:18 +0000377 bool NewElt;
378 InstToPartitionIdT::iterator Iter;
379
380 std::tie(Iter, NewElt) =
381 InstToPartitionId.insert(std::make_pair(Inst, PartitionID));
382 if (!NewElt)
383 Iter->second = -1;
384 }
385 ++PartitionID;
386 }
387 }
388
389 /// \brief Populates the partition with everything that the seeding
390 /// instructions require.
391 void populateUsedSet() {
392 for (auto &P : PartitionContainer)
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000393 P.populateUsedSet();
Adam Nemet938d3d62015-05-14 12:05:18 +0000394 }
395
396 /// \brief This performs the main chunk of the work of cloning the loops for
397 /// the partitions.
Justin Bogner843fb202015-12-15 19:40:57 +0000398 void cloneLoops() {
Adam Nemet938d3d62015-05-14 12:05:18 +0000399 BasicBlock *OrigPH = L->getLoopPreheader();
400 // At this point the predecessor of the preheader is either the memcheck
401 // block or the top part of the original preheader.
402 BasicBlock *Pred = OrigPH->getSinglePredecessor();
403 assert(Pred && "Preheader does not have a single predecessor");
404 BasicBlock *ExitBlock = L->getExitBlock();
405 assert(ExitBlock && "No single exit block");
406 Loop *NewLoop;
407
408 assert(!PartitionContainer.empty() && "at least two partitions expected");
409 // We're cloning the preheader along with the loop so we already made sure
410 // it was empty.
411 assert(&*OrigPH->begin() == OrigPH->getTerminator() &&
412 "preheader not empty");
413
414 // Create a loop for each partition except the last. Clone the original
415 // loop before PH along with adding a preheader for the cloned loop. Then
416 // update PH to point to the newly added preheader.
417 BasicBlock *TopPH = OrigPH;
418 unsigned Index = getSize() - 1;
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000419 for (auto I = std::next(PartitionContainer.rbegin()),
420 E = PartitionContainer.rend();
Adam Nemet938d3d62015-05-14 12:05:18 +0000421 I != E; ++I, --Index, TopPH = NewLoop->getLoopPreheader()) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000422 auto *Part = &*I;
Adam Nemet938d3d62015-05-14 12:05:18 +0000423
424 NewLoop = Part->cloneLoopWithPreheader(TopPH, Pred, Index, LI, DT);
425
426 Part->getVMap()[ExitBlock] = TopPH;
427 Part->remapInstructions();
428 }
429 Pred->getTerminator()->replaceUsesOfWith(OrigPH, TopPH);
430
431 // Now go in forward order and update the immediate dominator for the
432 // preheaders with the exiting block of the previous loop. Dominance
433 // within the loop is updated in cloneLoopWithPreheader.
434 for (auto Curr = PartitionContainer.cbegin(),
435 Next = std::next(PartitionContainer.cbegin()),
436 E = PartitionContainer.cend();
437 Next != E; ++Curr, ++Next)
438 DT->changeImmediateDominator(
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000439 Next->getDistributedLoop()->getLoopPreheader(),
440 Curr->getDistributedLoop()->getExitingBlock());
Adam Nemet938d3d62015-05-14 12:05:18 +0000441 }
442
443 /// \brief Removes the dead instructions from the cloned loops.
444 void removeUnusedInsts() {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000445 for (auto &Partition : PartitionContainer)
446 Partition.removeUnusedInsts();
Adam Nemet938d3d62015-05-14 12:05:18 +0000447 }
448
449 /// \brief For each memory pointer, it computes the partitionId the pointer is
450 /// used in.
451 ///
452 /// This returns an array of int where the I-th entry corresponds to I-th
453 /// entry in LAI.getRuntimePointerCheck(). If the pointer is used in multiple
454 /// partitions its entry is set to -1.
455 SmallVector<int, 8>
456 computePartitionSetForPointers(const LoopAccessInfo &LAI) {
Adam Nemet7cdebac2015-07-14 22:32:44 +0000457 const RuntimePointerChecking *RtPtrCheck = LAI.getRuntimePointerChecking();
Adam Nemet938d3d62015-05-14 12:05:18 +0000458
459 unsigned N = RtPtrCheck->Pointers.size();
460 SmallVector<int, 8> PtrToPartitions(N);
461 for (unsigned I = 0; I < N; ++I) {
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000462 Value *Ptr = RtPtrCheck->Pointers[I].PointerValue;
Adam Nemet938d3d62015-05-14 12:05:18 +0000463 auto Instructions =
Adam Nemet9f7dedc2015-07-14 22:32:50 +0000464 LAI.getInstructionsForAccess(Ptr, RtPtrCheck->Pointers[I].IsWritePtr);
Adam Nemet938d3d62015-05-14 12:05:18 +0000465
466 int &Partition = PtrToPartitions[I];
467 // First set it to uninitialized.
468 Partition = -2;
469 for (Instruction *Inst : Instructions) {
470 // Note that this could be -1 if Inst is duplicated across multiple
471 // partitions.
472 int ThisPartition = this->InstToPartitionId[Inst];
473 if (Partition == -2)
474 Partition = ThisPartition;
475 // -1 means belonging to multiple partitions.
476 else if (Partition == -1)
477 break;
478 else if (Partition != (int)ThisPartition)
479 Partition = -1;
480 }
481 assert(Partition != -2 && "Pointer not belonging to any partition");
482 }
483
484 return PtrToPartitions;
485 }
486
487 void print(raw_ostream &OS) const {
488 unsigned Index = 0;
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000489 for (const auto &P : PartitionContainer) {
490 OS << "Partition " << Index++ << " (" << &P << "):\n";
491 P.print();
Adam Nemet938d3d62015-05-14 12:05:18 +0000492 }
493 }
494
495 void dump() const { print(dbgs()); }
496
497#ifndef NDEBUG
498 friend raw_ostream &operator<<(raw_ostream &OS,
499 const InstPartitionContainer &Partitions) {
500 Partitions.print(OS);
501 return OS;
502 }
503#endif
504
505 void printBlocks() const {
506 unsigned Index = 0;
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000507 for (const auto &P : PartitionContainer) {
508 dbgs() << "\nPartition " << Index++ << " (" << &P << "):\n";
509 P.printBlocks();
Adam Nemet938d3d62015-05-14 12:05:18 +0000510 }
511 }
512
513private:
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000514 typedef std::list<InstPartition> PartitionContainerT;
Adam Nemet938d3d62015-05-14 12:05:18 +0000515
516 /// \brief List of partitions.
517 PartitionContainerT PartitionContainer;
518
519 /// \brief Mapping from Instruction to partition Id. If the instruction
520 /// belongs to multiple partitions the entry contains -1.
521 InstToPartitionIdT InstToPartitionId;
522
523 Loop *L;
524 LoopInfo *LI;
525 DominatorTree *DT;
526
527 /// \brief The control structure to merge adjacent partitions if both satisfy
528 /// the \p Predicate.
529 template <class UnaryPredicate>
530 void mergeAdjacentPartitionsIf(UnaryPredicate Predicate) {
531 InstPartition *PrevMatch = nullptr;
532 for (auto I = PartitionContainer.begin(); I != PartitionContainer.end();) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000533 auto DoesMatch = Predicate(&*I);
Adam Nemet938d3d62015-05-14 12:05:18 +0000534 if (PrevMatch == nullptr && DoesMatch) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000535 PrevMatch = &*I;
Adam Nemet938d3d62015-05-14 12:05:18 +0000536 ++I;
537 } else if (PrevMatch != nullptr && DoesMatch) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000538 I->moveTo(*PrevMatch);
Adam Nemet938d3d62015-05-14 12:05:18 +0000539 I = PartitionContainer.erase(I);
540 } else {
541 PrevMatch = nullptr;
542 ++I;
543 }
544 }
545 }
546};
547
548/// \brief For each memory instruction, this class maintains difference of the
549/// number of unsafe dependences that start out from this instruction minus
550/// those that end here.
551///
552/// By traversing the memory instructions in program order and accumulating this
553/// number, we know whether any unsafe dependence crosses over a program point.
554class MemoryInstructionDependences {
555 typedef MemoryDepChecker::Dependence Dependence;
556
557public:
558 struct Entry {
559 Instruction *Inst;
560 unsigned NumUnsafeDependencesStartOrEnd;
561
562 Entry(Instruction *Inst) : Inst(Inst), NumUnsafeDependencesStartOrEnd(0) {}
563 };
564
565 typedef SmallVector<Entry, 8> AccessesType;
566
567 AccessesType::const_iterator begin() const { return Accesses.begin(); }
568 AccessesType::const_iterator end() const { return Accesses.end(); }
569
570 MemoryInstructionDependences(
571 const SmallVectorImpl<Instruction *> &Instructions,
Adam Nemeta2df7502015-11-03 21:39:52 +0000572 const SmallVectorImpl<Dependence> &Dependences) {
Benjamin Kramere6987bf2015-05-21 18:32:07 +0000573 Accesses.append(Instructions.begin(), Instructions.end());
Adam Nemet938d3d62015-05-14 12:05:18 +0000574
575 DEBUG(dbgs() << "Backward dependences:\n");
Adam Nemeta2df7502015-11-03 21:39:52 +0000576 for (auto &Dep : Dependences)
Adam Nemet938d3d62015-05-14 12:05:18 +0000577 if (Dep.isPossiblyBackward()) {
578 // Note that the designations source and destination follow the program
579 // order, i.e. source is always first. (The direction is given by the
580 // DepType.)
581 ++Accesses[Dep.Source].NumUnsafeDependencesStartOrEnd;
582 --Accesses[Dep.Destination].NumUnsafeDependencesStartOrEnd;
583
584 DEBUG(Dep.print(dbgs(), 2, Instructions));
585 }
586 }
587
588private:
589 AccessesType Accesses;
590};
591
Adam Nemet61399ac2016-04-27 00:31:03 +0000592/// \brief The actual class performing the per-loop work.
593class LoopDistributeForLoop {
Adam Nemet938d3d62015-05-14 12:05:18 +0000594public:
Adam Nemeteff76642016-05-13 04:20:31 +0000595 LoopDistributeForLoop(Loop *L, Function *F, LoopInfo *LI, DominatorTree *DT,
Adam Nemetaad81602016-07-15 17:23:20 +0000596 ScalarEvolution *SE, OptimizationRemarkEmitter *ORE)
597 : L(L), F(F), LI(LI), LAI(nullptr), DT(DT), SE(SE), ORE(ORE) {
Adam Nemetd2fa4142016-04-27 05:28:18 +0000598 setForced();
599 }
Adam Nemetc75ad692015-07-30 03:29:16 +0000600
Adam Nemet938d3d62015-05-14 12:05:18 +0000601 /// \brief Try to distribute an inner-most loop.
Adam Nemetb2593f72016-07-18 16:29:27 +0000602 bool processLoop(std::function<const LoopAccessInfo &(Loop &)> &GetLAA) {
Adam Nemet938d3d62015-05-14 12:05:18 +0000603 assert(L->empty() && "Only process inner loops.");
604
605 DEBUG(dbgs() << "\nLDist: In \"" << L->getHeader()->getParent()->getName()
606 << "\" checking " << *L << "\n");
607
Adam Nemet7f38e112016-04-28 23:08:27 +0000608 if (!L->getExitBlock())
Adam Nemetf744ad72016-09-30 04:56:25 +0000609 return fail("MultipleExitBlocks", "multiple exit blocks");
Florian Hahn2e032132016-12-19 17:13:37 +0000610 if (!L->isLoopSimplifyForm())
611 return fail("NotLoopSimplifyForm",
612 "loop is not in loop-simplify form");
613
614 BasicBlock *PH = L->getLoopPreheader();
Adam Nemeteff76642016-05-13 04:20:31 +0000615
Adam Nemet938d3d62015-05-14 12:05:18 +0000616 // LAA will check that we only have a single exiting block.
Adam Nemetb2593f72016-07-18 16:29:27 +0000617 LAI = &GetLAA(*L);
Adam Nemet938d3d62015-05-14 12:05:18 +0000618
Adam Nemet938d3d62015-05-14 12:05:18 +0000619 // Currently, we only distribute to isolate the part of the loop with
620 // dependence cycles to enable partial vectorization.
Adam Nemeteff76642016-05-13 04:20:31 +0000621 if (LAI->canVectorizeMemory())
Adam Nemetf744ad72016-09-30 04:56:25 +0000622 return fail("MemOpsCanBeVectorized",
623 "memory operations are safe for vectorization");
Adam Nemet7f38e112016-04-28 23:08:27 +0000624
Adam Nemeteff76642016-05-13 04:20:31 +0000625 auto *Dependences = LAI->getDepChecker().getDependences();
Adam Nemet7f38e112016-04-28 23:08:27 +0000626 if (!Dependences || Dependences->empty())
Adam Nemetf744ad72016-09-30 04:56:25 +0000627 return fail("NoUnsafeDeps", "no unsafe dependences to isolate");
Adam Nemet938d3d62015-05-14 12:05:18 +0000628
629 InstPartitionContainer Partitions(L, LI, DT);
630
631 // First, go through each memory operation and assign them to consecutive
632 // partitions (the order of partitions follows program order). Put those
633 // with unsafe dependences into "cyclic" partition otherwise put each store
634 // in its own "non-cyclic" partition (we'll merge these later).
635 //
636 // Note that a memory operation (e.g. Load2 below) at a program point that
637 // has an unsafe dependence (Store3->Load1) spanning over it must be
638 // included in the same cyclic partition as the dependent operations. This
639 // is to preserve the original program order after distribution. E.g.:
640 //
641 // NumUnsafeDependencesStartOrEnd NumUnsafeDependencesActive
642 // Load1 -. 1 0->1
643 // Load2 | /Unsafe/ 0 1
644 // Store3 -' -1 1->0
645 // Load4 0 0
646 //
647 // NumUnsafeDependencesActive > 0 indicates this situation and in this case
648 // we just keep assigning to the same cyclic partition until
649 // NumUnsafeDependencesActive reaches 0.
Adam Nemeteff76642016-05-13 04:20:31 +0000650 const MemoryDepChecker &DepChecker = LAI->getDepChecker();
Adam Nemet938d3d62015-05-14 12:05:18 +0000651 MemoryInstructionDependences MID(DepChecker.getMemoryInstructions(),
Adam Nemeta2df7502015-11-03 21:39:52 +0000652 *Dependences);
Adam Nemet938d3d62015-05-14 12:05:18 +0000653
654 int NumUnsafeDependencesActive = 0;
655 for (auto &InstDep : MID) {
656 Instruction *I = InstDep.Inst;
657 // We update NumUnsafeDependencesActive post-instruction, catch the
658 // start of a dependence directly via NumUnsafeDependencesStartOrEnd.
659 if (NumUnsafeDependencesActive ||
660 InstDep.NumUnsafeDependencesStartOrEnd > 0)
661 Partitions.addToCyclicPartition(I);
662 else
663 Partitions.addToNewNonCyclicPartition(I);
664 NumUnsafeDependencesActive += InstDep.NumUnsafeDependencesStartOrEnd;
665 assert(NumUnsafeDependencesActive >= 0 &&
666 "Negative number of dependences active");
667 }
668
669 // Add partitions for values used outside. These partitions can be out of
670 // order from the original program order. This is OK because if the
671 // partition uses a load we will merge this partition with the original
672 // partition of the load that we set up in the previous loop (see
673 // mergeToAvoidDuplicatedLoads).
674 auto DefsUsedOutside = findDefsUsedOutsideOfLoop(L);
675 for (auto *Inst : DefsUsedOutside)
676 Partitions.addToNewNonCyclicPartition(Inst);
677
678 DEBUG(dbgs() << "Seeded partitions:\n" << Partitions);
679 if (Partitions.getSize() < 2)
Adam Nemetf744ad72016-09-30 04:56:25 +0000680 return fail("CantIsolateUnsafeDeps",
681 "cannot isolate unsafe dependencies");
Adam Nemet938d3d62015-05-14 12:05:18 +0000682
683 // Run the merge heuristics: Merge non-cyclic adjacent partitions since we
684 // should be able to vectorize these together.
685 Partitions.mergeBeforePopulating();
686 DEBUG(dbgs() << "\nMerged partitions:\n" << Partitions);
687 if (Partitions.getSize() < 2)
Adam Nemetf744ad72016-09-30 04:56:25 +0000688 return fail("CantIsolateUnsafeDeps",
689 "cannot isolate unsafe dependencies");
Adam Nemet938d3d62015-05-14 12:05:18 +0000690
691 // Now, populate the partitions with non-memory operations.
692 Partitions.populateUsedSet();
693 DEBUG(dbgs() << "\nPopulated partitions:\n" << Partitions);
694
695 // In order to preserve original lexical order for loads, keep them in the
696 // partition that we set up in the MemoryInstructionDependences loop.
697 if (Partitions.mergeToAvoidDuplicatedLoads()) {
698 DEBUG(dbgs() << "\nPartitions merged to ensure unique loads:\n"
699 << Partitions);
700 if (Partitions.getSize() < 2)
Adam Nemetf744ad72016-09-30 04:56:25 +0000701 return fail("CantIsolateUnsafeDeps",
702 "cannot isolate unsafe dependencies");
Adam Nemet938d3d62015-05-14 12:05:18 +0000703 }
704
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000705 // Don't distribute the loop if we need too many SCEV run-time checks.
Xinliang David Li94734ee2016-07-01 05:59:55 +0000706 const SCEVUnionPredicate &Pred = LAI->getPSE().getUnionPredicate();
Adam Nemetd2fa4142016-04-27 05:28:18 +0000707 if (Pred.getComplexity() > (IsForced.getValueOr(false)
708 ? PragmaDistributeSCEVCheckThreshold
Adam Nemet7f38e112016-04-28 23:08:27 +0000709 : DistributeSCEVCheckThreshold))
Adam Nemetf744ad72016-09-30 04:56:25 +0000710 return fail("TooManySCEVRuntimeChecks",
711 "too many SCEV run-time checks needed.\n");
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000712
Adam Nemet938d3d62015-05-14 12:05:18 +0000713 DEBUG(dbgs() << "\nDistributing loop: " << *L << "\n");
714 // We're done forming the partitions set up the reverse mapping from
715 // instructions to partitions.
716 Partitions.setupPartitionIdOnInstructions();
717
718 // To keep things simple have an empty preheader before we version or clone
719 // the loop. (Also split if this has no predecessor, i.e. entry, because we
720 // rely on PH having a predecessor.)
721 if (!PH->getSinglePredecessor() || &*PH->begin() != PH->getTerminator())
722 SplitBlock(PH, PH->getTerminator(), DT, LI);
723
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000724 // If we need run-time checks, version the loop now.
Adam Nemeteff76642016-05-13 04:20:31 +0000725 auto PtrToPartition = Partitions.computePartitionSetForPointers(*LAI);
726 const auto *RtPtrChecking = LAI->getRuntimePointerChecking();
Adam Nemet15840392015-08-07 22:44:15 +0000727 const auto &AllChecks = RtPtrChecking->getChecks();
Adam Nemetc75ad692015-07-30 03:29:16 +0000728 auto Checks = includeOnlyCrossPartitionChecks(AllChecks, PtrToPartition,
729 RtPtrChecking);
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000730
731 if (!Pred.isAlwaysTrue() || !Checks.empty()) {
Adam Nemet772a1502015-06-19 19:32:41 +0000732 DEBUG(dbgs() << "\nPointers:\n");
Adam Nemeteff76642016-05-13 04:20:31 +0000733 DEBUG(LAI->getRuntimePointerChecking()->printChecks(dbgs(), Checks));
734 LoopVersioning LVer(*LAI, L, LI, DT, SE, false);
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000735 LVer.setAliasChecks(std::move(Checks));
Xinliang David Li94734ee2016-07-01 05:59:55 +0000736 LVer.setSCEVChecks(LAI->getPSE().getUnionPredicate());
Adam Nemete4813402015-08-20 17:22:29 +0000737 LVer.versionLoop(DefsUsedOutside);
Adam Nemet5eccf072016-03-17 20:32:32 +0000738 LVer.annotateLoopWithNoAlias();
Adam Nemet938d3d62015-05-14 12:05:18 +0000739 }
740
741 // Create identical copies of the original loop for each partition and hook
742 // them up sequentially.
Justin Bogner843fb202015-12-15 19:40:57 +0000743 Partitions.cloneLoops();
Adam Nemet938d3d62015-05-14 12:05:18 +0000744
745 // Now, we remove the instruction from each loop that don't belong to that
746 // partition.
747 Partitions.removeUnusedInsts();
748 DEBUG(dbgs() << "\nAfter removing unused Instrs:\n");
749 DEBUG(Partitions.printBlocks());
750
751 if (LDistVerify) {
Michael Zolotukhine0b2d972016-08-31 19:26:19 +0000752 LI->verify(*DT);
Adam Nemet938d3d62015-05-14 12:05:18 +0000753 DT->verifyDomTree();
754 }
755
756 ++NumLoopsDistributed;
Adam Nemet88ec4912016-04-29 07:10:46 +0000757 // Report the success.
Adam Nemetf744ad72016-09-30 04:56:25 +0000758 ORE->emit(OptimizationRemark(LDIST_NAME, "Distribute", L->getStartLoc(),
759 L->getHeader())
760 << "distributed loop");
Adam Nemet938d3d62015-05-14 12:05:18 +0000761 return true;
762 }
763
Adam Nemet7f38e112016-04-28 23:08:27 +0000764 /// \brief Provide diagnostics then \return with false.
Adam Nemetf744ad72016-09-30 04:56:25 +0000765 bool fail(StringRef RemarkName, StringRef Message) {
Adam Nemet0ba164b2016-04-28 23:08:32 +0000766 LLVMContext &Ctx = F->getContext();
767 bool Forced = isForced().getValueOr(false);
768
Adam Nemetadeccf72016-04-28 23:08:30 +0000769 DEBUG(dbgs() << "Skipping; " << Message << "\n");
Adam Nemet0ba164b2016-04-28 23:08:32 +0000770
771 // With Rpass-missed report that distribution failed.
Adam Nemetf744ad72016-09-30 04:56:25 +0000772 ORE->emit(
773 OptimizationRemarkMissed(LDIST_NAME, "NotDistributed", L->getStartLoc(),
774 L->getHeader())
775 << "loop not distributed: use -Rpass-analysis=loop-distribute for more "
776 "info");
Adam Nemet0ba164b2016-04-28 23:08:32 +0000777
778 // With Rpass-analysis report why. This is on by default if distribution
779 // was requested explicitly.
Adam Nemetf744ad72016-09-30 04:56:25 +0000780 ORE->emit(OptimizationRemarkAnalysis(
781 Forced ? OptimizationRemarkAnalysis::AlwaysPrint : LDIST_NAME,
782 RemarkName, L->getStartLoc(), L->getHeader())
783 << "loop not distributed: " << Message);
Adam Nemet0ba164b2016-04-28 23:08:32 +0000784
785 // Also issue a warning if distribution was requested explicitly but it
786 // failed.
787 if (Forced)
788 Ctx.diagnose(DiagnosticInfoOptimizationFailure(
Adam Nemet74730d92016-07-14 22:33:46 +0000789 *F, L->getStartLoc(), "loop not distributed: failed "
Adam Nemet0ba164b2016-04-28 23:08:32 +0000790 "explicitly specified loop distribution"));
791
Adam Nemet7f38e112016-04-28 23:08:27 +0000792 return false;
793 }
794
Adam Nemetd2fa4142016-04-27 05:28:18 +0000795 /// \brief Return if distribution forced to be enabled/disabled for the loop.
796 ///
797 /// If the optional has a value, it indicates whether distribution was forced
798 /// to be enabled (true) or disabled (false). If the optional has no value
799 /// distribution was not forced either way.
800 const Optional<bool> &isForced() const { return IsForced; }
801
Adam Nemet61399ac2016-04-27 00:31:03 +0000802private:
803 /// \brief Filter out checks between pointers from the same partition.
804 ///
805 /// \p PtrToPartition contains the partition number for pointers. Partition
806 /// number -1 means that the pointer is used in multiple partitions. In this
807 /// case we can't safely omit the check.
808 SmallVector<RuntimePointerChecking::PointerCheck, 4>
809 includeOnlyCrossPartitionChecks(
810 const SmallVectorImpl<RuntimePointerChecking::PointerCheck> &AllChecks,
811 const SmallVectorImpl<int> &PtrToPartition,
812 const RuntimePointerChecking *RtPtrChecking) {
813 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks;
814
Sanjoy Das90208722017-02-21 00:38:44 +0000815 copy_if(AllChecks, std::back_inserter(Checks),
816 [&](const RuntimePointerChecking::PointerCheck &Check) {
817 for (unsigned PtrIdx1 : Check.first->Members)
818 for (unsigned PtrIdx2 : Check.second->Members)
819 // Only include this check if there is a pair of pointers
820 // that require checking and the pointers fall into
821 // separate partitions.
822 //
823 // (Note that we already know at this point that the two
824 // pointer groups need checking but it doesn't follow
825 // that each pair of pointers within the two groups need
826 // checking as well.
827 //
828 // In other words we don't want to include a check just
829 // because there is a pair of pointers between the two
830 // pointer groups that require checks and a different
831 // pair whose pointers fall into different partitions.)
832 if (RtPtrChecking->needsChecking(PtrIdx1, PtrIdx2) &&
833 !RuntimePointerChecking::arePointersInSamePartition(
834 PtrToPartition, PtrIdx1, PtrIdx2))
835 return true;
836 return false;
837 });
Adam Nemet61399ac2016-04-27 00:31:03 +0000838
839 return Checks;
840 }
841
Adam Nemetd2fa4142016-04-27 05:28:18 +0000842 /// \brief Check whether the loop metadata is forcing distribution to be
843 /// enabled/disabled.
844 void setForced() {
845 Optional<const MDOperand *> Value =
846 findStringMetadataForLoop(L, "llvm.loop.distribute.enable");
847 if (!Value)
848 return;
849
850 const MDOperand *Op = *Value;
851 assert(Op && mdconst::hasa<ConstantInt>(*Op) && "invalid metadata");
852 IsForced = mdconst::extract<ConstantInt>(*Op)->getZExtValue();
853 }
854
Adam Nemet61399ac2016-04-27 00:31:03 +0000855 Loop *L;
Adam Nemet4338d672016-04-29 07:10:39 +0000856 Function *F;
857
858 // Analyses used.
Adam Nemet938d3d62015-05-14 12:05:18 +0000859 LoopInfo *LI;
Adam Nemeteff76642016-05-13 04:20:31 +0000860 const LoopAccessInfo *LAI;
Adam Nemet938d3d62015-05-14 12:05:18 +0000861 DominatorTree *DT;
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000862 ScalarEvolution *SE;
Adam Nemetaad81602016-07-15 17:23:20 +0000863 OptimizationRemarkEmitter *ORE;
Adam Nemetd2fa4142016-04-27 05:28:18 +0000864
865 /// \brief Indicates whether distribution is forced to be enabled/disabled for
866 /// the loop.
867 ///
868 /// If the optional has a value, it indicates whether distribution was forced
869 /// to be enabled (true) or disabled (false). If the optional has no value
870 /// distribution was not forced either way.
871 Optional<bool> IsForced;
Adam Nemet938d3d62015-05-14 12:05:18 +0000872};
Adam Nemet61399ac2016-04-27 00:31:03 +0000873
Adam Nemetb2593f72016-07-18 16:29:27 +0000874/// Shared implementation between new and old PMs.
875static bool runImpl(Function &F, LoopInfo *LI, DominatorTree *DT,
876 ScalarEvolution *SE, OptimizationRemarkEmitter *ORE,
Adam Nemet32e6a342016-12-21 04:07:40 +0000877 std::function<const LoopAccessInfo &(Loop &)> &GetLAA) {
Adam Nemetb2593f72016-07-18 16:29:27 +0000878 // Build up a worklist of inner-loops to vectorize. This is necessary as the
879 // act of distributing a loop creates new loops and can invalidate iterators
880 // across the loops.
881 SmallVector<Loop *, 8> Worklist;
882
883 for (Loop *TopLevelLoop : *LI)
884 for (Loop *L : depth_first(TopLevelLoop))
885 // We only handle inner-most loops.
886 if (L->empty())
887 Worklist.push_back(L);
888
889 // Now walk the identified inner loops.
890 bool Changed = false;
891 for (Loop *L : Worklist) {
892 LoopDistributeForLoop LDL(L, &F, LI, DT, SE, ORE);
893
894 // If distribution was forced for the specific loop to be
895 // enabled/disabled, follow that. Otherwise use the global flag.
Adam Nemet32e6a342016-12-21 04:07:40 +0000896 if (LDL.isForced().getValueOr(EnableLoopDistribute))
Adam Nemetb2593f72016-07-18 16:29:27 +0000897 Changed |= LDL.processLoop(GetLAA);
898 }
899
900 // Process each loop nest in the function.
901 return Changed;
902}
903
Adam Nemet61399ac2016-04-27 00:31:03 +0000904/// \brief The pass class.
Adam Nemetb2593f72016-07-18 16:29:27 +0000905class LoopDistributeLegacy : public FunctionPass {
Adam Nemet61399ac2016-04-27 00:31:03 +0000906public:
Adam Nemet32e6a342016-12-21 04:07:40 +0000907 LoopDistributeLegacy() : FunctionPass(ID) {
Adam Nemetd2fa4142016-04-27 05:28:18 +0000908 // The default is set by the caller.
Adam Nemetb2593f72016-07-18 16:29:27 +0000909 initializeLoopDistributeLegacyPass(*PassRegistry::getPassRegistry());
Adam Nemet61399ac2016-04-27 00:31:03 +0000910 }
911
912 bool runOnFunction(Function &F) override {
Andrew Kaylor50271f72016-05-03 22:32:30 +0000913 if (skipFunction(F))
914 return false;
915
Adam Nemet61399ac2016-04-27 00:31:03 +0000916 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Xinliang David Li7853c1d2016-07-08 20:55:26 +0000917 auto *LAA = &getAnalysis<LoopAccessLegacyAnalysis>();
Adam Nemet61399ac2016-04-27 00:31:03 +0000918 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
919 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Adam Nemet79ac42a2016-07-18 16:29:21 +0000920 auto *ORE = &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
Adam Nemetb2593f72016-07-18 16:29:27 +0000921 std::function<const LoopAccessInfo &(Loop &)> GetLAA =
922 [&](Loop &L) -> const LoopAccessInfo & { return LAA->getInfo(&L); };
Adam Nemet61399ac2016-04-27 00:31:03 +0000923
Adam Nemet32e6a342016-12-21 04:07:40 +0000924 return runImpl(F, LI, DT, SE, ORE, GetLAA);
Adam Nemet61399ac2016-04-27 00:31:03 +0000925 }
926
927 void getAnalysisUsage(AnalysisUsage &AU) const override {
928 AU.addRequired<ScalarEvolutionWrapperPass>();
929 AU.addRequired<LoopInfoWrapperPass>();
930 AU.addPreserved<LoopInfoWrapperPass>();
Xinliang David Li7853c1d2016-07-08 20:55:26 +0000931 AU.addRequired<LoopAccessLegacyAnalysis>();
Adam Nemet61399ac2016-04-27 00:31:03 +0000932 AU.addRequired<DominatorTreeWrapperPass>();
933 AU.addPreserved<DominatorTreeWrapperPass>();
Adam Nemet79ac42a2016-07-18 16:29:21 +0000934 AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
Eli Friedman66fdba82016-09-16 18:01:48 +0000935 AU.addPreserved<GlobalsAAWrapperPass>();
Adam Nemet61399ac2016-04-27 00:31:03 +0000936 }
937
938 static char ID;
939};
Adam Nemet938d3d62015-05-14 12:05:18 +0000940} // anonymous namespace
941
Adam Nemetb2593f72016-07-18 16:29:27 +0000942PreservedAnalyses LoopDistributePass::run(Function &F,
943 FunctionAnalysisManager &AM) {
Adam Nemetb2593f72016-07-18 16:29:27 +0000944 auto &LI = AM.getResult<LoopAnalysis>(F);
945 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
946 auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
947 auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
948
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000949 // We don't directly need these analyses but they're required for loop
950 // analyses so provide them below.
951 auto &AA = AM.getResult<AAManager>(F);
952 auto &AC = AM.getResult<AssumptionAnalysis>(F);
953 auto &TTI = AM.getResult<TargetIRAnalysis>(F);
954 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
955
Adam Nemetb2593f72016-07-18 16:29:27 +0000956 auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager();
957 std::function<const LoopAccessInfo &(Loop &)> GetLAA =
958 [&](Loop &L) -> const LoopAccessInfo & {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000959 LoopStandardAnalysisResults AR = {AA, AC, DT, LI, SE, TLI, TTI};
960 return LAM.getResult<LoopAccessAnalysis>(L, AR);
Adam Nemetb2593f72016-07-18 16:29:27 +0000961 };
962
Adam Nemet32e6a342016-12-21 04:07:40 +0000963 bool Changed = runImpl(F, &LI, &DT, &SE, &ORE, GetLAA);
Adam Nemetb2593f72016-07-18 16:29:27 +0000964 if (!Changed)
965 return PreservedAnalyses::all();
966 PreservedAnalyses PA;
967 PA.preserve<LoopAnalysis>();
968 PA.preserve<DominatorTreeAnalysis>();
Davide Italiano11a871b2016-11-08 19:52:32 +0000969 PA.preserve<GlobalsAA>();
Adam Nemetb2593f72016-07-18 16:29:27 +0000970 return PA;
971}
972
973char LoopDistributeLegacy::ID;
Michael Zolotukhin5cda89a2016-10-05 00:44:52 +0000974static const char ldist_name[] = "Loop Distribution";
Adam Nemet938d3d62015-05-14 12:05:18 +0000975
Adam Nemetb2593f72016-07-18 16:29:27 +0000976INITIALIZE_PASS_BEGIN(LoopDistributeLegacy, LDIST_NAME, ldist_name, false,
977 false)
Adam Nemet938d3d62015-05-14 12:05:18 +0000978INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Xinliang David Li7853c1d2016-07-08 20:55:26 +0000979INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
Adam Nemet938d3d62015-05-14 12:05:18 +0000980INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000981INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Adam Nemet79ac42a2016-07-18 16:29:21 +0000982INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
Adam Nemetb2593f72016-07-18 16:29:27 +0000983INITIALIZE_PASS_END(LoopDistributeLegacy, LDIST_NAME, ldist_name, false, false)
Adam Nemet938d3d62015-05-14 12:05:18 +0000984
985namespace llvm {
Adam Nemet32e6a342016-12-21 04:07:40 +0000986FunctionPass *createLoopDistributePass() { return new LoopDistributeLegacy(); }
Adam Nemet938d3d62015-05-14 12:05:18 +0000987}