diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 01385db6c14..08905576d22 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -6454,16 +6454,25 @@ Instruction *InstCombiner::commonPointerCastTransforms(CastInst &CI) { while (Offset) { if (const StructType *STy = dyn_cast(GEPIdxTy)) { const StructLayout *SL = TD->getStructLayout(STy); - unsigned Elt = SL->getElementContainingOffset(Offset); - NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); + if (Offset < (int64_t)SL->getSizeInBytes()) { + unsigned Elt = SL->getElementContainingOffset(Offset); + NewIndices.push_back(ConstantInt::get(Type::Int32Ty, Elt)); - Offset -= SL->getElementOffset(Elt); - GEPIdxTy = STy->getElementType(Elt); + Offset -= SL->getElementOffset(Elt); + GEPIdxTy = STy->getElementType(Elt); + } else { + // Otherwise, we can't index into this, bail out. + Offset = 0; + OrigBase = 0; + } } else if (isa(GEPIdxTy) || isa(GEPIdxTy)) { const SequentialType *STy = cast(GEPIdxTy); - uint64_t EltSize = TD->getTypeSize(STy->getElementType()); - NewIndices.push_back(ConstantInt::get(IntPtrTy, Offset/EltSize)); - Offset %= EltSize; + if (uint64_t EltSize = TD->getTypeSize(STy->getElementType())) { + NewIndices.push_back(ConstantInt::get(IntPtrTy,Offset/EltSize)); + Offset %= EltSize; + } else { + NewIndices.push_back(ConstantInt::get(IntPtrTy, 0)); + } GEPIdxTy = STy->getElementType(); } else { // Otherwise, we can't index into this, bail out.