[llvm-commits] [llvm] r51179 - /llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp

Owen Anderson resistor at mac.com
Thu May 15 21:32:46 PDT 2008


Author: resistor
Date: Thu May 15 23:32:45 2008
New Revision: 51179

URL: http://llvm.org/viewvc/llvm-project?rev=51179&view=rev
Log:
Clean ups for loop deletion based on Chris' feedback.

Also, use SCEV to determine the trip count of the loop, which is more powerful
and accurate that Loop::getTripCount.

Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp?rev=51179&r1=51178&r2=51179&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopDeletion.cpp Thu May 15 23:32:45 2008
@@ -18,6 +18,7 @@
 
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Analysis/LoopPass.h"
+#include "llvm/Analysis/ScalarEvolution.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/SmallVector.h"
 
@@ -41,11 +42,13 @@
     bool IsLoopInvariantInst(Instruction *I, Loop* L);
     
     virtual void getAnalysisUsage(AnalysisUsage& AU) const {
+      AU.addRequired<ScalarEvolution>();
       AU.addRequired<DominatorTree>();
       AU.addRequired<LoopInfo>();
       AU.addRequiredID(LoopSimplifyID);
       AU.addRequiredID(LCSSAID);
       
+      AU.addPreserved<ScalarEvolution>();
       AU.addPreserved<DominatorTree>();
       AU.addPreserved<LoopInfo>();
       AU.addPreservedID(LoopSimplifyID);
@@ -62,10 +65,10 @@
 }
 
 /// SingleDominatingExit - Checks that there is only a single blocks that 
-/// branches out of the loop, and that it also dominates the latch block.  Loops
+/// branches out of the loop, and that it also g the latch block.  Loops
 /// with multiple or non-latch-dominating exiting blocks could be dead, but we'd
 /// have to do more extensive analysis to make sure, for instance, that the 
-/// control flow logic involves was or could be made loop-invariant.
+/// control flow logic involved was or could be made loop-invariant.
 bool LoopDeletion::SingleDominatingExit(Loop* L,
                                    SmallVector<BasicBlock*, 4>& exitingBlocks) {
   
@@ -77,10 +80,7 @@
     return false;
   
   DominatorTree& DT = getAnalysis<DominatorTree>();
-  if (DT.dominates(exitingBlocks[0], latch))
-    return true;
-  else
-    return false;
+  return DT.dominates(exitingBlocks[0], latch);
 }
 
 /// IsLoopInvariantInst - Checks if an instruction is invariant with respect to
@@ -98,7 +98,7 @@
   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
     if (!L->isLoopInvariant(I->getOperand(i)))
       return false;
-
+  
   // If we got this far, the instruction is loop invariant!
   return true;
 }
@@ -153,19 +153,6 @@
 /// NOTE: This entire process relies pretty heavily on LoopSimplify and LCSSA
 /// in order to make various safety checks work.
 bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
-  SmallVector<BasicBlock*, 4> exitingBlocks;
-  L->getExitingBlocks(exitingBlocks);
-  
-  SmallVector<BasicBlock*, 4> exitBlocks;
-  L->getUniqueExitBlocks(exitBlocks);
-  
-  // We require that the loop only have a single exit block.  Otherwise, we'd
-  // be in the situation of needing to be able to solve statically which exit
-  // block will be branced to, or trying to preserve the branching logic in
-  // a loop invariant manner.
-  if (exitBlocks.size() != 1)
-    return false;
-  
   // We can only remove the loop if there is a preheader that we can 
   // branch from after removing it.
   BasicBlock* preheader = L->getLoopPreheader();
@@ -177,9 +164,17 @@
   if (L->begin() != L->end())
     return false;
   
-  // Don't remove loops for which we can't solve the trip count.
-  // They could be infinite, in which case we'd be changing program behavior.
-  if (!L->getTripCount())
+  SmallVector<BasicBlock*, 4> exitingBlocks;
+  L->getExitingBlocks(exitingBlocks);
+  
+  SmallVector<BasicBlock*, 4> exitBlocks;
+  L->getUniqueExitBlocks(exitBlocks);
+  
+  // We require that the loop only have a single exit block.  Otherwise, we'd
+  // be in the situation of needing to be able to solve statically which exit
+  // block will be branched to, or trying to preserve the branching logic in
+  // a loop invariant manner.
+  if (exitBlocks.size() != 1)
     return false;
   
   // Loops with multiple exits or exits that don't dominate the latch
@@ -191,6 +186,13 @@
   if (!IsLoopDead(L, exitingBlocks, exitBlocks))
     return false;
   
+  // Don't remove loops for which we can't solve the trip count.
+  // They could be infinite, in which case we'd be changing program behavior.
+  ScalarEvolution& SE = getAnalysis<ScalarEvolution>();
+  SCEVHandle S = SE.getIterationCount(L);
+  if (isa<SCEVCouldNotCompute>(S))
+    return false;
+  
   // Now that we know the removal is safe, remove the loop by changing the
   // branch from the preheader to go to the single exit block.  
   BasicBlock* exitBlock = exitBlocks[0];
@@ -239,13 +241,15 @@
     ChildNodes.clear();
     DT.eraseNode(*LI);
     
-    // Drop all references between the instructions and the block so
-    // that we don't have reference counting problems later.
+    // Remove instructions that we're deleting from ScalarEvolution.
     for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end();
-         BI != BE; ++BI) {
-      BI->dropAllReferences();
-    }
+         BI != BE; ++BI)
+      SE.deleteValueFromRecords(BI);
+    
+    SE.deleteValueFromRecords(*LI);
     
+    // Remove the block from the reference counting scheme, so that we can
+    // delete it freely later.
     (*LI)->dropAllReferences();
   }
   





More information about the llvm-commits mailing list