diff --git a/llvm/include/llvm/Analysis/LoopInfo.h b/llvm/include/llvm/Analysis/LoopInfo.h index 8a8ee060e65ea..6e149b5d14fa9 100644 --- a/llvm/include/llvm/Analysis/LoopInfo.h +++ b/llvm/include/llvm/Analysis/LoopInfo.h @@ -1311,8 +1311,14 @@ Optional findStringMetadataForLoop(const Loop *TheLoop, StringRef Name); /// Look for the loop attribute that requires progress within the loop. +/// Note: Most consumers probably want "isMustProgress" which checks +/// the containing function attribute too. bool hasMustProgress(const Loop *L); +/// Return true if this loop can be assumed to make progress. (i.e. can't +/// be infinite without side effects without also being undefined) +bool isMustProgress(const Loop *L); + /// Return whether an MDNode might represent an access group. /// /// Access group metadata nodes have to be distinct and empty. Being diff --git a/llvm/lib/Analysis/LoopInfo.cpp b/llvm/lib/Analysis/LoopInfo.cpp index 5a2b7ff46cbbb..f45cc12d46302 100644 --- a/llvm/lib/Analysis/LoopInfo.cpp +++ b/llvm/lib/Analysis/LoopInfo.cpp @@ -1108,6 +1108,10 @@ bool llvm::hasMustProgress(const Loop *L) { return getBooleanLoopAttribute(L, LLVMLoopMustProgress); } +bool llvm::isMustProgress(const Loop *L) { + return L->getHeader()->getParent()->mustProgress() || hasMustProgress(L); +} + bool llvm::isValidAsAccessGroup(MDNode *Node) { return Node->getNumOperands() == 0 && Node->isDistinct(); } diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp index 06aa6cb49ae39..275fad4acb2b2 100644 --- a/llvm/lib/Analysis/ScalarEvolution.cpp +++ b/llvm/lib/Analysis/ScalarEvolution.cpp @@ -6579,14 +6579,10 @@ ScalarEvolution::getLoopProperties(const Loop *L) { } bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { - if (!L->getHeader()->getParent()->mustProgress() && - !hasMustProgress(L)) - return false; - - // A loop without side effects must be finite. + // A mustprogress loop without side effects must be finite. // TODO: The check used here is very conservative. It's only *specific* // side effects which are well defined in infinite loops. - return loopHasNoSideEffects(L); + return isMustProgress(L) && loopHasNoSideEffects(L); } const SCEV *ScalarEvolution::createSCEV(Value *V) { diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp index be95ac1844287..aed9994399d2e 100644 --- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -2535,8 +2535,7 @@ static bool detectShiftUntilZeroIdiom(Loop *CurLoop, ScalarEvolution *SE, // right-shift, iff the sign bit was set, the value will never become zero, // and the loop may never finish. if (ValShifted->getOpcode() == Instruction::AShr && - !CurLoop->getHeader()->getParent()->mustProgress() && - !hasMustProgress(CurLoop) && !SE->isKnownNonNegative(SE->getSCEV(Val))) { + !isMustProgress(CurLoop) && !SE->isKnownNonNegative(SE->getSCEV(Val))) { LLVM_DEBUG(dbgs() << DEBUG_TYPE " Can not prove the loop is finite.\n"); return false; } diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 761960f0da62b..95ae7dbb3ddc9 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1781,8 +1781,7 @@ Optional llvm::hasPartialIVCondition(Loop &L, // We could also allow loops with known trip counts without mustprogress, // but ScalarEvolution may not be available. - Info.PathIsNoop &= - L.getHeader()->getParent()->mustProgress() || hasMustProgress(&L); + Info.PathIsNoop &= isMustProgress(&L); // If the path is considered a no-op so far, check if it reaches a // single exit block without any phis. This ensures no values from the