1 /* Perform optimizations on tree structure.
2 Copyright (C) 1998-2018 Free Software Foundation, Inc.
3 Written by Mark Michell (mark@codesourcery.com).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "stringpool.h"
29 #include "tree-inline.h"
30 #include "tree-iterator.h"
34 static void update_cloned_parm (tree
, tree
, bool);
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. */
42 update_cloned_parm (tree parm
, tree cloned_parm
, bool first
)
44 DECL_ABSTRACT_ORIGIN (cloned_parm
) = parm
;
46 /* We may have taken its address. */
47 TREE_ADDRESSABLE (cloned_parm
) = TREE_ADDRESSABLE (parm
);
49 DECL_BY_REFERENCE (cloned_parm
) = DECL_BY_REFERENCE (parm
);
51 /* The definition might have different constness. */
52 TREE_READONLY (cloned_parm
) = TREE_READONLY (parm
);
54 TREE_USED (cloned_parm
) = !first
|| TREE_USED (parm
);
56 /* The name may have changed from the declaration. */
57 DECL_NAME (cloned_parm
) = DECL_NAME (parm
);
58 DECL_SOURCE_LOCATION (cloned_parm
) = DECL_SOURCE_LOCATION (parm
);
59 TREE_TYPE (cloned_parm
) = TREE_TYPE (parm
);
61 DECL_GIMPLE_REG_P (cloned_parm
) = DECL_GIMPLE_REG_P (parm
);
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. */
70 clone_body (tree clone
, tree fn
, void *arg_map
)
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. */
77 memset (&id
, 0, sizeof (id
));
80 id
.src_cfun
= DECL_STRUCT_FUNCTION (fn
);
81 id
.decl_map
= static_cast<hash_map
<tree
, tree
> *> (arg_map
);
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;
87 id
.transform_lang_insert_block
= NULL
;
89 /* We're not inside any EH region. */
92 stmts
= DECL_SAVED_TREE (fn
);
93 walk_tree (&stmts
, copy_tree_body_r
, &id
, NULL
);
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
)
104 FOR_EACH_LOCAL_DECL (DECL_STRUCT_FUNCTION (fn
), ix
, decl
)
105 walk_tree (&DECL_INITIAL (decl
), copy_tree_body_r
, &id
, NULL
);
108 append_to_statement_list_force (stmts
, &DECL_SAVED_TREE (clone
));
111 /* DELETE_DTOR is a delete destructor whose body will be built.
112 COMPLETE_DTOR is the corresponding complete destructor. */
115 build_delete_destructor_body (tree delete_dtor
, tree complete_dtor
)
117 tree parm
= DECL_ARGUMENTS (delete_dtor
);
118 tree virtual_size
= cxx_sizeof (current_class_type
);
120 /* Call the corresponding complete destructor. */
121 gcc_assert (complete_dtor
);
122 tree call_dtor
= build_cxx_call (complete_dtor
, 1, &parm
,
123 tf_warning_or_error
);
125 /* Call the delete function. */
126 tree call_delete
= build_op_delete_call (DELETE_EXPR
, current_class_ptr
,
129 /*placement=*/NULL_TREE
,
130 /*alloc_fn=*/NULL_TREE
,
131 tf_warning_or_error
);
133 /* Operator delete must be called, whether or not the dtor throws. */
134 add_stmt (build2 (TRY_FINALLY_EXPR
, void_type_node
, call_dtor
, call_delete
));
136 /* Return the address of the object. */
137 if (targetm
.cxx
.cdtor_returns_this ())
139 tree val
= DECL_ARGUMENTS (delete_dtor
);
140 val
= build2 (MODIFY_EXPR
, TREE_TYPE (val
),
141 DECL_RESULT (delete_dtor
), val
);
142 add_stmt (build_stmt (0, RETURN_EXPR
, val
));
146 /* Return name of comdat group for complete and base ctor (or dtor)
147 that have the same body. If dtor is virtual, deleting dtor goes
148 into this comdat group as well. */
151 cdtor_comdat_group (tree complete
, tree base
)
153 tree complete_name
= DECL_ASSEMBLER_NAME (complete
);
154 tree base_name
= DECL_ASSEMBLER_NAME (base
);
157 bool diff_seen
= false;
159 gcc_assert (IDENTIFIER_LENGTH (complete_name
)
160 == IDENTIFIER_LENGTH (base_name
));
161 grp_name
= XALLOCAVEC (char, IDENTIFIER_LENGTH (complete_name
) + 1);
162 p
= IDENTIFIER_POINTER (complete_name
);
163 q
= IDENTIFIER_POINTER (base_name
);
164 for (idx
= 0; idx
< IDENTIFIER_LENGTH (complete_name
); idx
++)
165 if (p
[idx
] == q
[idx
])
166 grp_name
[idx
] = p
[idx
];
169 gcc_assert (!diff_seen
171 && (p
[idx
- 1] == 'C' || p
[idx
- 1] == 'D'
172 || p
[idx
- 1] == 'I')
178 grp_name
[idx
] = '\0';
179 gcc_assert (diff_seen
);
180 return get_identifier (grp_name
);
183 /* Returns true iff we can make the base and complete [cd]tor aliases of
184 the same symbol rather than separate functions. */
187 can_alias_cdtor (tree fn
)
189 /* If aliases aren't supported by the assembler, fail. */
190 if (!TARGET_SUPPORTS_ALIASES
)
193 /* We can't use an alias if there are virtual bases. */
194 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn
)))
196 /* ??? Why not use aliases with -frepo? */
197 if (flag_use_repository
)
199 gcc_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (fn
));
200 /* Don't use aliases for weak/linkonce definitions unless we can put both
201 symbols in the same COMDAT group. */
202 return (DECL_INTERFACE_KNOWN (fn
)
203 && (SUPPORTS_ONE_ONLY
|| !DECL_WEAK (fn
))
204 && (!DECL_ONE_ONLY (fn
)
205 || (HAVE_COMDAT_GROUP
&& DECL_WEAK (fn
))));
208 /* FN is a [cd]tor, fns is a pointer to an array of length 3. Fill fns
209 with pointers to the base, complete, and deleting variants. */
212 populate_clone_array (tree fn
, tree
*fns
)
220 /* Look for the complete destructor which may be used to build the
221 delete destructor. */
222 FOR_EACH_CLONE (clone
, fn
)
223 if (DECL_NAME (clone
) == complete_dtor_identifier
224 || DECL_NAME (clone
) == complete_ctor_identifier
)
226 else if (DECL_NAME (clone
) == base_dtor_identifier
227 || DECL_NAME (clone
) == base_ctor_identifier
)
229 else if (DECL_NAME (clone
) == deleting_dtor_identifier
)
235 /* FN is a constructor or destructor, and there are FUNCTION_DECLs
236 cloned from it nearby. Instead of cloning this body, leave it
237 alone and create tiny one-call bodies for the cloned
238 FUNCTION_DECLs. These clones are sibcall candidates, and their
239 resulting code will be very thunk-esque. */
242 maybe_thunk_body (tree fn
, bool force
)
244 tree bind
, block
, call
, clone
, clone_result
, fn_parm
, fn_parm_typelist
;
245 tree last_arg
, modify
, *args
;
246 int parmno
, vtt_parmno
, max_parms
;
249 if (!force
&& !flag_declone_ctor_dtor
)
252 /* If function accepts variable arguments, give up. */
253 last_arg
= tree_last (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
254 if (last_arg
!= void_list_node
)
257 /* If we got this far, we've decided to turn the clones into thunks. */
259 /* We're going to generate code for fn, so it is no longer "abstract."
260 Also make the unified ctor/dtor private to either the translation unit
261 (for non-vague linkage ctors) or the COMDAT group (otherwise). */
263 populate_clone_array (fn
, fns
);
265 /* Can happen during error recovery (c++/71464). */
266 if (!fns
[0] || !fns
[1])
269 /* Don't use thunks if the base clone omits inherited parameters. */
270 if (ctor_omit_inherited_parms (fns
[0]))
273 DECL_ABSTRACT_P (fn
) = false;
276 TREE_PUBLIC (fn
) = false;
277 DECL_EXTERNAL (fn
) = false;
278 DECL_INTERFACE_KNOWN (fn
) = true;
280 else if (HAVE_COMDAT_GROUP
)
282 /* At eof, defer creation of mangling aliases temporarily. */
283 bool save_defer_mangling_aliases
= defer_mangling_aliases
;
284 defer_mangling_aliases
= true;
285 tree comdat_group
= cdtor_comdat_group (fns
[1], fns
[0]);
286 defer_mangling_aliases
= save_defer_mangling_aliases
;
287 cgraph_node::get_create (fns
[0])->set_comdat_group (comdat_group
);
288 cgraph_node::get_create (fns
[1])->add_to_same_comdat_group
289 (cgraph_node::get_create (fns
[0]));
290 symtab_node::get (fn
)->add_to_same_comdat_group
291 (symtab_node::get (fns
[0]));
293 /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
294 virtual, it goes into the same comdat group as well. */
295 cgraph_node::get_create (fns
[2])->add_to_same_comdat_group
296 (symtab_node::get (fns
[0]));
297 /* Emit them now that the thunks are same comdat group aliases. */
298 if (!save_defer_mangling_aliases
)
299 generate_mangling_aliases ();
300 TREE_PUBLIC (fn
) = false;
301 DECL_EXTERNAL (fn
) = false;
302 DECL_INTERFACE_KNOWN (fn
) = true;
303 /* function_and_variable_visibility doesn't want !PUBLIC decls to
304 have these flags set. */
305 DECL_WEAK (fn
) = false;
306 DECL_COMDAT (fn
) = false;
309 /* Find the vtt_parm, if present. */
310 for (vtt_parmno
= -1, parmno
= 0, fn_parm
= DECL_ARGUMENTS (fn
);
312 ++parmno
, fn_parm
= TREE_CHAIN (fn_parm
))
314 if (DECL_ARTIFICIAL (fn_parm
)
315 && DECL_NAME (fn_parm
) == vtt_parm_identifier
)
317 /* Compensate for removed in_charge parameter. */
323 /* Allocate an argument buffer for build_cxx_call().
324 Make sure it is large enough for any of the clones. */
326 FOR_EACH_CLONE (clone
, fn
)
328 int length
= list_length (DECL_ARGUMENTS (fn
));
329 if (length
> max_parms
)
332 args
= XALLOCAVEC (tree
, max_parms
);
334 /* We know that any clones immediately follow FN in TYPE_FIELDS. */
335 FOR_EACH_CLONE (clone
, fn
)
339 /* If we've already generated a body for this clone, avoid
340 duplicating it. (Is it possible for a clone-list to grow after we
342 if (DECL_SAVED_TREE (clone
) || TREE_ASM_WRITTEN (clone
))
345 /* Start processing the function. */
346 start_preparsed_function (clone
, NULL_TREE
, SF_PRE_PARSED
);
350 for (clone_parm
= DECL_ARGUMENTS (clone
); clone_parm
;
351 clone_parm
= TREE_CHAIN (clone_parm
))
352 DECL_ABSTRACT_ORIGIN (clone_parm
) = NULL_TREE
;
353 /* Build the delete destructor by calling complete destructor and
355 build_delete_destructor_body (clone
, fns
[1]);
359 /* Walk parameter lists together, creating parameter list for
360 call to original function. */
362 fn_parm
= DECL_ARGUMENTS (fn
),
363 fn_parm_typelist
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
364 clone_parm
= DECL_ARGUMENTS (clone
);
367 fn_parm
= TREE_CHAIN (fn_parm
))
369 if (parmno
== vtt_parmno
&& ! DECL_HAS_VTT_PARM_P (clone
))
371 gcc_assert (fn_parm_typelist
);
372 /* Clobber argument with formal parameter type. */
374 = convert (TREE_VALUE (fn_parm_typelist
),
377 else if (parmno
== 1 && DECL_HAS_IN_CHARGE_PARM_P (fn
))
380 = copy_node (in_charge_arg_for_name (DECL_NAME (clone
)));
381 args
[parmno
] = in_charge
;
383 /* Map other parameters to their equivalents in the cloned
387 gcc_assert (clone_parm
);
388 DECL_ABSTRACT_ORIGIN (clone_parm
) = NULL
;
389 args
[parmno
] = clone_parm
;
390 clone_parm
= TREE_CHAIN (clone_parm
);
392 if (fn_parm_typelist
)
393 fn_parm_typelist
= TREE_CHAIN (fn_parm_typelist
);
396 /* We built this list backwards; fix now. */
398 call
= build_cxx_call (fn
, parmno
, args
, tf_warning_or_error
);
399 /* Arguments passed to the thunk by invisible reference should
400 be transmitted to the callee unchanged. Do not create a
401 temporary and invoke the copy constructor. The thunking
402 transformation must not introduce any constructor calls. */
403 CALL_FROM_THUNK_P (call
) = 1;
404 block
= make_node (BLOCK
);
405 if (targetm
.cxx
.cdtor_returns_this ())
407 clone_result
= DECL_RESULT (clone
);
408 modify
= build2 (MODIFY_EXPR
, TREE_TYPE (clone_result
),
410 modify
= build1 (RETURN_EXPR
, void_type_node
, modify
);
417 bind
= c_build_bind_expr (DECL_SOURCE_LOCATION (clone
),
418 block
, cur_stmt_list
);
419 DECL_SAVED_TREE (clone
) = push_stmt_list ();
423 DECL_ABSTRACT_ORIGIN (clone
) = NULL
;
424 expand_or_defer_fn (finish_function (/*inline_p=*/false));
429 /* FN is a function that has a complete body. Clone the body as
430 necessary. Returns nonzero if there's no longer any need to
431 process the main body. */
434 maybe_clone_body (tree fn
)
436 tree comdat_group
= NULL_TREE
;
441 bool need_alias
= false;
443 /* We only clone constructors and destructors. */
444 if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (fn
))
447 populate_clone_array (fn
, fns
);
449 /* Remember if we can't have multiple clones for some reason. We need to
450 check this before we remap local static initializers in clone_body. */
451 if (!tree_versionable_function_p (fn
))
454 /* We know that any clones immediately follow FN in the TYPE_FIELDS
456 push_to_top_level ();
457 for (idx
= 0; idx
< 3; idx
++)
466 /* Update CLONE's source position information to match FN's. */
467 DECL_SOURCE_LOCATION (clone
) = DECL_SOURCE_LOCATION (fn
);
468 DECL_DECLARED_INLINE_P (clone
) = DECL_DECLARED_INLINE_P (fn
);
469 DECL_DECLARED_CONSTEXPR_P (clone
) = DECL_DECLARED_CONSTEXPR_P (fn
);
470 DECL_COMDAT (clone
) = DECL_COMDAT (fn
);
471 DECL_WEAK (clone
) = DECL_WEAK (fn
);
473 /* We don't copy the comdat group from fn to clone because the assembler
474 name of fn was corrupted by write_mangled_name by adding *INTERNAL*
475 to it. By doing so, it also corrupted the comdat group. */
476 if (DECL_ONE_ONLY (fn
))
477 cgraph_node::get_create (clone
)->set_comdat_group (cxx_comdat_group (clone
));
478 DECL_USE_TEMPLATE (clone
) = DECL_USE_TEMPLATE (fn
);
479 DECL_EXTERNAL (clone
) = DECL_EXTERNAL (fn
);
480 DECL_INTERFACE_KNOWN (clone
) = DECL_INTERFACE_KNOWN (fn
);
481 DECL_NOT_REALLY_EXTERN (clone
) = DECL_NOT_REALLY_EXTERN (fn
);
482 TREE_PUBLIC (clone
) = TREE_PUBLIC (fn
);
483 DECL_VISIBILITY (clone
) = DECL_VISIBILITY (fn
);
484 DECL_VISIBILITY_SPECIFIED (clone
) = DECL_VISIBILITY_SPECIFIED (fn
);
485 DECL_DLLIMPORT_P (clone
) = DECL_DLLIMPORT_P (fn
);
486 DECL_ATTRIBUTES (clone
) = copy_list (DECL_ATTRIBUTES (fn
));
487 DECL_DISREGARD_INLINE_LIMITS (clone
) = DECL_DISREGARD_INLINE_LIMITS (fn
);
488 set_decl_section_name (clone
, DECL_SECTION_NAME (fn
));
490 /* Adjust the parameter names and locations. */
491 parm
= DECL_ARGUMENTS (fn
);
492 clone_parm
= DECL_ARGUMENTS (clone
);
493 /* Update the `this' parameter, which is always first. */
494 update_cloned_parm (parm
, clone_parm
, first
);
495 parm
= DECL_CHAIN (parm
);
496 clone_parm
= DECL_CHAIN (clone_parm
);
497 if (DECL_HAS_IN_CHARGE_PARM_P (fn
))
498 parm
= DECL_CHAIN (parm
);
499 if (DECL_HAS_VTT_PARM_P (fn
))
500 parm
= DECL_CHAIN (parm
);
501 if (DECL_HAS_VTT_PARM_P (clone
))
502 clone_parm
= DECL_CHAIN (clone_parm
);
503 for (; parm
&& clone_parm
;
504 parm
= DECL_CHAIN (parm
), clone_parm
= DECL_CHAIN (clone_parm
))
505 /* Update this parameter. */
506 update_cloned_parm (parm
, clone_parm
, first
);
509 bool can_alias
= can_alias_cdtor (fn
);
511 /* If we decide to turn clones into thunks, they will branch to fn.
512 Must have original function available to call. */
513 if (!can_alias
&& maybe_thunk_body (fn
, need_alias
))
515 pop_from_top_level ();
516 /* We still need to emit the original function. */
520 /* Emit the DWARF1 abstract instance. */
521 (*debug_hooks
->deferred_inline_function
) (fn
);
523 /* We know that any clones immediately follow FN in the TYPE_FIELDS. */
524 for (idx
= 0; idx
< 3; idx
++)
529 hash_map
<tree
, tree
> *decl_map
;
536 /* Start processing the function. */
537 start_preparsed_function (clone
, NULL_TREE
, SF_PRE_PARSED
);
539 /* Tell cgraph if both ctors or both dtors are known to have
544 && cgraph_node::get_create (fns
[0])->create_same_body_alias
548 if (DECL_ONE_ONLY (fns
[0]))
550 /* For comdat base and complete cdtors put them
551 into the same, *[CD]5* comdat group instead of
553 comdat_group
= cdtor_comdat_group (fns
[1], fns
[0]);
554 cgraph_node::get_create (fns
[0])->set_comdat_group (comdat_group
);
555 if (symtab_node::get (clone
)->same_comdat_group
)
556 symtab_node::get (clone
)->remove_from_same_comdat_group ();
557 symtab_node::get (clone
)->add_to_same_comdat_group
558 (symtab_node::get (fns
[0]));
562 /* Build the delete destructor by calling complete destructor
563 and delete function. */
566 build_delete_destructor_body (clone
, fns
[1]);
567 /* If *[CD][12]* dtors go into the *[CD]5* comdat group and dtor is
568 virtual, it goes into the same comdat group as well. */
570 cgraph_node::get_create (clone
)->add_to_same_comdat_group
571 (symtab_node::get (fns
[0]));
574 /* No need to populate body. */ ;
577 /* If we can't have multiple copies of FN (say, because there's a
578 static local initialized with the address of a label), we need
579 to use an alias for the complete variant. */
580 if (idx
== 1 && need_alias
)
582 if (DECL_STRUCT_FUNCTION (fn
)->cannot_be_copied_set
)
583 sorry (DECL_STRUCT_FUNCTION (fn
)->cannot_be_copied_reason
, fn
);
585 sorry ("making multiple clones of %qD", fn
);
588 /* Remap the parameters. */
589 decl_map
= new hash_map
<tree
, tree
>;
591 parm
= DECL_ARGUMENTS (fn
),
592 clone_parm
= DECL_ARGUMENTS (clone
);
595 parm
= DECL_CHAIN (parm
))
597 /* Map the in-charge parameter to an appropriate constant. */
598 if (DECL_HAS_IN_CHARGE_PARM_P (fn
) && parmno
== 1)
601 in_charge
= in_charge_arg_for_name (DECL_NAME (clone
));
602 decl_map
->put (parm
, in_charge
);
604 else if (DECL_ARTIFICIAL (parm
)
605 && DECL_NAME (parm
) == vtt_parm_identifier
)
607 /* For a subobject constructor or destructor, the next
608 argument is the VTT parameter. Remap the VTT_PARM
609 from the CLONE to this parameter. */
610 if (DECL_HAS_VTT_PARM_P (clone
))
612 DECL_ABSTRACT_ORIGIN (clone_parm
) = parm
;
613 decl_map
->put (parm
, clone_parm
);
614 clone_parm
= DECL_CHAIN (clone_parm
);
616 /* Otherwise, map the VTT parameter to `NULL'. */
620 = fold_convert (TREE_TYPE (parm
), null_pointer_node
);
621 decl_map
->put (parm
, t
);
624 /* Map other parameters to their equivalents in the cloned
631 replacement
= clone_parm
;
632 clone_parm
= DECL_CHAIN (clone_parm
);
636 /* Inheriting ctors can omit parameters from the base
637 clone. Replace them with null lvalues. */
638 tree reftype
= build_reference_type (TREE_TYPE (parm
));
639 replacement
= fold_convert (reftype
, null_pointer_node
);
640 replacement
= convert_from_reference (replacement
);
642 decl_map
->put (parm
, replacement
);
646 if (targetm
.cxx
.cdtor_returns_this ())
648 parm
= DECL_RESULT (fn
);
649 clone_parm
= DECL_RESULT (clone
);
650 decl_map
->put (parm
, clone_parm
);
653 /* Clone the body. */
654 clone_body (clone
, fn
, decl_map
);
660 /* The clone can throw iff the original function can throw. */
661 cp_function_chain
->can_throw
= !TREE_NOTHROW (fn
);
663 /* Now, expand this function into RTL, if appropriate. */
664 finish_function (/*inline_p=*/false);
665 BLOCK_ABSTRACT_ORIGIN (DECL_INITIAL (clone
)) = DECL_INITIAL (fn
);
668 if (expand_or_defer_fn_1 (clone
))
669 emit_associated_thunks (clone
);
670 /* We didn't generate a body, so remove the empty one. */
671 DECL_SAVED_TREE (clone
) = NULL_TREE
;
674 expand_or_defer_fn (clone
);
677 pop_from_top_level ();
679 /* We don't need to process the original function any further. */