tree-ssa-dse.c (initialize_ao_ref_for_dse): Handle strncpy.
authorJeff Law <law@redhat.com>
Fri, 19 Jul 2019 17:04:51 +0000 (11:04 -0600)
committerJeff Law <law@gcc.gnu.org>
Fri, 19 Jul 2019 17:04:51 +0000 (11:04 -0600)
* tree-ssa-dse.c (initialize_ao_ref_for_dse): Handle
strncpy.  Drop some trivial dead code.
(maybe_trim_memstar_call): Handle strncpy.

* gcc.dg/tree-ssa/ssa-dse-37.c: New test.
* gcc.dg/tree-ssa/ssa-dse-38.c: New test.

From-SVN: r273606

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-37.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-38.c [new file with mode: 0644]
gcc/tree-ssa-dse.c

index 08f91ed32db3e323c9f2564872263d2fe4ad9c02..d8f60042ac1fda2fc37688032feb34dd00f60437 100644 (file)
@@ -1,3 +1,9 @@
+2019-07-19  Jeff Law  <law@redhat.com>
+
+       * tree-ssa-dse.c (initialize_ao_ref_for_dse): Handle
+       strncpy.  Drop some trivial dead code.
+       (maybe_trim_memstar_call): Handle strncpy.
+
 2019-07-19  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/91211
index 98fb40ddd96a9bb4e26621e282cedd9967d56423..ce8e3c781b984d82d23900d304c203408ef51a58 100644 (file)
@@ -1,3 +1,8 @@
+2019-07-19  Jeff Law  <law@redhat.com>
+
+       * gcc.dg/tree-ssa/ssa-dse-37.c: New test.
+       * gcc.dg/tree-ssa/ssa-dse-38.c: New test.
+
 2019-07-19  Richard Biener  <rguenther@suse.de>
 
        PR tree-optimization/91211
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-37.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-37.c
new file mode 100644 (file)
index 0000000..56251fc
--- /dev/null
@@ -0,0 +1,60 @@
+/* { dg-options "-O2 -fdump-tree-dse-details -fno-tree-fre" } */
+
+
+#ifndef SCOPE
+#define SCOPE
+#endif
+
+extern void frob (char *);
+
+void g (char *s)
+{
+  SCOPE char a[8];
+  __builtin_strncpy (a, s, sizeof a);
+  __builtin_memset (a, 0, sizeof a); 
+  frob (a);
+}
+
+void h (char *s)
+{
+  SCOPE char a[8];
+  __builtin_memset (a, 0, sizeof a); 
+  __builtin_strncpy (a, s, sizeof a);
+  frob (a);
+}
+
+void i (char *s)
+{
+  SCOPE char a[8];
+  __builtin_strncpy (a, s, sizeof a);
+  __builtin_memset (a, 0, sizeof a - 5); 
+  frob (a);
+}
+
+void j (char *s)
+{
+  SCOPE char a[8];
+  __builtin_memset (a, 0, sizeof a); 
+  __builtin_strncpy (a, s, sizeof a - 5);
+  frob (a);
+}
+
+void l (char *s)
+{
+  SCOPE char a[8];
+  __builtin_strncpy (a, s, sizeof a);
+  __builtin_memset (a + 2, 0, sizeof a - 2); 
+  frob (a);
+}
+
+void m (char *s)
+{
+  SCOPE char a[8];
+  __builtin_memset (a, 0, sizeof a); 
+  __builtin_strncpy (a + 2, s, sizeof a - 2);
+  frob (a);
+}
+
+/* { dg-final { scan-tree-dump-times "Deleted dead call" 2 "dse1" } } */
+/* { dg-final { scan-tree-dump-times "Trimming statement " 4 "dse1" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-38.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dse-38.c
new file mode 100644 (file)
index 0000000..7ae33bf
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-options "-O2 -fdump-tree-dse-details -fno-tree-fre" } */
+
+
+/* This changes the scope of the destination object and exposes
+   missed optimizations in DSE.  */
+#define SCOPE extern
+#include "ssa-dse-37.c"
+
+/* { dg-final { scan-tree-dump-times "Deleted dead call" 2 "dse1" { xfail *-*-* } } } */
+/* { dg-final { scan-tree-dump-times "Trimming statement " 4 "dse1" { xfail *-*-* } } } */
+
+
index 9bdcf9ae6affc8e48781b7118b5765a5fdc6fff6..5b7c4fc6d1acd07ae3708350faf89ccc68a01a8d 100644 (file)
@@ -113,10 +113,10 @@ initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write)
        case BUILT_IN_MEMCPY_CHK:
        case BUILT_IN_MEMMOVE_CHK:
        case BUILT_IN_MEMSET_CHK:
+       case BUILT_IN_STRNCPY:
+       case BUILT_IN_STRNCPY_CHK:
          {
-           tree size = NULL_TREE;
-           if (gimple_call_num_args (stmt) == 3)
-             size = gimple_call_arg (stmt, 2);
+           tree size = gimple_call_arg (stmt, 2);
            tree ptr = gimple_call_arg (stmt, 0);
            ao_ref_init_from_ptr_and_size (write, ptr, size);
            return true;
@@ -469,8 +469,10 @@ maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
     {
     case BUILT_IN_MEMCPY:
     case BUILT_IN_MEMMOVE:
+    case BUILT_IN_STRNCPY:
     case BUILT_IN_MEMCPY_CHK:
     case BUILT_IN_MEMMOVE_CHK:
+    case BUILT_IN_STRNCPY_CHK:
       {
        int head_trim, tail_trim;
        compute_trims (ref, live, &head_trim, &tail_trim, stmt);
@@ -966,9 +968,11 @@ dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator *gsi)
        {
        case BUILT_IN_MEMCPY:
        case BUILT_IN_MEMMOVE:
+       case BUILT_IN_STRNCPY:
        case BUILT_IN_MEMSET:
        case BUILT_IN_MEMCPY_CHK:
        case BUILT_IN_MEMMOVE_CHK:
+       case BUILT_IN_STRNCPY_CHK:
        case BUILT_IN_MEMSET_CHK:
          {
            /* Occasionally calls with an explicit length of zero