[llvm] r339500 - [UnJ] Create a hasInvariantIterationCount function. NFC

David Green via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 10 23:57:28 PDT 2018


Author: dmgreen
Date: Fri Aug 10 23:57:28 2018
New Revision: 339500

URL: http://llvm.org/viewvc/llvm-project?rev=339500&view=rev
Log:
[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

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/LoopUtils.h
    llvm/trunk/lib/Transforms/Utils/LoopUnrollAndJam.cpp
    llvm/trunk/lib/Transforms/Utils/LoopUtils.cpp

Modified: llvm/trunk/include/llvm/Transforms/Utils/LoopUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/LoopUtils.h?rev=339500&r1=339499&r2=339500&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/LoopUtils.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/LoopUtils.h Fri Aug 10 23:57:28 2018
@@ -490,6 +490,10 @@ void addStringMetadataToLoop(Loop *TheLo
 /// estimate can not be made.
 Optional<unsigned> 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.
 ///

Modified: llvm/trunk/lib/Transforms/Utils/LoopUnrollAndJam.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopUnrollAndJam.cpp?rev=339500&r1=339499&r2=339500&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopUnrollAndJam.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopUnrollAndJam.cpp Fri Aug 10 23:57:28 2018
@@ -754,20 +754,7 @@ bool llvm::isSafeToUnrollAndJam(Loop *L,
 
   // 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<SCEVCouldNotCompute>(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;

Modified: llvm/trunk/lib/Transforms/Utils/LoopUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopUtils.cpp?rev=339500&r1=339499&r2=339500&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopUtils.cpp Fri Aug 10 23:57:28 2018
@@ -1521,6 +1521,28 @@ Optional<unsigned> llvm::getLoopEstimate
     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<SCEVCouldNotCompute>(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<FPMathOperator>(V)) {




More information about the llvm-commits mailing list