Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2034,6 +2034,7 @@ } } + // Try to infer inbounds on GEPs of allocas. if (!GEP.isInBounds()) { unsigned IdxWidth = DL.getIndexSizeInBits(PtrOp->getType()->getPointerAddressSpace()); @@ -2045,7 +2046,8 @@ if (GEP.accumulateConstantOffset(DL, BasePtrOffset) && BasePtrOffset.isNonNegative()) { APInt AllocSize(IdxWidth, DL.getTypeAllocSize(AI->getAllocatedType())); - if (BasePtrOffset.ule(AllocSize)) { + if (BasePtrOffset.ule(AllocSize) && GEP.getAddressSpace() == + PtrOp->getType()->getPointerAddressSpace()) { return GetElementPtrInst::CreateInBounds( PtrOp, makeArrayRef(Ops).slice(1), GEP.getName()); } Index: test/Transforms/InstCombine/getelementptr.ll =================================================================== --- test/Transforms/InstCombine/getelementptr.ll +++ test/Transforms/InstCombine/getelementptr.ll @@ -942,4 +942,17 @@ ret <2 x i32*> %tmp1 } +; Test that inbounds inference does not crash in the presence of addrspacecasts. +define i8 addrspace(42)* @gep_inbounds_addrspace() { +; CHECK-LABEL: @gep_inbounds_addrspace +; CHECK: addrspacecast +; CHECK: getelementptr +; CHECK: ret + %A = alloca <8 x half>, align 16 + %B = bitcast <8 x half>* %A to i8* + %C = addrspacecast i8* %B to i8 addrspace(42)* + %gep = getelementptr i8, i8 addrspace(42)* %C, i64 12 + ret i8 addrspace(42)* %gep +} + ; CHECK: attributes [[$NUW]] = { nounwind }