* Clean up code a little bit
[oota-llvm.git] / lib / Transforms / Scalar / DecomposeMultiDimRefs.cpp
index 657640471a11b3b8e7ed49cc961468570b8f0abe..b50c4fb5bf8c97efcea32f2992479a9447685dba 100644 (file)
@@ -47,9 +47,9 @@ 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
+    if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&*II))
+      if (GEP->getNumIndices() >= 2) {
+        Changed |= decomposeArrayRef(II); // always modifies II
         continue;
       }
     ++II;
@@ -64,7 +64,7 @@ IsZero(Value* idx)
   return (isa<ConstantInt>(idx) && cast<ConstantInt>(idx)->isNullValue());
 }
 
-// For any MemAccessInst with 2 or more array and structure indices:
+// For any GetElementPtrInst with 2 or more array and structure indices:
 // 
 //      opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN
 // 
@@ -88,19 +88,9 @@ IsZero(Value* idx)
 bool
 DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
 {
-  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;
-    return false;
-  }
-
-  BasicBlock *BB = MAI.getParent();
-  Value *LastPtr = MAI.getPointerOperand();
+  GetElementPtrInst &GEP = cast<GetElementPtrInst>(*BBI);
+  BasicBlock *BB = GEP.getParent();
+  Value *LastPtr = GEP.getPointerOperand();
 
   // Remove the instruction from the stream
   BB->getInstList().remove(BBI);
@@ -109,12 +99,12 @@ DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
   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 (OI == GEP.idx_begin()) {
       if (IsZero(*OI)) continue;
     } else
       // Not the first index: include initial [0] to deref the last ptr
@@ -137,27 +127,14 @@ DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI)
   Indices.push_back(Constant::getNullValue(Type::UIntTy));
   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");
-  }
+  Instruction *NewI = new GetElementPtrInst(LastPtr, Indices, GEP.getName());
   NewInsts.push_back(NewI);
 
   // Replace all uses of the old instruction with the new
-  MAI.replaceAllUsesWith(NewI);
+  GEP.replaceAllUsesWith(NewI);
 
   // Now delete the old instruction...
-  delete &MAI;
+  delete &GEP;
 
   // Insert all of the new instructions...
   BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());