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