diff --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp --- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp +++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp @@ -195,13 +195,18 @@ for (User *U : llvm::make_early_inc_range(F.users())) { Instruction *Inst = cast(U); + // Even in the presence of -fno-builtin, users may directly call memcpy + // via the builtin. In this case expanding will cause a size increase, + // so just avoid expanding here if the caller is a minsize function. + bool IsMinSize = Inst->getFunction()->hasMinSize(); switch (ID) { case Intrinsic::memcpy: { auto *Memcpy = cast(Inst); Function *ParentFunc = Memcpy->getFunction(); const TargetTransformInfo &TTI = LookupTTI(*ParentFunc); - if (shouldExpandMemIntrinsicWithSize(Memcpy->getLength(), TTI)) { + if (!IsMinSize && + shouldExpandMemIntrinsicWithSize(Memcpy->getLength(), TTI)) { if (UseMemIntrinsicLibFunc && LookupLibInfo(*ParentFunc).has(LibFunc_memcpy)) break; @@ -217,7 +222,8 @@ auto *Memmove = cast(Inst); Function *ParentFunc = Memmove->getFunction(); const TargetTransformInfo &TTI = LookupTTI(*ParentFunc); - if (shouldExpandMemIntrinsicWithSize(Memmove->getLength(), TTI)) { + if (!IsMinSize && + shouldExpandMemIntrinsicWithSize(Memmove->getLength(), TTI)) { if (UseMemIntrinsicLibFunc && LookupLibInfo(*ParentFunc).has(LibFunc_memmove)) break; @@ -234,7 +240,8 @@ auto *Memset = cast(Inst); Function *ParentFunc = Memset->getFunction(); const TargetTransformInfo &TTI = LookupTTI(*ParentFunc); - if (shouldExpandMemIntrinsicWithSize(Memset->getLength(), TTI)) { + if (!IsMinSize && + shouldExpandMemIntrinsicWithSize(Memset->getLength(), TTI)) { if (UseMemIntrinsicLibFunc && LookupLibInfo(*Memset->getFunction()).has(LibFunc_memset)) break; diff --git a/llvm/test/CodeGen/ARM/no-expand-memcpy-minsize-no-builtins.ll b/llvm/test/CodeGen/ARM/no-expand-memcpy-minsize-no-builtins.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/ARM/no-expand-memcpy-minsize-no-builtins.ll @@ -0,0 +1,19 @@ +; RUN: llc < %s | FileCheck %s + +target datalayout = "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "thumbv7em-apple-unknown-macho" + +; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite) +declare void @llvm.memcpy.p0.p0.i32(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg) #0 + +; Check we don't expand memcpy to a loop when the caller is a minsize function, +; even if we have no-builtins attached. +define arm_aapcs_vfpcc void @test(ptr %p1, ptr %p2) #1 { + ; CHECK-LABEL: test: + ; CHECK: bl _memcpy + call void @llvm.memcpy.p0.p0.i32(ptr %p1, ptr %p2, i32 128, i1 false) + ret void +} + +attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) } +attributes #1 = { "no-builtins" minsize }