Implement test/Regression/Transforms/InstCombine/getelementptr_index.ll

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12762 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2004-04-07 18:38:20 +00:00
parent ef09aab9ce
commit cb69a4ed64

View File

@@ -2317,32 +2317,47 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
// Eliminate unneeded casts for indices. // Eliminate unneeded casts for indices.
bool MadeChange = false; bool MadeChange = false;
for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i) gep_type_iterator GTI = gep_type_begin(GEP);
if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) { for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI)
Value *Src = CI->getOperand(0); if (isa<SequentialType>(*GTI)) {
const Type *SrcTy = Src->getType(); if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
const Type *DestTy = CI->getType(); Value *Src = CI->getOperand(0);
if (Src->getType()->isInteger()) { const Type *SrcTy = Src->getType();
if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) { const Type *DestTy = CI->getType();
// We can always eliminate a cast from ulong or long to the other. We if (Src->getType()->isInteger()) {
// can always eliminate a cast from uint to int or the other on 32-bit if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) {
// pointer platforms. // We can always eliminate a cast from ulong or long to the other.
if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) { // We can always eliminate a cast from uint to int or the other on
MadeChange = true; // 32-bit pointer platforms.
GEP.setOperand(i, Src); if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) {
} MadeChange = true;
} else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() && GEP.setOperand(i, Src);
SrcTy->getPrimitiveSize() == 4) { }
// We can always eliminate a cast from int to [u]long. We can } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
// eliminate a cast from uint to [u]long iff the target is a 32-bit SrcTy->getPrimitiveSize() == 4) {
// pointer target. // We can always eliminate a cast from int to [u]long. We can
if (SrcTy->isSigned() || // eliminate a cast from uint to [u]long iff the target is a 32-bit
SrcTy->getPrimitiveSize() >= TD->getPointerSize()) { // pointer target.
MadeChange = true; if (SrcTy->isSigned() ||
GEP.setOperand(i, Src); SrcTy->getPrimitiveSize() >= TD->getPointerSize()) {
MadeChange = true;
GEP.setOperand(i, Src);
}
} }
} }
} }
// If we are using a wider index than needed for this platform, shrink it
// to what we need. If the incoming value needs a cast instruction,
// insert it. This explicit cast can make subsequent optimizations more
// obvious.
Value *Op = GEP.getOperand(i);
if (Op->getType()->getPrimitiveSize() > TD->getPointerSize())
if (!isa<Constant>(Op)) {
Op = InsertNewInstBefore(new CastInst(Op, TD->getIntPtrType(),
Op->getName()), GEP);
GEP.setOperand(i, Op);
MadeChange = true;
}
} }
if (MadeChange) return &GEP; if (MadeChange) return &GEP;