OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [tree-nrv.c] - Diff between revs 154 and 816

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 154 Rev 816
/* Language independent return value optimizations
/* Language independent return value optimizations
   Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
   Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
 
 
This file is part of GCC.
This file is part of GCC.
 
 
GCC is free software; you can redistribute it and/or modify
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
the Free Software Foundation; either version 3, or (at your option)
any later version.
any later version.
 
 
GCC is distributed in the hope that it will be useful,
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
GNU General Public License for more details.
 
 
You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
<http://www.gnu.org/licenses/>.  */
 
 
#include "config.h"
#include "config.h"
#include "system.h"
#include "system.h"
#include "coretypes.h"
#include "coretypes.h"
#include "tm.h"
#include "tm.h"
#include "tree.h"
#include "tree.h"
#include "rtl.h"
#include "rtl.h"
#include "function.h"
#include "function.h"
#include "basic-block.h"
#include "basic-block.h"
#include "expr.h"
#include "expr.h"
#include "diagnostic.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "tree-flow.h"
#include "timevar.h"
#include "timevar.h"
#include "tree-dump.h"
#include "tree-dump.h"
#include "tree-pass.h"
#include "tree-pass.h"
#include "langhooks.h"
#include "langhooks.h"
 
 
/* This file implements return value optimizations for functions which
/* This file implements return value optimizations for functions which
   return aggregate types.
   return aggregate types.
 
 
   Basically this pass searches the function for return statements which
   Basically this pass searches the function for return statements which
   return a local aggregate.  When converted to RTL such statements will
   return a local aggregate.  When converted to RTL such statements will
   generate a copy from the local aggregate to final return value destination
   generate a copy from the local aggregate to final return value destination
   mandated by the target's ABI.
   mandated by the target's ABI.
 
 
   That copy can often be avoided by directly constructing the return value
   That copy can often be avoided by directly constructing the return value
   into the final destination mandated by the target's ABI.
   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.  */
   Named Return Value optimization.  */
 
 
struct nrv_data
struct nrv_data
{
{
  /* This is the temporary (a VAR_DECL) which appears in all of
  /* This is the temporary (a VAR_DECL) which appears in all of
     this function's RETURN_EXPR statements.  */
     this function's RETURN_EXPR statements.  */
  tree var;
  tree var;
 
 
  /* This is the function's RESULT_DECL.  We will replace all occurrences
  /* This is the function's RESULT_DECL.  We will replace all occurrences
     of VAR with RESULT_DECL when we apply this optimization.  */
     of VAR with RESULT_DECL when we apply this optimization.  */
  tree result;
  tree result;
};
};
 
 
static tree finalize_nrv_r (tree *, int *, void *);
static tree finalize_nrv_r (tree *, int *, void *);
 
 
/* Callback for the tree walker.
/* Callback for the tree walker.
 
 
   If TP refers to a RETURN_EXPR, then set the expression being returned
   If TP refers to a RETURN_EXPR, then set the expression being returned
   to nrv_data->result.
   to nrv_data->result.
 
 
   If TP refers to nrv_data->var, then replace nrv_data->var with
   If TP refers to nrv_data->var, then replace nrv_data->var with
   nrv_data->result.
   nrv_data->result.
 
 
   If we reach a node where we know all the subtrees are uninteresting,
   If we reach a node where we know all the subtrees are uninteresting,
   then set *WALK_SUBTREES to zero.  */
   then set *WALK_SUBTREES to zero.  */
 
 
static tree
static tree
finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
{
{
  struct nrv_data *dp = (struct nrv_data *)data;
  struct nrv_data *dp = (struct nrv_data *)data;
 
 
  /* No need to walk into types.  */
  /* No need to walk into types.  */
  if (TYPE_P (*tp))
  if (TYPE_P (*tp))
    *walk_subtrees = 0;
    *walk_subtrees = 0;
 
 
  /* Otherwise replace all occurrences of VAR with RESULT.  */
  /* Otherwise replace all occurrences of VAR with RESULT.  */
  else if (*tp == dp->var)
  else if (*tp == dp->var)
    *tp = dp->result;
    *tp = dp->result;
 
 
  /* Keep iterating.  */
  /* Keep iterating.  */
  return NULL_TREE;
  return NULL_TREE;
}
}
 
 
/* Main entry point for return value optimizations.
/* Main entry point for return value optimizations.
 
 
   If this function always returns the same local variable, and that
   If this function always returns the same local variable, and that
   local variable is an aggregate type, then replace the variable with
   local variable is an aggregate type, then replace the variable with
   the function's DECL_RESULT.
   the function's DECL_RESULT.
 
 
   This is the equivalent of the C++ named return value optimization
   This is the equivalent of the C++ named return value optimization
   applied to optimized trees in a language independent form.  If we
   applied to optimized trees in a language independent form.  If we
   ever encounter languages which prevent this kind of optimization,
   ever encounter languages which prevent this kind of optimization,
   then we could either have the languages register the optimization or
   then we could either have the languages register the optimization or
   we could change the gating function to check the current language.  */
   we could change the gating function to check the current language.  */
 
 
static unsigned int
static unsigned int
tree_nrv (void)
tree_nrv (void)
{
{
  tree result = DECL_RESULT (current_function_decl);
  tree result = DECL_RESULT (current_function_decl);
  tree result_type = TREE_TYPE (result);
  tree result_type = TREE_TYPE (result);
  tree found = NULL;
  tree found = NULL;
  basic_block bb;
  basic_block bb;
  block_stmt_iterator bsi;
  block_stmt_iterator bsi;
  struct nrv_data data;
  struct nrv_data data;
 
 
  /* If this function does not return an aggregate type in memory, then
  /* If this function does not return an aggregate type in memory, then
     there is nothing to do.  */
     there is nothing to do.  */
  if (!aggregate_value_p (result, current_function_decl))
  if (!aggregate_value_p (result, current_function_decl))
    return 0;
    return 0;
 
 
  /* Look through each block for assignments to the RESULT_DECL.  */
  /* Look through each block for assignments to the RESULT_DECL.  */
  FOR_EACH_BB (bb)
  FOR_EACH_BB (bb)
    {
    {
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
        {
        {
          tree stmt = bsi_stmt (bsi);
          tree stmt = bsi_stmt (bsi);
          tree ret_expr;
          tree ret_expr;
 
 
          if (TREE_CODE (stmt) == RETURN_EXPR)
          if (TREE_CODE (stmt) == RETURN_EXPR)
            {
            {
              /* In a function with an aggregate return value, the
              /* In a function with an aggregate return value, the
                 gimplifier has changed all non-empty RETURN_EXPRs to
                 gimplifier has changed all non-empty RETURN_EXPRs to
                 return the RESULT_DECL.  */
                 return the RESULT_DECL.  */
              ret_expr = TREE_OPERAND (stmt, 0);
              ret_expr = TREE_OPERAND (stmt, 0);
              if (ret_expr)
              if (ret_expr)
                gcc_assert (ret_expr == result);
                gcc_assert (ret_expr == result);
            }
            }
          else if (TREE_CODE (stmt) == MODIFY_EXPR
          else if (TREE_CODE (stmt) == MODIFY_EXPR
                   && TREE_OPERAND (stmt, 0) == result)
                   && TREE_OPERAND (stmt, 0) == result)
            {
            {
              ret_expr = TREE_OPERAND (stmt, 1);
              ret_expr = TREE_OPERAND (stmt, 1);
 
 
              /* Now verify that this return statement uses the same value
              /* Now verify that this return statement uses the same value
                 as any previously encountered return statement.  */
                 as any previously encountered return statement.  */
              if (found != NULL)
              if (found != NULL)
                {
                {
                  /* If we found a return statement using a different variable
                  /* If we found a return statement using a different variable
                     than previous return statements, then we can not perform
                     than previous return statements, then we can not perform
                     NRV optimizations.  */
                     NRV optimizations.  */
                  if (found != ret_expr)
                  if (found != ret_expr)
                    return 0;
                    return 0;
                }
                }
              else
              else
                found = ret_expr;
                found = ret_expr;
 
 
              /* The returned value must be a local automatic variable of the
              /* The returned value must be a local automatic variable of the
                 same type and alignment as the function's result.  */
                 same type and alignment as the function's result.  */
              if (TREE_CODE (found) != VAR_DECL
              if (TREE_CODE (found) != VAR_DECL
                  || TREE_THIS_VOLATILE (found)
                  || TREE_THIS_VOLATILE (found)
                  || DECL_CONTEXT (found) != current_function_decl
                  || DECL_CONTEXT (found) != current_function_decl
                  || TREE_STATIC (found)
                  || TREE_STATIC (found)
                  || TREE_ADDRESSABLE (found)
                  || TREE_ADDRESSABLE (found)
                  || DECL_ALIGN (found) > DECL_ALIGN (result)
                  || DECL_ALIGN (found) > DECL_ALIGN (result)
                  || !lang_hooks.types_compatible_p (TREE_TYPE (found),
                  || !lang_hooks.types_compatible_p (TREE_TYPE (found),
                                                     result_type))
                                                     result_type))
                return 0;
                return 0;
            }
            }
          else if (TREE_CODE (stmt) == MODIFY_EXPR)
          else if (TREE_CODE (stmt) == MODIFY_EXPR)
            {
            {
              tree addr = get_base_address (TREE_OPERAND (stmt, 0));
              tree addr = get_base_address (TREE_OPERAND (stmt, 0));
               /* If there's any MODIFY of component of RESULT,
               /* If there's any MODIFY of component of RESULT,
                  then bail out.  */
                  then bail out.  */
              if (addr && addr == result)
              if (addr && addr == result)
                return 0;
                return 0;
            }
            }
        }
        }
    }
    }
 
 
  if (!found)
  if (!found)
    return 0;
    return 0;
 
 
  /* If dumping details, then note once and only the NRV replacement.  */
  /* If dumping details, then note once and only the NRV replacement.  */
  if (dump_file && (dump_flags & TDF_DETAILS))
  if (dump_file && (dump_flags & TDF_DETAILS))
    {
    {
      fprintf (dump_file, "NRV Replaced: ");
      fprintf (dump_file, "NRV Replaced: ");
      print_generic_expr (dump_file, found, dump_flags);
      print_generic_expr (dump_file, found, dump_flags);
      fprintf (dump_file, "  with: ");
      fprintf (dump_file, "  with: ");
      print_generic_expr (dump_file, result, dump_flags);
      print_generic_expr (dump_file, result, dump_flags);
      fprintf (dump_file, "\n");
      fprintf (dump_file, "\n");
    }
    }
 
 
  /* At this point we know that all the return statements return the
  /* At this point we know that all the return statements return the
     same local which has suitable attributes for NRV.   Copy debugging
     same local which has suitable attributes for NRV.   Copy debugging
     information from FOUND to RESULT.  */
     information from FOUND to RESULT.  */
  DECL_NAME (result) = DECL_NAME (found);
  DECL_NAME (result) = DECL_NAME (found);
  DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
  DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
  DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
  DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
  TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (found);
  TREE_ADDRESSABLE (result) = TREE_ADDRESSABLE (found);
 
 
  /* Now walk through the function changing all references to VAR to be
  /* Now walk through the function changing all references to VAR to be
     RESULT.  */
     RESULT.  */
  data.var = found;
  data.var = found;
  data.result = result;
  data.result = result;
  FOR_EACH_BB (bb)
  FOR_EACH_BB (bb)
    {
    {
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
        {
        {
          tree *tp = bsi_stmt_ptr (bsi);
          tree *tp = bsi_stmt_ptr (bsi);
          /* If this is a copy from VAR to RESULT, remove it.  */
          /* If this is a copy from VAR to RESULT, remove it.  */
          if (TREE_CODE (*tp) == MODIFY_EXPR
          if (TREE_CODE (*tp) == MODIFY_EXPR
              && TREE_OPERAND (*tp, 0) == result
              && TREE_OPERAND (*tp, 0) == result
              && TREE_OPERAND (*tp, 1) == found)
              && TREE_OPERAND (*tp, 1) == found)
            bsi_remove (&bsi, true);
            bsi_remove (&bsi, true);
          else
          else
            {
            {
              walk_tree (tp, finalize_nrv_r, &data, 0);
              walk_tree (tp, finalize_nrv_r, &data, 0);
              bsi_next (&bsi);
              bsi_next (&bsi);
            }
            }
        }
        }
    }
    }
 
 
  /* FOUND is no longer used.  Ensure it gets removed.  */
  /* FOUND is no longer used.  Ensure it gets removed.  */
  var_ann (found)->used = 0;
  var_ann (found)->used = 0;
  return 0;
  return 0;
}
}
 
 
struct tree_opt_pass pass_nrv =
struct tree_opt_pass pass_nrv =
{
{
  "nrv",                                /* name */
  "nrv",                                /* name */
  NULL,                                 /* gate */
  NULL,                                 /* gate */
  tree_nrv,                             /* execute */
  tree_nrv,                             /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  NULL,                                 /* next */
  0,                                     /* static_pass_number */
  0,                                     /* static_pass_number */
  TV_TREE_NRV,                          /* tv_id */
  TV_TREE_NRV,                          /* tv_id */
  PROP_cfg,                             /* properties_required */
  PROP_cfg,                             /* properties_required */
  0,                                     /* properties_provided */
  0,                                     /* properties_provided */
  0,                                     /* properties_destroyed */
  0,                                     /* properties_destroyed */
  0,                                     /* todo_flags_start */
  0,                                     /* todo_flags_start */
  TODO_dump_func | TODO_ggc_collect,                    /* todo_flags_finish */
  TODO_dump_func | TODO_ggc_collect,                    /* todo_flags_finish */
  0                                      /* letter */
  0                                      /* letter */
};
};
 
 
/* Determine (pessimistically) whether DEST is available for NRV
/* Determine (pessimistically) whether DEST is available for NRV
   optimization, where DEST is expected to be the LHS of a modify
   optimization, where DEST is expected to be the LHS of a modify
   expression where the RHS is a function returning an aggregate.
   expression where the RHS is a function returning an aggregate.
 
 
   We search for a base VAR_DECL and look to see if it, or any of its
   We search for a base VAR_DECL and look to see if it, or any of its
   subvars are clobbered.  Note that we could do better, for example, by
   subvars are clobbered.  Note that we could do better, for example, by
   attempting to doing points-to analysis on INDIRECT_REFs.  */
   attempting to doing points-to analysis on INDIRECT_REFs.  */
 
 
static bool
static bool
dest_safe_for_nrv_p (tree dest)
dest_safe_for_nrv_p (tree dest)
{
{
  switch (TREE_CODE (dest))
  switch (TREE_CODE (dest))
    {
    {
      case VAR_DECL:
      case VAR_DECL:
        {
        {
          subvar_t subvar;
          subvar_t subvar;
          if (is_call_clobbered (dest))
          if (is_call_clobbered (dest))
            return false;
            return false;
          for (subvar = get_subvars_for_var (dest);
          for (subvar = get_subvars_for_var (dest);
               subvar;
               subvar;
               subvar = subvar->next)
               subvar = subvar->next)
            if (is_call_clobbered (subvar->var))
            if (is_call_clobbered (subvar->var))
              return false;
              return false;
          return true;
          return true;
        }
        }
      case ARRAY_REF:
      case ARRAY_REF:
      case COMPONENT_REF:
      case COMPONENT_REF:
        return dest_safe_for_nrv_p (TREE_OPERAND (dest, 0));
        return dest_safe_for_nrv_p (TREE_OPERAND (dest, 0));
      default:
      default:
        return false;
        return false;
    }
    }
}
}
 
 
/* Walk through the function looking for MODIFY_EXPRs with calls that
/* Walk through the function looking for MODIFY_EXPRs with calls that
   return in memory on the RHS.  For each of these, determine whether it is
   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
   safe to pass the address of the LHS as the return slot, and mark the
   call appropriately if so.
   call appropriately if so.
 
 
   The NRV shares the return slot with a local variable in the callee; this
   The NRV shares the return slot with a local variable in the callee; this
   optimization shares the return slot with the target of the call within
   optimization shares the return slot with the target of the call within
   the caller.  If the NRV is performed (which we can't know in general),
   the caller.  If the NRV is performed (which we can't know in general),
   this optimization is safe if the address of the target has not
   this optimization is safe if the address of the target has not
   escaped prior to the call.  If it has, modifications to the local
   escaped prior to the call.  If it has, modifications to the local
   variable will produce visible changes elsewhere, as in PR c++/19317.  */
   variable will produce visible changes elsewhere, as in PR c++/19317.  */
 
 
static unsigned int
static unsigned int
execute_return_slot_opt (void)
execute_return_slot_opt (void)
{
{
  basic_block bb;
  basic_block bb;
 
 
  FOR_EACH_BB (bb)
  FOR_EACH_BB (bb)
    {
    {
      block_stmt_iterator i;
      block_stmt_iterator i;
      for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
      for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i))
        {
        {
          tree stmt = bsi_stmt (i);
          tree stmt = bsi_stmt (i);
          tree call;
          tree call;
 
 
          if (TREE_CODE (stmt) == MODIFY_EXPR
          if (TREE_CODE (stmt) == MODIFY_EXPR
              && (call = TREE_OPERAND (stmt, 1),
              && (call = TREE_OPERAND (stmt, 1),
                  TREE_CODE (call) == CALL_EXPR)
                  TREE_CODE (call) == CALL_EXPR)
              && !CALL_EXPR_RETURN_SLOT_OPT (call)
              && !CALL_EXPR_RETURN_SLOT_OPT (call)
              && aggregate_value_p (call, call))
              && aggregate_value_p (call, call))
            /* Check if the location being assigned to is
            /* Check if the location being assigned to is
               call-clobbered.  */
               call-clobbered.  */
            CALL_EXPR_RETURN_SLOT_OPT (call) =
            CALL_EXPR_RETURN_SLOT_OPT (call) =
              dest_safe_for_nrv_p (TREE_OPERAND (stmt, 0)) ? 1 : 0;
              dest_safe_for_nrv_p (TREE_OPERAND (stmt, 0)) ? 1 : 0;
        }
        }
    }
    }
  return 0;
  return 0;
}
}
 
 
struct tree_opt_pass pass_return_slot =
struct tree_opt_pass pass_return_slot =
{
{
  "retslot",                            /* name */
  "retslot",                            /* name */
  NULL,                                 /* gate */
  NULL,                                 /* gate */
  execute_return_slot_opt,              /* execute */
  execute_return_slot_opt,              /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  NULL,                                 /* next */
  0,                                     /* static_pass_number */
  0,                                     /* static_pass_number */
  0,                                     /* tv_id */
  0,                                     /* tv_id */
  PROP_ssa | PROP_alias,                /* properties_required */
  PROP_ssa | PROP_alias,                /* properties_required */
  0,                                     /* properties_provided */
  0,                                     /* properties_provided */
  0,                                     /* properties_destroyed */
  0,                                     /* properties_destroyed */
  0,                                     /* todo_flags_start */
  0,                                     /* todo_flags_start */
  0,                                     /* todo_flags_finish */
  0,                                     /* todo_flags_finish */
  0                                      /* letter */
  0                                      /* letter */
};
};
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.