From 7e14028056eafe6486281fb96d17d2f873f6248f Mon Sep 17 00:00:00 2001
From: Richard Henderson <rth@redhat.com>
Date: Thu, 20 May 2004 10:37:02 -0700
Subject: [PATCH] re PR tree-optimization/15454 (tree-ccp generates wrong code
 for nested functions)

        PR 15454
        * tree-nested.c (get_chain_decl): Create a PARM_DECL by hand.
        * function.c (expand_function_start): Expand static_chain_decl by hand.
        * gimplify.c (create_tmp_var_name): Export.
        * tree-gimple.h (create_tmp_var_name): Declare.
	* gcc.c-torture/execute/20040520-1.c: New.

From-SVN: r82064
---
 gcc/ChangeLog                                   |  8 ++++++++
 gcc/function.c                                  | 13 ++++++++-----
 gcc/gimplify.c                                  |  2 +-
 .../gcc.c-torture/execute/20040520-1.c          | 17 +++++++++++++++++
 gcc/tree-gimple.h                               |  1 +
 gcc/tree-nested.c                               | 17 +++++++++--------
 6 files changed, 44 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/20040520-1.c

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 82e5d0a9510e..8d00704e86f0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,11 @@
+2004-05-20  Richard Henderson  <rth@redhat.com>
+
+	PR 15454
+	* tree-nested.c (get_chain_decl): Create a PARM_DECL by hand.
+	* function.c (expand_function_start): Expand static_chain_decl by hand.
+	* gimplify.c (create_tmp_var_name): Export.
+	* tree-gimple.h (create_tmp_var_name): Declare.
+
 2004-05-20  Andrew Pinski  <pinskia@physics.uc.edu>
 
 	* rs6000.c (print_operand) <case 'z'>: Call
diff --git a/gcc/function.c b/gcc/function.c
index 4c535c713836..c34c191062d3 100644
--- a/gcc/function.c
+++ b/gcc/function.c
@@ -6565,12 +6565,15 @@ expand_function_start (tree subr, int parms_have_cleanups)
   /* If function gets a static chain arg, store it.  */
   if (cfun->static_chain_decl)
     {
-      rtx x;
+      tree parm = cfun->static_chain_decl;
+      rtx local = gen_reg_rtx (Pmode);
+
+      set_decl_incoming_rtl (parm, static_chain_incoming_rtx);
+      SET_DECL_RTL (parm, local);
+      maybe_set_unchanging (local, parm);
+      mark_reg_pointer (local, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (parm))));
 
-      expand_var (cfun->static_chain_decl);
-      x = expand_expr (cfun->static_chain_decl, NULL_RTX,
-		       VOIDmode, EXPAND_WRITE);
-      emit_move_insn (x, static_chain_incoming_rtx);
+      emit_move_insn (local, static_chain_incoming_rtx);
     }
 
   /* If the function receives a non-local goto, then store the
diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index 907d443dca57..21cf0141d504 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -286,7 +286,7 @@ create_artificial_label (void)
 
 static GTY(()) unsigned int tmp_var_id_num;
 
-static tree
+tree
 create_tmp_var_name (const char *prefix)
 {
   char *tmp_name;
diff --git a/gcc/testsuite/gcc.c-torture/execute/20040520-1.c b/gcc/testsuite/gcc.c-torture/execute/20040520-1.c
new file mode 100644
index 000000000000..b42483714375
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/20040520-1.c
@@ -0,0 +1,17 @@
+/* PR 15454 */
+
+void abort ();
+int main () {
+        int foo;
+        int bar (void)
+        {
+                int baz = 0;
+                if (foo!=45)
+                        baz = foo;
+                return baz;
+        }
+        foo = 1;
+        if (!bar ())
+                abort ();
+        return 0;
+}
diff --git a/gcc/tree-gimple.h b/gcc/tree-gimple.h
index d2c91032031a..59cb3b365ac2 100644
--- a/gcc/tree-gimple.h
+++ b/gcc/tree-gimple.h
@@ -26,6 +26,7 @@ Boston, MA 02111-1307, USA.  */
 #include "tree-iterator.h"
 
 extern tree create_tmp_var_raw (tree, const char *);
+extern tree create_tmp_var_name (const char *);
 extern tree create_tmp_var (tree, const char *);
 extern bool is_gimple_tmp_var (tree);
 extern tree get_initialized_tmp_var (tree, tree *, tree *);
diff --git a/gcc/tree-nested.c b/gcc/tree-nested.c
index 1a00ff30f864..14d2c3cb9ff5 100644
--- a/gcc/tree-nested.c
+++ b/gcc/tree-nested.c
@@ -306,15 +306,16 @@ get_chain_decl (struct nesting_info *info)
 
       /* Note that this variable is *not* entered into any BIND_EXPR;
 	 the construction of this variable is handled specially in
-	 expand_function_start and initialize_inlined_parameters.  */
-      decl = create_tmp_var_raw (type, "CHAIN");
+	 expand_function_start and initialize_inlined_parameters.
+	 Note also that it's represented as a parameter.  This is more
+	 close to the truth, since the initial value does come from 
+	 the caller.  */
+      decl = build_decl (PARM_DECL, create_tmp_var_name ("CHAIN"), type);
+      DECL_ARTIFICIAL (decl) = 1;
+      DECL_IGNORED_P (decl) = 1;
+      TREE_USED (decl) = 1;
       DECL_CONTEXT (decl) = info->context;
-      decl->decl.seen_in_bind_expr = 1;
-
-      /* The initialization of CHAIN is not visible to the tree-ssa
-	 analyzers and optimizers.  Thus we do not want to issue
-	 warnings for CHAIN.  */
-      TREE_NO_WARNING (decl) = 1;
+      DECL_ARG_TYPE (decl) = type;
 
       /* Tell tree-inline.c that we never write to this variable, so
 	 it can copy-prop the replacement value immediately.  */
-- 
GitLab