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