Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -3073,6 +3073,11 @@ } } + // Simplification is for external library calls, so bail out if the callee + // has an implementation. + if (!Callee->empty()) + return nullptr; + // Also try to simplify calls to fortified library functions. if (Value *SimplifiedFortifiedCI = FortifiedSimplifier.optimizeCall(CI, Builder)) { Index: llvm/test/Transforms/InstCombine/bcopy-chk.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/InstCombine/bcopy-chk.ll @@ -0,0 +1,27 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -O1 -S | FileCheck %s + +declare i8* @__memmove_chk(i8*, i8*, i64, i64) +declare i64 @llvm.objectsize.i64.p0i8(i8*, i1, i1, i1) + +define available_externally dso_local void @bcopy(i8* nocapture readonly %src, i8* nocapture %dst, i64 %len) #0 { + %size = call i64 @llvm.objectsize.i64.p0i8(i8* %dst, i1 false, i1 true, i1 false) + call i8* @__memmove_chk(i8* %dst, i8* %src, i64 %len, i64 %size) + ret void +} + +@buf = dso_local global [4 x i8] zeroinitializer, align 1 + +define dso_local void @fortified_bcopy(i64 %argc) { +; CHECK-LABEL: @fortified_bcopy( +; CHECK-NEXT: [[ADD:%.*]] = add i64 [[ARGC:%.*]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = call i8* @__memmove_chk(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @buf, i64 0, i64 2), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @buf, i64 0, i64 1), i64 [[ADD]], i64 2) +; CHECK-NEXT: ret void +; + %add = add i64 %argc, 1 + tail call void @bcopy(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @buf, i64 0, i64 1), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @buf, i64 0, i64 2), i64 %add) #1 + ret void +} + +attributes #0 = { alwaysinline } +attributes #1 = { builtin }