[PATCH] D50884: [MergeFunctions] Merge global unnamed_addr functions as aliases

Dmytro Shynkevych via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 16 23:45:25 PDT 2018


hermord created this revision.
hermord added reviewers: chandlerc, hfinkel.
Herald added a subscriber: llvm-commits.

Even if an unnamed_addr function being merged has external linkage, it should be still alright to make it an alias instead of a thunk, as an optimization.


Repository:
  rL LLVM

https://reviews.llvm.org/D50884

Files:
  lib/Transforms/IPO/MergeFunctions.cpp
  test/Transforms/MergeFunc/merge-unnamed-addr-global.ll


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
+}
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).


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50884.161169.patch
Type: text/x-patch
Size: 1718 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180817/80e07ac9/attachment.bin>


More information about the llvm-commits mailing list