forked from OSchip/llvm-project
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
This commit is contained in:
parent
116783613a
commit
dba59921d7
|
|
@ -21,15 +21,13 @@
|
||||||
namespace {
|
namespace {
|
||||||
Statistic<> NumAdded("lowerrefs\t\t- # of getelementptr instructions added");
|
Statistic<> NumAdded("lowerrefs\t\t- # of getelementptr instructions added");
|
||||||
|
|
||||||
class DecomposePass : public BasicBlockPass {
|
struct DecomposePass : public BasicBlockPass {
|
||||||
static bool decomposeArrayRef(GetElementPtrInst &GEP);
|
|
||||||
public:
|
|
||||||
virtual bool runOnBasicBlock(BasicBlock &BB);
|
virtual bool runOnBasicBlock(BasicBlock &BB);
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
|
RegisterOpt<DecomposePass> X("lowerrefs", "Decompose multi-dimensional "
|
||||||
"structure/array references");
|
"structure/array references");
|
||||||
}
|
|
||||||
|
|
||||||
Pass
|
Pass
|
||||||
*createDecomposeMultiDimRefsPass()
|
*createDecomposeMultiDimRefsPass()
|
||||||
|
|
@ -44,17 +42,17 @@ Pass
|
||||||
bool
|
bool
|
||||||
DecomposePass::runOnBasicBlock(BasicBlock &BB)
|
DecomposePass::runOnBasicBlock(BasicBlock &BB)
|
||||||
{
|
{
|
||||||
bool Changed = false;
|
bool changed = false;
|
||||||
for (BasicBlock::iterator II = BB.begin(); II != BB.end(); ) {
|
for (BasicBlock::iterator II = BB.begin(); II != BB.end(); )
|
||||||
Instruction *I = II;
|
if (GetElementPtrInst *gep = dyn_cast<GetElementPtrInst>(&*II++)) // pre-inc
|
||||||
++II;
|
if (gep->getNumIndices() >= 2)
|
||||||
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(I))
|
changed |= DecomposeArrayRef(gep); // always modifies II
|
||||||
if (GEP->getNumIndices() >= 2)
|
return changed;
|
||||||
Changed |= decomposeArrayRef(*GEP); // always modifies II
|
|
||||||
}
|
|
||||||
return Changed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Function: DecomposeArrayRef()
|
||||||
|
//
|
||||||
// For any GetElementPtrInst 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
|
// 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.
|
// Return value: true if the instruction was replaced; false otherwise.
|
||||||
//
|
//
|
||||||
bool
|
bool
|
||||||
DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP)
|
DecomposeArrayRef(GetElementPtrInst* GEP)
|
||||||
{
|
{
|
||||||
BasicBlock *BB = GEP.getParent();
|
if (GEP->getNumIndices() < 2)
|
||||||
Value *LastPtr = GEP.getPointerOperand();
|
return false;
|
||||||
Instruction *InsertPoint = GEP.getNext(); // Insert before the next insn
|
|
||||||
|
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.
|
// 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) {
|
for (; OI+1 != OE; ++OI) {
|
||||||
std::vector<Value*> Indices;
|
std::vector<Value*> Indices;
|
||||||
|
|
||||||
// If this is the first index and is 0, skip it and move on!
|
// 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()))
|
if (*OI == ConstantInt::getNullValue((*OI)->getType()))
|
||||||
continue;
|
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);
|
Indices.push_back(*OI);
|
||||||
|
|
||||||
|
|
@ -113,13 +116,14 @@ DecomposePass::decomposeArrayRef(GetElementPtrInst &GEP)
|
||||||
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
Indices.push_back(Constant::getNullValue(Type::LongTy));
|
||||||
Indices.push_back(*OI);
|
Indices.push_back(*OI);
|
||||||
|
|
||||||
Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP.getName(),
|
Value *NewVal = new GetElementPtrInst(LastPtr, Indices, GEP->getName(),
|
||||||
InsertPoint);
|
InsertPoint);
|
||||||
|
|
||||||
// Replace all uses of the old instruction with the new
|
// Replace all uses of the old instruction with the new
|
||||||
GEP.replaceAllUsesWith(NewVal);
|
GEP->replaceAllUsesWith(NewVal);
|
||||||
|
|
||||||
// Now remove and delete the old instruction...
|
// Now remove and delete the old instruction...
|
||||||
BB->getInstList().erase(&GEP);
|
BB->getInstList().erase(GEP);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue