Skip to content

Commit

Permalink
[GlobalMerge] Use private linkage for MergedGlobals variables
Browse files Browse the repository at this point in the history
Other objects can never reference the MergedGlobals symbol so external linkage
is never needed. Using private instead of internal linkage means the object is
more similar to what it looks like when global merging is not enabled, with
the only difference being that the merged variables are addressed indirectly
relative to the start of the section they are in.

Also add aliases for merged variables with internal linkage, as this also makes
the object be more like what it is when they are not merged.

Differential Revision: http://reviews.llvm.org/D11942

llvm-svn: 244615
john-brawn-arm committed Aug 11, 2015
1 parent b10555c commit 863bfdb
Showing 11 changed files with 62 additions and 91 deletions.
30 changes: 5 additions & 25 deletions llvm/lib/CodeGen/GlobalMerge.cpp
Original file line number Diff line number Diff line change
@@ -429,8 +429,6 @@ bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
std::vector<Type*> Tys;
std::vector<Constant*> Inits;

bool HasExternal = false;
GlobalVariable *TheFirstExternal = 0;
for (j = i; j != -1; j = GlobalSet.find_next(j)) {
Type *Ty = Globals[j]->getType()->getElementType();
MergedSize += DL.getTypeAllocSize(Ty);
@@ -439,30 +437,14 @@ bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
}
Tys.push_back(Ty);
Inits.push_back(Globals[j]->getInitializer());

if (Globals[j]->hasExternalLinkage() && !HasExternal) {
HasExternal = true;
TheFirstExternal = Globals[j];
}
}

// If merged variables doesn't have external linkage, we needn't to expose
// the symbol after merging.
GlobalValue::LinkageTypes Linkage = HasExternal
? GlobalValue::ExternalLinkage
: GlobalValue::InternalLinkage;

StructType *MergedTy = StructType::get(M.getContext(), Tys);
Constant *MergedInit = ConstantStruct::get(MergedTy, Inits);

// If merged variables have external linkage, we use symbol name of the
// first variable merged as the suffix of global symbol name. This would
// be able to avoid the link-time naming conflict for globalm symbols.
GlobalVariable *MergedGV = new GlobalVariable(
M, MergedTy, isConst, Linkage, MergedInit,
HasExternal ? "_MergedGlobals_" + TheFirstExternal->getName()
: "_MergedGlobals",
nullptr, GlobalVariable::NotThreadLocal, AddrSpace);
M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit,
"_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace);

for (ssize_t k = i, idx = 0; k != j; k = GlobalSet.find_next(k)) {
GlobalValue::LinkageTypes Linkage = Globals[k]->getLinkage();
@@ -477,11 +459,9 @@ bool GlobalMerge::doMerge(SmallVectorImpl<GlobalVariable *> &Globals,
Globals[k]->replaceAllUsesWith(GEP);
Globals[k]->eraseFromParent();

if (Linkage != GlobalValue::InternalLinkage) {
// Generate a new alias...
auto *PTy = cast<PointerType>(GEP->getType());
GlobalAlias::create(PTy, Linkage, Name, GEP, &M);
}
// Generate a new alias...
auto *PTy = cast<PointerType>(GEP->getType());
GlobalAlias::create(PTy, Linkage, Name, GEP, &M);

NumMerged++;
}
12 changes: 6 additions & 6 deletions llvm/test/CodeGen/AArch64/global-merge-1.ll
Original file line number Diff line number Diff line change
@@ -12,16 +12,16 @@

define void @f1(i32 %a1, i32 %a2) {
;CHECK-APPLE-IOS-NOT: adrp
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals@PAGE
;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE
;CHECK-APPLE-IOS-NOT: adrp
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals@PAGEOFF
;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF
store i32 %a1, i32* @m, align 4
store i32 %a2, i32* @n, align 4
ret void
}

;CHECK: .type _MergedGlobals,@object // @_MergedGlobals
;CHECK: .local _MergedGlobals
;CHECK: .comm _MergedGlobals,8,8
;CHECK: .type .L_MergedGlobals,@object // @_MergedGlobals
;CHECK: .local .L_MergedGlobals
;CHECK: .comm .L_MergedGlobals,8,8

;CHECK-APPLE-IOS: .zerofill __DATA,__bss,__MergedGlobals,8,3 ; @_MergedGlobals
;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals,8,3 ; @_MergedGlobals
31 changes: 14 additions & 17 deletions llvm/test/CodeGen/AArch64/global-merge-2.ll
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@
define void @f1(i32 %a1, i32 %a2) {
;CHECK-APPLE-IOS-LABEL: _f1:
;CHECK-APPLE-IOS-NOT: adrp
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF
;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE
;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF
;CHECK-APPLE-IOS-NOT: adrp
store i32 %a1, i32* @x, align 4
store i32 %a2, i32* @y, align 4
@@ -19,34 +19,31 @@ define void @f1(i32 %a1, i32 %a2) {

define void @g1(i32 %a1, i32 %a2) {
;CHECK-APPLE-IOS-LABEL: _g1:
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF
;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE
;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF
;CHECK-APPLE-IOS-NOT: adrp
store i32 %a1, i32* @y, align 4
store i32 %a2, i32* @z, align 4
ret void
}

;CHECK: .type _MergedGlobals_x,@object // @_MergedGlobals_x
;CHECK: .globl _MergedGlobals_x
;CHECK: .align 3
;CHECK: _MergedGlobals_x:
;CHECK: .size _MergedGlobals_x, 12
;CHECK: .type .L_MergedGlobals,@object // @_MergedGlobals
;CHECK: .local .L_MergedGlobals
;CHECK: .comm .L_MergedGlobals,12,8

;CHECK: .globl x
;CHECK: x = _MergedGlobals_x
;CHECK: x = .L_MergedGlobals
;CHECK: .globl y
;CHECK: y = _MergedGlobals_x+4
;CHECK: y = .L_MergedGlobals+4
;CHECK: .globl z
;CHECK: z = _MergedGlobals_x+8
;CHECK: z = .L_MergedGlobals+8

;CHECK-APPLE-IOS: .globl __MergedGlobals_x ; @_MergedGlobals_x
;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_x,12,3
;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals,12,3

;CHECK-APPLE-IOS: .globl _x
;CHECK-APPLE-IOS: _x = __MergedGlobals_x
;CHECK-APPLE-IOS: = l__MergedGlobals
;CHECK-APPLE-IOS: .globl _y
;CHECK-APPLE-IOS: _y = __MergedGlobals_x+4
;CHECK-APPLE-IOS: _y = l__MergedGlobals+4
;CHECK-APPLE-IOS: .globl _z
;CHECK-APPLE-IOS: _z = __MergedGlobals_x+8
;CHECK-APPLE-IOS: _z = l__MergedGlobals+8
;CHECK-APPLE-IOS: .subsections_via_symbols
36 changes: 16 additions & 20 deletions llvm/test/CodeGen/AArch64/global-merge-3.ll
Original file line number Diff line number Diff line change
@@ -7,11 +7,11 @@
@z = internal global i32 1, align 4

define void @f1(i32 %a1, i32 %a2, i32 %a3) {
;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE
;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE
;CHECK-APPLE-IOS-NOT: adrp
;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF
;CHECK-APPLE-IOS: adrp x9, __MergedGlobals_y@PAGE
;CHECK-APPLE-IOS: add x9, x9, __MergedGlobals_y@PAGEOFF
;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF
;CHECK-APPLE-IOS: adrp x9, l__MergedGlobals.1@PAGE
;CHECK-APPLE-IOS: add x9, x9, l__MergedGlobals.1@PAGEOFF
%x3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @x, i32 0, i64 3
%y3 = getelementptr inbounds [1000 x i32], [1000 x i32]* @y, i32 0, i64 3
store i32 %a1, i32* %x3, align 4
@@ -20,32 +20,28 @@ define void @f1(i32 %a1, i32 %a2, i32 %a3) {
ret void
}

;CHECK: .type _MergedGlobals_x,@object // @_MergedGlobals_x
;CHECK: .globl _MergedGlobals_x
;CHECK: .type .L_MergedGlobals,@object // @_MergedGlobals
;CHECK: .align 4
;CHECK: _MergedGlobals_x:
;CHECK: .size _MergedGlobals_x, 4004
;CHECK: .L_MergedGlobals:
;CHECK: .size .L_MergedGlobals, 4004

;CHECK: .type _MergedGlobals_y,@object // @_MergedGlobals_y
;CHECK: .globl _MergedGlobals_y
;CHECK: _MergedGlobals_y:
;CHECK: .size _MergedGlobals_y, 4000
;CHECK: .type .L_MergedGlobals.1,@object // @_MergedGlobals.1
;CHECK: .local .L_MergedGlobals.1
;CHECK: .comm .L_MergedGlobals.1,4000,16

;CHECK-APPLE-IOS: .globl __MergedGlobals_x ; @_MergedGlobals_x
;CHECK-APPLE-IOS: .align 4
;CHECK-APPLE-IOS: __MergedGlobals_x:
;CHECK-APPLE-IOS: l__MergedGlobals:
;CHECK-APPLE-IOS: .long 1
;CHECK-APPLE-IOS: .space 4000

;CHECK-APPLE-IOS: .globl __MergedGlobals_y ; @_MergedGlobals_y
;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_y,4000,4
;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals.1,4000,4

;CHECK: .globl x
;CHECK: x = _MergedGlobals_x+4
;CHECK: x = .L_MergedGlobals+4
;CHECK: .globl y
;CHECK: y = _MergedGlobals_y
;CHECK: y = .L_MergedGlobals.1

;CHECK-APPLE-IOS:.globl _x
;CHECK-APPLE-IOS: _x = __MergedGlobals_x+4
;CHECK-APPLE-IOS: _x = l__MergedGlobals+4
;CHECK-APPLE-IOS:.globl _y
;CHECK-APPLE-IOS: _y = __MergedGlobals_y
;CHECK-APPLE-IOS: _y = l__MergedGlobals.1
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/AArch64/global-merge-4.ll
Original file line number Diff line number Diff line change
@@ -64,9 +64,9 @@ define internal i32* @returnFoo() #1 {
ret i32* getelementptr inbounds ([5 x i32], [5 x i32]* @foo, i64 0, i64 0)
}

;CHECK: .type _MergedGlobals,@object // @_MergedGlobals
;CHECK: .local _MergedGlobals
;CHECK: .comm _MergedGlobals,60,16
;CHECK: .type .L_MergedGlobals,@object // @_MergedGlobals
;CHECK: .local .L_MergedGlobals
;CHECK: .comm .L_MergedGlobals,60,16

attributes #0 = { nounwind ssp }
attributes #1 = { nounwind readnone ssp }
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/AArch64/global-merge-group-by-use.ll
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@

; CHECK-LABEL: f1:
define void @f1(i32 %a1, i32 %a2) #0 {
; CHECK-NEXT: adrp x8, [[SET1:__MergedGlobals.[0-9]*]]@PAGE
; CHECK-NEXT: adrp x8, [[SET1:l__MergedGlobals.[0-9]*]]@PAGE
; CHECK-NEXT: add x8, x8, [[SET1]]@PAGEOFF
; CHECK-NEXT: stp w0, w1, [x8]
; CHECK-NEXT: ret
@@ -27,7 +27,7 @@ define void @f1(i32 %a1, i32 %a2) #0 {

; CHECK-LABEL: f2:
define void @f2(i32 %a1, i32 %a2, i32 %a3) #0 {
; CHECK-NEXT: adrp x8, [[SET2:__MergedGlobals.[0-9]*]]@PAGE
; CHECK-NEXT: adrp x8, [[SET2:l__MergedGlobals.[0-9]*]]@PAGE
; CHECK-NEXT: add x8, x8, [[SET2]]@PAGEOFF
; CHECK-NEXT: stp w0, w1, [x8]
; CHECK-NEXT: str w2, [x8, #8]
@@ -48,7 +48,7 @@ define void @f2(i32 %a1, i32 %a2, i32 %a3) #0 {
; CHECK-LABEL: f3:
define void @f3(i32 %a1, i32 %a2) #0 {
; CHECK-NEXT: adrp x8, _m3@PAGE
; CHECK-NEXT: adrp x9, [[SET3:__MergedGlobals[0-9]*]]@PAGE
; CHECK-NEXT: adrp x9, [[SET3:l__MergedGlobals[0-9]*]]@PAGE
; CHECK-NEXT: str w0, [x8, _m3@PAGEOFF]
; CHECK-NEXT: str w1, [x9, [[SET3]]@PAGEOFF]
; CHECK-NEXT: ret
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

; CHECK-LABEL: f1:
define void @f1(i32 %a1, i32 %a2) minsize nounwind {
; CHECK-NEXT: adrp x8, [[SET:__MergedGlobals]]@PAGE
; CHECK-NEXT: adrp x8, [[SET:l__MergedGlobals]]@PAGE
; CHECK-NEXT: add x8, x8, [[SET]]@PAGEOFF
; CHECK-NEXT: stp w0, w1, [x8]
; CHECK-NEXT: ret
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@

; CHECK-LABEL: f1:
define void @f1(i32 %a1, i32 %a2) #0 {
; CHECK-NEXT: adrp x8, [[SET:__MergedGlobals]]@PAGE
; CHECK-NEXT: adrp x8, [[SET:l__MergedGlobals]]@PAGE
; CHECK-NEXT: add x8, x8, [[SET]]@PAGEOFF
; CHECK-NEXT: stp w0, w1, [x8]
; CHECK-NEXT: ret
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; RUN: llc < %s -mtriple=thumbv7-apple-darwin10 -arm-global-merge -global-merge-group-by-use=false | FileCheck %s
; CHECK: .zerofill __DATA,__bss,__MergedGlobals,16,2
; CHECK: .zerofill __DATA,__bss,l__MergedGlobals,16,2

@prev = external global [0 x i16]
@max_lazy_match = internal unnamed_addr global i32 0, align 4
6 changes: 3 additions & 3 deletions llvm/test/CodeGen/ARM/global-merge-1.ll
Original file line number Diff line number Diff line change
@@ -11,16 +11,16 @@
; MERGE-NOT: .zerofill __DATA,__bss,_bar,20,2
; MERGE-NOT: .zerofill __DATA,__bss,_baz,20,2
; MERGE-NOT: .zerofill __DATA,__bss,_foo,20,2
; MERGE: .zerofill __DATA,__bss,__MergedGlobals,60,4
; MERGE: .zerofill __DATA,__bss,l__MergedGlobals,60,4
; MERGE-NOT: .zerofill __DATA,__bss,_bar,20,2
; MERGE-NOT: .zerofill __DATA,__bss,_baz,20,2
; MERGE-NOT: .zerofill __DATA,__bss,_foo,20,2

; NO-MERGE-NOT: .zerofill __DATA,__bss,__MergedGlobals,60,4
; NO-MERGE-NOT: .zerofill __DATA,__bss,l__MergedGlobals,60,4
; NO-MERGE: .zerofill __DATA,__bss,_bar,20,2
; NO-MERGE: .zerofill __DATA,__bss,_baz,20,2
; NO-MERGE: .zerofill __DATA,__bss,_foo,20,2
; NO-MERGE-NOT: .zerofill __DATA,__bss,__MergedGlobals,60,4
; NO-MERGE-NOT: .zerofill __DATA,__bss,l__MergedGlobals,60,4

target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32-S32"
target triple = "thumbv7-apple-ios3.0.0"
20 changes: 9 additions & 11 deletions llvm/test/CodeGen/ARM/global-merge-external.ll
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ define void @f1(i32 %a1, i32 %a2) {
;CHECK: f1:
;CHECK: ldr {{r[0-9]+}}, [[LABEL1:\.LCPI[0-9]+_[0-9]]]
;CHECK: [[LABEL1]]:
;CHECK-MERGE: .long _MergedGlobals_x
;CHECK-MERGE: .long .L_MergedGlobals
;CHECK-NO-MERGE: .long {{_?x}}
store i32 %a1, i32* @x, align 4
store i32 %a2, i32* @y, align 4
@@ -22,24 +22,22 @@ define void @g1(i32 %a1, i32 %a2) {
;CHECK: g1:
;CHECK: ldr {{r[0-9]+}}, [[LABEL2:\.LCPI[0-9]+_[0-9]]]
;CHECK: [[LABEL2]]:
;CHECK-MERGE: .long _MergedGlobals_x
;CHECK-MERGE: .long .L_MergedGlobals
;CHECK-NO-MERGE: .long {{_?y}}
store i32 %a1, i32* @y, align 4
store i32 %a2, i32* @z, align 4
ret void
}

;CHECK-NO-MERGE-NOT: .globl _MergedGlobals_x
;CHECK-NO-MERGE-NOT: .globl .L_MergedGlobals

;CHECK-MERGE: .type _MergedGlobals_x,%object
;CHECK-MERGE: .globl _MergedGlobals_x
;CHECK-MERGE: .align 2
;CHECK-MERGE: _MergedGlobals_x:
;CHECK-MERGE: .size _MergedGlobals_x, 12
;CHECK-MERGE: .type .L_MergedGlobals,%object
;CHECK-MERGE: .local .L_MergedGlobals
;CHECK-MERGE: .comm .L_MergedGlobals,12,4

;CHECK-MERGE: .globl x
;CHECK-MERGE: x = _MergedGlobals_x
;CHECK-MERGE: x = .L_MergedGlobals
;CHECK-MERGE: .globl y
;CHECK-MERGE: y = _MergedGlobals_x+4
;CHECK-MERGE: y = .L_MergedGlobals+4
;CHECK-MERGE: .globl z
;CHECK-MERGE: z = _MergedGlobals_x+8
;CHECK-MERGE: z = .L_MergedGlobals+8

0 comments on commit 863bfdb

Please sign in to comment.