mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-12-28 06:27:16 +00:00
Code cleanups
llvm-svn: 2391
This commit is contained in:
parent
4c9a3f11d3
commit
a8688d69c5
@ -1,4 +1,4 @@
|
|||||||
//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D ---=//
|
//===- llvm/Transforms/DecomposeMultiDimRefs.cpp - Lower array refs to 1D -===//
|
||||||
//
|
//
|
||||||
// DecomposeMultiDimRefs -
|
// DecomposeMultiDimRefs -
|
||||||
// Convert multi-dimensional references consisting of any combination
|
// Convert multi-dimensional references consisting of any combination
|
||||||
@ -17,6 +17,41 @@
|
|||||||
#include "llvm/Function.h"
|
#include "llvm/Function.h"
|
||||||
#include "llvm/Pass.h"
|
#include "llvm/Pass.h"
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
struct DecomposePass : public BasicBlockPass {
|
||||||
|
virtual bool runOnBasicBlock(BasicBlock *BB);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void decomposeArrayRef(BasicBlock::iterator &BBI);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
// For any combination of 2 or more array and structure indices,
|
||||||
@ -28,160 +63,109 @@
|
|||||||
// uses the last ptr2 generated in the loop and a single index.
|
// uses the last ptr2 generated in the loop and a single index.
|
||||||
// If any index is (uint) 0, we omit the getElementPtr instruction.
|
// If any index is (uint) 0, we omit the getElementPtr instruction.
|
||||||
//
|
//
|
||||||
static BasicBlock::iterator
|
void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI){
|
||||||
decomposeArrayRef(BasicBlock::iterator& BBI)
|
|
||||||
{
|
|
||||||
MemAccessInst *memI = cast<MemAccessInst>(*BBI);
|
MemAccessInst *memI = cast<MemAccessInst>(*BBI);
|
||||||
BasicBlock* BB = memI->getParent();
|
BasicBlock* BB = memI->getParent();
|
||||||
Value* lastPtr = memI->getPointerOperand();
|
Value* lastPtr = memI->getPointerOperand();
|
||||||
|
|
||||||
|
// Remove the instruction from the stream
|
||||||
|
BB->getInstList().remove(BBI);
|
||||||
|
|
||||||
vector<Instruction*> newIvec;
|
vector<Instruction*> newIvec;
|
||||||
|
|
||||||
// Process each index except the last one.
|
// Process each index except the last one.
|
||||||
//
|
//
|
||||||
MemAccessInst::const_op_iterator OI = memI->idx_begin();
|
User::const_op_iterator OI = memI->idx_begin(), OE = memI->idx_end();
|
||||||
MemAccessInst::const_op_iterator OE = memI->idx_end();
|
for (; OI != OE && OI+1 != OE; ++OI) {
|
||||||
for ( ; OI != OE; ++OI)
|
assert(isa<PointerType>(lastPtr->getType()));
|
||||||
{
|
|
||||||
assert(isa<PointerType>(lastPtr->getType()));
|
|
||||||
|
|
||||||
if (OI+1 == OE) // stop before the last operand
|
// Check for a zero index. This will need a cast instead of
|
||||||
break;
|
// a getElementPtr, or it may need neither.
|
||||||
|
bool indexIsZero = isa<ConstantUInt>(*OI) &&
|
||||||
|
cast<Constant>(*OI)->isNullValue();
|
||||||
|
|
||||||
// Check for a zero index. This will need a cast instead of
|
// Extract the first index. If the ptr is a pointer to a structure
|
||||||
// a getElementPtr, or it may need neither.
|
// and the next index is a structure offset (i.e., not an array offset),
|
||||||
bool indexIsZero = bool(isa<ConstantUInt>(*OI) &&
|
// we need to include an initial [0] to index into the pointer.
|
||||||
cast<ConstantUInt>(*OI)->getValue() == 0);
|
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));
|
||||||
|
|
||||||
|
// 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());
|
||||||
|
|
||||||
// Extract the first index. If the ptr is a pointer to a structure
|
// Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec
|
||||||
// and the next index is a structure offset (i.e., not an array offset),
|
// This is not needed if the index is zero.
|
||||||
// we need to include an initial [0] to index into the pointer.
|
Value *gepValue;
|
||||||
vector<Value*> idxVec(1, *OI);
|
if (indexIsZero)
|
||||||
PointerType* ptrType = cast<PointerType>(lastPtr->getType());
|
gepValue = lastPtr;
|
||||||
if (isa<StructType>(ptrType->getElementType())
|
else {
|
||||||
&& ! ptrType->indexValid(*OI))
|
gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
|
||||||
idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
|
newIvec.push_back(cast<Instruction>(gepValue));
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
lastPtr = castInst;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}
|
||||||
|
|
||||||
|
lastPtr = castInst;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Now create a new instruction to replace the original one
|
// Now create a new instruction to replace the original one
|
||||||
//
|
//
|
||||||
PointerType* ptrType = cast<PointerType>(lastPtr->getType());
|
PointerType *ptrType = cast<PointerType>(lastPtr->getType());
|
||||||
assert(ptrType);
|
|
||||||
|
|
||||||
// First, get the final index vector. As above, we may need an initial [0].
|
// First, get the final index vector. As above, we may need an initial [0].
|
||||||
vector<Value*> idxVec(1, *OI);
|
vector<Value*> idxVec(1, *OI);
|
||||||
if (isa<StructType>(ptrType->getElementType())
|
if (isa<StructType>(ptrType->getElementType())
|
||||||
&& ! ptrType->indexValid(*OI))
|
&& !ptrType->indexValid(*OI))
|
||||||
idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0));
|
idxVec.insert(idxVec.begin(), Constant::getNullValue(Type::UIntTy));
|
||||||
|
|
||||||
const std::string newInstName = memI->hasName()? memI->getName()
|
|
||||||
: string("finalRef");
|
|
||||||
Instruction* newInst = NULL;
|
Instruction* newInst = NULL;
|
||||||
|
switch(memI->getOpcode()) {
|
||||||
switch(memI->getOpcode())
|
case Instruction::Load:
|
||||||
{
|
newInst = new LoadInst(lastPtr, idxVec, memI->getName());
|
||||||
case Instruction::Load:
|
break;
|
||||||
newInst = new LoadInst(lastPtr, idxVec /*, newInstName */); break;
|
case Instruction::Store:
|
||||||
case Instruction::Store:
|
newInst = new StoreInst(memI->getOperand(0), lastPtr, idxVec);
|
||||||
newInst = new StoreInst(memI->getOperand(0),
|
break;
|
||||||
lastPtr, idxVec /*, newInstName */); break;
|
case Instruction::GetElementPtr:
|
||||||
break;
|
newInst = new GetElementPtrInst(lastPtr, idxVec, memI->getName());
|
||||||
case Instruction::GetElementPtr:
|
break;
|
||||||
newInst = new GetElementPtrInst(lastPtr, idxVec /*, newInstName */); break;
|
default:
|
||||||
default:
|
assert(0 && "Unrecognized memory access instruction");
|
||||||
assert(0 && "Unrecognized memory access instruction"); break;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
newIvec.push_back(newInst);
|
newIvec.push_back(newInst);
|
||||||
|
|
||||||
// Replace all uses of the old instruction with the new
|
// Replace all uses of the old instruction with the new
|
||||||
memI->replaceAllUsesWith(newInst);
|
memI->replaceAllUsesWith(newInst);
|
||||||
|
|
||||||
BasicBlock::iterator newI = BBI;;
|
// Now delete the old instruction...
|
||||||
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;
|
delete memI;
|
||||||
|
|
||||||
|
// Convert our iterator into an index... that cannot get invalidated
|
||||||
|
unsigned ItOffs = BBI-BB->begin();
|
||||||
|
|
||||||
|
// Insert all of the new instructions...
|
||||||
|
BB->getInstList().insert(BBI, newIvec.begin(), newIvec.end());
|
||||||
|
|
||||||
return newI + newIvec.size() - 1; // pointer to last new instr
|
// Advance the iterator to the instruction following the one just inserted...
|
||||||
}
|
BBI = BB->begin() + (ItOffs+newIvec.size());
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
// Entry point for array or structure references with multiple indices.
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
struct DecomposeMultiDimRefsPass : public FunctionPass {
|
|
||||||
virtual bool runOnFunction(Function *F) {
|
|
||||||
return doDecomposeMultiDimRefs(F);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
||||||
AU.preservesCFG();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
Pass *createDecomposeMultiDimRefsPass() {
|
|
||||||
return new DecomposeMultiDimRefsPass();
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user