diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -1589,6 +1589,12 @@ PointerType *TargetPtrTy = cast(PointerTy); Type *TargetTy = TargetPtrTy->getElementType(); + // As `addrspacecast` is , `Ptr` (the storage pointer) may have different + // address space from the expected `PointerTy` (the pointer to be used). + // Adjust the pointer type based the original storage pointer. + auto AS = cast(Ptr->getType())->getAddressSpace(); + PointerTy = PointerTy->getPointerTo(AS); + do { // First fold any existing GEPs into the offset. while (GEPOperator *GEP = dyn_cast(Ptr)) { diff --git a/llvm/test/Transforms/SROA/addrspacecast.ll b/llvm/test/Transforms/SROA/addrspacecast.ll --- a/llvm/test/Transforms/SROA/addrspacecast.ll +++ b/llvm/test/Transforms/SROA/addrspacecast.ll @@ -299,6 +299,21 @@ ret void } +; CHECK-LABEL: @select_addrspacecast_i8( +; CHECK: [[SEL:%.*]] = select i1 undef, i8 undef, i8 undef +; CHECK-NEXT: ret i8 [[SEL]] +define i8 @select_addrspacecast_i8() { + %a = alloca i8 + %b = alloca i8 + + %a.ptr = addrspacecast i8* %a to i8 addrspace(1)* + %b.ptr = addrspacecast i8* %b to i8 addrspace(1)* + + %ptr = select i1 undef, i8 addrspace(1)* %a.ptr, i8 addrspace(1)* %b.ptr + %ret = load i8, i8 addrspace(1)* %ptr + ret i8 %ret +} + !0 = !{!1, !1, i64 0, i64 1} !1 = !{!2, i64 1, !"type_0"} !2 = !{!"root"}