Bug 35494

Summary: [4.4 Regression]: Revision 132991 breaks C++ static member
Product: gcc Reporter: H.J. Lu <hjl.tools>
Component: tree-optimizationAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: major CC: gcc-bugs, pinskia, rguenth
Priority: P3    
Version: 4.4.0   
Target Milestone: 4.4.0   
URL: http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00631.html
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Bug Depends on: 35513    
Bug Blocks: 35493    

Description H.J. Lu 2008-03-07 06:06:19 UTC
Revision 132991:

http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00445.html

breaks 483.xalancbmk in SPEC CPU 2006 on Linux/Intel64
with -O2 -ffast-math. I got

  Running 483.xalancbmk ref base o2 default

483.xalancbmk: copy #0 non-zero return code (rc=0, signal=6)


****************************************
Contents of ref.err
****************************************
terminate called after throwing an instance of 'xercesc_2_5::DOMException'

****************************************

That optimization may not be valid for C++ code.
Comment 1 pinskia@gmail.com 2008-03-07 07:01:48 UTC
Subject: Re:   New: [4.4 Regression]: Revision 132991 breaks 483.xalancbmk

This is most likely the c++ front-end setting readonly when it should  
not.

Sent from my iPhone

On Mar 6, 2008, at 22:06, "hjl dot tools at gmail dot com" <gcc-bugzilla@gcc.gnu.org 
 > wrote:

> Revision 132991:
>
> http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00445.html
>
> breaks 483.xalancbmk in SPEC CPU 2006 on Linux/Intel64
> with -O2 -ffast-math. I got
>
>  Running 483.xalancbmk ref base o2 default
>
> 483.xalancbmk: copy #0 non-zero return code (rc=0, signal=6)
>
>
> ****************************************
> Contents of ref.err
> ****************************************
> terminate called after throwing an instance of  
> 'xercesc_2_5::DOMException'
>
> ****************************************
>
> That optimization may not be valid for C++ code.
>
>
> -- 
>           Summary: [4.4 Regression]: Revision 132991 breaks  
> 483.xalancbmk
>           Product: gcc
>           Version: 4.4.0
>            Status: UNCONFIRMED
>          Severity: major
>          Priority: P3
>         Component: tree-optimization
>        AssignedTo: unassigned at gcc dot gnu dot org
>        ReportedBy: hjl dot tools at gmail dot com
>
>
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35494
>
> ------- You are receiving this mail because: -------
> You are on the CC list for the bug, or are watching someone who is.
Comment 2 Richard Biener 2008-03-07 09:08:28 UTC
Will probably happen in cases like

static const int i = foo();

?
Comment 3 H.J. Lu 2008-03-07 13:54:44 UTC
Are

bash-3.2$ cat y.cc
class bar
{
public:
  static const int conststaticvariable;
};


int f(void)
{
  return bar::conststaticvariable;
}
bash-3.2$ cat z.cc
extern int foo (void);

class bar
{
public:
  static const int conststaticvariable;
};


const int bar::conststaticvariable = foo ();
bash-3.2$ 

valid C++ program?
Comment 4 H.J. Lu 2008-03-07 14:03:37 UTC
(In reply to comment #1)
> Subject: Re:   New: [4.4 Regression]: Revision 132991 breaks 483.xalancbmk
> 
> This is most likely the c++ front-end setting readonly when it should  
> not.
> 

I think you should check a symbol is both readonly and local.
conststaticvariable in

---
class bar
{
public:
  static const int conststaticvariable;
};
---

is global, not local.

Comment 5 rguenther@suse.de 2008-03-07 14:22:22 UTC
Subject: Re:  [4.4 Regression]: Revision 132991
 breaks 483.xalancbmk

On Fri, 7 Mar 2008, hjl dot tools at gmail dot com wrote:

> ------- Comment #4 from hjl dot tools at gmail dot com  2008-03-07 14:03 -------
> (In reply to comment #1)
> > Subject: Re:   New: [4.4 Regression]: Revision 132991 breaks 483.xalancbmk
> > 
> > This is most likely the c++ front-end setting readonly when it should  
> > not.
> > 
> 
> I think you should check a symbol is both readonly and local.
> conststaticvariable in
> 
> ---
> class bar
> {
> public:
>   static const int conststaticvariable;
> };
> ---
> 
> is global, not local.

Yep, and yes, the example was valid.

Richard.
Comment 6 H.J. Lu 2008-03-07 14:24:28 UTC
This patch

--- tree-ssa-ccp.c.local        2008-03-06 14:18:27.000000000 -0800
+++ tree-ssa-ccp.c      2008-03-07 06:21:57.000000000 -0800
@@ -306,9 +306,10 @@ get_symbol_constant_value (tree sym)
       if (val
          && ccp_decl_initial_min_invariant (val))
        return val;
-      /* Variables declared 'const' without an initializer
+      /* Local variables declared 'const' without an initializer
         have zero as the intializer.  */
       if (!val
+         && !TREE_PUBLIC (sym)
           && (INTEGRAL_TYPE_P (TREE_TYPE (sym))
               || SCALAR_FLOAT_TYPE_P (TREE_TYPE (sym))))
         return fold_convert (TREE_TYPE (sym), integer_zero_node);

works on small C++ testcase. I will try 483.xalancbmk next.
Comment 7 H.J. Lu 2008-03-07 14:39:34 UTC
My patch works on 483.xalancbmk with test input. I am running with
ref input.
Comment 8 H.J. Lu 2008-03-07 14:56:51 UTC
Another testcase in C:

---
const int conststaticvariable;

int f(void)
{
  return conststaticvariable;
}
---

Can we assume conststaticvariable will be 0?
Comment 9 H.J. Lu 2008-03-07 15:17:28 UTC
A patch is posted at

http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00466.html
Comment 10 rguenther@suse.de 2008-03-07 15:20:41 UTC
Subject: Re:  [4.4 Regression]: Revision 132991
 breaks 483.xalancbmk

On Fri, 7 Mar 2008, hjl dot tools at gmail dot com wrote:

> 
> 
> ------- Comment #8 from hjl dot tools at gmail dot com  2008-03-07 14:56 -------
> Another testcase in C:
> 
> ---
> const int conststaticvariable;
> 
> int f(void)
> {
>   return conststaticvariable;
> }
> ---
> 
> Can we assume conststaticvariable will be 0?

No.
Comment 11 H.J. Lu 2008-03-10 14:50:52 UTC
The current patch is

http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00572.html
Comment 12 H.J. Lu 2008-03-10 16:20:21 UTC
The current patch is

http://gcc.gnu.org/ml/gcc-patches/2008-03/msg00631.html
Comment 13 hjl@gcc.gnu.org 2008-03-10 18:21:08 UTC
Subject: Bug 35494

Author: hjl
Date: Mon Mar 10 18:20:23 2008
New Revision: 133082

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=133082
Log:
gcc/

2008-03-10  H.J. Lu  <hongjiu.lu@intel.com>

	PR tree-optimization/35494
	* tree-ssa-ccp.c (get_symbol_constant_value): Check if value
	may be overriden at link and run time.

gcc/testsuite/

2008-03-10  H.J. Lu  <hongjiu.lu@intel.com>

	PR tree-optimization/35494
	* g++.dg/tree-ssa/ssa-store-ccp-1.C: New.
	* gcc.dg/tree-ssa/ssa-store-ccp-2.c: Likewise.
	* gcc.dg/tree-ssa/ssa-store-ccp-3.c: Likewise.
	* gcc.dg/tree-ssa/ssa-store-ccp-4.c: Likewise.

Added:
    trunk/gcc/testsuite/g++.dg/tree-ssa/ssa-store-ccp-1.C
    trunk/gcc/testsuite/gcc.dg/tree-ssa/ssa-store-ccp-2.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/ssa-store-ccp-3.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/ssa-store-ccp-4.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-ccp.c

Comment 14 H.J. Lu 2008-03-10 18:21:39 UTC
Fixed.