* We were forgetting to pass varargs arguments through a call
[oota-llvm.git] / lib / Transforms / Scalar / DecomposeMultiDimRefs.cpp
index 657640471a11b3b8e7ed49cc961468570b8f0abe..d6aee14f2406bd918a4e9b056084714d742accc5 100644 (file)
@@ -1,4 +1,11 @@
 //===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // DecomposeMultiDimRefs - Convert multi-dimensional references consisting of
 // any combination of 2 or more array and structure indices into a sequence of
 #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");
+#include "Support/Statistic.h"
 
 namespace {
+  Statistic<> NumAdded("lowerrefs", "# of getelementptr instructions added");
+
   struct DecomposePass : public BasicBlockPass {
     virtual bool runOnBasicBlock(BasicBlock &BB);
-
-  private:
-    static bool decomposeArrayRef(BasicBlock::iterator &BBI);
   };
-
-  RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
-                               "structure/array references");
 }
 
-Pass
+RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
+                             "structure/array references");
+
+FunctionPass
 *createDecomposeMultiDimRefsPass()
 {
   return new DecomposePass();
@@ -45,26 +49,18 @@ Pass
 bool
 DecomposePass::runOnBasicBlock(BasicBlock &BB)
 {
-  bool Changed = false;
-  for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
-    if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(&*II))
-      if (MAI->getNumIndices() >= 2) {
-        Changed = decomposeArrayRef(II) || Changed; // always modifies II
-        continue;
-      }
-    ++II;
-  }
-  return Changed;
+  bool changed = false;
+  for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
+    if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(II++)) // pre-inc
+      if (gep->getNumIndices() >= 2)
+        changed |= DecomposeArrayRef(gep); // always modifies II
+  return changed;
 }
 
-// Check for a constant (uint) 0.
-inline bool
-IsZero(Value* idx)
-{
-  return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
-}
 
-// For any MemAccessInst with 2 or more array and structure indices:
+// Function: DecomposeArrayRef()
+//  
+// For any GetElementPtrInst with 2 or more array and structure indices:
 // 
 //      opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
 // 
@@ -86,45 +82,35 @@ IsZero(Value* idx)
 // Return value: true if the instruction was replaced; false otherwise.
 // 
 bool
-DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
+DecomposeArrayRef(GetElementPtrInst* GEP)
 {
-  MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
-
-  // If this instr two or fewer arguments and the first argument is 0,
-  // the decomposed version is identical to the instruction itself.
-  // This is common enough that it is worth checking for explicitly...
-  if (MAI.getNumIndices() == 0 ||
-      (MAI.getNumIndices() <= 2 && IsZero(*MAI.idx_begin()))) {
-    ++BBI;
+  if (GEP->getNumIndices() < 2)
     return false;
-  }
 
-  BasicBlock *BB = MAI.getParent();
-  Value *LastPtr = MAI.getPointerOperand();
-
-  // Remove the instruction from the stream
-  BB->getInstList().remove(BBI);
+  BasicBlock *BB = GEP->getParent();
+  Value *LastPtr = GEP->getPointerOperand();
+  Instruction *InsertPoint = GEP->getNext(); // Insert before the next insn
 
   // The vector of new instructions to be created
   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 = GEP->idx_begin(), OE = GEP->idx_end();
   for (; OI+1 != OE; ++OI) {
     std::vector<Value*> Indices;
     
     // If this is the first index and is 0, skip it and move on!
-    if (OI == MAI.idx_begin()) {
-      if (IsZero(*OI)) continue;
-    } else
-      // Not the first index: include initial [0] to deref the last ptr
-      Indices.push_back(Constant::getNullValue(Type::UIntTy));
+    if (OI == GEP->idx_begin()) {
+      if (*OI == ConstantInt::getNullValue((*OI)->getType()))
+        continue;
+    }
+    else // Not the first index: include initial [0] to deref the last ptr
+      Indices.push_back(Constant::getNullValue(Type::LongTy));
 
     Indices.push_back(*OI);
 
     // New Instruction: nextPtr1 = GetElementPtr LastPtr, Indices
-    LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
-    NewInsts.push_back(cast<Instruction>(LastPtr));
+    LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1", InsertPoint);
     ++NumAdded;
   }
 
@@ -134,36 +120,17 @@ DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
 
   // Get the final index vector, including an initial [0] as before.
   std::vector<Value*> Indices;
-  Indices.push_back(Constant::getNullValue(Type::UIntTy));
+  Indices.push_back(Constant::getNullValue(Type::LongTy));
   Indices.push_back(*OI);
 
-  Instruction *NewI = 0;
-  switch(MAI.getOpcode()) {
-  case Instruction::Load:
-    NewI = new LoadInst(LastPtr, Indices, MAI.getName());
-    break;
-  case Instruction::Store:
-    NewI = new StoreInst(MAI.getOperand(0), LastPtr, Indices);
-    break;
-  case Instruction::GetElementPtr:
-    NewI = new GetElementPtrInst(LastPtr, Indices, MAI.getName());
-    break;
-  default:
-    assert(0 && "Unrecognized memory access instruction");
-  }
-  NewInsts.push_back(NewI);
+  Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
+                                        InsertPoint);
 
   // Replace all uses of the old instruction with the new
-  MAI.replaceAllUsesWith(NewI);
-
-  // Now delete the old instruction...
-  delete &MAI;
+  GEP->replaceAllUsesWith(NewVal);
 
-  // Insert all of the new instructions...
-  BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
+  // Now remove and delete the old instruction...
+  BB->getInstList().erase(GEP);
 
-  // Advance the iterator to the instruction following the one just inserted...
-  BBI = NewInsts.back();
-  ++BBI;
   return true;
 }