Index: lib/CodeGen/GlobalMerge.cpp =================================================================== --- lib/CodeGen/GlobalMerge.cpp +++ lib/CodeGen/GlobalMerge.cpp @@ -432,6 +432,8 @@ std::vector Tys; std::vector Inits; + bool HasExternal = false; + GlobalVariable *TheFirstExternal = nullptr; for (j = i; j != -1; j = GlobalSet.find_next(j)) { Type *Ty = Globals[j]->getValueType(); MergedSize += DL.getTypeAllocSize(Ty); @@ -440,14 +442,37 @@ } 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); - GlobalVariable *MergedGV = new GlobalVariable( - M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit, - "_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace); + // On Darwin external linkage needs to be preserved, otherwise dsymutil + // cannot preserve the debug info for the merged globals. + GlobalVariable *MergedGV; + if (IsMachO) + // If merged variables have external linkage, we use the symbol name of + // the first variable merged as the suffix of global symbol name. This + // avoids a link-time naming conflict for _MergedGlobals symbols. + MergedGV = new GlobalVariable( + M, MergedTy, isConst, Linkage, MergedInit, + HasExternal ? "_MergedGlobals_" + TheFirstExternal->getName() + : "_MergedGlobals", + nullptr, GlobalVariable::NotThreadLocal, AddrSpace); + else + MergedGV = new GlobalVariable( + M, MergedTy, isConst, GlobalValue::PrivateLinkage, MergedInit, + "_MergedGlobals", nullptr, GlobalVariable::NotThreadLocal, AddrSpace); const StructLayout *MergedLayout = DL.getStructLayout(MergedTy); @@ -466,7 +491,7 @@ Constant *GEP = ConstantExpr::getInBoundsGetElementPtr(MergedTy, MergedGV, Idx); Globals[k]->replaceAllUsesWith(GEP); - Globals[k]->eraseFromParent(); + Globals[k]->eraseFromParent(); // When the linkage is not internal we must emit an alias for the original // variable name as it may be accessed from another object. On non-Mach-O Index: test/CodeGen/AArch64/global-merge-1.ll =================================================================== --- test/CodeGen/AArch64/global-merge-1.ll +++ test/CodeGen/AArch64/global-merge-1.ll @@ -12,9 +12,9 @@ define void @f1(i32 %a1, i32 %a2) { ;CHECK-APPLE-IOS-NOT: adrp -;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE +;CHECK-APPLE-IOS: adrp x8, __MergedGlobals@PAGE ;CHECK-APPLE-IOS-NOT: adrp -;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF +;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals@PAGEOFF store i32 %a1, i32* @m, align 4 store i32 %a2, i32* @n, align 4 ret void @@ -26,6 +26,6 @@ ;CHECK: m = .L_MergedGlobals ;CHECK: n = .L_MergedGlobals+4 -;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals,8,3 ; @_MergedGlobals +;CHECK-APPLE-IOS: .zerofill __DATA,__bss,__MergedGlobals,8,3 ; @_MergedGlobals ;CHECK-APPLE-IOS-NOT: _m = l__MergedGlobals ;CHECK-APPLE-IOS-NOT: _n = l__MergedGlobals+4 Index: test/CodeGen/AArch64/global-merge-2.ll =================================================================== --- test/CodeGen/AArch64/global-merge-2.ll +++ test/CodeGen/AArch64/global-merge-2.ll @@ -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, l__MergedGlobals@PAGE -;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF +;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE +;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF ;CHECK-APPLE-IOS-NOT: adrp store i32 %a1, i32* @x, align 4 store i32 %a2, i32* @y, align 4 @@ -19,8 +19,8 @@ define void @g1(i32 %a1, i32 %a2) { ;CHECK-APPLE-IOS-LABEL: _g1: -;CHECK-APPLE-IOS: adrp x8, l__MergedGlobals@PAGE -;CHECK-APPLE-IOS: add x8, x8, l__MergedGlobals@PAGEOFF +;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE +;CHECK-APPLE-IOS: add x8, x8, __MergedGlobals_x@PAGEOFF ;CHECK-APPLE-IOS-NOT: adrp store i32 %a1, i32* @y, align 4 store i32 %a2, i32* @z, align 4 @@ -41,12 +41,12 @@ ;CHECK: z = .L_MergedGlobals+8 ;CHECK: .size z, 4 -;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals,12,3 +;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_x,12,3 ;CHECK-APPLE-IOS: .globl _x -;CHECK-APPLE-IOS: = l__MergedGlobals +;CHECK-APPLE-IOS: = __MergedGlobals_x ;CHECK-APPLE-IOS: .globl _y -;CHECK-APPLE-IOS: _y = l__MergedGlobals+4 +;CHECK-APPLE-IOS: _y = __MergedGlobals_x+4 ;CHECK-APPLE-IOS: .globl _z -;CHECK-APPLE-IOS: _z = l__MergedGlobals+8 +;CHECK-APPLE-IOS: _z = __MergedGlobals_x+8 ;CHECK-APPLE-IOS: .subsections_via_symbols Index: test/CodeGen/AArch64/global-merge-3.ll =================================================================== --- test/CodeGen/AArch64/global-merge-3.ll +++ test/CodeGen/AArch64/global-merge-3.ll @@ -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, l__MergedGlobals@PAGE +;CHECK-APPLE-IOS: adrp x8, __MergedGlobals_x@PAGE ;CHECK-APPLE-IOS-NOT: adrp -;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 +;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 %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 @@ -30,11 +30,11 @@ ;CHECK: .comm .L_MergedGlobals.1,4000,16 ;CHECK-APPLE-IOS: .p2align 4 -;CHECK-APPLE-IOS: l__MergedGlobals: +;CHECK-APPLE-IOS: __MergedGlobals_x: ;CHECK-APPLE-IOS: .long 1 ;CHECK-APPLE-IOS: .space 4000 -;CHECK-APPLE-IOS: .zerofill __DATA,__bss,l__MergedGlobals.1,4000,4 +;CHECK-APPLE-IOS: .zerofill __DATA,__common,__MergedGlobals_y,4000,4 ;CHECK: z = .L_MergedGlobals ;CHECK: .globl x @@ -44,8 +44,8 @@ ;CHECK: y = .L_MergedGlobals.1 ;CHECK: .size y, 4000 -;CHECK-APPLE-IOS-NOT: _z = l__MergedGlobals +;CHECK-APPLE-IOS-NOT: _z = __MergedGlobals_x ;CHECK-APPLE-IOS:.globl _x -;CHECK-APPLE-IOS: _x = l__MergedGlobals+4 +;CHECK-APPLE-IOS: _x = __MergedGlobals_x+4 ;CHECK-APPLE-IOS:.globl _y -;CHECK-APPLE-IOS: _y = l__MergedGlobals.1 +;CHECK-APPLE-IOS: _y = __MergedGlobals_y Index: test/CodeGen/AArch64/global-merge-group-by-use.ll =================================================================== --- test/CodeGen/AArch64/global-merge-group-by-use.ll +++ test/CodeGen/AArch64/global-merge-group-by-use.ll @@ -13,7 +13,7 @@ ; CHECK-LABEL: f1: define void @f1(i32 %a1, i32 %a2) #0 { -; CHECK-NEXT: adrp x8, [[SET1:l__MergedGlobals.[0-9]*]]@PAGE +; CHECK-NEXT: adrp x8, [[SET1:__MergedGlobals.[0-9]*]]@PAGE ; CHECK-NEXT: add x8, x8, [[SET1]]@PAGEOFF ; CHECK-NEXT: stp w0, w1, [x8] ; CHECK-NEXT: ret @@ -28,7 +28,7 @@ ; CHECK-LABEL: f2: define void @f2(i32 %a1, i32 %a2, i32 %a3) #0 { -; CHECK-NEXT: adrp x8, [[SET2:l__MergedGlobals.[0-9]*]]@PAGE +; CHECK-NEXT: adrp x8, [[SET2:__MergedGlobals.[0-9]*]]@PAGE ; CHECK-NEXT: add x8, x8, [[SET2]]@PAGEOFF ; CHECK-NEXT: stp w0, w1, [x8] ; CHECK-NEXT: str w2, [x8, #8] @@ -49,7 +49,7 @@ ; CHECK-LABEL: f3: define void @f3(i32 %a1, i32 %a2) #0 { ; CHECK-NEXT: adrp x8, _m3@PAGE -; CHECK-NEXT: adrp x9, [[SET3:l__MergedGlobals[0-9]*]]@PAGE +; CHECK-NEXT: adrp x9, [[SET3:__MergedGlobals[0-9]*]]@PAGE ; CHECK-NEXT: str w0, [x8, _m3@PAGEOFF] ; CHECK-NEXT: str w1, [x9, [[SET3]]@PAGEOFF] ; CHECK-NEXT: ret Index: test/CodeGen/AArch64/global-merge-ignore-single-use-minsize.ll =================================================================== --- test/CodeGen/AArch64/global-merge-ignore-single-use-minsize.ll +++ test/CodeGen/AArch64/global-merge-ignore-single-use-minsize.ll @@ -11,7 +11,7 @@ ; CHECK-LABEL: f1: define void @f1(i32 %a1, i32 %a2) minsize nounwind { -; CHECK-NEXT: adrp x8, [[SET:l__MergedGlobals]]@PAGE +; CHECK-NEXT: adrp x8, [[SET:__MergedGlobals]]@PAGE ; CHECK-NEXT: add x8, x8, [[SET]]@PAGEOFF ; CHECK-NEXT: stp w0, w1, [x8] ; CHECK-NEXT: ret Index: test/CodeGen/AArch64/global-merge-ignore-single-use.ll =================================================================== --- test/CodeGen/AArch64/global-merge-ignore-single-use.ll +++ test/CodeGen/AArch64/global-merge-ignore-single-use.ll @@ -11,7 +11,7 @@ ; CHECK-LABEL: f1: define void @f1(i32 %a1, i32 %a2) #0 { -; CHECK-NEXT: adrp x8, [[SET:l__MergedGlobals]]@PAGE +; CHECK-NEXT: adrp x8, [[SET:__MergedGlobals]]@PAGE ; CHECK-NEXT: add x8, x8, [[SET]]@PAGEOFF ; CHECK-NEXT: stp w0, w1, [x8] ; CHECK-NEXT: ret Index: test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll =================================================================== --- test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll +++ test/CodeGen/ARM/2011-06-29-MergeGlobalsAlign.ll @@ -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,l__MergedGlobals,16,2 +; CHECK: .zerofill __DATA,__bss,__MergedGlobals,16,2 @prev = external global [0 x i16] @max_lazy_match = internal unnamed_addr global i32 0, align 4 Index: test/CodeGen/ARM/global-merge-1.ll =================================================================== --- test/CodeGen/ARM/global-merge-1.ll +++ test/CodeGen/ARM/global-merge-1.ll @@ -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,l__MergedGlobals,60,4 +; MERGE: .zerofill __DATA,__bss,__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,l__MergedGlobals,60,4 +; NO-MERGE-NOT: .zerofill __DATA,__bss,__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,l__MergedGlobals,60,4 +; NO-MERGE-NOT: .zerofill __DATA,__bss,__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"