Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp =================================================================== --- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp +++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp @@ -491,6 +491,11 @@ for (auto I = --SI->getIterator(), E = P->getIterator(); I != E; --I) { auto *C = &*I; + // Make sure hoisting does not perform a store that was not guaranteed to + // happen. + if (!isGuaranteedToTransferExecutionToSuccessor(C)) + return false; + bool MayAlias = isModOrRefSet(AA->getModRefInfo(C, None)); bool NeedLift = false; Index: llvm/test/Transforms/MemCpyOpt/fca2memcpy.ll =================================================================== --- llvm/test/Transforms/MemCpyOpt/fca2memcpy.ll +++ llvm/test/Transforms/MemCpyOpt/fca2memcpy.ll @@ -147,12 +147,11 @@ define void @throwing_call(%S* noalias %src, %S* %dst) { ; CHECK-LABEL: @throwing_call( -; CHECK-NEXT: [[TMP1:%.*]] = bitcast %S* [[SRC:%.*]] to i8* -; CHECK-NEXT: [[TMP2:%.*]] = bitcast %S* [[DST:%.*]] to i8* -; CHECK-NEXT: [[TMP3:%.*]] = bitcast %S* [[SRC]] to i8* -; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 [[TMP2]], i8* align 8 [[TMP3]], i64 16, i1 false) -; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP1]], i8 0, i64 16, i1 false) +; CHECK-NEXT: [[TMP1:%.*]] = load [[S:%.*]], %S* [[SRC:%.*]], align 8 +; CHECK-NEXT: [[TMP2:%.*]] = bitcast %S* [[SRC]] to i8* +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 8 [[TMP2]], i8 0, i64 16, i1 false) ; CHECK-NEXT: call void @call() [[ATTR2:#.*]] +; CHECK-NEXT: store [[S]] [[TMP1]], %S* [[DST:%.*]], align 8 ; CHECK-NEXT: ret void ; %1 = load %S, %S* %src