diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index b3cf01342bd..c73fb792d05 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -103,23 +103,6 @@ bool InstCombiner::ShouldChangeType(const Type *From, const Type *To) const { return true; } -/// getBitCastOperand - If the specified operand is a CastInst, a constant -/// expression bitcast, or a GetElementPtrInst with all zero indices, return the -/// operand value, otherwise return null. - -// FIXME: Value::stripPointerCasts -static Value *getBitCastOperand(Value *V) { - if (Operator *O = dyn_cast(V)) { - if (O->getOpcode() == Instruction::BitCast) - return O->getOperand(0); - if (GEPOperator *GEP = dyn_cast(V)) - if (GEP->hasAllZeroIndices()) - return GEP->getPointerOperand(); - } - return 0; -} - - // SimplifyCommutative - This performs a few simplifications for commutative // operators: @@ -2925,8 +2908,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { } if (!Indices.empty()) - return (cast(&GEP)->isInBounds() && - Src->isInBounds()) ? + return (GEP.isInBounds() && Src->isInBounds()) ? GetElementPtrInst::CreateInBounds(Src->getOperand(0), Indices.begin(), Indices.end(), GEP.getName()) : GetElementPtrInst::Create(Src->getOperand(0), Indices.begin(), @@ -2934,16 +2916,10 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { } // Handle gep(bitcast x) and gep(gep x, 0, 0, 0). - if (Value *X = getBitCastOperand(PtrOp)) { - assert(isa(X->getType()) && "Must be cast from pointer"); + Value *StrippedPtr = PtrOp->stripPointerCasts(); + if (StrippedPtr != PtrOp) { + const PointerType *StrippedPtrTy =cast(StrippedPtr->getType()); - // If the input bitcast is actually "bitcast(bitcast(x))", then we don't - // want to change the gep until the bitcasts are eliminated. - if (getBitCastOperand(X)) { - Worklist.AddValue(PtrOp); - return 0; - } - bool HasZeroPointerIndex = false; if (ConstantInt *C = dyn_cast(GEP.getOperand(1))) HasZeroPointerIndex = C->isZero(); @@ -2957,21 +2933,21 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // This occurs when the program declares an array extern like "int X[];" if (HasZeroPointerIndex) { const PointerType *CPTy = cast(PtrOp->getType()); - const PointerType *XTy = cast(X->getType()); if (const ArrayType *CATy = dyn_cast(CPTy->getElementType())) { // GEP (bitcast i8* X to [0 x i8]*), i32 0, ... ? - if (CATy->getElementType() == XTy->getElementType()) { + if (CATy->getElementType() == StrippedPtrTy->getElementType()) { // -> GEP i8* X, ... - SmallVector Indices(GEP.idx_begin()+1, GEP.idx_end()); - return cast(&GEP)->isInBounds() ? - GetElementPtrInst::CreateInBounds(X, Indices.begin(), Indices.end(), - GEP.getName()) : - GetElementPtrInst::Create(X, Indices.begin(), Indices.end(), - GEP.getName()); + SmallVector Idx(GEP.idx_begin()+1, GEP.idx_end()); + GetElementPtrInst *Res = + GetElementPtrInst::Create(StrippedPtr, Idx.begin(), + Idx.end(), GEP.getName()); + Res->setIsInBounds(GEP.isInBounds()); + return Res; } - if (const ArrayType *XATy = dyn_cast(XTy->getElementType())){ + if (const ArrayType *XATy = + dyn_cast(StrippedPtrTy->getElementType())){ // GEP (bitcast [10 x i8]* X to [0 x i8]*), i32 0, ... ? if (CATy->getElementType() == XATy->getElementType()) { // -> GEP [10 x i8]* X, i32 0, ... @@ -2979,7 +2955,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // to an array of the same type as the destination pointer // array. Because the array type is never stepped over (there // is a leading zero) we can fold the cast into this GEP. - GEP.setOperand(0, X); + GEP.setOperand(0, StrippedPtr); return &GEP; } } @@ -2988,7 +2964,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // Transform things like: // %t = getelementptr i32* bitcast ([2 x i32]* %str to i32*), i32 %V // into: %t1 = getelementptr [2 x i32]* %str, i32 0, i32 %V; bitcast - const Type *SrcElTy = cast(X->getType())->getElementType(); + const Type *SrcElTy = StrippedPtrTy->getElementType(); const Type *ResElTy=cast(PtrOp->getType())->getElementType(); if (TD && isa(SrcElTy) && TD->getTypeAllocSize(cast(SrcElTy)->getElementType()) == @@ -2996,9 +2972,9 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { Value *Idx[2]; Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); Idx[1] = GEP.getOperand(1); - Value *NewGEP = cast(&GEP)->isInBounds() ? - Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) : - Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName()); + Value *NewGEP = GEP.isInBounds() ? + Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()) : + Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()); // V and GEP are both pointer types --> BitCast return new BitCastInst(NewGEP, GEP.getType()); } @@ -3056,9 +3032,9 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { Value *Idx[2]; Idx[0] = Constant::getNullValue(Type::getInt32Ty(GEP.getContext())); Idx[1] = NewIdx; - Value *NewGEP = cast(&GEP)->isInBounds() ? - Builder->CreateInBoundsGEP(X, Idx, Idx + 2, GEP.getName()) : - Builder->CreateGEP(X, Idx, Idx + 2, GEP.getName()); + Value *NewGEP = GEP.isInBounds() ? + Builder->CreateInBoundsGEP(StrippedPtr, Idx, Idx + 2,GEP.getName()): + Builder->CreateGEP(StrippedPtr, Idx, Idx + 2, GEP.getName()); // The NewGEP must be pointer typed, so must the old one -> BitCast return new BitCastInst(NewGEP, GEP.getType()); } @@ -3106,7 +3082,7 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { const Type *InTy = cast(BCI->getOperand(0)->getType())->getElementType(); if (FindElementAtOffset(InTy, Offset, NewIndices)) { - Value *NGEP = cast(&GEP)->isInBounds() ? + Value *NGEP = GEP.isInBounds() ? Builder->CreateInBoundsGEP(BCI->getOperand(0), NewIndices.begin(), NewIndices.end()) : Builder->CreateGEP(BCI->getOperand(0), NewIndices.begin(),