return ExplicitUnroll;
}
-static bool tryToUnrollLoop(
+static LoopUnrollResult tryToUnrollLoop(
Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
const TargetTransformInfo &TTI, AssumptionCache &AC,
OptimizationRemarkEmitter &ORE, bool PreserveLCSSA, int OptLevel,
Optional<bool> ProvidedUpperBound, Optional<bool> ProvidedAllowPeeling) {
DEBUG(dbgs() << "Loop Unroll: F[" << L->getHeader()->getParent()->getName()
<< "] Loop %" << L->getHeader()->getName() << "\n");
- if (HasUnrollDisablePragma(L))
- return false;
+ if (HasUnrollDisablePragma(L))
+ return LoopUnrollResult::Unmodified;
if (!L->isLoopSimplifyForm()) {
DEBUG(
dbgs() << " Not unrolling loop which is not in loop-simplify form.\n");
- return false;
+ return LoopUnrollResult::Unmodified;
}
unsigned NumInlineCandidates;
ProvidedAllowPeeling);
// Exit early if unrolling is disabled.
if (UP.Threshold == 0 && (!UP.Partial || UP.PartialThreshold == 0))
- return false;
+ return LoopUnrollResult::Unmodified;
unsigned LoopSize = ApproximateLoopSize(
L, NumInlineCandidates, NotDuplicatable, Convergent, TTI, &AC, UP.BEInsns);
DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
if (NotDuplicatable) {
DEBUG(dbgs() << " Not unrolling loop which contains non-duplicatable"
<< " instructions.\n");
- return false;
+ return LoopUnrollResult::Unmodified;
}
if (NumInlineCandidates != 0) {
DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n");
- return false;
+ return LoopUnrollResult::Unmodified;
}
// Find trip count and trip multiple if count is not available
computeUnrollCount(L, TTI, DT, LI, SE, &ORE, TripCount, MaxTripCount,
TripMultiple, LoopSize, UP, UseUpperBound);
if (!UP.Count)
- return false;
+ return LoopUnrollResult::Unmodified;
// Unroll factor (Count) must be less or equal to TripCount.
if (TripCount && UP.Count > TripCount)
UP.Count = TripCount;
// Unroll the loop.
- LoopUnrollResult UnrollStatus = UnrollLoop(
+ LoopUnrollResult UnrollResult = UnrollLoop(
L, UP.Count, TripCount, UP.Force, UP.Runtime, UP.AllowExpensiveTripCount,
UseUpperBound, MaxOrZero, TripMultiple, UP.PeelCount, UP.UnrollRemainder,
LI, &SE, &DT, &AC, &ORE, PreserveLCSSA);
- if (UnrollStatus == LoopUnrollResult::Unmodified)
- return false;
+ if (UnrollResult == LoopUnrollResult::Unmodified)
+ return LoopUnrollResult::Unmodified;
// If loop has an unroll count pragma or unrolled by explicitly set count
// mark loop as unrolled to prevent unrolling beyond that requested.
// If the loop was peeled, we already "used up" the profile information
// we had, so we don't want to unroll or peel again.
- if (UnrollStatus != LoopUnrollResult::FullyUnrolled &&
+ if (UnrollResult != LoopUnrollResult::FullyUnrolled &&
(IsCountSetExplicitly || UP.PeelCount))
SetLoopAlreadyUnrolled(L);
- return true;
+ return UnrollResult;
}
namespace {
return tryToUnrollLoop(L, DT, LI, SE, TTI, AC, ORE, PreserveLCSSA, OptLevel,
ProvidedCount, ProvidedThreshold,
ProvidedAllowPartial, ProvidedRuntime,
- ProvidedUpperBound, ProvidedAllowPeeling);
+ ProvidedUpperBound, ProvidedAllowPeeling) !=
+ LoopUnrollResult::Unmodified;
}
/// This transformation requires natural loop information & requires that
/*PreserveLCSSA*/ true, OptLevel, /*Count*/ None,
/*Threshold*/ None, /*AllowPartial*/ false,
/*Runtime*/ false, /*UpperBound*/ false,
- /*AllowPeeling*/ false);
+ /*AllowPeeling*/ false) != LoopUnrollResult::Unmodified;
if (!Changed)
return PreservedAnalyses::all();
// bloating it further.
if (PSI && PSI->hasHugeWorkingSetSize())
AllowPeeling = false;
- bool CurChanged =
+ LoopUnrollResult Result =
tryToUnrollLoop(&L, DT, &LI, SE, TTI, AC, ORE,
/*PreserveLCSSA*/ true, OptLevel, /*Count*/ None,
/*Threshold*/ None, AllowPartialParam, RuntimeParam,
UpperBoundParam, AllowPeeling);
- Changed |= CurChanged;
+ Changed |= Result != LoopUnrollResult::Unmodified;
// The parent must not be damaged by unrolling!
#ifndef NDEBUG
- if (CurChanged && ParentL)
+ if (Result != LoopUnrollResult::Unmodified && ParentL)
ParentL->verifyLoop();
#endif
- // Walk the parent or top-level loops after unrolling to check whether we
- // actually removed a loop, and if so clear any cached analysis results for
- // it. We have to do this immediately as the next unrolling could allocate
- // a new loop object that ends up with the same address as the deleted loop
- // object causing cache collisions.
- if (LAM) {
- bool IsCurLoopValid =
- ParentL
- ? llvm::any_of(*ParentL, [&](Loop *SibL) { return SibL == &L; })
- : llvm::any_of(LI, [&](Loop *SibL) { return SibL == &L; });
- if (!IsCurLoopValid)
- LAM->clear(L);
- }
+ // Clear any cached analysis results for L if we removed it completely.
+ if (LAM && Result == LoopUnrollResult::FullyUnrolled)
+ LAM->clear(L);
}
if (!Changed)