Index: lib/Transforms/IPO/MergeFunctions.cpp =================================================================== --- lib/Transforms/IPO/MergeFunctions.cpp +++ lib/Transforms/IPO/MergeFunctions.cpp @@ -771,6 +771,19 @@ // If G's address is not significant, replace it entirely. Constant *BitcastF = ConstantExpr::getBitCast(F, G->getType()); G->replaceAllUsesWith(BitcastF); + // If G is local, then we will eliminate it completely below. + // Otherwise, the next best thing is to make it an alias to F. + if (!G->hasLocalLinkage()) { + // Initialize alias with empty name as G is still holding onto its own. + auto Alias = GlobalAlias::create(G->getValueType(), + G->getType()->getAddressSpace(), + G->getLinkage(), + "", BitcastF, G->getParent()); + Alias->takeName(G); + G->eraseFromParent(); + ++NumFunctionsMerged; + return; + } } else { // Redirect direct callers of G to F. (See note on MergeFunctionsPDI // above). Index: test/Transforms/MergeFunc/merge-unnamed-addr-global.ll =================================================================== --- /dev/null +++ test/Transforms/MergeFunc/merge-unnamed-addr-global.ll @@ -0,0 +1,18 @@ +; RUN: opt -S -mergefunc < %s | FileCheck %s + +%A = type { i32 } +%B = type { i32 } + +; CHECK: @b = alias {{.*}} @a {{.*}} + +define i32 @a(%A) unnamed_addr { + extractvalue %A %0, 0 + xor i32 %2, 0 + ret i32 %3 +} + +define i32 @b(%B) unnamed_addr { + extractvalue %B %0, 0 + xor i32 %2, 0 + ret i32 %3 +}