From 9fc3b39aa9656b1c626f6a6fed03646040b03122 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Mon, 23 Aug 2004 03:12:38 +0000 Subject: [PATCH] re PR tree-optimization/15262 ([tree-ssa] Alias analyzer cannot handle addressable fields) 2004-08-22 Andrew Pinski 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 * g++.dg/opt/pr14029.C: New test. * gcc.c-torture/execute/pr15262.c: New test. 2004-08-22 Andrew Pinski PR c++/14029 * typeck.c (build_unary_op): Use &a.b if the foldded lowered expression is not constant. From-SVN: r86396 --- gcc/ChangeLog | 8 ++++ gcc/c-typeck.c | 10 +++- gcc/cp/ChangeLog | 6 +++ gcc/cp/typeck.c | 5 ++ gcc/testsuite/ChangeLog | 5 ++ gcc/testsuite/g++.dg/opt/pr14029.C | 41 ++++++++++++++++ gcc/testsuite/gcc.c-torture/execute/pr15262.c | 48 +++++++++++++++++++ 7 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/opt/pr14029.C create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr15262.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 99864a89aa6..94756947a4e 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2004-08-22 Andrew Pinski + + 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 Richard Henderson PR 17075 diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index ad8acee710d..3530c20868a 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -2538,9 +2538,14 @@ build_unary_op (enum tree_code code, tree xarg, int flag) addr = fold (build2 (PLUS_EXPR, argtype, convert (argtype, addr), convert (argtype, byte_position (field)))); + + /* If the folded PLUS_EXPR is not a constant address, wrap + it in an ADDR_EXPR. */ + if (!TREE_CONSTANT (addr)) + addr = build1 (ADDR_EXPR, argtype, arg); } else - addr = build1 (code, argtype, arg); + addr = build1 (ADDR_EXPR, argtype, arg); if (TREE_CODE (arg) == COMPOUND_LITERAL_EXPR) TREE_INVARIANT (addr) = TREE_CONSTANT (addr) = 1; @@ -6310,7 +6315,8 @@ c_finish_return (tree retval) case ADDR_EXPR: inner = TREE_OPERAND (inner, 0); - while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r') + while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r' + && TREE_CODE (inner) != INDIRECT_REF) inner = TREE_OPERAND (inner, 0); if (DECL_P (inner) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index c0f488e94a2..e2b32126ce0 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2004-08-22 Andrew Pinski + + PR c++/14029 + * typeck.c (build_unary_op): Use &a.b if the foldded lowered + expression is not constant. + 2004-08-20 Mark Mitchell PR c++/17121 diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 01b969edb97..a614db58978 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -4123,6 +4123,11 @@ build_unary_op (enum tree_code code, tree xarg, int noconvert) addr = fold (build2 (PLUS_EXPR, argtype, rval, cp_convert (argtype, byte_position (field)))); + + /* If the folded PLUS_EXPR is not a constant address, wrap + it in an ADDR_EXPR. */ + if (!TREE_CONSTANT (addr)) + addr = build_address (arg); } if (TREE_CODE (argtype) == POINTER_TYPE diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 0bf7dfb0af5..23d0604c2de 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2004-08-22 Andrew Pinski + + * g++.dg/opt/pr14029.C: New test. + * gcc.c-torture/execute/pr15262.c: New test. + 2004-08-22 Tobias Schlueter * gfortran.dg/reduction.f90: Add checks with complex arguments. diff --git a/gcc/testsuite/g++.dg/opt/pr14029.C b/gcc/testsuite/g++.dg/opt/pr14029.C new file mode 100644 index 00000000000..1673edfd615 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/pr14029.C @@ -0,0 +1,41 @@ +// { dg-do run } +// { dg-options "-O2" } +// We used to mis-compile this testcase as we did not know that +// &a+offsetof(b,a) was the same as &a.b + +struct Iterator { + int * ptr; + + Iterator(int * i) : ptr(i) { } + void operator++() { ++ptr; } + int *const & base() const { return ptr; } +}; + + +Iterator find_7(Iterator first, Iterator last) +{ + int trip_count = (last.base() - first.base()) >> 1; + + for ( ; trip_count > 0 ; --trip_count) { + if (*first.ptr == 7) return first; + ++first; + + if (*first.ptr == 7) return first; + ++first; + } + + switch(last.base() - first.base()) { + case 1: + if (*first.ptr == 7) return first; + ++first; + case 0: + default: + return last; + } +} + +int main() { + int as[5] = {4,4,4,4,7}; + return (find_7(Iterator(&as[0]), Iterator(&as[5])).ptr == &as[5]); +}; + diff --git a/gcc/testsuite/gcc.c-torture/execute/pr15262.c b/gcc/testsuite/gcc.c-torture/execute/pr15262.c new file mode 100644 index 00000000000..2110f33a04a --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr15262.c @@ -0,0 +1,48 @@ +/* We used to mis-compile this testcase as we did not know that + &a+offsetof(b,a) was the same as &a.b */ +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; + *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; +} + -- 2.30.2