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; }
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.
After disabling the lowering I get: *&locp->nextc = str; return locp->nextc; which is right but causes not code generation.
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.
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.
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; }
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; }
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.
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.
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
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.
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
*** Bug 17461 has been marked as a duplicate of this bug. ***
*** Bug 17484 has been marked as a duplicate of this bug. ***
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
Fix: http://gcc.gnu.org/ml/gcc-patches/2004-09/msg01498.html
*** Bug 17489 has been marked as a duplicate of this bug. ***