[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
Chris Lattner
lattner at cs.uiuc.edu
Wed Apr 7 13:39:01 PDT 2004
Changes in directory llvm/lib/Transforms/Scalar:
InstructionCombining.cpp updated: 1.179 -> 1.180
---
Log message:
Implement test/Regression/Transforms/InstCombine/getelementptr_index.ll
---
Diffs of the changes: (+38 -23)
Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.179 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.180
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.179 Mon Apr 5 11:02:41 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp Wed Apr 7 13:38:20 2004
@@ -2317,32 +2317,47 @@
// Eliminate unneeded casts for indices.
bool MadeChange = false;
- for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i)
- if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
- Value *Src = CI->getOperand(0);
- const Type *SrcTy = Src->getType();
- const Type *DestTy = CI->getType();
- if (Src->getType()->isInteger()) {
- if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) {
- // We can always eliminate a cast from ulong or long to the other. We
- // can always eliminate a cast from uint to int or the other on 32-bit
- // pointer platforms.
- if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) {
- MadeChange = true;
- GEP.setOperand(i, Src);
- }
- } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
- SrcTy->getPrimitiveSize() == 4) {
- // We can always eliminate a cast from int to [u]long. We can
- // eliminate a cast from uint to [u]long iff the target is a 32-bit
- // pointer target.
- if (SrcTy->isSigned() ||
- SrcTy->getPrimitiveSize() >= TD->getPointerSize()) {
- MadeChange = true;
- GEP.setOperand(i, Src);
+ gep_type_iterator GTI = gep_type_begin(GEP);
+ for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i, ++GTI)
+ if (isa<SequentialType>(*GTI)) {
+ if (CastInst *CI = dyn_cast<CastInst>(GEP.getOperand(i))) {
+ Value *Src = CI->getOperand(0);
+ const Type *SrcTy = Src->getType();
+ const Type *DestTy = CI->getType();
+ if (Src->getType()->isInteger()) {
+ if (SrcTy->getPrimitiveSize() == DestTy->getPrimitiveSize()) {
+ // We can always eliminate a cast from ulong or long to the other.
+ // We can always eliminate a cast from uint to int or the other on
+ // 32-bit pointer platforms.
+ if (DestTy->getPrimitiveSize() >= TD->getPointerSize()) {
+ MadeChange = true;
+ GEP.setOperand(i, Src);
+ }
+ } else if (SrcTy->getPrimitiveSize() < DestTy->getPrimitiveSize() &&
+ SrcTy->getPrimitiveSize() == 4) {
+ // We can always eliminate a cast from int to [u]long. We can
+ // eliminate a cast from uint to [u]long iff the target is a 32-bit
+ // pointer target.
+ if (SrcTy->isSigned() ||
+ 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;
More information about the llvm-commits
mailing list