diff mbox

free is a killer

Message ID alpine.DEB.2.02.1310260837400.32346@stedding.saclay.inria.fr
State New
Headers show

Commit Message

Marc Glisse Oct. 26, 2013, 7:15 a.m. UTC
Hello,

this patch teaches gcc that free kills the memory its argument points to. 
The equality test is probably too strict, I guess we can loosen it later 
(unless you have suggestions?).

Note that the corresponding code for BUILT_IN_MEMCPY and others seems 
suspicious to me, it looks like it is testing for equality between a 
pointer and a mem_ref, which is unlikely to happen.

Bootstrap+testsuite on x86_64-unknown-linux-gnu.

2013-10-27  Marc Glisse  <marc.glisse@inria.fr>

 	PR tree-optimization/19831
gcc/
 	* tree-ssa-alias.c (stmt_kills_ref_p_1): Handle BUILT_IN_FREE.

gcc/testsuite/
 	* gcc.dg/tree-ssa/alias-25.c: New file.

Comments

Jeff Law Oct. 28, 2013, 9:17 p.m. UTC | #1
On 10/26/13 01:15, Marc Glisse wrote:
> Hello,
>
> this patch teaches gcc that free kills the memory its argument points
> to. The equality test is probably too strict, I guess we can loosen it
> later (unless you have suggestions?).
>
> Note that the corresponding code for BUILT_IN_MEMCPY and others seems
> suspicious to me, it looks like it is testing for equality between a
> pointer and a mem_ref, which is unlikely to happen.
>
> Bootstrap+testsuite on x86_64-unknown-linux-gnu.
>
> 2013-10-27  Marc Glisse  <marc.glisse@inria.fr>
>
>      PR tree-optimization/19831
> gcc/
>      * tree-ssa-alias.c (stmt_kills_ref_p_1): Handle BUILT_IN_FREE.
>
> gcc/testsuite/
>      * gcc.dg/tree-ssa/alias-25.c: New file.
OK for the trunk.

I agree the MEM_REF* and VA_END cases look strange and I think they're 
wrong as well.  Did you happen to try fixing them and running the 
testsuite to see if anything fails?

ISTM your testcase could be tweaked to test conclusively if they're 
doing the right thing -- and regardless of what's found that test 
can/should make its way into the testsuite.

Jeff
diff mbox

Patch

Index: gcc/testsuite/gcc.dg/tree-ssa/alias-25.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/alias-25.c	(revision 0)
+++ gcc/testsuite/gcc.dg/tree-ssa/alias-25.c	(working copy)
@@ -0,0 +1,12 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+
+void f (long *p) {
+  *p = 42;
+  p[4] = 42;
+  __builtin_free (p);
+}
+
+/* { dg-final { scan-tree-dump-not "= 42" "optimized" } } */
+/* { dg-final { cleanup-tree-dump "optimized" } } */
+

Property changes on: gcc/testsuite/gcc.dg/tree-ssa/alias-25.c
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision URL
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Index: gcc/tree-ssa-alias.c
===================================================================
--- gcc/tree-ssa-alias.c	(revision 204088)
+++ gcc/tree-ssa-alias.c	(working copy)
@@ -2053,20 +2053,30 @@  stmt_kills_ref_p_1 (gimple stmt, ao_ref
 	}
     }
 
   if (is_gimple_call (stmt))
     {
       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_FREE:
+	    {
+	      tree ptr = gimple_call_arg (stmt, 0);
+	      tree base = ao_ref_base (ref);
+	      if (base && TREE_CODE (base) == MEM_REF
+		  && TREE_OPERAND (base, 0) == ptr)
+		return true;
+	      break;
+	    }
+
 	  case BUILT_IN_MEMCPY:
 	  case BUILT_IN_MEMPCPY:
 	  case BUILT_IN_MEMMOVE:
 	  case BUILT_IN_MEMSET:
 	  case BUILT_IN_MEMCPY_CHK:
 	  case BUILT_IN_MEMPCPY_CHK:
 	  case BUILT_IN_MEMMOVE_CHK:
 	  case BUILT_IN_MEMSET_CHK:
 	    {
 	      tree dest = gimple_call_arg (stmt, 0);