1 //===- UnrollLoopPeel.cpp - Loop peeling utilities ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements some loop unrolling utilities for peeling loops
11 // with dynamically inferred (from PGO) trip counts. See LoopUnroll.cpp for
12 // unrolling loops with compile-time constant trip counts.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/LoopIterator.h"
22 #include "llvm/Analysis/ScalarEvolution.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/IR/BasicBlock.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/MDBuilder.h"
32 #include "llvm/IR/Metadata.h"
33 #include "llvm/Support/Casting.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/LoopSimplify.h"
40 #include "llvm/Transforms/Utils/LoopUtils.h"
41 #include "llvm/Transforms/Utils/UnrollLoop.h"
42 #include "llvm/Transforms/Utils/ValueMapper.h"
50 #define DEBUG_TYPE "loop-unroll"
52 STATISTIC(NumPeeled, "Number of loops peeled");
54 static cl::opt<unsigned> UnrollPeelMaxCount(
55 "unroll-peel-max-count", cl::init(7), cl::Hidden,
56 cl::desc("Max average trip count which will cause loop peeling."));
58 static cl::opt<unsigned> UnrollForcePeelCount(
59 "unroll-force-peel-count", cl::init(0), cl::Hidden,
60 cl::desc("Force a peel count regardless of profiling information."));
62 // Designates that a Phi is estimated to become invariant after an "infinite"
63 // number of loop iterations (i.e. only may become an invariant if the loop is
65 static const unsigned InfiniteIterationsToInvariance =
66 std::numeric_limits<unsigned>::max();
68 // Check whether we are capable of peeling this loop.
69 static bool canPeel(Loop *L) {
70 // Make sure the loop is in simplified form
71 if (!L->isLoopSimplifyForm())
74 // Only peel loops that contain a single exit
75 if (!L->getExitingBlock() || !L->getUniqueExitBlock())
78 // Don't try to peel loops where the latch is not the exiting block.
79 // This can be an indication of two different things:
80 // 1) The loop is not rotated.
81 // 2) The loop contains irreducible control flow that involves the latch.
82 if (L->getLoopLatch() != L->getExitingBlock())
88 // This function calculates the number of iterations after which the given Phi
89 // becomes an invariant. The pre-calculated values are memorized in the map. The
90 // function (shortcut is I) is calculated according to the following definition:
91 // Given %x = phi <Inputs from above the loop>, ..., [%y, %back.edge].
92 // If %y is a loop invariant, then I(%x) = 1.
93 // If %y is a Phi from the loop header, I(%x) = I(%y) + 1.
94 // Otherwise, I(%x) is infinite.
95 // TODO: Actually if %y is an expression that depends only on Phi %z and some
96 // loop invariants, we can estimate I(%x) = I(%z) + 1. The example
98 // %x = phi(0, %a), <-- becomes invariant starting from 3rd iteration.
101 static unsigned calculateIterationsToInvariance(
102 PHINode *Phi, Loop *L, BasicBlock *BackEdge,
103 SmallDenseMap<PHINode *, unsigned> &IterationsToInvariance) {
104 assert(Phi->getParent() == L->getHeader() &&
105 "Non-loop Phi should not be checked for turning into invariant.");
106 assert(BackEdge == L->getLoopLatch() && "Wrong latch?");
107 // If we already know the answer, take it from the map.
108 auto I = IterationsToInvariance.find(Phi);
109 if (I != IterationsToInvariance.end())
112 // Otherwise we need to analyze the input from the back edge.
113 Value *Input = Phi->getIncomingValueForBlock(BackEdge);
114 // Place infinity to map to avoid infinite recursion for cycled Phis. Such
115 // cycles can never stop on an invariant.
116 IterationsToInvariance[Phi] = InfiniteIterationsToInvariance;
117 unsigned ToInvariance = InfiniteIterationsToInvariance;
119 if (L->isLoopInvariant(Input))
121 else if (PHINode *IncPhi = dyn_cast<PHINode>(Input)) {
122 // Only consider Phis in header block.
123 if (IncPhi->getParent() != L->getHeader())
124 return InfiniteIterationsToInvariance;
125 // If the input becomes an invariant after X iterations, then our Phi
126 // becomes an invariant after X + 1 iterations.
127 unsigned InputToInvariance = calculateIterationsToInvariance(
128 IncPhi, L, BackEdge, IterationsToInvariance);
129 if (InputToInvariance != InfiniteIterationsToInvariance)
130 ToInvariance = InputToInvariance + 1u;
133 // If we found that this Phi lies in an invariant chain, update the map.
134 if (ToInvariance != InfiniteIterationsToInvariance)
135 IterationsToInvariance[Phi] = ToInvariance;
139 // Return the number of iterations we want to peel off.
140 void llvm::computePeelCount(Loop *L, unsigned LoopSize,
141 TargetTransformInfo::UnrollingPreferences &UP,
142 unsigned &TripCount) {
143 assert(LoopSize > 0 && "Zero loop size is not allowed!");
148 // Only try to peel innermost loops.
152 // Here we try to get rid of Phis which become invariants after 1, 2, ..., N
153 // iterations of the loop. For this we compute the number for iterations after
154 // which every Phi is guaranteed to become an invariant, and try to peel the
155 // maximum number of iterations among these values, thus turning all those
156 // Phis into invariants.
157 // First, check that we can peel at least one iteration.
158 if (2 * LoopSize <= UP.Threshold && UnrollPeelMaxCount > 0) {
159 // Store the pre-calculated values here.
160 SmallDenseMap<PHINode *, unsigned> IterationsToInvariance;
161 // Now go through all Phis to calculate their the number of iterations they
162 // need to become invariants.
163 unsigned DesiredPeelCount = 0;
164 BasicBlock *BackEdge = L->getLoopLatch();
165 assert(BackEdge && "Loop is not in simplified form?");
166 for (auto BI = L->getHeader()->begin(); isa<PHINode>(&*BI); ++BI) {
167 PHINode *Phi = cast<PHINode>(&*BI);
168 unsigned ToInvariance = calculateIterationsToInvariance(
169 Phi, L, BackEdge, IterationsToInvariance);
170 if (ToInvariance != InfiniteIterationsToInvariance)
171 DesiredPeelCount = std::max(DesiredPeelCount, ToInvariance);
173 if (DesiredPeelCount > 0) {
174 // Pay respect to limitations implied by loop size and the max peel count.
175 unsigned MaxPeelCount = UnrollPeelMaxCount;
176 MaxPeelCount = std::min(MaxPeelCount, UP.Threshold / LoopSize - 1);
177 DesiredPeelCount = std::min(DesiredPeelCount, MaxPeelCount);
178 // Consider max peel count limitation.
179 assert(DesiredPeelCount > 0 && "Wrong loop size estimation?");
180 DEBUG(dbgs() << "Peel " << DesiredPeelCount << " iteration(s) to turn"
181 << " some Phis into invariants.\n");
182 UP.PeelCount = DesiredPeelCount;
187 // Bail if we know the statically calculated trip count.
188 // In this case we rather prefer partial unrolling.
192 // If the user provided a peel count, use that.
193 bool UserPeelCount = UnrollForcePeelCount.getNumOccurrences() > 0;
195 DEBUG(dbgs() << "Force-peeling first " << UnrollForcePeelCount
196 << " iterations.\n");
197 UP.PeelCount = UnrollForcePeelCount;
201 // If we don't know the trip count, but have reason to believe the average
202 // trip count is low, peeling should be beneficial, since we will usually
203 // hit the peeled section.
204 // We only do this in the presence of profile information, since otherwise
205 // our estimates of the trip count are not reliable enough.
206 if (UP.AllowPeeling && L->getHeader()->getParent()->hasProfileData()) {
207 Optional<unsigned> PeelCount = getLoopEstimatedTripCount(L);
211 DEBUG(dbgs() << "Profile-based estimated trip count is " << *PeelCount
215 if ((*PeelCount <= UnrollPeelMaxCount) &&
216 (LoopSize * (*PeelCount + 1) <= UP.Threshold)) {
217 DEBUG(dbgs() << "Peeling first " << *PeelCount << " iterations.\n");
218 UP.PeelCount = *PeelCount;
221 DEBUG(dbgs() << "Requested peel count: " << *PeelCount << "\n");
222 DEBUG(dbgs() << "Max peel count: " << UnrollPeelMaxCount << "\n");
223 DEBUG(dbgs() << "Peel cost: " << LoopSize * (*PeelCount + 1) << "\n");
224 DEBUG(dbgs() << "Max peel cost: " << UP.Threshold << "\n");
229 /// \brief Update the branch weights of the latch of a peeled-off loop
231 /// This sets the branch weights for the latch of the recently peeled off loop
232 /// iteration correctly.
233 /// Our goal is to make sure that:
234 /// a) The total weight of all the copies of the loop body is preserved.
235 /// b) The total weight of the loop exit is preserved.
236 /// c) The body weight is reasonably distributed between the peeled iterations.
238 /// \param Header The copy of the header block that belongs to next iteration.
239 /// \param LatchBR The copy of the latch branch that belongs to this iteration.
240 /// \param IterNumber The serial number of the iteration that was just
242 /// \param AvgIters The average number of iterations we expect the loop to have.
243 /// \param[in,out] PeeledHeaderWeight The total number of dynamic loop
244 /// iterations that are unaccounted for. As an input, it represents the number
245 /// of times we expect to enter the header of the iteration currently being
246 /// peeled off. The output is the number of times we expect to enter the
247 /// header of the next iteration.
248 static void updateBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
249 unsigned IterNumber, unsigned AvgIters,
250 uint64_t &PeeledHeaderWeight) {
251 // FIXME: Pick a more realistic distribution.
252 // Currently the proportion of weight we assign to the fall-through
253 // side of the branch drops linearly with the iteration number, and we use
254 // a 0.9 fudge factor to make the drop-off less sharp...
255 if (PeeledHeaderWeight) {
256 uint64_t FallThruWeight =
257 PeeledHeaderWeight * ((float)(AvgIters - IterNumber) / AvgIters * 0.9);
258 uint64_t ExitWeight = PeeledHeaderWeight - FallThruWeight;
259 PeeledHeaderWeight -= ExitWeight;
261 unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
262 MDBuilder MDB(LatchBR->getContext());
264 HeaderIdx ? MDB.createBranchWeights(ExitWeight, FallThruWeight)
265 : MDB.createBranchWeights(FallThruWeight, ExitWeight);
266 LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
270 /// \brief Clones the body of the loop L, putting it between \p InsertTop and \p
272 /// \param IterNumber The serial number of the iteration currently being
274 /// \param Exit The exit block of the original loop.
275 /// \param[out] NewBlocks A list of the the blocks in the newly created clone
276 /// \param[out] VMap The value map between the loop and the new clone.
277 /// \param LoopBlocks A helper for DFS-traversal of the loop.
278 /// \param LVMap A value-map that maps instructions from the original loop to
279 /// instructions in the last peeled-off iteration.
280 static void cloneLoopBlocks(Loop *L, unsigned IterNumber, BasicBlock *InsertTop,
281 BasicBlock *InsertBot, BasicBlock *Exit,
282 SmallVectorImpl<BasicBlock *> &NewBlocks,
283 LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap,
284 ValueToValueMapTy &LVMap, DominatorTree *DT,
286 BasicBlock *Header = L->getHeader();
287 BasicBlock *Latch = L->getLoopLatch();
288 BasicBlock *PreHeader = L->getLoopPreheader();
290 Function *F = Header->getParent();
291 LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
292 LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
293 Loop *ParentLoop = L->getParentLoop();
295 // For each block in the original loop, create a new copy,
296 // and update the value map with the newly created values.
297 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
298 BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, ".peel", F);
299 NewBlocks.push_back(NewBB);
302 ParentLoop->addBasicBlockToLoop(NewBB, *LI);
306 // If dominator tree is available, insert nodes to represent cloned blocks.
309 DT->addNewBlock(NewBB, InsertTop);
311 DomTreeNode *IDom = DT->getNode(*BB)->getIDom();
312 // VMap must contain entry for IDom, as the iteration order is RPO.
313 DT->addNewBlock(NewBB, cast<BasicBlock>(VMap[IDom->getBlock()]));
318 // Hook-up the control flow for the newly inserted blocks.
319 // The new header is hooked up directly to the "top", which is either
320 // the original loop preheader (for the first iteration) or the previous
321 // iteration's exiting block (for every other iteration)
322 InsertTop->getTerminator()->setSuccessor(0, cast<BasicBlock>(VMap[Header]));
324 // Similarly, for the latch:
325 // The original exiting edge is still hooked up to the loop exit.
326 // The backedge now goes to the "bottom", which is either the loop's real
327 // header (for the last peeled iteration) or the copied header of the next
328 // iteration (for every other iteration)
329 BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
330 BranchInst *LatchBR = cast<BranchInst>(NewLatch->getTerminator());
331 unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
332 LatchBR->setSuccessor(HeaderIdx, InsertBot);
333 LatchBR->setSuccessor(1 - HeaderIdx, Exit);
335 DT->changeImmediateDominator(InsertBot, NewLatch);
337 // The new copy of the loop body starts with a bunch of PHI nodes
338 // that pick an incoming value from either the preheader, or the previous
339 // loop iteration. Since this copy is no longer part of the loop, we
340 // resolve this statically:
341 // For the first iteration, we use the value from the preheader directly.
342 // For any other iteration, we replace the phi with the value generated by
343 // the immediately preceding clone of the loop body (which represents
344 // the previous iteration).
345 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
346 PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
347 if (IterNumber == 0) {
348 VMap[&*I] = NewPHI->getIncomingValueForBlock(PreHeader);
350 Value *LatchVal = NewPHI->getIncomingValueForBlock(Latch);
351 Instruction *LatchInst = dyn_cast<Instruction>(LatchVal);
352 if (LatchInst && L->contains(LatchInst))
353 VMap[&*I] = LVMap[LatchInst];
355 VMap[&*I] = LatchVal;
357 cast<BasicBlock>(VMap[Header])->getInstList().erase(NewPHI);
360 // Fix up the outgoing values - we need to add a value for the iteration
361 // we've just created. Note that this must happen *after* the incoming
362 // values are adjusted, since the value going out of the latch may also be
363 // a value coming into the header.
364 for (BasicBlock::iterator I = Exit->begin(); isa<PHINode>(I); ++I) {
365 PHINode *PHI = cast<PHINode>(I);
366 Value *LatchVal = PHI->getIncomingValueForBlock(Latch);
367 Instruction *LatchInst = dyn_cast<Instruction>(LatchVal);
368 if (LatchInst && L->contains(LatchInst))
369 LatchVal = VMap[LatchVal];
370 PHI->addIncoming(LatchVal, cast<BasicBlock>(VMap[Latch]));
373 // LastValueMap is updated with the values for the current loop
374 // which are used the next time this function is called.
375 for (const auto &KV : VMap)
376 LVMap[KV.first] = KV.second;
379 /// \brief Peel off the first \p PeelCount iterations of loop \p L.
381 /// Note that this does not peel them off as a single straight-line block.
382 /// Rather, each iteration is peeled off separately, and needs to check the
384 /// For loops that dynamically execute \p PeelCount iterations or less
385 /// this provides a benefit, since the peeled off iterations, which account
386 /// for the bulk of dynamic execution, can be further simplified by scalar
388 bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
389 ScalarEvolution *SE, DominatorTree *DT,
390 AssumptionCache *AC, bool PreserveLCSSA) {
394 LoopBlocksDFS LoopBlocks(L);
395 LoopBlocks.perform(LI);
397 BasicBlock *Header = L->getHeader();
398 BasicBlock *PreHeader = L->getLoopPreheader();
399 BasicBlock *Latch = L->getLoopLatch();
400 BasicBlock *Exit = L->getUniqueExitBlock();
402 Function *F = Header->getParent();
404 // Set up all the necessary basic blocks. It is convenient to split the
405 // preheader into 3 parts - two blocks to anchor the peeled copy of the loop
406 // body, and a new preheader for the "real" loop.
408 // Peeling the first iteration transforms.
414 // If (cond) goto Header
421 // If (!cond) goto Exit
427 // If (cond) goto Header
430 // Each following iteration will split the current bottom anchor in two,
431 // and put the new copy of the loop body between these two blocks. That is,
432 // after peeling another iteration from the example above, we'll split
433 // InsertBot, and get:
437 // If (!cond) goto Exit
440 // If (!cond) goto Exit
446 // If (cond) goto Header
449 BasicBlock *InsertTop = SplitEdge(PreHeader, Header, DT, LI);
450 BasicBlock *InsertBot =
451 SplitBlock(InsertTop, InsertTop->getTerminator(), DT, LI);
452 BasicBlock *NewPreHeader =
453 SplitBlock(InsertBot, InsertBot->getTerminator(), DT, LI);
455 InsertTop->setName(Header->getName() + ".peel.begin");
456 InsertBot->setName(Header->getName() + ".peel.next");
457 NewPreHeader->setName(PreHeader->getName() + ".peel.newph");
459 ValueToValueMapTy LVMap;
461 // If we have branch weight information, we'll want to update it for the
462 // newly created branches.
463 BranchInst *LatchBR =
464 cast<BranchInst>(cast<BasicBlock>(Latch)->getTerminator());
465 unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
467 uint64_t TrueWeight, FalseWeight;
468 uint64_t ExitWeight = 0, CurHeaderWeight = 0;
469 if (LatchBR->extractProfMetadata(TrueWeight, FalseWeight)) {
470 ExitWeight = HeaderIdx ? TrueWeight : FalseWeight;
471 // The # of times the loop body executes is the sum of the exit block
472 // weight and the # of times the backedges are taken.
473 CurHeaderWeight = TrueWeight + FalseWeight;
476 // For each peeled-off iteration, make a copy of the loop.
477 for (unsigned Iter = 0; Iter < PeelCount; ++Iter) {
478 SmallVector<BasicBlock *, 8> NewBlocks;
479 ValueToValueMapTy VMap;
481 // Subtract the exit weight from the current header weight -- the exit
482 // weight is exactly the weight of the previous iteration's header.
483 // FIXME: due to the way the distribution is constructed, we need a
484 // guard here to make sure we don't end up with non-positive weights.
485 if (ExitWeight < CurHeaderWeight)
486 CurHeaderWeight -= ExitWeight;
490 cloneLoopBlocks(L, Iter, InsertTop, InsertBot, Exit,
491 NewBlocks, LoopBlocks, VMap, LVMap, DT, LI);
493 // Remap to use values from the current iteration instead of the
495 remapInstructionsInBlocks(NewBlocks, VMap);
498 // Latches of the cloned loops dominate over the loop exit, so idom of the
499 // latter is the first cloned loop body, as original PreHeader dominates
500 // the original loop body.
502 DT->changeImmediateDominator(Exit, cast<BasicBlock>(LVMap[Latch]));
509 updateBranchWeights(InsertBot, cast<BranchInst>(VMap[LatchBR]), Iter,
510 PeelCount, ExitWeight);
512 InsertTop = InsertBot;
513 InsertBot = SplitBlock(InsertBot, InsertBot->getTerminator(), DT, LI);
514 InsertBot->setName(Header->getName() + ".peel.next");
516 F->getBasicBlockList().splice(InsertTop->getIterator(),
517 F->getBasicBlockList(),
518 NewBlocks[0]->getIterator(), F->end());
521 // Now adjust the phi nodes in the loop header to get their initial values
522 // from the last peeled-off iteration instead of the preheader.
523 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
524 PHINode *PHI = cast<PHINode>(I);
525 Value *NewVal = PHI->getIncomingValueForBlock(Latch);
526 Instruction *LatchInst = dyn_cast<Instruction>(NewVal);
527 if (LatchInst && L->contains(LatchInst))
528 NewVal = LVMap[LatchInst];
530 PHI->setIncomingValue(PHI->getBasicBlockIndex(NewPreHeader), NewVal);
533 // Adjust the branch weights on the loop exit.
535 // The backedge count is the difference of current header weight and
536 // current loop exit weight. If the current header weight is smaller than
537 // the current loop exit weight, we mark the loop backedge weight as 1.
538 uint64_t BackEdgeWeight = 0;
539 if (ExitWeight < CurHeaderWeight)
540 BackEdgeWeight = CurHeaderWeight - ExitWeight;
543 MDBuilder MDB(LatchBR->getContext());
545 HeaderIdx ? MDB.createBranchWeights(ExitWeight, BackEdgeWeight)
546 : MDB.createBranchWeights(BackEdgeWeight, ExitWeight);
547 LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
550 // If the loop is nested, we changed the parent loop, update SE.
551 if (Loop *ParentLoop = L->getParentLoop()) {
552 SE->forgetLoop(ParentLoop);
554 // FIXME: Incrementally update loop-simplify
555 simplifyLoop(ParentLoop, DT, LI, SE, AC, PreserveLCSSA);
557 // FIXME: Incrementally update loop-simplify
558 simplifyLoop(L, DT, LI, SE, AC, PreserveLCSSA);