This fixes the easy piece of PR19831 - removal of "dead" calls to allocation functions (thus, memleaks). It looks like this should be valid even for corner-cases like
errno = 0; malloc (whatever); if (errno != 0) ...; as there is no standard defined value for whatever that is guaranteed to make malloc fail. Thus you'd have to test its return value in which case it wouldn't be dead (the above woulds simply always appear as if malloc worked). In practice the patch will do something about alloca at most, unless, of course, you have a memleak that you don't use ;) Followups will eventually lead us to remove a malloc/free pair as well (a free currently constitutes as a use of the allocated storage). Bootstrap and regtest running on x86_64-unknown-linux-gnu. Richard. 2011-09-07 Richard Guenther <rguent...@suse.de> PR tree-optimization/19831 * tree-ssa-dce.c (mark_stmt_if_obviously_necessary): Do not mark allocation functions as necessary. * gcc.dg/tree-ssa/ssa-dce-8.c: New testcase. Index: gcc/tree-ssa-dce.c =================================================================== *** gcc/tree-ssa-dce.c (revision 178633) --- gcc/tree-ssa-dce.c (working copy) *************** mark_stmt_if_obviously_necessary (gimple *** 299,315 **** return; case GIMPLE_CALL: ! /* Most, but not all function calls are required. Function calls that ! produce no result and have no side effects (i.e. const pure ! functions) are unnecessary. */ ! if (gimple_has_side_effects (stmt)) ! { ! mark_stmt_necessary (stmt, true); return; ! } ! if (!gimple_call_lhs (stmt)) ! return; ! break; case GIMPLE_DEBUG: /* Debug temps without a value are not useful. ??? If we could --- 299,327 ---- return; case GIMPLE_CALL: ! { ! tree callee = gimple_call_fndecl (stmt); ! if (callee != NULL_TREE ! && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) ! switch (DECL_FUNCTION_CODE (callee)) ! { ! case BUILT_IN_MALLOC: ! case BUILT_IN_CALLOC: ! case BUILT_IN_ALLOCA: ! return; ! } ! /* Most, but not all function calls are required. Function calls that ! produce no result and have no side effects (i.e. const pure ! functions) are unnecessary. */ ! if (gimple_has_side_effects (stmt)) ! { ! mark_stmt_necessary (stmt, true); ! return; ! } ! if (!gimple_call_lhs (stmt)) return; ! break; ! } case GIMPLE_DEBUG: /* Debug temps without a value are not useful. ??? If we could Index: gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-8.c =================================================================== *** gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-8.c (revision 0) --- gcc/testsuite/gcc.dg/tree-ssa/ssa-dce-8.c (revision 0) *************** *** 0 **** --- 1,12 ---- + /* { dg-do compile } */ + /* { dg-options "-O -fdump-tree-optimized" } */ + + int main() + { + int *p = __builtin_malloc (4); + *p = 4; + return 0; + } + + /* { dg-final { scan-tree-dump-not "malloc" "optimized" } } */ + /* { dg-final { cleanup-tree-dump "optimized" } } */