-//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D ---=//
+//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
//
-// DecomposeMultiDimRefs -
-// Convert multi-dimensional references consisting of any combination
-// of 2 or more array and structure indices into a sequence of
-// instructions (using getelementpr and cast) so that each instruction
-// has at most one index (except structure references,
-// which need an extra leading index of [0]).
+// DecomposeMultiDimRefs - Convert multi-dimensional references consisting of
+// any combination of 2 or more array and structure indices into a sequence of
+// instructions (using getelementpr and cast) so that each instruction has at
+// most one index (except structure references, which need an extra leading
+// index of [0]).
//
//===----------------------------------------------------------------------===//
-#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 {
+ virtual bool runOnBasicBlock(BasicBlock &BB);
+
+ private:
+ static void decomposeArrayRef(BasicBlock::iterator &BBI);
+ };
+
+ RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
+ "structure/array references");
+}
+
+Pass *createDecomposeMultiDimRefsPass() {
+ return new DecomposePass();
+}
+
+
+// runOnBasicBlock - Entry point for array or structure references with multiple
+// indices.
+//
+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->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
+ decomposeArrayRef(II);
+ Changed = true;
+ } else {
+ ++II;
+ }
+ } else {
+ ++II;
+ }
+ }
+
+ return Changed;
+}
//
// For any combination of 2 or more array and structure indices,
// uses the last ptr2 generated in the loop and a single index.
// If any index is (uint) 0, we omit the getElementPtr instruction.
//
-static BasicBlock::iterator
-decomposeArrayRef(BasicBlock::iterator& BBI)
-{
- MemAccessInst *memI = cast<MemAccessInst>(*BBI);
- BasicBlock* BB = memI->getParent();
- Value* lastPtr = memI->getPointerOperand();
- vector<Instruction*> newIvec;
+
+void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
+ MemAccessInst &MAI = cast<MemAccessInst>(*BBI);
+ BasicBlock *BB = MAI.getParent();
+ Value *LastPtr = MAI.getPointerOperand();
+
+ // Remove the instruction from the stream
+ BB->getInstList().remove(BBI);
+
+ std::vector<Instruction*> NewInsts;
// Process each index except the last one.
//
- MemAccessInst::const_op_iterator OI = memI->idx_begin();
- MemAccessInst::const_op_iterator OE = memI->idx_end();
- for ( ; OI != OE; ++OI)
- {
- assert(isa<PointerType>(lastPtr->getType()));
-
- if (OI+1 == OE) // stop before the last operand
- break;
-
- // Check for a zero index. This will need a cast instead of
- // a getElementPtr, or it may need neither.
- bool indexIsZero = bool(isa<ConstantUInt>(*OI) &&
- cast<ConstantUInt>(*OI)->getValue() == 0);
-
- // 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*> idxVec(1, *OI);
- PointerType* ptrType = cast<PointerType>(lastPtr->getType());
- if (isa<StructType>(ptrType->getElementType())
- && ! ptrType->indexValid(*OI))
- idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
-
- // Get the type obtained by applying the first index.
- // It must be a structure or array.
- const Type* nextType = MemAccessInst::getIndexedType(lastPtr->getType(),
- idxVec, true);
- assert(isa<StructType>(nextType) || isa<ArrayType>(nextType));
+
+ User::const_op_iterator OI = MAI.idx_begin(), OE = MAI.idx_end();
+ for (; OI+1 != OE; ++OI) {
+ assert(isa<PointerType>(LastPtr->getType()));
- // Get a pointer to the structure or to the elements of the array.
- const Type* nextPtrType =
- PointerType::get(isa<StructType>(nextType)? nextType
- : cast<ArrayType>(nextType)->getElementType());
+ // 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->get())->isNullValue() &&
+ OI->get()->getType() == Type::UIntTy;
- // Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec
- // This is not needed if the index is zero.
- Value* gepValue;
- if (indexIsZero)
- gepValue = lastPtr;
- else
- {
- gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
- newIvec.push_back(cast<Instruction>(gepValue));
- }
+ // 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.
+ //
+
+ 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));
+ Indices.push_back(*OI);
+
+ // Get the type obtained by applying the first index.
+ // It must be a structure or array.
+ const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
+ Indices, true);
+ assert(isa<CompositeType>(NextTy));
+
+ // Get a pointer to the structure or to the elements of the array.
+ const Type *NextPtrTy =
+ PointerType::get(isa<StructType>(NextTy) ? NextTy
+ : cast<ArrayType>(NextTy)->getElementType());
- // Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType
- // This is not needed if the two types are identical.
- Value* castInst;
- if (gepValue->getType() == nextPtrType)
- castInst = gepValue;
- else
- {
- castInst = new CastInst(gepValue, nextPtrType, "ptr2");
- newIvec.push_back(cast<Instruction>(castInst));
- }
+ // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
+ // This is not needed if the index is zero.
+ if (!indexIsZero) {
+ LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
+ NewInsts.push_back(cast<Instruction>(LastPtr));
+ ++NumAdded;
+ }
+
- lastPtr = castInst;
+ // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
+ // This is not needed if the two types are identical.
+ //
+ 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* ptrType = cast<PointerType>(lastPtr->getType());
- assert(ptrType);
+ const PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
// First, get the final index vector. As above, we may need an initial [0].
- vector<Value*> idxVec(1, *OI);
- if (isa<StructType>(ptrType->getElementType())
- && ! ptrType->indexValid(*OI))
- idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
-
- const std::string newInstName = memI->hasName()? memI->getName()
- : string("finalRef");
- Instruction* newInst = NULL;
-
- switch(memI->getOpcode())
- {
- case Instruction::Load:
- newInst = new LoadInst(lastPtr, idxVec /*, newInstName */); break;
- case Instruction::Store:
- newInst = new StoreInst(memI->getOperand(0),
- lastPtr, idxVec /*, newInstName */); break;
- break;
- case Instruction::GetElementPtr:
- newInst = new GetElementPtrInst(lastPtr, idxVec /*, newInstName */); break;
- default:
- assert(0 && "Unrecognized memory access instruction"); break;
- }
-
- newIvec.push_back(newInst);
-
- // Replace all uses of the old instruction with the new
- memI->replaceAllUsesWith(newInst);
-
- BasicBlock::iterator newI = BBI;;
- for (int i = newIvec.size()-1; i >= 0; i--)
- newI = BB->getInstList().insert(newI, newIvec[i]);
-
- // Now delete the old instruction and return a pointer to the last new one
- BB->getInstList().remove(memI);
- delete memI;
-
- return newI + newIvec.size() - 1; // pointer to last new instr
-}
-
-//---------------------------------------------------------------------------
-// Entry point for array or structure references with multiple indices.
-//---------------------------------------------------------------------------
+ std::vector<Value*> Indices;
+ if (isa<StructType>(PtrTy->getElementType())
+ && !PtrTy->indexValid(*OI))
+ Indices.push_back(Constant::getNullValue(Type::UIntTy));
-static bool
-doDecomposeMultiDimRefs(Function *F)
-{
- bool changed = false;
-
- for (Function::iterator BI = F->begin(), BE = F->end(); BI != BE; ++BI)
- for (BasicBlock::iterator newI, II = (*BI)->begin();
- II != (*BI)->end(); II = ++newI)
- {
- newI = II;
- if (MemAccessInst *memI = dyn_cast<MemAccessInst>(*II))
- if (memI->getNumOperands() > 1 + memI->getFirstIndexOperandNumber())
- {
- newI = decomposeArrayRef(II);
- changed = true;
- }
- }
-
- return changed;
-}
+ 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);
-namespace {
- struct DecomposeMultiDimRefsPass : public FunctionPass {
- virtual bool runOnFunction(Function *F) {
- return doDecomposeMultiDimRefs(F);
- }
+
+ // Replace all uses of the old instruction with the new
+ MAI.replaceAllUsesWith(NewI);
- virtual void getAnalysisUsage(AnalysisUsage &AU) const {
- AU.preservesCFG();
- }
- };
-}
+ // Now delete the old instruction...
+ delete &MAI;
-Pass *createDecomposeMultiDimRefsPass() {
- return new DecomposeMultiDimRefsPass();
+ // 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 = NewInsts.back();
+ ++BBI;
}