]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
switch-conversion: Mark CSWTCH as mergeable [PR120451]
authorAndrew Pinski <quic_apinski@quicinc.com>
Mon, 2 Jun 2025 22:56:20 +0000 (15:56 -0700)
committerAndrew Pinski <quic_apinski@quicinc.com>
Tue, 3 Jun 2025 05:13:13 +0000 (22:13 -0700)
When we have a smallish CSWTCH, it could be placed in the rodata.cst16
section so it can be merged with other constants across TUs.

The fix is simple; just mark the decl as mergable (DECL_MERGEABLE).
DECL_MERGEABLE was added with r14-1500-g4d935f52b0d5c0 specifically
to improve these kind of decls.

PR tree-optimization/120451

gcc/ChangeLog:

* tree-switch-conversion.cc (switch_conversion::build_one_array): Mark
the newly created decl as mergable.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/cswtch-6.c: New test.

Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
gcc/testsuite/gcc.dg/tree-ssa/cswtch-6.c [new file with mode: 0644]
gcc/tree-switch-conversion.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cswtch-6.c b/gcc/testsuite/gcc.dg/tree-ssa/cswtch-6.c
new file mode 100644 (file)
index 0000000..d765a03
--- /dev/null
@@ -0,0 +1,43 @@
+/* PR tree-optimization/120451 */
+/* { dg-do compile { target elf } } */
+/* { dg-options "-O2" } */
+
+void foo (int, int);
+
+__attribute__((noinline, noclone)) void
+f1 (int v, int w)
+{
+  int i, j;
+  if (w)
+    {
+      i = 129;
+      j = i - 1;
+      goto lab;
+    }
+  switch (v)
+    {
+    case 170:
+      j = 7;
+      i = 27;
+      break;
+    case 171:
+      i = 8;
+      j = 122;
+      break;
+    case 172:
+      i = 21;
+      j = -19;
+      break;
+    case 173:
+      i = 18;
+      j = 17;
+      break;
+    default:
+      __builtin_abort ();
+    }
+
+ lab:
+  foo (i, j);
+}
+
+/* { dg-final { scan-assembler ".rodata.cst16" } } */
index bd4de966892cd58019e1ce8d60f1759901c91e3b..d0882879e61a495e7b8447eb23a0c792469cfa08 100644 (file)
@@ -1030,6 +1030,9 @@ switch_conversion::build_one_array (int num, tree arr_index_type,
       TREE_CONSTANT (decl) = 1;
       TREE_READONLY (decl) = 1;
       DECL_IGNORED_P (decl) = 1;
+      /* The decl is mergable since we don't take the address ever and
+        just reading from it. */
+      DECL_MERGEABLE (decl) = 1;
       if (offloading_function_p (cfun->decl))
        DECL_ATTRIBUTES (decl)
          = tree_cons (get_identifier ("omp declare target"), NULL_TREE,