From 21cf718065ecda762be39bd34606b29239d2efaf Mon Sep 17 00:00:00 2001
From: Alexandre Oliva <aoliva@gcc.gnu.org>
Date: Sat, 21 Nov 2009 05:04:30 +0000
Subject: [PATCH] re PR tree-optimization/42078 (ICE in
 gimple_assign_set_rhs_code)

gcc/ChangeLog:
PR tree-optimization/42078
* gimple.h (gimple_replace_lhs): New declaration.
* gimple.c (gimple_replace_lhs): New function.
* tree-ssa-math-opts.c (execute_cse_reciprocals): Call it before
modifying the call.
gcc/testsuite/ChangeLog:
PR tree-optimization/42078
* gcc.dg/pr42078.c: New test.

From-SVN: r154400
---
 gcc/ChangeLog                  |  8 ++++++++
 gcc/gimple.c                   | 33 +++++++++++++++++++++++++++++++++
 gcc/gimple.h                   |  1 +
 gcc/testsuite/ChangeLog        |  5 +++++
 gcc/testsuite/gcc.dg/pr42078.c | 22 ++++++++++++++++++++++
 gcc/tree-ssa-math-opts.c       |  1 +
 6 files changed, 70 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr42078.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 9310c455addd..c4edde6f55b5 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2009-11-21  Alexandre Oliva  <aoliva@redhat.com>
+
+	PR tree-optimization/42078
+	* gimple.h (gimple_replace_lhs): New declaration.
+	* gimple.c (gimple_replace_lhs): New function.
+	* tree-ssa-math-opts.c (execute_cse_reciprocals): Call it before
+	modifying the call.
+
 2009-11-20  Sebastian Pop  <sebastian.pop@amd.com>
 
 	* config/i386/sse.md (*xop_pmacsdql_mem): Don't call reg_mentioned_p.
diff --git a/gcc/gimple.c b/gcc/gimple.c
index 9cec865a728b..f84a20cd6c1a 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -1981,6 +1981,39 @@ gimple_set_lhs (gimple stmt, tree lhs)
     gcc_unreachable();
 }
 
+/* Replace the LHS of STMT, an assignment, either a GIMPLE_ASSIGN or a
+   GIMPLE_CALL, with NLHS, in preparation for modifying the RHS to an
+   expression with a different value.
+
+   This will update any annotations (say debug bind stmts) referring
+   to the original LHS, so that they use the RHS instead.  This is
+   done even if NLHS and LHS are the same, for it is understood that
+   the RHS will be modified afterwards, and NLHS will not be assigned
+   an equivalent value.
+
+   Adjusting any non-annotation uses of the LHS, if needed, is a
+   responsibility of the caller.
+
+   The effect of this call should be pretty much the same as that of
+   inserting a copy of STMT before STMT, and then removing the
+   original stmt, at which time gsi_remove() would have update
+   annotations, but using this function saves all the inserting,
+   copying and removing.  */
+
+void
+gimple_replace_lhs (gimple stmt, tree nlhs)
+{
+  if (MAY_HAVE_DEBUG_STMTS)
+    {
+      tree lhs = gimple_get_lhs (stmt);
+
+      gcc_assert (SSA_NAME_DEF_STMT (lhs) == stmt);
+
+      insert_debug_temp_for_var_def (NULL, lhs);
+    }
+
+  gimple_set_lhs (stmt, nlhs);
+}
 
 /* Return a deep copy of statement STMT.  All the operands from STMT
    are reallocated and copied using unshare_expr.  The DEF, USE, VDEF
diff --git a/gcc/gimple.h b/gcc/gimple.h
index e956370bcd19..f355ab1ba356 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -843,6 +843,7 @@ void gimple_assign_set_rhs_with_ops (gimple_stmt_iterator *, enum tree_code,
 				     tree, tree);
 tree gimple_get_lhs (const_gimple);
 void gimple_set_lhs (gimple, tree);
+void gimple_replace_lhs (gimple, tree);
 gimple gimple_copy (gimple);
 bool is_gimple_operand (const_tree);
 void gimple_set_modified (gimple, bool);
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index f88e1a44d262..bb722a77ac3d 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2009-11-21  Jakub Jelinek  <jakub@redhat.com>
+
+	PR tree-optimization/42078
+	* gcc.dg/pr42078.c: New test.
+
 2009-11-19  Andy Hutchinson  <hutchinsonandy@gcc.gnu.org>
 
 	PR Testsuite/42114
diff --git a/gcc/testsuite/gcc.dg/pr42078.c b/gcc/testsuite/gcc.dg/pr42078.c
new file mode 100644
index 000000000000..8107ff5557a5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr42078.c
@@ -0,0 +1,22 @@
+/* PR tree-optimization/42078 */
+/* { dg-do compile } */
+/* { dg-options "-g -O -ffast-math" } */
+
+double sqrt (double x);
+
+float
+foo (float x)
+{
+  float y = sqrt (x);
+  return x / y;
+}
+
+inline float
+bar (float x)
+{
+  float y = sqrt (x);
+  float a = y;
+  float b = y;
+  float c = y;
+  return x / y;
+}
diff --git a/gcc/tree-ssa-math-opts.c b/gcc/tree-ssa-math-opts.c
index c0ddc8afa305..948707eb1a0e 100644
--- a/gcc/tree-ssa-math-opts.c
+++ b/gcc/tree-ssa-math-opts.c
@@ -563,6 +563,7 @@ execute_cse_reciprocals (void)
 		  if (fail)
 		    continue;
 
+		  gimple_replace_lhs (stmt1, arg1);
 		  gimple_call_set_fndecl (stmt1, fndecl);
 		  update_stmt (stmt1);
 
-- 
GitLab