diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index dcb9d934b4179eb8ae3ebc0fdfc634af09c9d23b..f406688442eda35ecc5f91339eab3bc4c300c3c0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,9 @@
+2012-09-17  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/54563
+	* tree-ssa-math-opts.c (execute_cse_sincos): Call
+	gimple_purge_dead_eh_edges if last call has been changed.
+
 2012-09-17  Georg-Johann Lay  <avr@gjlay.de>
 
 	PR target/54222
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 0a63f46d29933cb971ab200ca97e4fb372308660..ead2a9744a906ee5b451d243ff917bfa756eff9d 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2012-09-17  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/54563
+	* g++.dg/torture/pr54563.C: New test.
+
 2012-09-16  Janus Weil  <janus@gcc.gnu.org>
 
 	PR fortran/54594
diff --git a/gcc/testsuite/g++.dg/torture/pr54563.C b/gcc/testsuite/g++.dg/torture/pr54563.C
new file mode 100644
index 0000000000000000000000000000000000000000..2980320f66369ed868b5766702abdf0e1ae455c2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr54563.C
@@ -0,0 +1,14 @@
+// PR tree-optimization/54563
+// { dg-do compile }
+
+extern "C" float powf (float, float);
+struct S { ~S (); };
+double bar ();
+double x;
+
+void
+foo ()
+{
+  S s;
+  x = powf (bar (), 2.);
+}
diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c
index 94d19afc2c071482db16d4b33c7d5740915ca225..2020bb138cc8bca36d677f34b8bfe129042c66cf 100644
--- a/gcc/tree-ssa-math-opts.c
+++ b/gcc/tree-ssa-math-opts.c
@@ -1,5 +1,5 @@
 /* Global, SSA-based optimizations using mathematical identities.
-   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
+   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
    Free Software Foundation, Inc.
 
 This file is part of GCC.
@@ -1378,12 +1378,18 @@ execute_cse_sincos (void)
   FOR_EACH_BB (bb)
     {
       gimple_stmt_iterator gsi;
+      bool cleanup_eh = false;
 
       for (gsi = gsi_after_labels (bb); !gsi_end_p (gsi); gsi_next (&gsi))
         {
 	  gimple stmt = gsi_stmt (gsi);
 	  tree fndecl;
 
+	  /* Only the last stmt in a bb could throw, no need to call
+	     gimple_purge_dead_eh_edges if we change something in the middle
+	     of a basic block.  */
+	  cleanup_eh = false;
+
 	  if (is_gimple_call (stmt)
 	      && gimple_call_lhs (stmt)
 	      && (fndecl = gimple_call_fndecl (stmt))
@@ -1421,6 +1427,7 @@ execute_cse_sincos (void)
 		      gimple_set_location (new_stmt, loc);
 		      unlink_stmt_vdef (stmt);
 		      gsi_replace (&gsi, new_stmt, true);
+		      cleanup_eh = true;
 		      if (gimple_vdef (stmt))
 			release_ssa_name (gimple_vdef (stmt));
 		    }
@@ -1443,6 +1450,7 @@ execute_cse_sincos (void)
 		      gimple_set_location (new_stmt, loc);
 		      unlink_stmt_vdef (stmt);
 		      gsi_replace (&gsi, new_stmt, true);
+		      cleanup_eh = true;
 		      if (gimple_vdef (stmt))
 			release_ssa_name (gimple_vdef (stmt));
 		    }
@@ -1460,6 +1468,7 @@ execute_cse_sincos (void)
 		      gimple_set_location (new_stmt, loc);
 		      unlink_stmt_vdef (stmt);
 		      gsi_replace (&gsi, new_stmt, true);
+		      cleanup_eh = true;
 		      if (gimple_vdef (stmt))
 			release_ssa_name (gimple_vdef (stmt));
 		    }
@@ -1469,6 +1478,8 @@ execute_cse_sincos (void)
 		}
 	    }
 	}
+      if (cleanup_eh)
+	cfg_changed |= gimple_purge_dead_eh_edges (bb);
     }
 
   statistics_counter_event (cfun, "sincos statements inserted",