Avoid deleting individual instructions until AFTER dead blocks have dropped
[oota-llvm.git] / lib / Transforms / Scalar / DecomposeMultiDimRefs.cpp
index 0367348ef5d81f62dac0218721456719198ae7ec..90301f8792c91eb71de770956181498a3b757817 100644 (file)
@@ -8,16 +8,21 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.h"
-#include "llvm/Constants.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Constant.h"
 #include "llvm/iMemory.h"
 #include "llvm/iOther.h"
 #include "llvm/BasicBlock.h"
-#include "llvm/Function.h"
 #include "llvm/Pass.h"
+#include "Support/StatisticReporter.h"
+
+static Statistic<> NumAdded("lowerrefs\t\t- New instructions added");
 
 namespace {
   struct DecomposePass : public BasicBlockPass {
+    const char *getPassName() const { return "Decompose Subscripting Exps"; }
+
     virtual bool runOnBasicBlock(BasicBlock *BB);
 
   private:
@@ -79,8 +84,9 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
       
     // Check for a zero index.  This will need a cast instead of
     // a getElementPtr, or it may need neither.
-    bool indexIsZero = isa<ConstantUInt>(*OI) && 
-                       cast<Constant>(*OI)->isNullValue();
+    bool indexIsZero = isa<Constant>(*OI) && 
+                       cast<Constant>(*OI)->isNullValue() &&
+                       (*OI)->getType() == Type::UIntTy;
       
     // Extract the first index.  If the ptr is a pointer to a structure
     // and the next index is a structure offset (i.e., not an array offset), 
@@ -109,6 +115,7 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
     if (!indexIsZero) {
       LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
       NewInsts.push_back(cast<Instruction>(LastPtr));
+      ++NumAdded;
     }
       
     // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
@@ -117,6 +124,7 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
     if (LastPtr->getType() != NextPtrTy) {
       LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
       NewInsts.push_back(cast<Instruction>(LastPtr));
+      ++NumAdded;
     }
   }
   
@@ -155,12 +163,9 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
   // Now delete the old instruction...
   delete MAI;
 
-  // Convert our iterator into an index... that cannot get invalidated
-  unsigned ItOffs = BBI-BB->begin();
-
   // Insert all of the new instructions...
-  BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
+  BBI = BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
   
   // Advance the iterator to the instruction following the one just inserted...
-  BBI = BB->begin() + ItOffs + NewInsts.size();
+  BBI += NewInsts.size();
 }