Commit 738e2810 by Mark Mitchell Committed by Mark Mitchell

expr.c (cplus_expand_expr): Preserve temporaries when expanding STMT_EXPRs.

	* expr.c (cplus_expand_expr): Preserve temporaries when expanding
	STMT_EXPRs.
	* optimize.c (struct inline_data): Add target_exprs field.
	(declare_return_variable): When a function returns an aggregate,
	use the variable declared in the TARGET_EXPR as the remapped
	DECL_RESULT.
	(expand_call_inline): Update the pending target_exprs stack.
	(optimize_function): Initialize the stack.

From-SVN: r33561
parent e3fdc58a
2000-04-30 Mark Mitchell <mark@codesourcery.com> 2000-04-30 Mark Mitchell <mark@codesourcery.com>
* expr.c (cplus_expand_expr): Preserve temporaries when expanding
STMT_EXPRs.
* optimize.c (struct inline_data): Add target_exprs field.
(declare_return_variable): When a function returns an aggregate,
use the variable declared in the TARGET_EXPR as the remapped
DECL_RESULT.
(expand_call_inline): Update the pending target_exprs stack.
(optimize_function): Initialize the stack.
* decl2.c (finish_file): Fix typo in comment. * decl2.c (finish_file): Fix typo in comment.
* method.c (emit_thunk): Don't try to return a `void' value. * method.c (emit_thunk): Don't try to return a `void' value.
......
...@@ -143,10 +143,21 @@ cplus_expand_expr (exp, target, tmode, modifier) ...@@ -143,10 +143,21 @@ cplus_expand_expr (exp, target, tmode, modifier)
case STMT_EXPR: case STMT_EXPR:
{ {
tree rtl_expr = expand_start_stmt_expr (); tree rtl_expr;
rtx result;
/* Since expand_expr_stmt calls free_temp_slots after every
expression statement, we must call push_temp_slots here.
Otherwise, any temporaries in use now would be considered
out-of-scope after the first EXPR_STMT from within the
STMT_EXPR. */
push_temp_slots ();
rtl_expr = expand_start_stmt_expr ();
expand_stmt (STMT_EXPR_STMT (exp)); expand_stmt (STMT_EXPR_STMT (exp));
expand_end_stmt_expr (rtl_expr); expand_end_stmt_expr (rtl_expr);
return expand_expr (rtl_expr, target, tmode, modifier); result = expand_expr (rtl_expr, target, tmode, modifier);
pop_temp_slots ();
return result;
} }
break; break;
......
...@@ -60,6 +60,8 @@ typedef struct inline_data ...@@ -60,6 +60,8 @@ typedef struct inline_data
/* Nonzero if we are currently within the cleanup for a /* Nonzero if we are currently within the cleanup for a
TARGET_EXPR. */ TARGET_EXPR. */
int in_target_cleanup_p; int in_target_cleanup_p;
/* A stack of the TARGET_EXPRs that we are currently processing. */
varray_type target_exprs;
} inline_data; } inline_data;
/* Prototypes. */ /* Prototypes. */
...@@ -462,6 +464,7 @@ declare_return_variable (id, use_stmt) ...@@ -462,6 +464,7 @@ declare_return_variable (id, use_stmt)
tree fn = VARRAY_TOP_TREE (id->fns); tree fn = VARRAY_TOP_TREE (id->fns);
tree result = DECL_RESULT (fn); tree result = DECL_RESULT (fn);
tree var; tree var;
int aggregate_return_p;
/* We don't need to do anything for functions that don't return /* We don't need to do anything for functions that don't return
anything. */ anything. */
...@@ -472,8 +475,29 @@ declare_return_variable (id, use_stmt) ...@@ -472,8 +475,29 @@ declare_return_variable (id, use_stmt)
return NULL_TREE; return NULL_TREE;
} }
/* Make an appropriate copy. */ /* Figure out whether or not FN returns an aggregate. */
var = copy_decl_for_inlining (result, fn, VARRAY_TREE (id->fns, 0)); 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 /* Register the VAR_DECL as the equivalent for the RESULT_DECL; that
way, when the RESULT_DECL is encountered, it will be way, when the RESULT_DECL is encountered, it will be
automatically replaced by the VAR_DECL. */ automatically replaced by the VAR_DECL. */
...@@ -484,8 +508,14 @@ declare_return_variable (id, use_stmt) ...@@ -484,8 +508,14 @@ declare_return_variable (id, use_stmt)
/* Build the USE_STMT. */ /* Build the USE_STMT. */
*use_stmt = build_min_nt (EXPR_STMT, var); *use_stmt = build_min_nt (EXPR_STMT, var);
/* Build the declaration statement. */ /* Build the declaration statement if FN does not return an
return build_min_nt (DECL_STMT, var); 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. */ /* Returns non-zero if FN is a function that can be inlined. */
...@@ -586,6 +616,9 @@ expand_call_inline (tp, walk_subtrees, data) ...@@ -586,6 +616,9 @@ expand_call_inline (tp, walk_subtrees, data)
/* We're walking our own subtrees. */ /* We're walking our own subtrees. */
*walk_subtrees = 0; *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 /* Actually walk over them. This loop is the body of
walk_trees, omitting the case where the TARGET_EXPR walk_trees, omitting the case where the TARGET_EXPR
itself is handled. */ itself is handled. */
...@@ -598,6 +631,9 @@ expand_call_inline (tp, walk_subtrees, data) ...@@ -598,6 +631,9 @@ expand_call_inline (tp, walk_subtrees, data)
--id->in_target_cleanup_p; --id->in_target_cleanup_p;
} }
/* We're done with this TARGET_EXPR now. */
VARRAY_POP (id->target_exprs);
return NULL_TREE; return NULL_TREE;
} }
...@@ -780,12 +816,16 @@ optimize_function (fn) ...@@ -780,12 +816,16 @@ optimize_function (fn)
prev_fn = s->function_decl; 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 /* Replace all calls to inline functions with the bodies of those
functions. */ functions. */
expand_calls_inline (&DECL_SAVED_TREE (fn), &id); expand_calls_inline (&DECL_SAVED_TREE (fn), &id);
/* Clean up. */ /* Clean up. */
VARRAY_FREE (id.fns); VARRAY_FREE (id.fns);
VARRAY_FREE (id.target_exprs);
} }
} }
......
// Special g++ Options: -O1
// Origin: Mark Mitchell <mark@codesourcery.com>
int result;
struct S
{
S ();
S (const S&);
~S ();
int i;
double d[18];
};
S* s;
S::S ()
{
s = this;
}
S::~S ()
{
if (s != this)
result = 1;
}
inline S f ()
{
return S ();
}
int main ()
{
f ();
return result;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment