From: Chris Lattner Date: Sat, 21 Jun 2003 23:12:02 +0000 (+0000) Subject: Instcombine cast (getelementptr Ptr, 0, 0, 0) to ... into: cast Ptr to ... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=797249bc136378d8af429b136a6c35242bd71623;p=oota-llvm.git Instcombine cast (getelementptr Ptr, 0, 0, 0) to ... into: cast Ptr to ... This fixes type safety problems in a variety of benchmarks that were confusing DSA. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6837 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index bd95a18a701..a0a09e4b1da 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -930,6 +930,23 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) { } } + // If casting the result of a getelementptr instruction with no offset, turn + // this into a cast of the original pointer! + // + if (GetElementPtrInst *GEP = dyn_cast(CI.getOperand(0))) { + bool AllZeroOperands = true; + for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i) + if (!isa(GEP->getOperand(i)) || + !cast(GEP->getOperand(i))->isNullValue()) { + AllZeroOperands = false; + break; + } + if (AllZeroOperands) { + CI.setOperand(0, GEP->getOperand(0)); + return &CI; + } + } + return 0; }