diff options
Diffstat (limited to 'gcc/cp/optimize.c')
| -rw-r--r-- | gcc/cp/optimize.c | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/gcc/cp/optimize.c b/gcc/cp/optimize.c index e683f7e..a83b684 100644 --- a/gcc/cp/optimize.c +++ b/gcc/cp/optimize.c @@ -60,6 +60,8 @@ typedef struct inline_data /* Nonzero if we are currently within the cleanup for a TARGET_EXPR. */ int in_target_cleanup_p; + /* A stack of the TARGET_EXPRs that we are currently processing. */ + varray_type target_exprs; } inline_data; /* Prototypes. */ @@ -462,6 +464,7 @@ declare_return_variable (id, use_stmt) tree fn = VARRAY_TOP_TREE (id->fns); tree result = DECL_RESULT (fn); tree var; + int aggregate_return_p; /* We don't need to do anything for functions that don't return anything. */ @@ -472,8 +475,29 @@ declare_return_variable (id, use_stmt) return NULL_TREE; } - /* Make an appropriate copy. */ - var = copy_decl_for_inlining (result, fn, VARRAY_TREE (id->fns, 0)); + /* Figure out whether or not FN returns an aggregate. */ + aggregate_return_p = IS_AGGR_TYPE (TREE_TYPE (result)); + + /* If FN returns an aggregate then the caller will always create the + temporary (using a TARGET_EXPR) and the call will be the + initializing expression for the TARGET_EXPR. If we were just to + create a new VAR_DECL here, then the result of this function + would be copied (bitwise) into the variable initialized by the + TARGET_EXPR. That's incorrect, so we must transform any + references to the RESULT into references to the target. */ + if (aggregate_return_p) + { + my_friendly_assert (id->target_exprs->elements_used != 0, + 20000430); + var = TREE_OPERAND (VARRAY_TOP_TREE (id->target_exprs), 0); + my_friendly_assert (same_type_p (TREE_TYPE (var), + TREE_TYPE (result)), + 20000430); + } + /* Otherwise, make an appropriate copy. */ + else + var = copy_decl_for_inlining (result, fn, VARRAY_TREE (id->fns, 0)); + /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that way, when the RESULT_DECL is encountered, it will be automatically replaced by the VAR_DECL. */ @@ -484,8 +508,14 @@ declare_return_variable (id, use_stmt) /* Build the USE_STMT. */ *use_stmt = build_min_nt (EXPR_STMT, var); - /* Build the declaration statement. */ - return build_min_nt (DECL_STMT, var); + /* Build the declaration statement if FN does not return an + aggregate. */ + if (!aggregate_return_p) + return build_min_nt (DECL_STMT, var); + /* If FN does return an aggregate, there's no need to declare the + return variable; we're using a variable in our caller's frame. */ + else + return NULL_TREE; } /* Returns non-zero if FN is a function that can be inlined. */ @@ -586,6 +616,9 @@ expand_call_inline (tp, walk_subtrees, data) /* We're walking our own subtrees. */ *walk_subtrees = 0; + /* Push *TP on the stack of pending TARGET_EXPRs. */ + VARRAY_PUSH_TREE (id->target_exprs, *tp); + /* Actually walk over them. This loop is the body of walk_trees, omitting the case where the TARGET_EXPR itself is handled. */ @@ -598,6 +631,9 @@ expand_call_inline (tp, walk_subtrees, data) --id->in_target_cleanup_p; } + /* We're done with this TARGET_EXPR now. */ + VARRAY_POP (id->target_exprs); + return NULL_TREE; } @@ -780,12 +816,16 @@ optimize_function (fn) prev_fn = s->function_decl; } + /* Create the stack of TARGET_EXPRs. */ + VARRAY_TREE_INIT (id.target_exprs, 32, "target_exprs"); + /* Replace all calls to inline functions with the bodies of those functions. */ expand_calls_inline (&DECL_SAVED_TREE (fn), &id); /* Clean up. */ VARRAY_FREE (id.fns); + VARRAY_FREE (id.target_exprs); } } |
