Skip to content

Commit b9ed23b

Browse files
committed
[GlobalMerge] Update the GlobalMerge pass to merge private global variables.
This patch updates the GlobalMerge pass to be able to handle private global variables, which is required for a follow-up PowerPC specific GlobalMerge patch to merge internal and private globals. A new LIT test is also added to exhibit the ability to merge private globals.
1 parent 5ea9d3d commit b9ed23b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

llvm/lib/CodeGen/GlobalMerge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ bool GlobalMergeImpl::run(Module &M) {
664664
continue;
665665

666666
if (!(Opt.MergeExternal && GV.hasExternalLinkage()) &&
667-
!GV.hasInternalLinkage())
667+
!GV.hasInternalLinkage() && !GV.hasPrivateLinkage())
668668
continue;
669669

670670
PointerType *PT = dyn_cast<PointerType>(GV.getType());
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; RUN: opt -global-merge -global-merge-max-offset=100 -S -o - %s | FileCheck %s
2+
; RUN: opt -passes='global-merge<max-offset=100>' -S -o - %s | FileCheck %s
3+
4+
; NOTE: This is a copy of the llvm/test/Transforms/GlobalMerge/used.ll test,
5+
; using `private` global variables instead of `internal`. This is to show that
6+
; that private globals can be merged in the GlobalMerge pass.
7+
8+
target datalayout = "e-p:64:64"
9+
target triple = "x86_64-unknown-linux-gnu"
10+
11+
; CHECK: @_MergedGlobals = private global <{ i32, i32 }> <{ i32 3, i32 3 }>, align 4
12+
13+
@a = private global i32 1
14+
@b = private global i32 2
15+
@c = private global i32 3
16+
@d = private global i32 3
17+
18+
@llvm.used = appending global [1 x ptr] [ptr @a], section "llvm.metadata"
19+
@llvm.compiler.used = appending global [1 x ptr] [ptr @b], section "llvm.metadata"
20+
21+
define void @use_private() {
22+
; CHECK: load i32, ptr @a
23+
%x = load i32, ptr @a
24+
; CHECK: load i32, ptr @b
25+
%y = load i32, ptr @b
26+
; CHECK: load i32, ptr @_MergedGlobals
27+
%z1 = load i32, ptr @c
28+
; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
29+
%z2 = load i32, ptr @d
30+
ret void
31+
}

0 commit comments

Comments
 (0)