cgraphclones.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /* Callgraph clones
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Jan Hubicka
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. 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. /* This module provide facilities for clonning functions. I.e. creating
  17. new functions based on existing functions with simple modifications,
  18. such as replacement of parameters.
  19. To allow whole program optimization without actual presence of function
  20. bodies, an additional infrastructure is provided for so-called virtual
  21. clones
  22. A virtual clone in the callgraph is a function that has no
  23. associated body, just a description of how to create its body based
  24. on a different function (which itself may be a virtual clone).
  25. The description of function modifications includes adjustments to
  26. the function's signature (which allows, for example, removing or
  27. adding function arguments), substitutions to perform on the
  28. function body, and, for inlined functions, a pointer to the
  29. function that it will be inlined into.
  30. It is also possible to redirect any edge of the callgraph from a
  31. function to its virtual clone. This implies updating of the call
  32. site to adjust for the new function signature.
  33. Most of the transformations performed by inter-procedural
  34. optimizations can be represented via virtual clones. For
  35. instance, a constant propagation pass can produce a virtual clone
  36. of the function which replaces one of its arguments by a
  37. constant. The inliner can represent its decisions by producing a
  38. clone of a function whose body will be later integrated into
  39. a given function.
  40. Using virtual clones, the program can be easily updated
  41. during the Execute stage, solving most of pass interactions
  42. problems that would otherwise occur during Transform.
  43. Virtual clones are later materialized in the LTRANS stage and
  44. turned into real functions. Passes executed after the virtual
  45. clone were introduced also perform their Transform stage
  46. on new functions, so for a pass there is no significant
  47. difference between operating on a real function or a virtual
  48. clone introduced before its Execute stage.
  49. Optimization passes then work on virtual clones introduced before
  50. their Execute stage as if they were real functions. The
  51. only difference is that clones are not visible during the
  52. Generate Summary stage. */
  53. #include "config.h"
  54. #include "system.h"
  55. #include "coretypes.h"
  56. #include "tm.h"
  57. #include "rtl.h"
  58. #include "hash-set.h"
  59. #include "machmode.h"
  60. #include "vec.h"
  61. #include "double-int.h"
  62. #include "input.h"
  63. #include "alias.h"
  64. #include "symtab.h"
  65. #include "wide-int.h"
  66. #include "inchash.h"
  67. #include "tree.h"
  68. #include "fold-const.h"
  69. #include "stringpool.h"
  70. #include "hard-reg-set.h"
  71. #include "input.h"
  72. #include "function.h"
  73. #include "emit-rtl.h"
  74. #include "predict.h"
  75. #include "basic-block.h"
  76. #include "tree-ssa-alias.h"
  77. #include "internal-fn.h"
  78. #include "tree-eh.h"
  79. #include "gimple-expr.h"
  80. #include "is-a.h"
  81. #include "gimple.h"
  82. #include "bitmap.h"
  83. #include "tree-cfg.h"
  84. #include "tree-inline.h"
  85. #include "langhooks.h"
  86. #include "toplev.h"
  87. #include "flags.h"
  88. #include "debug.h"
  89. #include "target.h"
  90. #include "diagnostic.h"
  91. #include "params.h"
  92. #include "intl.h"
  93. #include "hash-map.h"
  94. #include "plugin-api.h"
  95. #include "ipa-ref.h"
  96. #include "cgraph.h"
  97. #include "alloc-pool.h"
  98. #include "symbol-summary.h"
  99. #include "ipa-prop.h"
  100. #include "tree-iterator.h"
  101. #include "tree-dump.h"
  102. #include "gimple-pretty-print.h"
  103. #include "coverage.h"
  104. #include "ipa-inline.h"
  105. #include "ipa-utils.h"
  106. #include "lto-streamer.h"
  107. #include "except.h"
  108. /* Create clone of edge in the node N represented by CALL_EXPR
  109. the callgraph. */
  110. cgraph_edge *
  111. cgraph_edge::clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
  112. gcov_type count_scale, int freq_scale, bool update_original)
  113. {
  114. cgraph_edge *new_edge;
  115. gcov_type gcov_count = apply_probability (count, count_scale);
  116. gcov_type freq;
  117. /* We do not want to ignore loop nest after frequency drops to 0. */
  118. if (!freq_scale)
  119. freq_scale = 1;
  120. freq = frequency * (gcov_type) freq_scale / CGRAPH_FREQ_BASE;
  121. if (freq > CGRAPH_FREQ_MAX)
  122. freq = CGRAPH_FREQ_MAX;
  123. if (indirect_unknown_callee)
  124. {
  125. tree decl;
  126. if (call_stmt && (decl = gimple_call_fndecl (call_stmt))
  127. /* When the call is speculative, we need to resolve it
  128. via cgraph_resolve_speculation and not here. */
  129. && !speculative)
  130. {
  131. cgraph_node *callee = cgraph_node::get (decl);
  132. gcc_checking_assert (callee);
  133. new_edge = n->create_edge (callee, call_stmt, gcov_count, freq);
  134. }
  135. else
  136. {
  137. new_edge = n->create_indirect_edge (call_stmt,
  138. indirect_info->ecf_flags,
  139. count, freq, false);
  140. *new_edge->indirect_info = *indirect_info;
  141. }
  142. }
  143. else
  144. {
  145. new_edge = n->create_edge (callee, call_stmt, gcov_count, freq);
  146. if (indirect_info)
  147. {
  148. new_edge->indirect_info
  149. = ggc_cleared_alloc<cgraph_indirect_call_info> ();
  150. *new_edge->indirect_info = *indirect_info;
  151. }
  152. }
  153. new_edge->inline_failed = inline_failed;
  154. new_edge->indirect_inlining_edge = indirect_inlining_edge;
  155. new_edge->lto_stmt_uid = stmt_uid;
  156. /* Clone flags that depend on call_stmt availability manually. */
  157. new_edge->can_throw_external = can_throw_external;
  158. new_edge->call_stmt_cannot_inline_p = call_stmt_cannot_inline_p;
  159. new_edge->speculative = speculative;
  160. new_edge->in_polymorphic_cdtor = in_polymorphic_cdtor;
  161. if (update_original)
  162. {
  163. count -= new_edge->count;
  164. if (count < 0)
  165. count = 0;
  166. }
  167. symtab->call_edge_duplication_hooks (this, new_edge);
  168. return new_edge;
  169. }
  170. /* Build variant of function type ORIG_TYPE skipping ARGS_TO_SKIP and the
  171. return value if SKIP_RETURN is true. */
  172. static tree
  173. build_function_type_skip_args (tree orig_type, bitmap args_to_skip,
  174. bool skip_return)
  175. {
  176. tree new_type = NULL;
  177. tree args, new_args = NULL;
  178. tree new_reversed;
  179. int i = 0;
  180. for (args = TYPE_ARG_TYPES (orig_type); args && args != void_list_node;
  181. args = TREE_CHAIN (args), i++)
  182. if (!args_to_skip || !bitmap_bit_p (args_to_skip, i))
  183. new_args = tree_cons (NULL_TREE, TREE_VALUE (args), new_args);
  184. new_reversed = nreverse (new_args);
  185. if (args)
  186. {
  187. if (new_reversed)
  188. TREE_CHAIN (new_args) = void_list_node;
  189. else
  190. new_reversed = void_list_node;
  191. }
  192. /* Use copy_node to preserve as much as possible from original type
  193. (debug info, attribute lists etc.)
  194. Exception is METHOD_TYPEs must have THIS argument.
  195. When we are asked to remove it, we need to build new FUNCTION_TYPE
  196. instead. */
  197. if (TREE_CODE (orig_type) != METHOD_TYPE
  198. || !args_to_skip
  199. || !bitmap_bit_p (args_to_skip, 0))
  200. {
  201. new_type = build_distinct_type_copy (orig_type);
  202. TYPE_ARG_TYPES (new_type) = new_reversed;
  203. }
  204. else
  205. {
  206. new_type
  207. = build_distinct_type_copy (build_function_type (TREE_TYPE (orig_type),
  208. new_reversed));
  209. TYPE_CONTEXT (new_type) = TYPE_CONTEXT (orig_type);
  210. }
  211. if (skip_return)
  212. TREE_TYPE (new_type) = void_type_node;
  213. return new_type;
  214. }
  215. /* Build variant of function decl ORIG_DECL skipping ARGS_TO_SKIP and the
  216. return value if SKIP_RETURN is true.
  217. Arguments from DECL_ARGUMENTS list can't be removed now, since they are
  218. linked by TREE_CHAIN directly. The caller is responsible for eliminating
  219. them when they are being duplicated (i.e. copy_arguments_for_versioning). */
  220. static tree
  221. build_function_decl_skip_args (tree orig_decl, bitmap args_to_skip,
  222. bool skip_return)
  223. {
  224. tree new_decl = copy_node (orig_decl);
  225. tree new_type;
  226. new_type = TREE_TYPE (orig_decl);
  227. if (prototype_p (new_type)
  228. || (skip_return && !VOID_TYPE_P (TREE_TYPE (new_type))))
  229. new_type
  230. = build_function_type_skip_args (new_type, args_to_skip, skip_return);
  231. TREE_TYPE (new_decl) = new_type;
  232. /* For declarations setting DECL_VINDEX (i.e. methods)
  233. we expect first argument to be THIS pointer. */
  234. if (args_to_skip && bitmap_bit_p (args_to_skip, 0))
  235. DECL_VINDEX (new_decl) = NULL_TREE;
  236. /* When signature changes, we need to clear builtin info. */
  237. if (DECL_BUILT_IN (new_decl)
  238. && args_to_skip
  239. && !bitmap_empty_p (args_to_skip))
  240. {
  241. DECL_BUILT_IN_CLASS (new_decl) = NOT_BUILT_IN;
  242. DECL_FUNCTION_CODE (new_decl) = (enum built_in_function) 0;
  243. }
  244. /* The FE might have information and assumptions about the other
  245. arguments. */
  246. DECL_LANG_SPECIFIC (new_decl) = NULL;
  247. return new_decl;
  248. }
  249. /* Set flags of NEW_NODE and its decl. NEW_NODE is a newly created private
  250. clone or its thunk. */
  251. static void
  252. set_new_clone_decl_and_node_flags (cgraph_node *new_node)
  253. {
  254. DECL_EXTERNAL (new_node->decl) = 0;
  255. TREE_PUBLIC (new_node->decl) = 0;
  256. DECL_COMDAT (new_node->decl) = 0;
  257. DECL_WEAK (new_node->decl) = 0;
  258. DECL_VIRTUAL_P (new_node->decl) = 0;
  259. DECL_STATIC_CONSTRUCTOR (new_node->decl) = 0;
  260. DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
  261. new_node->externally_visible = 0;
  262. new_node->local.local = 1;
  263. new_node->lowered = true;
  264. }
  265. /* Duplicate thunk THUNK if necessary but make it to refer to NODE.
  266. ARGS_TO_SKIP, if non-NULL, determines which parameters should be omitted.
  267. Function can return NODE if no thunk is necessary, which can happen when
  268. thunk is this_adjusting but we are removing this parameter. */
  269. static cgraph_node *
  270. duplicate_thunk_for_node (cgraph_node *thunk, cgraph_node *node)
  271. {
  272. cgraph_node *new_thunk, *thunk_of;
  273. thunk_of = thunk->callees->callee->ultimate_alias_target ();
  274. if (thunk_of->thunk.thunk_p)
  275. node = duplicate_thunk_for_node (thunk_of, node);
  276. if (!DECL_ARGUMENTS (thunk->decl))
  277. thunk->get_untransformed_body ();
  278. cgraph_edge *cs;
  279. for (cs = node->callers; cs; cs = cs->next_caller)
  280. if (cs->caller->thunk.thunk_p
  281. && cs->caller->thunk.this_adjusting == thunk->thunk.this_adjusting
  282. && cs->caller->thunk.fixed_offset == thunk->thunk.fixed_offset
  283. && cs->caller->thunk.virtual_offset_p == thunk->thunk.virtual_offset_p
  284. && cs->caller->thunk.virtual_value == thunk->thunk.virtual_value)
  285. return cs->caller;
  286. tree new_decl;
  287. if (!node->clone.args_to_skip)
  288. new_decl = copy_node (thunk->decl);
  289. else
  290. {
  291. /* We do not need to duplicate this_adjusting thunks if we have removed
  292. this. */
  293. if (thunk->thunk.this_adjusting
  294. && bitmap_bit_p (node->clone.args_to_skip, 0))
  295. return node;
  296. new_decl = build_function_decl_skip_args (thunk->decl,
  297. node->clone.args_to_skip,
  298. false);
  299. }
  300. tree *link = &DECL_ARGUMENTS (new_decl);
  301. int i = 0;
  302. for (tree pd = DECL_ARGUMENTS (thunk->decl); pd; pd = DECL_CHAIN (pd), i++)
  303. {
  304. if (!node->clone.args_to_skip
  305. || !bitmap_bit_p (node->clone.args_to_skip, i))
  306. {
  307. tree nd = copy_node (pd);
  308. DECL_CONTEXT (nd) = new_decl;
  309. *link = nd;
  310. link = &DECL_CHAIN (nd);
  311. }
  312. }
  313. *link = NULL_TREE;
  314. gcc_checking_assert (!DECL_STRUCT_FUNCTION (new_decl));
  315. gcc_checking_assert (!DECL_INITIAL (new_decl));
  316. gcc_checking_assert (!DECL_RESULT (new_decl));
  317. gcc_checking_assert (!DECL_RTL_SET_P (new_decl));
  318. DECL_NAME (new_decl) = clone_function_name (thunk->decl, "artificial_thunk");
  319. SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
  320. new_thunk = cgraph_node::create (new_decl);
  321. set_new_clone_decl_and_node_flags (new_thunk);
  322. new_thunk->definition = true;
  323. new_thunk->thunk = thunk->thunk;
  324. new_thunk->unique_name = in_lto_p;
  325. new_thunk->former_clone_of = thunk->decl;
  326. new_thunk->clone.args_to_skip = node->clone.args_to_skip;
  327. new_thunk->clone.combined_args_to_skip = node->clone.combined_args_to_skip;
  328. cgraph_edge *e = new_thunk->create_edge (node, NULL, 0,
  329. CGRAPH_FREQ_BASE);
  330. e->call_stmt_cannot_inline_p = true;
  331. symtab->call_edge_duplication_hooks (thunk->callees, e);
  332. symtab->call_cgraph_duplication_hooks (thunk, new_thunk);
  333. return new_thunk;
  334. }
  335. /* If E does not lead to a thunk, simply redirect it to N. Otherwise create
  336. one or more equivalent thunks for N and redirect E to the first in the
  337. chain. Note that it is then necessary to call
  338. n->expand_all_artificial_thunks once all callers are redirected. */
  339. void
  340. cgraph_edge::redirect_callee_duplicating_thunks (cgraph_node *n)
  341. {
  342. cgraph_node *orig_to = callee->ultimate_alias_target ();
  343. if (orig_to->thunk.thunk_p)
  344. n = duplicate_thunk_for_node (orig_to, n);
  345. redirect_callee (n);
  346. }
  347. /* Call expand_thunk on all callers that are thunks and if analyze those nodes
  348. that were expanded. */
  349. void
  350. cgraph_node::expand_all_artificial_thunks ()
  351. {
  352. cgraph_edge *e;
  353. for (e = callers; e;)
  354. if (e->caller->thunk.thunk_p)
  355. {
  356. cgraph_node *thunk = e->caller;
  357. e = e->next_caller;
  358. if (thunk->expand_thunk (false, false))
  359. {
  360. thunk->thunk.thunk_p = false;
  361. thunk->analyze ();
  362. }
  363. thunk->expand_all_artificial_thunks ();
  364. }
  365. else
  366. e = e->next_caller;
  367. }
  368. /* Create node representing clone of N executed COUNT times. Decrease
  369. the execution counts from original node too.
  370. The new clone will have decl set to DECL that may or may not be the same
  371. as decl of N.
  372. When UPDATE_ORIGINAL is true, the counts are subtracted from the original
  373. function's profile to reflect the fact that part of execution is handled
  374. by node.
  375. When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
  376. the new clone. Otherwise the caller is responsible for doing so later.
  377. If the new node is being inlined into another one, NEW_INLINED_TO should be
  378. the outline function the new one is (even indirectly) inlined to. All hooks
  379. will see this in node's global.inlined_to, when invoked. Can be NULL if the
  380. node is not inlined. */
  381. cgraph_node *
  382. cgraph_node::create_clone (tree new_decl, gcov_type gcov_count, int freq,
  383. bool update_original,
  384. vec<cgraph_edge *> redirect_callers,
  385. bool call_duplication_hook,
  386. cgraph_node *new_inlined_to,
  387. bitmap args_to_skip)
  388. {
  389. cgraph_node *new_node = symtab->create_empty ();
  390. cgraph_edge *e;
  391. gcov_type count_scale;
  392. unsigned i;
  393. new_node->decl = new_decl;
  394. new_node->register_symbol ();
  395. new_node->origin = origin;
  396. new_node->lto_file_data = lto_file_data;
  397. if (new_node->origin)
  398. {
  399. new_node->next_nested = new_node->origin->nested;
  400. new_node->origin->nested = new_node;
  401. }
  402. new_node->analyzed = analyzed;
  403. new_node->definition = definition;
  404. new_node->local = local;
  405. new_node->externally_visible = false;
  406. new_node->no_reorder = no_reorder;
  407. new_node->local.local = true;
  408. new_node->global = global;
  409. new_node->global.inlined_to = new_inlined_to;
  410. new_node->rtl = rtl;
  411. new_node->count = count;
  412. new_node->frequency = frequency;
  413. new_node->tp_first_run = tp_first_run;
  414. new_node->tm_clone = tm_clone;
  415. new_node->icf_merged = icf_merged;
  416. new_node->merged = merged;
  417. new_node->clone.tree_map = NULL;
  418. new_node->clone.args_to_skip = args_to_skip;
  419. new_node->split_part = split_part;
  420. if (!args_to_skip)
  421. new_node->clone.combined_args_to_skip = clone.combined_args_to_skip;
  422. else if (clone.combined_args_to_skip)
  423. {
  424. new_node->clone.combined_args_to_skip = BITMAP_GGC_ALLOC ();
  425. bitmap_ior (new_node->clone.combined_args_to_skip,
  426. clone.combined_args_to_skip, args_to_skip);
  427. }
  428. else
  429. new_node->clone.combined_args_to_skip = args_to_skip;
  430. if (count)
  431. {
  432. if (new_node->count > count)
  433. count_scale = REG_BR_PROB_BASE;
  434. else
  435. count_scale = GCOV_COMPUTE_SCALE (new_node->count, count);
  436. }
  437. else
  438. count_scale = 0;
  439. if (update_original)
  440. {
  441. count -= gcov_count;
  442. if (count < 0)
  443. count = 0;
  444. }
  445. FOR_EACH_VEC_ELT (redirect_callers, i, e)
  446. {
  447. /* Redirect calls to the old version node to point to its new
  448. version. The only exception is when the edge was proved to
  449. be unreachable during the clonning procedure. */
  450. if (!e->callee
  451. || DECL_BUILT_IN_CLASS (e->callee->decl) != BUILT_IN_NORMAL
  452. || DECL_FUNCTION_CODE (e->callee->decl) != BUILT_IN_UNREACHABLE)
  453. e->redirect_callee_duplicating_thunks (new_node);
  454. }
  455. new_node->expand_all_artificial_thunks ();
  456. for (e = callees;e; e=e->next_callee)
  457. e->clone (new_node, e->call_stmt, e->lto_stmt_uid, count_scale,
  458. freq, update_original);
  459. for (e = indirect_calls; e; e = e->next_callee)
  460. e->clone (new_node, e->call_stmt, e->lto_stmt_uid,
  461. count_scale, freq, update_original);
  462. new_node->clone_references (this);
  463. new_node->next_sibling_clone = clones;
  464. if (clones)
  465. clones->prev_sibling_clone = new_node;
  466. clones = new_node;
  467. new_node->clone_of = this;
  468. if (call_duplication_hook)
  469. symtab->call_cgraph_duplication_hooks (this, new_node);
  470. return new_node;
  471. }
  472. static GTY(()) unsigned int clone_fn_id_num;
  473. /* Return a new assembler name for a clone with SUFFIX of a decl named
  474. NAME. */
  475. tree
  476. clone_function_name_1 (const char *name, const char *suffix)
  477. {
  478. size_t len = strlen (name);
  479. char *tmp_name, *prefix;
  480. prefix = XALLOCAVEC (char, len + strlen (suffix) + 2);
  481. memcpy (prefix, name, len);
  482. strcpy (prefix + len + 1, suffix);
  483. #ifndef NO_DOT_IN_LABEL
  484. prefix[len] = '.';
  485. #elif !defined NO_DOLLAR_IN_LABEL
  486. prefix[len] = '$';
  487. #else
  488. prefix[len] = '_';
  489. #endif
  490. ASM_FORMAT_PRIVATE_NAME (tmp_name, prefix, clone_fn_id_num++);
  491. return get_identifier (tmp_name);
  492. }
  493. /* Return a new assembler name for a clone of DECL with SUFFIX. */
  494. tree
  495. clone_function_name (tree decl, const char *suffix)
  496. {
  497. tree name = DECL_ASSEMBLER_NAME (decl);
  498. return clone_function_name_1 (IDENTIFIER_POINTER (name), suffix);
  499. }
  500. /* Create callgraph node clone with new declaration. The actual body will
  501. be copied later at compilation stage.
  502. TODO: after merging in ipa-sra use function call notes instead of args_to_skip
  503. bitmap interface.
  504. */
  505. cgraph_node *
  506. cgraph_node::create_virtual_clone (vec<cgraph_edge *> redirect_callers,
  507. vec<ipa_replace_map *, va_gc> *tree_map,
  508. bitmap args_to_skip, const char * suffix)
  509. {
  510. tree old_decl = decl;
  511. cgraph_node *new_node = NULL;
  512. tree new_decl;
  513. size_t len, i;
  514. ipa_replace_map *map;
  515. char *name;
  516. if (!in_lto_p)
  517. gcc_checking_assert (tree_versionable_function_p (old_decl));
  518. gcc_assert (local.can_change_signature || !args_to_skip);
  519. /* Make a new FUNCTION_DECL tree node */
  520. if (!args_to_skip)
  521. new_decl = copy_node (old_decl);
  522. else
  523. new_decl = build_function_decl_skip_args (old_decl, args_to_skip, false);
  524. /* These pointers represent function body and will be populated only when clone
  525. is materialized. */
  526. gcc_assert (new_decl != old_decl);
  527. DECL_STRUCT_FUNCTION (new_decl) = NULL;
  528. DECL_ARGUMENTS (new_decl) = NULL;
  529. DECL_INITIAL (new_decl) = NULL;
  530. DECL_RESULT (new_decl) = NULL;
  531. /* We can not do DECL_RESULT (new_decl) = NULL; here because of LTO partitioning
  532. sometimes storing only clone decl instead of original. */
  533. /* Generate a new name for the new version. */
  534. len = IDENTIFIER_LENGTH (DECL_NAME (old_decl));
  535. name = XALLOCAVEC (char, len + strlen (suffix) + 2);
  536. memcpy (name, IDENTIFIER_POINTER (DECL_NAME (old_decl)), len);
  537. strcpy (name + len + 1, suffix);
  538. name[len] = '.';
  539. DECL_NAME (new_decl) = get_identifier (name);
  540. SET_DECL_ASSEMBLER_NAME (new_decl, clone_function_name (old_decl, suffix));
  541. SET_DECL_RTL (new_decl, NULL);
  542. new_node = create_clone (new_decl, count, CGRAPH_FREQ_BASE, false,
  543. redirect_callers, false, NULL, args_to_skip);
  544. /* Update the properties.
  545. Make clone visible only within this translation unit. Make sure
  546. that is not weak also.
  547. ??? We cannot use COMDAT linkage because there is no
  548. ABI support for this. */
  549. set_new_clone_decl_and_node_flags (new_node);
  550. new_node->clone.tree_map = tree_map;
  551. if (!implicit_section)
  552. new_node->set_section (get_section ());
  553. /* Clones of global symbols or symbols with unique names are unique. */
  554. if ((TREE_PUBLIC (old_decl)
  555. && !DECL_EXTERNAL (old_decl)
  556. && !DECL_WEAK (old_decl)
  557. && !DECL_COMDAT (old_decl))
  558. || in_lto_p)
  559. new_node->unique_name = true;
  560. FOR_EACH_VEC_SAFE_ELT (tree_map, i, map)
  561. new_node->maybe_create_reference (map->new_tree, IPA_REF_ADDR, NULL);
  562. if (ipa_transforms_to_apply.exists ())
  563. new_node->ipa_transforms_to_apply
  564. = ipa_transforms_to_apply.copy ();
  565. symtab->call_cgraph_duplication_hooks (this, new_node);
  566. return new_node;
  567. }
  568. /* callgraph node being removed from symbol table; see if its entry can be
  569. replaced by other inline clone. */
  570. cgraph_node *
  571. cgraph_node::find_replacement (void)
  572. {
  573. cgraph_node *next_inline_clone, *replacement;
  574. for (next_inline_clone = clones;
  575. next_inline_clone
  576. && next_inline_clone->decl != decl;
  577. next_inline_clone = next_inline_clone->next_sibling_clone)
  578. ;
  579. /* If there is inline clone of the node being removed, we need
  580. to put it into the position of removed node and reorganize all
  581. other clones to be based on it. */
  582. if (next_inline_clone)
  583. {
  584. cgraph_node *n;
  585. cgraph_node *new_clones;
  586. replacement = next_inline_clone;
  587. /* Unlink inline clone from the list of clones of removed node. */
  588. if (next_inline_clone->next_sibling_clone)
  589. next_inline_clone->next_sibling_clone->prev_sibling_clone
  590. = next_inline_clone->prev_sibling_clone;
  591. if (next_inline_clone->prev_sibling_clone)
  592. {
  593. gcc_assert (clones != next_inline_clone);
  594. next_inline_clone->prev_sibling_clone->next_sibling_clone
  595. = next_inline_clone->next_sibling_clone;
  596. }
  597. else
  598. {
  599. gcc_assert (clones == next_inline_clone);
  600. clones = next_inline_clone->next_sibling_clone;
  601. }
  602. new_clones = clones;
  603. clones = NULL;
  604. /* Copy clone info. */
  605. next_inline_clone->clone = clone;
  606. /* Now place it into clone tree at same level at NODE. */
  607. next_inline_clone->clone_of = clone_of;
  608. next_inline_clone->prev_sibling_clone = NULL;
  609. next_inline_clone->next_sibling_clone = NULL;
  610. if (clone_of)
  611. {
  612. if (clone_of->clones)
  613. clone_of->clones->prev_sibling_clone = next_inline_clone;
  614. next_inline_clone->next_sibling_clone = clone_of->clones;
  615. clone_of->clones = next_inline_clone;
  616. }
  617. /* Merge the clone list. */
  618. if (new_clones)
  619. {
  620. if (!next_inline_clone->clones)
  621. next_inline_clone->clones = new_clones;
  622. else
  623. {
  624. n = next_inline_clone->clones;
  625. while (n->next_sibling_clone)
  626. n = n->next_sibling_clone;
  627. n->next_sibling_clone = new_clones;
  628. new_clones->prev_sibling_clone = n;
  629. }
  630. }
  631. /* Update clone_of pointers. */
  632. n = new_clones;
  633. while (n)
  634. {
  635. n->clone_of = next_inline_clone;
  636. n = n->next_sibling_clone;
  637. }
  638. return replacement;
  639. }
  640. else
  641. return NULL;
  642. }
  643. /* Like cgraph_set_call_stmt but walk the clone tree and update all
  644. clones sharing the same function body.
  645. When WHOLE_SPECULATIVE_EDGES is true, all three components of
  646. speculative edge gets updated. Otherwise we update only direct
  647. call. */
  648. void
  649. cgraph_node::set_call_stmt_including_clones (gimple old_stmt,
  650. gcall *new_stmt,
  651. bool update_speculative)
  652. {
  653. cgraph_node *node;
  654. cgraph_edge *edge = get_edge (old_stmt);
  655. if (edge)
  656. edge->set_call_stmt (new_stmt, update_speculative);
  657. node = clones;
  658. if (node)
  659. while (node != this)
  660. {
  661. cgraph_edge *edge = node->get_edge (old_stmt);
  662. if (edge)
  663. {
  664. edge->set_call_stmt (new_stmt, update_speculative);
  665. /* If UPDATE_SPECULATIVE is false, it means that we are turning
  666. speculative call into a real code sequence. Update the
  667. callgraph edges. */
  668. if (edge->speculative && !update_speculative)
  669. {
  670. cgraph_edge *direct, *indirect;
  671. ipa_ref *ref;
  672. gcc_assert (!edge->indirect_unknown_callee);
  673. edge->speculative_call_info (direct, indirect, ref);
  674. direct->speculative = false;
  675. indirect->speculative = false;
  676. ref->speculative = false;
  677. }
  678. }
  679. if (node->clones)
  680. node = node->clones;
  681. else if (node->next_sibling_clone)
  682. node = node->next_sibling_clone;
  683. else
  684. {
  685. while (node != this && !node->next_sibling_clone)
  686. node = node->clone_of;
  687. if (node != this)
  688. node = node->next_sibling_clone;
  689. }
  690. }
  691. }
  692. /* Like cgraph_create_edge walk the clone tree and update all clones sharing
  693. same function body. If clones already have edge for OLD_STMT; only
  694. update the edge same way as cgraph_set_call_stmt_including_clones does.
  695. TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
  696. frequencies of the clones. */
  697. void
  698. cgraph_node::create_edge_including_clones (cgraph_node *callee,
  699. gimple old_stmt, gcall *stmt,
  700. gcov_type count,
  701. int freq,
  702. cgraph_inline_failed_t reason)
  703. {
  704. cgraph_node *node;
  705. cgraph_edge *edge;
  706. if (!get_edge (stmt))
  707. {
  708. edge = create_edge (callee, stmt, count, freq);
  709. edge->inline_failed = reason;
  710. }
  711. node = clones;
  712. if (node)
  713. while (node != this)
  714. {
  715. cgraph_edge *edge = node->get_edge (old_stmt);
  716. /* It is possible that clones already contain the edge while
  717. master didn't. Either we promoted indirect call into direct
  718. call in the clone or we are processing clones of unreachable
  719. master where edges has been removed. */
  720. if (edge)
  721. edge->set_call_stmt (stmt);
  722. else if (! node->get_edge (stmt))
  723. {
  724. edge = node->create_edge (callee, stmt, count, freq);
  725. edge->inline_failed = reason;
  726. }
  727. if (node->clones)
  728. node = node->clones;
  729. else if (node->next_sibling_clone)
  730. node = node->next_sibling_clone;
  731. else
  732. {
  733. while (node != this && !node->next_sibling_clone)
  734. node = node->clone_of;
  735. if (node != this)
  736. node = node->next_sibling_clone;
  737. }
  738. }
  739. }
  740. /* Remove the node from cgraph and all inline clones inlined into it.
  741. Skip however removal of FORBIDDEN_NODE and return true if it needs to be
  742. removed. This allows to call the function from outer loop walking clone
  743. tree. */
  744. bool
  745. cgraph_node::remove_symbol_and_inline_clones (cgraph_node *forbidden_node)
  746. {
  747. cgraph_edge *e, *next;
  748. bool found = false;
  749. if (this == forbidden_node)
  750. {
  751. callers->remove ();
  752. return true;
  753. }
  754. for (e = callees; e; e = next)
  755. {
  756. next = e->next_callee;
  757. if (!e->inline_failed)
  758. found |= e->callee->remove_symbol_and_inline_clones (forbidden_node);
  759. }
  760. remove ();
  761. return found;
  762. }
  763. /* The edges representing the callers of the NEW_VERSION node were
  764. fixed by cgraph_function_versioning (), now the call_expr in their
  765. respective tree code should be updated to call the NEW_VERSION. */
  766. static void
  767. update_call_expr (cgraph_node *new_version)
  768. {
  769. cgraph_edge *e;
  770. gcc_assert (new_version);
  771. /* Update the call expr on the edges to call the new version. */
  772. for (e = new_version->callers; e; e = e->next_caller)
  773. {
  774. function *inner_function = DECL_STRUCT_FUNCTION (e->caller->decl);
  775. gimple_call_set_fndecl (e->call_stmt, new_version->decl);
  776. maybe_clean_eh_stmt_fn (inner_function, e->call_stmt);
  777. }
  778. }
  779. /* Create a new cgraph node which is the new version of
  780. callgraph node. REDIRECT_CALLERS holds the callers
  781. edges which should be redirected to point to
  782. NEW_VERSION. ALL the callees edges of the node
  783. are cloned to the new version node. Return the new
  784. version node.
  785. If non-NULL BLOCK_TO_COPY determine what basic blocks
  786. was copied to prevent duplications of calls that are dead
  787. in the clone. */
  788. cgraph_node *
  789. cgraph_node::create_version_clone (tree new_decl,
  790. vec<cgraph_edge *> redirect_callers,
  791. bitmap bbs_to_copy)
  792. {
  793. cgraph_node *new_version;
  794. cgraph_edge *e;
  795. unsigned i;
  796. new_version = cgraph_node::create (new_decl);
  797. new_version->analyzed = analyzed;
  798. new_version->definition = definition;
  799. new_version->local = local;
  800. new_version->externally_visible = false;
  801. new_version->no_reorder = no_reorder;
  802. new_version->local.local = new_version->definition;
  803. new_version->global = global;
  804. new_version->rtl = rtl;
  805. new_version->count = count;
  806. for (e = callees; e; e=e->next_callee)
  807. if (!bbs_to_copy
  808. || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
  809. e->clone (new_version, e->call_stmt,
  810. e->lto_stmt_uid, REG_BR_PROB_BASE,
  811. CGRAPH_FREQ_BASE,
  812. true);
  813. for (e = indirect_calls; e; e=e->next_callee)
  814. if (!bbs_to_copy
  815. || bitmap_bit_p (bbs_to_copy, gimple_bb (e->call_stmt)->index))
  816. e->clone (new_version, e->call_stmt,
  817. e->lto_stmt_uid, REG_BR_PROB_BASE,
  818. CGRAPH_FREQ_BASE,
  819. true);
  820. FOR_EACH_VEC_ELT (redirect_callers, i, e)
  821. {
  822. /* Redirect calls to the old version node to point to its new
  823. version. */
  824. e->redirect_callee (new_version);
  825. }
  826. symtab->call_cgraph_duplication_hooks (this, new_version);
  827. return new_version;
  828. }
  829. /* Perform function versioning.
  830. Function versioning includes copying of the tree and
  831. a callgraph update (creating a new cgraph node and updating
  832. its callees and callers).
  833. REDIRECT_CALLERS varray includes the edges to be redirected
  834. to the new version.
  835. TREE_MAP is a mapping of tree nodes we want to replace with
  836. new ones (according to results of prior analysis).
  837. If non-NULL ARGS_TO_SKIP determine function parameters to remove
  838. from new version.
  839. If SKIP_RETURN is true, the new version will return void.
  840. If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
  841. If non_NULL NEW_ENTRY determine new entry BB of the clone.
  842. Return the new version's cgraph node. */
  843. cgraph_node *
  844. cgraph_node::create_version_clone_with_body
  845. (vec<cgraph_edge *> redirect_callers,
  846. vec<ipa_replace_map *, va_gc> *tree_map, bitmap args_to_skip,
  847. bool skip_return, bitmap bbs_to_copy, basic_block new_entry_block,
  848. const char *clone_name)
  849. {
  850. tree old_decl = decl;
  851. cgraph_node *new_version_node = NULL;
  852. tree new_decl;
  853. if (!tree_versionable_function_p (old_decl))
  854. return NULL;
  855. gcc_assert (local.can_change_signature || !args_to_skip);
  856. /* Make a new FUNCTION_DECL tree node for the new version. */
  857. if (!args_to_skip && !skip_return)
  858. new_decl = copy_node (old_decl);
  859. else
  860. new_decl
  861. = build_function_decl_skip_args (old_decl, args_to_skip, skip_return);
  862. /* Generate a new name for the new version. */
  863. DECL_NAME (new_decl) = clone_function_name (old_decl, clone_name);
  864. SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
  865. SET_DECL_RTL (new_decl, NULL);
  866. /* When the old decl was a con-/destructor make sure the clone isn't. */
  867. DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
  868. DECL_STATIC_DESTRUCTOR (new_decl) = 0;
  869. /* Create the new version's call-graph node.
  870. and update the edges of the new node. */
  871. new_version_node = create_version_clone (new_decl, redirect_callers,
  872. bbs_to_copy);
  873. if (ipa_transforms_to_apply.exists ())
  874. new_version_node->ipa_transforms_to_apply
  875. = ipa_transforms_to_apply.copy ();
  876. /* Copy the OLD_VERSION_NODE function tree to the new version. */
  877. tree_function_versioning (old_decl, new_decl, tree_map, false, args_to_skip,
  878. skip_return, bbs_to_copy, new_entry_block);
  879. /* Update the new version's properties.
  880. Make The new version visible only within this translation unit. Make sure
  881. that is not weak also.
  882. ??? We cannot use COMDAT linkage because there is no
  883. ABI support for this. */
  884. new_version_node->make_decl_local ();
  885. DECL_VIRTUAL_P (new_version_node->decl) = 0;
  886. new_version_node->externally_visible = 0;
  887. new_version_node->local.local = 1;
  888. new_version_node->lowered = true;
  889. if (!implicit_section)
  890. new_version_node->set_section (get_section ());
  891. /* Clones of global symbols or symbols with unique names are unique. */
  892. if ((TREE_PUBLIC (old_decl)
  893. && !DECL_EXTERNAL (old_decl)
  894. && !DECL_WEAK (old_decl)
  895. && !DECL_COMDAT (old_decl))
  896. || in_lto_p)
  897. new_version_node->unique_name = true;
  898. /* Update the call_expr on the edges to call the new version node. */
  899. update_call_expr (new_version_node);
  900. symtab->call_cgraph_insertion_hooks (this);
  901. return new_version_node;
  902. }
  903. /* Given virtual clone, turn it into actual clone. */
  904. static void
  905. cgraph_materialize_clone (cgraph_node *node)
  906. {
  907. bitmap_obstack_initialize (NULL);
  908. node->former_clone_of = node->clone_of->decl;
  909. if (node->clone_of->former_clone_of)
  910. node->former_clone_of = node->clone_of->former_clone_of;
  911. /* Copy the OLD_VERSION_NODE function tree to the new version. */
  912. tree_function_versioning (node->clone_of->decl, node->decl,
  913. node->clone.tree_map, true,
  914. node->clone.args_to_skip, false,
  915. NULL, NULL);
  916. if (symtab->dump_file)
  917. {
  918. dump_function_to_file (node->clone_of->decl, symtab->dump_file,
  919. dump_flags);
  920. dump_function_to_file (node->decl, symtab->dump_file, dump_flags);
  921. }
  922. /* Function is no longer clone. */
  923. if (node->next_sibling_clone)
  924. node->next_sibling_clone->prev_sibling_clone = node->prev_sibling_clone;
  925. if (node->prev_sibling_clone)
  926. node->prev_sibling_clone->next_sibling_clone = node->next_sibling_clone;
  927. else
  928. node->clone_of->clones = node->next_sibling_clone;
  929. node->next_sibling_clone = NULL;
  930. node->prev_sibling_clone = NULL;
  931. if (!node->clone_of->analyzed && !node->clone_of->clones)
  932. {
  933. node->clone_of->release_body ();
  934. node->clone_of->remove_callees ();
  935. node->clone_of->remove_all_references ();
  936. }
  937. node->clone_of = NULL;
  938. bitmap_obstack_release (NULL);
  939. }
  940. /* Once all functions from compilation unit are in memory, produce all clones
  941. and update all calls. We might also do this on demand if we don't want to
  942. bring all functions to memory prior compilation, but current WHOPR
  943. implementation does that and it is is bit easier to keep everything right in
  944. this order. */
  945. void
  946. symbol_table::materialize_all_clones (void)
  947. {
  948. cgraph_node *node;
  949. bool stabilized = false;
  950. if (symtab->dump_file)
  951. fprintf (symtab->dump_file, "Materializing clones\n");
  952. #ifdef ENABLE_CHECKING
  953. cgraph_node::verify_cgraph_nodes ();
  954. #endif
  955. /* We can also do topological order, but number of iterations should be
  956. bounded by number of IPA passes since single IPA pass is probably not
  957. going to create clones of clones it created itself. */
  958. while (!stabilized)
  959. {
  960. stabilized = true;
  961. FOR_EACH_FUNCTION (node)
  962. {
  963. if (node->clone_of && node->decl != node->clone_of->decl
  964. && !gimple_has_body_p (node->decl))
  965. {
  966. if (!node->clone_of->clone_of)
  967. node->clone_of->get_untransformed_body ();
  968. if (gimple_has_body_p (node->clone_of->decl))
  969. {
  970. if (symtab->dump_file)
  971. {
  972. fprintf (symtab->dump_file, "cloning %s to %s\n",
  973. xstrdup_for_dump (node->clone_of->name ()),
  974. xstrdup_for_dump (node->name ()));
  975. if (node->clone.tree_map)
  976. {
  977. unsigned int i;
  978. fprintf (symtab->dump_file, " replace map: ");
  979. for (i = 0;
  980. i < vec_safe_length (node->clone.tree_map);
  981. i++)
  982. {
  983. ipa_replace_map *replace_info;
  984. replace_info = (*node->clone.tree_map)[i];
  985. print_generic_expr (symtab->dump_file, replace_info->old_tree, 0);
  986. fprintf (symtab->dump_file, " -> ");
  987. print_generic_expr (symtab->dump_file, replace_info->new_tree, 0);
  988. fprintf (symtab->dump_file, "%s%s;",
  989. replace_info->replace_p ? "(replace)":"",
  990. replace_info->ref_p ? "(ref)":"");
  991. }
  992. fprintf (symtab->dump_file, "\n");
  993. }
  994. if (node->clone.args_to_skip)
  995. {
  996. fprintf (symtab->dump_file, " args_to_skip: ");
  997. dump_bitmap (symtab->dump_file,
  998. node->clone.args_to_skip);
  999. }
  1000. if (node->clone.args_to_skip)
  1001. {
  1002. fprintf (symtab->dump_file, " combined_args_to_skip:");
  1003. dump_bitmap (symtab->dump_file, node->clone.combined_args_to_skip);
  1004. }
  1005. }
  1006. cgraph_materialize_clone (node);
  1007. stabilized = false;
  1008. }
  1009. }
  1010. }
  1011. }
  1012. FOR_EACH_FUNCTION (node)
  1013. if (!node->analyzed && node->callees)
  1014. {
  1015. node->remove_callees ();
  1016. node->remove_all_references ();
  1017. }
  1018. else
  1019. node->clear_stmts_in_references ();
  1020. if (symtab->dump_file)
  1021. fprintf (symtab->dump_file, "Materialization Call site updates done.\n");
  1022. #ifdef ENABLE_CHECKING
  1023. cgraph_node::verify_cgraph_nodes ();
  1024. #endif
  1025. symtab->remove_unreachable_nodes (symtab->dump_file);
  1026. }
  1027. #include "gt-cgraphclones.h"