https://gcc.gnu.org/g:aa935ce40a777eb0b4a4d3d2e03cf2efb4cf9619

commit r16-1067-gaa935ce40a777eb0b4a4d3d2e03cf2efb4cf9619
Author: Andrew Pinski <quic_apin...@quicinc.com>
Date:   Mon Jun 2 15:56:20 2025 -0700

    switch-conversion: Mark CSWTCH as mergeable [PR120451]
    
    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_apin...@quicinc.com>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/cswtch-6.c | 43 ++++++++++++++++++++++++++++++++
 gcc/tree-switch-conversion.cc            |  3 +++
 2 files changed, 46 insertions(+)

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
index 000000000000..d765a03aa195
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/cswtch-6.c
@@ -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" } } */
diff --git a/gcc/tree-switch-conversion.cc b/gcc/tree-switch-conversion.cc
index bd4de966892c..d0882879e61a 100644
--- a/gcc/tree-switch-conversion.cc
+++ b/gcc/tree-switch-conversion.cc
@@ -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,

Reply via email to