More cleanups

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2392 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2002-04-29 01:58:47 +00:00
parent 2f6f03bddd
commit 097632eae3

View File

@ -1,11 +1,10 @@
//===- 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
// Convert multi-dimensional references consisting of any combination // any combination of 2 or more array and structure indices into a sequence of
// of 2 or more array and structure indices into a sequence of // instructions (using getelementpr and cast) so that each instruction has at
// instructions (using getelementpr and cast) so that each instruction // most one index (except structure references, which need an extra leading
// has at most one index (except structure references, // index of [0]).
// which need an extra leading index of [0]).
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -36,7 +35,6 @@ Pass *createDecomposeMultiDimRefsPass() {
// //
bool DecomposePass::runOnBasicBlock(BasicBlock *BB) { bool 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(); ) {
if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) { if (MemAccessInst *MAI = dyn_cast<MemAccessInst>(*II)) {
if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) { if (MAI->getNumOperands() > MAI->getFirstIndexOperandNumber()+1) {
@ -63,21 +61,21 @@ bool DecomposePass::runOnBasicBlock(BasicBlock *BB) {
// 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.
// //
void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI){ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI) {
MemAccessInst *memI = cast<MemAccessInst>(*BBI); MemAccessInst *MAI = cast<MemAccessInst>(*BBI);
BasicBlock* BB = memI->getParent(); BasicBlock *BB = MAI->getParent();
Value* lastPtr = memI->getPointerOperand(); Value *LastPtr = MAI->getPointerOperand();
// Remove the instruction from the stream // Remove the instruction from the stream
BB->getInstList().remove(BBI); BB->getInstList().remove(BBI);
vector<Instruction*> newIvec; vector<Instruction*> NewInsts;
// Process each index except the last one. // Process each index except the last one.
// //
User::const_op_iterator OI = memI->idx_begin(), OE = memI->idx_end(); User::const_op_iterator OI = MAI->idx_begin(), OE = MAI->idx_end();
for (; OI != OE && OI+1 != OE; ++OI) { for (; OI+1 != OE; ++OI) {
assert(isa<PointerType>(lastPtr->getType())); assert(isa<PointerType>(LastPtr->getType()));
// Check for a zero index. This will need a cast instead of // Check for a zero index. This will need a cast instead of
// a getElementPtr, or it may need neither. // a getElementPtr, or it may need neither.
@ -87,85 +85,82 @@ void DecomposePass::decomposeArrayRef(BasicBlock::iterator &BBI){
// Extract the first index. If the ptr is a pointer to a structure // 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), // 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. // we need to include an initial [0] to index into the pointer.
vector<Value*> idxVec(1, *OI); //
PointerType* ptrType = cast<PointerType>(lastPtr->getType()); vector<Value*> Indices;
if (isa<StructType>(ptrType->getElementType()) PointerType *PtrTy = cast<PointerType>(LastPtr->getType());
&& ! ptrType->indexValid(*OI)) if (isa<StructType>(PtrTy->getElementType())
idxVec.insert(idxVec.begin(), ConstantUInt::get(Type::UIntTy, 0)); && !PtrTy->indexValid(*OI))
Indices.push_back(Constant::getNullValue(Type::UIntTy));
Indices.push_back(*OI);
// Get the type obtained by applying the first index. // Get the type obtained by applying the first index.
// It must be a structure or array. // It must be a structure or array.
const Type* nextType = MemAccessInst::getIndexedType(lastPtr->getType(), const Type *NextTy = MemAccessInst::getIndexedType(LastPtr->getType(),
idxVec, true); Indices, true);
assert(isa<StructType>(nextType) || isa<ArrayType>(nextType)); assert(isa<CompositeType>(NextTy));
// Get a pointer to the structure or to the elements of the array. // Get a pointer to the structure or to the elements of the array.
const Type* nextPtrType = const Type *NextPtrTy =
PointerType::get(isa<StructType>(nextType) ? nextType PointerType::get(isa<StructType>(NextTy) ? NextTy
: cast<ArrayType>(nextType)->getElementType()); : cast<ArrayType>(NextTy)->getElementType());
// Instruction 1: nextPtr1 = GetElementPtr lastPtr, idxVec // Instruction 1: nextPtr1 = GetElementPtr LastPtr, Indices
// This is not needed if the index is zero. // This is not needed if the index is zero.
Value *gepValue; if (!indexIsZero) {
if (indexIsZero) LastPtr = new GetElementPtrInst(LastPtr, Indices, "ptr1");
gepValue = lastPtr; NewInsts.push_back(cast<Instruction>(LastPtr));
else {
gepValue = new GetElementPtrInst(lastPtr, idxVec,"ptr1");
newIvec.push_back(cast<Instruction>(gepValue));
} }
// Instruction 2: nextPtr2 = cast nextPtr1 to nextPtrType // Instruction 2: nextPtr2 = cast nextPtr1 to NextPtrTy
// This is not needed if the two types are identical. // This is not needed if the two types are identical.
Value *castInst; //
if (gepValue->getType() == nextPtrType) if (LastPtr->getType() != NextPtrTy) {
castInst = gepValue; LastPtr = new CastInst(LastPtr, NextPtrTy, "ptr2");
else { NewInsts.push_back(cast<Instruction>(LastPtr));
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 *PtrTy = cast<PointerType>(LastPtr->getType());
// 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*> Indices;
if (isa<StructType>(ptrType->getElementType()) if (isa<StructType>(PtrTy->getElementType())
&& !ptrType->indexValid(*OI)) && !PtrTy->indexValid(*OI))
idxVec.insert(idxVec.begin(), Constant::getNullValue(Type::UIntTy)); Indices.push_back(Constant::getNullValue(Type::UIntTy));
Instruction* newInst = NULL; Indices.push_back(*OI);
switch(memI->getOpcode()) {
Instruction *NewI = 0;
switch(MAI->getOpcode()) {
case Instruction::Load: case Instruction::Load:
newInst = new LoadInst(lastPtr, idxVec, memI->getName()); NewI = new LoadInst(LastPtr, Indices, MAI->getName());
break; break;
case Instruction::Store: case Instruction::Store:
newInst = new StoreInst(memI->getOperand(0), lastPtr, idxVec); NewI = new StoreInst(MAI->getOperand(0), LastPtr, Indices);
break; break;
case Instruction::GetElementPtr: case Instruction::GetElementPtr:
newInst = new GetElementPtrInst(lastPtr, idxVec, memI->getName()); NewI = new GetElementPtrInst(LastPtr, Indices, MAI->getName());
break; break;
default: default:
assert(0 && "Unrecognized memory access instruction"); assert(0 && "Unrecognized memory access instruction");
} }
newIvec.push_back(newInst); NewInsts.push_back(NewI);
// Replace all uses of the old instruction with the new // Replace all uses of the old instruction with the new
memI->replaceAllUsesWith(newInst); MAI->replaceAllUsesWith(NewI);
// Now delete the old instruction... // Now delete the old instruction...
delete memI; delete MAI;
// Convert our iterator into an index... that cannot get invalidated // Convert our iterator into an index... that cannot get invalidated
unsigned ItOffs = BBI-BB->begin(); unsigned ItOffs = BBI-BB->begin();
// Insert all of the new instructions... // Insert all of the new instructions...
BB->getInstList().insert(BBI, newIvec.begin(), newIvec.end()); BB->getInstList().insert(BBI, NewInsts.begin(), NewInsts.end());
// Advance the iterator to the instruction following the one just inserted... // Advance the iterator to the instruction following the one just inserted...
BBI = BB->begin() + (ItOffs+newIvec.size()); BBI = BB->begin() + ItOffs + NewInsts.size();
} }