LLVM 22.0.0git
LoopUnrollRuntime.cpp
Go to the documentation of this file.
1//===-- UnrollLoopRuntime.cpp - Runtime 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 for loops with run-time
10// trip counts. See LoopUnroll.cpp for unrolling loops with compile-time
11// trip counts.
12//
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.
16//
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
19// unrolled loop.
20//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/ADT/Statistic.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"
35#include "llvm/Support/Debug.h"
43
44using namespace llvm;
45
46#define DEBUG_TYPE "loop-unroll"
47
48STATISTIC(NumRuntimeUnrolled,
49 "Number of loops unrolled with run-time trip counts");
51 "unroll-runtime-multi-exit", cl::init(false), cl::Hidden,
52 cl::desc("Allow runtime unrolling for loops with multiple exits, when "
53 "epilog is generated"));
55 "unroll-runtime-other-exit-predictable", cl::init(false), cl::Hidden,
56 cl::desc("Assume the non latch exit block to be predictable"));
57
58// Probability that the loop trip count is so small that after the prolog
59// we do not enter the unrolled loop at all.
60// It is unlikely that the loop trip count is smaller than the unroll factor;
61// other than that, the choice of constant is not tuned yet.
62static const uint32_t UnrolledLoopHeaderWeights[] = {1, 127};
63// Probability that the loop trip count is so small that we skip the unrolled
64// loop completely and immediately enter the epilogue loop.
65// It is unlikely that the loop trip count is smaller than the unroll factor;
66// other than that, the choice of constant is not tuned yet.
67static const uint32_t EpilogHeaderWeights[] = {1, 127};
68
69/// Connect the unrolling prolog code to the original loop.
70/// The unrolling prolog code contains code to execute the
71/// 'extra' iterations if the run-time trip count modulo the
72/// unroll count is non-zero.
73///
74/// This function performs the following:
75/// - Create PHI nodes at prolog end block to combine values
76/// that exit the prolog code and jump around the prolog.
77/// - Add a PHI operand to a PHI node at the loop exit block
78/// for values that exit the prolog and go around the loop.
79/// - Branch around the original loop if the trip count is less
80/// than the unroll factor.
81///
82static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
83 BasicBlock *PrologExit,
84 BasicBlock *OriginalLoopLatchExit,
85 BasicBlock *PreHeader, BasicBlock *NewPreHeader,
87 LoopInfo *LI, bool PreserveLCSSA,
88 ScalarEvolution &SE) {
89 // Loop structure should be the following:
90 // Preheader
91 // PrologHeader
92 // ...
93 // PrologLatch
94 // PrologExit
95 // NewPreheader
96 // Header
97 // ...
98 // Latch
99 // LatchExit
100 BasicBlock *Latch = L->getLoopLatch();
101 assert(Latch && "Loop must have a latch");
102 BasicBlock *PrologLatch = cast<BasicBlock>(VMap[Latch]);
103
104 // Create a PHI node for each outgoing value from the original loop
105 // (which means it is an outgoing value from the prolog code too).
106 // The new PHI node is inserted in the prolog end basic block.
107 // The new PHI node value is added as an operand of a PHI node in either
108 // the loop header or the loop exit block.
109 for (BasicBlock *Succ : successors(Latch)) {
110 for (PHINode &PN : Succ->phis()) {
111 // Add a new PHI node to the prolog end block and add the
112 // appropriate incoming values.
113 // TODO: This code assumes that the PrologExit (or the LatchExit block for
114 // prolog loop) contains only one predecessor from the loop, i.e. the
115 // PrologLatch. When supporting multiple-exiting block loops, we can have
116 // two or more blocks that have the LatchExit as the target in the
117 // original loop.
118 PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr");
119 NewPN->insertBefore(PrologExit->getFirstNonPHIIt());
120 // Adding a value to the new PHI node from the original loop preheader.
121 // This is the value that skips all the prolog code.
122 if (L->contains(&PN)) {
123 // Succ is loop header.
124 NewPN->addIncoming(PN.getIncomingValueForBlock(NewPreHeader),
125 PreHeader);
126 } else {
127 // Succ is LatchExit.
128 NewPN->addIncoming(PoisonValue::get(PN.getType()), PreHeader);
129 }
130
131 Value *V = PN.getIncomingValueForBlock(Latch);
132 if (Instruction *I = dyn_cast<Instruction>(V)) {
133 if (L->contains(I)) {
134 V = VMap.lookup(I);
135 }
136 }
137 // Adding a value to the new PHI node from the last prolog block
138 // that was created.
139 NewPN->addIncoming(V, PrologLatch);
140
141 // Update the existing PHI node operand with the value from the
142 // new PHI node. How this is done depends on if the existing
143 // PHI node is in the original loop block, or the exit block.
144 if (L->contains(&PN))
145 PN.setIncomingValueForBlock(NewPreHeader, NewPN);
146 else
147 PN.addIncoming(NewPN, PrologExit);
149 }
150 }
151
152 // Make sure that created prolog loop is in simplified form
153 SmallVector<BasicBlock *, 4> PrologExitPreds;
154 Loop *PrologLoop = LI->getLoopFor(PrologLatch);
155 if (PrologLoop) {
156 for (BasicBlock *PredBB : predecessors(PrologExit))
157 if (PrologLoop->contains(PredBB))
158 PrologExitPreds.push_back(PredBB);
159
160 SplitBlockPredecessors(PrologExit, PrologExitPreds, ".unr-lcssa", DT, LI,
161 nullptr, PreserveLCSSA);
162 }
163
164 // Create a branch around the original loop, which is taken if there are no
165 // iterations remaining to be executed after running the prologue.
166 Instruction *InsertPt = PrologExit->getTerminator();
167 IRBuilder<> B(InsertPt);
168
169 assert(Count != 0 && "nonsensical Count!");
170
171 // If BECount <u (Count - 1) then (BECount + 1) % Count == (BECount + 1)
172 // This means %xtraiter is (BECount + 1) and all of the iterations of this
173 // loop were executed by the prologue. Note that if BECount <u (Count - 1)
174 // then (BECount + 1) cannot unsigned-overflow.
175 Value *BrLoopExit =
176 B.CreateICmpULT(BECount, ConstantInt::get(BECount->getType(), Count - 1));
177 // Split the exit to maintain loop canonicalization guarantees
178 SmallVector<BasicBlock *, 4> Preds(predecessors(OriginalLoopLatchExit));
179 SplitBlockPredecessors(OriginalLoopLatchExit, Preds, ".unr-lcssa", DT, LI,
180 nullptr, PreserveLCSSA);
181 // Add the branch to the exit block (around the unrolled loop)
182 MDNode *BranchWeights = nullptr;
183 if (hasBranchWeightMD(*Latch->getTerminator())) {
184 // Assume loop is nearly always entered.
185 MDBuilder MDB(B.getContext());
187 }
188 B.CreateCondBr(BrLoopExit, OriginalLoopLatchExit, NewPreHeader,
189 BranchWeights);
190 InsertPt->eraseFromParent();
191 if (DT) {
192 auto *NewDom = DT->findNearestCommonDominator(OriginalLoopLatchExit,
193 PrologExit);
194 DT->changeImmediateDominator(OriginalLoopLatchExit, NewDom);
195 }
196}
197
198/// Connect the unrolling epilog code to the original loop.
199/// The unrolling epilog code contains code to execute the
200/// 'extra' iterations if the run-time trip count modulo the
201/// unroll count is non-zero.
202///
203/// This function performs the following:
204/// - Update PHI nodes at the unrolling loop exit and epilog loop exit
205/// - Create PHI nodes at the unrolling loop exit to combine
206/// values that exit the unrolling loop code and jump around it.
207/// - Update PHI operands in the epilog loop by the new PHI nodes
208/// - Branch around the epilog loop if extra iters (ModVal) is zero.
209///
210static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
211 BasicBlock *Exit, BasicBlock *PreHeader,
212 BasicBlock *EpilogPreHeader, BasicBlock *NewPreHeader,
214 LoopInfo *LI, bool PreserveLCSSA, ScalarEvolution &SE,
215 unsigned Count) {
216 BasicBlock *Latch = L->getLoopLatch();
217 assert(Latch && "Loop must have a latch");
218 BasicBlock *EpilogLatch = cast<BasicBlock>(VMap[Latch]);
219
220 // Loop structure should be the following:
221 //
222 // PreHeader
223 // NewPreHeader
224 // Header
225 // ...
226 // Latch
227 // NewExit (PN)
228 // EpilogPreHeader
229 // EpilogHeader
230 // ...
231 // EpilogLatch
232 // Exit (EpilogPN)
233
234 // Update PHI nodes at NewExit and Exit.
235 for (PHINode &PN : NewExit->phis()) {
236 // PN should be used in another PHI located in Exit block as
237 // Exit was split by SplitBlockPredecessors into Exit and NewExit
238 // Basically it should look like:
239 // NewExit:
240 // PN = PHI [I, Latch]
241 // ...
242 // Exit:
243 // EpilogPN = PHI [PN, EpilogPreHeader], [X, Exit2], [Y, Exit2.epil]
244 //
245 // Exits from non-latch blocks point to the original exit block and the
246 // epilogue edges have already been added.
247 //
248 // There is EpilogPreHeader incoming block instead of NewExit as
249 // NewExit was spilt 1 more time to get EpilogPreHeader.
250 assert(PN.hasOneUse() && "The phi should have 1 use");
251 PHINode *EpilogPN = cast<PHINode>(PN.use_begin()->getUser());
252 assert(EpilogPN->getParent() == Exit && "EpilogPN should be in Exit block");
253
254 // Add incoming PreHeader from branch around the Loop
255 PN.addIncoming(PoisonValue::get(PN.getType()), PreHeader);
256 SE.forgetValue(&PN);
257
258 Value *V = PN.getIncomingValueForBlock(Latch);
259 Instruction *I = dyn_cast<Instruction>(V);
260 if (I && L->contains(I))
261 // If value comes from an instruction in the loop add VMap value.
262 V = VMap.lookup(I);
263 // For the instruction out of the loop, constant or undefined value
264 // insert value itself.
265 EpilogPN->addIncoming(V, EpilogLatch);
266
267 assert(EpilogPN->getBasicBlockIndex(EpilogPreHeader) >= 0 &&
268 "EpilogPN should have EpilogPreHeader incoming block");
269 // Change EpilogPreHeader incoming block to NewExit.
270 EpilogPN->setIncomingBlock(EpilogPN->getBasicBlockIndex(EpilogPreHeader),
271 NewExit);
272 // Now PHIs should look like:
273 // NewExit:
274 // PN = PHI [I, Latch], [poison, PreHeader]
275 // ...
276 // Exit:
277 // EpilogPN = PHI [PN, NewExit], [VMap[I], EpilogLatch]
278 }
279
280 // Create PHI nodes at NewExit (from the unrolling loop Latch and PreHeader).
281 // Update corresponding PHI nodes in epilog loop.
282 for (BasicBlock *Succ : successors(Latch)) {
283 // Skip this as we already updated phis in exit blocks.
284 if (!L->contains(Succ))
285 continue;
286 for (PHINode &PN : Succ->phis()) {
287 // Add new PHI nodes to the loop exit block and update epilog
288 // PHIs with the new PHI values.
289 PHINode *NewPN = PHINode::Create(PN.getType(), 2, PN.getName() + ".unr");
290 NewPN->insertBefore(NewExit->getFirstNonPHIIt());
291 // Adding a value to the new PHI node from the unrolling loop preheader.
292 NewPN->addIncoming(PN.getIncomingValueForBlock(NewPreHeader), PreHeader);
293 // Adding a value to the new PHI node from the unrolling loop latch.
294 NewPN->addIncoming(PN.getIncomingValueForBlock(Latch), Latch);
295
296 // Update the existing PHI node operand with the value from the new PHI
297 // node. Corresponding instruction in epilog loop should be PHI.
298 PHINode *VPN = cast<PHINode>(VMap[&PN]);
299 VPN->setIncomingValueForBlock(EpilogPreHeader, NewPN);
300 }
301 }
302
303 Instruction *InsertPt = NewExit->getTerminator();
304 IRBuilder<> B(InsertPt);
305 Value *BrLoopExit = B.CreateIsNotNull(ModVal, "lcmp.mod");
306 assert(Exit && "Loop must have a single exit block only");
307 // Split the epilogue exit to maintain loop canonicalization guarantees
309 SplitBlockPredecessors(Exit, Preds, ".epilog-lcssa", DT, LI, nullptr,
310 PreserveLCSSA);
311 // Add the branch to the exit block (around the unrolling loop)
312 MDNode *BranchWeights = nullptr;
313 if (hasBranchWeightMD(*Latch->getTerminator())) {
314 // Assume equal distribution in interval [0, Count).
315 MDBuilder MDB(B.getContext());
316 BranchWeights = MDB.createBranchWeights(1, Count - 1);
317 }
318 B.CreateCondBr(BrLoopExit, EpilogPreHeader, Exit, BranchWeights);
319 InsertPt->eraseFromParent();
320 if (DT) {
321 auto *NewDom = DT->findNearestCommonDominator(Exit, NewExit);
322 DT->changeImmediateDominator(Exit, NewDom);
323 }
324
325 // Split the main loop exit to maintain canonicalization guarantees.
326 SmallVector<BasicBlock*, 4> NewExitPreds{Latch};
327 SplitBlockPredecessors(NewExit, NewExitPreds, ".loopexit", DT, LI, nullptr,
328 PreserveLCSSA);
329}
330
331/// Create a clone of the blocks in a loop and connect them together. A new
332/// loop will be created including all cloned blocks, and the iterator of the
333/// new loop switched to count NewIter down to 0.
334/// The cloned blocks should be inserted between InsertTop and InsertBot.
335/// InsertTop should be new preheader, InsertBot new loop exit.
336/// Returns the new cloned loop that is created.
337static Loop *
338CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
339 const bool UnrollRemainder,
340 BasicBlock *InsertTop,
341 BasicBlock *InsertBot, BasicBlock *Preheader,
342 std::vector<BasicBlock *> &NewBlocks,
343 LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap,
344 DominatorTree *DT, LoopInfo *LI, unsigned Count) {
345 StringRef suffix = UseEpilogRemainder ? "epil" : "prol";
346 BasicBlock *Header = L->getHeader();
347 BasicBlock *Latch = L->getLoopLatch();
348 Function *F = Header->getParent();
349 LoopBlocksDFS::RPOIterator BlockBegin = LoopBlocks.beginRPO();
350 LoopBlocksDFS::RPOIterator BlockEnd = LoopBlocks.endRPO();
351 Loop *ParentLoop = L->getParentLoop();
352 NewLoopsMap NewLoops;
353 NewLoops[ParentLoop] = ParentLoop;
354
355 // For each block in the original loop, create a new copy,
356 // and update the value map with the newly created values.
357 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
358 BasicBlock *NewBB = CloneBasicBlock(*BB, VMap, "." + suffix, F);
359 NewBlocks.push_back(NewBB);
360
361 addClonedBlockToLoopInfo(*BB, NewBB, LI, NewLoops);
362
363 VMap[*BB] = NewBB;
364 if (Header == *BB) {
365 // For the first block, add a CFG connection to this newly
366 // created block.
367 InsertTop->getTerminator()->setSuccessor(0, NewBB);
368 }
369
370 if (DT) {
371 if (Header == *BB) {
372 // The header is dominated by the preheader.
373 DT->addNewBlock(NewBB, InsertTop);
374 } else {
375 // Copy information from original loop to unrolled loop.
376 BasicBlock *IDomBB = DT->getNode(*BB)->getIDom()->getBlock();
377 DT->addNewBlock(NewBB, cast<BasicBlock>(VMap[IDomBB]));
378 }
379 }
380
381 if (Latch == *BB) {
382 // For the last block, create a loop back to cloned head.
383 VMap.erase((*BB)->getTerminator());
384 // Use an incrementing IV. Pre-incr/post-incr is backedge/trip count.
385 // Subtle: NewIter can be 0 if we wrapped when computing the trip count,
386 // thus we must compare the post-increment (wrapping) value.
387 BasicBlock *FirstLoopBB = cast<BasicBlock>(VMap[Header]);
388 BranchInst *LatchBR = cast<BranchInst>(NewBB->getTerminator());
389 IRBuilder<> Builder(LatchBR);
390 PHINode *NewIdx =
391 PHINode::Create(NewIter->getType(), 2, suffix + ".iter");
392 NewIdx->insertBefore(FirstLoopBB->getFirstNonPHIIt());
393 auto *Zero = ConstantInt::get(NewIdx->getType(), 0);
394 auto *One = ConstantInt::get(NewIdx->getType(), 1);
395 Value *IdxNext =
396 Builder.CreateAdd(NewIdx, One, NewIdx->getName() + ".next");
397 Value *IdxCmp = Builder.CreateICmpNE(IdxNext, NewIter, NewIdx->getName() + ".cmp");
398 MDNode *BranchWeights = nullptr;
399 if (hasBranchWeightMD(*LatchBR)) {
400 uint32_t ExitWeight;
401 uint32_t BackEdgeWeight;
402 if (Count >= 3) {
403 // Note: We do not enter this loop for zero-remainders. The check
404 // is at the end of the loop. We assume equal distribution between
405 // possible remainders in [1, Count).
406 ExitWeight = 1;
407 BackEdgeWeight = (Count - 2) / 2;
408 } else {
409 // Unnecessary backedge, should never be taken. The conditional
410 // jump should be optimized away later.
411 ExitWeight = 1;
412 BackEdgeWeight = 0;
413 }
414 MDBuilder MDB(Builder.getContext());
415 BranchWeights = MDB.createBranchWeights(BackEdgeWeight, ExitWeight);
416 }
417 Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot, BranchWeights);
418 NewIdx->addIncoming(Zero, InsertTop);
419 NewIdx->addIncoming(IdxNext, NewBB);
420 LatchBR->eraseFromParent();
421 }
422 }
423
424 // Change the incoming values to the ones defined in the preheader or
425 // cloned loop.
426 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
427 PHINode *NewPHI = cast<PHINode>(VMap[&*I]);
428 unsigned idx = NewPHI->getBasicBlockIndex(Preheader);
429 NewPHI->setIncomingBlock(idx, InsertTop);
430 BasicBlock *NewLatch = cast<BasicBlock>(VMap[Latch]);
431 idx = NewPHI->getBasicBlockIndex(Latch);
432 Value *InVal = NewPHI->getIncomingValue(idx);
433 NewPHI->setIncomingBlock(idx, NewLatch);
434 if (Value *V = VMap.lookup(InVal))
435 NewPHI->setIncomingValue(idx, V);
436 }
437
438 Loop *NewLoop = NewLoops[L];
439 assert(NewLoop && "L should have been cloned");
440 MDNode *LoopID = NewLoop->getLoopID();
441
442 // Only add loop metadata if the loop is not going to be completely
443 // unrolled.
444 if (UnrollRemainder)
445 return NewLoop;
446
447 std::optional<MDNode *> NewLoopID = makeFollowupLoopID(
449 if (NewLoopID) {
450 NewLoop->setLoopID(*NewLoopID);
451
452 // Do not setLoopAlreadyUnrolled if loop attributes have been defined
453 // explicitly.
454 return NewLoop;
455 }
456
457 // Add unroll disable metadata to disable future unrolling for this loop.
458 NewLoop->setLoopAlreadyUnrolled();
459 return NewLoop;
460}
461
462/// Returns true if we can profitably unroll the multi-exit loop L. Currently,
463/// we return true only if UnrollRuntimeMultiExit is set to true.
465 Loop *L, SmallVectorImpl<BasicBlock *> &OtherExits, BasicBlock *LatchExit,
466 bool UseEpilogRemainder) {
467
468 // The main pain point with multi-exit loop unrolling is that once unrolled,
469 // we will not be able to merge all blocks into a straight line code.
470 // There are branches within the unrolled loop that go to the OtherExits.
471 // The second point is the increase in code size, but this is true
472 // irrespective of multiple exits.
473
474 // Note: Both the heuristics below are coarse grained. We are essentially
475 // enabling unrolling of loops that have a single side exit other than the
476 // normal LatchExit (i.e. exiting into a deoptimize block).
477 // The heuristics considered are:
478 // 1. low number of branches in the unrolled version.
479 // 2. high predictability of these extra branches.
480 // We avoid unrolling loops that have more than two exiting blocks. This
481 // limits the total number of branches in the unrolled loop to be atmost
482 // the unroll factor (since one of the exiting blocks is the latch block).
483 SmallVector<BasicBlock*, 4> ExitingBlocks;
484 L->getExitingBlocks(ExitingBlocks);
485 if (ExitingBlocks.size() > 2)
486 return false;
487
488 // Allow unrolling of loops with no non latch exit blocks.
489 if (OtherExits.size() == 0)
490 return true;
491
492 // The second heuristic is that L has one exit other than the latchexit and
493 // that exit is a deoptimize block. We know that deoptimize blocks are rarely
494 // taken, which also implies the branch leading to the deoptimize block is
495 // highly predictable. When UnrollRuntimeOtherExitPredictable is specified, we
496 // assume the other exit branch is predictable even if it has no deoptimize
497 // call.
498 return (OtherExits.size() == 1 &&
500 OtherExits[0]->getPostdominatingDeoptimizeCall()));
501 // TODO: These can be fine-tuned further to consider code size or deopt states
502 // that are captured by the deoptimize exit block.
503 // Also, we can extend this to support more cases, if we actually
504 // know of kinds of multiexit loops that would benefit from unrolling.
505}
506
507/// Calculate ModVal = (BECount + 1) % Count on the abstract integer domain
508/// accounting for the possibility of unsigned overflow in the 2s complement
509/// domain. Preconditions:
510/// 1) TripCount = BECount + 1 (allowing overflow)
511/// 2) Log2(Count) <= BitWidth(BECount)
513 Value *TripCount, unsigned Count) {
514 // Note that TripCount is BECount + 1.
515 if (isPowerOf2_32(Count))
516 // If the expression is zero, then either:
517 // 1. There are no iterations to be run in the prolog/epilog loop.
518 // OR
519 // 2. The addition computing TripCount overflowed.
520 //
521 // If (2) is true, we know that TripCount really is (1 << BEWidth) and so
522 // the number of iterations that remain to be run in the original loop is a
523 // multiple Count == (1 << Log2(Count)) because Log2(Count) <= BEWidth (a
524 // precondition of this method).
525 return B.CreateAnd(TripCount, Count - 1, "xtraiter");
526
527 // As (BECount + 1) can potentially unsigned overflow we count
528 // (BECount % Count) + 1 which is overflow safe as BECount % Count < Count.
529 Constant *CountC = ConstantInt::get(BECount->getType(), Count);
530 Value *ModValTmp = B.CreateURem(BECount, CountC);
531 Value *ModValAdd = B.CreateAdd(ModValTmp,
532 ConstantInt::get(ModValTmp->getType(), 1));
533 // At that point (BECount % Count) + 1 could be equal to Count.
534 // To handle this case we need to take mod by Count one more time.
535 return B.CreateURem(ModValAdd, CountC, "xtraiter");
536}
537
538
539/// Insert code in the prolog/epilog code when unrolling a loop with a
540/// run-time trip-count.
541///
542/// This method assumes that the loop unroll factor is total number
543/// of loop bodies in the loop after unrolling. (Some folks refer
544/// to the unroll factor as the number of *extra* copies added).
545/// We assume also that the loop unroll factor is a power-of-two. So, after
546/// unrolling the loop, the number of loop bodies executed is 2,
547/// 4, 8, etc. Note - LLVM converts the if-then-sequence to a switch
548/// instruction in SimplifyCFG.cpp. Then, the backend decides how code for
549/// the switch instruction is generated.
550///
551/// ***Prolog case***
552/// extraiters = tripcount % loopfactor
553/// if (extraiters == 0) jump Loop:
554/// else jump Prol:
555/// Prol: LoopBody;
556/// extraiters -= 1 // Omitted if unroll factor is 2.
557/// if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
558/// if (tripcount < loopfactor) jump End:
559/// Loop:
560/// ...
561/// End:
562///
563/// ***Epilog case***
564/// extraiters = tripcount % loopfactor
565/// if (tripcount < loopfactor) jump LoopExit:
566/// unroll_iters = tripcount - extraiters
567/// Loop: LoopBody; (executes unroll_iter times);
568/// unroll_iter -= 1
569/// if (unroll_iter != 0) jump Loop:
570/// LoopExit:
571/// if (extraiters == 0) jump EpilExit:
572/// Epil: LoopBody; (executes extraiters times)
573/// extraiters -= 1 // Omitted if unroll factor is 2.
574/// if (extraiters != 0) jump Epil: // Omitted if unroll factor is 2.
575/// EpilExit:
576
578 Loop *L, unsigned Count, bool AllowExpensiveTripCount,
579 bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
581 const TargetTransformInfo *TTI, bool PreserveLCSSA,
582 unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
583 Loop **ResultLoop) {
584 LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
585 LLVM_DEBUG(L->dump());
586 LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
587 : dbgs() << "Using prolog remainder.\n");
588
589 // Make sure the loop is in canonical form.
590 if (!L->isLoopSimplifyForm()) {
591 LLVM_DEBUG(dbgs() << "Not in simplify form!\n");
592 return false;
593 }
594
595 // Guaranteed by LoopSimplifyForm.
596 BasicBlock *Latch = L->getLoopLatch();
597 BasicBlock *Header = L->getHeader();
598
599 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
600
601 if (!LatchBR || LatchBR->isUnconditional()) {
602 // The loop-rotate pass can be helpful to avoid this in many cases.
604 dbgs()
605 << "Loop latch not terminated by a conditional branch.\n");
606 return false;
607 }
608
609 unsigned ExitIndex = LatchBR->getSuccessor(0) == Header ? 1 : 0;
610 BasicBlock *LatchExit = LatchBR->getSuccessor(ExitIndex);
611
612 if (L->contains(LatchExit)) {
613 // Cloning the loop basic blocks (`CloneLoopBlocks`) requires that one of the
614 // targets of the Latch be an exit block out of the loop.
616 dbgs()
617 << "One of the loop latch successors must be the exit block.\n");
618 return false;
619 }
620
621 // These are exit blocks other than the target of the latch exiting block.
623 L->getUniqueNonLatchExitBlocks(OtherExits);
624 // Support only single exit and exiting block unless multi-exit loop
625 // unrolling is enabled.
626 if (!L->getExitingBlock() || OtherExits.size()) {
627 // We rely on LCSSA form being preserved when the exit blocks are transformed.
628 // (Note that only an off-by-default mode of the old PM disables PreserveLCCA.)
629 if (!PreserveLCSSA)
630 return false;
631
632 // Priority goes to UnrollRuntimeMultiExit if it's supplied.
635 return false;
636 } else {
637 // Otherwise perform multi-exit unrolling, if either the target indicates
638 // it is profitable or the general profitability heuristics apply.
639 if (!RuntimeUnrollMultiExit &&
640 !canProfitablyRuntimeUnrollMultiExitLoop(L, OtherExits, LatchExit,
641 UseEpilogRemainder)) {
642 LLVM_DEBUG(dbgs() << "Multiple exit/exiting blocks in loop and "
643 "multi-exit unrolling not enabled!\n");
644 return false;
645 }
646 }
647 }
648 // Use Scalar Evolution to compute the trip count. This allows more loops to
649 // be unrolled than relying on induction var simplification.
650 if (!SE)
651 return false;
652
653 // Only unroll loops with a computable trip count.
654 // We calculate the backedge count by using getExitCount on the Latch block,
655 // which is proven to be the only exiting block in this loop. This is same as
656 // calculating getBackedgeTakenCount on the loop (which computes SCEV for all
657 // exiting blocks).
658 const SCEV *BECountSC = SE->getExitCount(L, Latch);
659 if (isa<SCEVCouldNotCompute>(BECountSC)) {
660 LLVM_DEBUG(dbgs() << "Could not compute exit block SCEV\n");
661 return false;
662 }
663
664 unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();
665
666 // Add 1 since the backedge count doesn't include the first loop iteration.
667 // (Note that overflow can occur, this is handled explicitly below)
668 const SCEV *TripCountSC =
669 SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
670 if (isa<SCEVCouldNotCompute>(TripCountSC)) {
671 LLVM_DEBUG(dbgs() << "Could not compute trip count SCEV.\n");
672 return false;
673 }
674
675 BasicBlock *PreHeader = L->getLoopPreheader();
676 BranchInst *PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
677 const DataLayout &DL = Header->getDataLayout();
678 SCEVExpander Expander(*SE, DL, "loop-unroll");
679 if (!AllowExpensiveTripCount &&
680 Expander.isHighCostExpansion(TripCountSC, L, SCEVExpansionBudget, TTI,
681 PreHeaderBR)) {
682 LLVM_DEBUG(dbgs() << "High cost for expanding trip count scev!\n");
683 return false;
684 }
685
686 // This constraint lets us deal with an overflowing trip count easily; see the
687 // comment on ModVal below.
688 if (Log2_32(Count) > BEWidth) {
690 dbgs()
691 << "Count failed constraint on overflow trip count calculation.\n");
692 return false;
693 }
694
695 // Loop structure is the following:
696 //
697 // PreHeader
698 // Header
699 // ...
700 // Latch
701 // LatchExit
702
703 BasicBlock *NewPreHeader;
704 BasicBlock *NewExit = nullptr;
705 BasicBlock *PrologExit = nullptr;
706 BasicBlock *EpilogPreHeader = nullptr;
707 BasicBlock *PrologPreHeader = nullptr;
708
709 if (UseEpilogRemainder) {
710 // If epilog remainder
711 // Split PreHeader to insert a branch around loop for unrolling.
712 NewPreHeader = SplitBlock(PreHeader, PreHeader->getTerminator(), DT, LI);
713 NewPreHeader->setName(PreHeader->getName() + ".new");
714 // Split LatchExit to create phi nodes from branch above.
715 NewExit = SplitBlockPredecessors(LatchExit, {Latch}, ".unr-lcssa", DT, LI,
716 nullptr, PreserveLCSSA);
717 // NewExit gets its DebugLoc from LatchExit, which is not part of the
718 // original Loop.
719 // Fix this by setting Loop's DebugLoc to NewExit.
720 auto *NewExitTerminator = NewExit->getTerminator();
721 NewExitTerminator->setDebugLoc(Header->getTerminator()->getDebugLoc());
722 // Split NewExit to insert epilog remainder loop.
723 EpilogPreHeader = SplitBlock(NewExit, NewExitTerminator, DT, LI);
724 EpilogPreHeader->setName(Header->getName() + ".epil.preheader");
725
726 // If the latch exits from multiple level of nested loops, then
727 // by assumption there must be another loop exit which branches to the
728 // outer loop and we must adjust the loop for the newly inserted blocks
729 // to account for the fact that our epilogue is still in the same outer
730 // loop. Note that this leaves loopinfo temporarily out of sync with the
731 // CFG until the actual epilogue loop is inserted.
732 if (auto *ParentL = L->getParentLoop())
733 if (LI->getLoopFor(LatchExit) != ParentL) {
734 LI->removeBlock(NewExit);
735 ParentL->addBasicBlockToLoop(NewExit, *LI);
736 LI->removeBlock(EpilogPreHeader);
737 ParentL->addBasicBlockToLoop(EpilogPreHeader, *LI);
738 }
739
740 } else {
741 // If prolog remainder
742 // Split the original preheader twice to insert prolog remainder loop
743 PrologPreHeader = SplitEdge(PreHeader, Header, DT, LI);
744 PrologPreHeader->setName(Header->getName() + ".prol.preheader");
745 PrologExit = SplitBlock(PrologPreHeader, PrologPreHeader->getTerminator(),
746 DT, LI);
747 PrologExit->setName(Header->getName() + ".prol.loopexit");
748 // Split PrologExit to get NewPreHeader.
749 NewPreHeader = SplitBlock(PrologExit, PrologExit->getTerminator(), DT, LI);
750 NewPreHeader->setName(PreHeader->getName() + ".new");
751 }
752 // Loop structure should be the following:
753 // Epilog Prolog
754 //
755 // PreHeader PreHeader
756 // *NewPreHeader *PrologPreHeader
757 // Header *PrologExit
758 // ... *NewPreHeader
759 // Latch Header
760 // *NewExit ...
761 // *EpilogPreHeader Latch
762 // LatchExit LatchExit
763
764 // Calculate conditions for branch around loop for unrolling
765 // in epilog case and around prolog remainder loop in prolog case.
766 // Compute the number of extra iterations required, which is:
767 // extra iterations = run-time trip count % loop unroll factor
768 PreHeaderBR = cast<BranchInst>(PreHeader->getTerminator());
769 IRBuilder<> B(PreHeaderBR);
770 Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
771 PreHeaderBR);
772 Value *BECount;
773 // If there are other exits before the latch, that may cause the latch exit
774 // branch to never be executed, and the latch exit count may be poison.
775 // In this case, freeze the TripCount and base BECount on the frozen
776 // TripCount. We will introduce two branches using these values, and it's
777 // important that they see a consistent value (which would not be guaranteed
778 // if were frozen independently.)
779 if ((!OtherExits.empty() || !SE->loopHasNoAbnormalExits(L)) &&
780 !isGuaranteedNotToBeUndefOrPoison(TripCount, AC, PreHeaderBR, DT)) {
781 TripCount = B.CreateFreeze(TripCount);
782 BECount =
783 B.CreateAdd(TripCount, Constant::getAllOnesValue(TripCount->getType()));
784 } else {
785 // If we don't need to freeze, use SCEVExpander for BECount as well, to
786 // allow slightly better value reuse.
787 BECount =
788 Expander.expandCodeFor(BECountSC, BECountSC->getType(), PreHeaderBR);
789 }
790
791 Value * const ModVal = CreateTripRemainder(B, BECount, TripCount, Count);
792
793 Value *BranchVal =
794 UseEpilogRemainder ? B.CreateICmpULT(BECount,
795 ConstantInt::get(BECount->getType(),
796 Count - 1)) :
797 B.CreateIsNotNull(ModVal, "lcmp.mod");
798 BasicBlock *RemainderLoop = UseEpilogRemainder ? NewExit : PrologPreHeader;
799 BasicBlock *UnrollingLoop = UseEpilogRemainder ? NewPreHeader : PrologExit;
800 // Branch to either remainder (extra iterations) loop or unrolling loop.
801 MDNode *BranchWeights = nullptr;
802 if (hasBranchWeightMD(*Latch->getTerminator())) {
803 // Assume loop is nearly always entered.
804 MDBuilder MDB(B.getContext());
805 BranchWeights = MDB.createBranchWeights(EpilogHeaderWeights);
806 }
807 B.CreateCondBr(BranchVal, RemainderLoop, UnrollingLoop, BranchWeights);
808 PreHeaderBR->eraseFromParent();
809 if (DT) {
810 if (UseEpilogRemainder)
811 DT->changeImmediateDominator(NewExit, PreHeader);
812 else
813 DT->changeImmediateDominator(PrologExit, PreHeader);
814 }
815 Function *F = Header->getParent();
816 // Get an ordered list of blocks in the loop to help with the ordering of the
817 // cloned blocks in the prolog/epilog code
818 LoopBlocksDFS LoopBlocks(L);
819 LoopBlocks.perform(LI);
820
821 //
822 // For each extra loop iteration, create a copy of the loop's basic blocks
823 // and generate a condition that branches to the copy depending on the
824 // number of 'left over' iterations.
825 //
826 std::vector<BasicBlock *> NewBlocks;
828
829 // Clone all the basic blocks in the loop. If Count is 2, we don't clone
830 // the loop, otherwise we create a cloned loop to execute the extra
831 // iterations. This function adds the appropriate CFG connections.
832 BasicBlock *InsertBot = UseEpilogRemainder ? LatchExit : PrologExit;
833 BasicBlock *InsertTop = UseEpilogRemainder ? EpilogPreHeader : PrologPreHeader;
834 Loop *remainderLoop = CloneLoopBlocks(
835 L, ModVal, UseEpilogRemainder, UnrollRemainder, InsertTop, InsertBot,
836 NewPreHeader, NewBlocks, LoopBlocks, VMap, DT, LI, Count);
837
838 // Insert the cloned blocks into the function.
839 F->splice(InsertBot->getIterator(), F, NewBlocks[0]->getIterator(), F->end());
840
841 // Now the loop blocks are cloned and the other exiting blocks from the
842 // remainder are connected to the original Loop's exit blocks. The remaining
843 // work is to update the phi nodes in the original loop, and take in the
844 // values from the cloned region.
845 for (auto *BB : OtherExits) {
846 // Given we preserve LCSSA form, we know that the values used outside the
847 // loop will be used through these phi nodes at the exit blocks that are
848 // transformed below.
849 for (PHINode &PN : BB->phis()) {
850 unsigned oldNumOperands = PN.getNumIncomingValues();
851 // Add the incoming values from the remainder code to the end of the phi
852 // node.
853 for (unsigned i = 0; i < oldNumOperands; i++){
854 auto *PredBB =PN.getIncomingBlock(i);
855 if (PredBB == Latch)
856 // The latch exit is handled separately, see connectX
857 continue;
858 if (!L->contains(PredBB))
859 // Even if we had dedicated exits, the code above inserted an
860 // extra branch which can reach the latch exit.
861 continue;
862
863 auto *V = PN.getIncomingValue(i);
864 if (Instruction *I = dyn_cast<Instruction>(V))
865 if (L->contains(I))
866 V = VMap.lookup(I);
867 PN.addIncoming(V, cast<BasicBlock>(VMap[PredBB]));
868 }
869 }
870#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
871 for (BasicBlock *SuccBB : successors(BB)) {
872 assert(!(llvm::is_contained(OtherExits, SuccBB) || SuccBB == LatchExit) &&
873 "Breaks the definition of dedicated exits!");
874 }
875#endif
876 }
877
878 // Update the immediate dominator of the exit blocks and blocks that are
879 // reachable from the exit blocks. This is needed because we now have paths
880 // from both the original loop and the remainder code reaching the exit
881 // blocks. While the IDom of these exit blocks were from the original loop,
882 // now the IDom is the preheader (which decides whether the original loop or
883 // remainder code should run).
884 if (DT && !L->getExitingBlock()) {
885 SmallVector<BasicBlock *, 16> ChildrenToUpdate;
886 // NB! We have to examine the dom children of all loop blocks, not just
887 // those which are the IDom of the exit blocks. This is because blocks
888 // reachable from the exit blocks can have their IDom as the nearest common
889 // dominator of the exit blocks.
890 for (auto *BB : L->blocks()) {
891 auto *DomNodeBB = DT->getNode(BB);
892 for (auto *DomChild : DomNodeBB->children()) {
893 auto *DomChildBB = DomChild->getBlock();
894 if (!L->contains(LI->getLoopFor(DomChildBB)))
895 ChildrenToUpdate.push_back(DomChildBB);
896 }
897 }
898 for (auto *BB : ChildrenToUpdate)
899 DT->changeImmediateDominator(BB, PreHeader);
900 }
901
902 // Loop structure should be the following:
903 // Epilog Prolog
904 //
905 // PreHeader PreHeader
906 // NewPreHeader PrologPreHeader
907 // Header PrologHeader
908 // ... ...
909 // Latch PrologLatch
910 // NewExit PrologExit
911 // EpilogPreHeader NewPreHeader
912 // EpilogHeader Header
913 // ... ...
914 // EpilogLatch Latch
915 // LatchExit LatchExit
916
917 // Rewrite the cloned instruction operands to use the values created when the
918 // clone is created.
919 for (BasicBlock *BB : NewBlocks) {
920 Module *M = BB->getModule();
921 for (Instruction &I : *BB) {
922 RemapInstruction(&I, VMap,
924 RemapDbgRecordRange(M, I.getDbgRecordRange(), VMap,
926 }
927 }
928
929 if (UseEpilogRemainder) {
930 // Connect the epilog code to the original loop and update the
931 // PHI functions.
932 ConnectEpilog(L, ModVal, NewExit, LatchExit, PreHeader, EpilogPreHeader,
933 NewPreHeader, VMap, DT, LI, PreserveLCSSA, *SE, Count);
934
935 // Update counter in loop for unrolling.
936 // Use an incrementing IV. Pre-incr/post-incr is backedge/trip count.
937 // Subtle: TestVal can be 0 if we wrapped when computing the trip count,
938 // thus we must compare the post-increment (wrapping) value.
939 IRBuilder<> B2(NewPreHeader->getTerminator());
940 Value *TestVal = B2.CreateSub(TripCount, ModVal, "unroll_iter");
941 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator());
942 PHINode *NewIdx = PHINode::Create(TestVal->getType(), 2, "niter");
943 NewIdx->insertBefore(Header->getFirstNonPHIIt());
944 B2.SetInsertPoint(LatchBR);
945 auto *Zero = ConstantInt::get(NewIdx->getType(), 0);
946 auto *One = ConstantInt::get(NewIdx->getType(), 1);
947 Value *IdxNext = B2.CreateAdd(NewIdx, One, NewIdx->getName() + ".next");
948 auto Pred = LatchBR->getSuccessor(0) == Header ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
949 Value *IdxCmp = B2.CreateICmp(Pred, IdxNext, TestVal, NewIdx->getName() + ".ncmp");
950 NewIdx->addIncoming(Zero, NewPreHeader);
951 NewIdx->addIncoming(IdxNext, Latch);
952 LatchBR->setCondition(IdxCmp);
953 } else {
954 // Connect the prolog code to the original loop and update the
955 // PHI functions.
956 ConnectProlog(L, BECount, Count, PrologExit, LatchExit, PreHeader,
957 NewPreHeader, VMap, DT, LI, PreserveLCSSA, *SE);
958 }
959
960 // If this loop is nested, then the loop unroller changes the code in the any
961 // of its parent loops, so the Scalar Evolution pass needs to be run again.
962 SE->forgetTopmostLoop(L);
963
964 // Verify that the Dom Tree and Loop Info are correct.
965#if defined(EXPENSIVE_CHECKS) && !defined(NDEBUG)
966 if (DT) {
967 assert(DT->verify(DominatorTree::VerificationLevel::Full));
968 LI->verify(*DT);
969 }
970#endif
971
972 // For unroll factor 2 remainder loop will have 1 iteration.
973 if (Count == 2 && DT && LI && SE) {
974 // TODO: This code could probably be pulled out into a helper function
975 // (e.g. breakLoopBackedgeAndSimplify) and reused in loop-deletion.
976 BasicBlock *RemainderLatch = remainderLoop->getLoopLatch();
977 assert(RemainderLatch);
978 SmallVector<BasicBlock *> RemainderBlocks(remainderLoop->getBlocks());
979 breakLoopBackedge(remainderLoop, *DT, *SE, *LI, nullptr);
980 remainderLoop = nullptr;
981
982 // Simplify loop values after breaking the backedge
983 const DataLayout &DL = L->getHeader()->getDataLayout();
985 for (BasicBlock *BB : RemainderBlocks) {
986 for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
987 if (Value *V = simplifyInstruction(&Inst, {DL, nullptr, DT, AC}))
988 if (LI->replacementPreservesLCSSAForm(&Inst, V))
989 Inst.replaceAllUsesWith(V);
991 DeadInsts.emplace_back(&Inst);
992 }
993 // We can't do recursive deletion until we're done iterating, as we might
994 // have a phi which (potentially indirectly) uses instructions later in
995 // the block we're iterating through.
997 }
998
999 // Merge latch into exit block.
1000 auto *ExitBB = RemainderLatch->getSingleSuccessor();
1001 assert(ExitBB && "required after breaking cond br backedge");
1002 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
1003 MergeBlockIntoPredecessor(ExitBB, &DTU, LI);
1004 }
1005
1006 // Canonicalize to LoopSimplifyForm both original and remainder loops. We
1007 // cannot rely on the LoopUnrollPass to do this because it only does
1008 // canonicalization for parent/subloops and not the sibling loops.
1009 if (OtherExits.size() > 0) {
1010 // Generate dedicated exit blocks for the original loop, to preserve
1011 // LoopSimplifyForm.
1012 formDedicatedExitBlocks(L, DT, LI, nullptr, PreserveLCSSA);
1013 // Generate dedicated exit blocks for the remainder loop if one exists, to
1014 // preserve LoopSimplifyForm.
1015 if (remainderLoop)
1016 formDedicatedExitBlocks(remainderLoop, DT, LI, nullptr, PreserveLCSSA);
1017 }
1018
1019 auto UnrollResult = LoopUnrollResult::Unmodified;
1020 if (remainderLoop && UnrollRemainder) {
1021 LLVM_DEBUG(dbgs() << "Unrolling remainder loop\n");
1023 ULO.Count = Count - 1;
1024 ULO.Force = false;
1025 ULO.Runtime = false;
1026 ULO.AllowExpensiveTripCount = false;
1027 ULO.UnrollRemainder = false;
1028 ULO.ForgetAllSCEV = ForgetAllSCEV;
1030 "A loop with a convergence heart does not allow runtime unrolling.");
1031 UnrollResult = UnrollLoop(remainderLoop, ULO, LI, SE, DT, AC, TTI,
1032 /*ORE*/ nullptr, PreserveLCSSA);
1033 }
1034
1035 if (ResultLoop && UnrollResult != LoopUnrollResult::FullyUnrolled)
1036 *ResultLoop = remainderLoop;
1037 NumRuntimeUnrolled++;
1038 return true;
1039}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Module.h This file contains the declarations for the Module class.
static Loop * CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder, const bool UnrollRemainder, BasicBlock *InsertTop, BasicBlock *InsertBot, BasicBlock *Preheader, std::vector< BasicBlock * > &NewBlocks, LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap, DominatorTree *DT, LoopInfo *LI, unsigned Count)
Create a clone of the blocks in a loop and connect them together.
static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit, BasicBlock *Exit, BasicBlock *PreHeader, BasicBlock *EpilogPreHeader, BasicBlock *NewPreHeader, ValueToValueMapTy &VMap, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA, ScalarEvolution &SE, unsigned Count)
Connect the unrolling epilog code to the original loop.
static const uint32_t UnrolledLoopHeaderWeights[]
static Value * CreateTripRemainder(IRBuilder<> &B, Value *BECount, Value *TripCount, unsigned Count)
Calculate ModVal = (BECount + 1) % Count on the abstract integer domain accounting for the possibilit...
static cl::opt< bool > UnrollRuntimeOtherExitPredictable("unroll-runtime-other-exit-predictable", cl::init(false), cl::Hidden, cl::desc("Assume the non latch exit block to be predictable"))
static bool canProfitablyRuntimeUnrollMultiExitLoop(Loop *L, SmallVectorImpl< BasicBlock * > &OtherExits, BasicBlock *LatchExit, bool UseEpilogRemainder)
Returns true if we can profitably unroll the multi-exit loop L.
static const uint32_t EpilogHeaderWeights[]
static cl::opt< bool > UnrollRuntimeMultiExit("unroll-runtime-multi-exit", cl::init(false), cl::Hidden, cl::desc("Allow runtime unrolling for loops with multiple exits, when " "epilog is generated"))
static void ConnectProlog(Loop *L, Value *BECount, unsigned Count, BasicBlock *PrologExit, BasicBlock *OriginalLoopLatchExit, BasicBlock *PreHeader, BasicBlock *NewPreHeader, ValueToValueMapTy &VMap, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA, ScalarEvolution &SE)
Connect the unrolling prolog code to the original loop.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for profiling metadata utility functions.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
#define LLVM_DEBUG(...)
Definition: Debug.h:119
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition: BasicBlock.h:528
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:337
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:467
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:233
Conditional or Unconditional Branch instruction.
void setCondition(Value *V)
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
This is an important base class in LLVM.
Definition: Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
Definition: Constants.cpp:420
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
bool verify(VerificationLevel VL=VerificationLevel::Full) const
verify - checks if the tree is correct.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
LLVM_ABI Instruction * findNearestCommonDominator(Instruction *I1, Instruction *I2) const
Find the nearest instruction I that dominates both I1 and I2, in the sense that a result produced bef...
Definition: Dominators.cpp:357
Value * CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2333
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1420
BranchInst * CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False, MDNode *BranchWeights=nullptr, MDNode *Unpredictable=nullptr)
Create a conditional 'br Cond, TrueDest, FalseDest' instruction.
Definition: IRBuilder.h:1197
LLVMContext & getContext() const
Definition: IRBuilder.h:203
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1403
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:207
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:2439
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2780
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:510
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
Store the result of a depth first search within basic blocks contained by a single loop.
Definition: LoopIterator.h:97
RPOIterator beginRPO() const
Reverse iterate over the cached postorder blocks.
Definition: LoopIterator.h:136
std::vector< BasicBlock * >::const_reverse_iterator RPOIterator
Definition: LoopIterator.h:101
void perform(const LoopInfo *LI)
Traverse the loop blocks and store the DFS result.
Definition: LoopInfo.cpp:1276
RPOIterator endRPO() const
Definition: LoopIterator.h:140
void verify(const DominatorTreeBase< BlockT, false > &DomTree) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
bool replacementPreservesLCSSAForm(Instruction *From, Value *To)
Returns true if replacing From with To everywhere is guaranteed to preserve LCSSA form.
Definition: LoopInfo.h:442
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:40
void setLoopID(MDNode *LoopID) const
Set the llvm.loop loop id metadata for this loop.
Definition: LoopInfo.cpp:538
void setLoopAlreadyUnrolled()
Add llvm.loop.unroll.disable to this loop's loop id metadata.
Definition: LoopInfo.cpp:550
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:514
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition: MDBuilder.cpp:38
Metadata node.
Definition: Metadata.h:1077
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValueForBlock(const BasicBlock *BB, Value *V)
Set every incoming value(s) for block BB to V.
void setIncomingBlock(unsigned i, BasicBlock *BB)
void setIncomingValue(unsigned i, Value *V)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
This class uses information about analyze scalars to rewrite expressions in canonical form.
bool isHighCostExpansion(ArrayRef< const SCEV * > Exprs, Loop *L, unsigned Budget, const TargetTransformInfo *TTI, const Instruction *At)
Return true for expressions that can't be evaluated at runtime within given Budget.
LLVM_ABI Value * expandCodeFor(const SCEV *SH, Type *Ty, BasicBlock::iterator I)
Insert code to directly compute the specified SCEV expression into the program.
This class represents an analyzed expression in the program.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
bool loopHasNoAbnormalExits(const Loop *L)
Return true if the loop has no abnormal exits.
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
LLVM_ABI void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
ValueT lookup(const KeyT &Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: ValueMap.h:169
bool erase(const KeyT &Val)
Definition: ValueMap.h:195
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:390
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:322
int getNumOccurrences() const
Definition: CommandLine.h:400
const ParentTy * getParent() const
Definition: ilist_node.h:34
self_iterator getIterator()
Definition: ilist_node.h:134
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:533
LLVM_ABI BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=nullptr, ClonedCodeInfo *CodeInfo=nullptr, bool MapAtoms=true)
Return a copy of the specified basic block, but without embedding the block into a particular functio...
auto successors(const MachineBasicBlock *BB)
LLVM_ABI std::optional< MDNode * > makeFollowupLoopID(MDNode *OrigLoopID, ArrayRef< StringRef > FollowupAttrs, const char *InheritOptionsAttrsPrefix="", bool AlwaysNew=false)
Create a new loop identifier for a loop created from a loop transformation.
Definition: LoopUtils.cpp:264
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:663
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:402
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:336
void RemapDbgRecordRange(Module *M, iterator_range< DbgRecordIterator > Range, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Remap the Values used in the DbgRecords Range using the value map VM.
Definition: ValueMapper.h:317
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:288
LLVM_ABI CallBase * getLoopConvergenceHeart(const Loop *TheLoop)
Find the convergence heart of the loop.
Definition: LoopInfo.cpp:1144
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
Definition: ValueMapper.h:98
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
Definition: ValueMapper.h:80
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
LLVM_ABI void breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, MemorySSA *MSSA)
Remove the backedge of the specified loop.
Definition: LoopUtils.cpp:711
LLVM_ABI BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
const char *const LLVMLoopUnrollFollowupAll
Definition: UnrollLoop.h:45
LLVM_ABI bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr, bool PredecessorWithTwoSuccessors=false, DominatorTree *DT=nullptr)
Attempts to merge a block into its predecessor, if possible.
LLVM_ABI bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
Definition: LoopUtils.cpp:58
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
Definition: ValueMapper.h:289
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
const char *const LLVMLoopUnrollFollowupRemainder
Definition: UnrollLoop.h:48
LLVM_ABI const Loop * addClonedBlockToLoopInfo(BasicBlock *OriginalBB, BasicBlock *ClonedBB, LoopInfo *LI, NewLoopsMap &NewLoops)
Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary and adds a mapping from the o...
Definition: LoopUnroll.cpp:146
LLVM_ABI BasicBlock * SplitBlock(BasicBlock *Old, BasicBlock::iterator SplitPt, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="", bool Before=false)
Split the specified block at the specified instruction.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1916
LLVM_ABI bool UnrollRuntimeLoopRemainder(Loop *L, unsigned Count, bool AllowExpensiveTripCount, bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, const TargetTransformInfo *TTI, bool PreserveLCSSA, unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit, Loop **ResultLoop=nullptr)
Insert code in the prolog/epilog code when unrolling a loop with a run-time trip-count.
LLVM_ABI bool hasBranchWeightMD(const Instruction &I)
Checks if an instructions has Branch Weight Metadata.
LLVM_ABI BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
LLVM_ABI LoopUnrollResult UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, const llvm::TargetTransformInfo *TTI, OptimizationRemarkEmitter *ORE, bool PreserveLCSSA, Loop **RemainderLoop=nullptr, AAResults *AA=nullptr)
Unroll the given loop by Count.
Definition: LoopUnroll.cpp:456