[PATCH] D155790: PreISelIntrinsicLowering: don't expand memcpys in minsize functions, even with no-builtins.

Amara Emerson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 20 00:27:53 PDT 2023


aemerson created this revision.
aemerson added a reviewer: arsenm.
aemerson added a project: LLVM.
Herald added a subscriber: hiraditya.
Herald added a project: All.
aemerson requested review of this revision.
Herald added a subscriber: wdng.

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 if the caller is a minsize function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155790

Files:
  llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
  llvm/test/CodeGen/ARM/no-expand-memcpy-minsize-no-builtins.ll


Index: llvm/test/CodeGen/ARM/no-expand-memcpy-minsize-no-builtins.ll
===================================================================
--- /dev/null
+++ 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 }
Index: llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
===================================================================
--- llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -195,13 +195,18 @@
 
   for (User *U : llvm::make_early_inc_range(F.users())) {
     Instruction *Inst = cast<Instruction>(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<MemCpyInst>(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<MemMoveInst>(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<MemSetInst>(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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155790.542335.patch
Type: text/x-patch
Size: 3145 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230720/2cee3080/attachment.bin>


More information about the llvm-commits mailing list