Fix the merging of the constantness of declarations.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 30 Oct 2014 20:50:23 +0000 (20:50 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 30 Oct 2014 20:50:23 +0000 (20:50 +0000)
The langref says:

LLVM explicitly allows declarations of global variables to be marked
constant, even if the final definition of the global is not. This
capability can be used to enable slightly better optimization of the
program, but requires the language definition to guarantee that
optimizations based on the ‘constantness’ are valid for the
translation units that do not include the definition.

Given that definition, when merging two declarations, we have to drop
constantness if of of them is not marked contant, since the Module
without the constant marker might not have the necessary guarantees.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220927 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Linker/LinkModules.cpp
test/Linker/ConstantGlobals3.ll

index 403a1ff90c598c6c1a37066e3b42a14268f2d782..2692ec97b7826f32c94c5b7987b8a85b472ba8cc 100644 (file)
@@ -1052,9 +1052,8 @@ bool ModuleLinker::linkGlobalProto(GlobalVariable *SGV) {
         if (GlobalVariable *DGVar = dyn_cast<GlobalVariable>(DGV)) {
           DGVar->setAlignment(Alignment);
 
-          if (DGVar->isDeclaration() && SGV->isConstant() &&
-              !DGVar->isConstant())
-            DGVar->setConstant(true);
+          if (DGVar->isDeclaration() && !SGV->isConstant())
+            DGVar->setConstant(false);
         }
 
         // Set calculated linkage, visibility and unnamed_addr.
index 5867ea50088234e1ba41c4bd6d682f9476931c70..0b70ddd646f8592314bd1d0e8447a64db01b0325 100644 (file)
@@ -1,6 +1,6 @@
 ; RUN: llvm-link %s %S/Inputs/ConstantGlobals3.ll -S | FileCheck %s
 ; RUN: llvm-link %S/Inputs/ConstantGlobals3.ll %s -S | FileCheck %s
 
-; CHECK: @X = external constant [1 x i32]
+; CHECK: @X = external global [1 x i32]
 
 @X = external global [1 x i32]