[llvm] b18bda6 - ARM: reuse existing libcall global variable if possible.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 14 06:15:02 PDT 2021


Author: Tim Northover
Date: 2021-07-14T14:14:47+01:00
New Revision: b18bda67915c67285c2b8d5fb88210f9a7436b8c

URL: https://github.com/llvm/llvm-project/commit/b18bda67915c67285c2b8d5fb88210f9a7436b8c
DIFF: https://github.com/llvm/llvm-project/commit/b18bda67915c67285c2b8d5fb88210f9a7436b8c.diff

LOG: ARM: reuse existing libcall global variable if possible.

If we try to create a new GlobalVariable on each iteration, the Module will
detect the name collision and "helpfully" rename later iterations by appending
".1" etc. But "___udivsi3.1" doesn't exist and we definitely don't want to try
to call it.

So instead check whether there's already a global with the right name in the
module and use that if so.

Added: 
    

Modified: 
    llvm/lib/Target/ARM/ARMFastISel.cpp
    llvm/test/CodeGen/ARM/fast-isel-call.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/ARM/ARMFastISel.cpp b/llvm/lib/Target/ARM/ARMFastISel.cpp
index 32c54e8712cf0..28a076edd6dcd 100644
--- a/llvm/lib/Target/ARM/ARMFastISel.cpp
+++ b/llvm/lib/Target/ARM/ARMFastISel.cpp
@@ -2181,10 +2181,11 @@ unsigned ARMFastISel::getLibcallReg(const Twine &Name) {
   EVT LCREVT = TLI.getValueType(DL, GVTy);
   if (!LCREVT.isSimple()) return 0;
 
-  GlobalValue *GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
-                                       GlobalValue::ExternalLinkage, nullptr,
-                                       Name);
-  assert(GV->getType() == GVTy && "We miscomputed the type for the global!");
+  GlobalValue *GV = M.getNamedGlobal(Name.str());
+  if (!GV)
+    GV = new GlobalVariable(M, Type::getInt32Ty(*Context), false,
+                            GlobalValue::ExternalLinkage, nullptr, Name);
+
   return ARMMaterializeGV(GV, LCREVT.getSimpleVT());
 }
 

diff  --git a/llvm/test/CodeGen/ARM/fast-isel-call.ll b/llvm/test/CodeGen/ARM/fast-isel-call.ll
index 3e0d7a4ad3ee1..f2d892d03d2d6 100644
--- a/llvm/test/CodeGen/ARM/fast-isel-call.ll
+++ b/llvm/test/CodeGen/ARM/fast-isel-call.ll
@@ -187,6 +187,33 @@ entry:
         ret i32 %tmp1
 }
 
+; Make sure we reuse the original ___udivsi3 rather than creating a new one
+; called ___udivsi3.1 or whatever.
+define i32 @LibCall2(i32 %a, i32 %b) {
+entry:
+; ARM-LABEL: LibCall2:
+; ARM: bl {{___udivsi3|__aeabi_uidiv}}
+; ARM-LONG-LABEL: LibCall2:
+
+; ARM-LONG-MACHO: {{(movw r2, :lower16:L___udivsi3\$non_lazy_ptr)|(ldr r2, .LCPI)}}
+; ARM-LONG-MACHO: {{(movt r2, :upper16:L___udivsi3\$non_lazy_ptr)?}}
+; ARM-LONG-MACHO: ldr r2, [r2]
+
+; ARM-LONG-ELF: movw r2, :lower16:__aeabi_uidiv
+; ARM-LONG-ELF: movt r2, :upper16:__aeabi_uidiv
+
+; ARM-LONG: blx r2
+; THUMB-LABEL: LibCall2:
+; THUMB: bl {{___udivsi3|__aeabi_uidiv}}
+; THUMB-LONG-LABEL: LibCall2
+; THUMB-LONG: {{(movw r2, :lower16:L___udivsi3\$non_lazy_ptr)|(ldr.n r2, .LCPI)}}
+; THUMB-LONG: {{(movt r2, :upper16:L___udivsi3\$non_lazy_ptr)?}}
+; THUMB-LONG: ldr r2, [r2]
+; THUMB-LONG: blx r2
+        %tmp1 = udiv i32 %a, %b         ; <i32> [#uses=1]
+        ret i32 %tmp1
+}
+
 ; Test fastcc
 
 define fastcc void @fast_callee(float %i) ssp {


        


More information about the llvm-commits mailing list