Avoid deleting individual instructions until AFTER dead blocks have dropped
[oota-llvm.git] / lib / Transforms / Scalar / IndVarSimplify.cpp
index 692fcacb7e3b2723334d9245d4e685c0c79a021b..644652691e8107ef48746b95f09d6fb80b67aa51 100644 (file)
@@ -5,21 +5,21 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Scalar/IndVarSimplify.h"
+#include "llvm/Transforms/Scalar.h"
 #include "llvm/Analysis/InductionVariable.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/Writer.h"
 #include "llvm/iPHINode.h"
 #include "llvm/iOther.h"
 #include "llvm/Type.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/ConstantVals.h"
+#include "llvm/Constants.h"
 #include "llvm/Support/CFG.h"
 #include "Support/STLExtras.h"
+#include "Support/StatisticReporter.h"
+
+static Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed");
+static Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added");
 
-#if 0
-#define DEBUG
-#include "llvm/Analysis/Writer.h"
-#endif
 
 // InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
 // name...
@@ -32,14 +32,15 @@ static Instruction *InsertCast(Instruction *Val, const Type *Ty,
   return Cast;
 }
 
-static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
+static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   // Transform all subloops before this loop...
   bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
                                    Loop->getSubLoops().end(),
                               std::bind1st(std::ptr_fun(TransformLoop), Loops));
   // Get the header node for this loop.  All of the phi nodes that could be
   // induction variables must live in this basic block.
-  BasicBlock *Header = (BasicBlock*)Loop->getBlocks().front();
+  //
+  BasicBlock *Header = Loop->getBlocks().front();
   
   // Loop over all of the PHI nodes in the basic block, calculating the
   // induction variables that they represent... stuffing the induction variable
@@ -100,7 +101,7 @@ static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
     assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
     
     // Add incoming values for the PHI node...
-    PN->addIncoming(Constant::getNullConstant(Type::UIntTy), Incoming);
+    PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
     PN->addIncoming(Add, BackEdgeBlock);
 
     // Analyze the new induction variable...
@@ -108,12 +109,11 @@ static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
     assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
            "Just inserted cannonical indvar that is not cannonical!");
     Cannonical = &IndVars.back();
+    ++NumInserted;
     Changed = true;
   }
 
-#ifdef DEBUG
-  cerr << "Induction variables:\n";
-#endif
+  DEBUG(cerr << "Induction variables:\n");
 
   // Get the current loop iteration count, which is always the value of the
   // cannonical phi node...
@@ -126,9 +126,9 @@ static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
   unsigned InsertPos = IndVars.size();
   for (unsigned i = 0; i < IndVars.size(); ++i) {
     InductionVariable *IV = &IndVars[i];
-#ifdef DEBUG
-    cerr << IndVars[i];
-#endif
+
+    DEBUG(cerr << IV);
+
     // Don't modify the cannonical indvar or unrecognized indvars...
     if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
       Instruction *Val = IterCount;
@@ -180,25 +180,36 @@ static bool TransformLoop(cfg::LoopInfo *Loops, cfg::Loop *Loop) {
       delete IV->Phi;
       InsertPos--;            // Deleted an instr, decrement insert position
       Changed = true;
+      ++NumRemoved;
     }
   }
 
   return Changed;
 }
 
-bool InductionVariableSimplify::doit(Method *M, cfg::LoopInfo &Loops) {
-  // Induction Variables live in the header nodes of the loops of the method...
-  return reduce_apply_bool(Loops.getTopLevelLoops().begin(),
-                           Loops.getTopLevelLoops().end(),
-                           std::bind1st(std::ptr_fun(TransformLoop), &Loops));
-}
+namespace {
+  struct InductionVariableSimplify : public FunctionPass {
+    const char *getPassName() const {
+      return "Induction Variable Cannonicalize";
+    }
+
+    virtual bool runOnFunction(Function *F) {
+      LoopInfo &LI = getAnalysis<LoopInfo>();
 
-bool InductionVariableSimplify::runOnMethod(Method *M) {
-  return doit(M, getAnalysis<cfg::LoopInfo>());
+      // Induction Variables live in the header nodes of loops
+      return reduce_apply_bool(LI.getTopLevelLoops().begin(),
+                               LI.getTopLevelLoops().end(),
+                               std::bind1st(std::ptr_fun(TransformLoop), &LI));
+    }
+    
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.addRequired(LoopInfo::ID);
+      AU.preservesCFG();
+    }
+  };
 }
 
-void InductionVariableSimplify::getAnalysisUsageInfo(Pass::AnalysisSet &Req,
-                                                     Pass::AnalysisSet &Dest,
-                                                     Pass::AnalysisSet &Prov) {
-  Req.push_back(cfg::LoopInfo::ID);
+Pass *createIndVarSimplifyPass() {
+  return new InductionVariableSimplify();
 }
+