1 //===-- UnrollLoopRuntime.cpp - Runtime Loop unrolling utilities ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements some loop unrolling utilities for loops with run-time
10 // trip counts. See LoopUnroll.cpp for unrolling loops with compile-time
13 // The functions in this file are used to generate extra code when the
14 // run-time trip count modulo the unroll factor is not 0. When this is the
15 // case, we need to generate code to execute these 'left over' iterations.
17 // The current strategy generates an if-then-else sequence prior to the
18 // unrolled loop to execute the 'left over' iterations before or after the
21 //===----------------------------------------------------------------------===//
23 #include "llvm/ADT/Statistic.h"
24 #include "llvm/Analysis/DomTreeUpdater.h"
25 #include "llvm/Analysis/InstructionSimplify.h"
26 #include "llvm/Analysis/LoopIterator.h"
27 #include "llvm/Analysis/ScalarEvolution.h"
28 #include "llvm/Analysis/ValueTracking.h"
29 #include "llvm/IR/BasicBlock.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/MDBuilder.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ProfDataUtils.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
38 #include "llvm/Transforms/Utils/Cloning.h"
39 #include "llvm/Transforms/Utils/Local.h"
40 #include "llvm/Transforms/Utils/LoopUtils.h"
41 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
42 #include "llvm/Transforms/Utils/UnrollLoop.h"
47 #define DEBUG_TYPE "loop-unroll"
49 STATISTIC(NumRuntimeUnrolled
,
50 "Number of loops unrolled with run-time trip counts");
51 static cl::opt
<bool> UnrollRuntimeMultiExit(
52 "unroll-runtime-multi-exit", cl::init(false), cl::Hidden
,
53 cl::desc("Allow runtime unrolling for loops with multiple exits, when "
54 "epilog is generated"));
55 static cl::opt
<bool> UnrollRuntimeOtherExitPredictable(
56 "unroll-runtime-other-exit-predictable", cl::init(false), cl::Hidden
,
57 cl::desc("Assume the non latch exit block to be predictable"));
59 // Probability that the loop trip count is so small that after the prolog
60 // we do not enter the unrolled loop at all.
61 // It is unlikely that the loop trip count is smaller than the unroll factor;
62 // other than that, the choice of constant is not tuned yet.
63 static const uint32_t UnrolledLoopHeaderWeights
[] = {1, 127};
64 // Probability that the loop trip count is so small that we skip the unrolled
65 // loop completely and immediately enter the epilogue loop.
66 // It is unlikely that the loop trip count is smaller than the unroll factor;
67 // other than that, the choice of constant is not tuned yet.
68 static const uint32_t EpilogHeaderWeights
[] = {1, 127};
70 /// Connect the unrolling prolog code to the original loop.
71 /// The unrolling prolog code contains code to execute the
72 /// 'extra' iterations if the run-time trip count modulo the
73 /// unroll count is non-zero.
75 /// This function performs the following:
76 /// - Create PHI nodes at prolog end block to combine values
77 /// that exit the prolog code and jump around the prolog.
78 /// - Add a PHI operand to a PHI node at the loop exit block
79 /// for values that exit the prolog and go around the loop.
80 /// - Branch around the original loop if the trip count is less
81 /// than the unroll factor.
83 static void ConnectProlog(Loop
*L
, Value
*BECount
, unsigned Count
,
84 BasicBlock
*PrologExit
,
85 BasicBlock
*OriginalLoopLatchExit
,
86 BasicBlock
*PreHeader
, BasicBlock
*NewPreHeader
,
87 ValueToValueMapTy
&VMap
, DominatorTree
*DT
,
88 LoopInfo
*LI
, bool PreserveLCSSA
,
89 ScalarEvolution
&SE
) {
90 // Loop structure should be the following:
101 BasicBlock
*Latch
= L
->getLoopLatch();
102 assert(Latch
&& "Loop must have a latch");
103 BasicBlock
*PrologLatch
= cast
<BasicBlock
>(VMap
[Latch
]);
105 // Create a PHI node for each outgoing value from the original loop
106 // (which means it is an outgoing value from the prolog code too).
107 // The new PHI node is inserted in the prolog end basic block.
108 // The new PHI node value is added as an operand of a PHI node in either
109 // the loop header or the loop exit block.
110 for (BasicBlock
*Succ
: successors(Latch
)) {
111 for (PHINode
&PN
: Succ
->phis()) {
112 // Add a new PHI node to the prolog end block and add the
113 // appropriate incoming values.
114 // TODO: This code assumes that the PrologExit (or the LatchExit block for
115 // prolog loop) contains only one predecessor from the loop, i.e. the
116 // PrologLatch. When supporting multiple-exiting block loops, we can have
117 // two or more blocks that have the LatchExit as the target in the
119 PHINode
*NewPN
= PHINode::Create(PN
.getType(), 2, PN
.getName() + ".unr");
120 NewPN
->insertBefore(PrologExit
->getFirstNonPHIIt());
121 // Adding a value to the new PHI node from the original loop preheader.
122 // This is the value that skips all the prolog code.
123 if (L
->contains(&PN
)) {
124 // Succ is loop header.
125 NewPN
->addIncoming(PN
.getIncomingValueForBlock(NewPreHeader
),
128 // Succ is LatchExit.
129 NewPN
->addIncoming(UndefValue::get(PN
.getType()), PreHeader
);
132 Value
*V
= PN
.getIncomingValueForBlock(Latch
);
133 if (Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
134 if (L
->contains(I
)) {
138 // Adding a value to the new PHI node from the last prolog block
140 NewPN
->addIncoming(V
, PrologLatch
);
142 // Update the existing PHI node operand with the value from the
143 // new PHI node. How this is done depends on if the existing
144 // PHI node is in the original loop block, or the exit block.
145 if (L
->contains(&PN
))
146 PN
.setIncomingValueForBlock(NewPreHeader
, NewPN
);
148 PN
.addIncoming(NewPN
, PrologExit
);
153 // Make sure that created prolog loop is in simplified form
154 SmallVector
<BasicBlock
*, 4> PrologExitPreds
;
155 Loop
*PrologLoop
= LI
->getLoopFor(PrologLatch
);
157 for (BasicBlock
*PredBB
: predecessors(PrologExit
))
158 if (PrologLoop
->contains(PredBB
))
159 PrologExitPreds
.push_back(PredBB
);
161 SplitBlockPredecessors(PrologExit
, PrologExitPreds
, ".unr-lcssa", DT
, LI
,
162 nullptr, PreserveLCSSA
);
165 // Create a branch around the original loop, which is taken if there are no
166 // iterations remaining to be executed after running the prologue.
167 Instruction
*InsertPt
= PrologExit
->getTerminator();
168 IRBuilder
<> B(InsertPt
);
170 assert(Count
!= 0 && "nonsensical Count!");
172 // If BECount <u (Count - 1) then (BECount + 1) % Count == (BECount + 1)
173 // This means %xtraiter is (BECount + 1) and all of the iterations of this
174 // loop were executed by the prologue. Note that if BECount <u (Count - 1)
175 // then (BECount + 1) cannot unsigned-overflow.
177 B
.CreateICmpULT(BECount
, ConstantInt::get(BECount
->getType(), Count
- 1));
178 // Split the exit to maintain loop canonicalization guarantees
179 SmallVector
<BasicBlock
*, 4> Preds(predecessors(OriginalLoopLatchExit
));
180 SplitBlockPredecessors(OriginalLoopLatchExit
, Preds
, ".unr-lcssa", DT
, LI
,
181 nullptr, PreserveLCSSA
);
182 // Add the branch to the exit block (around the unrolled loop)
183 MDNode
*BranchWeights
= nullptr;
184 if (hasBranchWeightMD(*Latch
->getTerminator())) {
185 // Assume loop is nearly always entered.
186 MDBuilder
MDB(B
.getContext());
187 BranchWeights
= MDB
.createBranchWeights(UnrolledLoopHeaderWeights
);
189 B
.CreateCondBr(BrLoopExit
, OriginalLoopLatchExit
, NewPreHeader
,
191 InsertPt
->eraseFromParent();
193 auto *NewDom
= DT
->findNearestCommonDominator(OriginalLoopLatchExit
,
195 DT
->changeImmediateDominator(OriginalLoopLatchExit
, NewDom
);
199 /// Connect the unrolling epilog code to the original loop.
200 /// The unrolling epilog code contains code to execute the
201 /// 'extra' iterations if the run-time trip count modulo the
202 /// unroll count is non-zero.
204 /// This function performs the following:
205 /// - Update PHI nodes at the unrolling loop exit and epilog loop exit
206 /// - Create PHI nodes at the unrolling loop exit to combine
207 /// values that exit the unrolling loop code and jump around it.
208 /// - Update PHI operands in the epilog loop by the new PHI nodes
209 /// - Branch around the epilog loop if extra iters (ModVal) is zero.
211 static void ConnectEpilog(Loop
*L
, Value
*ModVal
, BasicBlock
*NewExit
,
212 BasicBlock
*Exit
, BasicBlock
*PreHeader
,
213 BasicBlock
*EpilogPreHeader
, BasicBlock
*NewPreHeader
,
214 ValueToValueMapTy
&VMap
, DominatorTree
*DT
,
215 LoopInfo
*LI
, bool PreserveLCSSA
, ScalarEvolution
&SE
,
217 BasicBlock
*Latch
= L
->getLoopLatch();
218 assert(Latch
&& "Loop must have a latch");
219 BasicBlock
*EpilogLatch
= cast
<BasicBlock
>(VMap
[Latch
]);
221 // Loop structure should be the following:
235 // Update PHI nodes at NewExit and Exit.
236 for (PHINode
&PN
: NewExit
->phis()) {
237 // PN should be used in another PHI located in Exit block as
238 // Exit was split by SplitBlockPredecessors into Exit and NewExit
239 // Basically it should look like:
241 // PN = PHI [I, Latch]
244 // EpilogPN = PHI [PN, EpilogPreHeader], [X, Exit2], [Y, Exit2.epil]
246 // Exits from non-latch blocks point to the original exit block and the
247 // epilogue edges have already been added.
249 // There is EpilogPreHeader incoming block instead of NewExit as
250 // NewExit was spilt 1 more time to get EpilogPreHeader.
251 assert(PN
.hasOneUse() && "The phi should have 1 use");
252 PHINode
*EpilogPN
= cast
<PHINode
>(PN
.use_begin()->getUser());
253 assert(EpilogPN
->getParent() == Exit
&& "EpilogPN should be in Exit block");
255 // Add incoming PreHeader from branch around the Loop
256 PN
.addIncoming(UndefValue::get(PN
.getType()), PreHeader
);
259 Value
*V
= PN
.getIncomingValueForBlock(Latch
);
260 Instruction
*I
= dyn_cast
<Instruction
>(V
);
261 if (I
&& L
->contains(I
))
262 // If value comes from an instruction in the loop add VMap value.
264 // For the instruction out of the loop, constant or undefined value
265 // insert value itself.
266 EpilogPN
->addIncoming(V
, EpilogLatch
);
268 assert(EpilogPN
->getBasicBlockIndex(EpilogPreHeader
) >= 0 &&
269 "EpilogPN should have EpilogPreHeader incoming block");
270 // Change EpilogPreHeader incoming block to NewExit.
271 EpilogPN
->setIncomingBlock(EpilogPN
->getBasicBlockIndex(EpilogPreHeader
),
273 // Now PHIs should look like:
275 // PN = PHI [I, Latch], [undef, PreHeader]
278 // EpilogPN = PHI [PN, NewExit], [VMap[I], EpilogLatch]
281 // Create PHI nodes at NewExit (from the unrolling loop Latch and PreHeader).
282 // Update corresponding PHI nodes in epilog loop.
283 for (BasicBlock
*Succ
: successors(Latch
)) {
284 // Skip this as we already updated phis in exit blocks.
285 if (!L
->contains(Succ
))
287 for (PHINode
&PN
: Succ
->phis()) {
288 // Add new PHI nodes to the loop exit block and update epilog
289 // PHIs with the new PHI values.
290 PHINode
*NewPN
= PHINode::Create(PN
.getType(), 2, PN
.getName() + ".unr");
291 NewPN
->insertBefore(NewExit
->getFirstNonPHIIt());
292 // Adding a value to the new PHI node from the unrolling loop preheader.
293 NewPN
->addIncoming(PN
.getIncomingValueForBlock(NewPreHeader
), PreHeader
);
294 // Adding a value to the new PHI node from the unrolling loop latch.
295 NewPN
->addIncoming(PN
.getIncomingValueForBlock(Latch
), Latch
);
297 // Update the existing PHI node operand with the value from the new PHI
298 // node. Corresponding instruction in epilog loop should be PHI.
299 PHINode
*VPN
= cast
<PHINode
>(VMap
[&PN
]);
300 VPN
->setIncomingValueForBlock(EpilogPreHeader
, NewPN
);
304 Instruction
*InsertPt
= NewExit
->getTerminator();
305 IRBuilder
<> B(InsertPt
);
306 Value
*BrLoopExit
= B
.CreateIsNotNull(ModVal
, "lcmp.mod");
307 assert(Exit
&& "Loop must have a single exit block only");
308 // Split the epilogue exit to maintain loop canonicalization guarantees
309 SmallVector
<BasicBlock
*, 4> Preds(predecessors(Exit
));
310 SplitBlockPredecessors(Exit
, Preds
, ".epilog-lcssa", DT
, LI
, nullptr,
312 // Add the branch to the exit block (around the unrolling loop)
313 MDNode
*BranchWeights
= nullptr;
314 if (hasBranchWeightMD(*Latch
->getTerminator())) {
315 // Assume equal distribution in interval [0, Count).
316 MDBuilder
MDB(B
.getContext());
317 BranchWeights
= MDB
.createBranchWeights(1, Count
- 1);
319 B
.CreateCondBr(BrLoopExit
, EpilogPreHeader
, Exit
, BranchWeights
);
320 InsertPt
->eraseFromParent();
322 auto *NewDom
= DT
->findNearestCommonDominator(Exit
, NewExit
);
323 DT
->changeImmediateDominator(Exit
, NewDom
);
326 // Split the main loop exit to maintain canonicalization guarantees.
327 SmallVector
<BasicBlock
*, 4> NewExitPreds
{Latch
};
328 SplitBlockPredecessors(NewExit
, NewExitPreds
, ".loopexit", DT
, LI
, nullptr,
332 /// Create a clone of the blocks in a loop and connect them together. A new
333 /// loop will be created including all cloned blocks, and the iterator of the
334 /// new loop switched to count NewIter down to 0.
335 /// The cloned blocks should be inserted between InsertTop and InsertBot.
336 /// InsertTop should be new preheader, InsertBot new loop exit.
337 /// Returns the new cloned loop that is created.
339 CloneLoopBlocks(Loop
*L
, Value
*NewIter
, const bool UseEpilogRemainder
,
340 const bool UnrollRemainder
,
341 BasicBlock
*InsertTop
,
342 BasicBlock
*InsertBot
, BasicBlock
*Preheader
,
343 std::vector
<BasicBlock
*> &NewBlocks
,
344 LoopBlocksDFS
&LoopBlocks
, ValueToValueMapTy
&VMap
,
345 DominatorTree
*DT
, LoopInfo
*LI
, unsigned Count
) {
346 StringRef suffix
= UseEpilogRemainder
? "epil" : "prol";
347 BasicBlock
*Header
= L
->getHeader();
348 BasicBlock
*Latch
= L
->getLoopLatch();
349 Function
*F
= Header
->getParent();
350 LoopBlocksDFS::RPOIterator BlockBegin
= LoopBlocks
.beginRPO();
351 LoopBlocksDFS::RPOIterator BlockEnd
= LoopBlocks
.endRPO();
352 Loop
*ParentLoop
= L
->getParentLoop();
353 NewLoopsMap NewLoops
;
354 NewLoops
[ParentLoop
] = ParentLoop
;
356 // For each block in the original loop, create a new copy,
357 // and update the value map with the newly created values.
358 for (LoopBlocksDFS::RPOIterator BB
= BlockBegin
; BB
!= BlockEnd
; ++BB
) {
359 BasicBlock
*NewBB
= CloneBasicBlock(*BB
, VMap
, "." + suffix
, F
);
360 NewBlocks
.push_back(NewBB
);
362 addClonedBlockToLoopInfo(*BB
, NewBB
, LI
, NewLoops
);
366 // For the first block, add a CFG connection to this newly
368 InsertTop
->getTerminator()->setSuccessor(0, NewBB
);
373 // The header is dominated by the preheader.
374 DT
->addNewBlock(NewBB
, InsertTop
);
376 // Copy information from original loop to unrolled loop.
377 BasicBlock
*IDomBB
= DT
->getNode(*BB
)->getIDom()->getBlock();
378 DT
->addNewBlock(NewBB
, cast
<BasicBlock
>(VMap
[IDomBB
]));
383 // For the last block, create a loop back to cloned head.
384 VMap
.erase((*BB
)->getTerminator());
385 // Use an incrementing IV. Pre-incr/post-incr is backedge/trip count.
386 // Subtle: NewIter can be 0 if we wrapped when computing the trip count,
387 // thus we must compare the post-increment (wrapping) value.
388 BasicBlock
*FirstLoopBB
= cast
<BasicBlock
>(VMap
[Header
]);
389 BranchInst
*LatchBR
= cast
<BranchInst
>(NewBB
->getTerminator());
390 IRBuilder
<> Builder(LatchBR
);
392 PHINode::Create(NewIter
->getType(), 2, suffix
+ ".iter");
393 NewIdx
->insertBefore(FirstLoopBB
->getFirstNonPHIIt());
394 auto *Zero
= ConstantInt::get(NewIdx
->getType(), 0);
395 auto *One
= ConstantInt::get(NewIdx
->getType(), 1);
397 Builder
.CreateAdd(NewIdx
, One
, NewIdx
->getName() + ".next");
398 Value
*IdxCmp
= Builder
.CreateICmpNE(IdxNext
, NewIter
, NewIdx
->getName() + ".cmp");
399 MDNode
*BranchWeights
= nullptr;
400 if (hasBranchWeightMD(*LatchBR
)) {
402 uint32_t BackEdgeWeight
;
404 // Note: We do not enter this loop for zero-remainders. The check
405 // is at the end of the loop. We assume equal distribution between
406 // possible remainders in [1, Count).
408 BackEdgeWeight
= (Count
- 2) / 2;
410 // Unnecessary backedge, should never be taken. The conditional
411 // jump should be optimized away later.
415 MDBuilder
MDB(Builder
.getContext());
416 BranchWeights
= MDB
.createBranchWeights(BackEdgeWeight
, ExitWeight
);
418 Builder
.CreateCondBr(IdxCmp
, FirstLoopBB
, InsertBot
, BranchWeights
);
419 NewIdx
->addIncoming(Zero
, InsertTop
);
420 NewIdx
->addIncoming(IdxNext
, NewBB
);
421 LatchBR
->eraseFromParent();
425 // Change the incoming values to the ones defined in the preheader or
427 for (BasicBlock::iterator I
= Header
->begin(); isa
<PHINode
>(I
); ++I
) {
428 PHINode
*NewPHI
= cast
<PHINode
>(VMap
[&*I
]);
429 unsigned idx
= NewPHI
->getBasicBlockIndex(Preheader
);
430 NewPHI
->setIncomingBlock(idx
, InsertTop
);
431 BasicBlock
*NewLatch
= cast
<BasicBlock
>(VMap
[Latch
]);
432 idx
= NewPHI
->getBasicBlockIndex(Latch
);
433 Value
*InVal
= NewPHI
->getIncomingValue(idx
);
434 NewPHI
->setIncomingBlock(idx
, NewLatch
);
435 if (Value
*V
= VMap
.lookup(InVal
))
436 NewPHI
->setIncomingValue(idx
, V
);
439 Loop
*NewLoop
= NewLoops
[L
];
440 assert(NewLoop
&& "L should have been cloned");
441 MDNode
*LoopID
= NewLoop
->getLoopID();
443 // Only add loop metadata if the loop is not going to be completely
448 std::optional
<MDNode
*> NewLoopID
= makeFollowupLoopID(
449 LoopID
, {LLVMLoopUnrollFollowupAll
, LLVMLoopUnrollFollowupRemainder
});
451 NewLoop
->setLoopID(*NewLoopID
);
453 // Do not setLoopAlreadyUnrolled if loop attributes have been defined
458 // Add unroll disable metadata to disable future unrolling for this loop.
459 NewLoop
->setLoopAlreadyUnrolled();
463 /// Returns true if we can profitably unroll the multi-exit loop L. Currently,
464 /// we return true only if UnrollRuntimeMultiExit is set to true.
465 static bool canProfitablyUnrollMultiExitLoop(
466 Loop
*L
, SmallVectorImpl
<BasicBlock
*> &OtherExits
, BasicBlock
*LatchExit
,
467 bool UseEpilogRemainder
) {
469 // Priority goes to UnrollRuntimeMultiExit if it's supplied.
470 if (UnrollRuntimeMultiExit
.getNumOccurrences())
471 return UnrollRuntimeMultiExit
;
473 // The main pain point with multi-exit loop unrolling is that once unrolled,
474 // we will not be able to merge all blocks into a straight line code.
475 // There are branches within the unrolled loop that go to the OtherExits.
476 // The second point is the increase in code size, but this is true
477 // irrespective of multiple exits.
479 // Note: Both the heuristics below are coarse grained. We are essentially
480 // enabling unrolling of loops that have a single side exit other than the
481 // normal LatchExit (i.e. exiting into a deoptimize block).
482 // The heuristics considered are:
483 // 1. low number of branches in the unrolled version.
484 // 2. high predictability of these extra branches.
485 // We avoid unrolling loops that have more than two exiting blocks. This
486 // limits the total number of branches in the unrolled loop to be atmost
487 // the unroll factor (since one of the exiting blocks is the latch block).
488 SmallVector
<BasicBlock
*, 4> ExitingBlocks
;
489 L
->getExitingBlocks(ExitingBlocks
);
490 if (ExitingBlocks
.size() > 2)
493 // Allow unrolling of loops with no non latch exit blocks.
494 if (OtherExits
.size() == 0)
497 // The second heuristic is that L has one exit other than the latchexit and
498 // that exit is a deoptimize block. We know that deoptimize blocks are rarely
499 // taken, which also implies the branch leading to the deoptimize block is
500 // highly predictable. When UnrollRuntimeOtherExitPredictable is specified, we
501 // assume the other exit branch is predictable even if it has no deoptimize
503 return (OtherExits
.size() == 1 &&
504 (UnrollRuntimeOtherExitPredictable
||
505 OtherExits
[0]->getPostdominatingDeoptimizeCall()));
506 // TODO: These can be fine-tuned further to consider code size or deopt states
507 // that are captured by the deoptimize exit block.
508 // Also, we can extend this to support more cases, if we actually
509 // know of kinds of multiexit loops that would benefit from unrolling.
512 /// Calculate ModVal = (BECount + 1) % Count on the abstract integer domain
513 /// accounting for the possibility of unsigned overflow in the 2s complement
514 /// domain. Preconditions:
515 /// 1) TripCount = BECount + 1 (allowing overflow)
516 /// 2) Log2(Count) <= BitWidth(BECount)
517 static Value
*CreateTripRemainder(IRBuilder
<> &B
, Value
*BECount
,
518 Value
*TripCount
, unsigned Count
) {
519 // Note that TripCount is BECount + 1.
520 if (isPowerOf2_32(Count
))
521 // If the expression is zero, then either:
522 // 1. There are no iterations to be run in the prolog/epilog loop.
524 // 2. The addition computing TripCount overflowed.
526 // If (2) is true, we know that TripCount really is (1 << BEWidth) and so
527 // the number of iterations that remain to be run in the original loop is a
528 // multiple Count == (1 << Log2(Count)) because Log2(Count) <= BEWidth (a
529 // precondition of this method).
530 return B
.CreateAnd(TripCount
, Count
- 1, "xtraiter");
532 // As (BECount + 1) can potentially unsigned overflow we count
533 // (BECount % Count) + 1 which is overflow safe as BECount % Count < Count.
534 Constant
*CountC
= ConstantInt::get(BECount
->getType(), Count
);
535 Value
*ModValTmp
= B
.CreateURem(BECount
, CountC
);
536 Value
*ModValAdd
= B
.CreateAdd(ModValTmp
,
537 ConstantInt::get(ModValTmp
->getType(), 1));
538 // At that point (BECount % Count) + 1 could be equal to Count.
539 // To handle this case we need to take mod by Count one more time.
540 return B
.CreateURem(ModValAdd
, CountC
, "xtraiter");
544 /// Insert code in the prolog/epilog code when unrolling a loop with a
545 /// run-time trip-count.
547 /// This method assumes that the loop unroll factor is total number
548 /// of loop bodies in the loop after unrolling. (Some folks refer
549 /// to the unroll factor as the number of *extra* copies added).
550 /// We assume also that the loop unroll factor is a power-of-two. So, after
551 /// unrolling the loop, the number of loop bodies executed is 2,
552 /// 4, 8, etc. Note - LLVM converts the if-then-sequence to a switch
553 /// instruction in SimplifyCFG.cpp. Then, the backend decides how code for
554 /// the switch instruction is generated.
556 /// ***Prolog case***
557 /// extraiters = tripcount % loopfactor
558 /// if (extraiters == 0) jump Loop:
561 /// extraiters -= 1 // Omitted if unroll factor is 2.
562 /// if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
563 /// if (tripcount < loopfactor) jump End:
568 /// ***Epilog case***
569 /// extraiters = tripcount % loopfactor
570 /// if (tripcount < loopfactor) jump LoopExit:
571 /// unroll_iters = tripcount - extraiters
572 /// Loop: LoopBody; (executes unroll_iter times);
574 /// if (unroll_iter != 0) jump Loop:
576 /// if (extraiters == 0) jump EpilExit:
577 /// Epil: LoopBody; (executes extraiters times)
578 /// extraiters -= 1 // Omitted if unroll factor is 2.
579 /// if (extraiters != 0) jump Epil: // Omitted if unroll factor is 2.
582 bool llvm::UnrollRuntimeLoopRemainder(
583 Loop
*L
, unsigned Count
, bool AllowExpensiveTripCount
,
584 bool UseEpilogRemainder
, bool UnrollRemainder
, bool ForgetAllSCEV
,
585 LoopInfo
*LI
, ScalarEvolution
*SE
, DominatorTree
*DT
, AssumptionCache
*AC
,
586 const TargetTransformInfo
*TTI
, bool PreserveLCSSA
, Loop
**ResultLoop
) {
587 LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
588 LLVM_DEBUG(L
->dump());
589 LLVM_DEBUG(UseEpilogRemainder
? dbgs() << "Using epilog remainder.\n"
590 : dbgs() << "Using prolog remainder.\n");
592 // Make sure the loop is in canonical form.
593 if (!L
->isLoopSimplifyForm()) {
594 LLVM_DEBUG(dbgs() << "Not in simplify form!\n");
598 // Guaranteed by LoopSimplifyForm.
599 BasicBlock
*Latch
= L
->getLoopLatch();
600 BasicBlock
*Header
= L
->getHeader();
602 BranchInst
*LatchBR
= cast
<BranchInst
>(Latch
->getTerminator());
604 if (!LatchBR
|| LatchBR
->isUnconditional()) {
605 // The loop-rotate pass can be helpful to avoid this in many cases.
608 << "Loop latch not terminated by a conditional branch.\n");
612 unsigned ExitIndex
= LatchBR
->getSuccessor(0) == Header
? 1 : 0;
613 BasicBlock
*LatchExit
= LatchBR
->getSuccessor(ExitIndex
);
615 if (L
->contains(LatchExit
)) {
616 // Cloning the loop basic blocks (`CloneLoopBlocks`) requires that one of the
617 // targets of the Latch be an exit block out of the loop.
620 << "One of the loop latch successors must be the exit block.\n");
624 // These are exit blocks other than the target of the latch exiting block.
625 SmallVector
<BasicBlock
*, 4> OtherExits
;
626 L
->getUniqueNonLatchExitBlocks(OtherExits
);
627 // Support only single exit and exiting block unless multi-exit loop
628 // unrolling is enabled.
629 if (!L
->getExitingBlock() || OtherExits
.size()) {
630 // We rely on LCSSA form being preserved when the exit blocks are transformed.
631 // (Note that only an off-by-default mode of the old PM disables PreserveLCCA.)
635 if (!canProfitablyUnrollMultiExitLoop(L
, OtherExits
, LatchExit
,
636 UseEpilogRemainder
)) {
639 << "Multiple exit/exiting blocks in loop and multi-exit unrolling not "
644 // Use Scalar Evolution to compute the trip count. This allows more loops to
645 // be unrolled than relying on induction var simplification.
649 // Only unroll loops with a computable trip count.
650 // We calculate the backedge count by using getExitCount on the Latch block,
651 // which is proven to be the only exiting block in this loop. This is same as
652 // calculating getBackedgeTakenCount on the loop (which computes SCEV for all
654 const SCEV
*BECountSC
= SE
->getExitCount(L
, Latch
);
655 if (isa
<SCEVCouldNotCompute
>(BECountSC
)) {
656 LLVM_DEBUG(dbgs() << "Could not compute exit block SCEV\n");
660 unsigned BEWidth
= cast
<IntegerType
>(BECountSC
->getType())->getBitWidth();
662 // Add 1 since the backedge count doesn't include the first loop iteration.
663 // (Note that overflow can occur, this is handled explicitly below)
664 const SCEV
*TripCountSC
=
665 SE
->getAddExpr(BECountSC
, SE
->getConstant(BECountSC
->getType(), 1));
666 if (isa
<SCEVCouldNotCompute
>(TripCountSC
)) {
667 LLVM_DEBUG(dbgs() << "Could not compute trip count SCEV.\n");
671 BasicBlock
*PreHeader
= L
->getLoopPreheader();
672 BranchInst
*PreHeaderBR
= cast
<BranchInst
>(PreHeader
->getTerminator());
673 const DataLayout
&DL
= Header
->getModule()->getDataLayout();
674 SCEVExpander
Expander(*SE
, DL
, "loop-unroll");
675 if (!AllowExpensiveTripCount
&&
676 Expander
.isHighCostExpansion(TripCountSC
, L
, SCEVCheapExpansionBudget
,
678 LLVM_DEBUG(dbgs() << "High cost for expanding trip count scev!\n");
682 // This constraint lets us deal with an overflowing trip count easily; see the
683 // comment on ModVal below.
684 if (Log2_32(Count
) > BEWidth
) {
687 << "Count failed constraint on overflow trip count calculation.\n");
691 // Loop structure is the following:
699 BasicBlock
*NewPreHeader
;
700 BasicBlock
*NewExit
= nullptr;
701 BasicBlock
*PrologExit
= nullptr;
702 BasicBlock
*EpilogPreHeader
= nullptr;
703 BasicBlock
*PrologPreHeader
= nullptr;
705 if (UseEpilogRemainder
) {
706 // If epilog remainder
707 // Split PreHeader to insert a branch around loop for unrolling.
708 NewPreHeader
= SplitBlock(PreHeader
, PreHeader
->getTerminator(), DT
, LI
);
709 NewPreHeader
->setName(PreHeader
->getName() + ".new");
710 // Split LatchExit to create phi nodes from branch above.
711 NewExit
= SplitBlockPredecessors(LatchExit
, {Latch
}, ".unr-lcssa", DT
, LI
,
712 nullptr, PreserveLCSSA
);
713 // NewExit gets its DebugLoc from LatchExit, which is not part of the
715 // Fix this by setting Loop's DebugLoc to NewExit.
716 auto *NewExitTerminator
= NewExit
->getTerminator();
717 NewExitTerminator
->setDebugLoc(Header
->getTerminator()->getDebugLoc());
718 // Split NewExit to insert epilog remainder loop.
719 EpilogPreHeader
= SplitBlock(NewExit
, NewExitTerminator
, DT
, LI
);
720 EpilogPreHeader
->setName(Header
->getName() + ".epil.preheader");
722 // If the latch exits from multiple level of nested loops, then
723 // by assumption there must be another loop exit which branches to the
724 // outer loop and we must adjust the loop for the newly inserted blocks
725 // to account for the fact that our epilogue is still in the same outer
726 // loop. Note that this leaves loopinfo temporarily out of sync with the
727 // CFG until the actual epilogue loop is inserted.
728 if (auto *ParentL
= L
->getParentLoop())
729 if (LI
->getLoopFor(LatchExit
) != ParentL
) {
730 LI
->removeBlock(NewExit
);
731 ParentL
->addBasicBlockToLoop(NewExit
, *LI
);
732 LI
->removeBlock(EpilogPreHeader
);
733 ParentL
->addBasicBlockToLoop(EpilogPreHeader
, *LI
);
737 // If prolog remainder
738 // Split the original preheader twice to insert prolog remainder loop
739 PrologPreHeader
= SplitEdge(PreHeader
, Header
, DT
, LI
);
740 PrologPreHeader
->setName(Header
->getName() + ".prol.preheader");
741 PrologExit
= SplitBlock(PrologPreHeader
, PrologPreHeader
->getTerminator(),
743 PrologExit
->setName(Header
->getName() + ".prol.loopexit");
744 // Split PrologExit to get NewPreHeader.
745 NewPreHeader
= SplitBlock(PrologExit
, PrologExit
->getTerminator(), DT
, LI
);
746 NewPreHeader
->setName(PreHeader
->getName() + ".new");
748 // Loop structure should be the following:
751 // PreHeader PreHeader
752 // *NewPreHeader *PrologPreHeader
753 // Header *PrologExit
757 // *EpilogPreHeader Latch
758 // LatchExit LatchExit
760 // Calculate conditions for branch around loop for unrolling
761 // in epilog case and around prolog remainder loop in prolog case.
762 // Compute the number of extra iterations required, which is:
763 // extra iterations = run-time trip count % loop unroll factor
764 PreHeaderBR
= cast
<BranchInst
>(PreHeader
->getTerminator());
765 IRBuilder
<> B(PreHeaderBR
);
766 Value
*TripCount
= Expander
.expandCodeFor(TripCountSC
, TripCountSC
->getType(),
769 // If there are other exits before the latch, that may cause the latch exit
770 // branch to never be executed, and the latch exit count may be poison.
771 // In this case, freeze the TripCount and base BECount on the frozen
772 // TripCount. We will introduce two branches using these values, and it's
773 // important that they see a consistent value (which would not be guaranteed
774 // if were frozen independently.)
775 if ((!OtherExits
.empty() || !SE
->loopHasNoAbnormalExits(L
)) &&
776 !isGuaranteedNotToBeUndefOrPoison(TripCount
, AC
, PreHeaderBR
, DT
)) {
777 TripCount
= B
.CreateFreeze(TripCount
);
779 B
.CreateAdd(TripCount
, ConstantInt::get(TripCount
->getType(), -1));
781 // If we don't need to freeze, use SCEVExpander for BECount as well, to
782 // allow slightly better value reuse.
784 Expander
.expandCodeFor(BECountSC
, BECountSC
->getType(), PreHeaderBR
);
787 Value
* const ModVal
= CreateTripRemainder(B
, BECount
, TripCount
, Count
);
790 UseEpilogRemainder
? B
.CreateICmpULT(BECount
,
791 ConstantInt::get(BECount
->getType(),
793 B
.CreateIsNotNull(ModVal
, "lcmp.mod");
794 BasicBlock
*RemainderLoop
= UseEpilogRemainder
? NewExit
: PrologPreHeader
;
795 BasicBlock
*UnrollingLoop
= UseEpilogRemainder
? NewPreHeader
: PrologExit
;
796 // Branch to either remainder (extra iterations) loop or unrolling loop.
797 MDNode
*BranchWeights
= nullptr;
798 if (hasBranchWeightMD(*Latch
->getTerminator())) {
799 // Assume loop is nearly always entered.
800 MDBuilder
MDB(B
.getContext());
801 BranchWeights
= MDB
.createBranchWeights(EpilogHeaderWeights
);
803 B
.CreateCondBr(BranchVal
, RemainderLoop
, UnrollingLoop
, BranchWeights
);
804 PreHeaderBR
->eraseFromParent();
806 if (UseEpilogRemainder
)
807 DT
->changeImmediateDominator(NewExit
, PreHeader
);
809 DT
->changeImmediateDominator(PrologExit
, PreHeader
);
811 Function
*F
= Header
->getParent();
812 // Get an ordered list of blocks in the loop to help with the ordering of the
813 // cloned blocks in the prolog/epilog code
814 LoopBlocksDFS
LoopBlocks(L
);
815 LoopBlocks
.perform(LI
);
818 // For each extra loop iteration, create a copy of the loop's basic blocks
819 // and generate a condition that branches to the copy depending on the
820 // number of 'left over' iterations.
822 std::vector
<BasicBlock
*> NewBlocks
;
823 ValueToValueMapTy VMap
;
825 // Clone all the basic blocks in the loop. If Count is 2, we don't clone
826 // the loop, otherwise we create a cloned loop to execute the extra
827 // iterations. This function adds the appropriate CFG connections.
828 BasicBlock
*InsertBot
= UseEpilogRemainder
? LatchExit
: PrologExit
;
829 BasicBlock
*InsertTop
= UseEpilogRemainder
? EpilogPreHeader
: PrologPreHeader
;
830 Loop
*remainderLoop
= CloneLoopBlocks(
831 L
, ModVal
, UseEpilogRemainder
, UnrollRemainder
, InsertTop
, InsertBot
,
832 NewPreHeader
, NewBlocks
, LoopBlocks
, VMap
, DT
, LI
, Count
);
834 // Insert the cloned blocks into the function.
835 F
->splice(InsertBot
->getIterator(), F
, NewBlocks
[0]->getIterator(), F
->end());
837 // Now the loop blocks are cloned and the other exiting blocks from the
838 // remainder are connected to the original Loop's exit blocks. The remaining
839 // work is to update the phi nodes in the original loop, and take in the
840 // values from the cloned region.
841 for (auto *BB
: OtherExits
) {
842 // Given we preserve LCSSA form, we know that the values used outside the
843 // loop will be used through these phi nodes at the exit blocks that are
844 // transformed below.
845 for (PHINode
&PN
: BB
->phis()) {
846 unsigned oldNumOperands
= PN
.getNumIncomingValues();
847 // Add the incoming values from the remainder code to the end of the phi
849 for (unsigned i
= 0; i
< oldNumOperands
; i
++){
850 auto *PredBB
=PN
.getIncomingBlock(i
);
852 // The latch exit is handled seperately, see connectX
854 if (!L
->contains(PredBB
))
855 // Even if we had dedicated exits, the code above inserted an
856 // extra branch which can reach the latch exit.
859 auto *V
= PN
.getIncomingValue(i
);
860 if (Instruction
*I
= dyn_cast
<Instruction
>(V
))
863 PN
.addIncoming(V
, cast
<BasicBlock
>(VMap
[PredBB
]));
866 #if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
867 for (BasicBlock
*SuccBB
: successors(BB
)) {
868 assert(!(llvm::is_contained(OtherExits
, SuccBB
) || SuccBB
== LatchExit
) &&
869 "Breaks the definition of dedicated exits!");
874 // Update the immediate dominator of the exit blocks and blocks that are
875 // reachable from the exit blocks. This is needed because we now have paths
876 // from both the original loop and the remainder code reaching the exit
877 // blocks. While the IDom of these exit blocks were from the original loop,
878 // now the IDom is the preheader (which decides whether the original loop or
879 // remainder code should run).
880 if (DT
&& !L
->getExitingBlock()) {
881 SmallVector
<BasicBlock
*, 16> ChildrenToUpdate
;
882 // NB! We have to examine the dom children of all loop blocks, not just
883 // those which are the IDom of the exit blocks. This is because blocks
884 // reachable from the exit blocks can have their IDom as the nearest common
885 // dominator of the exit blocks.
886 for (auto *BB
: L
->blocks()) {
887 auto *DomNodeBB
= DT
->getNode(BB
);
888 for (auto *DomChild
: DomNodeBB
->children()) {
889 auto *DomChildBB
= DomChild
->getBlock();
890 if (!L
->contains(LI
->getLoopFor(DomChildBB
)))
891 ChildrenToUpdate
.push_back(DomChildBB
);
894 for (auto *BB
: ChildrenToUpdate
)
895 DT
->changeImmediateDominator(BB
, PreHeader
);
898 // Loop structure should be the following:
901 // PreHeader PreHeader
902 // NewPreHeader PrologPreHeader
903 // Header PrologHeader
906 // NewExit PrologExit
907 // EpilogPreHeader NewPreHeader
908 // EpilogHeader Header
911 // LatchExit LatchExit
913 // Rewrite the cloned instruction operands to use the values created when the
915 for (BasicBlock
*BB
: NewBlocks
) {
916 Module
*M
= BB
->getModule();
917 for (Instruction
&I
: *BB
) {
918 RemapInstruction(&I
, VMap
,
919 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
920 RemapDPValueRange(M
, I
.getDbgValueRange(), VMap
,
921 RF_NoModuleLevelChanges
| RF_IgnoreMissingLocals
);
925 if (UseEpilogRemainder
) {
926 // Connect the epilog code to the original loop and update the
928 ConnectEpilog(L
, ModVal
, NewExit
, LatchExit
, PreHeader
, EpilogPreHeader
,
929 NewPreHeader
, VMap
, DT
, LI
, PreserveLCSSA
, *SE
, Count
);
931 // Update counter in loop for unrolling.
932 // Use an incrementing IV. Pre-incr/post-incr is backedge/trip count.
933 // Subtle: TestVal can be 0 if we wrapped when computing the trip count,
934 // thus we must compare the post-increment (wrapping) value.
935 IRBuilder
<> B2(NewPreHeader
->getTerminator());
936 Value
*TestVal
= B2
.CreateSub(TripCount
, ModVal
, "unroll_iter");
937 BranchInst
*LatchBR
= cast
<BranchInst
>(Latch
->getTerminator());
938 PHINode
*NewIdx
= PHINode::Create(TestVal
->getType(), 2, "niter");
939 NewIdx
->insertBefore(Header
->getFirstNonPHIIt());
940 B2
.SetInsertPoint(LatchBR
);
941 auto *Zero
= ConstantInt::get(NewIdx
->getType(), 0);
942 auto *One
= ConstantInt::get(NewIdx
->getType(), 1);
943 Value
*IdxNext
= B2
.CreateAdd(NewIdx
, One
, NewIdx
->getName() + ".next");
944 auto Pred
= LatchBR
->getSuccessor(0) == Header
? ICmpInst::ICMP_NE
: ICmpInst::ICMP_EQ
;
945 Value
*IdxCmp
= B2
.CreateICmp(Pred
, IdxNext
, TestVal
, NewIdx
->getName() + ".ncmp");
946 NewIdx
->addIncoming(Zero
, NewPreHeader
);
947 NewIdx
->addIncoming(IdxNext
, Latch
);
948 LatchBR
->setCondition(IdxCmp
);
950 // Connect the prolog code to the original loop and update the
952 ConnectProlog(L
, BECount
, Count
, PrologExit
, LatchExit
, PreHeader
,
953 NewPreHeader
, VMap
, DT
, LI
, PreserveLCSSA
, *SE
);
956 // If this loop is nested, then the loop unroller changes the code in the any
957 // of its parent loops, so the Scalar Evolution pass needs to be run again.
958 SE
->forgetTopmostLoop(L
);
960 // Verify that the Dom Tree and Loop Info are correct.
961 #if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
963 assert(DT
->verify(DominatorTree::VerificationLevel::Full
));
968 // For unroll factor 2 remainder loop will have 1 iteration.
969 if (Count
== 2 && DT
&& LI
&& SE
) {
970 // TODO: This code could probably be pulled out into a helper function
971 // (e.g. breakLoopBackedgeAndSimplify) and reused in loop-deletion.
972 BasicBlock
*RemainderLatch
= remainderLoop
->getLoopLatch();
973 assert(RemainderLatch
);
974 SmallVector
<BasicBlock
*> RemainderBlocks(remainderLoop
->getBlocks().begin(),
975 remainderLoop
->getBlocks().end());
976 breakLoopBackedge(remainderLoop
, *DT
, *SE
, *LI
, nullptr);
977 remainderLoop
= nullptr;
979 // Simplify loop values after breaking the backedge
980 const DataLayout
&DL
= L
->getHeader()->getModule()->getDataLayout();
981 SmallVector
<WeakTrackingVH
, 16> DeadInsts
;
982 for (BasicBlock
*BB
: RemainderBlocks
) {
983 for (Instruction
&Inst
: llvm::make_early_inc_range(*BB
)) {
984 if (Value
*V
= simplifyInstruction(&Inst
, {DL
, nullptr, DT
, AC
}))
985 if (LI
->replacementPreservesLCSSAForm(&Inst
, V
))
986 Inst
.replaceAllUsesWith(V
);
987 if (isInstructionTriviallyDead(&Inst
))
988 DeadInsts
.emplace_back(&Inst
);
990 // We can't do recursive deletion until we're done iterating, as we might
991 // have a phi which (potentially indirectly) uses instructions later in
992 // the block we're iterating through.
993 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts
);
996 // Merge latch into exit block.
997 auto *ExitBB
= RemainderLatch
->getSingleSuccessor();
998 assert(ExitBB
&& "required after breaking cond br backedge");
999 DomTreeUpdater
DTU(DT
, DomTreeUpdater::UpdateStrategy::Eager
);
1000 MergeBlockIntoPredecessor(ExitBB
, &DTU
, LI
);
1003 // Canonicalize to LoopSimplifyForm both original and remainder loops. We
1004 // cannot rely on the LoopUnrollPass to do this because it only does
1005 // canonicalization for parent/subloops and not the sibling loops.
1006 if (OtherExits
.size() > 0) {
1007 // Generate dedicated exit blocks for the original loop, to preserve
1008 // LoopSimplifyForm.
1009 formDedicatedExitBlocks(L
, DT
, LI
, nullptr, PreserveLCSSA
);
1010 // Generate dedicated exit blocks for the remainder loop if one exists, to
1011 // preserve LoopSimplifyForm.
1013 formDedicatedExitBlocks(remainderLoop
, DT
, LI
, nullptr, PreserveLCSSA
);
1016 auto UnrollResult
= LoopUnrollResult::Unmodified
;
1017 if (remainderLoop
&& UnrollRemainder
) {
1018 LLVM_DEBUG(dbgs() << "Unrolling remainder loop\n");
1020 UnrollLoop(remainderLoop
,
1021 {/*Count*/ Count
- 1, /*Force*/ false, /*Runtime*/ false,
1022 /*AllowExpensiveTripCount*/ false,
1023 /*UnrollRemainder*/ false, ForgetAllSCEV
},
1024 LI
, SE
, DT
, AC
, TTI
, /*ORE*/ nullptr, PreserveLCSSA
);
1027 if (ResultLoop
&& UnrollResult
!= LoopUnrollResult::FullyUnrolled
)
1028 *ResultLoop
= remainderLoop
;
1029 NumRuntimeUnrolled
++;