LLVM 22.0.0git
LoopUnroll.cpp
Go to the documentation of this file.
1//===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements some loop unrolling utilities. It does not define any
10// actual pass or policy, but provides a single function to perform loop
11// unrolling.
12//
13// The process of unrolling can produce extraneous basic blocks linked with
14// unconditional branches. This will be corrected in the future.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/Twine.h"
36#include "llvm/IR/BasicBlock.h"
37#include "llvm/IR/CFG.h"
38#include "llvm/IR/Constants.h"
40#include "llvm/IR/DebugLoc.h"
42#include "llvm/IR/Dominators.h"
43#include "llvm/IR/Function.h"
44#include "llvm/IR/Instruction.h"
47#include "llvm/IR/Metadata.h"
49#include "llvm/IR/Use.h"
50#include "llvm/IR/User.h"
51#include "llvm/IR/ValueHandle.h"
52#include "llvm/IR/ValueMap.h"
55#include "llvm/Support/Debug.h"
66#include <assert.h>
67#include <numeric>
68#include <type_traits>
69#include <vector>
70
71namespace llvm {
72class DataLayout;
73class Value;
74} // namespace llvm
75
76using namespace llvm;
77
78#define DEBUG_TYPE "loop-unroll"
79
80// TODO: Should these be here or in LoopUnroll?
81STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
82STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)");
83STATISTIC(NumUnrolledNotLatch, "Number of loops unrolled without a conditional "
84 "latch (completely or otherwise)");
85
86static cl::opt<bool>
87UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden,
88 cl::desc("Allow runtime unrolled loops to be unrolled "
89 "with epilog instead of prolog."));
90
91static cl::opt<bool>
92UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden,
93 cl::desc("Verify domtree after unrolling"),
94#ifdef EXPENSIVE_CHECKS
95 cl::init(true)
96#else
97 cl::init(false)
98#endif
99 );
100
101static cl::opt<bool>
102UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden,
103 cl::desc("Verify loopinfo after unrolling"),
104#ifdef EXPENSIVE_CHECKS
105 cl::init(true)
106#else
107 cl::init(false)
108#endif
109 );
110
111
112/// Check if unrolling created a situation where we need to insert phi nodes to
113/// preserve LCSSA form.
114/// \param Blocks is a vector of basic blocks representing unrolled loop.
115/// \param L is the outer loop.
116/// It's possible that some of the blocks are in L, and some are not. In this
117/// case, if there is a use is outside L, and definition is inside L, we need to
118/// insert a phi-node, otherwise LCSSA will be broken.
119/// The function is just a helper function for llvm::UnrollLoop that returns
120/// true if this situation occurs, indicating that LCSSA needs to be fixed.
122 const std::vector<BasicBlock *> &Blocks,
123 LoopInfo *LI) {
124 for (BasicBlock *BB : Blocks) {
125 if (LI->getLoopFor(BB) == L)
126 continue;
127 for (Instruction &I : *BB) {
128 for (Use &U : I.operands()) {
129 if (const auto *Def = dyn_cast<Instruction>(U)) {
130 Loop *DefLoop = LI->getLoopFor(Def->getParent());
131 if (!DefLoop)
132 continue;
133 if (DefLoop->contains(L))
134 return true;
135 }
136 }
137 }
138 }
139 return false;
140}
141
142/// Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary
143/// and adds a mapping from the original loop to the new loop to NewLoops.
144/// Returns nullptr if no new loop was created and a pointer to the
145/// original loop OriginalBB was part of otherwise.
147 BasicBlock *ClonedBB, LoopInfo *LI,
148 NewLoopsMap &NewLoops) {
149 // Figure out which loop New is in.
150 const Loop *OldLoop = LI->getLoopFor(OriginalBB);
151 assert(OldLoop && "Should (at least) be in the loop being unrolled!");
152
153 Loop *&NewLoop = NewLoops[OldLoop];
154 if (!NewLoop) {
155 // Found a new sub-loop.
156 assert(OriginalBB == OldLoop->getHeader() &&
157 "Header should be first in RPO");
158
159 NewLoop = LI->AllocateLoop();
160 Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop());
161
162 if (NewLoopParent)
163 NewLoopParent->addChildLoop(NewLoop);
164 else
165 LI->addTopLevelLoop(NewLoop);
166
167 NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
168 return OldLoop;
169 } else {
170 NewLoop->addBasicBlockToLoop(ClonedBB, *LI);
171 return nullptr;
172 }
173}
174
175/// The function chooses which type of unroll (epilog or prolog) is more
176/// profitabale.
177/// Epilog unroll is more profitable when there is PHI that starts from
178/// constant. In this case epilog will leave PHI start from constant,
179/// but prolog will convert it to non-constant.
180///
181/// loop:
182/// PN = PHI [I, Latch], [CI, PreHeader]
183/// I = foo(PN)
184/// ...
185///
186/// Epilog unroll case.
187/// loop:
188/// PN = PHI [I2, Latch], [CI, PreHeader]
189/// I1 = foo(PN)
190/// I2 = foo(I1)
191/// ...
192/// Prolog unroll case.
193/// NewPN = PHI [PrologI, Prolog], [CI, PreHeader]
194/// loop:
195/// PN = PHI [I2, Latch], [NewPN, PreHeader]
196/// I1 = foo(PN)
197/// I2 = foo(I1)
198/// ...
199///
200static bool isEpilogProfitable(Loop *L) {
201 BasicBlock *PreHeader = L->getLoopPreheader();
202 BasicBlock *Header = L->getHeader();
203 assert(PreHeader && Header);
204 for (const PHINode &PN : Header->phis()) {
205 if (isa<ConstantInt>(PN.getIncomingValueForBlock(PreHeader)))
206 return true;
207 }
208 return false;
209}
210
211struct LoadValue {
212 Instruction *DefI = nullptr;
213 unsigned Generation = 0;
214 LoadValue() = default;
216 : DefI(Inst), Generation(Generation) {}
217};
218
221 unsigned CurrentGeneration;
222 unsigned ChildGeneration;
223 DomTreeNode *Node;
226 bool Processed = false;
227
228public:
230 unsigned cg, DomTreeNode *N, DomTreeNode::const_iterator Child,
232 : LoadScope(AvailableLoads), CurrentGeneration(cg), ChildGeneration(cg),
233 Node(N), ChildIter(Child), EndIter(End) {}
234 // Accessors.
235 unsigned currentGeneration() const { return CurrentGeneration; }
236 unsigned childGeneration() const { return ChildGeneration; }
237 void childGeneration(unsigned generation) { ChildGeneration = generation; }
238 DomTreeNode *node() { return Node; }
239 DomTreeNode::const_iterator childIter() const { return ChildIter; }
240
242 DomTreeNode *Child = *ChildIter;
243 ++ChildIter;
244 return Child;
245 }
246
247 DomTreeNode::const_iterator end() const { return EndIter; }
248 bool isProcessed() const { return Processed; }
249 void process() { Processed = true; }
250};
251
252Value *getMatchingValue(LoadValue LV, LoadInst *LI, unsigned CurrentGeneration,
253 BatchAAResults &BAA,
254 function_ref<MemorySSA *()> GetMSSA) {
255 if (!LV.DefI)
256 return nullptr;
257 if (LV.DefI->getType() != LI->getType())
258 return nullptr;
259 if (LV.Generation != CurrentGeneration) {
260 MemorySSA *MSSA = GetMSSA();
261 if (!MSSA)
262 return nullptr;
263 auto *EarlierMA = MSSA->getMemoryAccess(LV.DefI);
264 MemoryAccess *LaterDef =
265 MSSA->getWalker()->getClobberingMemoryAccess(LI, BAA);
266 if (!MSSA->dominates(LaterDef, EarlierMA))
267 return nullptr;
268 }
269 return LV.DefI;
270}
271
273 BatchAAResults &BAA, function_ref<MemorySSA *()> GetMSSA) {
276 DomTreeNode *HeaderD = DT.getNode(L->getHeader());
277 NodesToProcess.emplace_back(new StackNode(AvailableLoads, 0, HeaderD,
278 HeaderD->begin(), HeaderD->end()));
279
280 unsigned CurrentGeneration = 0;
281 while (!NodesToProcess.empty()) {
282 StackNode *NodeToProcess = &*NodesToProcess.back();
283
284 CurrentGeneration = NodeToProcess->currentGeneration();
285
286 if (!NodeToProcess->isProcessed()) {
287 // Process the node.
288
289 // If this block has a single predecessor, then the predecessor is the
290 // parent
291 // of the domtree node and all of the live out memory values are still
292 // current in this block. If this block has multiple predecessors, then
293 // they could have invalidated the live-out memory values of our parent
294 // value. For now, just be conservative and invalidate memory if this
295 // block has multiple predecessors.
296 if (!NodeToProcess->node()->getBlock()->getSinglePredecessor())
297 ++CurrentGeneration;
298 for (auto &I : make_early_inc_range(*NodeToProcess->node()->getBlock())) {
299
300 auto *Load = dyn_cast<LoadInst>(&I);
301 if (!Load || !Load->isSimple()) {
302 if (I.mayWriteToMemory())
303 CurrentGeneration++;
304 continue;
305 }
306
307 const SCEV *PtrSCEV = SE.getSCEV(Load->getPointerOperand());
308 LoadValue LV = AvailableLoads.lookup(PtrSCEV);
309 if (Value *M =
310 getMatchingValue(LV, Load, CurrentGeneration, BAA, GetMSSA)) {
311 if (LI.replacementPreservesLCSSAForm(Load, M)) {
312 Load->replaceAllUsesWith(M);
313 Load->eraseFromParent();
314 }
315 } else {
316 AvailableLoads.insert(PtrSCEV, LoadValue(Load, CurrentGeneration));
317 }
318 }
319 NodeToProcess->childGeneration(CurrentGeneration);
320 NodeToProcess->process();
321 } else if (NodeToProcess->childIter() != NodeToProcess->end()) {
322 // Push the next child onto the stack.
323 DomTreeNode *Child = NodeToProcess->nextChild();
324 if (!L->contains(Child->getBlock()))
325 continue;
326 NodesToProcess.emplace_back(
327 new StackNode(AvailableLoads, NodeToProcess->childGeneration(), Child,
328 Child->begin(), Child->end()));
329 } else {
330 // It has been processed, and there are no more children to process,
331 // so delete it and pop it off the stack.
332 NodesToProcess.pop_back();
333 }
334 }
335}
336
337/// Perform some cleanup and simplifications on loops after unrolling. It is
338/// useful to simplify the IV's in the new loop, as well as do a quick
339/// simplify/dce pass of the instructions.
340void llvm::simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
342 AssumptionCache *AC,
344 AAResults *AA) {
345 using namespace llvm::PatternMatch;
346
347 // Simplify any new induction variables in the partially unrolled loop.
348 if (SE && SimplifyIVs) {
350 simplifyLoopIVs(L, SE, DT, LI, TTI, DeadInsts);
351
352 // Aggressively clean up dead instructions that simplifyLoopIVs already
353 // identified. Any remaining should be cleaned up below.
354 while (!DeadInsts.empty()) {
355 Value *V = DeadInsts.pop_back_val();
356 if (Instruction *Inst = dyn_cast_or_null<Instruction>(V))
358 }
359
360 if (AA) {
361 std::unique_ptr<MemorySSA> MSSA = nullptr;
362 BatchAAResults BAA(*AA);
363 loadCSE(L, *DT, *SE, *LI, BAA, [L, AA, DT, &MSSA]() -> MemorySSA * {
364 if (!MSSA)
365 MSSA.reset(new MemorySSA(*L, AA, DT));
366 return &*MSSA;
367 });
368 }
369 }
370
371 // At this point, the code is well formed. Perform constprop, instsimplify,
372 // and dce.
373 const DataLayout &DL = L->getHeader()->getDataLayout();
375 for (BasicBlock *BB : L->getBlocks()) {
376 // Remove repeated debug instructions after loop unrolling.
377 if (BB->getParent()->getSubprogram())
379
380 for (Instruction &Inst : llvm::make_early_inc_range(*BB)) {
381 if (Value *V = simplifyInstruction(&Inst, {DL, nullptr, DT, AC}))
382 if (LI->replacementPreservesLCSSAForm(&Inst, V))
383 Inst.replaceAllUsesWith(V);
385 DeadInsts.emplace_back(&Inst);
386
387 // Fold ((add X, C1), C2) to (add X, C1+C2). This is very common in
388 // unrolled loops, and handling this early allows following code to
389 // identify the IV as a "simple recurrence" without first folding away
390 // a long chain of adds.
391 {
392 Value *X;
393 const APInt *C1, *C2;
394 if (match(&Inst, m_Add(m_Add(m_Value(X), m_APInt(C1)), m_APInt(C2)))) {
395 auto *InnerI = dyn_cast<Instruction>(Inst.getOperand(0));
396 auto *InnerOBO = cast<OverflowingBinaryOperator>(Inst.getOperand(0));
397 bool SignedOverflow;
398 APInt NewC = C1->sadd_ov(*C2, SignedOverflow);
399 Inst.setOperand(0, X);
400 Inst.setOperand(1, ConstantInt::get(Inst.getType(), NewC));
401 Inst.setHasNoUnsignedWrap(Inst.hasNoUnsignedWrap() &&
402 InnerOBO->hasNoUnsignedWrap());
403 Inst.setHasNoSignedWrap(Inst.hasNoSignedWrap() &&
404 InnerOBO->hasNoSignedWrap() &&
405 !SignedOverflow);
406 if (InnerI && isInstructionTriviallyDead(InnerI))
407 DeadInsts.emplace_back(InnerI);
408 }
409 }
410 }
411 // We can't do recursive deletion until we're done iterating, as we might
412 // have a phi which (potentially indirectly) uses instructions later in
413 // the block we're iterating through.
415 }
416}
417
418// Loops containing convergent instructions that are uncontrolled or controlled
419// from outside the loop must have a count that divides their TripMultiple.
421static bool canHaveUnrollRemainder(const Loop *L) {
423 return false;
424
425 // Check for uncontrolled convergent operations.
426 for (auto &BB : L->blocks()) {
427 for (auto &I : *BB) {
428 if (isa<ConvergenceControlInst>(I))
429 return true;
430 if (auto *CB = dyn_cast<CallBase>(&I))
431 if (CB->isConvergent())
432 return CB->getConvergenceControlToken();
433 }
434 }
435 return true;
436}
437
438/// Unroll the given loop by Count. The loop must be in LCSSA form. Unrolling
439/// can only fail when the loop's latch block is not terminated by a conditional
440/// branch instruction. However, if the trip count (and multiple) are not known,
441/// loop unrolling will mostly produce more code that is no faster.
442///
443/// If Runtime is true then UnrollLoop will try to insert a prologue or
444/// epilogue that ensures the latch has a trip multiple of Count. UnrollLoop
445/// will not runtime-unroll the loop if computing the run-time trip count will
446/// be expensive and AllowExpensiveTripCount is false.
447///
448/// The LoopInfo Analysis that is passed will be kept consistent.
449///
450/// This utility preserves LoopInfo. It will also preserve ScalarEvolution and
451/// DominatorTree if they are non-null.
452///
453/// If RemainderLoop is non-null, it will receive the remainder loop (if
454/// required and not fully unrolled).
459 bool PreserveLCSSA, Loop **RemainderLoop, AAResults *AA) {
460 assert(DT && "DomTree is required");
461
462 if (!L->getLoopPreheader()) {
463 LLVM_DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n");
464 return LoopUnrollResult::Unmodified;
465 }
466
467 if (!L->getLoopLatch()) {
468 LLVM_DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n");
469 return LoopUnrollResult::Unmodified;
470 }
471
472 // Loops with indirectbr cannot be cloned.
473 if (!L->isSafeToClone()) {
474 LLVM_DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n");
475 return LoopUnrollResult::Unmodified;
476 }
477
478 if (L->getHeader()->hasAddressTaken()) {
479 // The loop-rotate pass can be helpful to avoid this in many cases.
481 dbgs() << " Won't unroll loop: address of header block is taken.\n");
482 return LoopUnrollResult::Unmodified;
483 }
484
485 assert(ULO.Count > 0);
486
487 // All these values should be taken only after peeling because they might have
488 // changed.
489 BasicBlock *Preheader = L->getLoopPreheader();
490 BasicBlock *Header = L->getHeader();
491 BasicBlock *LatchBlock = L->getLoopLatch();
493 L->getExitBlocks(ExitBlocks);
494 std::vector<BasicBlock *> OriginalLoopBlocks = L->getBlocks();
495
496 const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L);
497 const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L);
498 unsigned EstimatedLoopInvocationWeight = 0;
499 std::optional<unsigned> OriginalTripCount =
500 llvm::getLoopEstimatedTripCount(L, &EstimatedLoopInvocationWeight);
501
502 // Effectively "DCE" unrolled iterations that are beyond the max tripcount
503 // and will never be executed.
504 if (MaxTripCount && ULO.Count > MaxTripCount)
505 ULO.Count = MaxTripCount;
506
507 struct ExitInfo {
508 unsigned TripCount;
509 unsigned TripMultiple;
510 unsigned BreakoutTrip;
511 bool ExitOnTrue;
512 BasicBlock *FirstExitingBlock = nullptr;
513 SmallVector<BasicBlock *> ExitingBlocks;
514 };
516 SmallVector<BasicBlock *, 4> ExitingBlocks;
517 L->getExitingBlocks(ExitingBlocks);
518 for (auto *ExitingBlock : ExitingBlocks) {
519 // The folding code is not prepared to deal with non-branch instructions
520 // right now.
521 auto *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
522 if (!BI)
523 continue;
524
525 ExitInfo &Info = ExitInfos[ExitingBlock];
526 Info.TripCount = SE->getSmallConstantTripCount(L, ExitingBlock);
527 Info.TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock);
528 if (Info.TripCount != 0) {
529 Info.BreakoutTrip = Info.TripCount % ULO.Count;
530 Info.TripMultiple = 0;
531 } else {
532 Info.BreakoutTrip = Info.TripMultiple =
533 (unsigned)std::gcd(ULO.Count, Info.TripMultiple);
534 }
535 Info.ExitOnTrue = !L->contains(BI->getSuccessor(0));
536 Info.ExitingBlocks.push_back(ExitingBlock);
537 LLVM_DEBUG(dbgs() << " Exiting block %" << ExitingBlock->getName()
538 << ": TripCount=" << Info.TripCount
539 << ", TripMultiple=" << Info.TripMultiple
540 << ", BreakoutTrip=" << Info.BreakoutTrip << "\n");
541 }
542
543 // Are we eliminating the loop control altogether? Note that we can know
544 // we're eliminating the backedge without knowing exactly which iteration
545 // of the unrolled body exits.
546 const bool CompletelyUnroll = ULO.Count == MaxTripCount;
547
548 const bool PreserveOnlyFirst = CompletelyUnroll && MaxOrZero;
549
550 // There's no point in performing runtime unrolling if this unroll count
551 // results in a full unroll.
552 if (CompletelyUnroll)
553 ULO.Runtime = false;
554
555 // Go through all exits of L and see if there are any phi-nodes there. We just
556 // conservatively assume that they're inserted to preserve LCSSA form, which
557 // means that complete unrolling might break this form. We need to either fix
558 // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For
559 // now we just recompute LCSSA for the outer loop, but it should be possible
560 // to fix it in-place.
561 bool NeedToFixLCSSA =
562 PreserveLCSSA && CompletelyUnroll &&
563 any_of(ExitBlocks,
564 [](const BasicBlock *BB) { return isa<PHINode>(BB->begin()); });
565
566 // The current loop unroll pass can unroll loops that have
567 // (1) single latch; and
568 // (2a) latch is unconditional; or
569 // (2b) latch is conditional and is an exiting block
570 // FIXME: The implementation can be extended to work with more complicated
571 // cases, e.g. loops with multiple latches.
572 BranchInst *LatchBI = dyn_cast<BranchInst>(LatchBlock->getTerminator());
573
574 // A conditional branch which exits the loop, which can be optimized to an
575 // unconditional branch in the unrolled loop in some cases.
576 bool LatchIsExiting = L->isLoopExiting(LatchBlock);
577 if (!LatchBI || (LatchBI->isConditional() && !LatchIsExiting)) {
579 dbgs() << "Can't unroll; a conditional latch must exit the loop");
580 return LoopUnrollResult::Unmodified;
581 }
582
584 "Can't runtime unroll if loop contains a convergent operation.");
585
586 bool EpilogProfitability =
589
590 if (ULO.Runtime &&
592 EpilogProfitability, ULO.UnrollRemainder,
593 ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
594 PreserveLCSSA, ULO.SCEVExpansionBudget,
595 ULO.RuntimeUnrollMultiExit, RemainderLoop)) {
596 if (ULO.Force)
597 ULO.Runtime = false;
598 else {
599 LLVM_DEBUG(dbgs() << "Won't unroll; remainder loop could not be "
600 "generated when assuming runtime trip count\n");
601 return LoopUnrollResult::Unmodified;
602 }
603 }
604
605 using namespace ore;
606 // Report the unrolling decision.
607 if (CompletelyUnroll) {
608 LLVM_DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName()
609 << " with trip count " << ULO.Count << "!\n");
610 if (ORE)
611 ORE->emit([&]() {
612 return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(),
613 L->getHeader())
614 << "completely unrolled loop with "
615 << NV("UnrollCount", ULO.Count) << " iterations";
616 });
617 } else {
618 LLVM_DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() << " by "
619 << ULO.Count);
620 if (ULO.Runtime)
621 LLVM_DEBUG(dbgs() << " with run-time trip count");
622 LLVM_DEBUG(dbgs() << "!\n");
623
624 if (ORE)
625 ORE->emit([&]() {
626 OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(),
627 L->getHeader());
628 Diag << "unrolled loop by a factor of " << NV("UnrollCount", ULO.Count);
629 if (ULO.Runtime)
630 Diag << " with run-time trip count";
631 return Diag;
632 });
633 }
634
635 // We are going to make changes to this loop. SCEV may be keeping cached info
636 // about it, in particular about backedge taken count. The changes we make
637 // are guaranteed to invalidate this information for our loop. It is tempting
638 // to only invalidate the loop being unrolled, but it is incorrect as long as
639 // all exiting branches from all inner loops have impact on the outer loops,
640 // and if something changes inside them then any of outer loops may also
641 // change. When we forget outermost loop, we also forget all contained loops
642 // and this is what we need here.
643 if (SE) {
644 if (ULO.ForgetAllSCEV)
645 SE->forgetAllLoops();
646 else {
647 SE->forgetTopmostLoop(L);
649 }
650 }
651
652 if (!LatchIsExiting)
653 ++NumUnrolledNotLatch;
654
655 // For the first iteration of the loop, we should use the precloned values for
656 // PHI nodes. Insert associations now.
657 ValueToValueMapTy LastValueMap;
658 std::vector<PHINode*> OrigPHINode;
659 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
660 OrigPHINode.push_back(cast<PHINode>(I));
661 }
662
663 std::vector<BasicBlock *> Headers;
664 std::vector<BasicBlock *> Latches;
665 Headers.push_back(Header);
666 Latches.push_back(LatchBlock);
667
668 // The current on-the-fly SSA update requires blocks to be processed in
669 // reverse postorder so that LastValueMap contains the correct value at each
670 // exit.
671 LoopBlocksDFS DFS(L);
672 DFS.perform(LI);
673
674 // Stash the DFS iterators before adding blocks to the loop.
675 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO();
676 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO();
677
678 std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks();
679
680 // Loop Unrolling might create new loops. While we do preserve LoopInfo, we
681 // might break loop-simplified form for these loops (as they, e.g., would
682 // share the same exit blocks). We'll keep track of loops for which we can
683 // break this so that later we can re-simplify them.
684 SmallSetVector<Loop *, 4> LoopsToSimplify;
685 LoopsToSimplify.insert_range(*L);
686
687 // When a FSDiscriminator is enabled, we don't need to add the multiply
688 // factors to the discriminators.
689 if (Header->getParent()->shouldEmitDebugInfoForProfiling() &&
691 for (BasicBlock *BB : L->getBlocks())
692 for (Instruction &I : *BB)
693 if (!I.isDebugOrPseudoInst())
694 if (const DILocation *DIL = I.getDebugLoc()) {
695 auto NewDIL = DIL->cloneByMultiplyingDuplicationFactor(ULO.Count);
696 if (NewDIL)
697 I.setDebugLoc(*NewDIL);
698 else
700 << "Failed to create new discriminator: "
701 << DIL->getFilename() << " Line: " << DIL->getLine());
702 }
703
704 // Identify what noalias metadata is inside the loop: if it is inside the
705 // loop, the associated metadata must be cloned for each iteration.
706 SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
707 identifyNoAliasScopesToClone(L->getBlocks(), LoopLocalNoAliasDeclScopes);
708
709 // We place the unrolled iterations immediately after the original loop
710 // latch. This is a reasonable default placement if we don't have block
711 // frequencies, and if we do, well the layout will be adjusted later.
712 auto BlockInsertPt = std::next(LatchBlock->getIterator());
713 for (unsigned It = 1; It != ULO.Count; ++It) {
716 NewLoops[L] = L;
717
718 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) {
720 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It));
721 Header->getParent()->insert(BlockInsertPt, New);
722
723 assert((*BB != Header || LI->getLoopFor(*BB) == L) &&
724 "Header should not be in a sub-loop");
725 // Tell LI about New.
726 const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops);
727 if (OldLoop)
728 LoopsToSimplify.insert(NewLoops[OldLoop]);
729
730 if (*BB == Header) {
731 // Loop over all of the PHI nodes in the block, changing them to use
732 // the incoming values from the previous block.
733 for (PHINode *OrigPHI : OrigPHINode) {
734 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]);
735 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock);
736 if (Instruction *InValI = dyn_cast<Instruction>(InVal))
737 if (It > 1 && L->contains(InValI))
738 InVal = LastValueMap[InValI];
739 VMap[OrigPHI] = InVal;
740 NewPHI->eraseFromParent();
741 }
742
743 // Eliminate copies of the loop heart intrinsic, if any.
744 if (ULO.Heart) {
745 auto it = VMap.find(ULO.Heart);
746 assert(it != VMap.end());
747 Instruction *heartCopy = cast<Instruction>(it->second);
748 heartCopy->eraseFromParent();
749 VMap.erase(it);
750 }
751 }
752
753 // Remap source location atom instance. Do this now, rather than
754 // when we remap instructions, because remap is called once we've
755 // cloned all blocks (all the clones would get the same atom
756 // number).
757 if (!VMap.AtomMap.empty())
758 for (Instruction &I : *New)
759 RemapSourceAtom(&I, VMap);
760
761 // Update our running map of newest clones
762 LastValueMap[*BB] = New;
763 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end();
764 VI != VE; ++VI)
765 LastValueMap[VI->first] = VI->second;
766
767 // Add phi entries for newly created values to all exit blocks.
768 for (BasicBlock *Succ : successors(*BB)) {
769 if (L->contains(Succ))
770 continue;
771 for (PHINode &PHI : Succ->phis()) {
772 Value *Incoming = PHI.getIncomingValueForBlock(*BB);
773 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming);
774 if (It != LastValueMap.end())
775 Incoming = It->second;
776 PHI.addIncoming(Incoming, New);
778 }
779 }
780 // Keep track of new headers and latches as we create them, so that
781 // we can insert the proper branches later.
782 if (*BB == Header)
783 Headers.push_back(New);
784 if (*BB == LatchBlock)
785 Latches.push_back(New);
786
787 // Keep track of the exiting block and its successor block contained in
788 // the loop for the current iteration.
789 auto ExitInfoIt = ExitInfos.find(*BB);
790 if (ExitInfoIt != ExitInfos.end())
791 ExitInfoIt->second.ExitingBlocks.push_back(New);
792
793 NewBlocks.push_back(New);
794 UnrolledLoopBlocks.push_back(New);
795
796 // Update DomTree: since we just copy the loop body, and each copy has a
797 // dedicated entry block (copy of the header block), this header's copy
798 // dominates all copied blocks. That means, dominance relations in the
799 // copied body are the same as in the original body.
800 if (*BB == Header)
801 DT->addNewBlock(New, Latches[It - 1]);
802 else {
803 auto BBDomNode = DT->getNode(*BB);
804 auto BBIDom = BBDomNode->getIDom();
805 BasicBlock *OriginalBBIDom = BBIDom->getBlock();
806 DT->addNewBlock(
807 New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)]));
808 }
809 }
810
811 // Remap all instructions in the most recent iteration.
812 // Key Instructions: Nothing to do - we've already remapped the atoms.
813 remapInstructionsInBlocks(NewBlocks, LastValueMap);
814 for (BasicBlock *NewBlock : NewBlocks)
815 for (Instruction &I : *NewBlock)
816 if (auto *II = dyn_cast<AssumeInst>(&I))
818
819 {
820 // Identify what other metadata depends on the cloned version. After
821 // cloning, replace the metadata with the corrected version for both
822 // memory instructions and noalias intrinsics.
823 std::string ext = (Twine("It") + Twine(It)).str();
824 cloneAndAdaptNoAliasScopes(LoopLocalNoAliasDeclScopes, NewBlocks,
825 Header->getContext(), ext);
826 }
827 }
828
829 // Loop over the PHI nodes in the original block, setting incoming values.
830 for (PHINode *PN : OrigPHINode) {
831 if (CompletelyUnroll) {
832 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader));
833 PN->eraseFromParent();
834 } else if (ULO.Count > 1) {
835 Value *InVal = PN->removeIncomingValue(LatchBlock, false);
836 // If this value was defined in the loop, take the value defined by the
837 // last iteration of the loop.
838 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) {
839 if (L->contains(InValI))
840 InVal = LastValueMap[InVal];
841 }
842 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch");
843 PN->addIncoming(InVal, Latches.back());
844 }
845 }
846
847 // Connect latches of the unrolled iterations to the headers of the next
848 // iteration. Currently they point to the header of the same iteration.
849 for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
850 unsigned j = (i + 1) % e;
851 Latches[i]->getTerminator()->replaceSuccessorWith(Headers[i], Headers[j]);
852 }
853
854 // Update dominators of blocks we might reach through exits.
855 // Immediate dominator of such block might change, because we add more
856 // routes which can lead to the exit: we can now reach it from the copied
857 // iterations too.
858 if (ULO.Count > 1) {
859 for (auto *BB : OriginalLoopBlocks) {
860 auto *BBDomNode = DT->getNode(BB);
861 SmallVector<BasicBlock *, 16> ChildrenToUpdate;
862 for (auto *ChildDomNode : BBDomNode->children()) {
863 auto *ChildBB = ChildDomNode->getBlock();
864 if (!L->contains(ChildBB))
865 ChildrenToUpdate.push_back(ChildBB);
866 }
867 // The new idom of the block will be the nearest common dominator
868 // of all copies of the previous idom. This is equivalent to the
869 // nearest common dominator of the previous idom and the first latch,
870 // which dominates all copies of the previous idom.
871 BasicBlock *NewIDom = DT->findNearestCommonDominator(BB, LatchBlock);
872 for (auto *ChildBB : ChildrenToUpdate)
873 DT->changeImmediateDominator(ChildBB, NewIDom);
874 }
875 }
876
878 DT->verify(DominatorTree::VerificationLevel::Fast));
879
881 auto SetDest = [&](BasicBlock *Src, bool WillExit, bool ExitOnTrue) {
882 auto *Term = cast<BranchInst>(Src->getTerminator());
883 const unsigned Idx = ExitOnTrue ^ WillExit;
884 BasicBlock *Dest = Term->getSuccessor(Idx);
885 BasicBlock *DeadSucc = Term->getSuccessor(1-Idx);
886
887 // Remove predecessors from all non-Dest successors.
888 DeadSucc->removePredecessor(Src, /* KeepOneInputPHIs */ true);
889
890 // Replace the conditional branch with an unconditional one.
891 auto *BI = BranchInst::Create(Dest, Term->getIterator());
892 BI->setDebugLoc(Term->getDebugLoc());
893 Term->eraseFromParent();
894
895 DTUpdates.emplace_back(DominatorTree::Delete, Src, DeadSucc);
896 };
897
898 auto WillExit = [&](const ExitInfo &Info, unsigned i, unsigned j,
899 bool IsLatch) -> std::optional<bool> {
900 if (CompletelyUnroll) {
901 if (PreserveOnlyFirst) {
902 if (i == 0)
903 return std::nullopt;
904 return j == 0;
905 }
906 // Complete (but possibly inexact) unrolling
907 if (j == 0)
908 return true;
909 if (Info.TripCount && j != Info.TripCount)
910 return false;
911 return std::nullopt;
912 }
913
914 if (ULO.Runtime) {
915 // If runtime unrolling inserts a prologue, information about non-latch
916 // exits may be stale.
917 if (IsLatch && j != 0)
918 return false;
919 return std::nullopt;
920 }
921
922 if (j != Info.BreakoutTrip &&
923 (Info.TripMultiple == 0 || j % Info.TripMultiple != 0)) {
924 // If we know the trip count or a multiple of it, we can safely use an
925 // unconditional branch for some iterations.
926 return false;
927 }
928 return std::nullopt;
929 };
930
931 // Fold branches for iterations where we know that they will exit or not
932 // exit.
933 for (auto &Pair : ExitInfos) {
934 ExitInfo &Info = Pair.second;
935 for (unsigned i = 0, e = Info.ExitingBlocks.size(); i != e; ++i) {
936 // The branch destination.
937 unsigned j = (i + 1) % e;
938 bool IsLatch = Pair.first == LatchBlock;
939 std::optional<bool> KnownWillExit = WillExit(Info, i, j, IsLatch);
940 if (!KnownWillExit) {
941 if (!Info.FirstExitingBlock)
942 Info.FirstExitingBlock = Info.ExitingBlocks[i];
943 continue;
944 }
945
946 // We don't fold known-exiting branches for non-latch exits here,
947 // because this ensures that both all loop blocks and all exit blocks
948 // remain reachable in the CFG.
949 // TODO: We could fold these branches, but it would require much more
950 // sophisticated updates to LoopInfo.
951 if (*KnownWillExit && !IsLatch) {
952 if (!Info.FirstExitingBlock)
953 Info.FirstExitingBlock = Info.ExitingBlocks[i];
954 continue;
955 }
956
957 SetDest(Info.ExitingBlocks[i], *KnownWillExit, Info.ExitOnTrue);
958 }
959 }
960
961 DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
962 DomTreeUpdater *DTUToUse = &DTU;
963 if (ExitingBlocks.size() == 1 && ExitInfos.size() == 1) {
964 // Manually update the DT if there's a single exiting node. In that case
965 // there's a single exit node and it is sufficient to update the nodes
966 // immediately dominated by the original exiting block. They will become
967 // dominated by the first exiting block that leaves the loop after
968 // unrolling. Note that the CFG inside the loop does not change, so there's
969 // no need to update the DT inside the unrolled loop.
970 DTUToUse = nullptr;
971 auto &[OriginalExit, Info] = *ExitInfos.begin();
972 if (!Info.FirstExitingBlock)
973 Info.FirstExitingBlock = Info.ExitingBlocks.back();
974 for (auto *C : to_vector(DT->getNode(OriginalExit)->children())) {
975 if (L->contains(C->getBlock()))
976 continue;
977 C->setIDom(DT->getNode(Info.FirstExitingBlock));
978 }
979 } else {
980 DTU.applyUpdates(DTUpdates);
981 }
982
983 // When completely unrolling, the last latch becomes unreachable.
984 if (!LatchIsExiting && CompletelyUnroll) {
985 // There is no need to update the DT here, because there must be a unique
986 // latch. Hence if the latch is not exiting it must directly branch back to
987 // the original loop header and does not dominate any nodes.
988 assert(LatchBlock->getSingleSuccessor() && "Loop with multiple latches?");
989 changeToUnreachable(Latches.back()->getTerminator(), PreserveLCSSA);
990 }
991
992 // Merge adjacent basic blocks, if possible.
993 for (BasicBlock *Latch : Latches) {
994 BranchInst *Term = dyn_cast<BranchInst>(Latch->getTerminator());
995 assert((Term ||
996 (CompletelyUnroll && !LatchIsExiting && Latch == Latches.back())) &&
997 "Need a branch as terminator, except when fully unrolling with "
998 "unconditional latch");
999 if (Term && Term->isUnconditional()) {
1000 BasicBlock *Dest = Term->getSuccessor(0);
1001 BasicBlock *Fold = Dest->getUniquePredecessor();
1002 if (MergeBlockIntoPredecessor(Dest, /*DTU=*/DTUToUse, LI,
1003 /*MSSAU=*/nullptr, /*MemDep=*/nullptr,
1004 /*PredecessorWithTwoSuccessors=*/false,
1005 DTUToUse ? nullptr : DT)) {
1006 // Dest has been folded into Fold. Update our worklists accordingly.
1007 llvm::replace(Latches, Dest, Fold);
1008 llvm::erase(UnrolledLoopBlocks, Dest);
1009 }
1010 }
1011 }
1012
1013 if (DTUToUse) {
1014 // Apply updates to the DomTree.
1015 DT = &DTU.getDomTree();
1016 }
1018 DT->verify(DominatorTree::VerificationLevel::Fast));
1019
1020 // At this point, the code is well formed. We now simplify the unrolled loop,
1021 // doing constant propagation and dead code elimination as we go.
1022 simplifyLoopAfterUnroll(L, !CompletelyUnroll && ULO.Count > 1, LI, SE, DT, AC,
1023 TTI, AA);
1024
1025 NumCompletelyUnrolled += CompletelyUnroll;
1026 ++NumUnrolled;
1027
1028 Loop *OuterL = L->getParentLoop();
1029 // Update LoopInfo if the loop is completely removed.
1030 if (CompletelyUnroll) {
1031 LI->erase(L);
1032 // We shouldn't try to use `L` anymore.
1033 L = nullptr;
1034 } else if (OriginalTripCount) {
1035 // Update the trip count. Note that the remainder has already logic
1036 // computing it in `UnrollRuntimeLoopRemainder`.
1037 setLoopEstimatedTripCount(L, *OriginalTripCount / ULO.Count,
1038 EstimatedLoopInvocationWeight);
1039 }
1040
1041 // LoopInfo should not be valid, confirm that.
1043 LI->verify(*DT);
1044
1045 // After complete unrolling most of the blocks should be contained in OuterL.
1046 // However, some of them might happen to be out of OuterL (e.g. if they
1047 // precede a loop exit). In this case we might need to insert PHI nodes in
1048 // order to preserve LCSSA form.
1049 // We don't need to check this if we already know that we need to fix LCSSA
1050 // form.
1051 // TODO: For now we just recompute LCSSA for the outer loop in this case, but
1052 // it should be possible to fix it in-place.
1053 if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA)
1054 NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI);
1055
1056 // Make sure that loop-simplify form is preserved. We want to simplify
1057 // at least one layer outside of the loop that was unrolled so that any
1058 // changes to the parent loop exposed by the unrolling are considered.
1059 if (OuterL) {
1060 // OuterL includes all loops for which we can break loop-simplify, so
1061 // it's sufficient to simplify only it (it'll recursively simplify inner
1062 // loops too).
1063 if (NeedToFixLCSSA) {
1064 // LCSSA must be performed on the outermost affected loop. The unrolled
1065 // loop's last loop latch is guaranteed to be in the outermost loop
1066 // after LoopInfo's been updated by LoopInfo::erase.
1067 Loop *LatchLoop = LI->getLoopFor(Latches.back());
1068 Loop *FixLCSSALoop = OuterL;
1069 if (!FixLCSSALoop->contains(LatchLoop))
1070 while (FixLCSSALoop->getParentLoop() != LatchLoop)
1071 FixLCSSALoop = FixLCSSALoop->getParentLoop();
1072
1073 formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE);
1074 } else if (PreserveLCSSA) {
1075 assert(OuterL->isLCSSAForm(*DT) &&
1076 "Loops should be in LCSSA form after loop-unroll.");
1077 }
1078
1079 // TODO: That potentially might be compile-time expensive. We should try
1080 // to fix the loop-simplified form incrementally.
1081 simplifyLoop(OuterL, DT, LI, SE, AC, nullptr, PreserveLCSSA);
1082 } else {
1083 // Simplify loops for which we might've broken loop-simplify form.
1084 for (Loop *SubLoop : LoopsToSimplify)
1085 simplifyLoop(SubLoop, DT, LI, SE, AC, nullptr, PreserveLCSSA);
1086 }
1087
1088 return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled
1089 : LoopUnrollResult::PartiallyUnrolled;
1090}
1091
1092/// Given an llvm.loop loop id metadata node, returns the loop hint metadata
1093/// node with the given name (for example, "llvm.loop.unroll.count"). If no
1094/// such metadata node exists, then nullptr is returned.
1096 // First operand should refer to the loop id itself.
1097 assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1098 assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1099
1100 for (const MDOperand &MDO : llvm::drop_begin(LoopID->operands())) {
1101 MDNode *MD = dyn_cast<MDNode>(MDO);
1102 if (!MD)
1103 continue;
1104
1105 MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1106 if (!S)
1107 continue;
1108
1109 if (Name == S->getString())
1110 return MD;
1111 }
1112 return nullptr;
1113}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Rewrite undef for PHI
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Optimize for code generation
#define LLVM_ATTRIBUTE_USED
Definition: Compiler.h:236
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
std::string Name
bool End
Definition: ELF_riscv.cpp:480
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This file defines a set of templates that efficiently compute a dominator tree over a generic graph.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This defines the Use class.
static bool needToInsertPhisForLCSSA(Loop *L, const std::vector< BasicBlock * > &Blocks, LoopInfo *LI)
Check if unrolling created a situation where we need to insert phi nodes to preserve LCSSA form.
Definition: LoopUnroll.cpp:121
static bool isEpilogProfitable(Loop *L)
The function chooses which type of unroll (epilog or prolog) is more profitabale.
Definition: LoopUnroll.cpp:200
void loadCSE(Loop *L, DominatorTree &DT, ScalarEvolution &SE, LoopInfo &LI, BatchAAResults &BAA, function_ref< MemorySSA *()> GetMSSA)
Definition: LoopUnroll.cpp:272
Value * getMatchingValue(LoadValue LV, LoadInst *LI, unsigned CurrentGeneration, BatchAAResults &BAA, function_ref< MemorySSA *()> GetMSSA)
Definition: LoopUnroll.cpp:252
static cl::opt< bool > UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden, cl::desc("Allow runtime unrolled loops to be unrolled " "with epilog instead of prolog."))
#define DEBUG_TYPE
Definition: LoopUnroll.cpp:78
static cl::opt< bool > UnrollVerifyLoopInfo("unroll-verify-loopinfo", cl::Hidden, cl::desc("Verify loopinfo after unrolling"), cl::init(false))
static cl::opt< bool > UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden, cl::desc("Verify domtree after unrolling"), cl::init(false))
static LLVM_ATTRIBUTE_USED bool canHaveUnrollRemainder(const Loop *L)
Definition: LoopUnroll.cpp:421
#define I(x, y, z)
Definition: MD5.cpp:58
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallVector class.
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
void childGeneration(unsigned generation)
Definition: LoopUnroll.cpp:237
bool isProcessed() const
Definition: LoopUnroll.cpp:248
unsigned currentGeneration() const
Definition: LoopUnroll.cpp:235
unsigned childGeneration() const
Definition: LoopUnroll.cpp:236
StackNode(ScopedHashTable< const SCEV *, LoadValue > &AvailableLoads, unsigned cg, DomTreeNode *N, DomTreeNode::const_iterator Child, DomTreeNode::const_iterator End)
Definition: LoopUnroll.cpp:229
DomTreeNode::const_iterator end() const
Definition: LoopUnroll.cpp:247
void process()
Definition: LoopUnroll.cpp:249
DomTreeNode * nextChild()
Definition: LoopUnroll.cpp:241
DomTreeNode::const_iterator childIter() const
Definition: LoopUnroll.cpp:239
DomTreeNode * node()
Definition: LoopUnroll.cpp:238
A private abstract base class describing the concept of an individual alias analysis implementation.
Class for arbitrary precision integers.
Definition: APInt.h:78
LLVM_ABI APInt sadd_ov(const APInt &RHS, bool &Overflow) const
Definition: APInt.cpp:1928
A cache of @llvm.assume calls within a function.
LLVM_ABI void registerAssumption(AssumeInst *CI)
Add an @llvm.assume intrinsic to this function's cache.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:459
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:437
LLVM_ABI const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:445
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
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
Definition: BasicBlock.cpp:494
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
Conditional or Unconditional Branch instruction.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
Debug location.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:203
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:177
unsigned size() const
Definition: DenseMap.h:120
bool empty() const
Definition: DenseMap.h:119
iterator begin()
Definition: DenseMap.h:78
iterator end()
Definition: DenseMap.h:87
iterator_range< iterator > children()
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
typename SmallVector< DomTreeNodeBase *, 4 >::const_iterator const_iterator
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
DomTreeT & getDomTree()
Flush DomTree updates and return DomTree.
void applyUpdates(ArrayRef< UpdateT > Updates)
Submit updates to all available trees.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
An instruction for reading from memory.
Definition: Instructions.h:180
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
void addBasicBlockToLoop(BlockT *NewBB, LoopInfoBase< BlockT, LoopT > &LI)
This method is used by other analyses to update loop information.
void addChildLoop(LoopT *NewChild)
Add the specified loop to be a child of this loop.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
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
void addTopLevelLoop(LoopT *New)
This adds the specified loop to the collection of top-level loops.
LoopT * AllocateLoop(ArgsTy &&...Args)
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
LLVM_ABI void erase(Loop *L)
Update LoopInfo after removing the last backedge from a loop.
Definition: LoopInfo.cpp:899
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:40
bool isLCSSAForm(const DominatorTree &DT, bool IgnoreTokens=true) const
Return true if the Loop is in LCSSA form.
Definition: LoopInfo.cpp:475
Metadata node.
Definition: Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1445
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1443
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1451
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:899
A single uniqued string.
Definition: Metadata.h:720
LLVM_ABI StringRef getString() const
Definition: Metadata.cpp:617
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Definition: MemorySSA.h:1053
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:702
LLVM_ABI bool dominates(const MemoryAccess *A, const MemoryAccess *B) const
Given two memory accesses in potentially different blocks, determine whether MemoryAccess A dominates...
Definition: MemorySSA.cpp:2173
LLVM_ABI MemorySSAWalker * getWalker()
Definition: MemorySSA.cpp:1590
MemoryUseOrDef * getMemoryAccess(const Instruction *I) const
Given a memory Mod/Ref'ing instruction, get the MemorySSA access associated with it.
Definition: MemorySSA.h:720
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for applied optimization remarks.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This class represents an analyzed expression in the program.
The main scalar evolution driver.
LLVM_ABI unsigned getSmallConstantTripMultiple(const Loop *L, const SCEV *ExitCount)
Returns the largest constant divisor of the trip count as a normal unsigned value,...
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI unsigned getSmallConstantMaxTripCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns the upper bound of the loop trip count as a normal unsigned value.
LLVM_ABI bool isBackedgeTakenCountMaxOrZero(const Loop *L)
Return true if the backedge taken count is either the value returned by getConstantMaxBackedgeTakenCo...
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
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 unsigned getSmallConstantTripCount(const Loop *L)
Returns the exact trip count of the loop if we can compute it, and the result is a small constant.
LLVM_ABI void forgetAllLoops()
void insert(const K &Key, const V &Val)
V lookup(const K &Key) const
void insert_range(Range &&R)
Definition: SetVector.h:193
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:168
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:356
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:82
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
iterator find(const KeyT &Val)
Definition: ValueMap.h:160
iterator begin()
Definition: ValueMap.h:138
iterator end()
Definition: ValueMap.h:139
bool erase(const KeyT &Val)
Definition: ValueMap.h:195
DMAtomT AtomMap
Map {(InlinedAt, old atom number) -> new atom number}.
Definition: ValueMap.h:123
LLVM Value Representation.
Definition: Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:256
int getNumOccurrences() const
Definition: CommandLine.h:400
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:134
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
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 simplifyLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE, AssumptionCache *AC, MemorySSAUpdater *MSSAU, bool PreserveLCSSA)
Simplify each loop in a loop nest recursively.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:338
LLVM_ABI bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
LLVM_ABI std::optional< unsigned > getLoopEstimatedTripCount(Loop *L, unsigned *EstimatedLoopInvocationWeight=nullptr)
Returns a loop's estimated trip count based on branch weight metadata.
Definition: LoopUtils.cpp:841
LLVM_ABI void simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, const TargetTransformInfo *TTI, AAResults *AA=nullptr)
Perform some cleanup and simplifications on loops after unrolling.
Definition: LoopUnroll.cpp:340
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 bool formLCSSARecursively(Loop &L, const DominatorTree &DT, const LoopInfo *LI, ScalarEvolution *SE)
Put a loop nest into LCSSA form.
Definition: LCSSA.cpp:449
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.
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition: STLExtras.h:2147
LLVM_ABI cl::opt< bool > EnableFSDiscriminator
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
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
LLVM_ABI CallBase * getLoopConvergenceHeart(const Loop *TheLoop)
Find the convergence heart of the loop.
Definition: LoopInfo.cpp:1144
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead)
SimplifyLoopIVs - Simplify users of induction variables within this loop.
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Definition: SmallVector.h:1300
LoopUnrollResult
Represents the result of a UnrollLoop invocation.
Definition: UnrollLoop.h:58
LLVM_ABI unsigned changeToUnreachable(Instruction *I, bool PreserveLCSSA=false, DomTreeUpdater *DTU=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Insert an unreachable instruction before the specified instruction, making it and the rest of the cod...
Definition: Local.cpp:2513
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.
void replace(R &&Range, const T &OldValue, const T &NewValue)
Provide wrappers to std::replace which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1879
LLVM_ABI bool setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount, unsigned EstimatedLoopInvocationWeight)
Set a loop's branch weight metadata to reflect that loop has EstimatedTripCount iterations and Estima...
Definition: LoopUtils.cpp:859
LLVM_ABI void cloneAndAdaptNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, ArrayRef< BasicBlock * > NewBlocks, LLVMContext &Context, StringRef Ext)
Clone the specified noalias decl scopes.
LLVM_ABI void remapInstructionsInBlocks(ArrayRef< BasicBlock * > Blocks, ValueToValueMapTy &VMap)
Remaps instructions in Blocks using the mapping in VMap.
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 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 void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
LLVM_ABI MDNode * GetUnrollMetadata(MDNode *LoopID, StringRef Name)
Given an llvm.loop loop id metadata node, returns the loop hint metadata node with the given name (fo...
LLVM_ABI void RemapSourceAtom(Instruction *I, ValueToValueMapTy &VM)
Remap source location atom.
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
#define N
Instruction * DefI
Definition: LoopUnroll.cpp:212
LoadValue()=default
unsigned Generation
Definition: LoopUnroll.cpp:213
LoadValue(Instruction *Inst, unsigned Generation)
Definition: LoopUnroll.cpp:215
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
const Instruction * Heart
Definition: UnrollLoop.h:79
unsigned SCEVExpansionBudget
Definition: UnrollLoop.h:80