I'm using r169046 on x86 Linux. The problem is that this code: const volatile int g_2 = 1; int g_1 = 0; void func_1 (void) { g_1 = g_2; } int main (void) { func_1(); return 0; } Compiled like this: current-gcc -O2 -S small.c Gives this main(): main: movl $1, g_1 xorl %eax, %eax ret The load from g_2 shouldn't have been removed. Oddly, the load is not eliminated from func_1(), nor is it eliminated from main if we inline the function by hand.
Buggy const_value_known_p.
4.5 actually miscompiles this too, 4.4 works.
Created attachment 23058 [details] gcc46-pr47391.patch Untested fix.
> Untested fix. tree_invariant_p_1 uses if (TREE_CONSTANT (t) || (TREE_READONLY (t) && !TREE_SIDE_EFFECTS (t))) return true; rather than TREE_THIS_VOLATILE.
setup_one_parameter too, on the other side tree_add_const_value_attribute_for_decl, set_mem_attributes_minus_bitpos and get_callee_fndecl test TREE_READONLY && !TREE_THIS_VOLATILE.
Author: jakub Date: Fri Jan 21 12:57:52 2011 New Revision: 169084 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=169084 Log: PR tree-optimization/47391 * varpool.c (const_value_known_p): Return false if decl is volatile. * gcc.dg/pr47391.c: New test. Added: trunk/gcc/testsuite/gcc.dg/pr47391.c Modified: trunk/gcc/ChangeLog trunk/gcc/testsuite/ChangeLog trunk/gcc/varpool.c
Fixed on the trunk so far.
Author: jakub Date: Thu Apr 7 18:22:50 2011 New Revision: 172112 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=172112 Log: PR tree-optimization/47391 * tree-ssa-ccp.c (get_symbol_constant_value): Don't optimize if sym is volatile. Backport from mainline 2011-01-21 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/47391 * gcc.dg/pr47391.c: New test. Added: branches/gcc-4_5-branch/gcc/testsuite/gcc.dg/pr47391.c Modified: branches/gcc-4_5-branch/gcc/ChangeLog branches/gcc-4_5-branch/gcc/testsuite/ChangeLog branches/gcc-4_5-branch/gcc/tree-ssa-ccp.c
Fixed.