Avoid inserting an entry block unless we need it
[oota-llvm.git] / lib / Transforms / Scalar / IndVarSimplify.cpp
index 7b1eb5d7327535c79f240beb8a012f530ef900ac..4e3e65beaf3363790f564c5b9450c4c84d7fdb64 100644 (file)
 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...
@@ -50,9 +46,10 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   // info into a vector...
   //
   std::vector<InductionVariable> IndVars;    // Induction variables for block
-  for (BasicBlock::iterator I = Header->begin(); 
-       PHINode *PN = dyn_cast<PHINode>(*I); ++I)
+  BasicBlock::iterator AfterPHIIt = Header->begin();
+  for (; PHINode *PN = dyn_cast<PHINode>(&*AfterPHIIt); ++AfterPHIIt)
     IndVars.push_back(InductionVariable(PN, Loops));
+  // AfterPHIIt now points to first nonphi instruction...
 
   // If there are no phi nodes in this basic block, there can't be indvars...
   if (IndVars.empty()) return Changed;
@@ -80,7 +77,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
     PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar");
 
     // Insert the phi node at the end of the other phi nodes...
-    Header->getInstList().insert(Header->begin()+IndVars.size(), PN);
+    AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
 
     // Create the increment instruction to add one to the counter...
     Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
@@ -88,7 +85,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
                                               "add1-indvar");
 
     // Insert the add instruction after all of the PHI nodes...
-    Header->getInstList().insert(Header->begin()+(IndVars.size()+1), Add);
+    Header->getInstList().insert(AfterPHIIt, Add);
 
     // Figure out which block is incoming and which is the backedge for the loop
     BasicBlock *Incoming, *BackEdgeBlock;
@@ -116,9 +113,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
     Changed = true;
   }
 
-#ifdef DEBUG
-  cerr << "Induction variables:\n";
-#endif
+  DEBUG(std::cerr << "Induction variables:\n");
 
   // Get the current loop iteration count, which is always the value of the
   // cannonical phi node...
@@ -128,12 +123,11 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   // Loop through and replace all of the auxillary induction variables with
   // references to the primary induction variable...
   //
-  unsigned InsertPos = IndVars.size();
   for (unsigned i = 0; i < IndVars.size(); ++i) {
     InductionVariable *IV = &IndVars[i];
-#ifdef DEBUG
-    cerr << IndVars[i];
-#endif
+
+    DEBUG(IV->print(std::cerr));
+
     // Don't modify the cannonical indvar or unrecognized indvars...
     if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
       Instruction *Val = IterCount;
@@ -144,12 +138,11 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
 
         // If the types are not compatible, insert a cast now...
         if (Val->getType() != IV->Step->getType())
-          Val = InsertCast(Val, IV->Step->getType(),
-                           Header->begin()+InsertPos++);
+          Val = InsertCast(Val, IV->Step->getType(), AfterPHIIt);
 
         Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, Name);
         // Insert the phi node at the end of the other phi nodes...
-        Header->getInstList().insert(Header->begin()+InsertPos++, Val);
+        Header->getInstList().insert(AfterPHIIt, Val);
       }
 
       if (!isa<Constant>(IV->Start) ||   // If the start != 0
@@ -159,18 +152,16 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
 
         // If the types are not compatible, insert a cast now...
         if (Val->getType() != IV->Start->getType())
-          Val = InsertCast(Val, IV->Start->getType(),
-                           Header->begin()+InsertPos++);
+          Val = InsertCast(Val, IV->Start->getType(), AfterPHIIt);
 
         Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, Name);
         // Insert the phi node at the end of the other phi nodes...
-        Header->getInstList().insert(Header->begin()+InsertPos++, Val);
+        Header->getInstList().insert(AfterPHIIt, Val);
       }
 
       // If the PHI node has a different type than val is, insert a cast now...
       if (Val->getType() != IV->Phi->getType())
-          Val = InsertCast(Val, IV->Phi->getType(),
-                           Header->begin()+InsertPos++);
+        Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
       
       // Replace all uses of the old PHI node with the new computed value...
       IV->Phi->replaceAllUsesWith(Val);
@@ -181,9 +172,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
       Val->setName(OldName);
 
       // Delete the old, now unused, phi node...
-      Header->getInstList().remove(IV->Phi);
-      delete IV->Phi;
-      InsertPos--;            // Deleted an instr, decrement insert position
+      Header->getInstList().erase(IV->Phi);
       Changed = true;
       ++NumRemoved;
     }
@@ -194,11 +183,7 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
 
 namespace {
   struct InductionVariableSimplify : public FunctionPass {
-    const char *getPassName() const {
-      return "Induction Variable Cannonicalize";
-    }
-
-    virtual bool runOnFunction(Function *F) {
+    virtual bool runOnFunction(Function &) {
       LoopInfo &LI = getAnalysis<LoopInfo>();
 
       // Induction Variables live in the header nodes of loops
@@ -208,13 +193,14 @@ namespace {
     }
     
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.addRequired(LoopInfo::ID);
+      AU.addRequired<LoopInfo>();
       AU.preservesCFG();
     }
   };
+  RegisterOpt<InductionVariableSimplify> X("indvars",
+                                           "Cannonicalize Induction Variables");
 }
 
 Pass *createIndVarSimplifyPass() {
   return new InductionVariableSimplify();
 }
-