From 99dc03a8d7128708d11433b72b549583c9c02e50 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 15 Oct 2017 12:29:09 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@315853 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/IPO/MergeFunctions.cpp | 22 +++++++++---------- .../MergeFunc/merge-small-unnamed-addr.ll | 14 ++++++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) create mode 100644 test/Transforms/MergeFunc/merge-small-unnamed-addr.ll diff --git a/lib/Transforms/IPO/MergeFunctions.cpp b/lib/Transforms/IPO/MergeFunctions.cpp index 95a05d87348..bffbb8f060d 100644 --- a/lib/Transforms/IPO/MergeFunctions.cpp +++ b/lib/Transforms/IPO/MergeFunctions.cpp @@ -647,6 +647,16 @@ void MergeFunctions::writeThunk(Function *F, Function *G) { 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 PDIUnrelatedWL; BasicBlock *BB = nullptr; @@ -779,18 +789,6 @@ bool MergeFunctions::insert(Function *NewFunction) { 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. diff --git a/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll b/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll new file mode 100644 index 00000000000..256f6864761 --- /dev/null +++ b/test/Transforms/MergeFunc/merge-small-unnamed-addr.ll @@ -0,0 +1,14 @@ +; RUN: opt -S -mergefunc < %s | FileCheck %s + +; CHECK-NOT: @b + +@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 +} -- 2.40.0