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.gli...@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. -- Marc Glisse
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;