Skip to content

Commit 4089763

Browse files
authored
[GlobalMerge] Update the GlobalMerge pass to merge private global variables. (#101222)
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 f117f0a commit 4089763

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-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.hasLocalLinkage())
668668
continue;
669669

670670
PointerType *PT = dyn_cast<PointerType>(GV.getType());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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/basic.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 1, i32 2 }>, align 4
12+
; CHECK: @_MergedGlobals.1 = private global <{ i32, i32 }> <{ i32 3, i32 4 }>, section "foo", align 4
13+
14+
; CHECK-DAG: @a = private alias i32, ptr @_MergedGlobals{{$}}
15+
@a = private global i32 1
16+
17+
; CHECK-DAG: @b = private alias i32, getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
18+
@b = private global i32 2
19+
20+
; CHECK-DAG: @c = private alias i32, ptr @_MergedGlobals.1{{$}}
21+
@c = private global i32 3, section "foo"
22+
23+
; CHECK-DAG: @d = private alias i32, getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals.1, i32 0, i32 1)
24+
@d = private global i32 4, section "foo"
25+
26+
define void @use_private() {
27+
; CHECK: load i32, ptr @_MergedGlobals,
28+
%x = load i32, ptr @a
29+
; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals, i32 0, i32 1)
30+
%y = load i32, ptr @b
31+
; CHECK: load i32, ptr @_MergedGlobals.1
32+
%z1 = load i32, ptr @c
33+
; CHECK: load i32, ptr getelementptr inbounds (<{ i32, i32 }>, ptr @_MergedGlobals.1, i32 0, i32 1)
34+
%z2 = load i32, ptr @d
35+
ret void
36+
}

0 commit comments

Comments
 (0)