From dba59921d7f4deff18c43c730f2fdcfa6485c4d2 Mon Sep 17 00:00:00 2001 From: "Vikram S. Adve" Date: Mon, 16 Sep 2002 16:40:07 +0000 Subject: [PATCH] Extract most of the transformation into an externally accessible function -- DecomposeArrayRef(GetElementPtrInst* GEP) -- that can be invoked on a single instruction at a time. llvm-svn: 3755 --- .../Scalar/DecomposeMultiDimRefs.cpp | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp b/llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp index 7a2ad4b03e11..2fb1346a5c85 100644 --- a/llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp +++ b/llvm/lib/Transforms/Scalar/DecomposeMultiDimRefs.cpp @@ -21,16 +21,14 @@ namespace { Statistic<> NumAdded("lowerrefs\t\t- # of getelementptr instructions added"); - class DecomposePass : public BasicBlockPass { - static bool decomposeArrayRef(GetElementPtrInst &GEP); - public: + struct DecomposePass : public BasicBlockPass { virtual bool runOnBasicBlock(BasicBlock &BB); }; - - RegisterOpt X("lowerrefs", "Decompose multi-dimensional " - "structure/array references"); } +RegisterOpt X("lowerrefs", "Decompose multi-dimensional " + "structure/array references"); + Pass *createDecomposeMultiDimRefsPass() { @@ -44,17 +42,17 @@ Pass bool DecomposePass::runOnBasicBlock(BasicBlock &BB) { - bool Changed = false; - for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) { - Instruction *I = II; - ++II; - if (GetElementPtrInst *GEP = dyn_cast(I)) - if (GEP->getNumIndices() >= 2) - Changed |= decomposeArrayRef(*GEP); // always modifies II - } - return Changed; + bool changed = false; + for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) + if (GetElementPtrInst *gep = dyn_cast(&*II++)) // pre-inc + if (gep->getNumIndices() >= 2) + changed |= DecomposeArrayRef(gep); // always modifies II + return changed; } + +// Function: DecomposeArrayRef() +// // For any GetElementPtrInst with 2 or more array and structure indices: // // opCode CompositeType* P, [uint|ubyte] idx1, ..., [uint|ubyte] idxN @@ -77,25 +75,30 @@ DecomposePass::runOnBasicBlock(BasicBlock &BB) // Return value: true if the instruction was replaced; false otherwise. // bool -DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP) +DecomposeArrayRef(GetElementPtrInst* GEP) { - BasicBlock *BB = GEP.getParent(); - Value *LastPtr = GEP.getPointerOperand(); - Instruction *InsertPoint = GEP.getNext(); // Insert before the next insn + if (GEP->getNumIndices() < 2) + return false; + + 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 NewInsts; // Process each index except the last one. - User::const_op_iterator OI = GEP.idx_begin(), OE = GEP.idx_end(); + User::const_op_iterator OI = GEP->idx_begin(), OE = GEP->idx_end(); for (; OI+1 != OE; ++OI) { std::vector Indices; // If this is the first index and is 0, skip it and move on! - if (OI == GEP.idx_begin()) { + 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)); } + else // Not the first index: include initial [0] to deref the last ptr + Indices.push_back(Constant::getNullValue(Type::LongTy)); Indices.push_back(*OI); @@ -113,13 +116,14 @@ DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP) Indices.push_back(Constant::getNullValue(Type::LongTy)); Indices.push_back(*OI); - Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP.getName(), + Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(), InsertPoint); // Replace all uses of the old instruction with the new - GEP.replaceAllUsesWith(NewVal); + GEP->replaceAllUsesWith(NewVal); // Now remove and delete the old instruction... - BB->getInstList().erase(&GEP); + BB->getInstList().erase(GEP); + return true; }