From 3feaea007d5df4fab5820e2e01fb175894ff2d75 Mon Sep 17 00:00:00 2001
From: Eric Botcazou <ebotcazou@adacore.com>
Date: Mon, 13 Mar 2006 14:18:24 +0000
Subject: [PATCH] re PR middle-end/18859 (ACATS ICE c37305a at -O0: in
 tree_low_cst, at tree.c:3839)

	PR middle-end/18859
	* gimplify.c (gimplify_switch_expr): Discard empty ranges.
	* stmt.c (expand_case): Likewise.

From-SVN: r112000
---
 gcc/ChangeLog                   |  6 ++++++
 gcc/gimplify.c                  | 32 +++++++++++++++++++++++---------
 gcc/stmt.c                      | 16 ++++++++++++----
 gcc/testsuite/ChangeLog         |  4 ++++
 gcc/testsuite/gcc.dg/switch-9.c | 25 +++++++++++++++++++++++++
 5 files changed, 70 insertions(+), 13 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/switch-9.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index ab470b709cda..b7a2f5d09ff4 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2006-03-13  Eric Botcazou  <ebotcazou@adacore.com>
+
+	PR middle-end/18859
+	* gimplify.c (gimplify_switch_expr): Discard empty ranges.
+	* stmt.c (expand_case): Likewise.
+
 2006-03-13  Zdenek Dvorak <dvorakz@suse.cz>
 
 	PR rtl-optimization/26254
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index c9498eef95af..506ad1896842 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -1327,20 +1327,34 @@ gimplify_switch_expr (tree *expr_p, tree *pre_p)
       labels = gimplify_ctxp->case_labels;
       gimplify_ctxp->case_labels = saved_labels;
 
-      len = VEC_length (tree, labels);
-
-      for (i = 0; i < len; ++i)
+      i = 0;
+      while (i < VEC_length (tree, labels))
 	{
-	  tree t = VEC_index (tree, labels, i);
-	  if (!CASE_LOW (t))
+	  tree elt = VEC_index (tree, labels, i);
+	  tree low = CASE_LOW (elt);
+	  bool remove_element = FALSE;
+
+	  if (low)
+	    {
+	      /* Discard empty ranges.  */
+	      tree high = CASE_HIGH (elt);
+	      if (high && INT_CST_LT (high, low))
+	        remove_element = TRUE;
+	    }
+	  else
 	    {
 	      /* The default case must be the last label in the list.  */
-	      default_case = t;
-	      VEC_replace (tree, labels, i, VEC_index (tree, labels, len - 1));
-	      len--;
-	      break;
+	      gcc_assert (!default_case);
+	      default_case = elt;
+	      remove_element = TRUE;
 	    }
+
+	  if (remove_element)
+	    VEC_ordered_remove (tree, labels, i);
+	  else
+	    i++;
 	}
+      len = i;
 
       label_vec = make_tree_vec (len + 1);
       SWITCH_LABELS (*expr_p) = label_vec;
diff --git a/gcc/stmt.c b/gcc/stmt.c
index 38a7f909e1b2..d199398e55f6 100644
--- a/gcc/stmt.c
+++ b/gcc/stmt.c
@@ -2286,7 +2286,7 @@ emit_case_bit_tests (tree index_type, tree index_expr, tree minval,
 #define HAVE_tablejump 0
 #endif
 
-/* Terminate a case (Pascal) or switch (C) statement
+/* Terminate a case (Pascal/Ada) or switch (C) statement
    in which ORIG_INDEX is the expression to be tested.
    If ORIG_TYPE is not NULL, it is the original ORIG_INDEX
    type as given in the source before any compiler conversions.
@@ -2348,10 +2348,18 @@ expand_case (tree exp)
 
       for (i = TREE_VEC_LENGTH (vec) - 1; --i >= 0; )
 	{
+	  tree low, high;
 	  elt = TREE_VEC_ELT (vec, i);
-	  gcc_assert (CASE_LOW (elt));
-	  case_list = add_case_node (case_list, index_type,
-				     CASE_LOW (elt), CASE_HIGH (elt),
+
+	  low = CASE_LOW (elt);
+	  gcc_assert (low);
+	  high = CASE_HIGH (elt);
+
+	  /* Discard empty ranges.  */
+	  if (high && INT_CST_LT (high, low))
+	    continue;
+
+	  case_list = add_case_node (case_list, index_type, low, high,
 				     CASE_LABEL (elt));
 	}
 
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 553dba479134..6ae2bca1e908 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2006-03-13  Eric Botcazou  <ebotcazou@adacore.com>
+
+	* gcc.dg/switch-9.c: New test.
+
 2006-03-13  Richard Guenther  <rguenther@suse.de>
 
 	PR middle-end/26630
diff --git a/gcc/testsuite/gcc.dg/switch-9.c b/gcc/testsuite/gcc.dg/switch-9.c
new file mode 100644
index 000000000000..2ae89d09518c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/switch-9.c
@@ -0,0 +1,25 @@
+/* PR middle-end/18859 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+void foo(int a)
+{
+  switch (a)
+  {
+    case 0 ... -1:  /* { dg-warning "empty range" } */
+      a = a+2;
+      break;
+
+    case 1 ... 2:
+      a = 0;
+      break;
+
+    case 3 ... 4:
+      a = 1;
+      break;
+
+    case 5 ... 6:
+      a = 0;
+      break;
+  }
+}
-- 
GitLab