Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST in fold_stmt_1
authorAndrew Pinski <apinski@marvell.com>
Tue, 23 Nov 2021 01:08:55 +0000 (01:08 +0000)
committerAndrew Pinski <apinski@marvell.com>
Tue, 23 Nov 2021 09:55:47 +0000 (09:55 +0000)
This is a new version of the patch to fix PR 102216.
Instead of doing the canonicalization inside forwprop, Richi
mentioned we should do it inside fold_stmt_1 and that is what
this patch does.

PR tree-optimization/102216

gcc/ChangeLog:

* gimple-fold.c (fold_stmt_1): Add canonicalization
of "&MEM[ssa_n, CST]" to "ssa_n p+ CST", note this
can only be done if !in_place.

gcc/testsuite/ChangeLog:

* g++.dg/tree-ssa/pr102216-1.C: New test.
* g++.dg/tree-ssa/pr102216-2.C: New test.

gcc/gimple-fold.c
gcc/testsuite/g++.dg/tree-ssa/pr102216-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/tree-ssa/pr102216-2.C [new file with mode: 0644]

index ad9703e..73f090b 100644 (file)
@@ -6061,6 +6061,28 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
          if (REFERENCE_CLASS_P (*lhs)
              && maybe_canonicalize_mem_ref_addr (lhs))
            changed = true;
+         /* Canonicalize &MEM[ssa_n, CST] to ssa_n p+ CST.
+            This cannot be done in maybe_canonicalize_mem_ref_addr
+            as the gimple now has two operands rather than one.
+            The same reason why this can't be done in
+            maybe_canonicalize_mem_ref_addr is the same reason why
+            this can't be done inplace.  */
+         if (!inplace && TREE_CODE (*rhs) == ADDR_EXPR)
+           {
+             tree inner = TREE_OPERAND (*rhs, 0);
+             if (TREE_CODE (inner) == MEM_REF
+                 && TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME
+                 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST)
+               {
+                 tree ptr = TREE_OPERAND (inner, 0);
+                 tree addon = TREE_OPERAND (inner, 1);
+                 addon = fold_convert (sizetype, addon);
+                 gimple_assign_set_rhs_with_ops (gsi, POINTER_PLUS_EXPR,
+                                                 ptr, addon);
+                 changed = true;
+                 stmt = gsi_stmt (*gsi);
+               }
+           }
        }
       else
        {
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216-1.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216-1.C
new file mode 100644 (file)
index 0000000..21f7f67
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+void link_error ();
+void g ()
+{
+  const char **language_names;
+
+  language_names = new const char *[6];
+
+  const char **language_names_p = language_names;
+
+  language_names_p++;
+  language_names_p++;
+  language_names_p++;
+
+  if ( (language_names_p) - (language_names+3) != 0)
+    link_error();
+  delete[] language_names;
+}
+/* We should have removed the link_error on the gimple level as GCC should
+   be able to tell that language_names_p is the same as language_names+3.  */
+/* { dg-final { scan-tree-dump-times "link_error" 0 "optimized" } } */
diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr102216-2.C b/gcc/testsuite/g++.dg/tree-ssa/pr102216-2.C
new file mode 100644 (file)
index 0000000..8d351a9
--- /dev/null
@@ -0,0 +1,45 @@
+/* { dg-options "-O2 -Wall" } */
+#include <algorithm>
+
+static inline bool
+compare_cstrings (const char *str1, const char *str2)
+{
+  return str1 < str2;
+}
+
+void
+add_set_language_command ()
+{
+  static const char **language_names;
+
+  language_names = new const char *[6];
+
+  language_names[0] = "auto";
+  language_names[1] = "local";
+  language_names[2] = "unknown";
+
+  const char **language_names_p = language_names;
+  /* language_names_p == &language_names[0].  */
+  language_names_p++;
+  /* language_names_p == &language_names[1].  */
+  language_names_p++;
+  /* language_names_p == &language_names[2].  */
+  language_names_p++;
+  /* language_names_p == &language_names[3].  */
+
+  const char **sort_begin;
+
+  if (0)
+    sort_begin = &language_names[3];
+  else
+    sort_begin = language_names_p;
+
+  language_names[3] = "";
+  language_names[4] = "";
+  language_names[5] = NULL;
+
+  /* There should be no warning associated with this std::sort as
+     sort_begin != &language_names[5] and GCC should be able to figure
+     that out.  */
+  std::sort (sort_begin, &language_names[5], compare_cstrings);
+}