Bug 15262 - [4.0 Regression] [tree-ssa] Alias analyzer cannot handle addressable fields
Summary: [4.0 Regression] [tree-ssa] Alias analyzer cannot handle addressable fields
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: tree-ssa
: P2 minor
Target Milestone: 4.0.0
Assignee: Diego Novillo
URL:
Keywords: wrong-code
: 17461 17484 17489 (view as bug list)
Depends on:
Blocks: 14029 14470
  Show dependency treegraph
 
Reported: 2004-05-03 14:22 UTC by Diego Novillo
Modified: 2004-09-14 23:10 UTC (History)
6 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2004-08-13 00:17:02


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Diego Novillo 2004-05-03 14:22:02 UTC
The type-based alias analyzer cannot handle aliasing fields in structures.  Given
&PTR->FIELD, it fails to build alias sets for PTR's memory tag.  There is very
limited support for handling it, but it needs to be revamped.

Filing as a bug against the branch.  I've got a partial fix, but it's proving to
be too intrusive for the freeze period.  Will fix after the merge into mainline.

Compile the following with -O2 (adapted from fortran/scanner.c:next_char)


struct A
{
  char *nextc;
  float b;
};
 
void
bar (float *p)
{
  *p = 5.2;
}
 
char *
foo(struct A *locp, int i, char *str)
{
  float f, g, *p;
  char *T355;
  char **T356;
 
  /* Currently, the alias analyzer has limited support for handling
     aliases of structure fields when no other variables are aliased.
     Introduce additional aliases to confuse it.  */
  p =  i ? &g : &f;
  bar (p);
  if (*p > 0.0)
    str = "abc";
 
  T355 = locp->nextc;
  T356 = &locp->nextc;
  *T356 = str;
  T355 = locp->nextc;
 
  return T355;
}
 
main ()
{
  struct A loc;
  char *str;
 
  loc.nextc = "123";
  str = foo (&loc, 10, "xyz");
  if (str[0] != 'a' || str[1] != 'b' || str[2] != 'c' || str[3] != '\0')
    abort ();
  return 0;
}
Comment 1 Andrew Pinski 2004-05-03 14:47:10 UTC
Confirmed, actually I think the front-end should not lower &a->b into just (typeof(&a-
>b))((char*)(a)+offsetof(typeof(*a),b)), in fact once I disable the lowering in the front-end it works just 
fine.  I will submit a patch after the tree-ssa gets merged into the mainline for this and offsetof working 
with this lowering disabled.
Comment 2 Andrew Pinski 2004-05-03 14:50:07 UTC
After disabling the lowering I get:

  *&locp->nextc = str;
  return locp->nextc;

which is right but causes not code generation.
Comment 3 Diego Novillo 2004-05-03 14:51:11 UTC
Subject: Re:  [tree-ssa] Alias analyzer cannot
	handle addressable fields

On Mon, 2004-05-03 at 10:47, pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2004-05-03 14:47 -------
> Confirmed, actually I think the front-end should not lower &a->b into just (typeof(&a-
> >b))((char*)(a)+offsetof(typeof(*a),b)), in fact once I disable the lowering in the front-end it works just 
> fine.  I will submit a patch after the tree-ssa gets merged into the mainline for this and offsetof working 
> with this lowering disabled.
>
I thought jason had already removed this limitation?  How is &PTR->FIELD
represented with your patch?


Diego.

Comment 4 Diego Novillo 2004-05-03 14:53:55 UTC
Subject: Re:  [tree-ssa] Alias analyzer cannot
	handle addressable fields

On Mon, 2004-05-03 at 10:50, pinskia at gcc dot gnu dot org wrote:
> ------- Additional Comments From pinskia at gcc dot gnu dot org  2004-05-03 14:50 -------
> After disabling the lowering I get:
> 
>   *&locp->nextc = str;
>   return locp->nextc;
> 
> which is right but causes not code generation.
>
I see.  However, that only papers over the alias bug.  We'll probably
need to make the test case more complex to disallow the propagation of
&PTR->FIELD into its dereference site.


Diego.

Comment 5 Andrew Pinski 2004-05-03 15:01:22 UTC
Actually it is not an aliasing bug per say in C or C++ because C and C++ aliasing rules say that the 
following code is undefined, see PR 14029 for another example for where this can happen (this time in 
C++ where &a->b happens more than C because of references) :

char *
foo(struct A *locp, int i, char *str)
{
  float f, g, *p;
  char *T355;
  char **T356;

  /* Currently, the alias analyzer has limited support for handling
     aliases of structure fields when no other variables are aliased.
     Introduce additional aliases to confuse it.  */
  p =  i ? &g : &f;
  bar (p);
  if (*p > 0.0)
    str = "abc";

  T355 = locp->nextc;
  T356 = (char**)locp;  // note the cast here which is what the front-end is producing which is wrong
  *T356 = str;
  T355 = locp->nextc;

  return T355;
}
Comment 6 Andrew Pinski 2004-05-03 15:32:26 UTC
Here is another example which does not use char pointers at all:
struct A
{
  int t;
  int i;
};

void
bar (float *p)
{
  *p = 5.2;
}

int
foo(struct A *locp, int i, int str)
{
  float f, g, *p;
  int T355;
  int *T356;
  /* Currently, the alias analyzer has limited support for handling
     aliases of structure fields when no other variables are aliased.
     Introduce additional aliases to confuse it.  */
  p =  i ? &g : &f;
  bar (p);
  if (*p > 0.0)
    str = 1;

  T355 = locp->i;
  T356 = &locp->i;  // the problem is that the front-end changes this to ((int *)((char*)locp +4))
  *T356 = str;
  T355 = locp->i;

  return T355;
}

main ()
{
  struct A loc;
  int str;

  loc.i = 2;
  str = foo (&loc, 10, 3);
  if (str!=1)
    abort ();
  return 0;
}
Comment 7 Andrew Pinski 2004-05-03 15:35:40 UTC
In my last example if the user wrote "((int *)((char*)locp +4))" this would violate C aliasing rules can 
cause undefined behavior to happen which is why I said this is not an alasing bug per say.
Comment 8 Diego Novillo 2004-05-03 15:47:54 UTC
Subject: Re:  [tree-ssa] Alias analyzer cannot
	handle addressable fields

On Mon, 2004-05-03 at 11:32, pinskia at gcc dot gnu dot org wrote:

>   T355 = locp->i;
>   T356 = &locp->i;  // the problem is that the front-end changes this to ((int *)((char*)locp +4))
>   *T356 = str;
>   T355 = locp->i;
> 
Regardless of FE problems.  The alias analyzer fails to connect *T356
with locp->i.  In terms of our implementation, the memory tag for T356
should have locp->i in its alias set.  However, we only operate on
symbols, so we should either make 'locp' be in *T356's alias set or keep
track of individual fields.

We also don't have information in the IL that tells us that locp->i is
addressable.  Which also makes things more complicated during TBAA.


Diego.

Comment 9 GCC Commits 2004-08-23 03:12:43 UTC
Subject: Bug 15262

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	pinskia@gcc.gnu.org	2004-08-23 03:12:39

Modified files:
	gcc            : ChangeLog c-typeck.c 
	gcc/testsuite  : ChangeLog 
	gcc/cp         : typeck.c ChangeLog 
Added files:
	gcc/testsuite/g++.dg/opt: pr14029.C 
	gcc/testsuite/gcc.c-torture/execute: pr15262.c 

Log message:
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	PR c/15262
	* c-typeck.c (build_unary_op): Use &a.b if the foldded lowered
	expression is not constant.
	(c_finish_return): Do not go through INDIRECT_REF when looking
	for the inner expression of an ADDR_EXPR for warning about.
	
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	* g++.dg/opt/pr14029.C: New test.
	* gcc.c-torture/execute/pr15262.c: New test.
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	PR c++/14029
	* typeck.c (build_unary_op): Use &a.b if the foldded lowered
	expression is not constant.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.5024&r2=2.5025
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-typeck.c.diff?cvsroot=gcc&r1=1.359&r2=1.360
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4172&r2=1.4173
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&r1=1.567&r2=1.568
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&r1=1.4293&r2=1.4294
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/opt/pr14029.C.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr15262.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 10 Andrew Pinski 2004-08-23 03:17:42 UTC
The testcases are fixed by there might be still a problem in that the memory tags are not right still 
even after my change but most of the time it does not matter, lowering the severity because I do not 
know of a testcase which fails now.
Comment 11 GCC Commits 2004-08-23 04:02:08 UTC
Subject: Bug 15262

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	apple-ppc-branch
Changes by:	pinskia@gcc.gnu.org	2004-08-23 04:02:01

Modified files:
	gcc            : ChangeLog.apple-ppc c-typeck.c 
	gcc/cp         : ChangeLog.apple-ppc typeck.c 
	gcc/testsuite  : ChangeLog.apple-ppc 
Added files:
	gcc/testsuite/g++.dg/opt: pr14029.C 
	gcc/testsuite/gcc.c-torture/execute: pr15262.c 

Log message:
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	PR c/15262
	* c-typeck.c (build_unary_op): Use &a.b if the foldded lowered
	expression is not constant.
	(c_finish_return): Do not go through INDIRECT_REF when looking
	for the inner expression of an ADDR_EXPR for warning about.
	
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	* g++.dg/opt/pr14029.C: New test.
	* gcc.c-torture/execute/pr15262.c: New test.
	2004-08-22  Andrew Pinski  <apinski@apple.com>
	
	PR c++/14029
	* typeck.c (build_unary_op): Use &a.b if the foldded lowered
	expression is not constant.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.apple-ppc.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.1.2.129&r2=1.1.2.130
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-typeck.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.196.2.43.2.13&r2=1.196.2.43.2.14
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.apple-ppc.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.1.2.7&r2=1.1.2.8
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/typeck.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.408.2.43.2.12&r2=1.408.2.43.2.13
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.apple-ppc.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=1.1.2.25&r2=1.1.2.26
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/opt/pr14029.C.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=NONE&r2=1.1.2.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr15262.c.diff?cvsroot=gcc&only_with_tag=apple-ppc-branch&r1=NONE&r2=1.1.2.1

Comment 12 Diego Novillo 2004-09-14 12:02:25 UTC
*** Bug 17461 has been marked as a duplicate of this bug. ***
Comment 13 Andrew Pinski 2004-09-14 21:47:14 UTC
*** Bug 17484 has been marked as a duplicate of this bug. ***
Comment 14 GCC Commits 2004-09-14 22:45:59 UTC
Subject: Bug 15262

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	dnovillo@gcc.gnu.org	2004-09-14 22:45:54

Modified files:
	gcc            : ChangeLog tree-dfa.c tree-ssa-alias.c 
	gcc/testsuite  : ChangeLog 
Added files:
	gcc/testsuite/gcc.c-torture/execute: pr15262-1.c pr15262-2.c 

Log message:
	PR tree-optimization/15262
	* tree-dfa.c (dump_variable): Also print the type of the
	variable.
	* tree-ssa-alias.c (compute_flow_insensitive_aliasing): If two
	memory tags are of conflicting alias sets but have no aliased
	symbols in common, add one tag to the alias set of the other.
	(setup_pointers_and_addressables): Remove hack to deal with
	programs with no aliased symbols.
	(may_alias_p): Don't special case aggregate types.
	
	testsuite/ChangeLog
	
	PR tree-optimization/15262
	* gcc.c-torture/execute/pr15262-1.c: New test.
	* gcc.c-torture/execute/pr15262-2.c: New test.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.5448&r2=2.5449
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-dfa.c.diff?cvsroot=gcc&r1=2.30&r2=2.31
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/tree-ssa-alias.c.diff?cvsroot=gcc&r1=2.33&r2=2.34
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.4286&r2=1.4287
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr15262-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.c-torture/execute/pr15262-2.c.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 15 Diego Novillo 2004-09-14 22:58:46 UTC
Fix: http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01498.html
Comment 16 Diego Novillo 2004-09-14 23:10:59 UTC
*** Bug 17489 has been marked as a duplicate of this bug. ***