From: Richard Guenther Date: Wed, 1 Jul 2009 12:27:33 +0000 (+0000) Subject: re PR tree-optimization/19831 (Missing DSE/malloc/free optimization) X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=14c41b9bb9c2918a0727d6dd43b96c2df0b3bcf8;p=platform%2Fupstream%2Fgcc.git re PR tree-optimization/19831 (Missing DSE/malloc/free optimization) 2009-07-01 Richard Guenther PR tree-optimization/19831 * tree-ssa-dce.c (propagate_necessity): Calls to functions that only act as barriers do not make any previous stores necessary. * tree-ssa-structalias.c (handle_lhs_call): Delay making HEAP variables global, do not add a constraint from nonlocal. (find_func_aliases): Handle escapes through return statements. (compute_points_to_sets): Make escaped HEAP variables global. * gcc.dg/tree-ssa/20041122-1.c: Enable TBAA, scan FRE dump, make allocated memory escape. Un-XFAIL. * gcc.dg/vect/pr21591.c: Make allocated memory escape. * gcc.dg/vect/pr31699.c: Likewise. * gcc.dg/tree-ssa/ssa-dce-7.c: New testcase. libmudflap/ * testsuite/libmudflap.c/fail11-frag.c: Make allocated memory escape. * testsuite/libmudflap.c/fail12-frag.c: Likewise. * testsuite/libmudflap.c/fail16-frag.c: Likewise. * testsuite/libmudflap.c/fail31-frag.c: Likewise. From-SVN: r149140 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 6891178..205ca44 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2009-07-01 Richard Guenther + + PR tree-optimization/19831 + * tree-ssa-dce.c (propagate_necessity): Calls to functions + that only act as barriers do not make any previous stores + necessary. + * tree-ssa-structalias.c (handle_lhs_call): Delay making + HEAP variables global, do not add a constraint from nonlocal. + (find_func_aliases): Handle escapes through return statements. + (compute_points_to_sets): Make escaped HEAP variables global. + 2009-07-01 Paolo Bonzini PR bootstrap/40597 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 513a973..e929100 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,12 @@ +2009-07-01 Richard Guenther + + PR tree-optimization/19831 + * gcc.dg/tree-ssa/20041122-1.c: Enable TBAA, scan FRE dump, + make allocated memory escape. Un-XFAIL. + * gcc.dg/vect/pr21591.c: Make allocated memory escape. + * gcc.dg/vect/pr31699.c: Likewise. + * gcc.dg/tree-ssa/ssa-dce-7.c: New testcase. + 2009-06-30 Jakub Jelinek PR c++/40566 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/20041122-1.c b/gcc/testsuite/gcc.dg/tree-ssa/20041122-1.c index d72d133..6007949 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/20041122-1.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/20041122-1.c @@ -1,6 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O1 -fdump-tree-dom2" } */ - +/* { dg-options "-O1 -fstrict-aliasing -fdump-tree-fre" } */ __extension__ typedef __SIZE_TYPE__ size_t; extern void *xmalloc (size_t) __attribute__ ((__malloc__)); @@ -17,10 +16,10 @@ struct basic_block_def typedef struct basic_block_def *basic_block; extern int n_basic_blocks; extern edge frob (); -void -find_unreachable_blocks (int frobit) +basic_block * +find_unreachable_blocks (void) { - basic_block *tos, *worklist, bb; + basic_block *tos, *worklist; tos = worklist = xmalloc (sizeof (basic_block) * n_basic_blocks); edge e = frob(); if (!(e->dest->flags & 4)) @@ -28,11 +27,12 @@ find_unreachable_blocks (int frobit) e->dest->flags |= 4; *tos++ = e->dest; } + return worklist; } /* If the aliasing code does its job properly, then we should be able to determine that modifying e->dest->flags does not - modify e or e->dest. The net result is that we only need one - load of e->dest. */ -/* { dg-final { scan-tree-dump-times "->dest" 1 "dom2" { xfail *-*-* } } } */ -/* { dg-final { cleanup-tree-dump "dom2" } } */ + modify e or e->dest if we can assert strict-aliasing rules. + The net result is that we only need one load of e->dest. */ +/* { dg-final { scan-tree-dump-times "->dest" 1 "fre" } } */ +/* { dg-final { cleanup-tree-dump "fre" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-7.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-7.c new file mode 100644 index 0000000..792dfb0 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-7.c @@ -0,0 +1,33 @@ +/* { dg-do link } */ +/* { dg-options "-O -fdump-tree-optimized" } */ + +extern void link_error (void); +void foo(int n) +{ + int * f = (int*) __builtin_malloc (n * sizeof (int)); + int * ff = (int*) __builtin_malloc (n * sizeof (int)); + int i; + + for (i = 0; i < n; ++i) + { + f[i] = 1; + ff[i] = 2; + if (f[i] != 1) + link_error (); + if (ff[i] != 2) + link_error (); + } + + __builtin_free (f); + __builtin_free (ff); +} +int main() +{ + return 0; +} + +/* We should have removed the calls to link_error () and all stores + to the allocated memory. */ + +/* { dg-final { scan-tree-dump-times "\\\*D" 0 "optimized" } } */ +/* { dg-final { cleanup-tree-dump "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/pr21591.c b/gcc/testsuite/gcc.dg/vect/pr21591.c index 8c3bef4..4257777 100644 --- a/gcc/testsuite/gcc.dg/vect/pr21591.c +++ b/gcc/testsuite/gcc.dg/vect/pr21591.c @@ -10,6 +10,8 @@ struct a struct a *malloc1(__SIZE_TYPE__) __attribute__((malloc)); void free(void*); +struct a *p, *q, *r; + void f(void) { struct a *a = malloc1(sizeof(struct a)); @@ -26,9 +28,9 @@ void f(void) { a->a1[i] = b->a1[i] + c->a1[i]; } - free(a); - free(b); - free(c); + p = a; + q = b; + r = c; } /* { dg-final { scan-tree-dump-times "vectorized 2 loops" 1 "vect" } } */ diff --git a/gcc/testsuite/gcc.dg/vect/pr31699.c b/gcc/testsuite/gcc.dg/vect/pr31699.c index 4f9cf58..6015d5c 100644 --- a/gcc/testsuite/gcc.dg/vect/pr31699.c +++ b/gcc/testsuite/gcc.dg/vect/pr31699.c @@ -7,13 +7,15 @@ float x[256]; __attribute__ ((noinline)) -void foo(void) +double *foo(void) { double *z = malloc (sizeof(double) * 256); int i; for (i=0; i<256; ++i) z[i] = x[i] + 1.0f; + + return z; } diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c index 8522c5c..6be298e 100644 --- a/gcc/tree-ssa-dce.c +++ b/gcc/tree-ssa-dce.c @@ -676,8 +676,19 @@ propagate_necessity (struct edge_list *el) if (is_gimple_call (stmt)) { + tree callee = gimple_call_fndecl (stmt); unsigned i; + /* Calls to functions that are merely acting as barriers + or that only store to memory do not make any previous + stores necessary. */ + if (callee != NULL_TREE + && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL + && (DECL_FUNCTION_CODE (callee) == BUILT_IN_MEMSET + || DECL_FUNCTION_CODE (callee) == BUILT_IN_MALLOC + || DECL_FUNCTION_CODE (callee) == BUILT_IN_FREE)) + continue; + /* Calls implicitly load from memory, their arguments in addition may explicitly perform memory loads. */ mark_all_reaching_defs_necessary (stmt); diff --git a/gcc/tree-ssa-structalias.c b/gcc/tree-ssa-structalias.c index ad5482a..84edf00 100644 --- a/gcc/tree-ssa-structalias.c +++ b/gcc/tree-ssa-structalias.c @@ -3473,7 +3473,9 @@ handle_lhs_call (tree lhs, int flags, VEC(ce_s, heap) *rhsc) { varinfo_t vi; vi = make_constraint_from_heapvar (get_vi_for_tree (lhs), "HEAP"); - make_copy_constraint (vi, nonlocal_id); + /* We delay marking allocated storage global until we know if + it escapes. */ + vi->is_global_var = 0; } else if (VEC_length (ce_s, rhsc) > 0) { @@ -3910,6 +3912,13 @@ find_func_aliases (gimple origt) { make_escape_constraint (gimple_assign_rhs1 (t)); } + /* Handle escapes through return. */ + else if (gimple_code (t) == GIMPLE_RETURN + && gimple_return_retval (t) != NULL_TREE + && could_have_pointers (gimple_return_retval (t))) + { + make_escape_constraint (gimple_return_retval (t)); + } /* Handle asms conservatively by adding escape constraints to everything. */ else if (gimple_code (t) == GIMPLE_ASM) { @@ -5350,6 +5359,7 @@ compute_points_to_sets (void) struct scc_info *si; basic_block bb; unsigned i; + varinfo_t vi; timevar_push (TV_TREE_PTA); @@ -5447,6 +5457,13 @@ compute_points_to_sets (void) points-to solution queries. */ cfun->gimple_df->escaped.escaped = 0; + /* Mark escaped HEAP variables as global. */ + for (i = 0; VEC_iterate (varinfo_t, varmap, i, vi); ++i) + if (vi->is_heap_var + && !vi->is_global_var) + vi->is_global_var = pt_solution_includes (&cfun->gimple_df->escaped, + vi->decl); + /* Compute the points-to sets for pointer SSA_NAMEs. */ for (i = 0; i < num_ssa_names; ++i) { diff --git a/libmudflap/ChangeLog b/libmudflap/ChangeLog index 11d6f52..7503165 100644 --- a/libmudflap/ChangeLog +++ b/libmudflap/ChangeLog @@ -1,3 +1,12 @@ +2009-07-01 Richard Guenther + + PR tree-optimization/19831 + * testsuite/libmudflap.c/fail11-frag.c: Make allocated memory + escape. + * testsuite/libmudflap.c/fail12-frag.c: Likewise. + * testsuite/libmudflap.c/fail16-frag.c: Likewise. + * testsuite/libmudflap.c/fail31-frag.c: Likewise. + 2009-06-30 Richard Sandiford * testsuite/lib/libmudflap.exp (libmudflap-init): Don't add "." diff --git a/libmudflap/testsuite/libmudflap.c/fail11-frag.c b/libmudflap/testsuite/libmudflap.c/fail11-frag.c index 72038fd..ebd1db9 100644 --- a/libmudflap/testsuite/libmudflap.c/fail11-frag.c +++ b/libmudflap/testsuite/libmudflap.c/fail11-frag.c @@ -1,11 +1,12 @@ #include #include #include +char *y; int main () { int i = 10; char *x = (char *) malloc (i * sizeof (char)); - +y = x; while (i--) { ++x; diff --git a/libmudflap/testsuite/libmudflap.c/fail12-frag.c b/libmudflap/testsuite/libmudflap.c/fail12-frag.c index da8bfb7..46dbdb23 100644 --- a/libmudflap/testsuite/libmudflap.c/fail12-frag.c +++ b/libmudflap/testsuite/libmudflap.c/fail12-frag.c @@ -1,11 +1,12 @@ #include #include #include +int *y; int main () { int i = 10; int *x = (int *) malloc (i * sizeof (int)); - +y = x; while (i--) { ++x; diff --git a/libmudflap/testsuite/libmudflap.c/fail16-frag.c b/libmudflap/testsuite/libmudflap.c/fail16-frag.c index 317e274..6ac6187 100644 --- a/libmudflap/testsuite/libmudflap.c/fail16-frag.c +++ b/libmudflap/testsuite/libmudflap.c/fail16-frag.c @@ -1,6 +1,7 @@ #include #include #include +void *p; int main () { struct base { @@ -15,7 +16,7 @@ struct derived { struct base *bp; bp = (struct base *) malloc (sizeof (struct base));; - +p = bp; bp->basic = 10; ((struct derived *)bp)->extra = 'x'; return 0; diff --git a/libmudflap/testsuite/libmudflap.c/fail31-frag.c b/libmudflap/testsuite/libmudflap.c/fail31-frag.c index b15056c..bd9a903 100644 --- a/libmudflap/testsuite/libmudflap.c/fail31-frag.c +++ b/libmudflap/testsuite/libmudflap.c/fail31-frag.c @@ -8,11 +8,12 @@ int main () int z = h (4, 10); return 0; } - +int *p; int h (int i, int j) { int k[i]; k[j] = i; + p = k; return j; }