Designate the widest case label to be the default label
authorPatrick Palka <ppalka@gcc.gnu.org>
Thu, 4 Aug 2016 18:11:08 +0000 (18:11 +0000)
committerPatrick Palka <ppalka@gcc.gnu.org>
Thu, 4 Aug 2016 18:11:08 +0000 (18:11 +0000)
gcc/ChangeLog:

* gimple.c (preprocess_case_label_vec_for_gimple): When the case
labels are exhaustive, designate the label with the widest
range to be the default label.

gcc/testsuite/ChangeLog:

* gcc.dg/switch-10.c: New test.

From-SVN: r239146

gcc/ChangeLog
gcc/gimple.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/switch-10.c [new file with mode: 0644]

index d0bd7dd2291574a8d0c2e2b9cf86c46469ee231c..6c1e81692a9e0e6983e974f814b72602070345d6 100644 (file)
@@ -1,3 +1,9 @@
+2016-08-04  Patrick Palka  <ppalka@gcc.gnu.org>
+
+       * gimple.c (preprocess_case_label_vec_for_gimple): When the case
+       labels are exhaustive, designate the label with the widest
+       range to be the default label.
+
 2016-08-04  Andrew Pinski  <apinski@cavium.com>
 
        * config/aarch64/aarch64.c (thunderx_vector_cost): New variable.
index e275dfc3c520d247565362b7e48f9de0230f91f4..fc81e52e776f8f2ea234baadc4e9f0f389560ab9 100644 (file)
@@ -2946,18 +2946,30 @@ preprocess_case_label_vec_for_gimple (vec<tree> labels,
            high = CASE_LOW (labels[len - 1]);
          if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
            {
+             tree widest_label = labels[0];
              for (i = 1; i < len; i++)
                {
                  high = CASE_LOW (labels[i]);
                  low = CASE_HIGH (labels[i - 1]);
                  if (!low)
                    low = CASE_LOW (labels[i - 1]);
+
+                 if (CASE_HIGH (labels[i]) != NULL_TREE
+                     && (CASE_HIGH (widest_label) == NULL_TREE
+                         || wi::gtu_p (wi::sub (CASE_HIGH (labels[i]),
+                                                CASE_LOW (labels[i])),
+                                       wi::sub (CASE_HIGH (widest_label),
+                                                CASE_LOW (widest_label)))))
+                   widest_label = labels[i];
+
                  if (wi::add (low, 1) != high)
                    break;
                }
              if (i == len)
                {
-                 tree label = CASE_LABEL (labels[0]);
+                 /* Designate the label with the widest range to be the
+                    default label.  */
+                 tree label = CASE_LABEL (widest_label);
                  default_case = build_case_label (NULL_TREE, NULL_TREE,
                                                   label);
                }
index 7c92a22a924093e4fba71382854db9b65af51429..58672ba83a05c8f7cfdb04f67a8eb15ffb1a2f5a 100644 (file)
@@ -1,3 +1,7 @@
+2016-08-04  Patrick Palka  <ppalka@gcc.gnu.org>
+
+       * gcc.dg/switch-10.c: New test.
+
 2016-08-04  James Greenhalgh  <james.greenhalgh@arm.com>
 
        * gcc.c-torture/execute/pr70903.c: Remove duplicate test body.
diff --git a/gcc/testsuite/gcc.dg/switch-10.c b/gcc/testsuite/gcc.dg/switch-10.c
new file mode 100644 (file)
index 0000000..0ffc9eb
--- /dev/null
@@ -0,0 +1,22 @@
+/* { dg-options "-O2 -fdump-tree-cfg" }  */
+/* { dg-final { scan-tree-dump "case 0:" "cfg" } }  */
+/* { dg-final { scan-tree-dump-not "case 1 ... 255:" "cfg" } }  */
+#include <stdint.h>
+
+void foo (void);
+void bar (void);
+
+void
+test (uint8_t ch)
+{
+  switch (ch)
+   {
+   case 0:
+     foo ();
+     break;
+
+   case 1 ... 255:
+     bar ();
+     break;
+   }
+}