Skip to content

Commit ae12efa

Browse files
committed
[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 llvm-svn: 315853
1 parent b2ce9ff commit ae12efa

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

llvm/lib/Transforms/IPO/MergeFunctions.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,16 @@ void MergeFunctions::writeThunk(Function *F, Function *G) {
647647
return;
648648
}
649649

650+
// Don't merge tiny functions using a thunk, since it can just end up
651+
// making the function larger.
652+
if (F->size() == 1) {
653+
if (F->front().size() <= 2) {
654+
DEBUG(dbgs() << "writeThunk: " << F->getName()
655+
<< " is too small to bother creating a thunk for\n");
656+
return;
657+
}
658+
}
659+
650660
BasicBlock *GEntryBlock = nullptr;
651661
std::vector<Instruction *> PDIUnrelatedWL;
652662
BasicBlock *BB = nullptr;
@@ -779,18 +789,6 @@ bool MergeFunctions::insert(Function *NewFunction) {
779789

780790
const FunctionNode &OldF = *Result.first;
781791

782-
// Don't merge tiny functions, since it can just end up making the function
783-
// larger.
784-
// FIXME: Should still merge them if they are unnamed_addr and produce an
785-
// alias.
786-
if (NewFunction->size() == 1) {
787-
if (NewFunction->front().size() <= 2) {
788-
DEBUG(dbgs() << NewFunction->getName()
789-
<< " is to small to bother merging\n");
790-
return false;
791-
}
792-
}
793-
794792
// Impose a total order (by name) on the replacement of functions. This is
795793
// important when operating on more than one module independently to prevent
796794
// cycles of thunks calling each other when the modules are linked together.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; RUN: opt -S -mergefunc < %s | FileCheck %s
2+
3+
; CHECK-NOT: @b
4+
5+
@x = constant { void ()*, void ()* } { void ()* @a, void ()* @b }
6+
; CHECK: { void ()* @a, void ()* @a }
7+
8+
define internal void @a() unnamed_addr {
9+
ret void
10+
}
11+
12+
define internal void @b() unnamed_addr {
13+
ret void
14+
}

0 commit comments

Comments
 (0)