diff mbox

Add st[pr]ncpy to stmt_kills_ref_p

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

Commit Message

Marc Glisse April 29, 2017, 7:17 a.m. UTC
Hello,

this patch seems rather trivial, once one knows that those functions 
always write exactly n characters (they fill with 0 as needed at the end).

Bootstrap+regtest on powerpc64le-unknown-linux-gnu.

2017-05-02  Marc Glisse  <marc.glisse@inria.fr>

 	PR tree-optimization/80487
gcc/
 	* tree-ssa-alias.c (stmt_kills_ref_p): Handle stpncpy and strncpy.

gcc/testsuite/
 	* gcc.dg/tree-ssa/strncpy-1.c: New file.

Comments

Richard Biener April 29, 2017, 2:14 p.m. UTC | #1
On April 29, 2017 9:17:12 AM GMT+02:00, Marc Glisse <marc.glisse@inria.fr> wrote:
>Hello,
>
>this patch seems rather trivial, once one knows that those functions 
>always write exactly n characters (they fill with 0 as needed at the
>end).
>
>Bootstrap+regtest on powerpc64le-unknown-linux-gnu.

OK.

Richard.

>2017-05-02  Marc Glisse  <marc.glisse@inria.fr>
>
> 	PR tree-optimization/80487
>gcc/
> 	* tree-ssa-alias.c (stmt_kills_ref_p): Handle stpncpy and strncpy.
>
>gcc/testsuite/
> 	* gcc.dg/tree-ssa/strncpy-1.c: New file.
Martin Sebor April 30, 2017, 5:54 p.m. UTC | #2
On 04/29/2017 01:17 AM, Marc Glisse wrote:
> Hello,
>
> this patch seems rather trivial, once one knows that those functions
> always write exactly n characters (they fill with 0 as needed at the end).

Nice!  Now it just needs to be made to work the other way around
too, so the dead str{,n}cpy calls is eliminated:

   void sink (void*);

   void f (const char *s)
   {
     char a[256];

     __builtin_strncpy (a, s, sizeof a);
     __builtin_memset (a, 0, sizeof a);

     sink (a);
   }

I've opened bug 80576 for this small improvement.

Martin
Marc Glisse April 30, 2017, 5:59 p.m. UTC | #3
On Sun, 30 Apr 2017, Martin Sebor wrote:

> On 04/29/2017 01:17 AM, Marc Glisse wrote:
>> Hello,
>> 
>> this patch seems rather trivial, once one knows that those functions
>> always write exactly n characters (they fill with 0 as needed at the end).
>
> Nice!  Now it just needs to be made to work the other way around
> too, so the dead str{,n}cpy calls is eliminated:

I think I've seen related patches on the list, Prathamesh Kulkarni seems 
to be working on it.
diff mbox

Patch

Index: gcc/testsuite/gcc.dg/tree-ssa/strncpy-1.c
===================================================================
--- gcc/testsuite/gcc.dg/tree-ssa/strncpy-1.c	(nonexistent)
+++ gcc/testsuite/gcc.dg/tree-ssa/strncpy-1.c	(working copy)
@@ -0,0 +1,16 @@ 
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+
+void sink (void*);
+
+void f (const char *s)
+{
+  char a[256];
+
+  __builtin_memset (a, 0, sizeof a);   // redundant memset
+  __builtin_strncpy (a, s, sizeof a);
+
+  sink (a);
+}
+
+/* { dg-final { scan-tree-dump-not "memset" "optimized" } } */
Index: gcc/tree-ssa-alias.c
===================================================================
--- gcc/tree-ssa-alias.c	(revision 247405)
+++ gcc/tree-ssa-alias.c	(working copy)
@@ -2531,20 +2531,22 @@  stmt_kills_ref_p (gimple *stmt, ao_ref *
 	    }
 
 	  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:
+	  case BUILT_IN_STRNCPY:
+	  case BUILT_IN_STPNCPY:
 	    {
 	      /* For a must-alias check we need to be able to constrain
 		 the access properly.  */
 	      if (ref->max_size == -1)
 		return false;
 	      tree dest = gimple_call_arg (stmt, 0);
 	      tree len = gimple_call_arg (stmt, 2);
 	      if (!tree_fits_shwi_p (len))
 		return false;
 	      tree rbase = ref->base;