Bug 47391 - [4.5 Regression] read from const volatile incorrectly eliminated
Summary: [4.5 Regression] read from const volatile incorrectly eliminated
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.6.0
: P1 normal
Target Milestone: 4.5.3
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-21 05:51 UTC by John Regehr
Modified: 2011-04-07 18:39 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.4.5, 4.6.0
Known to fail: 4.5.2
Last reconfirmed: 2011-01-21 08:24:13


Attachments
gcc46-pr47391.patch (547 bytes, patch)
2011-01-21 08:36 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description John Regehr 2011-01-21 05:51:52 UTC
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.
Comment 1 Jakub Jelinek 2011-01-21 08:24:13 UTC
Buggy const_value_known_p.
Comment 2 Jakub Jelinek 2011-01-21 08:28:20 UTC
4.5 actually miscompiles this too, 4.4 works.
Comment 3 Jakub Jelinek 2011-01-21 08:36:08 UTC
Created attachment 23058 [details]
gcc46-pr47391.patch

Untested fix.
Comment 4 Eric Botcazou 2011-01-21 09:19:42 UTC
> 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.
Comment 5 Jakub Jelinek 2011-01-21 09:29:46 UTC
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.
Comment 6 Jakub Jelinek 2011-01-21 12:57:55 UTC
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
Comment 7 Jakub Jelinek 2011-01-21 13:02:29 UTC
Fixed on the trunk so far.
Comment 8 Jakub Jelinek 2011-04-07 18:22:54 UTC
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
Comment 9 Jakub Jelinek 2011-04-07 18:39:38 UTC
Fixed.