GCC Middle and Back End API Reference
tree-nrv.c File Reference

Data Structures

struct  nrv_data


static tree finalize_nrv_r (tree *, int *, void *)
static tree finalize_nrv_r ()
static unsigned int tree_nrv ()
static bool gate_pass_return_slot ()
gimple_opt_passmake_pass_nrv ()
static bool dest_safe_for_nrv_p ()
static unsigned int execute_return_slot_opt ()
gimple_opt_passmake_pass_return_slot ()

Function Documentation

static bool dest_safe_for_nrv_p ( )
   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 or used by the call.  

References aggregate_value_p(), gimple_call_fndecl(), gimple_call_lhs(), gimple_call_return_slot_opt_p(), gimple_call_set_return_slot_opt(), walk_stmt_info::gsi, gsi_end_p(), gsi_next(), gsi_start_bb(), gsi_stmt(), and is_gimple_call().

static unsigned int execute_return_slot_opt ( )
   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.

   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
   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
   escaped prior to the call.  If it has, modifications to the local
   variable will produce visible changes elsewhere, as in PR c++/19317.  
                 Check if the location being assigned to is
                 clobbered by the call.  
static tree finalize_nrv_r ( tree ,
int *  ,
void *   
static tree finalize_nrv_r ( )
   Callback for the tree walker.

   If TP refers to a RETURN_EXPR, then set the expression being returned
   to nrv_data->result.

   If TP refers to nrv_data->var, then replace nrv_data->var with

   If we reach a node where we know all the subtrees are uninteresting,
   then set *WALK_SUBTREES to zero.  
     No need to walk into types.  
     Otherwise replace all occurrences of VAR with RESULT.  
     Keep iterating.  

References nrv_data::modified, and nrv_data::result.

static bool gate_pass_return_slot ( )

References execute(), and tree_nrv().

gimple_opt_pass* make_pass_nrv ( )
gimple_opt_pass* make_pass_return_slot ( )
static unsigned int tree_nrv ( )
   Main entry point for return value optimizations.

   If this function always returns the same local variable, and that
   local variable is an aggregate type, then replace the variable with
   the function's DECL_RESULT.

   This is the equivalent of the C++ named return value optimization
   applied to optimized trees in a language independent form.  If we
   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.  
     If this function does not return an aggregate type in memory, then
     there is nothing to do.  
     If a GIMPLE type is returned in memory, finalize_nrv_r might create
     If the front end already did something like this, don't do it here.  
     If the result has its address taken then it might be modified
     by means not detected in the following loop.  Bail out in this
     Look through each block for assignments to the RESULT_DECL.  
                 In a function with an aggregate return value, the
                 gimplifier has changed all non-empty RETURN_EXPRs to
                 return the RESULT_DECL.  
                 Now verify that this return statement uses the same value
                 as any previously encountered return statement.  
                     If we found a return statement using a different variable
                     than previous return statements, then we can not perform
                     NRV optimizations.  
                 The returned value must be a local automatic variable of the
                 same type and alignment as the function's result.  
                  If there's any MODIFY of component of RESULT,
                  then bail out.  
     If dumping details, then note once and only the NRV replacement.  
     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 if it will be useful.  But don't set
     DECL_ABSTRACT_ORIGIN to point at another function.  
     Now walk through the function changing all references to VAR to be
             If this is a copy from VAR to RESULT, remove it.  

References current_function_decl, get_base_address(), gimple_assign_copy_p(), gimple_assign_rhs1(), gimple_get_lhs(), gimple_has_lhs(), gimple_return_retval(), gsi_end_p(), gsi_next(), gsi_start_bb(), gsi_stmt(), and useless_type_conversion_p().

Referenced by gate_pass_return_slot().