[llvm] r315853 - [MergeFunctions] Merge small functions if possible without a thunk.

whitequark via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 15 05:29:09 PDT 2017


Author: whitequark
Date: Sun Oct 15 05:29:09 2017
New Revision: 315853

URL: http://llvm.org/viewvc/llvm-project?rev=315853&view=rev
Log:
[MergeFunctions] Merge small functions if possible without a thunk.

This can result in significant code size savings in some cases,
e.g. an interrupt table all filled with the same assembly stub
in a certain Cortex-M BSP results in code blowup by a factor of 2.5.

Differential Revision: https://reviews.llvm.org/D34806

Added:
    llvm/trunk/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll
Modified:
    llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp

Modified: llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp?rev=315853&r1=315852&r2=315853&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/MergeFunctions.cpp Sun Oct 15 05:29:09 2017
@@ -647,6 +647,16 @@ void MergeFunctions::writeThunk(Function
     return;
   }
 
+  // Don't merge tiny functions using a thunk, since it can just end up
+  // making the function larger.
+  if (F->size() == 1) {
+    if (F->front().size() <= 2) {
+      DEBUG(dbgs() << "writeThunk: " << F->getName()
+                   << " is too small to bother creating a thunk for\n");
+      return;
+    }
+  }
+
   BasicBlock *GEntryBlock = nullptr;
   std::vector<Instruction *> PDIUnrelatedWL;
   BasicBlock *BB = nullptr;
@@ -779,18 +789,6 @@ bool MergeFunctions::insert(Function *Ne
 
   const FunctionNode &OldF = *Result.first;
 
-  // Don't merge tiny functions, since it can just end up making the function
-  // larger.
-  // FIXME: Should still merge them if they are unnamed_addr and produce an
-  // alias.
-  if (NewFunction->size() == 1) {
-    if (NewFunction->front().size() <= 2) {
-      DEBUG(dbgs() << NewFunction->getName()
-                   << " is to small to bother merging\n");
-      return false;
-    }
-  }
-
   // Impose a total order (by name) on the replacement of functions. This is
   // important when operating on more than one module independently to prevent
   // cycles of thunks calling each other when the modules are linked together.

Added: llvm/trunk/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll?rev=315853&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll (added)
+++ llvm/trunk/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll Sun Oct 15 05:29:09 2017
@@ -0,0 +1,14 @@
+; RUN: opt -S -mergefunc < %s | FileCheck %s
+
+; CHECK-NOT: @b
+
+ at x = constant { void ()*, void ()* } { void ()* @a, void ()* @b }
+; CHECK: { void ()* @a, void ()* @a }
+
+define internal void @a() unnamed_addr {
+  ret void
+}
+
+define internal void @b() unnamed_addr {
+  ret void
+}




More information about the llvm-commits mailing list