Don't delete asm's just because their inputs are undefined;
[oota-llvm.git] / lib / Transforms / Scalar / LoopDeletion.cpp
index ac807a4c9b20e11664a0df38046b18595941cc86..38d0b39a08268b71c820292d85d6ea532f38da63 100644 (file)
@@ -53,6 +53,7 @@ namespace {
       AU.addPreserved<LoopInfo>();
       AU.addPreservedID(LoopSimplifyID);
       AU.addPreservedID(LCSSAID);
+      AU.addPreserved<DominanceFrontier>();
     }
   };
 }
@@ -135,11 +136,8 @@ bool LoopDeletion::IsLoopDead(Loop* L,
        LI != LE; ++LI) {
     for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end();
          BI != BE; ++BI) {
-      if (BI->mayWriteToMemory())
+      if (BI->mayHaveSideEffects())
         return false;
-      else if (LoadInst* L = dyn_cast<LoadInst>(BI))
-        if (L->isVolatile())
-          return false;
     }
   }
   
@@ -189,7 +187,7 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
   // 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);
+  const SCEV *S = SE.getBackedgeTakenCount(L);
   if (isa<SCEVCouldNotCompute>(S))
     return false;
   
@@ -201,7 +199,12 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
   // Because we're deleting a large chunk of code at once, the sequence in which
   // we remove things is very important to avoid invalidation issues.  Don't
   // mess with this unless you have good reason and know what you're doing.
-  
+
+  // Tell ScalarEvolution that the loop is deleted. Do this before
+  // deleting the loop so that ScalarEvolution can look at the loop
+  // to determine what it needs to clean up.
+  SE.forgetLoopBackedgeTakenCount(L);
+
   // Move simple loop-invariant expressions out of the loop, since they
   // might be needed by the exit phis.
   for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
@@ -228,6 +231,7 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
   // Update the dominator tree and remove the instructions and blocks that will
   // be deleted from the reference counting scheme.
   DominatorTree& DT = getAnalysis<DominatorTree>();
+  DominanceFrontier* DF = getAnalysisIfAvailable<DominanceFrontier>();
   SmallPtrSet<DomTreeNode*, 8> ChildNodes;
   for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
        LI != LE; ++LI) {
@@ -235,19 +239,15 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
     // allows us to remove the domtree entry for the block.
     ChildNodes.insert(DT[*LI]->begin(), DT[*LI]->end());
     for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = ChildNodes.begin(),
-         DE = ChildNodes.end(); DI != DE; ++DI)
+         DE = ChildNodes.end(); DI != DE; ++DI) {
       DT.changeImmediateDominator(*DI, DT[preheader]);
+      if (DF) DF->changeImmediateDominator((*DI)->getBlock(), preheader, &DT);
+    }
     
     ChildNodes.clear();
     DT.eraseNode(*LI);
-    
-    // Remove instructions that we're deleting from ScalarEvolution.
-    for (BasicBlock::iterator BI = (*LI)->begin(), BE = (*LI)->end();
-         BI != BE; ++BI)
-      SE.deleteValueFromRecords(BI);
-    
-    SE.deleteValueFromRecords(*LI);
-    
+    if (DF) DF->removeBlock(*LI);
+
     // Remove the block from the reference counting scheme, so that we can
     // delete it freely later.
     (*LI)->dropAllReferences();
@@ -261,9 +261,6 @@ bool LoopDeletion::runOnLoop(Loop* L, LPPassManager& LPM) {
        LI != LE; ++LI)
     (*LI)->eraseFromParent();
 
-  // Tell ScalarEvolution that the loop is deleted.
-  SE.forgetLoopIterationCount(L);
-
   // Finally, the blocks from loopinfo.  This has to happen late because
   // otherwise our loop iterators won't work.
   LoopInfo& loopInfo = getAnalysis<LoopInfo>();