[PATCH] D50063: [UnJ] Pull code out into a separate function NFC

Dave Green via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 31 07:19:29 PDT 2018


dmgreen created this revision.
dmgreen added reviewers: SjoerdMeijer, hfinkel, Meinersbur.
Herald added a subscriber: zzheng.

Pulled this code out into a common isInnerLoopIterationCountInvariant
function as it seems generally useful.


https://reviews.llvm.org/D50063

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


Index: lib/Transforms/Utils/LoopUtils.cpp
===================================================================
--- lib/Transforms/Utils/LoopUtils.cpp
+++ lib/Transforms/Utils/LoopUtils.cpp
@@ -1519,6 +1519,28 @@
     return (FalseVal + (TrueVal / 2)) / TrueVal;
 }
 
+bool llvm::isInnerLoopIterationCountInvariant(Loop *SubLoop,
+                                              ScalarEvolution &SE) {
+  Loop *OuterL = SubLoop->getParentLoop();
+  if (!OuterL)
+    return true;
+
+  // Get the backedge taken count for the inner loop
+  BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
+  const SCEV *SubLoopBECountSC = SE.getExitCount(SubLoop, SubLoopLatch);
+  if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
+      !SubLoopBECountSC->getType()->isIntegerTy())
+    return false;
+
+  // Get whether count is invariant to the loop
+  ScalarEvolution::LoopDisposition LD =
+      SE.getLoopDisposition(SubLoopBECountSC, 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)) {
Index: lib/Transforms/Utils/LoopUnrollAndJam.cpp
===================================================================
--- lib/Transforms/Utils/LoopUnrollAndJam.cpp
+++ lib/Transforms/Utils/LoopUnrollAndJam.cpp
@@ -754,20 +754,7 @@
 
   // 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 (!isInnerLoopIterationCountInvariant(SubLoop, SE)) {
     LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Inner loop iteration count is "
                          "not consistent on each iteration\n");
     return false;
Index: include/llvm/Transforms/Utils/LoopUtils.h
===================================================================
--- include/llvm/Transforms/Utils/LoopUtils.h
+++ include/llvm/Transforms/Utils/LoopUtils.h
@@ -490,6 +490,10 @@
 /// estimate can not be made.
 Optional<unsigned> getLoopEstimatedTripCount(Loop *L);
 
+/// Check inner loop backedge count is known to be consistent on all iterations
+/// of the outer loop.
+bool isInnerLoopIterationCountInvariant(Loop *L, ScalarEvolution &SE);
+
 /// Helper to consistently add the set of standard passes to a loop pass's \c
 /// AnalysisUsage.
 ///


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50063.158252.patch
Type: text/x-patch
Size: 2977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180731/a5a2cb73/attachment.bin>


More information about the llvm-commits mailing list