Hello,

This patch fixes PR53881 by making group_case_labels_stmt look at the
CFG instead of relying on label equality.

Bootstrapped&tested on powerpc64-unknown-linux-gnu. OK?

Ciao!
Steven
gcc/
        PR tree-optimization/53881
        * tree-cfg.c (group_case_labels_stmt): Look up the basic block
        for each label and compare them instead of labels.

testsuite/
        PR tree-optimization/53881
        * gcc.dg/pr53881.c: Renamed to ...
        * gcc.dg/pr53881-1.c: ... this.
        * gcc.dg/pr53881-2.c: New test.

Index: tree-cfg.c
===================================================================
--- tree-cfg.c  (revision 189758)
+++ tree-cfg.c  (working copy)
@@ -1332,8 +1332,7 @@ group_case_labels_stmt (gimple stmt)
 {
   int old_size = gimple_switch_num_labels (stmt);
   int i, j, new_size = old_size;
-  tree default_case = NULL_TREE;
-  tree default_label = NULL_TREE;
+  basic_block default_bb = NULL;
   bool has_default;
 
   /* The default label is always the first case in a switch
@@ -1342,8 +1341,8 @@ group_case_labels_stmt (gimple stmt)
   if (!CASE_LOW (gimple_switch_default_label (stmt))
       && !CASE_HIGH (gimple_switch_default_label (stmt)))
     {
-      default_case = gimple_switch_default_label (stmt);
-      default_label = CASE_LABEL (default_case);
+      tree default_case = gimple_switch_default_label (stmt);
+      default_bb = label_to_block (CASE_LABEL (default_case));
       has_default = true;
     }
   else
@@ -1356,15 +1355,17 @@ group_case_labels_stmt (gimple stmt)
     i = 0;
   while (i < old_size)
     {
-      tree base_case, base_label, base_high;
+      tree base_case, base_high;
+      basic_block base_bb;
+
       base_case = gimple_switch_label (stmt, i);
 
       gcc_assert (base_case);
-      base_label = CASE_LABEL (base_case);
+      base_bb = label_to_block (CASE_LABEL (base_case));
 
       /* Discard cases that have the same destination as the
         default case.  */
-      if (base_label == default_label)
+      if (base_bb == default_bb)
        {
          gimple_switch_set_label (stmt, i, NULL_TREE);
          i++;
@@ -1383,13 +1384,13 @@ group_case_labels_stmt (gimple stmt)
       while (i < old_size)
        {
          tree merge_case = gimple_switch_label (stmt, i);
-         tree merge_label = CASE_LABEL (merge_case);
+         basic_block merge_bb = label_to_block (CASE_LABEL (merge_case));
          double_int bhp1 = double_int_add (tree_to_double_int (base_high),
                                            double_int_one);
 
          /* Merge the cases if they jump to the same place,
             and their ranges are consecutive.  */
-         if (merge_label == base_label
+         if (merge_bb == base_bb
              && double_int_equal_p (tree_to_double_int (CASE_LOW (merge_case)),
                                     bhp1))
            {
Index: testsuite/gcc.dg/pr53881-2.c
===================================================================
--- testsuite/gcc.dg/pr53881-2.c        (revision 0)
+++ testsuite/gcc.dg/pr53881-2.c        (revision 0)
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+int a,b,c;
+void
+fn1 ()
+{
+  switch (a)
+    {
+    case 0:
+    case 10:
+      b=c;
+out_bcon:
+      break;
+    case 3:
+      goto out_bcon;
+    }
+}

Reply via email to