aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/LoopPeel.cpp
diff options
context:
space:
mode:
authorMax Kazantsev <mkazantsev@azul.com>2021-11-09 18:11:46 +0700
committerMax Kazantsev <mkazantsev@azul.com>2021-11-09 18:13:19 +0700
commitcb728cb8a9b3d0329b41dbe6d0de830eb2a0312b (patch)
tree5a5df048a414d7851fab82017cda9052b36765ad /llvm/lib/Transforms/Utils/LoopPeel.cpp
parentd484cc152b1d9282230a17a218337342d52536e2 (diff)
downloadllvm-cb728cb8a9b3d0329b41dbe6d0de830eb2a0312b.zip
llvm-cb728cb8a9b3d0329b41dbe6d0de830eb2a0312b.tar.gz
llvm-cb728cb8a9b3d0329b41dbe6d0de830eb2a0312b.tar.bz2
[NFC] Get rid of hardcoded magical constant and use Optionals instead
Refactor calculateIterationsToInvariance so that it doesn't need a magical constant to signify unknown answer.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopPeel.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/LoopPeel.cpp32
1 files changed, 13 insertions, 19 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopPeel.cpp b/llvm/lib/Transforms/Utils/LoopPeel.cpp
index d6bdc1b..f3cf42b 100644
--- a/llvm/lib/Transforms/Utils/LoopPeel.cpp
+++ b/llvm/lib/Transforms/Utils/LoopPeel.cpp
@@ -76,12 +76,6 @@ static cl::opt<unsigned> UnrollForcePeelCount(
static const char *PeeledCountMetaData = "llvm.loop.peeled.count";
-// Designates that a Phi is estimated to become invariant after an "infinite"
-// number of loop iterations (i.e. only may become an invariant if the loop is
-// fully unrolled).
-static const unsigned InfiniteIterationsToInvariance =
- std::numeric_limits<unsigned>::max();
-
// Check whether we are capable of peeling this loop.
bool llvm::canPeel(Loop *L) {
// Make sure the loop is in simplified form
@@ -128,9 +122,9 @@ bool llvm::canPeel(Loop *L) {
// %x = phi(0, %a), <-- becomes invariant starting from 3rd iteration.
// %y = phi(0, 5),
// %a = %y + 1.
-static unsigned calculateIterationsToInvariance(
+static Optional<unsigned> calculateIterationsToInvariance(
PHINode *Phi, Loop *L, BasicBlock *BackEdge,
- SmallDenseMap<PHINode *, unsigned> &IterationsToInvariance) {
+ SmallDenseMap<PHINode *, Optional<unsigned> > &IterationsToInvariance) {
assert(Phi->getParent() == L->getHeader() &&
"Non-loop Phi should not be checked for turning into invariant.");
assert(BackEdge == L->getLoopLatch() && "Wrong latch?");
@@ -143,25 +137,25 @@ static unsigned calculateIterationsToInvariance(
Value *Input = Phi->getIncomingValueForBlock(BackEdge);
// Place infinity to map to avoid infinite recursion for cycled Phis. Such
// cycles can never stop on an invariant.
- IterationsToInvariance[Phi] = InfiniteIterationsToInvariance;
- unsigned ToInvariance = InfiniteIterationsToInvariance;
+ IterationsToInvariance[Phi] = None;
+ Optional<unsigned> ToInvariance = None;
if (L->isLoopInvariant(Input))
ToInvariance = 1u;
else if (PHINode *IncPhi = dyn_cast<PHINode>(Input)) {
// Only consider Phis in header block.
if (IncPhi->getParent() != L->getHeader())
- return InfiniteIterationsToInvariance;
+ return None;
// If the input becomes an invariant after X iterations, then our Phi
// becomes an invariant after X + 1 iterations.
- unsigned InputToInvariance = calculateIterationsToInvariance(
+ auto InputToInvariance = calculateIterationsToInvariance(
IncPhi, L, BackEdge, IterationsToInvariance);
- if (InputToInvariance != InfiniteIterationsToInvariance)
- ToInvariance = InputToInvariance + 1u;
+ if (InputToInvariance)
+ ToInvariance = *InputToInvariance + 1u;
}
// If we found that this Phi lies in an invariant chain, update the map.
- if (ToInvariance != InfiniteIterationsToInvariance)
+ if (ToInvariance)
IterationsToInvariance[Phi] = ToInvariance;
return ToInvariance;
}
@@ -387,7 +381,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
// First, check that we can peel at least one iteration.
if (2 * LoopSize <= Threshold && UnrollPeelMaxCount > 0) {
// Store the pre-calculated values here.
- SmallDenseMap<PHINode *, unsigned> IterationsToInvariance;
+ SmallDenseMap<PHINode *, Optional<unsigned> > IterationsToInvariance;
// Now go through all Phis to calculate their the number of iterations they
// need to become invariants.
// Start the max computation with the UP.PeelCount value set by the target
@@ -397,10 +391,10 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
assert(BackEdge && "Loop is not in simplified form?");
for (auto BI = L->getHeader()->begin(); isa<PHINode>(&*BI); ++BI) {
PHINode *Phi = cast<PHINode>(&*BI);
- unsigned ToInvariance = calculateIterationsToInvariance(
+ auto ToInvariance = calculateIterationsToInvariance(
Phi, L, BackEdge, IterationsToInvariance);
- if (ToInvariance != InfiniteIterationsToInvariance)
- DesiredPeelCount = std::max(DesiredPeelCount, ToInvariance);
+ if (ToInvariance)
+ DesiredPeelCount = std::max(DesiredPeelCount, *ToInvariance);
}
// Pay respect to limitations implied by loop size and the max peel count.