1 //===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===//
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 some loop unrolling utilities. It does not define any
11 // actual pass or policy, but provides a single function to perform loop
12 // unrolling.
13 //
14 // The process of unrolling can produce extraneous basic blocks linked with
15 // unconditional branches.  This will be corrected in the future.
16 //
17 //===----------------------------------------------------------------------===//
18 
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/LoopIterator.h"
24 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
25 #include "llvm/Analysis/ScalarEvolution.h"
26 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DebugInfoMetadata.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
36 #include "llvm/Transforms/Utils/Cloning.h"
37 #include "llvm/Transforms/Utils/LoopSimplify.h"
38 #include "llvm/Transforms/Utils/LoopUtils.h"
39 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
40 #include "llvm/Transforms/Utils/UnrollLoop.h"
41 using namespace llvm;
42 
43 #define DEBUG_TYPE "loop-unroll"
44 
45 // TODO: Should these be here or in LoopUnroll?
46 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
47 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
48 
49 static cl::opt<bool>
50 UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden,
51                     cl::desc("Allow runtime unrolled loops to be unrolled "
52                              "with epilog instead of prolog."));
53 
54 static cl::opt<bool>
55 UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden,
56                     cl::desc("Verify domtree after unrolling"),
57 #ifdef NDEBUG
58     cl::init(false)
59 #else
60     cl::init(true)
61 #endif
62                     );
63 
64 /// Convert the instruction operands from referencing the current values into
65 /// those specified by VMap.
66 void llvm::remapInstruction(Instruction *I, ValueToValueMapTy &VMap) {
67   for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) {
68     Value *Op = I->getOperand(op);
69 
70     // Unwrap arguments of dbg.value intrinsics.
71     bool Wrapped = false;
72     if (auto *V = dyn_cast<MetadataAsValue>(Op))
73       if (auto *Unwrapped = dyn_cast<ValueAsMetadata>(V->getMetadata())) {
74         Op = Unwrapped->getValue();
75         Wrapped = true;
76       }
77 
78     auto wrap = [&](Value *V) {
79       auto &C = I->getContext();
80       return Wrapped ? MetadataAsValue::get(C, ValueAsMetadata::get(V)) : V;
81     };
82 
83     ValueToValueMapTy::iterator It = VMap.find(Op);
84     if (It != VMap.end())
85       I->setOperand(op, wrap(It->second));
86   }
87 
88   if (PHINode *PN = dyn_cast<PHINode>(I)) {
89     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
90       ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i));
91       if (It != VMap.end())
92         PN->setIncomingBlock(i, cast<BasicBlock>(It->second));
93     }
94   }
95 }
96 
97 /// Folds a basic block into its predecessor if it only has one predecessor, and
98 /// that predecessor only has one successor.
99 /// The LoopInfo Analysis that is passed will be kept consistent.
100 BasicBlock *llvm::foldBlockIntoPredecessor(BasicBlock *BB, LoopInfo *LI,
101                                            ScalarEvolution *SE,
102                                            DominatorTree *DT) {
103   // Merge basic blocks into their predecessor if there is only one distinct
104   // pred, and if there is only one distinct successor of the predecessor, and
105   // if there are no PHI nodes.
106   BasicBlock *OnlyPred = BB->getSinglePredecessor();
107   if (!OnlyPred) return nullptr;
108 
109   if (OnlyPred->getTerminator()->getNumSuccessors() != 1)
110     return nullptr;
111 
112   LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
113                     << OnlyPred->getName() << "\n");
114 
115   // Resolve any PHI nodes at the start of the block.  They are all
116   // guaranteed to have exactly one entry if they exist, unless there are
117   // multiple duplicate (but guaranteed to be equal) entries for the
118   // incoming edges.  This occurs when there are multiple edges from
119   // OnlyPred to OnlySucc.
120   FoldSingleEntryPHINodes(BB);
121 
122   // Delete the unconditional branch from the predecessor...
123   OnlyPred->getInstList().pop_back();
124 
125   // Make all PHI nodes that referred to BB now refer to Pred as their
126   // source...
127   BB->replaceAllUsesWith(OnlyPred);
128 
129   // Move all definitions in the successor to the predecessor...
130   OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
131 
132   // OldName will be valid until erased.
133   StringRef OldName = BB->getName();
134 
135   // Erase the old block and update dominator info.
136   if (DT)
137     if (DomTreeNode *DTN = DT->getNode(BB)) {
138       DomTreeNode *PredDTN = DT->getNode(OnlyPred);
139       SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end());
140       for (auto *DI : Children)
141         DT->changeImmediateDominator(DI, PredDTN);
142 
143       DT->eraseNode(BB);
144     }
145 
146   LI->removeBlock(BB);
147 
148   // Inherit predecessor's name if it exists...
149   if (!OldName.empty() && !OnlyPred->hasName())
150     OnlyPred->setName(OldName);
151 
152   BB->eraseFromParent();
153 
154   return OnlyPred;
155 }
156 
157 /// Check if unrolling created a situation where we need to insert phi nodes to
158 /// preserve LCSSA form.
159 /// \param Blocks is a vector of basic blocks representing unrolled loop.
160 /// \param L is the outer loop.
161 /// It's possible that some of the blocks are in L, and some are not. In this
162 /// case, if there is a use is outside L, and definition is inside L, we need to
163 /// insert a phi-node, otherwise LCSSA will be broken.
164 /// The function is just a helper function for llvm::UnrollLoop that returns
165 /// true if this situation occurs, indicating that LCSSA needs to be fixed.
166 static bool needToInsertPhisForLCSSA(Loop *L, std::vector<BasicBlock *> Blocks,
167                                      LoopInfo *LI) {
168   for (BasicBlock *BB : Blocks) {
169     if (LI->getLoopFor(BB) == L)
170       continue;
171     for (Instruction &I : *BB) {
172       for (Use &U : I.operands()) {
173         if (auto Def = dyn_cast<Instruction>(U)) {
174           Loop *DefLoop = LI->getLoopFor(Def->getParent());
175           if (!DefLoop)
176             continue;
177           if (DefLoop->contains(L))
178             return true;
179         }
180       }
181     }
182   }
183   return false;
184 }
185 
186 /// Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary
187 /// and adds a mapping from the original loop to the new loop to NewLoops.
188 /// Returns nullptr if no new loop was created and a pointer to the
189 /// original loop OriginalBB was part of otherwise.
190 const Loop* llvm::addClonedBlockToLoopInfo(BasicBlock *OriginalBB,
191                                            BasicBlock *ClonedBB, LoopInfo *LI,
192                                            NewLoopsMap &NewLoops) {
193   // Figure out which loop New is in.
194   const Loop *OldLoop = LI->getLoopFor(OriginalBB);
195   assert(OldLoop && "Should (at least) be in the loop being unrolled!");
196 
197   Loop *&NewLoop = NewLoops[OldLoop];
198   if (!NewLoop) {
199     // Found a new sub-loop.
200     assert(OriginalBB == OldLoop->getHeader() &&
201            "Header should be first in RPO");
202 
203     NewLoop = LI->AllocateLoop();
204     Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
205 
206     if (NewLoopParent)
207       NewLoopParent->addChildLoop(NewLoop);
208     else
209       LI->addTopLevelLoop(NewLoop);
210 
211     NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
212     return OldLoop;
213   } else {
214     NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
215     return nullptr;
216   }
217 }
218 
219 /// The function chooses which type of unroll (epilog or prolog) is more
220 /// profitabale.
221 /// Epilog unroll is more profitable when there is PHI that starts from
222 /// constant.  In this case epilog will leave PHI start from constant,
223 /// but prolog will convert it to non-constant.
224 ///
225 /// loop:
226 ///   PN = PHI [I, Latch], [CI, PreHeader]
227 ///   I = foo(PN)
228 ///   ...
229 ///
230 /// Epilog unroll case.
231 /// loop:
232 ///   PN = PHI [I2, Latch], [CI, PreHeader]
233 ///   I1 = foo(PN)
234 ///   I2 = foo(I1)
235 ///   ...
236 /// Prolog unroll case.
237 ///   NewPN = PHI [PrologI, Prolog], [CI, PreHeader]
238 /// loop:
239 ///   PN = PHI [I2, Latch], [NewPN, PreHeader]
240 ///   I1 = foo(PN)
241 ///   I2 = foo(I1)
242 ///   ...
243 ///
244 static bool isEpilogProfitable(Loop *L) {
245   BasicBlock *PreHeader = L->getLoopPreheader();
246   BasicBlock *Header = L->getHeader();
247   assert(PreHeader && Header);
248   for (const PHINode &PN : Header->phis()) {
249     if (isa<ConstantInt>(PN.getIncomingValueForBlock(PreHeader)))
250       return true;
251   }
252   return false;
253 }
254 
255 /// Perform some cleanup and simplifications on loops after unrolling. It is
256 /// useful to simplify the IV's in the new loop, as well as do a quick
257 /// simplify/dce pass of the instructions.
258 void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
259                                    ScalarEvolution *SE, DominatorTree *DT,
260                                    AssumptionCache *AC) {
261   // Simplify any new induction variables in the partially unrolled loop.
262   if (SE && SimplifyIVs) {
263     SmallVector<WeakTrackingVH, 16> DeadInsts;
264     simplifyLoopIVs(L, SE, DT, LI, DeadInsts);
265 
266     // Aggressively clean up dead instructions that simplifyLoopIVs already
267     // identified. Any remaining should be cleaned up below.
268     while (!DeadInsts.empty())
269       if (Instruction *Inst =
270               dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val()))
271         RecursivelyDeleteTriviallyDeadInstructions(Inst);
272   }
273 
274   // At this point, the code is well formed.  We now do a quick sweep over the
275   // inserted code, doing constant propagation and dead code elimination as we
276   // go.
277   const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
278   for (BasicBlock *BB : L->getBlocks()) {
279     for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
280       Instruction *Inst = &*I++;
281 
282       if (Value *V = SimplifyInstruction(Inst, {DL, nullptr, DT, AC}))
283         if (LI->replacementPreservesLCSSAForm(Inst, V))
284           Inst->replaceAllUsesWith(V);
285       if (isInstructionTriviallyDead(Inst))
286         BB->getInstList().erase(Inst);
287     }
288   }
289 
290   // TODO: after peeling or unrolling, previously loop variant conditions are
291   // likely to fold to constants, eagerly propagating those here will require
292   // fewer cleanup passes to be run.  Alternatively, a LoopEarlyCSE might be
293   // appropriate.
294 }
295 
296 /// Unroll the given loop by Count. The loop must be in LCSSA form.  Unrolling
297 /// can only fail when the loop's latch block is not terminated by a conditional
298 /// branch instruction. However, if the trip count (and multiple) are not known,
299 /// loop unrolling will mostly produce more code that is no faster.
300 ///
301 /// TripCount is the upper bound of the iteration on which control exits
302 /// LatchBlock. Control may exit the loop prior to TripCount iterations either
303 /// via an early branch in other loop block or via LatchBlock terminator. This
304 /// is relaxed from the general definition of trip count which is the number of
305 /// times the loop header executes. Note that UnrollLoop assumes that the loop
306 /// counter test is in LatchBlock in order to remove unnecesssary instances of
307 /// the test.  If control can exit the loop from the LatchBlock's terminator
308 /// prior to TripCount iterations, flag PreserveCondBr needs to be set.
309 ///
310 /// PreserveCondBr indicates whether the conditional branch of the LatchBlock
311 /// needs to be preserved.  It is needed when we use trip count upper bound to
312 /// fully unroll the loop. If PreserveOnlyFirst is also set then only the first
313 /// conditional branch needs to be preserved.
314 ///
315 /// Similarly, TripMultiple divides the number of times that the LatchBlock may
316 /// execute without exiting the loop.
317 ///
318 /// If AllowRuntime is true then UnrollLoop will consider unrolling loops that
319 /// have a runtime (i.e. not compile time constant) trip count.  Unrolling these
320 /// loops require a unroll "prologue" that runs "RuntimeTripCount % Count"
321 /// iterations before branching into the unrolled loop.  UnrollLoop will not
322 /// runtime-unroll the loop if computing RuntimeTripCount will be expensive and
323 /// AllowExpensiveTripCount is false.
324 ///
325 /// If we want to perform PGO-based loop peeling, PeelCount is set to the
326 /// number of iterations we want to peel off.
327 ///
328 /// The LoopInfo Analysis that is passed will be kept consistent.
329 ///
330 /// This utility preserves LoopInfo. It will also preserve ScalarEvolution and
331 /// DominatorTree if they are non-null.
332 ///
333 /// If RemainderLoop is non-null, it will receive the remainder loop (if
334 /// required and not fully unrolled).
335 LoopUnrollResult llvm::UnrollLoop(
336     Loop *L, unsigned Count, unsigned TripCount, bool Force, bool AllowRuntime,
337     bool AllowExpensiveTripCount, bool PreserveCondBr, bool PreserveOnlyFirst,
338     unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder,
339     LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
340     OptimizationRemarkEmitter *ORE, bool PreserveLCSSA, Loop **RemainderLoop) {
341 
342   BasicBlock *Preheader = L->getLoopPreheader();
343   if (!Preheader) {
344     LLVM_DEBUG(dbgs() << "  Can't unroll; loop preheader-insertion failed.\n");
345     return LoopUnrollResult::Unmodified;
346   }
347 
348   BasicBlock *LatchBlock = L->getLoopLatch();
349   if (!LatchBlock) {
350     LLVM_DEBUG(dbgs() << "  Can't unroll; loop exit-block-insertion failed.\n");
351     return LoopUnrollResult::Unmodified;
352   }
353 
354   // Loops with indirectbr cannot be cloned.
355   if (!L->isSafeToClone()) {
356     LLVM_DEBUG(dbgs() << "  Can't unroll; Loop body cannot be cloned.\n");
357     return LoopUnrollResult::Unmodified;
358   }
359 
360   // The current loop unroll pass can only unroll loops with a single latch
361   // that's a conditional branch exiting the loop.
362   // FIXME: The implementation can be extended to work with more complicated
363   // cases, e.g. loops with multiple latches.
364   BasicBlock *Header = L->getHeader();
365   BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
366 
367   if (!BI || BI->isUnconditional()) {
368     // The loop-rotate pass can be helpful to avoid this in many cases.
369     LLVM_DEBUG(
370         dbgs()
371         << "  Can't unroll; loop not terminated by a conditional branch.\n");
372     return LoopUnrollResult::Unmodified;
373   }
374 
375   auto CheckSuccessors = [&](unsigned S1, unsigned S2) {
376     return BI->getSuccessor(S1) == Header && !L->contains(BI->getSuccessor(S2));
377   };
378 
379   if (!CheckSuccessors(0, 1) && !CheckSuccessors(1, 0)) {
380     LLVM_DEBUG(dbgs() << "Can't unroll; only loops with one conditional latch"
381                          " exiting the loop can be unrolled\n");
382     return LoopUnrollResult::Unmodified;
383   }
384 
385   if (Header->hasAddressTaken()) {
386     // The loop-rotate pass can be helpful to avoid this in many cases.
387     LLVM_DEBUG(
388         dbgs() << "  Won't unroll loop: address of header block is taken.\n");
389     return LoopUnrollResult::Unmodified;
390   }
391 
392   if (TripCount != 0)
393     LLVM_DEBUG(dbgs() << "  Trip Count = " << TripCount << "\n");
394   if (TripMultiple != 1)
395     LLVM_DEBUG(dbgs() << "  Trip Multiple = " << TripMultiple << "\n");
396 
397   // Effectively "DCE" unrolled iterations that are beyond the tripcount
398   // and will never be executed.
399   if (TripCount != 0 && Count > TripCount)
400     Count = TripCount;
401 
402   // Don't enter the unroll code if there is nothing to do.
403   if (TripCount == 0 && Count < 2 && PeelCount == 0) {
404     LLVM_DEBUG(dbgs() << "Won't unroll; almost nothing to do\n");
405     return LoopUnrollResult::Unmodified;
406   }
407 
408   assert(Count > 0);
409   assert(TripMultiple > 0);
410   assert(TripCount == 0 || TripCount % TripMultiple == 0);
411 
412   // Are we eliminating the loop control altogether?
413   bool CompletelyUnroll = Count == TripCount;
414   SmallVector<BasicBlock *, 4> ExitBlocks;
415   L->getExitBlocks(ExitBlocks);
416   std::vector<BasicBlock*> OriginalLoopBlocks = L->getBlocks();
417 
418   // Go through all exits of L and see if there are any phi-nodes there. We just
419   // conservatively assume that they're inserted to preserve LCSSA form, which
420   // means that complete unrolling might break this form. We need to either fix
421   // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For
422   // now we just recompute LCSSA for the outer loop, but it should be possible
423   // to fix it in-place.
424   bool NeedToFixLCSSA = PreserveLCSSA && CompletelyUnroll &&
425                         any_of(ExitBlocks, [](const BasicBlock *BB) {
426                           return isa<PHINode>(BB->begin());
427                         });
428 
429   // We assume a run-time trip count if the compiler cannot
430   // figure out the loop trip count and the unroll-runtime
431   // flag is specified.
432   bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime);
433 
434   assert((!RuntimeTripCount || !PeelCount) &&
435          "Did not expect runtime trip-count unrolling "
436          "and peeling for the same loop");
437 
438   bool Peeled = false;
439   if (PeelCount) {
440     Peeled = peelLoop(L, PeelCount, LI, SE, DT, AC, PreserveLCSSA);
441 
442     // Successful peeling may result in a change in the loop preheader/trip
443     // counts. If we later unroll the loop, we want these to be updated.
444     if (Peeled) {
445       BasicBlock *ExitingBlock = L->getExitingBlock();
446       assert(ExitingBlock && "Loop without exiting block?");
447       Preheader = L->getLoopPreheader();
448       TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
449       TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
450     }
451   }
452 
453   // Loops containing convergent instructions must have a count that divides
454   // their TripMultiple.
455   LLVM_DEBUG(
456       {
457         bool HasConvergent = false;
458         for (auto &BB : L->blocks())
459           for (auto &I : *BB)
460             if (auto CS = CallSite(&I))
461               HasConvergent |= CS.isConvergent();
462         assert((!HasConvergent || TripMultiple % Count == 0) &&
463                "Unroll count must divide trip multiple if loop contains a "
464                "convergent operation.");
465       });
466 
467   bool EpilogProfitability =
468       UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog
469                                               : isEpilogProfitable(L);
470 
471   if (RuntimeTripCount && TripMultiple % Count != 0 &&
472       !UnrollRuntimeLoopRemainder(L, Count, AllowExpensiveTripCount,
473                                   EpilogProfitability, UnrollRemainder, LI, SE,
474                                   DT, AC, PreserveLCSSA, RemainderLoop)) {
475     if (Force)
476       RuntimeTripCount = false;
477     else {
478       LLVM_DEBUG(dbgs() << "Won't unroll; remainder loop could not be "
479                            "generated when assuming runtime trip count\n");
480       return LoopUnrollResult::Unmodified;
481     }
482   }
483 
484   // If we know the trip count, we know the multiple...
485   unsigned BreakoutTrip = 0;
486   if (TripCount != 0) {
487     BreakoutTrip = TripCount % Count;
488     TripMultiple = 0;
489   } else {
490     // Figure out what multiple to use.
491     BreakoutTrip = TripMultiple =
492       (unsigned)GreatestCommonDivisor64(Count, TripMultiple);
493   }
494 
495   using namespace ore;
496   // Report the unrolling decision.
497   if (CompletelyUnroll) {
498     LLVM_DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
499                       << " with trip count " << TripCount << "!\n");
500     if (ORE)
501       ORE->emit([&]() {
502         return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(),
503                                   L->getHeader())
504                << "completely unrolled loop with "
505                << NV("UnrollCount", TripCount) << " iterations";
506       });
507   } else if (PeelCount) {
508     LLVM_DEBUG(dbgs() << "PEELING loop %" << Header->getName()
509                       << " with iteration count " << PeelCount << "!\n");
510     if (ORE)
511       ORE->emit([&]() {
512         return OptimizationRemark(DEBUG_TYPE, "Peeled", L->getStartLoc(),
513                                   L->getHeader())
514                << " peeled loop by " << NV("PeelCount", PeelCount)
515                << " iterations";
516       });
517   } else {
518     auto DiagBuilder = [&]() {
519       OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(),
520                               L->getHeader());
521       return Diag << "unrolled loop by a factor of "
522                   << NV("UnrollCount", Count);
523     };
524 
525     LLVM_DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() << " by "
526                       << Count);
527     if (TripMultiple == 0 || BreakoutTrip != TripMultiple) {
528       LLVM_DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip);
529       if (ORE)
530         ORE->emit([&]() {
531           return DiagBuilder() << " with a breakout at trip "
532                                << NV("BreakoutTrip", BreakoutTrip);
533         });
534     } else if (TripMultiple != 1) {
535       LLVM_DEBUG(dbgs() << " with " << TripMultiple << " trips per branch");
536       if (ORE)
537         ORE->emit([&]() {
538           return DiagBuilder() << " with " << NV("TripMultiple", TripMultiple)
539                                << " trips per branch";
540         });
541     } else if (RuntimeTripCount) {
542       LLVM_DEBUG(dbgs() << " with run-time trip count");
543       if (ORE)
544         ORE->emit(
545             [&]() { return DiagBuilder() << " with run-time trip count"; });
546     }
547     LLVM_DEBUG(dbgs() << "!\n");
548   }
549 
550   // We are going to make changes to this loop. SCEV may be keeping cached info
551   // about it, in particular about backedge taken count. The changes we make
552   // are guaranteed to invalidate this information for our loop. It is tempting
553   // to only invalidate the loop being unrolled, but it is incorrect as long as
554   // all exiting branches from all inner loops have impact on the outer loops,
555   // and if something changes inside them then any of outer loops may also
556   // change. When we forget outermost loop, we also forget all contained loops
557   // and this is what we need here.
558   if (SE)
559     SE->forgetTopmostLoop(L);
560 
561   bool ContinueOnTrue = L->contains(BI->getSuccessor(0));
562   BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue);
563 
564   // For the first iteration of the loop, we should use the precloned values for
565   // PHI nodes.  Insert associations now.
566   ValueToValueMapTy LastValueMap;
567   std::vector<PHINode*> OrigPHINode;
568   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
569     OrigPHINode.push_back(cast<PHINode>(I));
570   }
571 
572   std::vector<BasicBlock*> Headers;
573   std::vector<BasicBlock*> Latches;
574   Headers.push_back(Header);
575   Latches.push_back(LatchBlock);
576 
577   // The current on-the-fly SSA update requires blocks to be processed in
578   // reverse postorder so that LastValueMap contains the correct value at each
579   // exit.
580   LoopBlocksDFS DFS(L);
581   DFS.perform(LI);
582 
583   // Stash the DFS iterators before adding blocks to the loop.
584   LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
585   LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
586 
587   std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks();
588 
589   // Loop Unrolling might create new loops. While we do preserve LoopInfo, we
590   // might break loop-simplified form for these loops (as they, e.g., would
591   // share the same exit blocks). We'll keep track of loops for which we can
592   // break this so that later we can re-simplify them.
593   SmallSetVector<Loop *, 4> LoopsToSimplify;
594   for (Loop *SubLoop : *L)
595     LoopsToSimplify.insert(SubLoop);
596 
597   if (Header->getParent()->isDebugInfoForProfiling())
598     for (BasicBlock *BB : L->getBlocks())
599       for (Instruction &I : *BB)
600         if (!isa<DbgInfoIntrinsic>(&I))
601           if (const DILocation *DIL = I.getDebugLoc())
602             I.setDebugLoc(DIL->cloneWithDuplicationFactor(Count));
603 
604   for (unsigned It = 1; It != Count; ++It) {
605     std::vector<BasicBlock*> NewBlocks;
606     SmallDenseMap<const Loop *, Loop *, 4> NewLoops;
607     NewLoops[L] = L;
608 
609     for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
610       ValueToValueMapTy VMap;
611       BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
612       Header->getParent()->getBasicBlockList().push_back(New);
613 
614       assert((*BB != Header || LI->getLoopFor(*BB) == L) &&
615              "Header should not be in a sub-loop");
616       // Tell LI about New.
617       const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops);
618       if (OldLoop)
619         LoopsToSimplify.insert(NewLoops[OldLoop]);
620 
621       if (*BB == Header)
622         // Loop over all of the PHI nodes in the block, changing them to use
623         // the incoming values from the previous block.
624         for (PHINode *OrigPHI : OrigPHINode) {
625           PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]);
626           Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
627           if (Instruction *InValI = dyn_cast<Instruction>(InVal))
628             if (It > 1 && L->contains(InValI))
629               InVal = LastValueMap[InValI];
630           VMap[OrigPHI] = InVal;
631           New->getInstList().erase(NewPHI);
632         }
633 
634       // Update our running map of newest clones
635       LastValueMap[*BB] = New;
636       for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
637            VI != VE; ++VI)
638         LastValueMap[VI->first] = VI->second;
639 
640       // Add phi entries for newly created values to all exit blocks.
641       for (BasicBlock *Succ : successors(*BB)) {
642         if (L->contains(Succ))
643           continue;
644         for (PHINode &PHI : Succ->phis()) {
645           Value *Incoming = PHI.getIncomingValueForBlock(*BB);
646           ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
647           if (It != LastValueMap.end())
648             Incoming = It->second;
649           PHI.addIncoming(Incoming, New);
650         }
651       }
652       // Keep track of new headers and latches as we create them, so that
653       // we can insert the proper branches later.
654       if (*BB == Header)
655         Headers.push_back(New);
656       if (*BB == LatchBlock)
657         Latches.push_back(New);
658 
659       NewBlocks.push_back(New);
660       UnrolledLoopBlocks.push_back(New);
661 
662       // Update DomTree: since we just copy the loop body, and each copy has a
663       // dedicated entry block (copy of the header block), this header's copy
664       // dominates all copied blocks. That means, dominance relations in the
665       // copied body are the same as in the original body.
666       if (DT) {
667         if (*BB == Header)
668           DT->addNewBlock(New, Latches[It - 1]);
669         else {
670           auto BBDomNode = DT->getNode(*BB);
671           auto BBIDom = BBDomNode->getIDom();
672           BasicBlock *OriginalBBIDom = BBIDom->getBlock();
673           DT->addNewBlock(
674               New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)]));
675         }
676       }
677     }
678 
679     // Remap all instructions in the most recent iteration
680     for (BasicBlock *NewBlock : NewBlocks) {
681       for (Instruction &I : *NewBlock) {
682         ::remapInstruction(&I, LastValueMap);
683         if (auto *II = dyn_cast<IntrinsicInst>(&I))
684           if (II->getIntrinsicID() == Intrinsic::assume)
685             AC->registerAssumption(II);
686       }
687     }
688   }
689 
690   // Loop over the PHI nodes in the original block, setting incoming values.
691   for (PHINode *PN : OrigPHINode) {
692     if (CompletelyUnroll) {
693       PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
694       Header->getInstList().erase(PN);
695     }
696     else if (Count > 1) {
697       Value *InVal = PN->removeIncomingValue(LatchBlock, false);
698       // If this value was defined in the loop, take the value defined by the
699       // last iteration of the loop.
700       if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
701         if (L->contains(InValI))
702           InVal = LastValueMap[InVal];
703       }
704       assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
705       PN->addIncoming(InVal, Latches.back());
706     }
707   }
708 
709   // Now that all the basic blocks for the unrolled iterations are in place,
710   // set up the branches to connect them.
711   for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
712     // The original branch was replicated in each unrolled iteration.
713     BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
714 
715     // The branch destination.
716     unsigned j = (i + 1) % e;
717     BasicBlock *Dest = Headers[j];
718     bool NeedConditional = true;
719 
720     if (RuntimeTripCount && j != 0) {
721       NeedConditional = false;
722     }
723 
724     // For a complete unroll, make the last iteration end with a branch
725     // to the exit block.
726     if (CompletelyUnroll) {
727       if (j == 0)
728         Dest = LoopExit;
729       // If using trip count upper bound to completely unroll, we need to keep
730       // the conditional branch except the last one because the loop may exit
731       // after any iteration.
732       assert(NeedConditional &&
733              "NeedCondition cannot be modified by both complete "
734              "unrolling and runtime unrolling");
735       NeedConditional = (PreserveCondBr && j && !(PreserveOnlyFirst && i != 0));
736     } else if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) {
737       // If we know the trip count or a multiple of it, we can safely use an
738       // unconditional branch for some iterations.
739       NeedConditional = false;
740     }
741 
742     if (NeedConditional) {
743       // Update the conditional branch's successor for the following
744       // iteration.
745       Term->setSuccessor(!ContinueOnTrue, Dest);
746     } else {
747       // Remove phi operands at this loop exit
748       if (Dest != LoopExit) {
749         BasicBlock *BB = Latches[i];
750         for (BasicBlock *Succ: successors(BB)) {
751           if (Succ == Headers[i])
752             continue;
753           for (PHINode &Phi : Succ->phis())
754             Phi.removeIncomingValue(BB, false);
755         }
756       }
757       // Replace the conditional branch with an unconditional one.
758       BranchInst::Create(Dest, Term);
759       Term->eraseFromParent();
760     }
761   }
762 
763   // Update dominators of blocks we might reach through exits.
764   // Immediate dominator of such block might change, because we add more
765   // routes which can lead to the exit: we can now reach it from the copied
766   // iterations too.
767   if (DT && Count > 1) {
768     for (auto *BB : OriginalLoopBlocks) {
769       auto *BBDomNode = DT->getNode(BB);
770       SmallVector<BasicBlock *, 16> ChildrenToUpdate;
771       for (auto *ChildDomNode : BBDomNode->getChildren()) {
772         auto *ChildBB = ChildDomNode->getBlock();
773         if (!L->contains(ChildBB))
774           ChildrenToUpdate.push_back(ChildBB);
775       }
776       BasicBlock *NewIDom;
777       if (BB == LatchBlock) {
778         // The latch is special because we emit unconditional branches in
779         // some cases where the original loop contained a conditional branch.
780         // Since the latch is always at the bottom of the loop, if the latch
781         // dominated an exit before unrolling, the new dominator of that exit
782         // must also be a latch.  Specifically, the dominator is the first
783         // latch which ends in a conditional branch, or the last latch if
784         // there is no such latch.
785         NewIDom = Latches.back();
786         for (BasicBlock *IterLatch : Latches) {
787           Instruction *Term = IterLatch->getTerminator();
788           if (isa<BranchInst>(Term) && cast<BranchInst>(Term)->isConditional()) {
789             NewIDom = IterLatch;
790             break;
791           }
792         }
793       } else {
794         // The new idom of the block will be the nearest common dominator
795         // of all copies of the previous idom. This is equivalent to the
796         // nearest common dominator of the previous idom and the first latch,
797         // which dominates all copies of the previous idom.
798         NewIDom = DT->findNearestCommonDominator(BB, LatchBlock);
799       }
800       for (auto *ChildBB : ChildrenToUpdate)
801         DT->changeImmediateDominator(ChildBB, NewIDom);
802     }
803   }
804 
805   assert(!DT || !UnrollVerifyDomtree ||
806       DT->verify(DominatorTree::VerificationLevel::Fast));
807 
808   // Merge adjacent basic blocks, if possible.
809   for (BasicBlock *Latch : Latches) {
810     BranchInst *Term = cast<BranchInst>(Latch->getTerminator());
811     if (Term->isUnconditional()) {
812       BasicBlock *Dest = Term->getSuccessor(0);
813       if (BasicBlock *Fold = foldBlockIntoPredecessor(Dest, LI, SE, DT)) {
814         // Dest has been folded into Fold. Update our worklists accordingly.
815         std::replace(Latches.begin(), Latches.end(), Dest, Fold);
816         UnrolledLoopBlocks.erase(std::remove(UnrolledLoopBlocks.begin(),
817                                              UnrolledLoopBlocks.end(), Dest),
818                                  UnrolledLoopBlocks.end());
819       }
820     }
821   }
822 
823   // At this point, the code is well formed.  We now simplify the unrolled loop,
824   // doing constant propagation and dead code elimination as we go.
825   simplifyLoopAfterUnroll(L, !CompletelyUnroll && (Count > 1 || Peeled), LI, SE,
826                           DT, AC);
827 
828   NumCompletelyUnrolled += CompletelyUnroll;
829   ++NumUnrolled;
830 
831   Loop *OuterL = L->getParentLoop();
832   // Update LoopInfo if the loop is completely removed.
833   if (CompletelyUnroll)
834     LI->erase(L);
835 
836   // After complete unrolling most of the blocks should be contained in OuterL.
837   // However, some of them might happen to be out of OuterL (e.g. if they
838   // precede a loop exit). In this case we might need to insert PHI nodes in
839   // order to preserve LCSSA form.
840   // We don't need to check this if we already know that we need to fix LCSSA
841   // form.
842   // TODO: For now we just recompute LCSSA for the outer loop in this case, but
843   // it should be possible to fix it in-place.
844   if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA)
845     NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI);
846 
847   // If we have a pass and a DominatorTree we should re-simplify impacted loops
848   // to ensure subsequent analyses can rely on this form. We want to simplify
849   // at least one layer outside of the loop that was unrolled so that any
850   // changes to the parent loop exposed by the unrolling are considered.
851   if (DT) {
852     if (OuterL) {
853       // OuterL includes all loops for which we can break loop-simplify, so
854       // it's sufficient to simplify only it (it'll recursively simplify inner
855       // loops too).
856       if (NeedToFixLCSSA) {
857         // LCSSA must be performed on the outermost affected loop. The unrolled
858         // loop's last loop latch is guaranteed to be in the outermost loop
859         // after LoopInfo's been updated by LoopInfo::erase.
860         Loop *LatchLoop = LI->getLoopFor(Latches.back());
861         Loop *FixLCSSALoop = OuterL;
862         if (!FixLCSSALoop->contains(LatchLoop))
863           while (FixLCSSALoop->getParentLoop() != LatchLoop)
864             FixLCSSALoop = FixLCSSALoop->getParentLoop();
865 
866         formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE);
867       } else if (PreserveLCSSA) {
868         assert(OuterL->isLCSSAForm(*DT) &&
869                "Loops should be in LCSSA form after loop-unroll.");
870       }
871 
872       // TODO: That potentially might be compile-time expensive. We should try
873       // to fix the loop-simplified form incrementally.
874       simplifyLoop(OuterL, DT, LI, SE, AC, PreserveLCSSA);
875     } else {
876       // Simplify loops for which we might've broken loop-simplify form.
877       for (Loop *SubLoop : LoopsToSimplify)
878         simplifyLoop(SubLoop, DT, LI, SE, AC, PreserveLCSSA);
879     }
880   }
881 
882   return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled
883                           : LoopUnrollResult::PartiallyUnrolled;
884 }
885 
886 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata
887 /// node with the given name (for example, "llvm.loop.unroll.count"). If no
888 /// such metadata node exists, then nullptr is returned.
889 MDNode *llvm::GetUnrollMetadata(MDNode *LoopID, StringRef Name) {
890   // First operand should refer to the loop id itself.
891   assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
892   assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
893 
894   for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
895     MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
896     if (!MD)
897       continue;
898 
899     MDString *S = dyn_cast<MDString>(MD->getOperand(0));
900     if (!S)
901       continue;
902 
903     if (Name.equals(S->getString()))
904       return MD;
905   }
906   return nullptr;
907 }
908