OSDN Git Service

* g++.dg/ext/altivec-17.C: Adjust error message.
[pf3gnuchains/gcc-fork.git] / gcc / tree-nrv.c
index 761e6a0..06b1351 100644 (file)
@@ -1,11 +1,12 @@
 /* Language independent return value optimizations
-   Copyright (C) 2004, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010
+   Free Software Foundation, Inc.
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -14,25 +15,26 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
 #include "tree.h"
-#include "rtl.h"
 #include "function.h"
 #include "basic-block.h"
-#include "expr.h"
 #include "diagnostic.h"
+#include "tree-pretty-print.h"
 #include "tree-flow.h"
 #include "timevar.h"
 #include "tree-dump.h"
 #include "tree-pass.h"
 #include "langhooks.h"
+#include "flags.h"     /* For "optimize" in gate_pass_return_slot.
+                          FIXME: That should be up to the pass manager,
+                          but pass_nrv is not in pass_all_optimizations.  */
 
 /* This file implements return value optimizations for functions which
    return aggregate types.
@@ -45,7 +47,7 @@ Boston, MA 02110-1301, USA.  */
    That copy can often be avoided by directly constructing the return value
    into the final destination mandated by the target's ABI.
 
-   This is basically a generic equivalent to the C++ front-end's 
+   This is basically a generic equivalent to the C++ front-end's
    Named Return Value optimization.  */
 
 struct nrv_data
@@ -57,6 +59,7 @@ struct nrv_data
   /* This is the function's RESULT_DECL.  We will replace all occurrences
      of VAR with RESULT_DECL when we apply this optimization.  */
   tree result;
+  int modified;
 };
 
 static tree finalize_nrv_r (tree *, int *, void *);
@@ -75,7 +78,8 @@ static tree finalize_nrv_r (tree *, int *, void *);
 static tree
 finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
 {
-  struct nrv_data *dp = (struct nrv_data *)data;
+  struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
+  struct nrv_data *dp = (struct nrv_data *) wi->info;
 
   /* No need to walk into types.  */
   if (TYPE_P (*tp))
@@ -83,7 +87,10 @@ finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
 
   /* Otherwise replace all occurrences of VAR with RESULT.  */
   else if (*tp == dp->var)
-    *tp = dp->result;
+    {
+      *tp = dp->result;
+      dp->modified = 1;
+    }
 
   /* Keep iterating.  */
   return NULL_TREE;
@@ -100,43 +107,63 @@ finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
    ever encounter languages which prevent this kind of optimization,
    then we could either have the languages register the optimization or
    we could change the gating function to check the current language.  */
-   
-static void
+
+static unsigned int
 tree_nrv (void)
 {
   tree result = DECL_RESULT (current_function_decl);
   tree result_type = TREE_TYPE (result);
   tree found = NULL;
   basic_block bb;
-  block_stmt_iterator bsi;
+  gimple_stmt_iterator gsi;
   struct nrv_data data;
 
   /* If this function does not return an aggregate type in memory, then
      there is nothing to do.  */
   if (!aggregate_value_p (result, current_function_decl))
-    return;
+    return 0;
+
+  /* If a GIMPLE type is returned in memory, finalize_nrv_r might create
+     non-GIMPLE.  */
+  if (is_gimple_reg_type (result_type))
+    return 0;
+
+  /* If the front end already did something like this, don't do it here.  */
+  if (DECL_NAME (result))
+    return 0;
+
+  /* If the result has its address taken then it might be modified
+     by means not detected in the following loop.  Bail out in this
+     case.  */
+  if (TREE_ADDRESSABLE (result))
+    return 0;
 
   /* Look through each block for assignments to the RESULT_DECL.  */
   FOR_EACH_BB (bb)
     {
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         tree stmt = bsi_stmt (bsi);
-         tree ret_expr;
+         gimple stmt = gsi_stmt (gsi);
+         tree ret_val;
 
-         if (TREE_CODE (stmt) == RETURN_EXPR)
+         if (gimple_code (stmt) == GIMPLE_RETURN)
            {
              /* In a function with an aggregate return value, the
                 gimplifier has changed all non-empty RETURN_EXPRs to
                 return the RESULT_DECL.  */
-             ret_expr = TREE_OPERAND (stmt, 0);
-             if (ret_expr)
-               gcc_assert (ret_expr == result);
+             ret_val = gimple_return_retval (stmt);
+             if (ret_val)
+               gcc_assert (ret_val == result);
            }
-         else if (TREE_CODE (stmt) == MODIFY_EXPR
-                  && TREE_OPERAND (stmt, 0) == result)
+         else if (gimple_has_lhs (stmt)
+                  && gimple_get_lhs (stmt) == result)
            {
-             ret_expr = TREE_OPERAND (stmt, 1);
+              tree rhs;
+
+             if (!gimple_assign_copy_p (stmt))
+               return 0;
+
+             rhs = gimple_assign_rhs1 (stmt);
 
              /* Now verify that this return statement uses the same value
                 as any previously encountered return statement.  */
@@ -145,11 +172,11 @@ tree_nrv (void)
                  /* If we found a return statement using a different variable
                     than previous return statements, then we can not perform
                     NRV optimizations.  */
-                 if (found != ret_expr)
-                   return;
+                 if (found != rhs)
+                   return 0;
                }
              else
-               found = ret_expr;
+               found = rhs;
 
              /* The returned value must be a local automatic variable of the
                 same type and alignment as the function's result.  */
@@ -159,15 +186,23 @@ tree_nrv (void)
                  || TREE_STATIC (found)
                  || TREE_ADDRESSABLE (found)
                  || DECL_ALIGN (found) > DECL_ALIGN (result)
-                 || !lang_hooks.types_compatible_p (TREE_TYPE (found), 
-                                                    result_type))
-               return;
+                 || !useless_type_conversion_p (result_type,
+                                                TREE_TYPE (found)))
+               return 0;
+           }
+         else if (gimple_has_lhs (stmt))
+           {
+             tree addr = get_base_address (gimple_get_lhs (stmt));
+              /* If there's any MODIFY of component of RESULT,
+                 then bail out.  */
+             if (addr && addr == result)
+               return 0;
            }
        }
     }
 
   if (!found)
-    return;
+    return 0;
 
   /* If dumping details, then note once and only the NRV replacement.  */
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -181,11 +216,18 @@ tree_nrv (void)
 
   /* At this point we know that all the return statements return the
      same local which has suitable attributes for NRV.   Copy debugging
-     information from FOUND to RESULT.  */
-  DECL_NAME (result) = DECL_NAME (found);
-  DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
-  DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
-  TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (found);
+     information from FOUND to RESULT if it will be useful.  But don't set
+     DECL_ABSTRACT_ORIGIN to point at another function.  */
+  if (!DECL_IGNORED_P (found)
+      && !(DECL_ABSTRACT_ORIGIN (found)
+          && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (found)) != current_function_decl))
+    {
+      DECL_NAME (result) = DECL_NAME (found);
+      DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
+      DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
+    }
+
+  TREE_ADDRESSABLE (result) |= TREE_ADDRESSABLE (found);
 
   /* Now walk through the function changing all references to VAR to be
      RESULT.  */
@@ -193,44 +235,86 @@ tree_nrv (void)
   data.result = result;
   FOR_EACH_BB (bb)
     {
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
        {
-         tree *tp = bsi_stmt_ptr (bsi);
+         gimple stmt = gsi_stmt (gsi);
          /* If this is a copy from VAR to RESULT, remove it.  */
-         if (TREE_CODE (*tp) == MODIFY_EXPR
-             && TREE_OPERAND (*tp, 0) == result
-             && TREE_OPERAND (*tp, 1) == found)
-           bsi_remove (&bsi, true);
+         if (gimple_assign_copy_p (stmt)
+             && gimple_assign_lhs (stmt) == result
+             && gimple_assign_rhs1 (stmt) == found)
+           {
+             unlink_stmt_vdef (stmt);
+             gsi_remove (&gsi, true);
+           }
          else
            {
-             walk_tree (tp, finalize_nrv_r, &data, 0);
-             bsi_next (&bsi);
+             struct walk_stmt_info wi;
+             memset (&wi, 0, sizeof (wi));
+             wi.info = &data;
+             data.modified = 0;
+             walk_gimple_op (stmt, finalize_nrv_r, &wi);
+             if (data.modified)
+               update_stmt (stmt);
+             gsi_next (&gsi);
            }
        }
     }
 
   /* FOUND is no longer used.  Ensure it gets removed.  */
   var_ann (found)->used = 0;
+  return 0;
+}
+
+static bool
+gate_pass_return_slot (void)
+{
+  return optimize > 0;
 }
 
-struct tree_opt_pass pass_nrv = 
+struct gimple_opt_pass pass_nrv =
 {
+ {
+  GIMPLE_PASS,
   "nrv",                               /* name */
-  NULL,                                        /* gate */
+  gate_pass_return_slot,               /* gate */
   tree_nrv,                            /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
   TV_TREE_NRV,                         /* tv_id */
-  PROP_cfg,                            /* properties_required */
+  PROP_ssa | PROP_cfg,                         /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  TODO_dump_func | TODO_ggc_collect,                   /* todo_flags_finish */
-  0                                    /* letter */
+  TODO_dump_func | TODO_ggc_collect                    /* todo_flags_finish */
+ }
 };
 
-/* Walk through the function looking for MODIFY_EXPRs with calls that
+/* Determine (pessimistically) whether DEST is available for NRV
+   optimization, where DEST is expected to be the LHS of a modify
+   expression where the RHS is a function returning an aggregate.
+
+   DEST is available if it is not clobbered by the call.  */
+
+static bool
+dest_safe_for_nrv_p (gimple call)
+{
+  tree dest = gimple_call_lhs (call);
+
+  dest = get_base_address (dest);
+  if (! dest)
+    return false;
+
+  if (TREE_CODE (dest) == SSA_NAME)
+    return true;
+
+  if (call_may_clobber_ref_p (call, dest))
+    return false;
+
+  return true;
+}
+
+/* Walk through the function looking for GIMPLE_ASSIGNs with calls that
    return in memory on the RHS.  For each of these, determine whether it is
    safe to pass the address of the LHS as the return slot, and mark the
    call appropriately if so.
@@ -242,66 +326,51 @@ struct tree_opt_pass pass_nrv =
    escaped prior to the call.  If it has, modifications to the local
    variable will produce visible changes elsewhere, as in PR c++/19317.  */
 
-static void
+static unsigned int
 execute_return_slot_opt (void)
 {
   basic_block bb;
 
   FOR_EACH_BB (bb)
     {
-      block_stmt_iterator i;
-      for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
+      gimple_stmt_iterator gsi;
+      for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         tree stmt = bsi_stmt (i);
-         tree call;
-
-         if (TREE_CODE (stmt) == MODIFY_EXPR
-             && (call = TREE_OPERAND (stmt, 1),
-                 TREE_CODE (call) == CALL_EXPR)
-             && !CALL_EXPR_RETURN_SLOT_OPT (call)
-             && aggregate_value_p (call, call))
+         gimple stmt = gsi_stmt (gsi);
+         bool slot_opt_p;
+
+         if (is_gimple_call (stmt)
+             && gimple_call_lhs (stmt)
+             && !gimple_call_return_slot_opt_p (stmt)
+             && aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)),
+                                   gimple_call_fndecl (stmt))
+            )
            {
-             def_operand_p def_p;
-             ssa_op_iter op_iter;
-
-             /* We determine whether or not the LHS address escapes by
-                asking whether it is call clobbered.  When the LHS isn't a
-                simple decl, we need to check the VDEFs, so it's simplest
-                to just loop through all the DEFs.  */
-             FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
-               {
-                 tree def = DEF_FROM_PTR (def_p);
-                 if (TREE_CODE (def) == SSA_NAME)
-                   def = SSA_NAME_VAR (def);
-                 if (is_call_clobbered (def))
-                   goto unsafe;
-               }
-
-             /* No defs are call clobbered, so the optimization is safe.  */
-             CALL_EXPR_RETURN_SLOT_OPT (call) = 1;
-             /* This is too late to mark the target addressable like we do
-                in gimplify_modify_expr_rhs, but that's OK; anything that
-                wasn't already addressable was handled there.  */
-
-             unsafe:;
+             /* Check if the location being assigned to is
+                clobbered by the call.  */
+             slot_opt_p = dest_safe_for_nrv_p (stmt);
+             gimple_call_set_return_slot_opt (stmt, slot_opt_p);
            }
        }
     }
+  return 0;
 }
 
-struct tree_opt_pass pass_return_slot = 
+struct gimple_opt_pass pass_return_slot =
 {
+ {
+  GIMPLE_PASS,
   "retslot",                           /* name */
   NULL,                                        /* gate */
   execute_return_slot_opt,             /* execute */
   NULL,                                        /* sub */
   NULL,                                        /* next */
   0,                                   /* static_pass_number */
-  0,                                   /* tv_id */
-  PROP_ssa | PROP_alias,               /* properties_required */
+  TV_NONE,                             /* tv_id */
+  PROP_ssa,                            /* properties_required */
   0,                                   /* properties_provided */
   0,                                   /* properties_destroyed */
   0,                                   /* todo_flags_start */
-  0,                                   /* todo_flags_finish */
-  0                                    /* letter */
+  0                                    /* todo_flags_finish */
+ }
 };