diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 42d5e7923f06339f7f5f5cb94ddb12b701d4a175..4d0d40a0a8ecbeaac489b5a2f5a11ff44392ddd9 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -922,7 +922,8 @@ static bool DeclIsConstantGlobal(ASTContext &Context, const VarDecl *D) { llvm::Constant * CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, const llvm::PointerType *Ty, - const VarDecl *D) { + const VarDecl *D, + bool UnnamedAddr) { // Lookup the entry, lazily creating it if necessary. llvm::GlobalValue *Entry = GetGlobalValue(MangledName); if (Entry) { @@ -933,6 +934,9 @@ CodeGenModule::GetOrCreateLLVMGlobal(llvm::StringRef MangledName, WeakRefReferences.erase(Entry); } + if (UnnamedAddr) + Entry->setUnnamedAddr(true); + if (Entry->getType() == Ty) return Entry; @@ -1008,7 +1012,8 @@ llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, llvm::Constant * CodeGenModule::CreateRuntimeVariable(const llvm::Type *Ty, llvm::StringRef Name) { - return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0); + return GetOrCreateLLVMGlobal(Name, llvm::PointerType::getUnqual(Ty), 0, + true); } void CodeGenModule::EmitTentativeDefinition(const VarDecl *D) { diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h index b1395be8c54e367907499d8845bfcb237f05c207..55674ba6a6a9b0b43de0360d225c5af2c4efd096 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -519,7 +519,8 @@ private: GlobalDecl D); llvm::Constant *GetOrCreateLLVMGlobal(llvm::StringRef MangledName, const llvm::PointerType *PTy, - const VarDecl *D); + const VarDecl *D, + bool UnnamedAddr = false); /// SetCommonAttributes - Set attributes which are common to any /// form of a global definition (alias, Objective-C method, diff --git a/test/CodeGenCXX/global-init.cpp b/test/CodeGenCXX/global-init.cpp index f89e9781319f96687f484568f7048fc63b6d6ed7..6003270816814d714ee7ef2d5f15c6a1f54728fe 100644 --- a/test/CodeGenCXX/global-init.cpp +++ b/test/CodeGenCXX/global-init.cpp @@ -12,6 +12,7 @@ struct C { void *field; }; struct D { ~D(); }; +// CHECK: @__dso_handle = external unnamed_addr global i8* // CHECK: @c = global %struct.C zeroinitializer, align 8 // It's okay if we ever implement the IR-generation optimization to remove this. diff --git a/test/CodeGenObjC/constant-string-class.m b/test/CodeGenObjC/constant-string-class.m index 3d182384a2ed7ba58fcb01693807d8ec9898b00b..12253d6c70f35c2d8a57185d17d6e82df802c4ea 100644 --- a/test/CodeGenObjC/constant-string-class.m +++ b/test/CodeGenObjC/constant-string-class.m @@ -32,3 +32,4 @@ int main () { // CHECK-FRAGILE: @_FooClassReference = common global // CHECK-NONFRAGILE: @"OBJC_CLASS_$_Object" = external global +// CHECK-NONFRAGILE: "OBJC_CLASS_$_Foo" = unnamed_addr global