re PR tree-optimization/83444 (missing strlen optimization on a member array of a...
authorJakub Jelinek <jakub@redhat.com>
Tue, 19 Dec 2017 07:44:07 +0000 (08:44 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 19 Dec 2017 07:44:07 +0000 (08:44 +0100)
PR tree-optimization/83444
* tree-ssa-strlen.c (strlen_check_and_optimize_stmt): Optimize
character loads.

* gcc.dg/strlenopt-38.c: New test.

From-SVN: r255806

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

index 9d7875e3918633813c8d33c632544e4a281533fd..9c0419d83334317f5d0bea14cd55cf109c368b5f 100644 (file)
@@ -1,5 +1,9 @@
 2017-12-19  Jakub Jelinek  <jakub@redhat.com>
 
+       PR tree-optimization/83444
+       * tree-ssa-strlen.c (strlen_check_and_optimize_stmt): Optimize
+       character loads.
+
        PR ipa/82801
        PR ipa/83346
        * ipa-inline.c (flatten_remove_node_hook): New function.
index 5c375c1b8b6d67b915a4129604a5697c39a1f99e..1eb45e541e935cceb121f73ffe8be92772a8571b 100644 (file)
@@ -1,5 +1,8 @@
 2017-12-19  Jakub Jelinek  <jakub@redhat.com>
 
+       PR tree-optimization/83444
+       * gcc.dg/strlenopt-38.c: New test.
+
        PR ipa/82801
        PR ipa/83346
        * g++.dg/ipa/pr82801.C: New test.
diff --git a/gcc/testsuite/gcc.dg/strlenopt-38.c b/gcc/testsuite/gcc.dg/strlenopt-38.c
new file mode 100644 (file)
index 0000000..3b698f9
--- /dev/null
@@ -0,0 +1,38 @@
+/* PR tree-optimization/83444 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+/* { dg-final { scan-tree-dump-not "abort \\(\\)" "optimized" } } */
+
+#include "strlenopt.h"
+
+void
+foo (void)
+{
+  char a[5] = "012";
+  strcpy (a, "");
+  if (strlen (a) != 0)
+    abort ();
+}
+
+void
+bar (void)
+{
+  char a[5] = "012";
+  char b[7] = "";
+  strcpy (a, b);
+  if (strlen (a) != 0)
+    abort ();
+}
+
+struct S { char a[4]; char b[5]; char c[7]; };
+
+void
+baz (void)
+{
+  struct S s;
+  strcpy (s.b, "012");
+  strcpy (s.c, "");
+  strcpy (s.b, s.c);
+  if (s.b[0] != 0)
+    abort ();
+}
index 0386883a74902eedaca6466b9a08113a1179472f..a2d514cb35a954e86a33ac7a915297b92bac97a9 100644 (file)
@@ -3146,6 +3146,64 @@ strlen_check_and_optimize_stmt (gimple_stmt_iterator *gsi)
        else if (code == EQ_EXPR || code == NE_EXPR)
          fold_strstr_to_strncmp (gimple_assign_rhs1 (stmt),
                                  gimple_assign_rhs2 (stmt), stmt);
+       else if (gimple_assign_load_p (stmt)
+                && TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE
+                && TYPE_MODE (TREE_TYPE (lhs)) == TYPE_MODE (char_type_node)
+                && (TYPE_PRECISION (TREE_TYPE (lhs))
+                    == TYPE_PRECISION (char_type_node))
+                && !gimple_has_volatile_ops (stmt))
+         {
+           tree off = integer_zero_node;
+           unsigned HOST_WIDE_INT coff = 0;
+           int idx = -1;
+           tree rhs1 = gimple_assign_rhs1 (stmt);
+           if (code == MEM_REF)
+             {
+               idx = get_stridx (TREE_OPERAND (rhs1, 0));
+               off = TREE_OPERAND (rhs1, 1);
+             }
+           else
+             idx = get_addr_stridx (rhs1, NULL_TREE, &coff);
+           if (idx > 0)
+             {
+               strinfo *si = get_strinfo (idx);
+               if (si
+                   && si->nonzero_chars
+                   && TREE_CODE (si->nonzero_chars) == INTEGER_CST)
+                 {
+                   widest_int w1 = wi::to_widest (si->nonzero_chars);
+                   widest_int w2 = wi::to_widest (off) + coff;
+                   if (w1 == w2
+                       && si->full_string_p)
+                     {
+                       /* Reading the final '\0' character.  */
+                       tree zero = build_int_cst (TREE_TYPE (lhs), 0);
+                       gimple_set_vuse (stmt, NULL_TREE);
+                       gimple_assign_set_rhs_from_tree (gsi, zero);
+                       update_stmt (gsi_stmt (*gsi));
+                     }
+                   else if (w1 > w2)
+                     {
+                       /* Reading a character before the final '\0'
+                          character.  Just set the value range to ~[0, 0]
+                          if we don't have anything better.  */
+                       wide_int min, max;
+                       tree type = TREE_TYPE (lhs);
+                       enum value_range_type vr
+                         = get_range_info (lhs, &min, &max);
+                       if (vr == VR_VARYING
+                           || (vr == VR_RANGE
+                               && min == wi::min_value (TYPE_PRECISION (type),
+                                                        TYPE_SIGN (type))
+                               && max == wi::max_value (TYPE_PRECISION (type),
+                                                        TYPE_SIGN (type))))
+                         set_range_info (lhs, VR_ANTI_RANGE,
+                                         wi::zero (TYPE_PRECISION (type)),
+                                         wi::zero (TYPE_PRECISION (type)));
+                     }
+                 }
+             }
+         }
 
        if (strlen_to_stridx)
          {