1*38fd1498Szrj /* Language independent return value optimizations
2*38fd1498Szrj Copyright (C) 2004-2018 Free Software Foundation, Inc.
3*38fd1498Szrj
4*38fd1498Szrj This file is part of GCC.
5*38fd1498Szrj
6*38fd1498Szrj GCC is free software; you can redistribute it and/or modify
7*38fd1498Szrj it under the terms of the GNU General Public License as published by
8*38fd1498Szrj the Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj any later version.
10*38fd1498Szrj
11*38fd1498Szrj GCC is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj GNU General Public License for more details.
15*38fd1498Szrj
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with GCC; see the file COPYING3. If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>. */
19*38fd1498Szrj
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "coretypes.h"
23*38fd1498Szrj #include "backend.h"
24*38fd1498Szrj #include "tree.h"
25*38fd1498Szrj #include "gimple.h"
26*38fd1498Szrj #include "tree-pass.h"
27*38fd1498Szrj #include "ssa.h"
28*38fd1498Szrj #include "tree-pretty-print.h"
29*38fd1498Szrj #include "gimple-iterator.h"
30*38fd1498Szrj #include "gimple-walk.h"
31*38fd1498Szrj #include "internal-fn.h"
32*38fd1498Szrj
33*38fd1498Szrj /* This file implements return value optimizations for functions which
34*38fd1498Szrj return aggregate types.
35*38fd1498Szrj
36*38fd1498Szrj Basically this pass searches the function for return statements which
37*38fd1498Szrj return a local aggregate. When converted to RTL such statements will
38*38fd1498Szrj generate a copy from the local aggregate to final return value destination
39*38fd1498Szrj mandated by the target's ABI.
40*38fd1498Szrj
41*38fd1498Szrj That copy can often be avoided by directly constructing the return value
42*38fd1498Szrj into the final destination mandated by the target's ABI.
43*38fd1498Szrj
44*38fd1498Szrj This is basically a generic equivalent to the C++ front-end's
45*38fd1498Szrj Named Return Value optimization. */
46*38fd1498Szrj
47*38fd1498Szrj struct nrv_data_t
48*38fd1498Szrj {
49*38fd1498Szrj /* This is the temporary (a VAR_DECL) which appears in all of
50*38fd1498Szrj this function's RETURN_EXPR statements. */
51*38fd1498Szrj tree var;
52*38fd1498Szrj
53*38fd1498Szrj /* This is the function's RESULT_DECL. We will replace all occurrences
54*38fd1498Szrj of VAR with RESULT_DECL when we apply this optimization. */
55*38fd1498Szrj tree result;
56*38fd1498Szrj int modified;
57*38fd1498Szrj };
58*38fd1498Szrj
59*38fd1498Szrj static tree finalize_nrv_r (tree *, int *, void *);
60*38fd1498Szrj
61*38fd1498Szrj /* Callback for the tree walker.
62*38fd1498Szrj
63*38fd1498Szrj If TP refers to a RETURN_EXPR, then set the expression being returned
64*38fd1498Szrj to nrv_data->result.
65*38fd1498Szrj
66*38fd1498Szrj If TP refers to nrv_data->var, then replace nrv_data->var with
67*38fd1498Szrj nrv_data->result.
68*38fd1498Szrj
69*38fd1498Szrj If we reach a node where we know all the subtrees are uninteresting,
70*38fd1498Szrj then set *WALK_SUBTREES to zero. */
71*38fd1498Szrj
72*38fd1498Szrj static tree
finalize_nrv_r(tree * tp,int * walk_subtrees,void * data)73*38fd1498Szrj finalize_nrv_r (tree *tp, int *walk_subtrees, void *data)
74*38fd1498Szrj {
75*38fd1498Szrj struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
76*38fd1498Szrj struct nrv_data_t *dp = (struct nrv_data_t *) wi->info;
77*38fd1498Szrj
78*38fd1498Szrj /* No need to walk into types. */
79*38fd1498Szrj if (TYPE_P (*tp))
80*38fd1498Szrj *walk_subtrees = 0;
81*38fd1498Szrj
82*38fd1498Szrj /* Otherwise replace all occurrences of VAR with RESULT. */
83*38fd1498Szrj else if (*tp == dp->var)
84*38fd1498Szrj {
85*38fd1498Szrj *tp = dp->result;
86*38fd1498Szrj dp->modified = 1;
87*38fd1498Szrj }
88*38fd1498Szrj
89*38fd1498Szrj /* Keep iterating. */
90*38fd1498Szrj return NULL_TREE;
91*38fd1498Szrj }
92*38fd1498Szrj
93*38fd1498Szrj /* Main entry point for return value optimizations.
94*38fd1498Szrj
95*38fd1498Szrj If this function always returns the same local variable, and that
96*38fd1498Szrj local variable is an aggregate type, then replace the variable with
97*38fd1498Szrj the function's DECL_RESULT.
98*38fd1498Szrj
99*38fd1498Szrj This is the equivalent of the C++ named return value optimization
100*38fd1498Szrj applied to optimized trees in a language independent form. If we
101*38fd1498Szrj ever encounter languages which prevent this kind of optimization,
102*38fd1498Szrj then we could either have the languages register the optimization or
103*38fd1498Szrj we could change the gating function to check the current language. */
104*38fd1498Szrj
105*38fd1498Szrj namespace {
106*38fd1498Szrj
107*38fd1498Szrj const pass_data pass_data_nrv =
108*38fd1498Szrj {
109*38fd1498Szrj GIMPLE_PASS, /* type */
110*38fd1498Szrj "nrv", /* name */
111*38fd1498Szrj OPTGROUP_NONE, /* optinfo_flags */
112*38fd1498Szrj TV_TREE_NRV, /* tv_id */
113*38fd1498Szrj ( PROP_ssa | PROP_cfg ), /* properties_required */
114*38fd1498Szrj 0, /* properties_provided */
115*38fd1498Szrj 0, /* properties_destroyed */
116*38fd1498Szrj 0, /* todo_flags_start */
117*38fd1498Szrj 0, /* todo_flags_finish */
118*38fd1498Szrj };
119*38fd1498Szrj
120*38fd1498Szrj class pass_nrv : public gimple_opt_pass
121*38fd1498Szrj {
122*38fd1498Szrj public:
pass_nrv(gcc::context * ctxt)123*38fd1498Szrj pass_nrv (gcc::context *ctxt)
124*38fd1498Szrj : gimple_opt_pass (pass_data_nrv, ctxt)
125*38fd1498Szrj {}
126*38fd1498Szrj
127*38fd1498Szrj /* opt_pass methods: */
gate(function *)128*38fd1498Szrj virtual bool gate (function *) { return optimize > 0; }
129*38fd1498Szrj
130*38fd1498Szrj virtual unsigned int execute (function *);
131*38fd1498Szrj
132*38fd1498Szrj }; // class pass_nrv
133*38fd1498Szrj
134*38fd1498Szrj unsigned int
execute(function * fun)135*38fd1498Szrj pass_nrv::execute (function *fun)
136*38fd1498Szrj {
137*38fd1498Szrj tree result = DECL_RESULT (current_function_decl);
138*38fd1498Szrj tree result_type = TREE_TYPE (result);
139*38fd1498Szrj tree found = NULL;
140*38fd1498Szrj basic_block bb;
141*38fd1498Szrj gimple_stmt_iterator gsi;
142*38fd1498Szrj struct nrv_data_t data;
143*38fd1498Szrj
144*38fd1498Szrj /* If this function does not return an aggregate type in memory, then
145*38fd1498Szrj there is nothing to do. */
146*38fd1498Szrj if (!aggregate_value_p (result, current_function_decl))
147*38fd1498Szrj return 0;
148*38fd1498Szrj
149*38fd1498Szrj /* If a GIMPLE type is returned in memory, finalize_nrv_r might create
150*38fd1498Szrj non-GIMPLE. */
151*38fd1498Szrj if (is_gimple_reg_type (result_type))
152*38fd1498Szrj return 0;
153*38fd1498Szrj
154*38fd1498Szrj /* If the front end already did something like this, don't do it here. */
155*38fd1498Szrj if (DECL_NAME (result))
156*38fd1498Szrj return 0;
157*38fd1498Szrj
158*38fd1498Szrj /* If the result has its address taken then it might be modified
159*38fd1498Szrj by means not detected in the following loop. Bail out in this
160*38fd1498Szrj case. */
161*38fd1498Szrj if (TREE_ADDRESSABLE (result))
162*38fd1498Szrj return 0;
163*38fd1498Szrj
164*38fd1498Szrj /* Look through each block for assignments to the RESULT_DECL. */
165*38fd1498Szrj FOR_EACH_BB_FN (bb, fun)
166*38fd1498Szrj {
167*38fd1498Szrj for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
168*38fd1498Szrj {
169*38fd1498Szrj gimple *stmt = gsi_stmt (gsi);
170*38fd1498Szrj tree ret_val;
171*38fd1498Szrj
172*38fd1498Szrj if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
173*38fd1498Szrj {
174*38fd1498Szrj /* In a function with an aggregate return value, the
175*38fd1498Szrj gimplifier has changed all non-empty RETURN_EXPRs to
176*38fd1498Szrj return the RESULT_DECL. */
177*38fd1498Szrj ret_val = gimple_return_retval (return_stmt);
178*38fd1498Szrj if (ret_val)
179*38fd1498Szrj gcc_assert (ret_val == result);
180*38fd1498Szrj }
181*38fd1498Szrj else if (gimple_has_lhs (stmt)
182*38fd1498Szrj && gimple_get_lhs (stmt) == result)
183*38fd1498Szrj {
184*38fd1498Szrj tree rhs;
185*38fd1498Szrj
186*38fd1498Szrj if (!gimple_assign_copy_p (stmt))
187*38fd1498Szrj return 0;
188*38fd1498Szrj
189*38fd1498Szrj rhs = gimple_assign_rhs1 (stmt);
190*38fd1498Szrj
191*38fd1498Szrj /* Now verify that this return statement uses the same value
192*38fd1498Szrj as any previously encountered return statement. */
193*38fd1498Szrj if (found != NULL)
194*38fd1498Szrj {
195*38fd1498Szrj /* If we found a return statement using a different variable
196*38fd1498Szrj than previous return statements, then we can not perform
197*38fd1498Szrj NRV optimizations. */
198*38fd1498Szrj if (found != rhs)
199*38fd1498Szrj return 0;
200*38fd1498Szrj }
201*38fd1498Szrj else
202*38fd1498Szrj found = rhs;
203*38fd1498Szrj
204*38fd1498Szrj /* The returned value must be a local automatic variable of the
205*38fd1498Szrj same type and alignment as the function's result. */
206*38fd1498Szrj if (!VAR_P (found)
207*38fd1498Szrj || TREE_THIS_VOLATILE (found)
208*38fd1498Szrj || !auto_var_in_fn_p (found, current_function_decl)
209*38fd1498Szrj || TREE_ADDRESSABLE (found)
210*38fd1498Szrj || DECL_ALIGN (found) > DECL_ALIGN (result)
211*38fd1498Szrj || !useless_type_conversion_p (result_type,
212*38fd1498Szrj TREE_TYPE (found)))
213*38fd1498Szrj return 0;
214*38fd1498Szrj }
215*38fd1498Szrj else if (gimple_has_lhs (stmt))
216*38fd1498Szrj {
217*38fd1498Szrj tree addr = get_base_address (gimple_get_lhs (stmt));
218*38fd1498Szrj /* If there's any MODIFY of component of RESULT,
219*38fd1498Szrj then bail out. */
220*38fd1498Szrj if (addr && addr == result)
221*38fd1498Szrj return 0;
222*38fd1498Szrj }
223*38fd1498Szrj }
224*38fd1498Szrj }
225*38fd1498Szrj
226*38fd1498Szrj if (!found)
227*38fd1498Szrj return 0;
228*38fd1498Szrj
229*38fd1498Szrj /* If dumping details, then note once and only the NRV replacement. */
230*38fd1498Szrj if (dump_file && (dump_flags & TDF_DETAILS))
231*38fd1498Szrj {
232*38fd1498Szrj fprintf (dump_file, "NRV Replaced: ");
233*38fd1498Szrj print_generic_expr (dump_file, found, dump_flags);
234*38fd1498Szrj fprintf (dump_file, " with: ");
235*38fd1498Szrj print_generic_expr (dump_file, result, dump_flags);
236*38fd1498Szrj fprintf (dump_file, "\n");
237*38fd1498Szrj }
238*38fd1498Szrj
239*38fd1498Szrj /* At this point we know that all the return statements return the
240*38fd1498Szrj same local which has suitable attributes for NRV. Copy debugging
241*38fd1498Szrj information from FOUND to RESULT if it will be useful. But don't set
242*38fd1498Szrj DECL_ABSTRACT_ORIGIN to point at another function. */
243*38fd1498Szrj if (!DECL_IGNORED_P (found)
244*38fd1498Szrj && !(DECL_ABSTRACT_ORIGIN (found)
245*38fd1498Szrj && DECL_CONTEXT (DECL_ABSTRACT_ORIGIN (found)) != current_function_decl))
246*38fd1498Szrj {
247*38fd1498Szrj DECL_NAME (result) = DECL_NAME (found);
248*38fd1498Szrj DECL_SOURCE_LOCATION (result) = DECL_SOURCE_LOCATION (found);
249*38fd1498Szrj DECL_ABSTRACT_ORIGIN (result) = DECL_ABSTRACT_ORIGIN (found);
250*38fd1498Szrj }
251*38fd1498Szrj
252*38fd1498Szrj TREE_ADDRESSABLE (result) |= TREE_ADDRESSABLE (found);
253*38fd1498Szrj
254*38fd1498Szrj /* Now walk through the function changing all references to VAR to be
255*38fd1498Szrj RESULT. */
256*38fd1498Szrj data.var = found;
257*38fd1498Szrj data.result = result;
258*38fd1498Szrj FOR_EACH_BB_FN (bb, fun)
259*38fd1498Szrj {
260*38fd1498Szrj for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
261*38fd1498Szrj {
262*38fd1498Szrj gimple *stmt = gsi_stmt (gsi);
263*38fd1498Szrj /* If this is a copy from VAR to RESULT, remove it. */
264*38fd1498Szrj if (gimple_assign_copy_p (stmt)
265*38fd1498Szrj && gimple_assign_lhs (stmt) == result
266*38fd1498Szrj && gimple_assign_rhs1 (stmt) == found)
267*38fd1498Szrj {
268*38fd1498Szrj unlink_stmt_vdef (stmt);
269*38fd1498Szrj gsi_remove (&gsi, true);
270*38fd1498Szrj release_defs (stmt);
271*38fd1498Szrj }
272*38fd1498Szrj else
273*38fd1498Szrj {
274*38fd1498Szrj struct walk_stmt_info wi;
275*38fd1498Szrj memset (&wi, 0, sizeof (wi));
276*38fd1498Szrj wi.info = &data;
277*38fd1498Szrj data.modified = 0;
278*38fd1498Szrj walk_gimple_op (stmt, finalize_nrv_r, &wi);
279*38fd1498Szrj if (data.modified)
280*38fd1498Szrj update_stmt (stmt);
281*38fd1498Szrj gsi_next (&gsi);
282*38fd1498Szrj }
283*38fd1498Szrj }
284*38fd1498Szrj }
285*38fd1498Szrj
286*38fd1498Szrj SET_DECL_VALUE_EXPR (found, result);
287*38fd1498Szrj DECL_HAS_VALUE_EXPR_P (found) = 1;
288*38fd1498Szrj
289*38fd1498Szrj return 0;
290*38fd1498Szrj }
291*38fd1498Szrj
292*38fd1498Szrj } // anon namespace
293*38fd1498Szrj
294*38fd1498Szrj gimple_opt_pass *
make_pass_nrv(gcc::context * ctxt)295*38fd1498Szrj make_pass_nrv (gcc::context *ctxt)
296*38fd1498Szrj {
297*38fd1498Szrj return new pass_nrv (ctxt);
298*38fd1498Szrj }
299*38fd1498Szrj
300*38fd1498Szrj /* Determine (pessimistically) whether DEST is available for NRV
301*38fd1498Szrj optimization, where DEST is expected to be the LHS of a modify
302*38fd1498Szrj expression where the RHS is a function returning an aggregate.
303*38fd1498Szrj
304*38fd1498Szrj DEST is available if it is not clobbered or used by the call. */
305*38fd1498Szrj
306*38fd1498Szrj static bool
dest_safe_for_nrv_p(gcall * call)307*38fd1498Szrj dest_safe_for_nrv_p (gcall *call)
308*38fd1498Szrj {
309*38fd1498Szrj tree dest = gimple_call_lhs (call);
310*38fd1498Szrj
311*38fd1498Szrj dest = get_base_address (dest);
312*38fd1498Szrj if (! dest)
313*38fd1498Szrj return false;
314*38fd1498Szrj
315*38fd1498Szrj if (TREE_CODE (dest) == SSA_NAME)
316*38fd1498Szrj return true;
317*38fd1498Szrj
318*38fd1498Szrj if (call_may_clobber_ref_p (call, dest)
319*38fd1498Szrj || ref_maybe_used_by_stmt_p (call, dest))
320*38fd1498Szrj return false;
321*38fd1498Szrj
322*38fd1498Szrj return true;
323*38fd1498Szrj }
324*38fd1498Szrj
325*38fd1498Szrj /* Walk through the function looking for GIMPLE_ASSIGNs with calls that
326*38fd1498Szrj return in memory on the RHS. For each of these, determine whether it is
327*38fd1498Szrj safe to pass the address of the LHS as the return slot, and mark the
328*38fd1498Szrj call appropriately if so.
329*38fd1498Szrj
330*38fd1498Szrj The NRV shares the return slot with a local variable in the callee; this
331*38fd1498Szrj optimization shares the return slot with the target of the call within
332*38fd1498Szrj the caller. If the NRV is performed (which we can't know in general),
333*38fd1498Szrj this optimization is safe if the address of the target has not
334*38fd1498Szrj escaped prior to the call. If it has, modifications to the local
335*38fd1498Szrj variable will produce visible changes elsewhere, as in PR c++/19317. */
336*38fd1498Szrj
337*38fd1498Szrj namespace {
338*38fd1498Szrj
339*38fd1498Szrj const pass_data pass_data_return_slot =
340*38fd1498Szrj {
341*38fd1498Szrj GIMPLE_PASS, /* type */
342*38fd1498Szrj "retslot", /* name */
343*38fd1498Szrj OPTGROUP_NONE, /* optinfo_flags */
344*38fd1498Szrj TV_NONE, /* tv_id */
345*38fd1498Szrj PROP_ssa, /* properties_required */
346*38fd1498Szrj 0, /* properties_provided */
347*38fd1498Szrj 0, /* properties_destroyed */
348*38fd1498Szrj 0, /* todo_flags_start */
349*38fd1498Szrj 0, /* todo_flags_finish */
350*38fd1498Szrj };
351*38fd1498Szrj
352*38fd1498Szrj class pass_return_slot : public gimple_opt_pass
353*38fd1498Szrj {
354*38fd1498Szrj public:
pass_return_slot(gcc::context * ctxt)355*38fd1498Szrj pass_return_slot (gcc::context *ctxt)
356*38fd1498Szrj : gimple_opt_pass (pass_data_return_slot, ctxt)
357*38fd1498Szrj {}
358*38fd1498Szrj
359*38fd1498Szrj /* opt_pass methods: */
360*38fd1498Szrj virtual unsigned int execute (function *);
361*38fd1498Szrj
362*38fd1498Szrj }; // class pass_return_slot
363*38fd1498Szrj
364*38fd1498Szrj unsigned int
execute(function * fun)365*38fd1498Szrj pass_return_slot::execute (function *fun)
366*38fd1498Szrj {
367*38fd1498Szrj basic_block bb;
368*38fd1498Szrj
369*38fd1498Szrj FOR_EACH_BB_FN (bb, fun)
370*38fd1498Szrj {
371*38fd1498Szrj gimple_stmt_iterator gsi;
372*38fd1498Szrj for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
373*38fd1498Szrj {
374*38fd1498Szrj gcall *stmt;
375*38fd1498Szrj bool slot_opt_p;
376*38fd1498Szrj
377*38fd1498Szrj stmt = dyn_cast <gcall *> (gsi_stmt (gsi));
378*38fd1498Szrj if (stmt
379*38fd1498Szrj && gimple_call_lhs (stmt)
380*38fd1498Szrj && !gimple_call_return_slot_opt_p (stmt)
381*38fd1498Szrj /* Ignore internal functions without direct optabs,
382*38fd1498Szrj those are expanded specially and aggregate_value_p
383*38fd1498Szrj on their result might result in undesirable warnings
384*38fd1498Szrj with some backends. */
385*38fd1498Szrj && (!gimple_call_internal_p (stmt)
386*38fd1498Szrj || direct_internal_fn_p (gimple_call_internal_fn (stmt)))
387*38fd1498Szrj && aggregate_value_p (TREE_TYPE (gimple_call_lhs (stmt)),
388*38fd1498Szrj gimple_call_fndecl (stmt)))
389*38fd1498Szrj {
390*38fd1498Szrj /* Check if the location being assigned to is
391*38fd1498Szrj clobbered by the call. */
392*38fd1498Szrj slot_opt_p = dest_safe_for_nrv_p (stmt);
393*38fd1498Szrj gimple_call_set_return_slot_opt (stmt, slot_opt_p);
394*38fd1498Szrj }
395*38fd1498Szrj }
396*38fd1498Szrj }
397*38fd1498Szrj return 0;
398*38fd1498Szrj }
399*38fd1498Szrj
400*38fd1498Szrj } // anon namespace
401*38fd1498Szrj
402*38fd1498Szrj gimple_opt_pass *
make_pass_return_slot(gcc::context * ctxt)403*38fd1498Szrj make_pass_return_slot (gcc::context *ctxt)
404*38fd1498Szrj {
405*38fd1498Szrj return new pass_return_slot (ctxt);
406*38fd1498Szrj }
407