diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2021-06-05 10:49:51 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2021-06-07 21:08:42 +0200 |
commit | 8fdd7c2ff16da370e28ef1b22e400d57a541484f (patch) | |
tree | ca339a4206602a2273fb4c7dbb15729eb85264cb /llvm/lib/Transforms/Utils/LoopUnroll.cpp | |
parent | d70e1f12765fd6bb644ffb561dff9a31c522308e (diff) | |
download | llvm-8fdd7c2ff16da370e28ef1b22e400d57a541484f.zip llvm-8fdd7c2ff16da370e28ef1b22e400d57a541484f.tar.gz llvm-8fdd7c2ff16da370e28ef1b22e400d57a541484f.tar.bz2 |
[LoopUnroll] Clamp unroll count to MaxTripCount
Unrolling with more iterations than MaxTripCount is pointless, as
those iterations can never be executed. As such, we clamp ULO.Count
to MaxTripCount if it is known. This means we no longer need to
consider iterations after MaxTripCount for exit folding, and the
CompletelyUnroll flag becomes independent of ULO.TripCount.
Differential Revision: https://reviews.llvm.org/D103748
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUnroll.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/LoopUnroll.cpp | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp index f7590acc..b3658fb 100644 --- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp @@ -301,11 +301,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, if (ULO.TripMultiple != 1) LLVM_DEBUG(dbgs() << " Trip Multiple = " << ULO.TripMultiple << "\n"); - // Effectively "DCE" unrolled iterations that are beyond the tripcount - // and will never be executed. - if (ULO.TripCount != 0 && ULO.Count > ULO.TripCount) - ULO.Count = ULO.TripCount; - // Don't enter the unroll code if there is nothing to do. if (ULO.TripCount == 0 && ULO.Count < 2) { LLVM_DEBUG(dbgs() << "Won't unroll; almost nothing to do\n"); @@ -316,17 +311,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, assert(ULO.TripMultiple > 0); assert(ULO.TripCount == 0 || ULO.TripCount % ULO.TripMultiple == 0); - // Are we eliminating the loop control altogether? Note that we can know - // we're eliminating the backedge without knowing exactly which iteration - // of the unrolled body exits. - const bool CompletelyUnroll = ULO.Count == ULO.TripCount; - - // We assume a run-time trip count if the compiler cannot - // figure out the loop trip count and the unroll-runtime - // flag is specified. - bool RuntimeTripCount = - (ULO.TripCount == 0 && ULO.Count > 0 && ULO.AllowRuntime); - // All these values should be taken only after peeling because they might have // changed. BasicBlock *Preheader = L->getLoopPreheader(); @@ -336,6 +320,27 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, L->getExitBlocks(ExitBlocks); std::vector<BasicBlock *> OriginalLoopBlocks = L->getBlocks(); + const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L); + const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L); + + // Effectively "DCE" unrolled iterations that are beyond the max tripcount + // and will never be executed. + if (MaxTripCount && ULO.Count > MaxTripCount) + ULO.Count = MaxTripCount; + + // Are we eliminating the loop control altogether? Note that we can know + // we're eliminating the backedge without knowing exactly which iteration + // of the unrolled body exits. + const bool CompletelyUnroll = ULO.Count == MaxTripCount; + + const bool PreserveOnlyFirst = CompletelyUnroll && MaxOrZero; + + // We assume a run-time trip count if the compiler cannot + // figure out the loop trip count and the unroll-runtime + // flag is specified. + bool RuntimeTripCount = + !CompletelyUnroll && ULO.TripCount == 0 && ULO.AllowRuntime; + // Go through all exits of L and see if there are any phi-nodes there. We just // conservatively assume that they're inserted to preserve LCSSA form, which // means that complete unrolling might break this form. We need to either fix @@ -347,11 +352,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, any_of(ExitBlocks, [](const BasicBlock *BB) { return isa<PHINode>(BB->begin()); }); - const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L); - const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L); - - const bool PreserveOnlyFirst = ULO.Count == MaxTripCount && MaxOrZero; - // The current loop unroll pass can unroll loops that have // (1) single latch; and // (2a) latch is unconditional; or @@ -728,8 +728,6 @@ LoopUnrollResult llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, // Complete (but possibly inexact) unrolling if (j == 0) return true; - if (MaxTripCount && j >= MaxTripCount) - return false; // Warning: ExactTripCount is the trip count of the exiting // block which ends in ExitingBI, not neccessarily the loop. if (ExactTripCount && j != ExactTripCount) |