[GlobalMerge] Use private linkage for MergedGlobals variables
authorJohn Brawn <john.brawn@arm.com>
Tue, 11 Aug 2015 15:48:04 +0000 (15:48 +0000)
committerJohn Brawn <john.brawn@arm.com>
Tue, 11 Aug 2015 15:48:04 +0000 (15:48 +0000)
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244615 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/GlobalMerge.cpp
test/CodeGen/AArch64/global-merge-1.ll
test/CodeGen/AArch64/global-merge-2.ll
test/CodeGen/AArch64/global-merge-3.ll
test/CodeGen/AArch64/global-merge-4.ll
test/CodeGen/AArch64/global-merge-group-by-use.ll
test/CodeGen/AArch64/global-merge-ignore-single-use-minsize.ll
test/CodeGen/AArch64/global-merge-ignore-single-use.ll
test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll
test/CodeGen/ARM/global-merge-1.ll
test/CodeGen/ARM/global-merge-external.ll

index d477143d62db6afae5ff62a9e2b4df7bb1a86d62..3a7e2f4768689fb8eefe3547e09e1a77122de94a 100644 (file)
@@ -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++;
     }
index 14b04303ffb38ed72f5dcda8522a08bf0e67f420..304d0a78c2c98d959a33119afa5b113f97570245 100644 (file)
 
 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
index af684039bf10fab3f1156361d5d47e340dc5ca94..f168bea9e1585192cea7aa1110c97df6ac52dda8 100644 (file)
@@ -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
index 925108308e56332631afbae7ac8bfddba260b4fe..a6a8a7e8a04e02b1b8764bfaaade248bf60ba095 100644 (file)
@@ -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
index bc6b68a9c046a1da0f7d35f1a02810b8dc6d8434..a5109f6e8ea5965900ffd9f88d1049aea8eb4985 100644 (file)
@@ -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 }
index ddc044ed9e082f9d1613d8989694ea112ea43027..8b3fc97c9e2e3539d3e31577b9820e9affd835fa 100644 (file)
@@ -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
index e83cbab140a74b20d60fd3fd1c0de4ba707df664..3994389257719f273d8ba79d3b1e9df5ec37c7eb 100644 (file)
@@ -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
index e6de4699132ae6dfcbae318d3b190e720309ee5f..c3756a85feff5b12241c0a745599eb2f3392ea81 100644 (file)
@@ -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
index aac8f7b3a026b9e22ae5c30929c52ca97be759ff..1097050df54b3516f67d5c973723641772df2464 100644 (file)
@@ -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
index d4d9b0f9d1f3e90f333f7fab9d8b3044b9c28da3..a3cbe8aec0984f51fb4fbfe45737f68770d5f30d 100644 (file)
 ; 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"
index 0dd3f477e9b39d0b3dba3f96043fbe7d051b9913..f379e654f0473a22900208d68c6b30451f186815 100644 (file)
@@ -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