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/STLExtras.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/ADT/StringRef.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/ADT/ilist_iterator.h"
27 #include "llvm/ADT/iterator_range.h"
28 #include "llvm/Analysis/AssumptionCache.h"
29 #include "llvm/Analysis/DomTreeUpdater.h"
30 #include "llvm/Analysis/InstructionSimplify.h"
31 #include "llvm/Analysis/LoopInfo.h"
32 #include "llvm/Analysis/LoopIterator.h"
33 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
34 #include "llvm/Analysis/ScalarEvolution.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/CFG.h"
37 #include "llvm/IR/Constants.h"
38 #include "llvm/IR/DebugInfoMetadata.h"
39 #include "llvm/IR/DebugLoc.h"
40 #include "llvm/IR/DiagnosticInfo.h"
41 #include "llvm/IR/Dominators.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/Instruction.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/IR/Metadata.h"
47 #include "llvm/IR/Module.h"
48 #include "llvm/IR/Use.h"
49 #include "llvm/IR/User.h"
50 #include "llvm/IR/ValueHandle.h"
51 #include "llvm/IR/ValueMap.h"
52 #include "llvm/Support/Casting.h"
53 #include "llvm/Support/CommandLine.h"
54 #include "llvm/Support/Debug.h"
55 #include "llvm/Support/GenericDomTree.h"
56 #include "llvm/Support/MathExtras.h"
57 #include "llvm/Support/raw_ostream.h"
58 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
59 #include "llvm/Transforms/Utils/Cloning.h"
60 #include "llvm/Transforms/Utils/Local.h"
61 #include "llvm/Transforms/Utils/LoopSimplify.h"
62 #include "llvm/Transforms/Utils/LoopUtils.h"
63 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
64 #include "llvm/Transforms/Utils/UnrollLoop.h"
65 #include "llvm/Transforms/Utils/ValueMapper.h"
66 #include <algorithm>
67 #include <assert.h>
68 #include <numeric>
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 BasicBlock *FirstExitingBlock = nullptr;
325 SmallVector<BasicBlock *> ExitingBlocks;
326 };
327 DenseMap<BasicBlock *, ExitInfo> ExitInfos;
328 SmallVector<BasicBlock *, 4> ExitingBlocks;
329 L->getExitingBlocks(ExitingBlocks);
330 for (auto *ExitingBlock : ExitingBlocks) {
331 // The folding code is not prepared to deal with non-branch instructions
332 // right now.
333 auto *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
334 if (!BI)
335 continue;
336
337 ExitInfo &Info = ExitInfos.try_emplace(ExitingBlock).first->second;
338 Info.TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
339 Info.TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
340 if (Info.TripCount != 0) {
341 Info.BreakoutTrip = Info.TripCount % ULO.Count;
342 Info.TripMultiple = 0;
343 } else {
344 Info.BreakoutTrip = Info.TripMultiple =
345 (unsigned)std::gcd(ULO.Count, Info.TripMultiple);
346 }
347 Info.ExitOnTrue = !L->contains(BI->getSuccessor(0));
348 Info.ExitingBlocks.push_back(ExitingBlock);
349 LLVM_DEBUG(dbgs() << " Exiting block %" << ExitingBlock->getName()
350 << ": TripCount=" << Info.TripCount
351 << ", TripMultiple=" << Info.TripMultiple
352 << ", BreakoutTrip=" << Info.BreakoutTrip << "\n");
353 }
354
355 // Are we eliminating the loop control altogether? Note that we can know
356 // we're eliminating the backedge without knowing exactly which iteration
357 // of the unrolled body exits.
358 const bool CompletelyUnroll = ULO.Count == MaxTripCount;
359
360 const bool PreserveOnlyFirst = CompletelyUnroll && MaxOrZero;
361
362 // There's no point in performing runtime unrolling if this unroll count
363 // results in a full unroll.
364 if (CompletelyUnroll)
365 ULO.Runtime = false;
366
367 // Go through all exits of L and see if there are any phi-nodes there. We just
368 // conservatively assume that they're inserted to preserve LCSSA form, which
369 // means that complete unrolling might break this form. We need to either fix
370 // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For
371 // now we just recompute LCSSA for the outer loop, but it should be possible
372 // to fix it in-place.
373 bool NeedToFixLCSSA =
374 PreserveLCSSA && CompletelyUnroll &&
375 any_of(ExitBlocks,
376 [](const BasicBlock *BB) { return isa<PHINode>(BB->begin()); });
377
378 // The current loop unroll pass can unroll loops that have
379 // (1) single latch; and
380 // (2a) latch is unconditional; or
381 // (2b) latch is conditional and is an exiting block
382 // FIXME: The implementation can be extended to work with more complicated
383 // cases, e.g. loops with multiple latches.
384 BranchInst *LatchBI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
385
386 // A conditional branch which exits the loop, which can be optimized to an
387 // unconditional branch in the unrolled loop in some cases.
388 bool LatchIsExiting = L->isLoopExiting(LatchBlock);
389 if (!LatchBI || (LatchBI->isConditional() && !LatchIsExiting)) {
390 LLVM_DEBUG(
391 dbgs() << "Can't unroll; a conditional latch must exit the loop");
392 return LoopUnrollResult::Unmodified;
393 }
394
395 // Loops containing convergent instructions cannot use runtime unrolling,
396 // as the prologue/epilogue may add additional control-dependencies to
397 // convergent operations.
398 LLVM_DEBUG(
399 {
400 bool HasConvergent = false;
401 for (auto &BB : L->blocks())
402 for (auto &I : *BB)
403 if (auto *CB = dyn_cast<CallBase>(&I))
404 HasConvergent |= CB->isConvergent();
405 assert((!HasConvergent || !ULO.Runtime) &&
406 "Can't runtime unroll if loop contains a convergent operation.");
407 });
408
409 bool EpilogProfitability =
410 UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog
411 : isEpilogProfitable(L);
412
413 if (ULO.Runtime &&
414 !UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount,
415 EpilogProfitability, ULO.UnrollRemainder,
416 ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
417 PreserveLCSSA, RemainderLoop)) {
418 if (ULO.Force)
419 ULO.Runtime = false;
420 else {
421 LLVM_DEBUG(dbgs() << "Won't unroll; remainder loop could not be "
422 "generated when assuming runtime trip count\n");
423 return LoopUnrollResult::Unmodified;
424 }
425 }
426
427 using namespace ore;
428 // Report the unrolling decision.
429 if (CompletelyUnroll) {
430 LLVM_DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
431 << " with trip count " << ULO.Count << "!\n");
432 if (ORE)
433 ORE->emit([&]() {
434 return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(),
435 L->getHeader())
436 << "completely unrolled loop with "
437 << NV("UnrollCount", ULO.Count) << " iterations";
438 });
439 } else {
440 LLVM_DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() << " by "
441 << ULO.Count);
442 if (ULO.Runtime)
443 LLVM_DEBUG(dbgs() << " with run-time trip count");
444 LLVM_DEBUG(dbgs() << "!\n");
445
446 if (ORE)
447 ORE->emit([&]() {
448 OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(),
449 L->getHeader());
450 Diag << "unrolled loop by a factor of " << NV("UnrollCount", ULO.Count);
451 if (ULO.Runtime)
452 Diag << " with run-time trip count";
453 return Diag;
454 });
455 }
456
457 // We are going to make changes to this loop. SCEV may be keeping cached info
458 // about it, in particular about backedge taken count. The changes we make
459 // are guaranteed to invalidate this information for our loop. It is tempting
460 // to only invalidate the loop being unrolled, but it is incorrect as long as
461 // all exiting branches from all inner loops have impact on the outer loops,
462 // and if something changes inside them then any of outer loops may also
463 // change. When we forget outermost loop, we also forget all contained loops
464 // and this is what we need here.
465 if (SE) {
466 if (ULO.ForgetAllSCEV)
467 SE->forgetAllLoops();
468 else {
469 SE->forgetTopmostLoop(L);
470 SE->forgetBlockAndLoopDispositions();
471 }
472 }
473
474 if (!LatchIsExiting)
475 ++NumUnrolledNotLatch;
476
477 // For the first iteration of the loop, we should use the precloned values for
478 // PHI nodes. Insert associations now.
479 ValueToValueMapTy LastValueMap;
480 std::vector<PHINode*> OrigPHINode;
481 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
482 OrigPHINode.push_back(cast<PHINode>(I));
483 }
484
485 std::vector<BasicBlock *> Headers;
486 std::vector<BasicBlock *> Latches;
487 Headers.push_back(Header);
488 Latches.push_back(LatchBlock);
489
490 // The current on-the-fly SSA update requires blocks to be processed in
491 // reverse postorder so that LastValueMap contains the correct value at each
492 // exit.
493 LoopBlocksDFS DFS(L);
494 DFS.perform(LI);
495
496 // Stash the DFS iterators before adding blocks to the loop.
497 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
498 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
499
500 std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks();
501
502 // Loop Unrolling might create new loops. While we do preserve LoopInfo, we
503 // might break loop-simplified form for these loops (as they, e.g., would
504 // share the same exit blocks). We'll keep track of loops for which we can
505 // break this so that later we can re-simplify them.
506 SmallSetVector<Loop *, 4> LoopsToSimplify;
507 for (Loop *SubLoop : *L)
508 LoopsToSimplify.insert(SubLoop);
509
510 // When a FSDiscriminator is enabled, we don't need to add the multiply
511 // factors to the discriminators.
512 if (Header->getParent()->shouldEmitDebugInfoForProfiling() &&
513 !EnableFSDiscriminator)
514 for (BasicBlock *BB : L->getBlocks())
515 for (Instruction &I : *BB)
516 if (!isa<DbgInfoIntrinsic>(&I))
517 if (const DILocation *DIL = I.getDebugLoc()) {
518 auto NewDIL = DIL->cloneByMultiplyingDuplicationFactor(ULO.Count);
519 if (NewDIL)
520 I.setDebugLoc(*NewDIL);
521 else
522 LLVM_DEBUG(dbgs()
523 << "Failed to create new discriminator: "
524 << DIL->getFilename() << " Line: " << DIL->getLine());
525 }
526
527 // Identify what noalias metadata is inside the loop: if it is inside the
528 // loop, the associated metadata must be cloned for each iteration.
529 SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
530 identifyNoAliasScopesToClone(L->getBlocks(), LoopLocalNoAliasDeclScopes);
531
532 // We place the unrolled iterations immediately after the original loop
533 // latch. This is a reasonable default placement if we don't have block
534 // frequencies, and if we do, well the layout will be adjusted later.
535 auto BlockInsertPt = std::next(LatchBlock->getIterator());
536 for (unsigned It = 1; It != ULO.Count; ++It) {
537 SmallVector<BasicBlock *, 8> NewBlocks;
538 SmallDenseMap<const Loop *, Loop *, 4> NewLoops;
539 NewLoops[L] = L;
540
541 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
542 ValueToValueMapTy VMap;
543 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
544 Header->getParent()->insert(BlockInsertPt, New);
545
546 assert((*BB != Header || LI->getLoopFor(*BB) == L) &&
547 "Header should not be in a sub-loop");
548 // Tell LI about New.
549 const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops);
550 if (OldLoop)
551 LoopsToSimplify.insert(NewLoops[OldLoop]);
552
553 if (*BB == Header)
554 // Loop over all of the PHI nodes in the block, changing them to use
555 // the incoming values from the previous block.
556 for (PHINode *OrigPHI : OrigPHINode) {
557 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]);
558 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
559 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
560 if (It > 1 && L->contains(InValI))
561 InVal = LastValueMap[InValI];
562 VMap[OrigPHI] = InVal;
563 NewPHI->eraseFromParent();
564 }
565
566 // Update our running map of newest clones
567 LastValueMap[*BB] = New;
568 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
569 VI != VE; ++VI)
570 LastValueMap[VI->first] = VI->second;
571
572 // Add phi entries for newly created values to all exit blocks.
573 for (BasicBlock *Succ : successors(*BB)) {
574 if (L->contains(Succ))
575 continue;
576 for (PHINode &PHI : Succ->phis()) {
577 Value *Incoming = PHI.getIncomingValueForBlock(*BB);
578 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
579 if (It != LastValueMap.end())
580 Incoming = It->second;
581 PHI.addIncoming(Incoming, New);
582 SE->forgetValue(&PHI);
583 }
584 }
585 // Keep track of new headers and latches as we create them, so that
586 // we can insert the proper branches later.
587 if (*BB == Header)
588 Headers.push_back(New);
589 if (*BB == LatchBlock)
590 Latches.push_back(New);
591
592 // Keep track of the exiting block and its successor block contained in
593 // the loop for the current iteration.
594 auto ExitInfoIt = ExitInfos.find(*BB);
595 if (ExitInfoIt != ExitInfos.end())
596 ExitInfoIt->second.ExitingBlocks.push_back(New);
597
598 NewBlocks.push_back(New);
599 UnrolledLoopBlocks.push_back(New);
600
601 // Update DomTree: since we just copy the loop body, and each copy has a
602 // dedicated entry block (copy of the header block), this header's copy
603 // dominates all copied blocks. That means, dominance relations in the
604 // copied body are the same as in the original body.
605 if (*BB == Header)
606 DT->addNewBlock(New, Latches[It - 1]);
607 else {
608 auto BBDomNode = DT->getNode(*BB);
609 auto BBIDom = BBDomNode->getIDom();
610 BasicBlock *OriginalBBIDom = BBIDom->getBlock();
611 DT->addNewBlock(
612 New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)]));
613 }
614 }
615
616 // Remap all instructions in the most recent iteration
617 remapInstructionsInBlocks(NewBlocks, LastValueMap);
618 for (BasicBlock *NewBlock : NewBlocks)
619 for (Instruction &I : *NewBlock)
620 if (auto *II = dyn_cast<AssumeInst>(&I))
621 AC->registerAssumption(II);
622
623 {
624 // Identify what other metadata depends on the cloned version. After
625 // cloning, replace the metadata with the corrected version for both
626 // memory instructions and noalias intrinsics.
627 std::string ext = (Twine("It") + Twine(It)).str();
628 cloneAndAdaptNoAliasScopes(LoopLocalNoAliasDeclScopes, NewBlocks,
629 Header->getContext(), ext);
630 }
631 }
632
633 // Loop over the PHI nodes in the original block, setting incoming values.
634 for (PHINode *PN : OrigPHINode) {
635 if (CompletelyUnroll) {
636 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
637 PN->eraseFromParent();
638 } else if (ULO.Count > 1) {
639 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
640 // If this value was defined in the loop, take the value defined by the
641 // last iteration of the loop.
642 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
643 if (L->contains(InValI))
644 InVal = LastValueMap[InVal];
645 }
646 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
647 PN->addIncoming(InVal, Latches.back());
648 }
649 }
650
651 // Connect latches of the unrolled iterations to the headers of the next
652 // iteration. Currently they point to the header of the same iteration.
653 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
654 unsigned j = (i + 1) % e;
655 Latches[i]->getTerminator()->replaceSuccessorWith(Headers[i], Headers[j]);
656 }
657
658 // Update dominators of blocks we might reach through exits.
659 // Immediate dominator of such block might change, because we add more
660 // routes which can lead to the exit: we can now reach it from the copied
661 // iterations too.
662 if (ULO.Count > 1) {
663 for (auto *BB : OriginalLoopBlocks) {
664 auto *BBDomNode = DT->getNode(BB);
665 SmallVector<BasicBlock *, 16> ChildrenToUpdate;
666 for (auto *ChildDomNode : BBDomNode->children()) {
667 auto *ChildBB = ChildDomNode->getBlock();
668 if (!L->contains(ChildBB))
669 ChildrenToUpdate.push_back(ChildBB);
670 }
671 // The new idom of the block will be the nearest common dominator
672 // of all copies of the previous idom. This is equivalent to the
673 // nearest common dominator of the previous idom and the first latch,
674 // which dominates all copies of the previous idom.
675 BasicBlock *NewIDom = DT->findNearestCommonDominator(BB, LatchBlock);
676 for (auto *ChildBB : ChildrenToUpdate)
677 DT->changeImmediateDominator(ChildBB, NewIDom);
678 }
679 }
680
681 assert(!UnrollVerifyDomtree ||
682 DT->verify(DominatorTree::VerificationLevel::Fast));
683
684 SmallVector<DominatorTree::UpdateType> DTUpdates;
685 auto SetDest = [&](BasicBlock *Src, bool WillExit, bool ExitOnTrue) {
686 auto *Term = cast<BranchInst>(Src->getTerminator());
687 const unsigned Idx = ExitOnTrue ^ WillExit;
688 BasicBlock *Dest = Term->getSuccessor(Idx);
689 BasicBlock *DeadSucc = Term->getSuccessor(1-Idx);
690
691 // Remove predecessors from all non-Dest successors.
692 DeadSucc->removePredecessor(Src, /* KeepOneInputPHIs */ true);
693
694 // Replace the conditional branch with an unconditional one.
695 BranchInst::Create(Dest, Term);
696 Term->eraseFromParent();
697
698 DTUpdates.emplace_back(DominatorTree::Delete, Src, DeadSucc);
699 };
700
701 auto WillExit = [&](const ExitInfo &Info, unsigned i, unsigned j,
702 bool IsLatch) -> std::optional<bool> {
703 if (CompletelyUnroll) {
704 if (PreserveOnlyFirst) {
705 if (i == 0)
706 return std::nullopt;
707 return j == 0;
708 }
709 // Complete (but possibly inexact) unrolling
710 if (j == 0)
711 return true;
712 if (Info.TripCount && j != Info.TripCount)
713 return false;
714 return std::nullopt;
715 }
716
717 if (ULO.Runtime) {
718 // If runtime unrolling inserts a prologue, information about non-latch
719 // exits may be stale.
720 if (IsLatch && j != 0)
721 return false;
722 return std::nullopt;
723 }
724
725 if (j != Info.BreakoutTrip &&
726 (Info.TripMultiple == 0 || j % Info.TripMultiple != 0)) {
727 // If we know the trip count or a multiple of it, we can safely use an
728 // unconditional branch for some iterations.
729 return false;
730 }
731 return std::nullopt;
732 };
733
734 // Fold branches for iterations where we know that they will exit or not
735 // exit.
736 for (auto &Pair : ExitInfos) {
737 ExitInfo &Info = Pair.second;
738 for (unsigned i = 0, e = Info.ExitingBlocks.size(); i != e; ++i) {
739 // The branch destination.
740 unsigned j = (i + 1) % e;
741 bool IsLatch = Pair.first == LatchBlock;
742 std::optional<bool> KnownWillExit = WillExit(Info, i, j, IsLatch);
743 if (!KnownWillExit) {
744 if (!Info.FirstExitingBlock)
745 Info.FirstExitingBlock = Info.ExitingBlocks[i];
746 continue;
747 }
748
749 // We don't fold known-exiting branches for non-latch exits here,
750 // because this ensures that both all loop blocks and all exit blocks
751 // remain reachable in the CFG.
752 // TODO: We could fold these branches, but it would require much more
753 // sophisticated updates to LoopInfo.
754 if (*KnownWillExit && !IsLatch) {
755 if (!Info.FirstExitingBlock)
756 Info.FirstExitingBlock = Info.ExitingBlocks[i];
757 continue;
758 }
759
760 SetDest(Info.ExitingBlocks[i], *KnownWillExit, Info.ExitOnTrue);
761 }
762 }
763
764 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
765 DomTreeUpdater *DTUToUse = &DTU;
766 if (ExitingBlocks.size() == 1 && ExitInfos.size() == 1) {
767 // Manually update the DT if there's a single exiting node. In that case
768 // there's a single exit node and it is sufficient to update the nodes
769 // immediately dominated by the original exiting block. They will become
770 // dominated by the first exiting block that leaves the loop after
771 // unrolling. Note that the CFG inside the loop does not change, so there's
772 // no need to update the DT inside the unrolled loop.
773 DTUToUse = nullptr;
774 auto &[OriginalExit, Info] = *ExitInfos.begin();
775 if (!Info.FirstExitingBlock)
776 Info.FirstExitingBlock = Info.ExitingBlocks.back();
777 for (auto *C : to_vector(DT->getNode(OriginalExit)->children())) {
778 if (L->contains(C->getBlock()))
779 continue;
780 C->setIDom(DT->getNode(Info.FirstExitingBlock));
781 }
782 } else {
783 DTU.applyUpdates(DTUpdates);
784 }
785
786 // When completely unrolling, the last latch becomes unreachable.
787 if (!LatchIsExiting && CompletelyUnroll) {
788 // There is no need to update the DT here, because there must be a unique
789 // latch. Hence if the latch is not exiting it must directly branch back to
790 // the original loop header and does not dominate any nodes.
791 assert(LatchBlock->getSingleSuccessor() && "Loop with multiple latches?");
792 changeToUnreachable(Latches.back()->getTerminator(), PreserveLCSSA);
793 }
794
795 // Merge adjacent basic blocks, if possible.
796 for (BasicBlock *Latch : Latches) {
797 BranchInst *Term = dyn_cast<BranchInst>(Latch->getTerminator());
798 assert((Term ||
799 (CompletelyUnroll && !LatchIsExiting && Latch == Latches.back())) &&
800 "Need a branch as terminator, except when fully unrolling with "
801 "unconditional latch");
802 if (Term && Term->isUnconditional()) {
803 BasicBlock *Dest = Term->getSuccessor(0);
804 BasicBlock *Fold = Dest->getUniquePredecessor();
805 if (MergeBlockIntoPredecessor(Dest, /*DTU=*/DTUToUse, LI,
806 /*MSSAU=*/nullptr, /*MemDep=*/nullptr,
807 /*PredecessorWithTwoSuccessors=*/false,
808 DTUToUse ? nullptr : DT)) {
809 // Dest has been folded into Fold. Update our worklists accordingly.
810 std::replace(Latches.begin(), Latches.end(), Dest, Fold);
811 llvm::erase_value(UnrolledLoopBlocks, Dest);
812 }
813 }
814 }
815
816 if (DTUToUse) {
817 // Apply updates to the DomTree.
818 DT = &DTU.getDomTree();
819 }
820 assert(!UnrollVerifyDomtree ||
821 DT->verify(DominatorTree::VerificationLevel::Fast));
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 && ULO.Count > 1, LI, SE, DT, AC,
826 TTI);
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 // LoopInfo should not be valid, confirm that.
837 if (UnrollVerifyLoopInfo)
838 LI->verify(*DT);
839
840 // After complete unrolling most of the blocks should be contained in OuterL.
841 // However, some of them might happen to be out of OuterL (e.g. if they
842 // precede a loop exit). In this case we might need to insert PHI nodes in
843 // order to preserve LCSSA form.
844 // We don't need to check this if we already know that we need to fix LCSSA
845 // form.
846 // TODO: For now we just recompute LCSSA for the outer loop in this case, but
847 // it should be possible to fix it in-place.
848 if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA)
849 NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI);
850
851 // Make sure that loop-simplify form is preserved. We want to simplify
852 // at least one layer outside of the loop that was unrolled so that any
853 // changes to the parent loop exposed by the unrolling are considered.
854 if (OuterL) {
855 // OuterL includes all loops for which we can break loop-simplify, so
856 // it's sufficient to simplify only it (it'll recursively simplify inner
857 // loops too).
858 if (NeedToFixLCSSA) {
859 // LCSSA must be performed on the outermost affected loop. The unrolled
860 // loop's last loop latch is guaranteed to be in the outermost loop
861 // after LoopInfo's been updated by LoopInfo::erase.
862 Loop *LatchLoop = LI->getLoopFor(Latches.back());
863 Loop *FixLCSSALoop = OuterL;
864 if (!FixLCSSALoop->contains(LatchLoop))
865 while (FixLCSSALoop->getParentLoop() != LatchLoop)
866 FixLCSSALoop = FixLCSSALoop->getParentLoop();
867
868 formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE);
869 } else if (PreserveLCSSA) {
870 assert(OuterL->isLCSSAForm(*DT) &&
871 "Loops should be in LCSSA form after loop-unroll.");
872 }
873
874 // TODO: That potentially might be compile-time expensive. We should try
875 // to fix the loop-simplified form incrementally.
876 simplifyLoop(OuterL, DT, LI, SE, AC, nullptr, PreserveLCSSA);
877 } else {
878 // Simplify loops for which we might've broken loop-simplify form.
879 for (Loop *SubLoop : LoopsToSimplify)
880 simplifyLoop(SubLoop, DT, LI, SE, AC, nullptr, PreserveLCSSA);
881 }
882
883 return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled
884 : LoopUnrollResult::PartiallyUnrolled;
885 }
886
887 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata
888 /// node with the given name (for example, "llvm.loop.unroll.count"). If no
889 /// such metadata node exists, then nullptr is returned.
GetUnrollMetadata(MDNode * LoopID,StringRef Name)890 MDNode *llvm::GetUnrollMetadata(MDNode *LoopID, StringRef Name) {
891 // First operand should refer to the loop id itself.
892 assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
893 assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
894
895 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
896 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
897 if (!MD)
898 continue;
899
900 MDString *S = dyn_cast<MDString>(MD->getOperand(0));
901 if (!S)
902 continue;
903
904 if (Name.equals(S->getString()))
905 return MD;
906 }
907 return nullptr;
908 }
909