Hi,
this patch is in mainline for a while, but apparently I forgot to attach
PR info to it, so it did not make it to the release branch.

Bootstrapped/regtested x86_64-linux, comitted.

Honza

        PR ipa/61144
        * varpool.c (ctor_for_folding): Do not fold WEAK symbols.
        * gcc.dg/tree-ssa/pr61144.c: New testcase.
Index: varpool.c
===================================================================
--- varpool.c   (revision 215895)
+++ varpool.c   (working copy)
@@ -329,8 +329,16 @@ ctor_for_folding (tree decl)
 
   /* Variables declared 'const' without an initializer
      have zero as the initializer if they may not be
-     overridden at link or run time.  */
-  if (!DECL_INITIAL (real_decl)
+     overridden at link or run time.
+
+     It is actually requirement for C++ compiler to optimize const variables
+     consistently. As a GNU extension, do not enfore this rule for user defined
+     weak variables, so we support interposition on:
+     static const int dummy = 0;
+     extern const int foo __attribute__((__weak__, __alias__("dummy"))); 
+   */
+  if ((!DECL_INITIAL (real_decl)
+       || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
       && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
     return error_mark_node;
 
Index: testsuite/gcc.dg/tree-ssa/pr61144.c
===================================================================
--- testsuite/gcc.dg/tree-ssa/pr61144.c (revision 0)
+++ testsuite/gcc.dg/tree-ssa/pr61144.c (revision 0)
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+/* { dg-require-weak "" } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+static int dummy = 0;
+extern int foo __attribute__((__weak__, __alias__("dummy")));
+int bar() { if (foo) return 1; return 0; }
+/* { dg-final { scan-tree-dump-not "return 0" "optimized"} } */

Reply via email to