<div dir="ltr">This fixes a wrong-code regression since 3.4, and should go onto the branch.</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Jul 29, 2014 at 2:20 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard-llvm@metafoo.co.uk" target="_blank">richard-llvm@metafoo.co.uk</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: rsmith<br>
Date: Tue Jul 29 16:20:12 2014<br>
New Revision: 214222<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=214222&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=214222&view=rev</a><br>
Log:<br>
PR20473: Don't "deduplicate" string literals with the same value but different<br>
lengths! In passing, simplify string literal deduplication by relying on LLVM<br>
to deduplicate the underlying constant values.<br>
<br>
Modified:<br>
cfe/trunk/lib/CodeGen/CodeGenModule.cpp<br>
cfe/trunk/lib/CodeGen/CodeGenModule.h<br>
cfe/trunk/test/CodeGen/init.c<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=214222&r1=214221&r2=214222&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=214222&r1=214221&r2=214222&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Tue Jul 29 16:20:12 2014<br>
@@ -2773,10 +2773,11 @@ CodeGenModule::GetAddrOfConstantStringFr<br>
auto Alignment =<br>
getContext().getAlignOfGlobalVarInChars(S->getType()).getQuantity();<br>
<br>
- llvm::StringMapEntry<llvm::GlobalVariable *> *Entry = nullptr;<br>
+ llvm::Constant *C = GetConstantArrayFromStringLiteral(S);<br>
+ llvm::GlobalVariable **Entry = nullptr;<br>
if (!LangOpts.WritableStrings) {<br>
- Entry = getConstantStringMapEntry(S->getBytes(), S->getCharByteWidth());<br>
- if (auto GV = Entry->getValue()) {<br>
+ Entry = &ConstantStringMap[C];<br>
+ if (auto GV = *Entry) {<br>
if (Alignment > GV->getAlignment())<br>
GV->setAlignment(Alignment);<br>
return GV;<br>
@@ -2803,10 +2804,9 @@ CodeGenModule::GetAddrOfConstantStringFr<br>
GlobalVariableName = ".str";<br>
}<br>
<br>
- llvm::Constant *C = GetConstantArrayFromStringLiteral(S);<br>
auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);<br>
if (Entry)<br>
- Entry->setValue(GV);<br>
+ *Entry = GV;<br>
<br>
reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>");<br>
return GV;<br>
@@ -2822,26 +2822,6 @@ CodeGenModule::GetAddrOfConstantStringFr<br>
return GetAddrOfConstantCString(Str);<br>
}<br>
<br>
-<br>
-llvm::StringMapEntry<llvm::GlobalVariable *> *CodeGenModule::getConstantStringMapEntry(<br>
- StringRef Str, int CharByteWidth) {<br>
- llvm::StringMap<llvm::GlobalVariable *> *ConstantStringMap = nullptr;<br>
- switch (CharByteWidth) {<br>
- case 1:<br>
- ConstantStringMap = &Constant1ByteStringMap;<br>
- break;<br>
- case 2:<br>
- ConstantStringMap = &Constant2ByteStringMap;<br>
- break;<br>
- case 4:<br>
- ConstantStringMap = &Constant4ByteStringMap;<br>
- break;<br>
- default:<br>
- llvm_unreachable("unhandled byte width!");<br>
- }<br>
- return &ConstantStringMap->GetOrCreateValue(Str);<br>
-}<br>
-<br>
/// GetAddrOfConstantCString - Returns a pointer to a character array containing<br>
/// the literal and a terminating '\0' character.<br>
/// The result has pointer to array type.<br>
@@ -2854,19 +2834,20 @@ llvm::GlobalVariable *CodeGenModule::Get<br>
.getQuantity();<br>
}<br>
<br>
+ llvm::Constant *C =<br>
+ llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);<br>
+<br>
// Don't share any string literals if strings aren't constant.<br>
- llvm::StringMapEntry<llvm::GlobalVariable *> *Entry = nullptr;<br>
+ llvm::GlobalVariable **Entry = nullptr;<br>
if (!LangOpts.WritableStrings) {<br>
- Entry = getConstantStringMapEntry(StrWithNull, 1);<br>
- if (auto GV = Entry->getValue()) {<br>
+ Entry = &ConstantStringMap[C];<br>
+ if (auto GV = *Entry) {<br>
if (Alignment > GV->getAlignment())<br>
GV->setAlignment(Alignment);<br>
return GV;<br>
}<br>
}<br>
<br>
- llvm::Constant *C =<br>
- llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);<br>
// Get the default prefix if a name wasn't specified.<br>
if (!GlobalName)<br>
GlobalName = ".str";<br>
@@ -2874,7 +2855,7 @@ llvm::GlobalVariable *CodeGenModule::Get<br>
auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,<br>
GlobalName, Alignment);<br>
if (Entry)<br>
- Entry->setValue(GV);<br>
+ *Entry = GV;<br>
return GV;<br>
}<br>
<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=214222&r1=214221&r2=214222&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=214222&r1=214221&r2=214222&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Tue Jul 29 16:20:12 2014<br>
@@ -361,9 +361,7 @@ class CodeGenModule : public CodeGenType<br>
<br>
llvm::StringMap<llvm::Constant*> CFConstantStringMap;<br>
<br>
- llvm::StringMap<llvm::GlobalVariable *> Constant1ByteStringMap;<br>
- llvm::StringMap<llvm::GlobalVariable *> Constant2ByteStringMap;<br>
- llvm::StringMap<llvm::GlobalVariable *> Constant4ByteStringMap;<br>
+ llvm::DenseMap<llvm::Constant *, llvm::GlobalVariable *> ConstantStringMap;<br>
llvm::DenseMap<const Decl*, llvm::Constant *> StaticLocalDeclMap;<br>
llvm::DenseMap<const Decl*, llvm::GlobalVariable*> StaticLocalDeclGuardMap;<br>
llvm::DenseMap<const Expr*, llvm::Constant *> MaterializedGlobalTemporaryMap;<br>
@@ -1044,9 +1042,6 @@ private:<br>
llvm::PointerType *PTy,<br>
const VarDecl *D);<br>
<br>
- llvm::StringMapEntry<llvm::GlobalVariable *> *<br>
- getConstantStringMapEntry(StringRef Str, int CharByteWidth);<br>
-<br>
/// Set attributes which are common to any form of a global definition (alias,<br>
/// Objective-C method, function, global variable).<br>
///<br>
<br>
Modified: cfe/trunk/test/CodeGen/init.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/init.c?rev=214222&r1=214221&r2=214222&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/init.c?rev=214222&r1=214221&r2=214222&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/CodeGen/init.c (original)<br>
+++ cfe/trunk/test/CodeGen/init.c Tue Jul 29 16:20:12 2014<br>
@@ -132,3 +132,11 @@ void test13(int x) {<br>
// CHECK: @test13<br>
// CHECK: and i16 {{.*}}, -1024<br>
}<br>
+<br>
+// CHECK-LABEL: @PR20473<br>
+void PR20473() {<br>
+ // CHECK: memcpy{{.*}}([2 x i8]* @<br>
+ bar((char[2]) {""});<br>
+ // CHECK: memcpy{{.*}}([3 x i8]* @<br>
+ bar((char[3]) {""});<br>
+}<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>