From 3afa3e1d918058af30c2dae3902d2852a4198d71 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 16 Jul 2009 05:03:48 +0000 Subject: [PATCH] codegen string literals using private linkage now like llvm-gcc, eliminating some target hooks. llvm-svn: 75895 --- clang/include/clang/Basic/TargetInfo.h | 12 -------- clang/lib/Basic/Targets.cpp | 8 ------ clang/lib/CodeGen/CodeGenModule.cpp | 32 +++++++++++---------- clang/test/CodeGen/darwin-string-literals.c | 9 +++--- clang/test/CodeGen/string-init.c | 2 +- 5 files changed, 22 insertions(+), 41 deletions(-) diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index b1f9821d3121..8aa3378fec66 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -300,18 +300,6 @@ public: virtual bool useGlobalsForAutomaticVariables() const { return false; } - /// getStringSymbolPrefix - Get the default symbol prefix to - /// use for string literals. - virtual const char *getStringSymbolPrefix(bool IsConstant) const { - return ".str"; - } - - /// getCFStringSymbolPrefix - Get the default symbol prefix - /// to use for CFString literals. - virtual const char *getCFStringSymbolPrefix() const { - return ""; - } - /// getUnicodeStringSymbolPrefix - Get the default symbol prefix to /// use for string literals. virtual const char *getUnicodeStringSymbolPrefix() const { diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp index 65c5264eb114..112e21159c54 100644 --- a/clang/lib/Basic/Targets.cpp +++ b/clang/lib/Basic/Targets.cpp @@ -238,14 +238,6 @@ public: this->TLSSupported = false; } - virtual const char *getCFStringSymbolPrefix() const { - return "\01L_unnamed_cfstring_"; - } - - virtual const char *getStringSymbolPrefix(bool IsConstant) const { - return IsConstant ? "\01LC" : "\01lC"; - } - virtual const char *getUnicodeStringSymbolPrefix() const { return "__utf16_string_"; } diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 2341a14398e1..8e65ef09feae 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -494,16 +494,15 @@ llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV, // Get the two global values corresponding to the ConstantArrays we just // created to hold the bytes of the strings. - const char *StringPrefix = getContext().Target.getStringSymbolPrefix(true); llvm::GlobalValue *annoGV = - new llvm::GlobalVariable(*M, anno->getType(), false, - llvm::GlobalValue::InternalLinkage, anno, - GV->getName() + StringPrefix); + new llvm::GlobalVariable(*M, anno->getType(), false, + llvm::GlobalValue::PrivateLinkage, anno, + GV->getName()); // translation unit name string, emitted into the llvm.metadata section. llvm::GlobalValue *unitGV = - new llvm::GlobalVariable(*M, unit->getType(), false, - llvm::GlobalValue::InternalLinkage, unit, - StringPrefix); + new llvm::GlobalVariable(*M, unit->getType(), false, + llvm::GlobalValue::PrivateLinkage, unit, + ".str"); // Create the ConstantStruct for the global annotation. llvm::Constant *Fields[4] = { @@ -1287,22 +1286,25 @@ GetAddrOfConstantCFString(const StringLiteral *Literal) { const char *Sect, *Prefix; bool isConstant; + llvm::GlobalValue::LinkageTypes Linkage; if (isUTF16) { Prefix = getContext().Target.getUnicodeStringSymbolPrefix(); Sect = getContext().Target.getUnicodeStringSection(); + // FIXME: why do utf strings get "l" labels instead of "L" labels? + Linkage = llvm::GlobalValue::InternalLinkage; // FIXME: Why does GCC not set constant here? isConstant = false; } else { - Prefix = getContext().Target.getStringSymbolPrefix(true); + Prefix = ".str"; Sect = getContext().Target.getCFStringDataSection(); + Linkage = llvm::GlobalValue::PrivateLinkage; // FIXME: -fwritable-strings should probably affect this, but we // are following gcc here. isConstant = true; } llvm::GlobalVariable *GV = new llvm::GlobalVariable(getModule(), C->getType(), isConstant, - llvm::GlobalValue::InternalLinkage, - C, Prefix); + Linkage, C, Prefix); if (Sect) GV->setSection(Sect); if (isUTF16) { @@ -1323,8 +1325,8 @@ GetAddrOfConstantCFString(const StringLiteral *Literal) { // The struct. C = VMContext.getConstantStruct(STy, Fields); GV = new llvm::GlobalVariable(getModule(), C->getType(), true, - llvm::GlobalVariable::InternalLinkage, C, - getContext().Target.getCFStringSymbolPrefix()); + llvm::GlobalVariable::PrivateLinkage, C, + "_unnamed_cfstring_"); if (const char *Sect = getContext().Target.getCFStringSection()) GV->setSection(Sect); Entry.setValue(GV); @@ -1383,7 +1385,7 @@ static llvm::Constant *GenerateStringLiteral(const std::string &str, // Create a global variable for this string return new llvm::GlobalVariable(CGM.getModule(), C->getType(), constant, - llvm::GlobalValue::InternalLinkage, + llvm::GlobalValue::PrivateLinkage, C, GlobalName); } @@ -1401,14 +1403,14 @@ llvm::Constant *CodeGenModule::GetAddrOfConstantString(const std::string &str, // Get the default prefix if a name wasn't specified. if (!GlobalName) - GlobalName = getContext().Target.getStringSymbolPrefix(IsConstant); + GlobalName = ".str"; // Don't share any string literals if strings aren't constant. if (!IsConstant) return GenerateStringLiteral(str, false, *this, GlobalName); llvm::StringMapEntry &Entry = - ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); + ConstantStringMap.GetOrCreateValue(&str[0], &str[str.length()]); if (Entry.getValue()) return Entry.getValue(); diff --git a/clang/test/CodeGen/darwin-string-literals.c b/clang/test/CodeGen/darwin-string-literals.c index ff245fcc7a5c..90662d15e6b4 100644 --- a/clang/test/CodeGen/darwin-string-literals.c +++ b/clang/test/CodeGen/darwin-string-literals.c @@ -1,9 +1,8 @@ -// RUN: clang-cc -triple i386-apple-darwin9 -emit-llvm %s -o %t && +// RUN: clang-cc -triple i386-apple-darwin9 -emit-llvm %s -o - | FileCheck %s -// RUN: grep -F '@"\01LC" = internal constant [8 x i8] c"string0\00"' %t && -// RUN: grep -F '@"\01LC1" = internal constant [8 x i8] c"string1\00", section "__TEXT,__cstring,cstring_literals"' %t && -// RUN: grep -F '@__utf16_string_ = internal global [35 x i8] c"h\00e\00l\00l\00o\00 \00\92! \00\03& \00\90! \00w\00o\00r\00l\00d\00\00", section "__TEXT,__ustring", align 2' %t && -// RUN: true +// CHECK: @.str = private constant [8 x i8] c"string0\00" +// CHECK: @.str1 = private constant [8 x i8] c"string1\00", section "__TEXT,__cstring,cstring_literals" +// CHECK: @__utf16_string_ = internal global [35 x i8] c"h\00e\00l\00l\00o\00 \00\92! \00\03& \00\90! \00w\00o\00r\00l\00d\00\00", section "__TEXT,__ustring", align 2 const char *g0 = "string0"; const void *g1 = __builtin___CFStringMakeConstantString("string1"); diff --git a/clang/test/CodeGen/string-init.c b/clang/test/CodeGen/string-init.c index 38c7ec057597..4a808512601e 100644 --- a/clang/test/CodeGen/string-init.c +++ b/clang/test/CodeGen/string-init.c @@ -1,5 +1,5 @@ // RUN: clang-cc -emit-llvm %s -o %t && -// RUN: grep 'internal constant \[10 x i8\]' %t && +// RUN: grep 'private constant \[10 x i8\]' %t && // RUN: not grep -F "[5 x i8]" %t && // RUN: not grep "store " %t