optimize.c 22 KB

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