From b944197840869869bdaff1fbb486cbfaddefbeac Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Sun, 23 Mar 2014 17:47:16 +0000 Subject: MS ABI: Eliminate Duplicate Strings COFF doesn't have mergeable sections so LLVM/clang's normal tactics for string deduplication will not have any effect. To remedy this we place each string inside it's own section and mark the section as IMAGE_COMDAT_SELECT_ANY. However, we can only do this if the string has an external name that we can generate from it's contents. To be compatible with MSVC, we must use their scheme. Otherwise identical strings in translation units from clang may not be deduplicated with translation units in MSVC. This fixes PR18248. N.B. We will not attempt to do anything with a string literal which is not of type 'char' or 'wchar_t' because their compiler does not support unicode string literals as of this date. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204562 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenModule.cpp | 78 +++++++++++++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 18 deletions(-) (limited to 'lib/CodeGen/CodeGenModule.cpp') diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp index 53e7d56a8d..097e30c71d 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -2572,25 +2572,67 @@ CodeGenModule::GetConstantArrayFromStringLiteral(const StringLiteral *E) { llvm::Constant * CodeGenModule::GetAddrOfConstantStringFromLiteral(const StringLiteral *S) { CharUnits Align = getContext().getAlignOfGlobalVarInChars(S->getType()); - if (S->isAscii() || S->isUTF8()) { - SmallString<64> Str(S->getString()); - - // Resize the string to the right size, which is indicated by its type. - const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType()); - Str.resize(CAT->getSize().getZExtValue()); - return GetAddrOfConstantString(Str, /*GlobalName*/ 0, Align.getQuantity()); + + llvm::StringMapEntry *Entry = nullptr; + llvm::GlobalVariable *GV = nullptr; + if (!LangOpts.WritableStrings) { + llvm::StringMap *ConstantStringMap = nullptr; + switch (S->getCharByteWidth()) { + case 1: + ConstantStringMap = &Constant1ByteStringMap; + break; + case 2: + ConstantStringMap = &Constant2ByteStringMap; + break; + case 4: + ConstantStringMap = &Constant4ByteStringMap; + break; + default: + llvm_unreachable("unhandled byte width!"); + } + Entry = &ConstantStringMap->GetOrCreateValue(S->getBytes()); + GV = Entry->getValue(); + } + + if (!GV) { + StringRef GlobalVariableName; + llvm::GlobalValue::LinkageTypes LT; + if (!LangOpts.WritableStrings && + getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) { + LT = llvm::GlobalValue::LinkOnceODRLinkage; + + SmallString<256> Buffer; + llvm::raw_svector_ostream Out(Buffer); + getCXXABI().getMangleContext().mangleStringLiteral(S, Out); + Out.flush(); + + size_t Length = Buffer.size(); + char *Name = MangledNamesAllocator.Allocate(Length); + std::copy(Buffer.begin(), Buffer.end(), Name); + GlobalVariableName = StringRef(Name, Length); + } else { + LT = llvm::GlobalValue::PrivateLinkage;; + GlobalVariableName = ".str"; + } + + // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. + unsigned AddrSpace = 0; + if (getLangOpts().OpenCL) + AddrSpace = getContext().getTargetAddressSpace(LangAS::opencl_constant); + + llvm::Constant *C = GetConstantArrayFromStringLiteral(S); + GV = new llvm::GlobalVariable( + getModule(), C->getType(), !LangOpts.WritableStrings, LT, C, + GlobalVariableName, /*InsertBefore=*/nullptr, + llvm::GlobalVariable::NotThreadLocal, AddrSpace); + GV->setUnnamedAddr(true); + if (Entry) + Entry->setValue(GV); } - // FIXME: the following does not memoize wide strings. - llvm::Constant *C = GetConstantArrayFromStringLiteral(S); - llvm::GlobalVariable *GV = - new llvm::GlobalVariable(getModule(),C->getType(), - !LangOpts.WritableStrings, - llvm::GlobalValue::PrivateLinkage, - C,".str"); + if (Align.getQuantity() > GV->getAlignment()) + GV->setAlignment(Align.getQuantity()); - GV->setAlignment(Align.getQuantity()); - GV->setUnnamedAddr(true); return GV; } @@ -2615,7 +2657,7 @@ static llvm::GlobalVariable *GenerateStringLiteral(StringRef str, llvm::Constant *C = llvm::ConstantDataArray::getString(CGM.getLLVMContext(), str, false); - // OpenCL v1.1 s6.5.3: a string literal is in the constant address space. + // OpenCL v1.2 s6.5.3: a string literal is in the constant address space. unsigned AddrSpace = 0; if (CGM.getLangOpts().OpenCL) AddrSpace = CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant); @@ -2654,7 +2696,7 @@ llvm::Constant *CodeGenModule::GetAddrOfConstantString(StringRef Str, return GenerateStringLiteral(Str, false, *this, GlobalName, Alignment); llvm::StringMapEntry &Entry = - ConstantStringMap.GetOrCreateValue(Str); + Constant1ByteStringMap.GetOrCreateValue(Str); if (llvm::GlobalVariable *GV = Entry.getValue()) { if (Alignment > GV->getAlignment()) { -- cgit v1.2.3