Use early exits. Reduce indentation.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@64226 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Devang Patel 2009-02-10 19:28:07 +00:00
parent d4e1845849
commit 7afe8fa066

View File

@ -1142,47 +1142,52 @@ void SROA::CleanupGEP(GetElementPtrInst *GEPI) {
gep_type_iterator I = gep_type_begin(GEPI); gep_type_iterator I = gep_type_begin(GEPI);
++I; ++I;
if (const ArrayType *AT = dyn_cast<ArrayType>(*I)) { const ArrayType *AT = dyn_cast<ArrayType>(*I);
uint64_t NumElements = AT->getNumElements(); if (!AT)
return;
uint64_t NumElements = AT->getNumElements();
if (isa<ConstantInt>(I.getOperand()))
return;
if (NumElements == 1) {
GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty));
return;
}
if (!isa<ConstantInt>(I.getOperand())) { assert(NumElements == 2 && "Unhandled case!");
if (NumElements == 1) { // All users of the GEP must be loads. At each use of the GEP, insert
GEPI->setOperand(2, Constant::getNullValue(Type::Int32Ty)); // two loads of the appropriate indexed GEP and select between them.
} else { Value *IsOne = new ICmpInst(ICmpInst::ICMP_NE, I.getOperand(),
assert(NumElements == 2 && "Unhandled case!"); Constant::getNullValue(I.getOperand()->getType()),
// All users of the GEP must be loads. At each use of the GEP, insert "isone", GEPI);
// two loads of the appropriate indexed GEP and select between them. // Insert the new GEP instructions, which are properly indexed.
Value *IsOne = new ICmpInst(ICmpInst::ICMP_NE, I.getOperand(), SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end());
Constant::getNullValue(I.getOperand()->getType()), Indices[1] = Constant::getNullValue(Type::Int32Ty);
"isone", GEPI); Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
// Insert the new GEP instructions, which are properly indexed. Indices.begin(),
SmallVector<Value*, 8> Indices(GEPI->op_begin()+1, GEPI->op_end()); Indices.end(),
Indices[1] = Constant::getNullValue(Type::Int32Ty); GEPI->getName()+".0", GEPI);
Value *ZeroIdx = GetElementPtrInst::Create(GEPI->getOperand(0), Indices[1] = ConstantInt::get(Type::Int32Ty, 1);
Indices.begin(), Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0),
Indices.end(), Indices.begin(),
GEPI->getName()+".0", GEPI); Indices.end(),
Indices[1] = ConstantInt::get(Type::Int32Ty, 1); GEPI->getName()+".1", GEPI);
Value *OneIdx = GetElementPtrInst::Create(GEPI->getOperand(0), // Replace all loads of the variable index GEP with loads from both
Indices.begin(), // indexes and a select.
Indices.end(), while (!GEPI->use_empty()) {
GEPI->getName()+".1", GEPI); LoadInst *LI = cast<LoadInst>(GEPI->use_back());
// Replace all loads of the variable index GEP with loads from both Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI);
// indexes and a select. Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI);
while (!GEPI->use_empty()) { Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI);
LoadInst *LI = cast<LoadInst>(GEPI->use_back()); LI->replaceAllUsesWith(R);
Value *Zero = new LoadInst(ZeroIdx, LI->getName()+".0", LI); LI->eraseFromParent();
Value *One = new LoadInst(OneIdx , LI->getName()+".1", LI);
Value *R = SelectInst::Create(IsOne, One, Zero, LI->getName(), LI);
LI->replaceAllUsesWith(R);
LI->eraseFromParent();
}
GEPI->eraseFromParent();
}
}
} }
GEPI->eraseFromParent();
} }
/// CleanupAllocaUsers - If SROA reported that it can promote the specified /// CleanupAllocaUsers - If SROA reported that it can promote the specified
/// allocation, but only if cleaned up, perform the cleanups required. /// allocation, but only if cleaned up, perform the cleanups required.
void SROA::CleanupAllocaUsers(AllocationInst *AI) { void SROA::CleanupAllocaUsers(AllocationInst *AI) {