[PATCH] Partially fix PR20058: reduce compile time for loop unrolling with very high count

Mark Heffernan meheff at google.com
Tue Jun 24 23:41:10 PDT 2014


Hi eliben,

This patch removes redundant calls to ScalarEvolution::forgetLoop when loop unrolling.  For very high loop count loops this cuts the compile time (such as the one in the PR) by about a factor of two.

http://reviews.llvm.org/D4285

Files:
  lib/Transforms/Utils/LoopUnroll.cpp

Index: lib/Transforms/Utils/LoopUnroll.cpp
===================================================================
--- lib/Transforms/Utils/LoopUnroll.cpp
+++ lib/Transforms/Utils/LoopUnroll.cpp
@@ -62,11 +62,16 @@
 }
 
 /// FoldBlockIntoPredecessor - Folds a basic block into its predecessor if it
-/// only has one predecessor, and that predecessor only has one successor.
-/// The LoopInfo Analysis that is passed will be kept consistent.
-/// Returns the new combined block.
+/// only has one predecessor, and that predecessor only has one successor.  The
+/// LoopInfo Analysis that is passed will be kept consistent.  If folding is
+/// successful references to the loop must be removed from ScalarEvolution by
+/// calling ScalarEvolution::forgetLoop because SE may have references to the
+/// eliminated BB.  The parameter LoopForgotten indicates that the loop has
+/// already been forgotten to prevent redundant, expensive calls to
+/// ScalarEvolution::forgetLoop.  Returns the new combined block.
 static BasicBlock *FoldBlockIntoPredecessor(BasicBlock *BB, LoopInfo* LI,
-                                            LPPassManager *LPM) {
+                                            LPPassManager *LPM,
+                                            bool &LoopForgotten) {
   // Merge basic blocks into their predecessor if there is only one distinct
   // pred, and if there is only one distinct successor of the predecessor, and
   // if there are no PHI nodes.
@@ -101,10 +106,12 @@
   // Erase basic block from the function...
 
   // ScalarEvolution holds references to loop exit blocks.
-  if (LPM) {
+  if (!LoopForgotten && LPM) {
     if (ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>()) {
-      if (Loop *L = LI->getLoopFor(BB))
+      if (Loop *L = LI->getLoopFor(BB)) {
         SE->forgetLoop(L);
+        LoopForgotten = true;
+      }
     }
   }
   LI->removeBlock(BB);
@@ -418,11 +425,13 @@
   }
 
   // Merge adjacent basic blocks, if possible.
+  bool LoopForgotten = false;
   for (unsigned i = 0, e = Latches.size(); i != e; ++i) {
     BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator());
     if (Term->isUnconditional()) {
       BasicBlock *Dest = Term->getSuccessor(0);
-      if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM))
+      if (BasicBlock *Fold = FoldBlockIntoPredecessor(Dest, LI, LPM,
+                                                      LoopForgotten))
         std::replace(Latches.begin(), Latches.end(), Dest, Fold);
     }
   }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D4285.10818.patch
Type: text/x-patch
Size: 2529 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140625/f8fd798f/attachment.bin>


More information about the llvm-commits mailing list