]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/optimize.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[thirdparty/gcc.git] / gcc / cp / optimize.c
CommitLineData
31236dcd 1/* Perform optimizations on tree structure.
8e8f6434 2 Copyright (C) 1998-2018 Free Software Foundation, Inc.
31236dcd 3 Written by Mark Michell (mark@codesourcery.com).
4
6f0d25a6 5This file is part of GCC.
31236dcd 6
6f0d25a6 7GCC is free software; you can redistribute it and/or modify it
b278476e 8under the terms of the GNU General Public License as published by
aa139c3f 9the Free Software Foundation; either version 3, or (at your option)
b278476e 10any later version.
11
6f0d25a6 12GCC is distributed in the hope that it will be useful, but
b278476e 13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
4d26ca8c 16
b278476e 17You should have received a copy of the GNU General Public License
aa139c3f 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
31236dcd 20
21#include "config.h"
22#include "system.h"
805e22b2 23#include "coretypes.h"
4cba6f60 24#include "target.h"
31236dcd 25#include "cp-tree.h"
4cba6f60 26#include "stringpool.h"
27#include "cgraph.h"
c37d72e9 28#include "debug.h"
1e3b023c 29#include "tree-inline.h"
bfec3452 30#include "tree-iterator.h"
31236dcd 31
31236dcd 32/* Prototypes. */
33
ff64eef0 34static void update_cloned_parm (tree, tree, bool);
cd25c883 35
b8792625 36/* CLONED_PARM is a copy of CLONE, generated for a cloned constructor
37 or destructor. Update it to ensure that the source-position for
38 the cloned parameter matches that for the original, and that the
39 debugging generation code will be able to find the original PARM. */
40
41static void
ff64eef0 42update_cloned_parm (tree parm, tree cloned_parm, bool first)
b8792625 43{
44 DECL_ABSTRACT_ORIGIN (cloned_parm) = parm;
b00bfd31 45
47cd6605 46 /* We may have taken its address. */
b00bfd31 47 TREE_ADDRESSABLE (cloned_parm) = TREE_ADDRESSABLE (parm);
48
bdb5503b 49 DECL_BY_REFERENCE (cloned_parm) = DECL_BY_REFERENCE (parm);
50
47cd6605 51 /* The definition might have different constness. */
b00bfd31 52 TREE_READONLY (cloned_parm) = TREE_READONLY (parm);
9031d10b 53
ff64eef0 54 TREE_USED (cloned_parm) = !first || TREE_USED (parm);
9031d10b 55
47cd6605 56 /* The name may have changed from the declaration. */
b8792625 57 DECL_NAME (cloned_parm) = DECL_NAME (parm);
346064d9 58 DECL_SOURCE_LOCATION (cloned_parm) = DECL_SOURCE_LOCATION (parm);
39908afa 59 TREE_TYPE (cloned_parm) = TREE_TYPE (parm);
b58456b5 60
61 DECL_GIMPLE_REG_P (cloned_parm) = DECL_GIMPLE_REG_P (parm);
b8792625 62}
63
75a70cf9 64
65/* FN is a function in High GIMPLE form that has a complete body and no
66 CFG. CLONE is a function whose body is to be set to a copy of FN,
67 mapping argument declarations according to the ARG_MAP splay_tree. */
32020b10 68
69static void
70clone_body (tree clone, tree fn, void *arg_map)
71{
72 copy_body_data id;
bfec3452 73 tree stmts;
32020b10 74
75a70cf9 75 /* Clone the body, as if we were making an inline call. But, remap
76 the parameters in the callee to the parameters of caller. */
32020b10 77 memset (&id, 0, sizeof (id));
78 id.src_fn = fn;
79 id.dst_fn = clone;
80 id.src_cfun = DECL_STRUCT_FUNCTION (fn);
06ecf488 81 id.decl_map = static_cast<hash_map<tree, tree> *> (arg_map);
32020b10 82
83 id.copy_decl = copy_decl_no_change;
84 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
85 id.transform_new_cfg = true;
86 id.transform_return_to_modify = false;
75a70cf9 87 id.transform_lang_insert_block = NULL;
32020b10 88
89 /* We're not inside any EH region. */
e38def9c 90 id.eh_lp_nr = 0;
32020b10 91
bfec3452 92 stmts = DECL_SAVED_TREE (fn);
93 walk_tree (&stmts, copy_tree_body_r, &id, NULL);
a2ad849c 94
95 /* Also remap the initializer of any static variables so that they (in
96 particular, any label addresses) correspond to the base variant rather
97 than the abstract one. */
98 if (DECL_NAME (clone) == base_dtor_identifier
99 || DECL_NAME (clone) == base_ctor_identifier)
100 {
2ab2ce89 101 unsigned ix;
102 tree decl;
103
104 FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (fn), ix, decl)
105 walk_tree (&DECL_INITIAL (decl), copy_tree_body_r, &id, NULL);
a2ad849c 106 }
107
bfec3452 108 append_to_statement_list_force (stmts, &DECL_SAVED_TREE (clone));
32020b10 109}
110
e2932b0d 111/* DELETE_DTOR is a delete destructor whose body will be built.
112 COMPLETE_DTOR is the corresponding complete destructor. */
113
114static void
115build_delete_destructor_body (tree delete_dtor, tree complete_dtor)
116{
e2932b0d 117 tree parm = DECL_ARGUMENTS (delete_dtor);
118 tree virtual_size = cxx_sizeof (current_class_type);
119
e2932b0d 120 /* Call the delete function. */
2b073aa6 121 tree call_delete = build_op_delete_call (DELETE_EXPR, current_class_ptr,
122 virtual_size,
123 /*global_p=*/false,
124 /*placement=*/NULL_TREE,
125 /*alloc_fn=*/NULL_TREE,
126 tf_warning_or_error);
127
76b94d4b 128 tree op = get_callee_fndecl (call_delete);
129 if (op && DECL_P (op) && destroying_delete_p (op))
130 {
131 /* The destroying delete will handle calling complete_dtor. */
132 add_stmt (call_delete);
133 }
134 else
135 {
136 /* Call the corresponding complete destructor. */
137 gcc_assert (complete_dtor);
138 tree call_dtor = build_cxx_call (complete_dtor, 1, &parm,
139 tf_warning_or_error);
140
141 /* Operator delete must be called, whether or not the dtor throws. */
142 add_stmt (build2 (TRY_FINALLY_EXPR, void_type_node,
143 call_dtor, call_delete));
144 }
e2932b0d 145
76b94d4b 146 /* Return the address of the object.
147 ??? How is it useful to return an invalid address? */
e2932b0d 148 if (targetm.cxx.cdtor_returns_this ())
149 {
150 tree val = DECL_ARGUMENTS (delete_dtor);
151 val = build2 (MODIFY_EXPR, TREE_TYPE (val),
152 DECL_RESULT (delete_dtor), val);
153 add_stmt (build_stmt (0, RETURN_EXPR, val));
154 }
155}
156
1d4ef5e7 157/* Return name of comdat group for complete and base ctor (or dtor)
158 that have the same body. If dtor is virtual, deleting dtor goes
159 into this comdat group as well. */
160
161static tree
162cdtor_comdat_group (tree complete, tree base)
163{
237d7956 164 tree complete_name = DECL_ASSEMBLER_NAME (complete);
165 tree base_name = DECL_ASSEMBLER_NAME (base);
1d4ef5e7 166 char *grp_name;
167 const char *p, *q;
168 bool diff_seen = false;
169 size_t idx;
1d4ef5e7 170 gcc_assert (IDENTIFIER_LENGTH (complete_name)
171 == IDENTIFIER_LENGTH (base_name));
172 grp_name = XALLOCAVEC (char, IDENTIFIER_LENGTH (complete_name) + 1);
173 p = IDENTIFIER_POINTER (complete_name);
174 q = IDENTIFIER_POINTER (base_name);
175 for (idx = 0; idx < IDENTIFIER_LENGTH (complete_name); idx++)
176 if (p[idx] == q[idx])
177 grp_name[idx] = p[idx];
178 else
179 {
180 gcc_assert (!diff_seen
181 && idx > 0
7896267d 182 && (p[idx - 1] == 'C' || p[idx - 1] == 'D'
183 || p[idx - 1] == 'I')
1d4ef5e7 184 && p[idx] == '1'
185 && q[idx] == '2');
186 grp_name[idx] = '5';
187 diff_seen = true;
188 }
189 grp_name[idx] = '\0';
237d7956 190 gcc_assert (diff_seen);
1d4ef5e7 191 return get_identifier (grp_name);
192}
193
468088ac 194/* Returns true iff we can make the base and complete [cd]tor aliases of
195 the same symbol rather than separate functions. */
e55cba4c 196
468088ac 197static bool
198can_alias_cdtor (tree fn)
e55cba4c 199{
468088ac 200 /* If aliases aren't supported by the assembler, fail. */
07b8a412 201 if (!TARGET_SUPPORTS_ALIASES)
202 return false;
203
468088ac 204 /* We can't use an alias if there are virtual bases. */
205 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn)))
206 return false;
207 /* ??? Why not use aliases with -frepo? */
208 if (flag_use_repository)
209 return false;
c6e04fca 210 gcc_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn));
468088ac 211 /* Don't use aliases for weak/linkonce definitions unless we can put both
212 symbols in the same COMDAT group. */
213 return (DECL_INTERFACE_KNOWN (fn)
214 && (SUPPORTS_ONE_ONLY || !DECL_WEAK (fn))
215 && (!DECL_ONE_ONLY (fn)
216 || (HAVE_COMDAT_GROUP && DECL_WEAK (fn))));
217}
e55cba4c 218
468088ac 219/* FN is a [cd]tor, fns is a pointer to an array of length 3. Fill fns
220 with pointers to the base, complete, and deleting variants. */
e55cba4c 221
468088ac 222static void
223populate_clone_array (tree fn, tree *fns)
224{
225 tree clone;
8f80e66d 226
ed772161 227 fns[0] = NULL_TREE;
228 fns[1] = NULL_TREE;
229 fns[2] = NULL_TREE;
230
e2932b0d 231 /* Look for the complete destructor which may be used to build the
232 delete destructor. */
233 FOR_EACH_CLONE (clone, fn)
ed772161 234 if (DECL_NAME (clone) == complete_dtor_identifier
235 || DECL_NAME (clone) == complete_ctor_identifier)
236 fns[1] = clone;
237 else if (DECL_NAME (clone) == base_dtor_identifier
238 || DECL_NAME (clone) == base_ctor_identifier)
239 fns[0] = clone;
240 else if (DECL_NAME (clone) == deleting_dtor_identifier)
241 fns[2] = clone;
242 else
243 gcc_unreachable ();
468088ac 244}
245
246/* FN is a constructor or destructor, and there are FUNCTION_DECLs
247 cloned from it nearby. Instead of cloning this body, leave it
248 alone and create tiny one-call bodies for the cloned
249 FUNCTION_DECLs. These clones are sibcall candidates, and their
250 resulting code will be very thunk-esque. */
251
252static bool
253maybe_thunk_body (tree fn, bool force)
254{
255 tree bind, block, call, clone, clone_result, fn_parm, fn_parm_typelist;
256 tree last_arg, modify, *args;
257 int parmno, vtt_parmno, max_parms;
258 tree fns[3];
259
260 if (!force && !flag_declone_ctor_dtor)
261 return 0;
262
263 /* If function accepts variable arguments, give up. */
264 last_arg = tree_last (TYPE_ARG_TYPES (TREE_TYPE (fn)));
265 if (last_arg != void_list_node)
266 return 0;
267
268 /* If we got this far, we've decided to turn the clones into thunks. */
269
270 /* We're going to generate code for fn, so it is no longer "abstract."
271 Also make the unified ctor/dtor private to either the translation unit
272 (for non-vague linkage ctors) or the COMDAT group (otherwise). */
273
274 populate_clone_array (fn, fns);
7896267d 275
c1585b9c 276 /* Can happen during error recovery (c++/71464). */
277 if (!fns[0] || !fns[1])
278 return 0;
279
7896267d 280 /* Don't use thunks if the base clone omits inherited parameters. */
c1585b9c 281 if (ctor_omit_inherited_parms (fns[0]))
7896267d 282 return 0;
283
16d41ae2 284 DECL_ABSTRACT_P (fn) = false;
468088ac 285 if (!DECL_WEAK (fn))
286 {
287 TREE_PUBLIC (fn) = false;
288 DECL_EXTERNAL (fn) = false;
289 DECL_INTERFACE_KNOWN (fn) = true;
290 }
291 else if (HAVE_COMDAT_GROUP)
292 {
d3d81eb2 293 /* At eof, defer creation of mangling aliases temporarily. */
294 bool save_defer_mangling_aliases = defer_mangling_aliases;
295 defer_mangling_aliases = true;
468088ac 296 tree comdat_group = cdtor_comdat_group (fns[1], fns[0]);
d3d81eb2 297 defer_mangling_aliases = save_defer_mangling_aliases;
415d1b9a 298 cgraph_node::get_create (fns[0])->set_comdat_group (comdat_group);
299 cgraph_node::get_create (fns[1])->add_to_same_comdat_group
300 (cgraph_node::get_create (fns[0]));
301 symtab_node::get (fn)->add_to_same_comdat_group
302 (symtab_node::get (fns[0]));
468088ac 303 if (fns[2])
304 /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
305 virtual, it goes into the same comdat group as well. */
415d1b9a 306 cgraph_node::get_create (fns[2])->add_to_same_comdat_group
307 (symtab_node::get (fns[0]));
d3d81eb2 308 /* Emit them now that the thunks are same comdat group aliases. */
309 if (!save_defer_mangling_aliases)
310 generate_mangling_aliases ();
468088ac 311 TREE_PUBLIC (fn) = false;
312 DECL_EXTERNAL (fn) = false;
313 DECL_INTERFACE_KNOWN (fn) = true;
314 /* function_and_variable_visibility doesn't want !PUBLIC decls to
315 have these flags set. */
316 DECL_WEAK (fn) = false;
317 DECL_COMDAT (fn) = false;
318 }
319
320 /* Find the vtt_parm, if present. */
321 for (vtt_parmno = -1, parmno = 0, fn_parm = DECL_ARGUMENTS (fn);
322 fn_parm;
323 ++parmno, fn_parm = TREE_CHAIN (fn_parm))
324 {
325 if (DECL_ARTIFICIAL (fn_parm)
326 && DECL_NAME (fn_parm) == vtt_parm_identifier)
327 {
328 /* Compensate for removed in_charge parameter. */
329 vtt_parmno = parmno;
330 break;
331 }
332 }
333
334 /* Allocate an argument buffer for build_cxx_call().
335 Make sure it is large enough for any of the clones. */
336 max_parms = 0;
337 FOR_EACH_CLONE (clone, fn)
338 {
339 int length = list_length (DECL_ARGUMENTS (fn));
340 if (length > max_parms)
341 max_parms = length;
342 }
4611960c 343 args = XALLOCAVEC (tree, max_parms);
468088ac 344
ab87ee8f 345 /* We know that any clones immediately follow FN in TYPE_FIELDS. */
468088ac 346 FOR_EACH_CLONE (clone, fn)
347 {
348 tree clone_parm;
349
350 /* If we've already generated a body for this clone, avoid
351 duplicating it. (Is it possible for a clone-list to grow after we
352 first see it?) */
353 if (DECL_SAVED_TREE (clone) || TREE_ASM_WRITTEN (clone))
354 continue;
355
356 /* Start processing the function. */
357 start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
358
359 if (clone == fns[2])
360 {
361 for (clone_parm = DECL_ARGUMENTS (clone); clone_parm;
362 clone_parm = TREE_CHAIN (clone_parm))
363 DECL_ABSTRACT_ORIGIN (clone_parm) = NULL_TREE;
364 /* Build the delete destructor by calling complete destructor and
365 delete function. */
366 build_delete_destructor_body (clone, fns[1]);
367 }
368 else
369 {
370 /* Walk parameter lists together, creating parameter list for
371 call to original function. */
372 for (parmno = 0,
373 fn_parm = DECL_ARGUMENTS (fn),
374 fn_parm_typelist = TYPE_ARG_TYPES (TREE_TYPE (fn)),
375 clone_parm = DECL_ARGUMENTS (clone);
376 fn_parm;
377 ++parmno,
378 fn_parm = TREE_CHAIN (fn_parm))
379 {
380 if (parmno == vtt_parmno && ! DECL_HAS_VTT_PARM_P (clone))
381 {
382 gcc_assert (fn_parm_typelist);
383 /* Clobber argument with formal parameter type. */
384 args[parmno]
385 = convert (TREE_VALUE (fn_parm_typelist),
386 null_pointer_node);
387 }
388 else if (parmno == 1 && DECL_HAS_IN_CHARGE_PARM_P (fn))
389 {
390 tree in_charge
391 = copy_node (in_charge_arg_for_name (DECL_NAME (clone)));
392 args[parmno] = in_charge;
393 }
394 /* Map other parameters to their equivalents in the cloned
395 function. */
396 else
397 {
398 gcc_assert (clone_parm);
399 DECL_ABSTRACT_ORIGIN (clone_parm) = NULL;
400 args[parmno] = clone_parm;
401 clone_parm = TREE_CHAIN (clone_parm);
402 }
403 if (fn_parm_typelist)
404 fn_parm_typelist = TREE_CHAIN (fn_parm_typelist);
405 }
406
407 /* We built this list backwards; fix now. */
408 mark_used (fn);
409 call = build_cxx_call (fn, parmno, args, tf_warning_or_error);
410 /* Arguments passed to the thunk by invisible reference should
411 be transmitted to the callee unchanged. Do not create a
412 temporary and invoke the copy constructor. The thunking
413 transformation must not introduce any constructor calls. */
414 CALL_FROM_THUNK_P (call) = 1;
415 block = make_node (BLOCK);
416 if (targetm.cxx.cdtor_returns_this ())
417 {
418 clone_result = DECL_RESULT (clone);
419 modify = build2 (MODIFY_EXPR, TREE_TYPE (clone_result),
420 clone_result, call);
7663e4b8 421 modify = build1 (RETURN_EXPR, void_type_node, modify);
468088ac 422 add_stmt (modify);
468088ac 423 }
424 else
425 {
426 add_stmt (call);
427 }
428 bind = c_build_bind_expr (DECL_SOURCE_LOCATION (clone),
429 block, cur_stmt_list);
430 DECL_SAVED_TREE (clone) = push_stmt_list ();
431 add_stmt (bind);
432 }
433
434 DECL_ABSTRACT_ORIGIN (clone) = NULL;
4775c814 435 expand_or_defer_fn (finish_function (/*inline_p=*/false));
468088ac 436 }
437 return 1;
438}
439
440/* FN is a function that has a complete body. Clone the body as
441 necessary. Returns nonzero if there's no longer any need to
442 process the main body. */
443
444bool
445maybe_clone_body (tree fn)
446{
447 tree comdat_group = NULL_TREE;
448 tree clone;
449 tree fns[3];
450 bool first = true;
451 int idx;
452 bool need_alias = false;
453
454 /* We only clone constructors and destructors. */
c6e04fca 455 if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (fn))
468088ac 456 return 0;
457
458 populate_clone_array (fn, fns);
e2932b0d 459
a2ad849c 460 /* Remember if we can't have multiple clones for some reason. We need to
461 check this before we remap local static initializers in clone_body. */
462 if (!tree_versionable_function_p (fn))
463 need_alias = true;
464
ab87ee8f 465 /* We know that any clones immediately follow FN in the TYPE_FIELDS
e55cba4c 466 list. */
93c149df 467 push_to_top_level ();
ed772161 468 for (idx = 0; idx < 3; idx++)
e55cba4c 469 {
470 tree parm;
471 tree clone_parm;
e55cba4c 472
ed772161 473 clone = fns[idx];
474 if (!clone)
475 continue;
476
e55cba4c 477 /* Update CLONE's source position information to match FN's. */
346064d9 478 DECL_SOURCE_LOCATION (clone) = DECL_SOURCE_LOCATION (fn);
d3981643 479 DECL_DECLARED_INLINE_P (clone) = DECL_DECLARED_INLINE_P (fn);
611b6173 480 DECL_DECLARED_CONSTEXPR_P (clone) = DECL_DECLARED_CONSTEXPR_P (fn);
0ce25b06 481 DECL_COMDAT (clone) = DECL_COMDAT (fn);
482 DECL_WEAK (clone) = DECL_WEAK (fn);
ecd88073 483
484 /* We don't copy the comdat group from fn to clone because the assembler
485 name of fn was corrupted by write_mangled_name by adding *INTERNAL*
486 to it. By doing so, it also corrupted the comdat group. */
487 if (DECL_ONE_ONLY (fn))
415d1b9a 488 cgraph_node::get_create (clone)->set_comdat_group (cxx_comdat_group (clone));
f753592a 489 DECL_USE_TEMPLATE (clone) = DECL_USE_TEMPLATE (fn);
490 DECL_EXTERNAL (clone) = DECL_EXTERNAL (fn);
491 DECL_INTERFACE_KNOWN (clone) = DECL_INTERFACE_KNOWN (fn);
492 DECL_NOT_REALLY_EXTERN (clone) = DECL_NOT_REALLY_EXTERN (fn);
3418ec43 493 TREE_PUBLIC (clone) = TREE_PUBLIC (fn);
9c40570a 494 DECL_VISIBILITY (clone) = DECL_VISIBILITY (fn);
b212f378 495 DECL_VISIBILITY_SPECIFIED (clone) = DECL_VISIBILITY_SPECIFIED (fn);
578887bb 496 DECL_DLLIMPORT_P (clone) = DECL_DLLIMPORT_P (fn);
8514bbf4 497 DECL_ATTRIBUTES (clone) = copy_list (DECL_ATTRIBUTES (fn));
498 DECL_DISREGARD_INLINE_LIMITS (clone) = DECL_DISREGARD_INLINE_LIMITS (fn);
71e19e54 499 set_decl_section_name (clone, DECL_SECTION_NAME (fn));
e55cba4c 500
47cd6605 501 /* Adjust the parameter names and locations. */
17d36341 502 parm = DECL_ARGUMENTS (fn);
503 clone_parm = DECL_ARGUMENTS (clone);
4cb90235 504 /* Update the `this' parameter, which is always first. */
ff64eef0 505 update_cloned_parm (parm, clone_parm, first);
1767a056 506 parm = DECL_CHAIN (parm);
507 clone_parm = DECL_CHAIN (clone_parm);
17d36341 508 if (DECL_HAS_IN_CHARGE_PARM_P (fn))
1767a056 509 parm = DECL_CHAIN (parm);
17d36341 510 if (DECL_HAS_VTT_PARM_P (fn))
1767a056 511 parm = DECL_CHAIN (parm);
17d36341 512 if (DECL_HAS_VTT_PARM_P (clone))
1767a056 513 clone_parm = DECL_CHAIN (clone_parm);
7896267d 514 for (; parm && clone_parm;
1767a056 515 parm = DECL_CHAIN (parm), clone_parm = DECL_CHAIN (clone_parm))
0767cba9 516 /* Update this parameter. */
ff64eef0 517 update_cloned_parm (parm, clone_parm, first);
468088ac 518 }
519
520 bool can_alias = can_alias_cdtor (fn);
521
522 /* If we decide to turn clones into thunks, they will branch to fn.
523 Must have original function available to call. */
524 if (!can_alias && maybe_thunk_body (fn, need_alias))
525 {
526 pop_from_top_level ();
527 /* We still need to emit the original function. */
528 return 0;
529 }
530
531 /* Emit the DWARF1 abstract instance. */
532 (*debug_hooks->deferred_inline_function) (fn);
533
ab87ee8f 534 /* We know that any clones immediately follow FN in the TYPE_FIELDS. */
468088ac 535 for (idx = 0; idx < 3; idx++)
536 {
537 tree parm;
538 tree clone_parm;
539 int parmno;
06ecf488 540 hash_map<tree, tree> *decl_map;
468088ac 541 bool alias = false;
542
543 clone = fns[idx];
544 if (!clone)
545 continue;
17d36341 546
e55cba4c 547 /* Start processing the function. */
3046c0a3 548 start_preparsed_function (clone, NULL_TREE, SF_PRE_PARSED);
e55cba4c 549
ed772161 550 /* Tell cgraph if both ctors or both dtors are known to have
551 the same body. */
468088ac 552 if (can_alias
ed772161 553 && fns[0]
554 && idx == 1
415d1b9a 555 && cgraph_node::get_create (fns[0])->create_same_body_alias
556 (clone, fns[0]))
28454517 557 {
558 alias = true;
1d4ef5e7 559 if (DECL_ONE_ONLY (fns[0]))
560 {
561 /* For comdat base and complete cdtors put them
562 into the same, *[CD]5* comdat group instead of
563 *[CD][12]*. */
564 comdat_group = cdtor_comdat_group (fns[1], fns[0]);
415d1b9a 565 cgraph_node::get_create (fns[0])->set_comdat_group (comdat_group);
566 if (symtab_node::get (clone)->same_comdat_group)
567 symtab_node::get (clone)->remove_from_same_comdat_group ();
568 symtab_node::get (clone)->add_to_same_comdat_group
569 (symtab_node::get (fns[0]));
1d4ef5e7 570 }
28454517 571 }
ed772161 572
e2932b0d 573 /* Build the delete destructor by calling complete destructor
574 and delete function. */
ed772161 575 if (idx == 2)
dabebf7e 576 {
577 build_delete_destructor_body (clone, fns[1]);
578 /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
579 virtual, it goes into the same comdat group as well. */
580 if (comdat_group)
415d1b9a 581 cgraph_node::get_create (clone)->add_to_same_comdat_group
582 (symtab_node::get (fns[0]));
dabebf7e 583 }
ed772161 584 else if (alias)
585 /* No need to populate body. */ ;
e2932b0d 586 else
ed772161 587 {
a2ad849c 588 /* If we can't have multiple copies of FN (say, because there's a
589 static local initialized with the address of a label), we need
590 to use an alias for the complete variant. */
591 if (idx == 1 && need_alias)
592 {
593 if (DECL_STRUCT_FUNCTION (fn)->cannot_be_copied_set)
594 sorry (DECL_STRUCT_FUNCTION (fn)->cannot_be_copied_reason, fn);
595 else
596 sorry ("making multiple clones of %qD", fn);
597 }
598
e2932b0d 599 /* Remap the parameters. */
06ecf488 600 decl_map = new hash_map<tree, tree>;
e2932b0d 601 for (parmno = 0,
602 parm = DECL_ARGUMENTS (fn),
603 clone_parm = DECL_ARGUMENTS (clone);
604 parm;
605 ++parmno,
1767a056 606 parm = DECL_CHAIN (parm))
e2932b0d 607 {
608 /* Map the in-charge parameter to an appropriate constant. */
609 if (DECL_HAS_IN_CHARGE_PARM_P (fn) && parmno == 1)
610 {
611 tree in_charge;
612 in_charge = in_charge_arg_for_name (DECL_NAME (clone));
06ecf488 613 decl_map->put (parm, in_charge);
e2932b0d 614 }
615 else if (DECL_ARTIFICIAL (parm)
616 && DECL_NAME (parm) == vtt_parm_identifier)
617 {
618 /* For a subobject constructor or destructor, the next
619 argument is the VTT parameter. Remap the VTT_PARM
620 from the CLONE to this parameter. */
621 if (DECL_HAS_VTT_PARM_P (clone))
622 {
623 DECL_ABSTRACT_ORIGIN (clone_parm) = parm;
06ecf488 624 decl_map->put (parm, clone_parm);
1767a056 625 clone_parm = DECL_CHAIN (clone_parm);
e2932b0d 626 }
627 /* Otherwise, map the VTT parameter to `NULL'. */
628 else
06ecf488 629 {
630 tree t
631 = fold_convert (TREE_TYPE (parm), null_pointer_node);
632 decl_map->put (parm, t);
633 }
e2932b0d 634 }
635 /* Map other parameters to their equivalents in the cloned
636 function. */
637 else
638 {
bfd3584e 639 tree replacement;
7896267d 640 if (clone_parm)
bfd3584e 641 {
642 replacement = clone_parm;
643 clone_parm = DECL_CHAIN (clone_parm);
644 }
645 else
646 {
647 /* Inheriting ctors can omit parameters from the base
648 clone. Replace them with null lvalues. */
649 tree reftype = build_reference_type (TREE_TYPE (parm));
650 replacement = fold_convert (reftype, null_pointer_node);
651 replacement = convert_from_reference (replacement);
652 }
653 decl_map->put (parm, replacement);
e2932b0d 654 }
655 }
656
657 if (targetm.cxx.cdtor_returns_this ())
658 {
659 parm = DECL_RESULT (fn);
660 clone_parm = DECL_RESULT (clone);
06ecf488 661 decl_map->put (parm, clone_parm);
e2932b0d 662 }
663
664 /* Clone the body. */
665 clone_body (clone, fn, decl_map);
666
667 /* Clean up. */
06ecf488 668 delete decl_map;
e2932b0d 669 }
e55cba4c 670
95cedffb 671 /* The clone can throw iff the original function can throw. */
672 cp_function_chain->can_throw = !TREE_NOTHROW (fn);
673
e55cba4c 674 /* Now, expand this function into RTL, if appropriate. */
4775c814 675 finish_function (/*inline_p=*/false);
8f80e66d 676 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone)) = DECL_INITIAL (fn);
ed772161 677 if (alias)
9358725b 678 {
679 if (expand_or_defer_fn_1 (clone))
680 emit_associated_thunks (clone);
6a385150 681 /* We didn't generate a body, so remove the empty one. */
682 DECL_SAVED_TREE (clone) = NULL_TREE;
9358725b 683 }
ed772161 684 else
685 expand_or_defer_fn (clone);
ff64eef0 686 first = false;
e55cba4c 687 }
93c149df 688 pop_from_top_level ();
4d26ca8c 689
e55cba4c 690 /* We don't need to process the original function any further. */
691 return 1;
692}