From 1537737f284eaacd8a335d1c8dbfb6cd10b0803b Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Thu, 26 May 2011 12:25:21 +0200
Subject: [PATCH] re PR c++/49165 (ICE on for-loop/throw combination)

	PR c++/49165
	* gimplify.c (shortcut_cond_r): Don't special case
	COND_EXPRs if they have void type on one of their arms.

	* g++.dg/eh/cond5.C: New test.

From-SVN: r174273
---
 gcc/ChangeLog                   |  6 +++++
 gcc/gimplify.c                  |  9 +++++--
 gcc/testsuite/ChangeLog         |  3 +++
 gcc/testsuite/g++.dg/eh/cond5.C | 43 +++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/eh/cond5.C

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index d6bfc8011edc..825858ceec81 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2011-05-26  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c++/49165
+	* gimplify.c (shortcut_cond_r): Don't special case
+	COND_EXPRs if they have void type on one of their arms.
+
 2011-05-26  Bernd Schmidt  <bernds@codesourcery.com>
 
 	* haifa-sched.c (schedule-block): Reorder the inner scheduling loop
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 5fd22c011c20..31e0daf3d41f 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -2573,7 +2573,9 @@ shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
 			   new_locus);
       append_to_statement_list (t, &expr);
     }
-  else if (TREE_CODE (pred) == COND_EXPR)
+  else if (TREE_CODE (pred) == COND_EXPR
+	   && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 1)))
+	   && !VOID_TYPE_P (TREE_TYPE (TREE_OPERAND (pred, 2))))
     {
       location_t new_locus;
 
@@ -2581,7 +2583,10 @@ shortcut_cond_r (tree pred, tree *true_label_p, tree *false_label_p,
 	 if (a)
 	   if (b) goto yes; else goto no;
 	 else
-	   if (c) goto yes; else goto no;  */
+	   if (c) goto yes; else goto no;
+
+	 Don't do this if one of the arms has void type, which can happen
+	 in C++ when the arm is throw.  */
 
       /* Keep the original source location on the first 'if'.  Set the source
 	 location of the ? on the second 'if'.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 27f468d308c4..db7d49dbb688 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,5 +1,8 @@
 2011-05-26  Jakub Jelinek  <jakub@redhat.com>
 
+	PR c++/49165
+	* g++.dg/eh/cond5.C: New test.
+
 	PR tree-optimization/49161
 	* gcc.c-torture/execute/pr49161.c: New test.
 
diff --git a/gcc/testsuite/g++.dg/eh/cond5.C b/gcc/testsuite/g++.dg/eh/cond5.C
new file mode 100644
index 000000000000..3f0c59964f11
--- /dev/null
+++ b/gcc/testsuite/g++.dg/eh/cond5.C
@@ -0,0 +1,43 @@
+// PR c++/49165
+// { dg-do run }
+
+extern "C" void abort ();
+
+int
+foo (bool x, int y)
+{
+  if (y < 10 && (x ? true : throw 1))
+    y++;
+  if (y > 20 || (x ? true : throw 2))
+    y++;
+  return y;
+}
+
+int
+main ()
+{
+  if (foo (true, 0) != 2
+      || foo (true, 10) != 11
+      || foo (false, 30) != 31)
+    abort ();
+  try
+    {
+      foo (false, 0);
+      abort ();
+    }
+  catch (int i)
+    {
+      if (i != 1)
+	abort ();
+    }
+  try
+    {
+      foo (false, 10);
+      abort ();
+    }
+  catch (int i)
+    {
+      if (i != 2)
+	abort ();
+    }
+}
-- 
GitLab