From: David Green Date: Sat, 11 Aug 2018 06:57:28 +0000 (+0000) Subject: [UnJ] Create a hasInvariantIterationCount function. NFC X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=395b80cd3cf41da895d0bf217dfc2a0918593a13;p=platform%2Fupstream%2Fllvm.git [UnJ] Create a hasInvariantIterationCount function. NFC Pulled out a separate function for some code that calculates if an inner loop iteration count is invariant to it's outer loop. Differential Revision: https://reviews.llvm.org/D50063 llvm-svn: 339500 --- diff --git a/llvm/include/llvm/Transforms/Utils/LoopUtils.h b/llvm/include/llvm/Transforms/Utils/LoopUtils.h index 33dcc2a..a9193c7 100644 --- a/llvm/include/llvm/Transforms/Utils/LoopUtils.h +++ b/llvm/include/llvm/Transforms/Utils/LoopUtils.h @@ -490,6 +490,10 @@ void addStringMetadataToLoop(Loop *TheLoop, const char *MDString, /// estimate can not be made. Optional getLoopEstimatedTripCount(Loop *L); +/// Check inner loop (L) backedge count is known to be invariant on all iterations +/// of its outer loop. If the loop has no parent, this is trivially true. +bool hasInvariantIterationCount(Loop *L, ScalarEvolution &SE); + /// Helper to consistently add the set of standard passes to a loop pass's \c /// AnalysisUsage. /// diff --git a/llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp b/llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp index 07b5f20..cc80e11 100644 --- a/llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp @@ -754,20 +754,7 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT, // Check inner loop backedge count is consistent on all iterations of the // outer loop - auto CheckInnerLoopIterationCountInvariant = [](Loop *SubLoop, Loop *OuterL, - ScalarEvolution &SE) { - BasicBlock *SubLoopLatch = SubLoop->getLoopLatch(); - const SCEV *SubLoopBECountSC = SE.getExitCount(SubLoop, SubLoopLatch); - if (isa(SubLoopBECountSC) || - !SubLoopBECountSC->getType()->isIntegerTy()) - return false; - ScalarEvolution::LoopDisposition LD = - SE.getLoopDisposition(SubLoopBECountSC, OuterL); - if (LD != ScalarEvolution::LoopInvariant) - return false; - return true; - }; - if (!CheckInnerLoopIterationCountInvariant(SubLoop, L, SE)) { + if (!hasInvariantIterationCount(SubLoop, SE)) { LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Inner loop iteration count is " "not consistent on each iteration\n"); return false; diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 8e9235b..e9db472 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1521,6 +1521,28 @@ Optional llvm::getLoopEstimatedTripCount(Loop *L) { return (FalseVal + (TrueVal / 2)) / TrueVal; } +bool llvm::hasInvariantIterationCount(Loop *InnerLoop, + ScalarEvolution &SE) { + Loop *OuterL = InnerLoop->getParentLoop(); + if (!OuterL) + return true; + + // Get the backedge taken count for the inner loop + BasicBlock *InnerLoopLatch = InnerLoop->getLoopLatch(); + const SCEV *InnerLoopBECountSC = SE.getExitCount(InnerLoop, InnerLoopLatch); + if (isa(InnerLoopBECountSC) || + !InnerLoopBECountSC->getType()->isIntegerTy()) + return false; + + // Get whether count is invariant to the outer loop + ScalarEvolution::LoopDisposition LD = + SE.getLoopDisposition(InnerLoopBECountSC, OuterL); + if (LD != ScalarEvolution::LoopInvariant) + return false; + + return true; +} + /// Adds a 'fast' flag to floating point operations. static Value *addFastMathFlag(Value *V) { if (isa(V)) {