1 //===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements some loop unrolling utilities. It does not define any
10 // actual pass or policy, but provides a single function to perform loop
11 // unrolling.
12 //
13 // The process of unrolling can produce extraneous basic blocks linked with
14 // unconditional branches. This will be corrected in the future.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/ADT/Twine.h"
27 #include "llvm/ADT/ilist_iterator.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/Analysis/AssumptionCache.h"
30 #include "llvm/Analysis/DomTreeUpdater.h"
31 #include "llvm/Analysis/InstructionSimplify.h"
32 #include "llvm/Analysis/LoopInfo.h"
33 #include "llvm/Analysis/LoopIterator.h"
34 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
35 #include "llvm/Analysis/ScalarEvolution.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DebugInfoMetadata.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/IR/DiagnosticInfo.h"
42 #include "llvm/IR/Dominators.h"
43 #include "llvm/IR/Function.h"
44 #include "llvm/IR/Instruction.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/IR/IntrinsicInst.h"
47 #include "llvm/IR/Metadata.h"
48 #include "llvm/IR/Module.h"
49 #include "llvm/IR/Use.h"
50 #include "llvm/IR/User.h"
51 #include "llvm/IR/ValueHandle.h"
52 #include "llvm/IR/ValueMap.h"
53 #include "llvm/Support/Casting.h"
54 #include "llvm/Support/CommandLine.h"
55 #include "llvm/Support/Debug.h"
56 #include "llvm/Support/GenericDomTree.h"
57 #include "llvm/Support/MathExtras.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
60 #include "llvm/Transforms/Utils/Cloning.h"
61 #include "llvm/Transforms/Utils/Local.h"
62 #include "llvm/Transforms/Utils/LoopSimplify.h"
63 #include "llvm/Transforms/Utils/LoopUtils.h"
64 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
65 #include "llvm/Transforms/Utils/UnrollLoop.h"
66 #include "llvm/Transforms/Utils/ValueMapper.h"
67 #include <algorithm>
68 #include <assert.h>
69 #include <type_traits>
70 #include <vector>
71
72 namespace llvm {
73 class DataLayout;
74 class Value;
75 } // namespace llvm
76
77 using namespace llvm;
78
79 #define DEBUG_TYPE "loop-unroll"
80
81 // TODO: Should these be here or in LoopUnroll?
82 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
83 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
84 STATISTIC(NumUnrolledNotLatch, "Number of loops unrolled without a conditional "
85 "latch (completely or otherwise)");
86
87 static cl::opt<bool>
88 UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden,
89 cl::desc("Allow runtime unrolled loops to be unrolled "
90 "with epilog instead of prolog."));
91
92 static cl::opt<bool>
93 UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden,
94 cl::desc("Verify domtree after unrolling"),
95 #ifdef EXPENSIVE_CHECKS
96 cl::init(true)
97 #else
98 cl::init(false)
99 #endif
100 );
101
102 static cl::opt<bool>
103 UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden,
104 cl::desc("Verify loopinfo after unrolling"),
105 #ifdef EXPENSIVE_CHECKS
106 cl::init(true)
107 #else
108 cl::init(false)
109 #endif
110 );
111
112
113 /// Check if unrolling created a situation where we need to insert phi nodes to
114 /// preserve LCSSA form.
115 /// \param Blocks is a vector of basic blocks representing unrolled loop.
116 /// \param L is the outer loop.
117 /// It's possible that some of the blocks are in L, and some are not. In this
118 /// case, if there is a use is outside L, and definition is inside L, we need to
119 /// insert a phi-node, otherwise LCSSA will be broken.
120 /// The function is just a helper function for llvm::UnrollLoop that returns
121 /// true if this situation occurs, indicating that LCSSA needs to be fixed.
needToInsertPhisForLCSSA(Loop * L,const std::vector<BasicBlock * > & Blocks,LoopInfo * LI)122 static bool needToInsertPhisForLCSSA(Loop *L,
123 const std::vector<BasicBlock *> &Blocks,
124 LoopInfo *LI) {
125 for (BasicBlock *BB : Blocks) {
126 if (LI->getLoopFor(BB) == L)
127 continue;
128 for (Instruction &I : *BB) {
129 for (Use &U : I.operands()) {
130 if (const auto *Def = dyn_cast<Instruction>(U)) {
131 Loop *DefLoop = LI->getLoopFor(Def->getParent());
132 if (!DefLoop)
133 continue;
134 if (DefLoop->contains(L))
135 return true;
136 }
137 }
138 }
139 }
140 return false;
141 }
142
143 /// Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary
144 /// and adds a mapping from the original loop to the new loop to NewLoops.
145 /// Returns nullptr if no new loop was created and a pointer to the
146 /// original loop OriginalBB was part of otherwise.
addClonedBlockToLoopInfo(BasicBlock * OriginalBB,BasicBlock * ClonedBB,LoopInfo * LI,NewLoopsMap & NewLoops)147 const Loop* llvm::addClonedBlockToLoopInfo(BasicBlock *OriginalBB,
148 BasicBlock *ClonedBB, LoopInfo *LI,
149 NewLoopsMap &NewLoops) {
150 // Figure out which loop New is in.
151 const Loop *OldLoop = LI->getLoopFor(OriginalBB);
152 assert(OldLoop && "Should (at least) be in the loop being unrolled!");
153
154 Loop *&NewLoop = NewLoops[OldLoop];
155 if (!NewLoop) {
156 // Found a new sub-loop.
157 assert(OriginalBB == OldLoop->getHeader() &&
158 "Header should be first in RPO");
159
160 NewLoop = LI->AllocateLoop();
161 Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
162
163 if (NewLoopParent)
164 NewLoopParent->addChildLoop(NewLoop);
165 else
166 LI->addTopLevelLoop(NewLoop);
167
168 NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
169 return OldLoop;
170 } else {
171 NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
172 return nullptr;
173 }
174 }
175
176 /// The function chooses which type of unroll (epilog or prolog) is more
177 /// profitabale.
178 /// Epilog unroll is more profitable when there is PHI that starts from
179 /// constant. In this case epilog will leave PHI start from constant,
180 /// but prolog will convert it to non-constant.
181 ///
182 /// loop:
183 /// PN = PHI [I, Latch], [CI, PreHeader]
184 /// I = foo(PN)
185 /// ...
186 ///
187 /// Epilog unroll case.
188 /// loop:
189 /// PN = PHI [I2, Latch], [CI, PreHeader]
190 /// I1 = foo(PN)
191 /// I2 = foo(I1)
192 /// ...
193 /// Prolog unroll case.
194 /// NewPN = PHI [PrologI, Prolog], [CI, PreHeader]
195 /// loop:
196 /// PN = PHI [I2, Latch], [NewPN, PreHeader]
197 /// I1 = foo(PN)
198 /// I2 = foo(I1)
199 /// ...
200 ///
isEpilogProfitable(Loop * L)201 static bool isEpilogProfitable(Loop *L) {
202 BasicBlock *PreHeader = L->getLoopPreheader();
203 BasicBlock *Header = L->getHeader();
204 assert(PreHeader && Header);
205 for (const PHINode &PN : Header->phis()) {
206 if (isa<ConstantInt>(PN.getIncomingValueForBlock(PreHeader)))
207 return true;
208 }
209 return false;
210 }
211
212 /// Perform some cleanup and simplifications on loops after unrolling. It is
213 /// useful to simplify the IV's in the new loop, as well as do a quick
214 /// simplify/dce pass of the instructions.
simplifyLoopAfterUnroll(Loop * L,bool SimplifyIVs,LoopInfo * LI,ScalarEvolution * SE,DominatorTree * DT,AssumptionCache * AC,const TargetTransformInfo * TTI)215 void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
216 ScalarEvolution *SE, DominatorTree *DT,
217 AssumptionCache *AC,
218 const TargetTransformInfo *TTI) {
219 // Simplify any new induction variables in the partially unrolled loop.
220 if (SE && SimplifyIVs) {
221 SmallVector<WeakTrackingVH, 16> DeadInsts;
222 simplifyLoopIVs(L, SE, DT, LI, TTI, DeadInsts);
223
224 // Aggressively clean up dead instructions that simplifyLoopIVs already
225 // identified. Any remaining should be cleaned up below.
226 while (!DeadInsts.empty()) {
227 Value *V = DeadInsts.pop_back_val();
228 if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
229 RecursivelyDeleteTriviallyDeadInstructions(Inst);
230 }
231 }
232
233 // At this point, the code is well formed. Perform constprop, instsimplify,
234 // and dce.
235 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
236 SmallVector<WeakTrackingVH, 16> DeadInsts;
237 for (BasicBlock *BB : L->getBlocks()) {
238 for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
239 if (Value *V = simplifyInstruction(&Inst, {DL, nullptr, DT, AC}))
240 if (LI->replacementPreservesLCSSAForm(&Inst, V))
241 Inst.replaceAllUsesWith(V);
242 if (isInstructionTriviallyDead(&Inst))
243 DeadInsts.emplace_back(&Inst);
244 }
245 // We can't do recursive deletion until we're done iterating, as we might
246 // have a phi which (potentially indirectly) uses instructions later in
247 // the block we're iterating through.
248 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
249 }
250 }
251
252 /// Unroll the given loop by Count. The loop must be in LCSSA form. Unrolling
253 /// can only fail when the loop's latch block is not terminated by a conditional
254 /// branch instruction. However, if the trip count (and multiple) are not known,
255 /// loop unrolling will mostly produce more code that is no faster.
256 ///
257 /// If Runtime is true then UnrollLoop will try to insert a prologue or
258 /// epilogue that ensures the latch has a trip multiple of Count. UnrollLoop
259 /// will not runtime-unroll the loop if computing the run-time trip count will
260 /// be expensive and AllowExpensiveTripCount is false.
261 ///
262 /// The LoopInfo Analysis that is passed will be kept consistent.
263 ///
264 /// This utility preserves LoopInfo. It will also preserve ScalarEvolution and
265 /// DominatorTree if they are non-null.
266 ///
267 /// If RemainderLoop is non-null, it will receive the remainder loop (if
268 /// required and not fully unrolled).
UnrollLoop(Loop * L,UnrollLoopOptions ULO,LoopInfo * LI,ScalarEvolution * SE,DominatorTree * DT,AssumptionCache * AC,const TargetTransformInfo * TTI,OptimizationRemarkEmitter * ORE,bool PreserveLCSSA,Loop ** RemainderLoop)269 LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
270 ScalarEvolution *SE, DominatorTree *DT,
271 AssumptionCache *AC,
272 const TargetTransformInfo *TTI,
273 OptimizationRemarkEmitter *ORE,
274 bool PreserveLCSSA, Loop **RemainderLoop) {
275 assert(DT && "DomTree is required");
276
277 if (!L->getLoopPreheader()) {
278 LLVM_DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
279 return LoopUnrollResult::Unmodified;
280 }
281
282 if (!L->getLoopLatch()) {
283 LLVM_DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
284 return LoopUnrollResult::Unmodified;
285 }
286
287 // Loops with indirectbr cannot be cloned.
288 if (!L->isSafeToClone()) {
289 LLVM_DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n");
290 return LoopUnrollResult::Unmodified;
291 }
292
293 if (L->getHeader()->hasAddressTaken()) {
294 // The loop-rotate pass can be helpful to avoid this in many cases.
295 LLVM_DEBUG(
296 dbgs() << " Won't unroll loop: address of header block is taken.\n");
297 return LoopUnrollResult::Unmodified;
298 }
299
300 assert(ULO.Count > 0);
301
302 // All these values should be taken only after peeling because they might have
303 // changed.
304 BasicBlock *Preheader = L->getLoopPreheader();
305 BasicBlock *Header = L->getHeader();
306 BasicBlock *LatchBlock = L->getLoopLatch();
307 SmallVector<BasicBlock *, 4> ExitBlocks;
308 L->getExitBlocks(ExitBlocks);
309 std::vector<BasicBlock *> OriginalLoopBlocks = L->getBlocks();
310
311 const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L);
312 const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L);
313
314 // Effectively "DCE" unrolled iterations that are beyond the max tripcount
315 // and will never be executed.
316 if (MaxTripCount && ULO.Count > MaxTripCount)
317 ULO.Count = MaxTripCount;
318
319 struct ExitInfo {
320 unsigned TripCount;
321 unsigned TripMultiple;
322 unsigned BreakoutTrip;
323 bool ExitOnTrue;
324 SmallVector<BasicBlock *> ExitingBlocks;
325 };
326 DenseMap<BasicBlock *, ExitInfo> ExitInfos;
327 SmallVector<BasicBlock *, 4> ExitingBlocks;
328 L->getExitingBlocks(ExitingBlocks);
329 for (auto *ExitingBlock : ExitingBlocks) {
330 // The folding code is not prepared to deal with non-branch instructions
331 // right now.
332 auto *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
333 if (!BI)
334 continue;
335
336 ExitInfo &Info = ExitInfos.try_emplace(ExitingBlock).first->second;
337 Info.TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
338 Info.TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
339 if (Info.TripCount != 0) {
340 Info.BreakoutTrip = Info.TripCount % ULO.Count;
341 Info.TripMultiple = 0;
342 } else {
343 Info.BreakoutTrip = Info.TripMultiple =
344 (unsigned)GreatestCommonDivisor64(ULO.Count, Info.TripMultiple);
345 }
346 Info.ExitOnTrue = !L->contains(BI->getSuccessor(0));
347 Info.ExitingBlocks.push_back(ExitingBlock);
348 LLVM_DEBUG(dbgs() << " Exiting block %" << ExitingBlock->getName()
349 << ": TripCount=" << Info.TripCount
350 << ", TripMultiple=" << Info.TripMultiple
351 << ", BreakoutTrip=" << Info.BreakoutTrip << "\n");
352 }
353
354 // Are we eliminating the loop control altogether? Note that we can know
355 // we're eliminating the backedge without knowing exactly which iteration
356 // of the unrolled body exits.
357 const bool CompletelyUnroll = ULO.Count == MaxTripCount;
358
359 const bool PreserveOnlyFirst = CompletelyUnroll && MaxOrZero;
360
361 // There's no point in performing runtime unrolling if this unroll count
362 // results in a full unroll.
363 if (CompletelyUnroll)
364 ULO.Runtime = false;
365
366 // Go through all exits of L and see if there are any phi-nodes there. We just
367 // conservatively assume that they're inserted to preserve LCSSA form, which
368 // means that complete unrolling might break this form. We need to either fix
369 // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For
370 // now we just recompute LCSSA for the outer loop, but it should be possible
371 // to fix it in-place.
372 bool NeedToFixLCSSA =
373 PreserveLCSSA && CompletelyUnroll &&
374 any_of(ExitBlocks,
375 [](const BasicBlock *BB) { return isa<PHINode>(BB->begin()); });
376
377 // The current loop unroll pass can unroll loops that have
378 // (1) single latch; and
379 // (2a) latch is unconditional; or
380 // (2b) latch is conditional and is an exiting block
381 // FIXME: The implementation can be extended to work with more complicated
382 // cases, e.g. loops with multiple latches.
383 BranchInst *LatchBI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
384
385 // A conditional branch which exits the loop, which can be optimized to an
386 // unconditional branch in the unrolled loop in some cases.
387 bool LatchIsExiting = L->isLoopExiting(LatchBlock);
388 if (!LatchBI || (LatchBI->isConditional() && !LatchIsExiting)) {
389 LLVM_DEBUG(
390 dbgs() << "Can't unroll; a conditional latch must exit the loop");
391 return LoopUnrollResult::Unmodified;
392 }
393
394 // Loops containing convergent instructions cannot use runtime unrolling,
395 // as the prologue/epilogue may add additional control-dependencies to
396 // convergent operations.
397 LLVM_DEBUG(
398 {
399 bool HasConvergent = false;
400 for (auto &BB : L->blocks())
401 for (auto &I : *BB)
402 if (auto *CB = dyn_cast<CallBase>(&I))
403 HasConvergent |= CB->isConvergent();
404 assert((!HasConvergent || !ULO.Runtime) &&
405 "Can't runtime unroll if loop contains a convergent operation.");
406 });
407
408 bool EpilogProfitability =
409 UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog
410 : isEpilogProfitable(L);
411
412 if (ULO.Runtime &&
413 !UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount,
414 EpilogProfitability, ULO.UnrollRemainder,
415 ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
416 PreserveLCSSA, RemainderLoop)) {
417 if (ULO.Force)
418 ULO.Runtime = false;
419 else {
420 LLVM_DEBUG(dbgs() << "Won't unroll; remainder loop could not be "
421 "generated when assuming runtime trip count\n");
422 return LoopUnrollResult::Unmodified;
423 }
424 }
425
426 using namespace ore;
427 // Report the unrolling decision.
428 if (CompletelyUnroll) {
429 LLVM_DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
430 << " with trip count " << ULO.Count << "!\n");
431 if (ORE)
432 ORE->emit([&]() {
433 return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(),
434 L->getHeader())
435 << "completely unrolled loop with "
436 << NV("UnrollCount", ULO.Count) << " iterations";
437 });
438 } else {
439 LLVM_DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() << " by "
440 << ULO.Count);
441 if (ULO.Runtime)
442 LLVM_DEBUG(dbgs() << " with run-time trip count");
443 LLVM_DEBUG(dbgs() << "!\n");
444
445 if (ORE)
446 ORE->emit([&]() {
447 OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(),
448 L->getHeader());
449 Diag << "unrolled loop by a factor of " << NV("UnrollCount", ULO.Count);
450 if (ULO.Runtime)
451 Diag << " with run-time trip count";
452 return Diag;
453 });
454 }
455
456 // We are going to make changes to this loop. SCEV may be keeping cached info
457 // about it, in particular about backedge taken count. The changes we make
458 // are guaranteed to invalidate this information for our loop. It is tempting
459 // to only invalidate the loop being unrolled, but it is incorrect as long as
460 // all exiting branches from all inner loops have impact on the outer loops,
461 // and if something changes inside them then any of outer loops may also
462 // change. When we forget outermost loop, we also forget all contained loops
463 // and this is what we need here.
464 if (SE) {
465 if (ULO.ForgetAllSCEV)
466 SE->forgetAllLoops();
467 else
468 SE->forgetTopmostLoop(L);
469 }
470
471 if (!LatchIsExiting)
472 ++NumUnrolledNotLatch;
473
474 // For the first iteration of the loop, we should use the precloned values for
475 // PHI nodes. Insert associations now.
476 ValueToValueMapTy LastValueMap;
477 std::vector<PHINode*> OrigPHINode;
478 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
479 OrigPHINode.push_back(cast<PHINode>(I));
480 }
481
482 std::vector<BasicBlock *> Headers;
483 std::vector<BasicBlock *> Latches;
484 Headers.push_back(Header);
485 Latches.push_back(LatchBlock);
486
487 // The current on-the-fly SSA update requires blocks to be processed in
488 // reverse postorder so that LastValueMap contains the correct value at each
489 // exit.
490 LoopBlocksDFS DFS(L);
491 DFS.perform(LI);
492
493 // Stash the DFS iterators before adding blocks to the loop.
494 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
495 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
496
497 std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks();
498
499 // Loop Unrolling might create new loops. While we do preserve LoopInfo, we
500 // might break loop-simplified form for these loops (as they, e.g., would
501 // share the same exit blocks). We'll keep track of loops for which we can
502 // break this so that later we can re-simplify them.
503 SmallSetVector<Loop *, 4> LoopsToSimplify;
504 for (Loop *SubLoop : *L)
505 LoopsToSimplify.insert(SubLoop);
506
507 // When a FSDiscriminator is enabled, we don't need to add the multiply
508 // factors to the discriminators.
509 if (Header->getParent()->isDebugInfoForProfiling() && !EnableFSDiscriminator)
510 for (BasicBlock *BB : L->getBlocks())
511 for (Instruction &I : *BB)
512 if (!isa<DbgInfoIntrinsic>(&I))
513 if (const DILocation *DIL = I.getDebugLoc()) {
514 auto NewDIL = DIL->cloneByMultiplyingDuplicationFactor(ULO.Count);
515 if (NewDIL)
516 I.setDebugLoc(*NewDIL);
517 else
518 LLVM_DEBUG(dbgs()
519 << "Failed to create new discriminator: "
520 << DIL->getFilename() << " Line: " << DIL->getLine());
521 }
522
523 // Identify what noalias metadata is inside the loop: if it is inside the
524 // loop, the associated metadata must be cloned for each iteration.
525 SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
526 identifyNoAliasScopesToClone(L->getBlocks(), LoopLocalNoAliasDeclScopes);
527
528 // We place the unrolled iterations immediately after the original loop
529 // latch. This is a reasonable default placement if we don't have block
530 // frequencies, and if we do, well the layout will be adjusted later.
531 auto BlockInsertPt = std::next(LatchBlock->getIterator());
532 for (unsigned It = 1; It != ULO.Count; ++It) {
533 SmallVector<BasicBlock *, 8> NewBlocks;
534 SmallDenseMap<const Loop *, Loop *, 4> NewLoops;
535 NewLoops[L] = L;
536
537 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
538 ValueToValueMapTy VMap;
539 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
540 Header->getParent()->getBasicBlockList().insert(BlockInsertPt, New);
541
542 assert((*BB != Header || LI->getLoopFor(*BB) == L) &&
543 "Header should not be in a sub-loop");
544 // Tell LI about New.
545 const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops);
546 if (OldLoop)
547 LoopsToSimplify.insert(NewLoops[OldLoop]);
548
549 if (*BB == Header)
550 // Loop over all of the PHI nodes in the block, changing them to use
551 // the incoming values from the previous block.
552 for (PHINode *OrigPHI : OrigPHINode) {
553 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]);
554 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
555 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
556 if (It > 1 && L->contains(InValI))
557 InVal = LastValueMap[InValI];
558 VMap[OrigPHI] = InVal;
559 New->getInstList().erase(NewPHI);
560 }
561
562 // Update our running map of newest clones
563 LastValueMap[*BB] = New;
564 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
565 VI != VE; ++VI)
566 LastValueMap[VI->first] = VI->second;
567
568 // Add phi entries for newly created values to all exit blocks.
569 for (BasicBlock *Succ : successors(*BB)) {
570 if (L->contains(Succ))
571 continue;
572 for (PHINode &PHI : Succ->phis()) {
573 Value *Incoming = PHI.getIncomingValueForBlock(*BB);
574 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
575 if (It != LastValueMap.end())
576 Incoming = It->second;
577 PHI.addIncoming(Incoming, New);
578 }
579 }
580 // Keep track of new headers and latches as we create them, so that
581 // we can insert the proper branches later.
582 if (*BB == Header)
583 Headers.push_back(New);
584 if (*BB == LatchBlock)
585 Latches.push_back(New);
586
587 // Keep track of the exiting block and its successor block contained in
588 // the loop for the current iteration.
589 auto ExitInfoIt = ExitInfos.find(*BB);
590 if (ExitInfoIt != ExitInfos.end())
591 ExitInfoIt->second.ExitingBlocks.push_back(New);
592
593 NewBlocks.push_back(New);
594 UnrolledLoopBlocks.push_back(New);
595
596 // Update DomTree: since we just copy the loop body, and each copy has a
597 // dedicated entry block (copy of the header block), this header's copy
598 // dominates all copied blocks. That means, dominance relations in the
599 // copied body are the same as in the original body.
600 if (*BB == Header)
601 DT->addNewBlock(New, Latches[It - 1]);
602 else {
603 auto BBDomNode = DT->getNode(*BB);
604 auto BBIDom = BBDomNode->getIDom();
605 BasicBlock *OriginalBBIDom = BBIDom->getBlock();
606 DT->addNewBlock(
607 New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)]));
608 }
609 }
610
611 // Remap all instructions in the most recent iteration
612 remapInstructionsInBlocks(NewBlocks, LastValueMap);
613 for (BasicBlock *NewBlock : NewBlocks)
614 for (Instruction &I : *NewBlock)
615 if (auto *II = dyn_cast<AssumeInst>(&I))
616 AC->registerAssumption(II);
617
618 {
619 // Identify what other metadata depends on the cloned version. After
620 // cloning, replace the metadata with the corrected version for both
621 // memory instructions and noalias intrinsics.
622 std::string ext = (Twine("It") + Twine(It)).str();
623 cloneAndAdaptNoAliasScopes(LoopLocalNoAliasDeclScopes, NewBlocks,
624 Header->getContext(), ext);
625 }
626 }
627
628 // Loop over the PHI nodes in the original block, setting incoming values.
629 for (PHINode *PN : OrigPHINode) {
630 if (CompletelyUnroll) {
631 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
632 Header->getInstList().erase(PN);
633 } else if (ULO.Count > 1) {
634 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
635 // If this value was defined in the loop, take the value defined by the
636 // last iteration of the loop.
637 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
638 if (L->contains(InValI))
639 InVal = LastValueMap[InVal];
640 }
641 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
642 PN->addIncoming(InVal, Latches.back());
643 }
644 }
645
646 // Connect latches of the unrolled iterations to the headers of the next
647 // iteration. Currently they point to the header of the same iteration.
648 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
649 unsigned j = (i + 1) % e;
650 Latches[i]->getTerminator()->replaceSuccessorWith(Headers[i], Headers[j]);
651 }
652
653 // Update dominators of blocks we might reach through exits.
654 // Immediate dominator of such block might change, because we add more
655 // routes which can lead to the exit: we can now reach it from the copied
656 // iterations too.
657 if (ULO.Count > 1) {
658 for (auto *BB : OriginalLoopBlocks) {
659 auto *BBDomNode = DT->getNode(BB);
660 SmallVector<BasicBlock *, 16> ChildrenToUpdate;
661 for (auto *ChildDomNode : BBDomNode->children()) {
662 auto *ChildBB = ChildDomNode->getBlock();
663 if (!L->contains(ChildBB))
664 ChildrenToUpdate.push_back(ChildBB);
665 }
666 // The new idom of the block will be the nearest common dominator
667 // of all copies of the previous idom. This is equivalent to the
668 // nearest common dominator of the previous idom and the first latch,
669 // which dominates all copies of the previous idom.
670 BasicBlock *NewIDom = DT->findNearestCommonDominator(BB, LatchBlock);
671 for (auto *ChildBB : ChildrenToUpdate)
672 DT->changeImmediateDominator(ChildBB, NewIDom);
673 }
674 }
675
676 assert(!UnrollVerifyDomtree ||
677 DT->verify(DominatorTree::VerificationLevel::Fast));
678
679 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
680
681 auto SetDest = [&](BasicBlock *Src, bool WillExit, bool ExitOnTrue) {
682 auto *Term = cast<BranchInst>(Src->getTerminator());
683 const unsigned Idx = ExitOnTrue ^ WillExit;
684 BasicBlock *Dest = Term->getSuccessor(Idx);
685 BasicBlock *DeadSucc = Term->getSuccessor(1-Idx);
686
687 // Remove predecessors from all non-Dest successors.
688 DeadSucc->removePredecessor(Src, /* KeepOneInputPHIs */ true);
689
690 // Replace the conditional branch with an unconditional one.
691 BranchInst::Create(Dest, Term);
692 Term->eraseFromParent();
693
694 DTU.applyUpdates({{DominatorTree::Delete, Src, DeadSucc}});
695 };
696
697 auto WillExit = [&](const ExitInfo &Info, unsigned i, unsigned j,
698 bool IsLatch) -> Optional<bool> {
699 if (CompletelyUnroll) {
700 if (PreserveOnlyFirst) {
701 if (i == 0)
702 return None;
703 return j == 0;
704 }
705 // Complete (but possibly inexact) unrolling
706 if (j == 0)
707 return true;
708 if (Info.TripCount && j != Info.TripCount)
709 return false;
710 return None;
711 }
712
713 if (ULO.Runtime) {
714 // If runtime unrolling inserts a prologue, information about non-latch
715 // exits may be stale.
716 if (IsLatch && j != 0)
717 return false;
718 return None;
719 }
720
721 if (j != Info.BreakoutTrip &&
722 (Info.TripMultiple == 0 || j % Info.TripMultiple != 0)) {
723 // If we know the trip count or a multiple of it, we can safely use an
724 // unconditional branch for some iterations.
725 return false;
726 }
727 return None;
728 };
729
730 // Fold branches for iterations where we know that they will exit or not
731 // exit.
732 for (const auto &Pair : ExitInfos) {
733 const ExitInfo &Info = Pair.second;
734 for (unsigned i = 0, e = Info.ExitingBlocks.size(); i != e; ++i) {
735 // The branch destination.
736 unsigned j = (i + 1) % e;
737 bool IsLatch = Pair.first == LatchBlock;
738 Optional<bool> KnownWillExit = WillExit(Info, i, j, IsLatch);
739 if (!KnownWillExit)
740 continue;
741
742 // We don't fold known-exiting branches for non-latch exits here,
743 // because this ensures that both all loop blocks and all exit blocks
744 // remain reachable in the CFG.
745 // TODO: We could fold these branches, but it would require much more
746 // sophisticated updates to LoopInfo.
747 if (*KnownWillExit && !IsLatch)
748 continue;
749
750 SetDest(Info.ExitingBlocks[i], *KnownWillExit, Info.ExitOnTrue);
751 }
752 }
753
754 // When completely unrolling, the last latch becomes unreachable.
755 if (!LatchIsExiting && CompletelyUnroll)
756 changeToUnreachable(Latches.back()->getTerminator(), PreserveLCSSA, &DTU);
757
758 // Merge adjacent basic blocks, if possible.
759 for (BasicBlock *Latch : Latches) {
760 BranchInst *Term = dyn_cast<BranchInst>(Latch->getTerminator());
761 assert((Term ||
762 (CompletelyUnroll && !LatchIsExiting && Latch == Latches.back())) &&
763 "Need a branch as terminator, except when fully unrolling with "
764 "unconditional latch");
765 if (Term && Term->isUnconditional()) {
766 BasicBlock *Dest = Term->getSuccessor(0);
767 BasicBlock *Fold = Dest->getUniquePredecessor();
768 if (MergeBlockIntoPredecessor(Dest, &DTU, LI)) {
769 // Dest has been folded into Fold. Update our worklists accordingly.
770 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
771 llvm::erase_value(UnrolledLoopBlocks, Dest);
772 }
773 }
774 }
775 // Apply updates to the DomTree.
776 DT = &DTU.getDomTree();
777
778 assert(!UnrollVerifyDomtree ||
779 DT->verify(DominatorTree::VerificationLevel::Fast));
780
781 // At this point, the code is well formed. We now simplify the unrolled loop,
782 // doing constant propagation and dead code elimination as we go.
783 simplifyLoopAfterUnroll(L, !CompletelyUnroll && ULO.Count > 1, LI, SE, DT, AC,
784 TTI);
785
786 NumCompletelyUnrolled += CompletelyUnroll;
787 ++NumUnrolled;
788
789 Loop *OuterL = L->getParentLoop();
790 // Update LoopInfo if the loop is completely removed.
791 if (CompletelyUnroll)
792 LI->erase(L);
793
794 // LoopInfo should not be valid, confirm that.
795 if (UnrollVerifyLoopInfo)
796 LI->verify(*DT);
797
798 // After complete unrolling most of the blocks should be contained in OuterL.
799 // However, some of them might happen to be out of OuterL (e.g. if they
800 // precede a loop exit). In this case we might need to insert PHI nodes in
801 // order to preserve LCSSA form.
802 // We don't need to check this if we already know that we need to fix LCSSA
803 // form.
804 // TODO: For now we just recompute LCSSA for the outer loop in this case, but
805 // it should be possible to fix it in-place.
806 if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA)
807 NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI);
808
809 // Make sure that loop-simplify form is preserved. We want to simplify
810 // at least one layer outside of the loop that was unrolled so that any
811 // changes to the parent loop exposed by the unrolling are considered.
812 if (OuterL) {
813 // OuterL includes all loops for which we can break loop-simplify, so
814 // it's sufficient to simplify only it (it'll recursively simplify inner
815 // loops too).
816 if (NeedToFixLCSSA) {
817 // LCSSA must be performed on the outermost affected loop. The unrolled
818 // loop's last loop latch is guaranteed to be in the outermost loop
819 // after LoopInfo's been updated by LoopInfo::erase.
820 Loop *LatchLoop = LI->getLoopFor(Latches.back());
821 Loop *FixLCSSALoop = OuterL;
822 if (!FixLCSSALoop->contains(LatchLoop))
823 while (FixLCSSALoop->getParentLoop() != LatchLoop)
824 FixLCSSALoop = FixLCSSALoop->getParentLoop();
825
826 formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE);
827 } else if (PreserveLCSSA) {
828 assert(OuterL->isLCSSAForm(*DT) &&
829 "Loops should be in LCSSA form after loop-unroll.");
830 }
831
832 // TODO: That potentially might be compile-time expensive. We should try
833 // to fix the loop-simplified form incrementally.
834 simplifyLoop(OuterL, DT, LI, SE, AC, nullptr, PreserveLCSSA);
835 } else {
836 // Simplify loops for which we might've broken loop-simplify form.
837 for (Loop *SubLoop : LoopsToSimplify)
838 simplifyLoop(SubLoop, DT, LI, SE, AC, nullptr, PreserveLCSSA);
839 }
840
841 return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled
842 : LoopUnrollResult::PartiallyUnrolled;
843 }
844
845 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata
846 /// node with the given name (for example, "llvm.loop.unroll.count"). If no
847 /// such metadata node exists, then nullptr is returned.
GetUnrollMetadata(MDNode * LoopID,StringRef Name)848 MDNode *llvm::GetUnrollMetadata(MDNode *LoopID, StringRef Name) {
849 // First operand should refer to the loop id itself.
850 assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
851 assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
852
853 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
854 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
855 if (!MD)
856 continue;
857
858 MDString *S = dyn_cast<MDString>(MD->getOperand(0));
859 if (!S)
860 continue;
861
862 if (Name.equals(S->getString()))
863 return MD;
864 }
865 return nullptr;
866 }
867