Bug 23192 - [4.0 Regression] Wrong code
[4.0 Regression] Wrong code
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: tree-optimization
4.0.2
: P2 critical
: 4.0.2
Assigned To: Diego Novillo
: alias, wrong-code
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2005-08-02 06:03 UTC by Andrew Pinski
Modified: 2005-08-02 14:17 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work: 4.1.0 3.4.5
Known to fail:
Last reconfirmed: 2005-08-02 12:50:29


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andrew Pinski 2005-08-02 06:03:36 UTC
The following is creates wrong code because of aliasing issue:

typedef __SIZE_TYPE__ size_t;
typedef struct {
  size_t car;
  size_t cdr;
} cons;

int g(void);

size_t f(size_t a)
{
  size_t x;
  size_t *d;
  size_t i;
  d = &x;
  while ( (i = g()) != 0)
  {
    if (i == 30)
      d = &((cons*)i)->car;    
  }
  *d = 1;
}

This is reported here:
http://gcc.gnu.org/ml/gcc/2005-08/msg00041.html
http://gcc.gnu.org/ml/gcc-help/2005-08/msg00020.html

I think this is caused by &a->b being defined as a deference which is wrong.
Comment 1 Andrew Pinski 2005-08-02 06:12:38 UTC
This was broken on 20050705.
I think this was fixed by:
2005-07-22  Diego Novillo  <dnovillo@redhat.com>

        * tree-ssa-alias.c (count_ptr_derefs): Do not consider
        &PTR->FLD a dereference of PTR.
        * tree-ssa-structalias.c (update_alias_info): Consider &PTR->FLD
        a potential dereference of PTR.
Comment 2 Andrew Pinski 2005-08-02 06:22:08 UTC
I should note the better testcase is:
typedef __SIZE_TYPE__ size_t;
typedef struct {
  size_t car;
  size_t cdr;
} cons;

int g(void);

size_t f(size_t a)
{
  size_t x;
  size_t *d;
  size_t i;
  d = &x;
  while ( (i = g()) != 0)
  {
    if (i >= 30)
      d = &((cons*)i)->car;    
  }
  *d = 1;
}


The previous passes on 20050705 because of constant prop.
Comment 3 Serge Belyshev 2005-08-02 08:13:42 UTC
I think this testcase even better, it fails with -O1 -fstrict-aliasing and -O2:

struct S { int i; } s;

void f (struct S *q)
{
  int a, *p;
  p = &a;
  if (q)
    p = &q->i;
  *p = 1;
}

void abort (void);

int main (void)
{
  f (&s);
  if (s.i != 1)
    abort ();
  return 0;
}
Comment 4 Diego Novillo 2005-08-02 12:50:29 UTC
Testing patch.
Comment 5 CVS Commits 2005-08-02 14:04:54 UTC
Subject: Bug 23192

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	dnovillo@gcc.gnu.org	2005-08-02 14:04:37

Modified files:
	gcc            : ChangeLog tree-ssa-alias.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.dg/tree-ssa: pr23192.c 

Log message:
	PR 23192
	* tree-ssa-alias.c (add_pointed_to_var): If VALUE is of the
	form &(*PTR), take points-to information from PTR.
	Give up if VALUE by marking PTR as pointing anywhere if VALUE
	is not recognized.
	
	testsuite/ChangeLog
	
	PR 23192
	* gcc.dg/pr23192.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=2.7592.2.345&r2=2.7592.2.346
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-alias.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=2.71.2.2&r2=2.71.2.3
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.5084.2.314&r2=1.5084.2.315
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/tree-ssa/pr23192.c.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=NONE&r2=1.1.2.1

Comment 6 Diego Novillo 2005-08-02 14:17:28 UTC
Fixed.  http://gcc.gnu.org/ml/gcc-patches/2005-08/msg00102.html