Don't load values out of global constants with weak
authorDuncan Sands <baldrick@free.fr>
Fri, 20 Mar 2009 21:53:29 +0000 (21:53 +0000)
committerDuncan Sands <baldrick@free.fr>
Fri, 20 Mar 2009 21:53:29 +0000 (21:53 +0000)
linkage: the value may be replaced with something
different at link time.  (Frontends that want to
allow values to be loaded out of weak constants can
give their constants weak_odr linkage).

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

lib/Transforms/Scalar/InstructionCombining.cpp
lib/Transforms/Scalar/SCCP.cpp
lib/Transforms/Utils/CloneFunction.cpp
test/Feature/weak_constant.ll [new file with mode: 0644]
test/FrontendC/weak_constant.c [new file with mode: 0644]

index 4a7f4c74f3358d60e54cb1aa3000bc3290b6eb10..10671cd908b767ca2cd88e4a96ea4c1de4316f11 100644 (file)
@@ -11215,14 +11215,15 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
 
     // Instcombine load (constant global) into the value loaded.
     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op))
-      if (GV->isConstant() && !GV->isDeclaration())
+      if (GV->isConstant() && !GV->isDeclaration() && !GV->mayBeOverridden())
         return ReplaceInstUsesWith(LI, GV->getInitializer());
 
     // Instcombine load (constantexpr_GEP global, 0, ...) into the value loaded.
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Op)) {
       if (CE->getOpcode() == Instruction::GetElementPtr) {
         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
-          if (GV->isConstant() && !GV->isDeclaration())
+          if (GV->isConstant() && !GV->isDeclaration() &&
+              !GV->mayBeOverridden())
             if (Constant *V = 
                ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE))
               return ReplaceInstUsesWith(LI, V);
@@ -11246,7 +11247,7 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) {
   // If this load comes from anywhere in a constant global, and if the global
   // is all undef or zero, we know what it loads.
   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Op->getUnderlyingObject())){
-    if (GV->isConstant() && GV->hasInitializer()) {
+    if (GV->isConstant() && GV->hasInitializer() && !GV->mayBeOverridden()) {
       if (GV->getInitializer()->isNullValue())
         return ReplaceInstUsesWith(LI, Constant::getNullValue(LI.getType()));
       else if (isa<UndefValue>(GV->getInitializer()))
index 7adc80fb5a577187bfc390e0512fcb82988cffc1..a49bcc84547b7ba2a74aa0fb1864509359f3f3fe 100644 (file)
@@ -1131,7 +1131,7 @@ void SCCPSolver::visitLoadInst(LoadInst &I) {
     // Transform load (constant global) into the value loaded.
     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Ptr)) {
       if (GV->isConstant()) {
-        if (!GV->isDeclaration()) {
+        if (!GV->isDeclaration() && !GV->mayBeOverridden()) {
           markConstant(IV, &I, GV->getInitializer());
           return;
         }
@@ -1150,7 +1150,7 @@ void SCCPSolver::visitLoadInst(LoadInst &I) {
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ptr))
       if (CE->getOpcode() == Instruction::GetElementPtr)
     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
-      if (GV->isConstant() && !GV->isDeclaration())
+      if (GV->isConstant() && !GV->isDeclaration() && !GV->mayBeOverridden())
         if (Constant *V =
              ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(), CE)) {
           markConstant(IV, &I, V);
index 59211028f313c82a0aa5d6daac537b208e0fb8a7..5dc2d1cdc3a773ae234d0fa1648eb61191732172 100644 (file)
@@ -335,7 +335,8 @@ ConstantFoldMappedInstruction(const Instruction *I) {
     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ops[0]))
       if (!LI->isVolatile() && CE->getOpcode() == Instruction::GetElementPtr)
         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(CE->getOperand(0)))
-          if (GV->isConstant() && !GV->isDeclaration())
+          if (GV->isConstant() && !GV->isDeclaration() &&
+              !GV->mayBeOverridden())
             return ConstantFoldLoadThroughGEPConstantExpr(GV->getInitializer(),
                                                           CE);
 
diff --git a/test/Feature/weak_constant.ll b/test/Feature/weak_constant.ll
new file mode 100644 (file)
index 0000000..d27adfe
--- /dev/null
@@ -0,0 +1,38 @@
+; RUN: llvm-as < %s | opt -std-compile-opts | llvm-dis > %t
+; RUN:   grep undef %t | count 1
+; RUN:   grep 5 %t | count 1
+; RUN:   grep 7 %t | count 1
+; RUN:   grep 9 %t | count 1
+
+       type { i32, i32 }               ; type %0
+@a = weak constant i32 undef           ; <i32*> [#uses=1]
+@b = weak constant i32 5               ; <i32*> [#uses=1]
+@c = weak constant %0 { i32 7, i32 9 }         ; <%0*> [#uses=1]
+
+define i32 @la() {
+       %v = load i32* @a               ; <i32> [#uses=1]
+       ret i32 %v
+}
+
+define i32 @lb() {
+       %v = load i32* @b               ; <i32> [#uses=1]
+       ret i32 %v
+}
+
+define i32 @lc() {
+       %g = getelementptr %0* @c, i32 0, i32 0         ; <i32*> [#uses=1]
+       %u = load i32* %g               ; <i32> [#uses=1]
+       %h = getelementptr %0* @c, i32 0, i32 1         ; <i32*> [#uses=1]
+       %v = load i32* %h               ; <i32> [#uses=1]
+       %r = add i32 %u, %v
+       ret i32 %r
+}
+
+define i32 @f() {
+       %u = call i32 @la()             ; <i32> [#uses=1]
+       %v = call i32 @lb()             ; <i32> [#uses=1]
+       %w = call i32 @lc()             ; <i32> [#uses=1]
+       %r = add i32 %u, %v             ; <i32> [#uses=1]
+       %s = add i32 %r, %w             ; <i32> [#uses=1]
+       ret i32 %s
+}
diff --git a/test/FrontendC/weak_constant.c b/test/FrontendC/weak_constant.c
new file mode 100644 (file)
index 0000000..5337948
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: %llvmgcc -S %s -O1 -o - | grep {ret.*123}
+// Check for bug compatibility with gcc.
+
+const int x __attribute((weak)) = 123;
+
+int* f(void) {
+  return &x;
+}
+
+int g(void) {
+  return *f();
+}