Index: lib/Transforms/Scalar/MemCpyOptimizer.cpp =================================================================== --- lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -932,6 +932,17 @@ if (MR != MRI_NoModRef) return false; + // We can't create address space casts here because we don't know if they're + // safe for the target. + if (cpySrc->getType()->getPointerAddressSpace() != + cpyDest->getType()->getPointerAddressSpace()) + return false; + for (unsigned i = 0; i < CS.arg_size(); ++i) + if (CS.getArgument(i)->stripPointerCasts() == cpySrc && + cpySrc->getType()->getPointerAddressSpace() != + CS.getArgument(i)->getType()->getPointerAddressSpace()) + return false; + // All the checks have passed, so do the transformation. bool changedArgument = false; for (unsigned i = 0; i < CS.arg_size(); ++i) Index: test/Transforms/MemCpyOpt/memcpy.ll =================================================================== --- test/Transforms/MemCpyOpt/memcpy.ll +++ test/Transforms/MemCpyOpt/memcpy.ll @@ -202,6 +202,20 @@ ret void } +; don't create new addressspacecasts when we don't know they're safe for the target +define void @test11(i8 addrspace(1)* nocapture dereferenceable(80) %P) { + %A = alloca [20 x i32], align 4 + %a = bitcast [20 x i32]* %A to i8* + call void @llvm.memset.p0i8.i64(i8* %a, i8 0, i64 80, i32 4, i1 false) + call void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* %P, i8* %a, i64 80, i32 4, i1 false) + ret void +; CHECK-LABEL: @test11( +; CHECK-NOT: addrspacecast +} + +declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind +declare void @llvm.memcpy.p1i8.p0i8.i64(i8 addrspace(1)* nocapture, i8* nocapture, i64, i32, i1) nounwind + declare void @f1(%struct.big* nocapture sret) declare void @f2(%struct.big*)