Fix two fixmes: integrate with inlining, and document
[oota-llvm.git] / lib / Transforms / Scalar / IndVarSimplify.cpp
index 35fe697f0f9c63feccf6c3732c636343e0e671a3..ff2b9939c70cdd952207c5e893a63ccad91d81d2 100644 (file)
@@ -8,28 +8,25 @@
 #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/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");
+#include "Support/Statistic.h"
 
+namespace {
+  Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
+  Statistic<> NumInserted("indvars", "Number of cannonical indvars added");
+}
 
 // InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
 // name...
 //
-static Instruction *InsertCast(Instruction *Val, const Type *Ty,
-                               BasicBlock::iterator It) {
-  Instruction *Cast = new CastInst(Val, Ty);
-  if (Val->hasName()) Cast->setName(Val->getName()+"-casted");
-  Val->getParent()->getInstList().insert(It, Cast);
-  return Cast;
+static Instruction *InsertCast(Value *Val, const Type *Ty,
+                               Instruction *InsertBefore) {
+  return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
 }
 
 static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
@@ -62,7 +59,8 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   bool FoundIndVars = false;
   InductionVariable *Cannonical = 0;
   for (unsigned i = 0; i < IndVars.size(); ++i) {
-    if (IndVars[i].InductionType == InductionVariable::Cannonical)
+    if (IndVars[i].InductionType == InductionVariable::Cannonical &&
+        !isa<PointerType>(IndVars[i].Phi->getType()))
       Cannonical = &IndVars[i];
     if (IndVars[i].InductionType != InductionVariable::Unknown)
       FoundIndVars = true;
@@ -74,19 +72,14 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   // Okay, we want to convert other induction variables to use a cannonical
   // indvar.  If we don't have one, add one now...
   if (!Cannonical) {
-    // Create the PHI node for the new induction variable
-    PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar");
-
-    // Insert the phi node at the end of the other phi nodes...
-    AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
+    // Create the PHI node for the new induction variable, and insert the phi
+    // node at the end of the other phi nodes...
+    PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
 
     // Create the increment instruction to add one to the counter...
     Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
                                               ConstantUInt::get(Type::UIntTy,1),
-                                              "add1-indvar");
-
-    // Insert the add instruction after all of the PHI nodes...
-    Header->getInstList().insert(AfterPHIIt, Add);
+                                              "add1-indvar", AfterPHIIt);
 
     // Figure out which block is incoming and which is the backedge for the loop
     BasicBlock *Incoming, *BackEdgeBlock;
@@ -127,37 +120,39 @@ static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
   for (unsigned i = 0; i < IndVars.size(); ++i) {
     InductionVariable *IV = &IndVars[i];
 
-    DEBUG(std::cerr << IV);
+    DEBUG(IV->print(std::cerr));
+
+    // Don't do math with pointers...
+    const Type *IVTy = IV->Phi->getType();
+    if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
 
     // Don't modify the cannonical indvar or unrecognized indvars...
     if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
       Instruction *Val = IterCount;
       if (!isa<ConstantInt>(IV->Step) ||   // If the step != 1
           !cast<ConstantInt>(IV->Step)->equalsInt(1)) {
-        std::string Name;   // Create a scale by the step value...
-        if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-scale";
 
         // If the types are not compatible, insert a cast now...
-        if (Val->getType() != IV->Step->getType())
-          Val = InsertCast(Val, IV->Step->getType(), AfterPHIIt);
+        if (Val->getType() != IVTy)
+          Val = InsertCast(Val, IVTy, AfterPHIIt);
+        if (IV->Step->getType() != IVTy)
+          IV->Step = InsertCast(IV->Step, IVTy, 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(AfterPHIIt, Val);
+        Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
+                                     IV->Phi->getName()+"-scale", AfterPHIIt);
       }
 
-      if (!isa<Constant>(IV->Start) ||   // If the start != 0
-          !cast<Constant>(IV->Start)->isNullValue()) {
-        std::string Name;   // Create a offset by the start value...
-        if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-offset";
-
+      // If the start != 0
+      if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
         // If the types are not compatible, insert a cast now...
-        if (Val->getType() != IV->Start->getType())
-          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(AfterPHIIt, Val);
+        if (Val->getType() != IVTy)
+          Val = InsertCast(Val, IVTy, AfterPHIIt);
+        if (IV->Start->getType() != IVTy)
+          IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
+
+        // Insert the instruction after the phi nodes...
+        Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
+                                     IV->Phi->getName()+"-offset", AfterPHIIt);
       }
 
       // If the PHI node has a different type than val is, insert a cast now...
@@ -194,11 +189,11 @@ namespace {
     }
     
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
-      AU.addRequired(LoopInfo::ID);
-      AU.preservesCFG();
+      AU.addRequired<LoopInfo>();
+      AU.setPreservesCFG();
     }
   };
-  RegisterPass<InductionVariableSimplify> X("indvars",
+  RegisterOpt<InductionVariableSimplify> X("indvars",
                                            "Cannonicalize Induction Variables");
 }