This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] [PR81810] unused strcpy to a local buffer not eliminated


Hi ,
This patch include fix for PR81810
Bootstrapped/regtested on x86_64-linux, ok for trunk?

Thanks
./Kamlesh

2019-08-21  Kamlesh Kumar   <kamleshbhalui@gmail.com>

       PR tree-optimization/81810
       * tree-ssa-dse.c (dse_dom_walker::dse_optimize_stmt): Added
       BUILT_IN_STRCPY to consider for dse.
       (maybe_trim_memstar_call): Likewise.
       (initialize_ao_ref_for_dse): Likewise.
       * gcc.dg/tree-ssa/pr81810.c: New testcase.


diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c
index ba67884a825..dc4da4c9730 100644
--- a/gcc/tree-ssa-dse.c
+++ b/gcc/tree-ssa-dse.c
@@ -115,8 +115,11 @@ initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write)
        case BUILT_IN_MEMSET_CHK:
        case BUILT_IN_STRNCPY:
        case BUILT_IN_STRNCPY_CHK:
+       case BUILT_IN_STRCPY:
          {
-           tree size = gimple_call_arg (stmt, 2);
+           tree size = NULL;
+           if (gimple_call_num_args (stmt) > 2)
+               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;
@@ -470,6 +473,7 @@ 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_STRCPY:
     case BUILT_IN_MEMCPY_CHK:
     case BUILT_IN_MEMMOVE_CHK:
     case BUILT_IN_STRNCPY_CHK:
@@ -966,6 +970,7 @@ 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_STRCPY:
        case BUILT_IN_MEMSET:
        case BUILT_IN_MEMCPY_CHK:
        case BUILT_IN_MEMMOVE_CHK:
@@ -975,11 +980,14 @@ dse_dom_walker::dse_optimize_stmt
(gimple_stmt_iterator *gsi)
            /* Occasionally calls with an explicit length of zero
               show up in the IL.  It's pointless to do analysis
               on them, they're trivially dead.  */
-           tree size = gimple_call_arg (stmt, 2);
-           if (integer_zerop (size))
+           if (gimple_call_num_args (stmt) > 2)
              {
-               delete_dead_or_redundant_call (gsi, "dead");
-               return;
+               tree size = gimple_call_arg (stmt, 2);
+               if (integer_zerop (size))
+                 {
+                   delete_dead_or_redundant_call (gsi, "dead");
+                   return;
+                 }
              }

            /* If this is a memset call that initializes an object
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr81810.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr81810.c
new file mode 100644
index 00000000000..89cf36a1367
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr81810.c
@@ -0,0 +1,25 @@
+/* PR tree-optimization/81810
+   { dg-do compile }
+   { dg-options "-O2 -fdump-tree-optimized" } */
+
+void f (const void *p, unsigned n)
+{
+  char a[8];
+  __builtin_memcpy (a, p, n);
+}
+
+void g (const char *s)
+{
+  char a[8];
+  __builtin_strcpy (a, s);
+}
+
+void h (const char *s)
+{
+  char a[8];
+  __builtin_strncpy (a, s, sizeof a);
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_memcpy" "optimized" } }
+   { dg-final { scan-tree-dump-not "__builtin_strcpy" "optimized" } }
+   { dg-final { scan-tree-dump-not "__builtin_strncpy" "optimized" } } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]