<p dir="ltr">LGTM. </p>
<br><div class="gmail_quote">On Thu, Jan 29, 2015, 4:42 PM Akira Hatanaka <<a href="mailto:ahatanak@gmail.com">ahatanak@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Add comment that explains what accumulateAndSortLibcalls does.<br>
<br>
<br>
<a href="http://reviews.llvm.org/D7275" target="_blank">http://reviews.llvm.org/D7275</a><br>
<br>
Files:<br>
  lib/LTO/LTOCodeGenerator.cpp<br>
  test/LTO/runtime-library-<u></u>subtarget.ll<br>
  tools/llvm-lto/llvm-lto.cpp<br>
<br>
Index: lib/LTO/LTOCodeGenerator.cpp<br>
==============================<u></u>==============================<u></u>=======<br>
--- lib/LTO/LTOCodeGenerator.cpp<br>
+++ lib/LTO/LTOCodeGenerator.cpp<br>
@@ -368,10 +368,13 @@<br>
       UsedValues.insert(GV);<br>
 }<br>
<br>
+// Collect names of runtime library functions. User-defined functions with the<br>
+// same names are added to llvm.compiler.used to prevent them from being<br>
+// deleted by optimizations.<br>
 static void accumulateAndSortLibcalls(std:<u></u>:vector<StringRef> &Libcalls,<br>
                                       const TargetLibraryInfo& TLI,<br>
-                                      const TargetLowering *Lowering)<br>
-{<br>
+                                      const Module &Mod,<br>
+                                      const TargetMachine &TM) {<br>
   // TargetLibraryInfo has info on C runtime library calls on the current<br>
   // target.<br>
   for (unsigned I = 0, E = static_cast<unsigned>(LibFunc:<u></u>:NumLibFuncs);<br>
@@ -381,14 +384,21 @@<br>
       Libcalls.push_back(TLI.<u></u>getName(F));<br>
   }<br>
<br>
-  // TargetLowering has info on library calls that CodeGen expects to be<br>
-  // available, both from the C runtime and compiler-rt.<br>
-  if (Lowering)<br>
-    for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::<u></u>UNKNOWN_LIBCALL);<br>
-         I != E; ++I)<br>
-      if (const char *Name<br>
-          = Lowering->getLibcallName(<u></u>static_cast<RTLIB::Libcall>(I)<u></u>))<br>
-        Libcalls.push_back(Name);<br>
+  SmallPtrSet<const TargetLowering *, 1> TLSet;<br>
+<br>
+  for (const Function &F : Mod) {<br>
+    const TargetLowering *Lowering =<br>
+        TM.getSubtargetImpl(F)-><u></u>getTargetLowering();<br>
+<br>
+    if (Lowering && TLSet.insert(Lowering).second)<br>
+      // TargetLowering has info on library calls that CodeGen expects to be<br>
+      // available, both from the C runtime and compiler-rt.<br>
+      for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::<u></u>UNKNOWN_LIBCALL);<br>
+           I != E; ++I)<br>
+        if (const char *Name =<br>
+                Lowering->getLibcallName(<u></u>static_cast<RTLIB::Libcall>(I)<u></u>))<br>
+          Libcalls.push_back(Name);<br>
+  }<br>
<br>
   array_pod_sort(Libcalls.begin(<u></u>), Libcalls.end());<br>
   Libcalls.erase(std::unique(<u></u>Libcalls.begin(), Libcalls.end()),<br>
@@ -412,8 +422,8 @@<br>
   std::vector<StringRef> Libcalls;<br>
   TargetLibraryInfoImpl TLII(Triple(TargetMach-><u></u>getTargetTriple()));<br>
   TargetLibraryInfo TLI(TLII);<br>
-  accumulateAndSortLibcalls(<br>
-      Libcalls, TLI, TargetMach->getSubtargetImpl()<u></u>->getTargetLowering());<br>
+<br>
+  accumulateAndSortLibcalls(<u></u>Libcalls, TLI, *mergedModule, *TargetMach);<br>
<br>
   for (Module::iterator f = mergedModule->begin(),<br>
          e = mergedModule->end(); f != e; ++f)<br>
Index: test/LTO/runtime-library-<u></u>subtarget.ll<br>
==============================<u></u>==============================<u></u>=======<br>
--- /dev/null<br>
+++ test/LTO/runtime-library-<u></u>subtarget.ll<br>
@@ -0,0 +1,18 @@<br>
+; Check that user-defined runtime library function __addsf3vfp is not removed<br>
+;<br>
+; RUN: llvm-as <%s >%t1<br>
+; RUN: llvm-lto -o %t2 %t1 -mcpu arm1176jz-s<br>
+; RUN: llvm-nm %t2 | FileCheck %s<br>
+<br>
+target datalayout = "e-m:o-p:32:32-f64:32:64-v64:<u></u>32:64-v128:32:128-a:0:32-n32-<u></u>S32"<br>
+target triple = "thumbv7-apple-ios"<br>
+<br>
+; CHECK: ___addsf3vfp<br>
+<br>
+define float @__addsf3vfp(float %a, float %b) #0 {<br>
+entry:<br>
+  %add = fadd float %a, %b<br>
+  ret float %add<br>
+}<br>
+<br>
+attributes #0 = { "target-cpu"="arm1176jzf-s"}<br>
Index: tools/llvm-lto/llvm-lto.cpp<br>
==============================<u></u>==============================<u></u>=======<br>
--- tools/llvm-lto/llvm-lto.cpp<br>
+++ tools/llvm-lto/llvm-lto.cpp<br>
@@ -217,6 +217,9 @@<br>
   for (unsigned i = 0; i < KeptDSOSyms.size(); ++i)<br>
     CodeGen.addMustPreserveSymbol(<u></u>KeptDSOSyms[i].c_str());<br>
<br>
+  // Set cpu and attrs strings for the default target/subtarget.<br>
+  CodeGen.setCpu(MCPU.c_str());<br>
+<br>
   std::string attrs;<br>
   for (unsigned i = 0; i < MAttrs.size(); ++i) {<br>
     if (i > 0)<br>
<br>
EMAIL PREFERENCES<br>
  <a href="http://reviews.llvm.org/settings/panel/emailpreferences/" target="_blank">http://reviews.llvm.org/<u></u>settings/panel/<u></u>emailpreferences/</a><br>
</blockquote></div>