Bug 41317

Summary: folding causes strict aliasing violation
Product: gcc Reporter: Richard Biener <rguenth>
Component: middle-endAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: normal CC: gcc-bugs
Priority: P3 Keywords: alias, wrong-code
Version: 4.5.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2009-09-09 12:16:29
Bug Depends on:    
Bug Blocks: 41316    

Description Richard Biener 2009-09-09 11:36:32 UTC
extern void abort (void);

struct A
{
  int i;
};
struct B
{
  struct A a;
  int j;
};

static void
foo (struct B *p)
{
  ((struct A *)p)->i = 1;
}

int main()
{
  struct A a;
  a.i = 0;
  foo ((struct B *)&a);
  if (a.i != 1)
    abort ();
  return 0;
}


Folding (struct A *)p to &p->a causes us to see

  a.i = 0;
  a.0_1 = (struct B *) &a;
  a.0_1->a.i = 1;
  D.2703_2 = a.i;

where the store a.0_1->a.i does not alias a.i because of TBAA on the base
object (you can't access an object of type A via a pointer to type B).

This blocks the fix for PR41316.
Comment 1 Richard Biener 2009-09-09 11:42:25 UTC
4.4 rightfully complains:

t.i: In function ‘main’:
t.i:16: warning: dereferencing pointer ‘a.0’ does break strict-aliasing rules
t.i:23: note: initialized from here
Comment 2 Richard Biener 2009-09-09 12:16:29 UTC
Mine.
Comment 3 Richard Biener 2009-09-09 14:36:01 UTC
Fixed.
Comment 4 Richard Biener 2009-09-09 14:37:12 UTC
Subject: Bug 41317

Author: rguenth
Date: Wed Sep  9 14:35:51 2009
New Revision: 151559

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=151559
Log:
2009-09-09  Richard Guenther  <rguenther@suse.de>

	PR middle-end/41317
	* tree-ssa-ccp.c (maybe_fold_offset_to_component_ref): Remove
	code dealing with plain pointer bases.
	(maybe_fold_offset_to_reference): Likewise.
	(maybe_fold_stmt_addition): Adjust.

	* gcc.c-torture/execute/pr41317.c: New testcase.
	* gcc.dg/tree-ssa/forwprop-11.c: XFAIL.
	* gcc.dg/tree-ssa/forwprop-12.c: Likewise.

Added:
    trunk/gcc/testsuite/gcc.c-torture/execute/pr41317.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/tree-ssa/forwprop-11.c
    trunk/gcc/testsuite/gcc.dg/tree-ssa/forwprop-12.c
    trunk/gcc/tree-ssa-ccp.c

Comment 5 Jakub Jelinek 2009-11-09 19:38:45 UTC
Subject: Bug 41317

Author: jakub
Date: Mon Nov  9 19:38:29 2009
New Revision: 154046

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=154046
Log:
	PR middle-end/40946
	Backport from mainline
	2009-09-09  Richard Guenther  <rguenther@suse.de>

	PR middle-end/41317
	* tree-ssa-ccp.c (maybe_fold_offset_to_component_ref): Remove
	code dealing with plain pointer bases.
	(maybe_fold_offset_to_reference): Likewise.
	(maybe_fold_stmt_addition): Adjust.

	PR middle-end/40946
	* gcc.dg/pr40946.c: New test.

	Backport from mainline
	2009-09-09  Richard Guenther  <rguenther@suse.de>

	PR middle-end/41317
	* gcc.c-torture/execute/pr41317.c: New testcase.

Added:
    branches/gcc-4_4-branch/gcc/testsuite/gcc.c-torture/execute/pr41317.c
    branches/gcc-4_4-branch/gcc/testsuite/gcc.dg/pr40946.c
Modified:
    branches/gcc-4_4-branch/gcc/ChangeLog
    branches/gcc-4_4-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_4-branch/gcc/tree-ssa-ccp.c