* Standardize how analysis results/passes as printed with the print() virtual
[oota-llvm.git] / lib / Transforms / Scalar / DecomposeMultiDimRefs.cpp
index 396b503ebae97231f6514e2c2fd9b7a3e0d9dc32..f0a807497a77e76cd4f9ed4ad09e7fe620f0b178 100644 (file)
@@ -8,23 +8,27 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/Transforms/Scalar/DecomposeMultiDimRefs.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/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);
+    virtual bool runOnBasicBlock(BasicBlock &BB);
 
   private:
     static void decomposeArrayRef(BasicBlock::iterator &BBI);
   };
+
+  RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
+                               "structure/array references");
 }
 
 Pass *createDecomposeMultiDimRefsPass() {
@@ -35,10 +39,10 @@ Pass *createDecomposeMultiDimRefsPass() {
 // runOnBasicBlock - Entry point for array or structure references with multiple
 // indices.
 //
-bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
+bool DecomposePass::runOnBasicBlock(BasicBlock &BB) {
   bool Changed = false;
-  for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ) {
-    if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) {
+  for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
+    if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II)) {
       if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
         decomposeArrayRef(II);
         Changed = true;
@@ -63,34 +67,38 @@ bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
 // uses the last ptr2 generated in the loop and a single index.
 // If any index is (uint) 0, we omit the getElementPtr instruction.
 // 
+
 void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
-  MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
-  BasicBlock *BB = MAI->getParent();
-  Value *LastPtr = MAI->getPointerOperand();
+  MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
+  BasicBlock *BB = MAI.getParent();
+  Value *LastPtr = MAI.getPointerOperand();
 
   // Remove the instruction from the stream
   BB->getInstList().remove(BBI);
 
-  vector<Instruction*> NewInsts;
+  std::vector<Instruction*> NewInsts;
   
   // Process each index except the last one.
   // 
-  User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
+
+  User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
   for (; OI+1 != OE; ++OI) {
     assert(isa<PointerType>(LastPtr->getType()));
       
     // Check for a zero index.  This will need a cast instead of
     // a getElementPtr, or it may need neither.
     bool indexIsZero = isa<Constant>(*OI) && 
-                       cast<Constant>(*OI)->isNullValue() &&
-                       (*OI)->getType() == Type::UIntTy;
+                       cast<Constant>(OI->get())->isNullValue() &&
+                       OI->get()->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), 
     // we need to include an initial [0] to index into the pointer.
     //
-    vector<Value*> Indices;
-    PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
+
+    std::vector<Value*> Indices;
+    const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
+
     if (isa<StructType>(PtrTy->getElementType())
         && !PtrTy->indexValid(*OI))
       Indices.push_back(Constant::getNullValue(Type::UIntTy));
@@ -112,7 +120,9 @@ 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
     // This is not needed if the two types are identical.
@@ -120,16 +130,18 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
     if (LastPtr->getType() != NextPtrTy) {
       LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
       NewInsts.push_back(cast<Instruction>(LastPtr));
+      ++NumAdded;
     }
   }
   
   // 
   // Now create a new instruction to replace the original one
   //
-  PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
+  const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
 
   // First, get the final index vector.  As above, we may need an initial [0].
-  vector<Value*> Indices;
+
+  std::vector<Value*> Indices;
   if (isa<StructType>(PtrTy->getElementType())
       && !PtrTy->indexValid(*OI))
     Indices.push_back(Constant::getNullValue(Type::UIntTy));
@@ -137,33 +149,32 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
   Indices.push_back(*OI);
 
   Instruction *NewI = 0;
-  switch(MAI->getOpcode()) {
+  switch(MAI.getOpcode()) {
   case Instruction::Load:
-    NewI = new LoadInst(LastPtr, Indices, MAI->getName());
+    NewI = new LoadInst(LastPtr, Indices, MAI.getName());
     break;
   case Instruction::Store:
-    NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
+    NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
     break;
   case Instruction::GetElementPtr:
-    NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
+    NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
     break;
   default:
     assert(0 && "Unrecognized memory access instruction");
   }
   NewInsts.push_back(NewI);
+
   
   // Replace all uses of the old instruction with the new
-  MAI->replaceAllUsesWith(NewI);
+  MAI.replaceAllUsesWith(NewI);
 
   // Now delete the old instruction...
-  delete MAI;
-
-  // Convert our iterator into an index... that cannot get invalidated
-  unsigned ItOffs = BBI-BB->begin();
+  delete &MAI;
 
   // Insert all of the new instructions...
   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.back();
+  ++BBI;
 }